#include #include #include #include "util.h" #include "game.h" #include "renderer.h" enum class TitleState { FadeIn, Sustain, FadeOut }; int main(int, char**) { std::random_device randomEngine; std::mt19937 rng(randomEngine()); try { Renderer renderer; Muxer muxer; Game game(rng, muxer); constexpr int titleFadeLen = 2000; bool doneTitles = false; int titleNum = 0; TitleState titleState = TitleState::FadeIn; Interpolation titleFade; Timer titleSustain(3000); titleFade.start(titleFadeLen); size_t lastTime = SDL_GetTicks(); while (!game.quit) { size_t currentTime = SDL_GetTicks(); size_t frameTime = currentTime - lastTime; lastTime = currentTime; if (doneTitles) { game.update(frameTime); renderer.renderGame(game, true); muxer.update(); } else { SDL_Event e; while (SDL_PollEvent(&e)); switch (titleState) { case TitleState::FadeIn: { titleFade.tick(frameTime); if (titleFade.isComplete()) { titleState = TitleState::Sustain; titleSustain.reset(); } renderer.renderTitle(titleNum, titleFade.getProgress(0, 1)); break; } case TitleState::Sustain: { titleSustain.accumulate(frameTime); const Uint8* state = SDL_GetKeyboardState(NULL); if (state[SDL_SCANCODE_SPACE] || titleSustain.step()) { titleState = TitleState::FadeOut; titleFade.start(titleFadeLen); } renderer.renderTitle(titleNum, 1); break; } case TitleState::FadeOut: { titleFade.tick(frameTime); renderer.renderTitle(titleNum, titleFade.getProgress(1, 0)); if (titleFade.isComplete()) { titleNum++; if (titleNum < NUM_TITLES) { titleState = TitleState::FadeIn; titleFade.start(titleFadeLen); } else { doneTitles = true; } } break; } } } } } catch (const sdl_error& ex) { std::cout << "SDL error (" << ex.what() << ")" << std::endl; } catch (const img_error& ex) { std::cout << "SDL_IMG error (" << ex.what() << ")" << std::endl; } return 0; }