-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.c
51 lines (45 loc) · 1.17 KB
/
utils.c
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
#include "utils.h"
#include <SDL2/SDL_image.h>
int utils_popArray(int* array, int* arrayLength) {
// swap the first element with the last, reduce the length, return the old
// first element
if (arrayLength == 0) {
return -1;
}
int elem = array[0];
int tmp = array[(*arrayLength) - 1];
array[(*arrayLength) - 1] = elem;
array[0] = tmp;
(*arrayLength) -= 1;
return elem;
}
char utils_loadImageTexture(SDL_Renderer *renderer, const char *fileName, SDL_Texture **texture) {
SDL_Surface* tempSurface = IMG_Load(fileName);
if (tempSurface == 0) {
return 0;
}
*texture = SDL_CreateTextureFromSurface(renderer, tempSurface);
SDL_FreeSurface(tempSurface);
// everything went ok, add the texture to our list
return texture != 0;
}
void utils_createTextTexture(
SDL_Renderer *renderer,
TTF_Font *font,
const char *text,
SDL_Color color,
SDL_Texture **texture
) {
SDL_Surface* textSurface;
textSurface = TTF_RenderText_Solid(font, text, color);
if (textSurface == NULL) {
printf(
"Unable to render text surface! SDL_ttf Error: %s\n",
TTF_GetError()
);
}
else {
*texture = SDL_CreateTextureFromSurface(renderer, textSurface);
SDL_FreeSurface(textSurface);
}
}