summary refs log tree commit diff stats
path: root/mazeoflife.cpp
blob: 3afff787d57ec7bb944a35b721416f4951282e46 (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
#include "mazeoflife.h"

#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif

#include <SDL.h>
#include <SDL_net.h>
#include <SDL_ttf.h>

#include <cstdlib>
#include <ctime>
#include <iostream>
#include <memory>

#include "sdl.h"
#include "state.h"
#include "titlestate.h"

static void main_loop(void* arg) {
  Game& game = *static_cast<Game*>(arg);

  std::unique_ptr<State> next_state = (*game.state)(game);
  if (next_state) {
    game.state = std::move(next_state);
  }

  if (game.should_quit) {
#ifdef __EMSCRIPTEN__
    emscripten_cancel_main_loop();
#else
    exit(0);
#endif
  }
}

int main(int, char**) {
  Game game;

  std::random_device randomEngine;
  game.rng = std::mt19937(randomEngine());

  game.window = window_ptr(
      SDL_CreateWindow("Maze of Life", 100, 100, 480, 480, SDL_WINDOW_SHOWN));
  if (!game.window) {
    throw sdl_error();
  }

  SDL_Surface* icon = SDL_LoadBMP("resources/icon.bmp");
  SDL_SetWindowIcon(game.window.get(), icon);

  game.renderer = renderer_ptr(
      SDL_CreateRenderer(game.window.get(), -1,
                         SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC));
  if (!game.renderer) {
    throw sdl_error();
  }

  game.state = std::make_unique<TitleState>(game);

#ifdef __EMSCRIPTEN__
  emscripten_set_main_loop_arg(main_loop, &game, 0, 1);
#else
  for (;;) {
    main_loop(&game);
  }
#endif
}