00001
00002
00003
00004 #include "main.h"
00005 #include "DrawMain.h"
00006
00007
00008 #ifndef __NOT_USE_DIRECTX
00009
00010
00011
00012
00013 #include <d3d9.h>
00014 #include <d3dx9.h>
00015
00016
00017
00018
00019 #pragma comment(lib, "d3d9.lib")
00020 #pragma comment(lib, "d3dx9.lib")
00021
00022
00023
00024
00025 LPDIRECT3D9 pD3D = NULL;
00026 LPDIRECT3DDEVICE9 pD3DDevice = NULL;
00027 LPD3DXSPRITE pSprite = NULL;
00028 LPD3DXFONT lpFont = NULL;
00029 D3DPRESENT_PARAMETERS d3dpp;
00030
00031 #endif
00032
00033
00034
00035
00036 HWND hWindow = NULL;
00037 HINSTANCE hInstance = NULL;
00038 RECT rcClientRect;
00039
00040
00041
00042
00043 #define WINDOW_WIDTH 640
00044 #define WINDOW_HEIGHT 480
00045
00046
00047
00048
00049
00050 LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
00051
00052
00053 #ifndef __NOT_USE_DIRECTX
00054
00055 HRESULT InitD3D(HWND hWnd);
00056 void ReleaseD3D();
00057 void InitRenderState();
00058 void InitAnotherState();
00059
00060 #endif
00061
00062
00073
00074 int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR pCmdLine, int nCmdShow)
00075 {
00076 HWND hWnd = NULL;
00077 MSG msg;
00078
00079
00080 static char szAppName[] = "実験用サンプル";
00081 WNDCLASSEX wc;
00082 wc.cbSize = sizeof(wc);
00083 wc.style = CS_HREDRAW | CS_VREDRAW;
00084 wc.lpfnWndProc = WndProc;
00085 wc.cbClsExtra = 0;
00086 wc.cbWndExtra = 0;
00087 wc.hInstance = hInst;
00088 wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
00089 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
00090 wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
00091 wc.lpszMenuName = NULL;
00092 wc.lpszClassName = szAppName;
00093 wc.hIconSm = LoadIcon(NULL, IDI_ASTERISK);
00094
00095
00096 if(!RegisterClassEx(&wc))
00097 return 0;
00098
00099
00100 rcClientRect.right = WINDOW_WIDTH;
00101 rcClientRect.bottom = WINDOW_HEIGHT;
00102 AdjustWindowRect(&rcClientRect,
00103 WS_OVERLAPPEDWINDOW,
00104 FALSE
00105 );
00106
00107
00108 hWnd = CreateWindow(szAppName,
00109 szAppName,
00110 WS_OVERLAPPEDWINDOW,
00111 CW_USEDEFAULT, CW_USEDEFAULT,
00112 rcClientRect.right - rcClientRect.left,
00113 rcClientRect.bottom - rcClientRect.top,
00114 NULL, NULL, hInst, NULL);
00115 if(hWnd == NULL)
00116 return 0;
00117
00118 ShowWindow(hWnd, SW_SHOW);
00119 UpdateWindow(hWnd);
00120
00121 hWindow = hWnd;
00122 hInstance = hInst;
00123
00124 #ifndef __NOT_USE_DIRECTX
00125
00126 if(FAILED(InitD3D(hWnd)))
00127 {
00128 return 0;
00129 }
00130 #endif
00131
00132
00133 FrameInit();
00134
00135
00136 ZeroMemory(&msg, sizeof(msg));
00137 while(msg.message != WM_QUIT)
00138 {
00139
00140 if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
00141 {
00142 TranslateMessage(&msg);
00143 DispatchMessage(&msg);
00144 }
00145 else
00146 {
00147
00148 DrawFrame();
00149
00150 Sleep(1);
00151 }
00152 }
00153
00154
00155 FrameRelease();
00156 #ifndef __NOT_USE_DIRECTX
00157
00158 ReleaseD3D();
00159 #endif
00160
00161 return (int)msg.wParam;
00162 }
00163
00164
00165
00166
00176
00177 LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
00178 {
00179
00180 if(!MyWndProc(hWnd,uMsg,wParam,lParam))
00181 return 0;
00182
00183 switch(uMsg)
00184 {
00185
00186 case WM_DESTROY:
00187 PostQuitMessage(0);
00188 return 0;
00189
00190 case WM_KEYDOWN:
00191 if(wParam == VK_ESCAPE)
00192 PostMessage(hWnd,WM_CLOSE,0,0);
00193 return 0;;
00194 }
00195
00196
00197 return DefWindowProc(hWnd, uMsg, wParam, lParam);
00198 }
00199
00200
00201 #ifndef __NOT_USE_DIRECTX
00202
00203
00209
00210 HRESULT InitD3D(HWND hWnd)
00211 {
00212
00213 pD3D = Direct3DCreate9(D3D_SDK_VERSION);
00214 if(pD3D == NULL)
00215 {
00216 MessageBox(0, "Direct3Dの作成に失敗しました", "", MB_OK);
00217 return E_FAIL;
00218 }
00219
00220
00221 ZeroMemory(&d3dpp, sizeof(d3dpp));
00222 d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
00223 d3dpp.BackBufferCount = 1;
00224 d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
00225 d3dpp.Windowed = TRUE;
00226 d3dpp.EnableAutoDepthStencil = TRUE;
00227 d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
00228 d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250 if(FAILED(pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
00251 D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &pD3DDevice)))
00252 {
00253
00254 if(FAILED(pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
00255 D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pD3DDevice)))
00256 {
00257
00258 MessageBox(0, "HALモードでDirect3Dデバイスを作成できません¥nREFモードで再試行します", NULL, MB_OK);
00259 if(FAILED(pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, hWnd,
00260 D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &pD3DDevice)))
00261 {
00262
00263 if(FAILED(pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, hWnd,
00264 D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pD3DDevice)))
00265 {
00266 MessageBox(0, "Direct3Dデバイスの作成に失敗しました", NULL, MB_OK);
00267 return E_FAIL;
00268 }
00269 }
00270 }
00271 }
00272
00273
00274 InitRenderState();
00275
00276 InitAnotherState();
00277
00278 return S_OK;
00279 }
00280
00281
00286
00287 void InitRenderState()
00288 {
00289
00290
00291 pD3DDevice->SetRenderState(D3DRS_ZENABLE,D3DZB_TRUE);
00292 pD3DDevice->SetRenderState(D3DRS_ZWRITEENABLE,TRUE);
00293
00294
00295 pD3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE,TRUE);
00296
00297
00298 pD3DDevice->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_SRCALPHA);
00299 pD3DDevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA);
00300
00301
00302 pD3DDevice->SetTextureStageState(0,D3DTSS_ALPHAOP,D3DTOP_MODULATE);
00303
00304 pD3DDevice->SetTextureStageState(0,D3DTSS_ALPHAARG1,D3DTA_TEXTURE);
00305
00306 pD3DDevice->SetTextureStageState(0,D3DTSS_ALPHAARG2,D3DTA_DIFFUSE);
00307
00308 pD3DDevice->SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_MODULATE);
00309
00310
00311 pD3DDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);
00312
00313
00314
00315
00316 pD3DDevice->SetSamplerState(0,D3DSAMP_MIPFILTER,D3DTEXF_LINEAR);
00317 pD3DDevice->SetSamplerState(0,D3DSAMP_MINFILTER,D3DTEXF_LINEAR);
00318 pD3DDevice->SetSamplerState(0,D3DSAMP_MAGFILTER,D3DTEXF_LINEAR);
00319
00320
00321 pD3DDevice->SetRenderState(D3DRS_LIGHTING,TRUE);
00322
00323
00324
00325
00326
00327 }
00328
00329
00333
00334 void InitAnotherState()
00335 {
00336
00337 D3DLIGHT9 Light;
00338 ZeroMemory(&Light,sizeof(D3DLIGHT9));
00339 Light.Type = D3DLIGHT_DIRECTIONAL;
00340 Light.Diffuse.a = 1.0f;
00341 Light.Diffuse.r = 1.0f;
00342 Light.Diffuse.g = 1.0f;
00343 Light.Diffuse.b = 1.0f;
00344 Light.Direction = D3DXVECTOR3(1.0f,-1.0f,-1.0f);
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356 pD3DDevice->SetLight(0,&Light);
00357
00358 pD3DDevice->LightEnable(0,TRUE);
00359
00360
00361 D3DXCreateSprite(pD3DDevice,&pSprite);
00362
00363
00364 D3DXCreateFont(pD3DDevice,16,0,1000,1,
00365 FALSE,
00366 SHIFTJIS_CHARSET,OUT_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH,
00367 NULL,
00368 &lpFont
00369 );
00370 lpFont->OnResetDevice();
00371 }
00372
00373
00377
00378 void ReleaseD3D()
00379 {
00380 SAFE_RELEASE(lpFont);
00381 SAFE_RELEASE(pSprite);
00382 SAFE_RELEASE(pD3DDevice);
00383 SAFE_RELEASE(pD3D);
00384 }
00385
00386 #endif