From a59fcafb2e81f3cb40ff320b106030e8fed4bd66 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Thu, 2 Nov 2023 18:38:53 -0400 Subject: Modernized C++ a bit (and removed global highscores) --- mazeoflife.cpp | 75 ++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 42 insertions(+), 33 deletions(-) (limited to 'mazeoflife.cpp') diff --git a/mazeoflife.cpp b/mazeoflife.cpp index 952fbdb..3afff78 100644 --- a/mazeoflife.cpp +++ b/mazeoflife.cpp @@ -1,5 +1,9 @@ #include "mazeoflife.h" +#ifdef __EMSCRIPTEN__ +#include +#endif + #include #include #include @@ -7,53 +11,58 @@ #include #include #include +#include +#include "sdl.h" #include "state.h" #include "titlestate.h" -int main(int argc, char* argv[]) { - srand(time(NULL)); +static void main_loop(void* arg) { + Game& game = *static_cast(arg); - if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) { - printf("Could not initialize SDL: %s.\n", SDL_GetError()); - exit(-1); + std::unique_ptr next_state = (*game.state)(game); + if (next_state) { + game.state = std::move(next_state); } - if (TTF_Init() == -1) { - printf("Could not initialize SDL_ttf: %s.\n", TTF_GetError()); - exit(-1); + if (game.should_quit) { +#ifdef __EMSCRIPTEN__ + emscripten_cancel_main_loop(); +#else + exit(0); +#endif } +} - if (SDLNet_Init() == -1) { - printf("Cound not initalize SDL_net: %s.\n", SDLNet_GetError()); - exit(-1); - } +int main(int, char**) { + Game game; + + std::random_device randomEngine; + game.rng = std::mt19937(randomEngine()); - SDL_Window* window = - SDL_CreateWindow("Maze of Life", 100, 100, 480, 480, SDL_WINDOW_SHOWN); - if (window == NULL) { - std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl; - return 1; + 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(window, icon); + SDL_SetWindowIcon(game.window.get(), icon); - SDL_Renderer* renderer = SDL_CreateRenderer( - window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); - if (renderer == NULL) { - std::cout << "SDL_CreateRenderer Error: " << SDL_GetError() << std::endl; - return 1; + game.renderer = renderer_ptr( + SDL_CreateRenderer(game.window.get(), -1, + SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC)); + if (!game.renderer) { + throw sdl_error(); } - State* state = new TitleState(); - while (state != NULL) { - state = (*state)(window, renderer); - } + game.state = std::make_unique(game); - SDL_DestroyRenderer(renderer); - SDL_DestroyWindow(window); - SDLNet_Quit(); - TTF_Quit(); - SDL_Quit(); -} \ No newline at end of file +#ifdef __EMSCRIPTEN__ + emscripten_set_main_loop_arg(main_loop, &game, 0, 1); +#else + for (;;) { + main_loop(&game); + } +#endif +} -- cgit 1.4.1