#include <windows.h>
#include <d3dx9.h>
#include <mmsystem.h>
#include "input.h"
#pragma once
#pragma comment (lib,"winmm.lib")
#pragma comment(lib,"d3d9.lib")
#pragma comment(lib,"d3dx9.lib")
#define FVF_VERTEX_2D (D3DFVF_XYZRHW|D3DFVF_DIFFUSE|D3DFVF_TEX1)
LPDIRECT3D9 g_pD3D = NULL;
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL;
LPDIRECT3DTEXTURE9 g_pd3dTexture = NULL;
LPDIRECT3DTEXTURE9 g_pd3dTexture1 = NULL;
LPDIRECT3DTEXTURE9 g_pd3dTexture2 = NULL;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HRESULT InitD3D(HWND);
void DrawD3D(void);
void DestroyD3D(void);
void drawhaikei();
void openig();
void ending();
enum {
TITLE,
GAME_PLAY,
RESULT,
};
int game_status = TITLE;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hWnd;
MSG msg;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.hInstance = hInstance;
wc.lpszClassName = "TEST";
wc.lpfnWndProc = (WNDPROC)WndProc;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszMenuName = NULL;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
if (!RegisterClassEx(&wc))
{
MessageBox(NULL, "ウインドウクラス構造体の初期化エラー", "", MB_OK);
}
hWnd = CreateWindow(wc.lpszClassName,
"DirextX9",
WS_CAPTION | WS_SYSMENU,
CW_USEDEFAULT,
CW_USEDEFAULT,
640,
480,
NULL,
NULL,
hInstance,
NULL);
if (FAILED(InitD3D(hWnd))) return -1;
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
InitInput(hInstance, hWnd);
while (1)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
{
if (GetMessage(&msg, NULL, 0, 0) == 0)
{
break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else {
UpdateInput();
DrawD3D();
}
}
UninitInput();
DestroyD3D();
return (int)msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) {
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_CLOSE:
int id;
id = MessageBox(NULL, "閉じますか?", "終了確認", MB_OKCANCEL | MB_ICONINFORMATION);
switch (id) {
case IDOK:
PostQuitMessage(0);
break;
case IDCANCEL:
break;
}
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
HRESULT InitD3D(HWND hWnd)
{
D3DPRESENT_PARAMETERS d3dpp;
if (NULL == (g_pD3D = Direct3DCreate9(D3D_SDK_VERSION)))
{
return E_FAIL;
}
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
if (FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hWnd,
D3DCREATE_HARDWARE_VERTEXPROCESSING,
&d3dpp,
&g_pd3dDevice)))
{
return E_FAIL;
}
g_pd3dDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
g_pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
g_pd3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
g_pd3dDevice->LightEnable(0, FALSE);
return S_OK;
}
void DrawD3D(void)
{
g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(1, 1, 255), 1.0f, 0);
if (SUCCEEDED(g_pd3dDevice->BeginScene()))
{
g_pd3dDevice->SetFVF(FVF_VERTEX_2D);
D3DXCreateTextureFromFile(g_pd3dDevice, "title.png", &g_pd3dTexture);
if (FAILED(D3DXCreateTextureFromFile(g_pd3dDevice, "dxtitle.png", &g_pd3dTexture)))
{
//失敗処理
}
D3DXCreateTextureFromFile(g_pd3dDevice, "mgame.png", &g_pd3dTexture1);
if (FAILED(D3DXCreateTextureFromFile(g_pd3dDevice, "game.png", &g_pd3dTexture1)))
{
//失敗処理
}
D3DXCreateTextureFromFile(g_pd3dDevice, "result.tga", &g_pd3dTexture2);
if (FAILED(D3DXCreateTextureFromFile(g_pd3dDevice, "result.tga", &g_pd3dTexture2)))
{
//失敗処理
}
switch (game_status){
case TITLE:
g_pd3dDevice->SetTexture(0, g_pd3dTexture);
drawhaikei();
if (GetKeyboardTrigger(DIK_Z)){
game_status = GAME_PLAY;
}
break;
case GAME_PLAY:
g_pd3dDevice->SetTexture(0, g_pd3dTexture1);
drawhaikei();
if (GetKeyboardTrigger(DIK_Z)){
game_status = RESULT;
}
break;
case RESULT:
g_pd3dDevice->SetTexture(0, g_pd3dTexture2);
drawhaikei();
if (GetKeyboardTrigger(DIK_Z)){
game_status = TITLE;
}
break;
}
g_pd3dDevice->EndScene();
}
g_pd3dDevice->Present(NULL, NULL, NULL, NULL);
}
void DestroyD3D(void)
{
if (g_pd3dDevice != NULL)
{
g_pd3dDevice->Release();
}
if (g_pD3D != NULL)
{
g_pD3D->Release();
}
if (g_pd3dTexture != NULL) {
g_pd3dTexture->Release();
}
if (g_pd3dTexture1 != NULL) {
g_pd3dTexture1->Release();
}
if (g_pd3dTexture2 != NULL) {
g_pd3dTexture2->Release();
}
}
void drawhaikei()
{
typedef struct {
float x, y, z;
float rhw;
D3DCOLOR cor;
float tu, tv;
}HAIKEI2D;
HAIKEI2D haikei[4];
haikei[0].x = 0.0f;
haikei[0].y = 0.0f;
haikei[0].z = 0.0f;
haikei[1].x = 640.0f;
haikei[1].y = 0.0f;
haikei[1].z = 0.0f;
haikei[2].x = 640.0f;
haikei[2].y = 480.0f;
haikei[0].z = 0.0f;
haikei[3].x = 0.0f;
haikei[3].y = 480.0f;
haikei[3].z = 0.0f;
haikei[0].rhw = 1.0f;
haikei[1].rhw = 1.0f;
haikei[2].rhw = 1.0f;
haikei[3].rhw = 1.0f;
haikei[0].cor = D3DCOLOR_XRGB(255, 255, 255);
haikei[1].cor = D3DCOLOR_XRGB(255, 255, 255);
haikei[2].cor = D3DCOLOR_XRGB(255, 255, 255);
haikei[3].cor = D3DCOLOR_XRGB(255, 255, 255);
haikei[0].tu = 0.0f;
haikei[0].tv = 0.0f;
haikei[1].tu = 1.0f;
haikei[1].tv = 0.0f;
haikei[2].tu = 1.0f;
haikei[2].tv = 1.0f;
haikei[3].tu = 0.0f;
haikei[3].tv = 1.0f;
g_pd3dDevice->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, haikei, sizeof(HAIKEI2D));
}
時間がたつと長押ししなくても遷移できるようになります。
なぜですか?