0001: // ----------------------------------------------------------------------------
0002: //
0003: // main.cpp - 最初のファイル
0004: // 
0005: // Copyright (c) 2002 IMAGIRE Takashi (imagire@gmail.com)
0006: // All Rights Reserved.
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;      // the flag to judge the 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: // Name: WinMain()
0037: //-----------------------------------------------------------------------------
0038: int PASCAL WinMain(HINSTANCE hInst,HINSTANCE hPrev,char *CmdLine,int CmdShow)
0039: {
0040:     s_end = 0;
0041:     
0042:     srand(timeGetTime());
0043: 
0044:     // ウィンドウの生成
0045:     RECT    rect;
0046:     SetRect(&rect, 0, 0, WIDTH, HEIGHT);
0047:     DWORD   style = (FULLSCREEN) ? WS_POPUP :(WS_CAPTION|WS_SYSMENU|WS_BORDER|WS_MINIMIZEBOX);
0048:     AdjustWindowRect(&rect, style, FALSE);
0049:     int width  = rect.right - rect.left;
0050:     int height = rect.bottom - rect.top;    
0051:     
0052:     WNDCLASS wc;
0053:     ZeroMemory(&wc, sizeof(WNDCLASS));
0054:     wc.hbrBackground    = (HBRUSH)GetStockObject(BLACK_BRUSH);
0055:     wc.hCursor          = LoadCursor(NULL, IDC_ARROW);
0056:     wc.hInstance        = hInst;
0057:     wc.lpfnWndProc      = MsgProc;
0058:     wc.lpszClassName    = CAPTION;
0059:     if(RegisterClass(&wc) == 0) return 0;
0060:     
0061:     HWND hWnd = CreateWindow(CAPTION        // class name
0062:                             ,"Collision Shader - [Left-click]:Shoot [Right-Drag]:Camera Control [SPACE]:reset"      // window name
0063:                             ,style          // window style
0064:                             ,CW_USEDEFAULT
0065:                             ,CW_USEDEFAULT
0066:                             ,width
0067:                             ,height
0068:                             ,NULL
0069:                             ,NULL
0070:                             ,hInst
0071:                             ,NULL
0072:                             );
0073:     if(hWnd == NULL) return 0;
0074:     
0075:     // Initialization of the Direct3D
0076:     if(SUCCEEDED(InitD3D(hWnd))){
0077:         
0078:         if(FAILED(InitRender(s_lpD3DDEV))) s_end = true;
0079:         
0080:         ShowWindow(hWnd, SW_SHOW);
0081:         UpdateWindow(hWnd);
0082:     
0083:         // The main loop
0084:         MSG msg;
0085:         while (!s_end)
0086:         {
0087:             if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)){
0088:                 TranslateMessage(&msg);
0089:                 DispatchMessage(&msg);
0090:             }else{
0091:                 s_lpD3DDEV->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,D3DCOLOR_XRGB(0,0,0),1.0f,0);
0092:                 s_lpD3DDEV->BeginScene();
0093:                 
0094:                 Render(s_lpD3DDEV);
0095:                 
0096:                 s_lpD3DDEV->EndScene();
0097:                 if (FAILED(s_lpD3DDEV->Present(NULL,NULL,NULL,NULL)))
0098:                     s_lpD3DDEV->Reset(&s_d3dpp);
0099:             }
0100:         }
0101:     }
0102:     
0103:     // END
0104:     CleanRender(s_lpD3DDEV);
0105:     RELEASE(s_lpD3DDEV);
0106:     RELEASE(s_lpD3D);
0107:     
0108:     return 0;
0109: }
0110: // ----------------------------------------------------------------------------
0111: // Name: MsgProc()
0112: //-----------------------------------------------------------------------------
0113: LRESULT CALLBACK MsgProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
0114: {
0115:     switch (msg){
0116:     case WM_CLOSE:
0117:         s_end = true;
0118:         return 1;
0119:         break;
0120:     default:
0121:         break;
0122:     }
0123: 
0124:     return DefWindowProc(hWnd,msg,wParam,lParam);
0125: }
0126: //-----------------------------------------------------------------------------
0127: // Name: InitD3D()
0128: //-----------------------------------------------------------------------------
0129: HRESULT InitD3D( HWND hWnd )
0130: {
0131:     // Create Direct3D Object
0132:     if (NULL == (s_lpD3D = Direct3DCreate8(D3D_SDK_VERSION))){
0133:         MessageBox(NULL,"Direct3D の作成に失敗しました。",CAPTION,MB_OK | MB_ICONSTOP);
0134:         return E_FAIL;
0135:     }
0136: 
0137:     // Get the video mode
0138:     D3DDISPLAYMODE d3ddm;
0139:     if( FAILED( s_lpD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &d3ddm ) ) ) return E_FAIL;
0140:     // Seach the ability of the video card
0141:     D3DCAPS8 caps;
0142:     s_lpD3D->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps);
0143: 
0144:     ZeroMemory(&s_d3dpp, sizeof(D3DPRESENT_PARAMETERS));
0145: 
0146:     s_d3dpp.BackBufferCount = 1;
0147:     if (FULLSCREEN){
0148:         s_d3dpp.Windowed = FALSE;
0149:         s_d3dpp.BackBufferWidth = WIDTH;
0150:         s_d3dpp.BackBufferHeight = HEIGHT;
0151:     }else{
0152:         s_d3dpp.Windowed = TRUE;
0153:         s_d3dpp.BackBufferWidth = WIDTH;
0154:         s_d3dpp.BackBufferHeight = HEIGHT;
0155:     }
0156:     s_d3dpp.BackBufferFormat = d3ddm.Format;
0157:     s_d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
0158:     s_d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
0159:     s_d3dpp.hDeviceWindow = hWnd;
0160:     // Judge the z-buffer
0161:     s_d3dpp.EnableAutoDepthStencil = TRUE;
0162:     if( FAILED( s_lpD3D->CheckDeviceFormat( caps.AdapterOrdinal, 
0163:                                            caps.DeviceType, d3ddm.Format, 
0164:                                            D3DUSAGE_DEPTHSTENCIL, 
0165:                                            D3DRTYPE_SURFACE,
0166:                                            D3DFMT_D24S8 ) ) ){
0167:         s_d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
0168:     }else{
0169:         s_d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
0170:     }
0171:  
0172:     DWORD  vs[] = {D3DCREATE_HARDWARE_VERTEXPROCESSING, D3DCREATE_MIXED_VERTEXPROCESSING, D3DCREATE_SOFTWARE_VERTEXPROCESSING,0 };
0173:     D3DDEVTYPE ps[] = {D3DDEVTYPE_HAL, D3DDEVTYPE_REF, (D3DDEVTYPE)0};
0174:     
0175:     DWORD v = (caps.VertexShaderVersion < D3DVS_VERSION(1,0)) ? 1 : 0;
0176:     DWORD p = (caps.PixelShaderVersion  < D3DPS_VERSION(1,0)) ? 1 : 0;
0177: 
0178:     for(;ps[p];p++){
0179:     for(;vs[v];v++){
0180:         if(SUCCEEDED(s_lpD3D->CreateDevice(D3DADAPTER_DEFAULT, ps[p],hWnd,vs[v],&s_d3dpp,&s_lpD3DDEV))) goto set_up;
0181:     }
0182:     }
0183: set_up:
0184:     return S_OK;
0185: }
0186: