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