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: extern float a;
0027: extern float b;
0028:
0029:
0030:
0031:
0032:
0033: int PASCAL WinMain(HINSTANCE hInst,HINSTANCE hPrev,char *CmdLine,int CmdShow);
0034: LRESULT CALLBACK MsgProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam);
0035: HRESULT InitD3D( HWND hWnd );
0036:
0037:
0038:
0039:
0040:
0041:
0042:
0043:
0044:
0045:
0046:
0047:
0048: int PASCAL WinMain(HINSTANCE hInst,HINSTANCE hPrev,char *CmdLine,int CmdShow)
0049: {
0050: s_end = 0;
0051:
0052:
0053: RECT rect;
0054: SetRect(&rect, 0, 0, WIDTH, HEIGHT);
0055: DWORD style = (FULLSCREEN) ? WS_POPUP :(WS_CAPTION|WS_SYSMENU|WS_BORDER|WS_MINIMIZEBOX);
0056: AdjustWindowRect(&rect, style, FALSE);
0057: int width = rect.right - rect.left;
0058: int height = rect.bottom - rect.top;
0059:
0060: WNDCLASS wc;
0061: ZeroMemory(&wc, sizeof(WNDCLASS));
0062: wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
0063: wc.hCursor = LoadCursor(NULL, IDC_ARROW);
0064: wc.hInstance = hInst;
0065: wc.lpfnWndProc = MsgProc;
0066: wc.lpszClassName = CAPTION;
0067: if(RegisterClass(&wc) == 0) return 0;
0068:
0069: HWND hWnd = CreateWindow(CAPTION
0070: ,CAPTION
0071: ,style
0072: ,CW_USEDEFAULT
0073: ,CW_USEDEFAULT
0074: ,width
0075: ,height
0076: ,NULL
0077: ,NULL
0078: ,hInst
0079: ,NULL
0080: );
0081: if(hWnd == NULL) return 0;
0082:
0083:
0084: if(SUCCEEDED(InitD3D(hWnd))){
0085:
0086: if(FAILED(InitRender(s_lpD3DDEV))) s_end = true;
0087:
0088: ShowWindow(hWnd, SW_SHOW);
0089: UpdateWindow(hWnd);
0090:
0091:
0092: MSG msg;
0093: while (!s_end)
0094: {
0095: if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)){
0096:
0097: TranslateMessage(&msg);
0098: DispatchMessage(&msg);
0099: }else{
0100:
0101:
0102:
0103:
0104: s_lpD3DDEV->BeginScene();
0105:
0106: Render(s_lpD3DDEV);
0107:
0108:
0109: s_lpD3DDEV->EndScene();
0110: if (FAILED(s_lpD3DDEV->Present(NULL,NULL,NULL,NULL)))
0111: s_lpD3DDEV->Reset(&s_d3dpp);
0112: }
0113: }
0114: }
0115:
0116:
0117: CleanRender(s_lpD3DDEV);
0118: RELEASE(s_lpD3DDEV);
0119: RELEASE(s_lpD3D);
0120:
0121: return 0;
0122: }
0123:
0124:
0125:
0126:
0127: LRESULT CALLBACK MsgProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
0128: {
0129: switch (msg){
0130: case WM_KEYDOWN:
0131: case WM_CLOSE:
0132: s_end = true;
0133: return 1;
0134: break;
0135: default:
0136: break;
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: D3DCAPS8 caps;
0157: s_lpD3D->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps);
0158:
0159:
0160: ZeroMemory(&s_d3dpp, sizeof(D3DPRESENT_PARAMETERS));
0161:
0162: s_d3dpp.BackBufferCount = 1;
0163: if (FULLSCREEN){
0164: s_d3dpp.Windowed = FALSE;
0165: s_d3dpp.BackBufferWidth = WIDTH;
0166: s_d3dpp.BackBufferHeight = HEIGHT;
0167: }else{
0168: s_d3dpp.Windowed = TRUE;
0169: s_d3dpp.BackBufferWidth = WIDTH;
0170: s_d3dpp.BackBufferHeight = HEIGHT;
0171: }
0172:
0173: s_d3dpp.BackBufferFormat = d3ddm.Format;
0174: s_d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
0175: s_d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
0176: s_d3dpp.hDeviceWindow = hWnd;
0177:
0178: s_d3dpp.EnableAutoDepthStencil = TRUE;
0179: if( FAILED( s_lpD3D->CheckDeviceFormat( caps.AdapterOrdinal,
0180: caps.DeviceType, d3ddm.Format,
0181: D3DUSAGE_DEPTHSTENCIL,
0182: D3DRTYPE_SURFACE,
0183: D3DFMT_D24S8 ) ) ){
0184: s_d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
0185: }else{
0186: s_d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
0187: }
0188:
0189:
0190: if(FAILED(s_lpD3D->CheckDeviceFormat(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,D3DFMT_X8R8G8B8,
0191: D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE,s_d3dpp.AutoDepthStencilFormat)))
0192: return -1;
0193:
0194: DWORD vs[] = {D3DCREATE_HARDWARE_VERTEXPROCESSING, D3DCREATE_MIXED_VERTEXPROCESSING, D3DCREATE_SOFTWARE_VERTEXPROCESSING,0 };
0195: D3DDEVTYPE ps[] = {D3DDEVTYPE_HAL, D3DDEVTYPE_REF, (D3DDEVTYPE)0};
0196:
0197: DWORD v = (caps.VertexShaderVersion < D3DVS_VERSION(1,1)) ? 1 : 0;
0198: DWORD p = (caps.PixelShaderVersion < D3DPS_VERSION(1,1)) ? 1 : 0;
0199:
0200:
0201: if( 0 == ( caps.TextureCaps & D3DPTEXTURECAPS_CUBEMAP ) ){
0202: p=1;
0203: }
0204:
0205: for(;ps[p];p++){
0206: for(;vs[v];v++){
0207: if(SUCCEEDED(s_lpD3D->CreateDevice(D3DADAPTER_DEFAULT, ps[p],hWnd,vs[v],&s_d3dpp,&s_lpD3DDEV))) goto set_up;
0208: }
0209: }
0210: set_up:
0211: return S_OK;
0212: }
0213: