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