-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdlutil.c
74 lines (61 loc) · 1.35 KB
/
sdlutil.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "sdlutil.h"
SDL_Texture *TextureFromFile(const char *filename, SDL_Renderer *renderer)
{
SDL_Texture *texture;
SDL_Surface *surface;
if((surface = SDL_LoadBMP(filename)) == NULL)
{
printf("SDL_LoadBMP: %s\n", SDL_GetError());
return NULL;
}
if((texture = SDL_CreateTextureFromSurface(renderer, surface)) == NULL)
{
printf("SDL_CreateTextureFromSurface: %s\n", SDL_GetError());
return NULL;
}
SDL_FreeSurface(surface);
return texture;
}
SDL_Texture *TextureFromFont(const char *str, SDL_Renderer *renderer, TTF_Font *font, SDL_Color *color)
{
SDL_Surface *surface_str;
SDL_Texture *texture_str;
if((surface_str = TTF_RenderText_Solid(font, str, *color)) == NULL)
{
return NULL;
}
texture_str = SDL_CreateTextureFromSurface(renderer, surface_str);
SDL_FreeSurface(surface_str);
return texture_str;
}
void DrawFontTexture(SDL_Renderer *r, SDL_Texture *t, int x, int y, Align hAlign, Align vAlign)
{
int tW, tH;
SDL_Rect rect;
SDL_QueryTexture(t, NULL, NULL, &tW, &tH);
rect.w = tW;
rect.h = tH;
switch(hAlign)
{
case Align_Center:
rect.x = x - tW / 2;
break;
case Align_Right:
rect.x = x - tW;
break;
default:
break;
}
switch(vAlign)
{
case Align_Center:
rect.y = y - tH / 2;
break;
case Align_Right:
rect.y = y - tH;
break;
default:
break;
}
SDL_RenderCopy(r, t, NULL, &rect);
}