diff options
author | Star Rauchenberger <fefferburbia@gmail.com> | 2023-11-02 18:38:53 -0400 |
---|---|---|
committer | Star Rauchenberger <fefferburbia@gmail.com> | 2023-11-02 18:38:53 -0400 |
commit | a59fcafb2e81f3cb40ff320b106030e8fed4bd66 (patch) | |
tree | 7e7396a9422814365a5f903a53d7391d3e7b22fd /util.cpp | |
parent | 45d6e635c880a7fae8711fba366519dd314d9faf (diff) | |
download | mazeoflife-a59fcafb2e81f3cb40ff320b106030e8fed4bd66.tar.gz mazeoflife-a59fcafb2e81f3cb40ff320b106030e8fed4bd66.tar.bz2 mazeoflife-a59fcafb2e81f3cb40ff320b106030e8fed4bd66.zip |
Diffstat (limited to 'util.cpp')
-rw-r--r-- | util.cpp | 32 |
1 files changed, 14 insertions, 18 deletions
diff --git a/util.cpp b/util.cpp index 693d0ad..5947620 100644 --- a/util.cpp +++ b/util.cpp | |||
@@ -1,8 +1,10 @@ | |||
1 | #include "util.h" | 1 | #include "util.h" |
2 | 2 | ||
3 | #include <filesystem> | ||
3 | #include <iostream> | 4 | #include <iostream> |
4 | 5 | ||
5 | #include "mazeoflife.h" | 6 | #include "mazeoflife.h" |
7 | #include "sdl.h" | ||
6 | 8 | ||
7 | void wrap(int& x, int& y) { | 9 | void wrap(int& x, int& y) { |
8 | if (x < 0) { | 10 | if (x < 0) { |
@@ -18,38 +20,32 @@ void wrap(int& x, int& y) { | |||
18 | } | 20 | } |
19 | } | 21 | } |
20 | 22 | ||
21 | TTF_Font* loadFont(int size) { | 23 | font_ptr loadFont(int size) { |
22 | TTF_Font* tmpfont = TTF_OpenFont("resources/mono.ttf", size); | 24 | font_ptr font = font_ptr(TTF_OpenFont("resources/mono.ttf", size)); |
23 | 25 | if (!font) { | |
24 | if (tmpfont == NULL) { | 26 | throw ttf_error(); |
25 | printf("Unable to load font: %s\n", TTF_GetError()); | ||
26 | exit(1); | ||
27 | } | 27 | } |
28 | 28 | ||
29 | return tmpfont; | 29 | return font; |
30 | } | 30 | } |
31 | 31 | ||
32 | const char* getDataFile() { | 32 | std::string getDataFile() { |
33 | #ifdef WINDOWS | 33 | #ifdef WINDOWS |
34 | char* dir = getenv("USERPROFILE"); | 34 | char* dir = getenv("USERPROFILE"); |
35 | #else | 35 | #else |
36 | char* dir = getenv("HOME"); | 36 | char* dir = getenv("HOME"); |
37 | #endif | 37 | #endif |
38 | 38 | ||
39 | return (std::string(dir) + "/.molhslist").c_str(); | 39 | return std::string(std::filesystem::path(dir) / ".molhslist"); |
40 | } | 40 | } |
41 | 41 | ||
42 | SDL_Texture* loadImage(SDL_Renderer* renderer, std::string file) { | 42 | texture_ptr loadImage(SDL_Renderer* renderer, std::string file) { |
43 | SDL_Surface* surface = SDL_LoadBMP(file.c_str()); | 43 | surface_ptr surface = surface_ptr(SDL_LoadBMP(file.c_str())); |
44 | if (surface == NULL) { | 44 | if (!surface) { |
45 | std::cout << SDL_GetError() << std::endl; | 45 | throw sdl_error(); |
46 | return NULL; | ||
47 | } | 46 | } |
48 | 47 | ||
49 | SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface); | 48 | return texture_ptr(SDL_CreateTextureFromSurface(renderer, surface.get())); |
50 | SDL_FreeSurface(surface); | ||
51 | |||
52 | return texture; | ||
53 | } | 49 | } |
54 | 50 | ||
55 | void applyTexture(SDL_Renderer* renderer, SDL_Texture* tex, int x, int y) { | 51 | void applyTexture(SDL_Renderer* renderer, SDL_Texture* tex, int x, int y) { |