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