diff options
Diffstat (limited to 'util.cpp')
-rw-r--r-- | util.cpp | 89 |
1 files changed, 40 insertions, 49 deletions
diff --git a/util.cpp b/util.cpp index 9fcdbbd..693d0ad 100644 --- a/util.cpp +++ b/util.cpp | |||
@@ -1,71 +1,62 @@ | |||
1 | #include "util.h" | 1 | #include "util.h" |
2 | #include "mazeoflife.h" | 2 | |
3 | #include <iostream> | 3 | #include <iostream> |
4 | 4 | ||
5 | void wrap(int& x, int& y) | 5 | #include "mazeoflife.h" |
6 | { | ||
7 | if (x < 0) | ||
8 | { | ||
9 | x = WIDTH+x; | ||
10 | } else if (x >= WIDTH) | ||
11 | { | ||
12 | x = x-WIDTH; | ||
13 | } | ||
14 | 6 | ||
15 | if (y < 0) | 7 | void wrap(int& x, int& y) { |
16 | { | 8 | if (x < 0) { |
17 | y = HEIGHT+y; | 9 | x = WIDTH + x; |
18 | } else if (y >= HEIGHT) | 10 | } else if (x >= WIDTH) { |
19 | { | 11 | x = x - WIDTH; |
20 | y = y-HEIGHT; | 12 | } |
21 | } | 13 | |
14 | if (y < 0) { | ||
15 | y = HEIGHT + y; | ||
16 | } else if (y >= HEIGHT) { | ||
17 | y = y - HEIGHT; | ||
18 | } | ||
22 | } | 19 | } |
23 | 20 | ||
24 | TTF_Font* loadFont(int size) | 21 | TTF_Font* loadFont(int size) { |
25 | { | 22 | TTF_Font* tmpfont = TTF_OpenFont("resources/mono.ttf", size); |
26 | TTF_Font* tmpfont = TTF_OpenFont("resources/mono.ttf", size); | ||
27 | 23 | ||
28 | if (tmpfont == NULL) | 24 | if (tmpfont == NULL) { |
29 | { | 25 | printf("Unable to load font: %s\n", TTF_GetError()); |
30 | printf("Unable to load font: %s\n", TTF_GetError()); | 26 | exit(1); |
31 | exit(1); | 27 | } |
32 | } | ||
33 | 28 | ||
34 | return tmpfont; | 29 | return tmpfont; |
35 | } | 30 | } |
36 | 31 | ||
37 | const char* getDataFile() | 32 | const char* getDataFile() { |
38 | { | ||
39 | #ifdef WINDOWS | 33 | #ifdef WINDOWS |
40 | char* dir = getenv("USERPROFILE"); | 34 | char* dir = getenv("USERPROFILE"); |
41 | #else | 35 | #else |
42 | char* dir = getenv("HOME"); | 36 | char* dir = getenv("HOME"); |
43 | #endif | 37 | #endif |
44 | 38 | ||
45 | return (std::string(dir) + "/.molhslist").c_str(); | 39 | return (std::string(dir) + "/.molhslist").c_str(); |
46 | } | 40 | } |
47 | 41 | ||
48 | SDL_Texture* loadImage(SDL_Renderer* renderer, std::string file) | 42 | SDL_Texture* loadImage(SDL_Renderer* renderer, std::string file) { |
49 | { | 43 | SDL_Surface* surface = SDL_LoadBMP(file.c_str()); |
50 | SDL_Surface* surface = SDL_LoadBMP(file.c_str()); | 44 | if (surface == NULL) { |
51 | if (surface == NULL) | 45 | std::cout << SDL_GetError() << std::endl; |
52 | { | 46 | return NULL; |
53 | std::cout << SDL_GetError() << std::endl; | 47 | } |
54 | return NULL; | ||
55 | } | ||
56 | 48 | ||
57 | SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface); | 49 | SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface); |
58 | SDL_FreeSurface(surface); | 50 | SDL_FreeSurface(surface); |
59 | 51 | ||
60 | return texture; | 52 | return texture; |
61 | } | 53 | } |
62 | 54 | ||
63 | void applyTexture(SDL_Renderer* renderer, SDL_Texture* tex, int x, int y) | 55 | void applyTexture(SDL_Renderer* renderer, SDL_Texture* tex, int x, int y) { |
64 | { | 56 | SDL_Rect pos; |
65 | SDL_Rect pos; | 57 | pos.x = x; |
66 | pos.x = x; | 58 | pos.y = y; |
67 | pos.y = y; | 59 | SDL_QueryTexture(tex, NULL, NULL, &pos.w, &pos.h); |
68 | SDL_QueryTexture(tex, NULL, NULL, &pos.w, &pos.h); | ||
69 | 60 | ||
70 | SDL_RenderCopy(renderer, tex, NULL, &pos); | 61 | SDL_RenderCopy(renderer, tex, NULL, &pos); |
71 | } \ No newline at end of file | 62 | } \ No newline at end of file |