Skip to content

Instantly share code, notes, and snippets.

@Komzpa
Created November 16, 2014 20:45
Show Gist options
  • Save Komzpa/a0d54f70fa8d3927738d to your computer and use it in GitHub Desktop.
Save Komzpa/a0d54f70fa8d3927738d to your computer and use it in GitHub Desktop.
#include "NewGameMenu.h"
const
int
BUTTON_HEIGHT
=
80
;
const
int
BUTTON_WIDTH
=
128
;
void
NewGameMenu::createVertexBuffer
(
)
{
if
(
FAILED
(
pD3DDevice->CreateVertexBuffer
(
4*sizeof
(
XZGAMEMENUVERTEX
)
,
0
,
D3DFVF_XZGAMEMENUVERTEX
,
D3DPOOL_DEFAULT
,
&pVertices
,
NULL
)
)
)
{
ExceptionBase err
(
VERTEXBUFFERCREATIONERROR
,
"Не могу создать Vertex Buffer"
)
;
throw err
;
}
XZGAMEMENUVERTEX *pVer
;
if
(
FAILED
(
pVertices->Lock
(
0
,
0
,
(
void**
)
&pVer
,
0
)
)
)
{
ExceptionBase err
(
VERTEXBUFFERLOCKERROR
,
"Ошибка при вызове метода Lock"
)
;
throw err
;
}
pVer[0].position
=
D3DXVECTOR3
(
-0.5f
,
0.0f
,
-
0.5f
)
;
pVer[0].vecNormal
=
D3DXVECTOR3
(
0.0f
,
1.0f
,
0.0f
)
;
pVer[0].tu
=
0.0f
;
pVer[0].tv
=
1.0f
;
pVer[1].position
=
D3DXVECTOR3
(
-0.5f
,
0.0f
,
0.5f
)
;
pVer[1].vecNormal
=
D3DXVECTOR3
(
0.0f
,
1.0f
,
0.0f
)
;
pVer[1].tu
=
0.0f
;
pVer[1].tv
=
0.0f
;
pVer[2].position
=
D3DXVECTOR3
(
0.5f
,
0.0f
,
-
0.5f
)
;
pVer[2].vecNormal
=
D3DXVECTOR3
(
0.0f
,
1.0f
,
0.0f
)
;
pVer[2].tu
=
1.0f
;
pVer[2].tv
=
1.0f
;
pVer[3].position
=
D3DXVECTOR3
(
0.5f
,
0.0f
,
0.5f
)
;
pVer[3].vecNormal
=
D3DXVECTOR3
(
0.0f
,
1.0f
,
0.0f
)
;
pVer[3].tu
=
1.0f
;
pVer[3].tv
=
0.0f
;
pVertices->Unlock
(
)
;
}
void
NewGameMenu::loadTextures
(
char
**filesNames
)
{
textures
=
new LPDIRECT3DTEXTURE9[texNumber]
;
for
(
int
i
=
0
;
i < texNumber
;
i++
)
{
if
(
FAILED
(
D3DXCreateTextureFromFile
(
pD3DDevice
,
filesNames[i]
,
&textures[i]
)
)
)
{
char
*mes
=
"Не могу загрузить текстуру: "
;
size_t
len
=
strlen
(
mes
)
+
strlen
(
filesNames[i]
)
;
char
*fullMes
=
new
char
[len
+
1]
;
strcpy
(
fullMes
,
mes
)
;
strcat
(
fullMes
,
filesNames[i]
)
;
ExceptionBase err
(
TEXTURELOADERROR
,
fullMes
)
;
delete [] fullMes
;
throw err
;
}
}
}
NewGameMenu::NewGameMenu
(
LPDIRECT3DDEVICE9 pD3DDev
,
D3DXVECTOR3 position
,
char
**files
,
int
filesNumber
,
float
width
,
float
height
,
RECT menuRectangle
)
:
pD3DDevice
(
pD3DDev
)
,
pos
(
position
)
,
pVertices
(
NULL
)
,
textures
(
NULL
)
,
curTex
(
NEW_GAME
)
,
menuRect
(
menuRectangle
)
{
if
(
width <= 0
||
height <= 0
||
filesNumber <= 0
)
{
ExceptionBase err
(
INCORRECTPARAMETERERROR
,
"Не допустимый параметр"
)
;
throw err
;
}
texNumber
=
filesNumber
;
createVertexBuffer
(
)
;
loadTextures
(
files
)
;
pD3DDevice->SetTextureStageState
(
0
,
D3DTSS_COLOROP
,
D3DTOP_SELECTARG1
)
;
pD3DDevice->SetTextureStageState
(
0
,
D3DTSS_COLORARG1
,
D3DTA_TEXTURE
)
;
pD3DDevice->SetTextureStageState
(
0
,
D3DTSS_ALPHAOP
,
D3DTOP_SELECTARG1
)
;
pD3DDevice->SetTextureStageState
(
0
,
D3DTSS_ALPHAARG1
,
D3DTA_TEXTURE
)
;
}
NewGameMenu::~NewGameMenu
(
void
)
{
if
(
pVertices != NULL
)
pVertices->Release
(
)
;
if
(
textures != NULL
)
{
for
(
int
i
=
0
;
i < texNumber
;
i++
)
{
textures[i]->Release
(
)
;
}
delete [] textures
;
}
}
void
NewGameMenu::render
(
)
{
if
(
FAILED
(
pD3DDevice->SetTexture
(
0
,
textures[curTex]
)
)
)
{
ExceptionBase err
(
SETTEXTUREERROR
,
"Не могу установить текстуру"
)
;
throw err
;
}
pD3DDevice->SetStreamSource
(
0
,
pVertices
,
0
,
sizeof
(
XZGAMEMENUVERTEX
)
)
;
pD3DDevice->SetFVF
(
D3DFVF_XZGAMEMENUVERTEX
)
;
pD3DDevice->DrawPrimitive
(
D3DPT_TRIANGLESTRIP
,
0
,
2
)
;
pD3DDevice->SetTexture
(
0
,
NULL
)
;
}
void
NewGameMenu::setCurrentMenu
(
int
curMenu
)
{
if
(
curMenu < 0
&&
curMenu >= texNumber
)
{
ExceptionBase err
(
INCORRECTPARAMETERERROR
,
"Не известное меню"
)
;
throw err
;
}
curTex
=
curMenu
;
}
int
NewGameMenu::checkSelectedButtons
(
POINT cursorPos
)
{
if
(
cursorPos.x > menuRect.left
&&
cursorPos.x < menuRect.right
&&
cursorPos.y < menuRect.bottom
&&
cursorPos.y > menuRect.top
+
(
menuRect.bottom
-
menuRect.top
-
BUTTON_HEIGHT
)
)
{
if
(
cursorPos.x > menuRect.left
+
BUTTON_WIDTH
)
{
return NO
;
}
else
{
return YES
;
}
}
return NOTSELECTED
;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment