-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.c
71 lines (53 loc) · 1.67 KB
/
ui.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
#include "ui.h"
#include <SDL2/SDL.h>
#include "spritesheet.h"
#include "text.h"
static const SDL_Rect uiRect = {0, 0, 800, 50};
static const SDL_Rect uiInvadersShotRect = {50, 10, 30, 20};
static SDL_Rect scoreTextureOnScreen = {10, 10, 16, 16};
static SDL_Rect scoreTextureRect = {0, 0, 0, 0};
static unsigned score = 0;
static SDL_Texture *scoreTexture = NULL;
static const SDL_Color textColor = {255, 255, 255, 255};
static SDL_bool needRedraw = SDL_TRUE;
static void redraw_score(SDL_Renderer *r) {
char buf[16];
if (scoreTexture) {
SDL_DestroyTexture(scoreTexture);
scoreTexture = NULL;
}
snprintf(buf, 16, "%u", score);
SDL_Surface *scoreSurface = draw_text(buf, textColor);
scoreTextureRect.w = scoreSurface->w;
scoreTextureRect.h = scoreSurface->h;
scoreTextureOnScreen.w = scoreTextureRect.w;
scoreTextureOnScreen.h = scoreTextureRect.h;
scoreTextureOnScreen.x = 800 / 2 - scoreTextureOnScreen.w/2;
scoreTexture = SDL_CreateTextureFromSurface(r, scoreSurface);
SDL_FreeSurface(scoreSurface);
needRedraw = SDL_FALSE;
}
int init_ui() {}
void draw_ui(SDL_Renderer *r) {
if (needRedraw == SDL_TRUE) {
redraw_score(r);
}
SDL_SetRenderDrawColor(r, 40, 40, 40, 255);
SDL_RenderFillRect(r, &uiRect);
SDL_SetRenderDrawColor(r, 0, 0, 0, 255);
SDL_RenderCopy(r, scoreTexture, &scoreTextureRect, &scoreTextureOnScreen);
}
void add_score(unsigned amount) {
score += amount;
needRedraw = SDL_TRUE;
}
void cleanup_ui() {
if (scoreTexture) {
SDL_DestroyTexture(scoreTexture);
scoreTexture = NULL;
}
}
void reset_score() {
score = 0;
needRedraw = SDL_TRUE;
}