#include #include #include #include #include #include #include // TODO: move this to Application::create() #ifdef _WIN32 #include typedef sp::Win32Application ApplicationType; #elif defined(__linux__) || defined(unix) || defined(__unix) || defined(__unix__) #include typedef sp::UnixApplication ApplicationType; #else #error "No Application implementation exists" #endif namespace sp { Game::Game() : m_running(false) { m_platform = new ApplicationType(); m_graphics = new Graphics(m_platform); m_input = new InputModule(&m_platform->getInput()); m_messageHandler = new MessageHandler(); } Game::~Game() { delete m_input; delete m_graphics; delete m_platform; delete m_messageHandler; } void Game::run() { if (!m_graphics->init()) { return; } init(); gameLoop(); } void Game::gameLoop() { GameTime time; time.setTimeStep(120); m_running = true; while(m_running) { processEvents(); // Update input. getInput()->update(); if (time.beginFrame()) { while(time.tick()) { update(time.getTimeStep()); } render(); m_fpsCounter.addFrame(); } } } void Game::processEvents() { MessageQueue& queue = m_platform->getMessageQueue(); Event event; while(queue.pollEvent(event)) { if (event.type == Event::Quit) { exit(); break; } // Call message handler. if (event.type == Event::Size) { m_messageHandler->onSizeChanged(event.size.display, event.size.width, event.size.height); } m_messageHandler->onEvent(event); } } void Game::exit() { m_running = false; } Graphics* Game::getGraphics() const { return m_graphics; } InputModule* Game::getInput() const { return m_input; } FPSCounter& Game::getFpsCounter() { return m_fpsCounter; } MessageHandler* Game::getMessageHandler() const { return m_messageHandler; } } // namespace sp