summary refs log tree commit diff stats
path: root/src/main.cpp
blob: cd8ee3ed3880c8fe6d9f77c954084715168ca7b7 (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
#include "renderer.h"

#include <random>
#include "renderer.h"
#include "simulation.h"
#include "level.h"
#include "editor.h"

int main(int argc, char** argv)
{
  std::random_device randomEngine;
  std::mt19937 rng(randomEngine());

  sdl_wrapper sdl;
  img_wrapper img;

  window_ptr win(
    SDL_CreateWindow(
      "dispatcher",
      100,
      100,
      WINDOW_SIZE.w(),
      WINDOW_SIZE.h(),
      SDL_WINDOW_SHOWN));

  if (!win)
  {
    throw sdl_error();
  }

  renderer_ptr ren(
    SDL_CreateRenderer(
      win.get(),
      -1,
      SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC));

  if (!ren)
  {
    throw sdl_error();
  }

  Level level(ren.get());

  std::unique_ptr<State> state;

  if (argc == 2 && !strcmp(argv[1], "--editor"))
  {
    state.reset(new Editor(ren.get()));
  } else {
    state.reset(new Simulation(level));
  }

  bool quit = false;

  SDL_Event e;
  size_t lastTime = SDL_GetTicks();

  while (!quit)
  {
    size_t currentTime = SDL_GetTicks();
    size_t frameTime = currentTime - lastTime;
    lastTime = currentTime;

    while (SDL_PollEvent(&e))
    {
      if (e.type == SDL_QUIT ||
            (e.type == SDL_KEYDOWN &&
             e.key.keysym.sym == SDLK_ESCAPE))
      {
        quit = true;
        break;
      }
    }

    const Uint8* keystate = SDL_GetKeyboardState(NULL);

    state->tick(frameTime / 1000.0, keystate);

    SDL_SetRenderTarget(ren.get(), nullptr);
    SDL_RenderClear(ren.get());
    state->render(ren.get());
    SDL_RenderPresent(ren.get());
  }
}