summary refs log tree commit diff stats
path: root/util.cpp
blob: 594762036a5a915e224a40de33ba3f20dc4c507e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include "util.h"

#include <filesystem>
#include <iostream>

#include "mazeoflife.h"
#include "sdl.h"

void wrap(int& x, int& y) {
  if (x < 0) {
    x = WIDTH + x;
  } else if (x >= WIDTH) {
    x = x - WIDTH;
  }

  if (y < 0) {
    y = HEIGHT + y;
  } else if (y >= HEIGHT) {
    y = y - HEIGHT;
  }
}

font_ptr loadFont(int size) {
  font_ptr font = font_ptr(TTF_OpenFont("resources/mono.ttf", size));
  if (!font) {
    throw ttf_error();
  }

  return font;
}

std::string getDataFile() {
#ifdef WINDOWS
  char* dir = getenv("USERPROFILE");
#else
  char* dir = getenv("HOME");
#endif

  return std::string(std::filesystem::path(dir) / ".molhslist");
}

texture_ptr loadImage(SDL_Renderer* renderer, std::string file) {
  surface_ptr surface = surface_ptr(SDL_LoadBMP(file.c_str()));
  if (!surface) {
    throw sdl_error();
  }

  return texture_ptr(SDL_CreateTextureFromSurface(renderer, surface.get()));
}

void applyTexture(SDL_Renderer* renderer, SDL_Texture* tex, int x, int y) {
  SDL_Rect pos;
  pos.x = x;
  pos.y = y;
  SDL_QueryTexture(tex, NULL, NULL, &pos.w, &pos.h);

  SDL_RenderCopy(renderer, tex, NULL, &pos);
}