summary refs log tree commit diff stats
path: root/util.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'util.cpp')
-rw-r--r--util.cpp32
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
7void wrap(int& x, int& y) { 9void 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
21TTF_Font* loadFont(int size) { 23font_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
32const char* getDataFile() { 32std::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
42SDL_Texture* loadImage(SDL_Renderer* renderer, std::string file) { 42texture_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
55void applyTexture(SDL_Renderer* renderer, SDL_Texture* tex, int x, int y) { 51void applyTexture(SDL_Renderer* renderer, SDL_Texture* tex, int x, int y) {