#include #include "main.h" //declare global objects LPDIRECT3DDEVICE9 d3d_device; LPD3DXSPRITE sprite_handler; HWND window_handle; LPDIRECT3D9 d3d; LPDIRECT3DSURFACE9 backbuffer; bool gameover; //macro to read the key states #define KEY_DOWN(vk) ((GetAsyncKeyState(vk) & 0x8000)?1:0) //function prototypes bool InitD3D(); bool game_init(); void game_update(); void game_end(); //window event callback function LRESULT WINAPI WinProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam ) { switch( msg ) { case WM_QUIT: case WM_CLOSE: case WM_DESTROY: gameover = true; break; } return DefWindowProc( hWnd, msg, wParam, lParam ); } bool InitD3D() { //initialize Direct3D d3d = Direct3DCreate9(D3D_SDK_VERSION); if (d3d == NULL) return false; //get system desktop color depth D3DDISPLAYMODE dm; d3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &dm); //set configuration options for Direct3D D3DPRESENT_PARAMETERS d3dpp; ZeroMemory(&d3dpp, sizeof(d3dpp)); d3dpp.Windowed = (!FULLSCREEN); d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.EnableAutoDepthStencil = TRUE; d3dpp.AutoDepthStencilFormat = D3DFMT_D16; d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; d3dpp.BackBufferFormat = dm.Format; d3dpp.BackBufferCount = 1; d3dpp.BackBufferWidth = SCREENW; d3dpp.BackBufferHeight = SCREENH; d3dpp.hDeviceWindow = window_handle; //create Direct3D device d3d->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, window_handle, D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &d3d_device); if (d3d_device == NULL) return false; //create pointer to the back buffer d3d_device->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer); //use ambient lighting and z-buffering d3d_device->SetRenderState(D3DRS_ZENABLE, TRUE); d3d_device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID); d3d_device->SetRenderState(D3DRS_LIGHTING, true); d3d_device->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(30,30,30)); //set a default material D3DMATERIAL9 mat; memset(&mat, 0, sizeof(mat)); mat.Diffuse.r = mat.Ambient.r = 1.0f; mat.Diffuse.g = mat.Ambient.g = 1.0f; mat.Diffuse.b = mat.Ambient.b = 1.0f; mat.Diffuse.a = mat.Ambient.a = 1.0f; d3d_device->SetMaterial(&mat); //initialize 2D renderer HRESULT result = D3DXCreateSprite(d3d_device, &sprite_handler); if (result != D3D_OK) return 0; return true; } int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow) { MSG msg; HINSTANCE g_hInstance = hInstance; DWORD dwStyle, dwExStyle; RECT windowRect; //get window caption string from engine char title[255]; sprintf(title, "%s", TITLE.c_str()); //set window dimensions windowRect.left = (long)0; windowRect.right = (long)SCREENW; windowRect.top = (long)0; windowRect.bottom = (long)SCREENH; //create the window class structure WNDCLASSEX wc; wc.cbSize = sizeof(WNDCLASSEX); //fill the struct with info wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = (WNDPROC)WinProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = NULL; wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = NULL; wc.lpszMenuName = NULL; wc.lpszClassName = title; wc.hIconSm = NULL; //set up the window with the class info RegisterClassEx(&wc); //set up the screen in windowed or fullscreen mode? if (FULLSCREEN) { DEVMODE dm; memset(&dm, 0, sizeof(dm)); dm.dmSize = sizeof(dm); dm.dmPelsWidth = SCREENW; dm.dmPelsHeight = SCREENH; dm.dmBitsPerPel = SCREEND; dm.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT; if (ChangeDisplaySettings(&dm, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL) { MessageBox(NULL, "Display mode failed", NULL, MB_OK); return 0; } dwStyle = WS_POPUP; dwExStyle = WS_EX_APPWINDOW; ShowCursor(FALSE); } else { dwStyle = WS_OVERLAPPEDWINDOW; dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; } //adjust window to true requested size AdjustWindowRectEx(&windowRect, dwStyle, FALSE, dwExStyle); //create the program window window_handle = CreateWindowEx( 0, title, //window class title, //title bar dwStyle | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, 0, 0, //x,y coordinate windowRect.right - windowRect.left, //width of the window windowRect.bottom - windowRect.top, //height of the window 0, //parent window 0, //menu g_hInstance, //application instance 0); //window parameters //was there an error creating the window? if (!window_handle) { MessageBox(window_handle, "Error creating program window!", "Error", MB_OK); return 0; } //display the window ShowWindow(window_handle, nCmdShow); UpdateWindow(window_handle); //initialize Direct3D if (!InitD3D()) { MessageBox(window_handle, "Error initializing Direct3D", "Error", MB_OK); return 0; } //initialize game resources if (!game_init()) { MessageBox(window_handle, "Error initializing the game", "Error", MB_OK); return 0; } // main message loop gameover = false; while (gameover = false) { while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } game_update(); } if (FULLSCREEN) { ShowCursor(TRUE); } //release game resources game_end(); //release Direct3D if (d3d_device) d3d_device->Release(); if (d3d) d3d->Release(); return 1; } /* * Local game variable definitions */ Font *font; Mesh *virus; Camera *camera; Light *spotlight; bool game_init() { //load the Verdana10 font font = new Font(); if (!font->loadImage("verdana10.tga")) return false; if (!font->loadWidthData("verdana10.dat")) return false; font->setColumns(16); font->setCharSize(20,16); //set the camera and perspective camera = new Camera(); camera->setPosition(0.0f, 6.0f, 6.0f); camera->setTarget(0.0f, 2.0f, 0.0f); camera->Update(); //load the mesh virus = new Mesh(); virus->Load("bones_all.x"); virus->SetScale(2.0f,2.0f,2.0f); //create a spot light D3DXVECTOR3 pos2(-10.0f,-20.0f,0.0f); D3DXVECTOR3 dir2(0.0f,2.0f,0.0f); spotlight = new Light(1, D3DLIGHT_SPOT, pos2, dir2, 1000); return true; } void game_update() { //begin 3D rendering if (!d3d_device) return; d3d_device->BeginScene(); d3d_device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,60), 1.0f, 0); //rotate and draw the mesh virus->Rotate(0.01,0.0,0.0); virus->draw(); //begin 2d rendering sprite_handler->Begin(D3DXSPRITE_ALPHABLEND); //print out mesh properties std::ostringstream ostr; ostr << "Mesh has " << virus->GetFaceCount() << " faces"; font->Print(1, 0, ostr.str()); ostr.str(""); ostr << "Mesh has " << virus->GetVertexCount() << " vertices"; font->Print(1, 15, ostr.str()); //done with 2d rendering sprite_handler->End(); //done 3D rendering d3d_device->EndScene(); d3d_device->Present(NULL, NULL, NULL, NULL); //ESC key quits if (KEY_DOWN(VK_ESCAPE)) gameover = true; } void game_end() { delete virus; delete camera; delete spotlight; delete font; }