summary refs log tree commit diff stats
path: root/src/main.cpp
blob: 0b9195f3c9b0d6cded33374e3b795b061f9b703b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <vector>
#include <iostream>
#include <memory>
#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;

    std::unique_ptr<Game> game = std::make_unique<Game>(rng, muxer, renderer);

    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();

        if (game->losing == LoseState::Done) {
          game = std::make_unique<Game>(rng, muxer, renderer);
        }
      } else {
        SDL_Event e;

        while (SDL_PollEvent(&e)) {
          if (e.type == SDL_QUIT) {
            game->quit = true;
          }
        }

        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;
}