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
|
#ifndef gobjects
#define gobjects
#include <stdlib.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#define flaot float
typedef struct TEXT {
SDL_Rect textRect;
SDL_Texture *texture;
} TEXT;
typedef struct GS{
int width;
int height;
SDL_Renderer *renderer;
TTF_Font *font;
} GS;
TEXT *text_constructor(GS *stuff, char *text_buf){
TEXT *text=(TEXT*)malloc(sizeof(TEXT));
SDL_Color color={
244,222,205
};
int w, h;
TTF_SizeUTF8(stuff->font,text_buf,&w,&h);
SDL_Rect textRect={
0,0,w,h
};
textRect.x=(stuff->width/2)-(textRect.w/2);
textRect.y=(stuff->height/2)-(textRect.h/2);
SDL_Surface *textSurface = TTF_RenderUTF8_Solid_Wrapped(stuff->font, text_buf, color, textRect.w);
if(!textSurface){
fprintf(stderr,"ERROR WITH TEXT SURFACE\n");
exit(EXIT_FAILURE);
}
SDL_Texture *texture = SDL_CreateTextureFromSurface(stuff->renderer, textSurface);
SDL_FreeSurface(textSurface);
if(!texture){
fprintf(stderr,"ERROR WITH TEXTURE\n");
exit(EXIT_FAILURE);
}
text->texture=texture;
text->textRect=textRect;
return text;
}
#endif
|