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) --- util.cpp | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) (limited to 'util.cpp') diff --git a/util.cpp b/util.cpp index 693d0ad..5947620 100644 --- a/util.cpp +++ b/util.cpp @@ -1,8 +1,10 @@ #include "util.h" +#include #include #include "mazeoflife.h" +#include "sdl.h" void wrap(int& x, int& y) { if (x < 0) { @@ -18,38 +20,32 @@ void wrap(int& x, int& y) { } } -TTF_Font* loadFont(int size) { - TTF_Font* tmpfont = TTF_OpenFont("resources/mono.ttf", size); - - if (tmpfont == NULL) { - printf("Unable to load font: %s\n", TTF_GetError()); - exit(1); +font_ptr loadFont(int size) { + font_ptr font = font_ptr(TTF_OpenFont("resources/mono.ttf", size)); + if (!font) { + throw ttf_error(); } - return tmpfont; + return font; } -const char* getDataFile() { +std::string getDataFile() { #ifdef WINDOWS char* dir = getenv("USERPROFILE"); #else char* dir = getenv("HOME"); #endif - return (std::string(dir) + "/.molhslist").c_str(); + return std::string(std::filesystem::path(dir) / ".molhslist"); } -SDL_Texture* loadImage(SDL_Renderer* renderer, std::string file) { - SDL_Surface* surface = SDL_LoadBMP(file.c_str()); - if (surface == NULL) { - std::cout << SDL_GetError() << std::endl; - return NULL; +texture_ptr loadImage(SDL_Renderer* renderer, std::string file) { + surface_ptr surface = surface_ptr(SDL_LoadBMP(file.c_str())); + if (!surface) { + throw sdl_error(); } - SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface); - SDL_FreeSurface(surface); - - return texture; + return texture_ptr(SDL_CreateTextureFromSurface(renderer, surface.get())); } void applyTexture(SDL_Renderer* renderer, SDL_Texture* tex, int x, int y) { -- cgit 1.4.1