0001: // ----------------------------------------------------------------------------
0002: //
0003: // main.cpp - starting point
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: // Objects
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: // Declaration
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:     // Creation of the window
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:                             ,"t-pot"        // 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:     MyMsgProc(hWnd,msg,wParam,lParam);
0125: 
0126:     return DefWindowProc(hWnd,msg,wParam,lParam);
0127: }
0128: //-----------------------------------------------------------------------------
0129: // Name: InitD3D()
0130: //-----------------------------------------------------------------------------
0131: HRESULT InitD3D( HWND hWnd )
0132: {
0133:     // Create Direct3D Object
0134:     if (NULL == (s_lpD3D = Direct3DCreate8(D3D_SDK_VERSION))){
0135:         MessageBox(NULL,"Direct3D の作成に失敗しました。",CAPTION,MB_OK | MB_ICONSTOP);
0136:         return E_FAIL;
0137:     }
0138: 
0139:     // Get the video mode
0140:     D3DDISPLAYMODE d3ddm;
0141:     if( FAILED( s_lpD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &d3ddm ) ) ) return E_FAIL;
0142:     // Seach the ability of the video card
0143:     D3DCAPS8 caps;
0144:     s_lpD3D->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps);
0145: 
0146:     ZeroMemory(&s_d3dpp, sizeof(D3DPRESENT_PARAMETERS));
0147: 
0148:     s_d3dpp.BackBufferCount = 1;
0149:     if (FULLSCREEN){
0150:         s_d3dpp.Windowed = FALSE;
0151:         s_d3dpp.BackBufferWidth = WIDTH;
0152:         s_d3dpp.BackBufferHeight = HEIGHT;
0153:     }else{
0154:         s_d3dpp.Windowed = TRUE;
0155:         s_d3dpp.BackBufferWidth = WIDTH;
0156:         s_d3dpp.BackBufferHeight = HEIGHT;
0157:     }
0158:     s_d3dpp.BackBufferFormat = d3ddm.Format;
0159:     s_d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
0160:     s_d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
0161:     s_d3dpp.hDeviceWindow = hWnd;
0162:     // Judge the z-buffer
0163:     s_d3dpp.EnableAutoDepthStencil = TRUE;
0164:     if( FAILED( s_lpD3D->CheckDeviceFormat( caps.AdapterOrdinal, 
0165:                                            caps.DeviceType, d3ddm.Format, 
0166:                                            D3DUSAGE_DEPTHSTENCIL, 
0167:                                            D3DRTYPE_SURFACE,
0168:                                            D3DFMT_D24S8 ) ) ){
0169:         s_d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
0170:     }else{
0171:         s_d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
0172:     }
0173:  
0174:     DWORD  vs[] = {D3DCREATE_HARDWARE_VERTEXPROCESSING, D3DCREATE_MIXED_VERTEXPROCESSING, D3DCREATE_SOFTWARE_VERTEXPROCESSING,0 };
0175:     D3DDEVTYPE ps[] = {D3DDEVTYPE_HAL, D3DDEVTYPE_REF, (D3DDEVTYPE)0};
0176:     
0177:     DWORD v = (caps.VertexShaderVersion < D3DVS_VERSION(1,0)) ? 1 : 0;
0178:     DWORD p = (caps.PixelShaderVersion  < D3DPS_VERSION(1,0)) ? 1 : 0;
0179: 
0180:     for(;ps[p];p++){
0181:     for(;vs[v];v++){
0182:         if(SUCCEEDED(s_lpD3D->CreateDevice(D3DADAPTER_DEFAULT, ps[p],hWnd,vs[v],&s_d3dpp,&s_lpD3DDEV))) goto set_up;
0183:     }
0184:     }
0185: set_up:
0186:     return S_OK;
0187: }
0188: