summary refs log tree commit diff stats
path: root/util.cpp
blob: 693d0adacc3b76c1c8694b772797c93bc13e763d (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
59
60
61
62
#include "util.h"

#include <iostream>

#include "mazeoflife.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;
  }
}

TTF_Font* loadFont(int size) {
  TTF_Font* tmpfont = TTF_OpenFont("resources/mono.ttf", size);

  if (tmpfont == NULL) {
    printf("Unable to load font: %s\n", TTF_GetError());
    exit(1);
  }

  return tmpfont;
}

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

  return (std::string(dir) + "/.molhslist").c_str();
}

SDL_Texture* loadImage(SDL_Renderer* renderer, std::string file) {
  SDL_Surface* surface = SDL_LoadBMP(file.c_str());
  if (surface == NULL) {
    std::cout << SDL_GetError() << std::endl;
    return NULL;
  }

  SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
  SDL_FreeSurface(surface);

  return texture;
}

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);
}