0001: // ----------------------------------------------------------------------------
0002: //
0003: // main.cpp - Main part for windows programming
0004: // 
0005: // Copyright (c) 2001 if (if@edokko.com)
0006: // All Rights Reserved.
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: // Object
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;      // flag for finish
0025: 
0026: // ----------------------------------------------------------------------------
0027: // decl
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: void SetDrop(float x , float y);
0034: void ResetDrop();
0035: int  IsDrop();
0036: 
0037: // ----------------------------------------------------------------------------
0038: // Name: WinMain()
0039: //-----------------------------------------------------------------------------
0040: int PASCAL WinMain(HINSTANCE hInst,HINSTANCE hPrev,char *CmdLine,int CmdShow)
0041: {
0042:     s_end = 0;
0043:     
0044:     RECT    rect;
0045:     SetRect(&rect, 0, 0, WIDTH, HEIGHT);
0046:     DWORD   style = WS_CAPTION|WS_SYSMENU|WS_BORDER|WS_MINIMIZEBOX;
0047:     AdjustWindowRect(&rect, style, FALSE);
0048:     int width  = rect.right - rect.left;
0049:     int height = rect.bottom - rect.top;    
0050:     
0051:     WNDCLASS wc;
0052:     ZeroMemory(&wc, sizeof(WNDCLASS));
0053:     wc.hbrBackground    = (HBRUSH)GetStockObject(BLACK_BRUSH);
0054:     wc.hCursor          = LoadCursor(NULL, IDC_ARROW);
0055:     wc.hInstance        = hInst;
0056:     wc.lpfnWndProc      = MsgProc;
0057:     wc.lpszClassName    = CAPTION;
0058:     if(RegisterClass(&wc) == 0) return 0;
0059:     
0060:     HWND hWnd = CreateWindow(CAPTION
0061:                             ,CAPTION
0062:                             ,style
0063:                             ,CW_USEDEFAULT
0064:                             ,CW_USEDEFAULT
0065:                             ,width
0066:                             ,height
0067:                             ,NULL
0068:                             ,NULL
0069:                             ,hInst
0070:                             ,NULL
0071:                             );
0072:     if(hWnd == NULL) return 0;
0073:     
0074:     if(SUCCEEDED(InitD3D(hWnd))){
0075:         
0076:         if(FAILED(InitRender(s_lpD3DDEV))) s_end = true;
0077:         
0078:         ShowWindow(hWnd, SW_SHOW);
0079:         UpdateWindow(hWnd);
0080:     
0081:         // Main loop
0082:         MSG msg;
0083:         while (!s_end)
0084:         {
0085:             if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)){
0086:                 TranslateMessage(&msg);
0087:                 DispatchMessage(&msg);
0088:             }else{
0089:                 s_lpD3DDEV->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,D3DCOLOR_XRGB(255,255,255),1.0f,0);
0090:                 s_lpD3DDEV->BeginScene();
0091:                 Render(s_lpD3DDEV);
0092:                 s_lpD3DDEV->EndScene();
0093:                 if (FAILED(s_lpD3DDEV->Present(NULL,NULL,NULL,NULL)))
0094:                     s_lpD3DDEV->Reset(&s_d3dpp);
0095:             }
0096:         }
0097:     }
0098:     
0099:     // finish
0100:     CleanRender(s_lpD3DDEV);
0101:     RELEASE(s_lpD3DDEV);
0102:     RELEASE(s_lpD3D);
0103:     
0104:     return 0;
0105: }
0106: // ----------------------------------------------------------------------------
0107: // Name: MsgProc()
0108: //-----------------------------------------------------------------------------
0109: LRESULT CALLBACK MsgProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
0110: {
0111:     long x, y;
0112: 
0113:     switch (msg){
0114:     case WM_LBUTTONDOWN:
0115:         x = (lParam << 16) >> 16;
0116:         y = lParam >> 16;
0117:         SetDrop((float)x , (float)y);
0118:         break;
0119:     case WM_MOUSEMOVE:
0120:         x = (lParam << 16) >> 16;
0121:         y = lParam >> 16;
0122:         if(IsDrop())SetDrop((float)x , (float)y);
0123:         break;
0124:     case WM_LBUTTONUP:
0125:         ResetDrop();
0126:         break;
0127:     case WM_KEYDOWN:
0128:     case WM_CLOSE:
0129:         s_end = true;
0130:         return 1;
0131:         break;
0132:     default:
0133:         break;
0134:     }
0135:     return DefWindowProc(hWnd,msg,wParam,lParam);
0136: }
0137: //-----------------------------------------------------------------------------
0138: // Name: InitD3D()
0139: //-----------------------------------------------------------------------------
0140: HRESULT InitD3D( HWND hWnd )
0141: {
0142:     if (NULL == (s_lpD3D = Direct3DCreate8(D3D_SDK_VERSION))){
0143:         MessageBox(NULL,"Failed to Create Direct3D",CAPTION,MB_OK | MB_ICONSTOP);
0144:         return E_FAIL;
0145:     }
0146: 
0147:     D3DDISPLAYMODE d3ddm;
0148:     if( FAILED( s_lpD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &d3ddm ) ) ) return E_FAIL;
0149: 
0150:     ZeroMemory(&s_d3dpp, sizeof(D3DPRESENT_PARAMETERS));
0151: 
0152:     s_d3dpp.BackBufferCount = 1;
0153:     s_d3dpp.Windowed = TRUE;
0154:     s_d3dpp.BackBufferWidth = WIDTH;
0155:     s_d3dpp.BackBufferHeight = HEIGHT;
0156:     s_d3dpp.BackBufferFormat = d3ddm.Format;
0157:     s_d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
0158:     s_d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
0159:     s_d3dpp.hDeviceWindow = hWnd;
0160:     s_d3dpp.EnableAutoDepthStencil = TRUE;
0161:     s_d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
0162:  
0163:     D3DCAPS8 caps;
0164:     s_lpD3D->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps);
0165:     DWORD  vs[] = {D3DCREATE_HARDWARE_VERTEXPROCESSING, D3DCREATE_MIXED_VERTEXPROCESSING, D3DCREATE_SOFTWARE_VERTEXPROCESSING,0 };
0166:     D3DDEVTYPE ps[] = {D3DDEVTYPE_HAL, D3DDEVTYPE_REF, (D3DDEVTYPE)0};
0167:     
0168:     DWORD v = (caps.VertexShaderVersion < D3DVS_VERSION(1,1)) ? 1 : 0;
0169:     DWORD p = (caps.PixelShaderVersion  < D3DPS_VERSION(1,1)) ? 1 : 0;
0170: 
0171:     for(;ps[p];p++){
0172:     for(;vs[v];v++){
0173:         if(SUCCEEDED(s_lpD3D->CreateDevice(D3DADAPTER_DEFAULT, ps[p],hWnd,vs[v],&s_d3dpp,&s_lpD3DDEV))) goto set_up;
0174:     }
0175:     }
0176: set_up:
0177:     return S_OK;
0178: }
0179: