/
view.c
100 lines (76 loc) · 1.89 KB
/
view.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "global.h"
#include "texture.h"
int digit1 = 0;
int digit2 = 0;
int colon = 1;
int digit3 = 0;
int digit4 = 0;
#define DIGIT_WIDTH 43
#define DIGIT_HEIGHT 70
#define COLON_WIDTH 25
#define COLON_HEIGHT 70
#define PANEL_WIDTH (DIGIT_WIDTH * 4 + COLON_WIDTH)
#define PANEL_HEIGHT DIGIT_HEIGHT
#define PANEL_X ((SCREEN_WIDTH - PANEL_WIDTH) / 2)
#define PANEL_Y ((SCREEN_HEIGHT - PANEL_HEIGHT) / 2)
#define COLON_X (PANEL_X + DIGIT_WIDTH * 2)
#define COLON_Y PANEL_Y
static LTexture digits_texture;
int view_load_media()
{
int success = 1;
texture_init(&digits_texture);
if(!texture_load(&digits_texture, "digits.bmp")) {
printf("Failed to load digits texture!\n");
success = 0;
}
return success;
}
void view_free_media()
{
texture_free(&digits_texture);
}
static int get_char_index(int ch)
{
int index;
switch(ch) {
case 0: case 1: case 2: case 3: case 4:
case 5: case 6: case 7: case 8: case 9:
index = ch;
break;
case 'A':
index = 10;
break;
case 'P':
index = 11;
break;
default:
index = 12;
break;
}
return index;
}
static void render_char(int ch, int x)
{
int char_index = get_char_index(ch);
SDL_Rect clip = { char_index * DIGIT_WIDTH, 0, DIGIT_WIDTH, DIGIT_HEIGHT };
texture_render(&digits_texture, PANEL_X + x, PANEL_Y, &clip);
}
static void render_colon(int show)
{
if(show) {
SDL_Rect clip = { 13 * DIGIT_WIDTH, 0, COLON_WIDTH, COLON_HEIGHT };
texture_render(&digits_texture, COLON_X, COLON_Y, &clip);
}
}
void view_paint()
{
SDL_SetRenderDrawColor(g_renderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_RenderClear(g_renderer);
render_char(digit1, 0);
render_char(digit2, DIGIT_WIDTH);
render_colon(colon);
render_char(digit3, DIGIT_WIDTH * 2 + COLON_WIDTH);
render_char(digit4, DIGIT_WIDTH * 2 + COLON_WIDTH + DIGIT_WIDTH);
SDL_RenderPresent(g_renderer);
}