0001:
0002:
0003:
0004:
0005:
0006:
0007:
0008:
0009: #define STRICT
0010:
0011: #include <windows.h>
0012: #include <d3d8.h>
0013: #include <d3dx8.h>
0014: #include "main.h"
0015: #include "draw.h"
0016:
0017:
0018:
0019:
0020: static LPDIRECT3D8 s_lpD3D = NULL;
0021: static LPDIRECT3DDEVICE8 s_lpD3DDEV = NULL;
0022: static D3DPRESENT_PARAMETERS s_d3dpp;
0023:
0024: static bool s_end;
0025:
0026:
0027:
0028:
0029: int PASCAL WinMain(HINSTANCE hInst,HINSTANCE hPrev,char *CmdLine,int CmdShow);
0030: LRESULT CALLBACK MsgProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam);
0031: HRESULT InitD3D( HWND hWnd );
0032:
0033:
0034:
0035:
0036:
0037:
0038:
0039:
0040:
0041:
0042:
0043:
0044: int PASCAL WinMain(HINSTANCE hInst,HINSTANCE hPrev,char *CmdLine,int CmdShow)
0045: {
0046: s_end = 0;
0047:
0048:
0049: RECT rect;
0050: SetRect(&rect, 0, 0, WIDTH, HEIGHT);
0051: DWORD style = (FULLSCREEN) ? WS_POPUP :(WS_CAPTION|WS_SYSMENU|WS_BORDER|WS_MINIMIZEBOX);
0052: AdjustWindowRect(&rect, style, FALSE);
0053: int width = rect.right - rect.left;
0054: int height = rect.bottom - rect.top;
0055:
0056: WNDCLASS wc;
0057: ZeroMemory(&wc, sizeof(WNDCLASS));
0058: wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
0059: wc.hCursor = LoadCursor(NULL, IDC_ARROW);
0060: wc.hInstance = hInst;
0061: wc.lpfnWndProc = MsgProc;
0062: wc.lpszClassName = CAPTION;
0063: if(RegisterClass(&wc) == 0) return 0;
0064:
0065: HWND hWnd = CreateWindow(CAPTION
0066: ,CAPTION
0067: ,style
0068: ,CW_USEDEFAULT
0069: ,CW_USEDEFAULT
0070: ,width
0071: ,height
0072: ,NULL
0073: ,NULL
0074: ,hInst
0075: ,NULL
0076: );
0077: if(hWnd == NULL) return 0;
0078:
0079:
0080: if(SUCCEEDED(InitD3D(hWnd))){
0081:
0082: if(FAILED(InitRender(s_lpD3DDEV))) s_end = true;
0083:
0084: ShowWindow(hWnd, SW_SHOW);
0085: UpdateWindow(hWnd);
0086:
0087:
0088: MSG msg;
0089: while (!s_end)
0090: {
0091: if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)){
0092:
0093: TranslateMessage(&msg);
0094: DispatchMessage(&msg);
0095: }else{
0096:
0097:
0098: s_lpD3DDEV->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,D3DCOLOR_XRGB(0,0,0),1.0f,0);
0099:
0100: s_lpD3DDEV->BeginScene();
0101:
0102: Render(s_lpD3DDEV);
0103:
0104:
0105: s_lpD3DDEV->EndScene();
0106: if (FAILED(s_lpD3DDEV->Present(NULL,NULL,NULL,NULL)))
0107: s_lpD3DDEV->Reset(&s_d3dpp);
0108: }
0109: }
0110: }
0111:
0112:
0113: CleanRender(s_lpD3DDEV);
0114: RELEASE(s_lpD3DDEV);
0115: RELEASE(s_lpD3D);
0116:
0117: return 0;
0118: }
0119:
0120:
0121:
0122:
0123: LRESULT CALLBACK MsgProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
0124: {
0125: switch (msg){
0126: case WM_KEYDOWN:
0127: case WM_CLOSE:
0128: s_end = true;
0129: return 1;
0130: break;
0131: default:
0132: break;
0133: }
0134: return DefWindowProc(hWnd,msg,wParam,lParam);
0135: }
0136:
0137:
0138:
0139:
0140: HRESULT InitD3D( HWND hWnd )
0141: {
0142:
0143: if (NULL == (s_lpD3D = Direct3DCreate8(D3D_SDK_VERSION))){
0144: MessageBox(NULL,"Direct3D の作成に失敗しました。",CAPTION,MB_OK | MB_ICONSTOP);
0145: return E_FAIL;
0146: }
0147:
0148:
0149: D3DDISPLAYMODE d3ddm;
0150: if( FAILED( s_lpD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &d3ddm ) ) ) return E_FAIL;
0151:
0152:
0153: ZeroMemory(&s_d3dpp, sizeof(D3DPRESENT_PARAMETERS));
0154:
0155: s_d3dpp.BackBufferCount = 1;
0156: if (FULLSCREEN){
0157: s_d3dpp.Windowed = FALSE;
0158: s_d3dpp.BackBufferWidth = WIDTH;
0159: s_d3dpp.BackBufferHeight = HEIGHT;
0160: }else{
0161: s_d3dpp.Windowed = TRUE;
0162: s_d3dpp.BackBufferWidth = WIDTH;
0163: s_d3dpp.BackBufferHeight = HEIGHT;
0164: }
0165:
0166: s_d3dpp.BackBufferFormat = d3ddm.Format;
0167: s_d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
0168: s_d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
0169: s_d3dpp.hDeviceWindow = hWnd;
0170:
0171: s_d3dpp.EnableAutoDepthStencil = TRUE;
0172: s_d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
0173:
0174: D3DCAPS8 caps;
0175: s_lpD3D->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps);
0176: if(caps.VertexShaderVersion < D3DVS_VERSION(1,0)){
0177:
0178: if(FAILED(s_lpD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,hWnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&s_d3dpp,&s_lpD3DDEV))){
0179: if(FAILED(s_lpD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_REF,hWnd,D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE,&s_d3dpp,&s_lpD3DDEV))){
0180: return FALSE;
0181: }
0182: }
0183: }else{
0184:
0185: if (FAILED(s_lpD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,hWnd,D3DCREATE_HARDWARE_VERTEXPROCESSING,&s_d3dpp,&s_lpD3DDEV))){
0186:
0187: if (FAILED(s_lpD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,hWnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&s_d3dpp,&s_lpD3DDEV))){
0188:
0189: if (FAILED(s_lpD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_REF,hWnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&s_d3dpp,&s_lpD3DDEV))){
0190:
0191: MessageBox(NULL,"Direct3D の初期化に失敗しました。",CAPTION,MB_OK | MB_ICONSTOP);
0192: return E_FAIL;
0193: }
0194: }
0195: }
0196: }
0197: return S_OK;
0198: }
0199: