LRESULT WINAPI WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) { Game* game; CREATESTRUCT *pCreate; switch (Msg) { case WM_CREATE: { pCreate = reinterpret_cast<CREATESTRUCT*>(lParam); game = reinterpret_cast<Game*>(pCreate->lpCreateParams); SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR)game); return 0; } case WM_GETMINMAXINFO: { LPMINMAXINFO info = (LPMINMAXINFO)lParam; info->ptMaxTrackSize.x = MAX_WINDOW_WIDTH; info->ptMaxTrackSize.y = MAX_WINDOW_HEIGHT; info->ptMinTrackSize.x = MAX_WINDOW_WIDTH; info->ptMinTrackSize.y = MAX_WINDOW_HEIGHT; return 0; } case WM_CLOSE: { DestroyWindow(hWnd); PostQuitMessage(0); } case WM_PAINT: { LONG_PTR ptr = GetWindowLongPtr(hWnd, GWLP_USERDATA); game = reinterpret_cast<Game*>(ptr); Game::mmgdi->BeginScene(); Game::mmgdi->EndScene(); return 0; } case WM_LBUTTONDOWN: { LONG_PTR ptr = GetWindowLongPtr(hWnd, GWLP_USERDATA); game = reinterpret_cast<Game*>(ptr); game->Click((float)GET_X_LPARAM(lParam), (float)GET_Y_LPARAM(lParam)); game->UpdateEffects(); game->Render(); return 0; } } return DefWindowProc(hWnd, Msg, wParam, lParam); };
int main( int argc, char **argv ) { // Tutorial 1: Initialize SDL and L3Init if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE | SDL_INIT_TIMER ) < 0 ) { fprintf( stderr, "SDL initialization failed: %s\n", SDL_GetError( ) ); exit( 1 ); } SDL_Surface* surface = Lilith3D::SetSDLVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, 0, false ); GLASSERT( surface ); SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL ); InitLilith3D(); // Tutorial 1: Textures and Models ResourcePool* resourcePool = new ResourcePool(); resourcePool->SetImportPath( "../graphics/" ); resourcePool->LoadRequiredAssets(); resourcePool->SetImportPath( "../grdemo/" ); // Tutorial 5: Decal texture resourcePool->LoadTexture( "impact.tga", "impact" ); // Tutorial 1: Engine Initialization Lilith3D* lilith = new Lilith3D(); Game* game = new Game( lilith ); bool ignoreJump = false; // Tutorial 1: Event Loop bool done = false; while ( !done ) { SDL_Event event; while ( SDL_PollEvent( &event ) ) { switch( event.type ) { case SDL_KEYDOWN: if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = true; } else if ( event.key.keysym.sym == SDLK_TAB ) { lilith->ScreenCapture( "tutorial" ); } break; case SDL_QUIT: done = true; break; // Tutorial 2: Adjust the camera. case SDL_MOUSEBUTTONDOWN: { if ( event.button.button == SDL_BUTTON_RIGHT ) { SDL_WM_GrabInput( SDL_GRAB_ON ); SDL_ShowCursor( SDL_DISABLE ); ignoreJump = true; } // What did the user click? else if ( event.button.button == SDL_BUTTON_LEFT ) { // Extra check to make sure only the left button is pressed. if ( SDL_GetMouseState(0, 0) == SDL_BUTTON( SDL_BUTTON_LEFT ) ) { LilithObjectList oList; lilith->IntersectRayFromScreen( event.button.x, event.button.y, TEST_TERRAIN, &oList ); if ( !oList.empty() ) { game->Click( *oList.begin(), true ); } } } } break; case SDL_MOUSEBUTTONUP: { if ( event.button.button == SDL_BUTTON_RIGHT ) { SDL_WM_GrabInput( SDL_GRAB_OFF ); SDL_ShowCursor( SDL_ENABLE ); } } break; case SDL_MOUSEMOTION: { if ( event.motion.state & SDL_BUTTON (SDL_BUTTON_RIGHT) ) { if ( !ignoreJump ) { Camera* camera = lilith->GetCamera(); float xVel = (float)event.motion.xrel * 5.0f; float yVel = (float)event.motion.yrel * 5.0f; camera->MoveCameraRotation( Camera::YAW, -xVel ); camera->MoveCameraRotation( Camera::PITCH, yVel ); } ignoreJump = false; } } break; default: break; } } // Tutorial 2: Incremental move if ( SDL_GetMouseState(0, 0) == (SDL_BUTTON( SDL_BUTTON_LEFT ) | SDL_BUTTON( SDL_BUTTON_RIGHT ) ) ) lilith->GetCamera()->MoveCameraAxial3D( 20.0f ); game->DoTick(); lilith->BeginDraw(); lilith->Draw(); lilith->EndDraw(); SDL_GL_SwapBuffers(); } // Tutorial 1: Cleanup delete game; delete lilith; delete resourcePool; SDL_Quit(); return( 0 ); }