summary refs log tree commit diff stats
path: root/mazeoflife.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'mazeoflife.cpp')
-rw-r--r--mazeoflife.cpp75
1 files changed, 42 insertions, 33 deletions
diff --git a/mazeoflife.cpp b/mazeoflife.cpp index 952fbdb..3afff78 100644 --- a/mazeoflife.cpp +++ b/mazeoflife.cpp
@@ -1,5 +1,9 @@
1#include "mazeoflife.h" 1#include "mazeoflife.h"
2 2
3#ifdef __EMSCRIPTEN__
4#include <emscripten.h>
5#endif
6
3#include <SDL.h> 7#include <SDL.h>
4#include <SDL_net.h> 8#include <SDL_net.h>
5#include <SDL_ttf.h> 9#include <SDL_ttf.h>
@@ -7,53 +11,58 @@
7#include <cstdlib> 11#include <cstdlib>
8#include <ctime> 12#include <ctime>
9#include <iostream> 13#include <iostream>
14#include <memory>
10 15
16#include "sdl.h"
11#include "state.h" 17#include "state.h"
12#include "titlestate.h" 18#include "titlestate.h"
13 19
14int main(int argc, char* argv[]) { 20static void main_loop(void* arg) {
15 srand(time(NULL)); 21 Game& game = *static_cast<Game*>(arg);
16 22
17 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) { 23 std::unique_ptr<State> next_state = (*game.state)(game);
18 printf("Could not initialize SDL: %s.\n", SDL_GetError()); 24 if (next_state) {
19 exit(-1); 25 game.state = std::move(next_state);
20 } 26 }
21 27
22 if (TTF_Init() == -1) { 28 if (game.should_quit) {
23 printf("Could not initialize SDL_ttf: %s.\n", TTF_GetError()); 29#ifdef __EMSCRIPTEN__
24 exit(-1); 30 emscripten_cancel_main_loop();
31#else
32 exit(0);
33#endif
25 } 34 }
35}
26 36
27 if (SDLNet_Init() == -1) { 37int main(int, char**) {
28 printf("Cound not initalize SDL_net: %s.\n", SDLNet_GetError()); 38 Game game;
29 exit(-1); 39
30 } 40 std::random_device randomEngine;
41 game.rng = std::mt19937(randomEngine());
31 42
32 SDL_Window* window = 43 game.window = window_ptr(
33 SDL_CreateWindow("Maze of Life", 100, 100, 480, 480, SDL_WINDOW_SHOWN); 44 SDL_CreateWindow("Maze of Life", 100, 100, 480, 480, SDL_WINDOW_SHOWN));
34 if (window == NULL) { 45 if (!game.window) {
35 std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl; 46 throw sdl_error();
36 return 1;
37 } 47 }
38 48
39 SDL_Surface* icon = SDL_LoadBMP("resources/icon.bmp"); 49 SDL_Surface* icon = SDL_LoadBMP("resources/icon.bmp");
40 SDL_SetWindowIcon(window, icon); 50 SDL_SetWindowIcon(game.window.get(), icon);
41 51
42 SDL_Renderer* renderer = SDL_CreateRenderer( 52 game.renderer = renderer_ptr(
43 window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); 53 SDL_CreateRenderer(game.window.get(), -1,
44 if (renderer == NULL) { 54 SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC));
45 std::cout << "SDL_CreateRenderer Error: " << SDL_GetError() << std::endl; 55 if (!game.renderer) {
46 return 1; 56 throw sdl_error();
47 } 57 }
48 58
49 State* state = new TitleState(); 59 game.state = std::make_unique<TitleState>(game);
50 while (state != NULL) {
51 state = (*state)(window, renderer);
52 }
53 60
54 SDL_DestroyRenderer(renderer); 61#ifdef __EMSCRIPTEN__
55 SDL_DestroyWindow(window); 62 emscripten_set_main_loop_arg(main_loop, &game, 0, 1);
56 SDLNet_Quit(); 63#else
57 TTF_Quit(); 64 for (;;) {
58 SDL_Quit(); 65 main_loop(&game);
59} \ No newline at end of file 66 }
67#endif
68}