summary refs log tree commit diff stats
path: root/src/main.cpp
blob: 4ae11d68a5b8297f9054874dfbf0822e022043a1 (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
#include <vector>
#include <fov.h>
#include <iostream>
#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 = 3000;
    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;
}