-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathscreen_end_game.c
147 lines (113 loc) · 3.9 KB
/
screen_end_game.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
End Game Screen
Written by Peter de Tagyos
Started: 2/22/2017
*/
#define ENDGAME_BG_WIDTH 80
#define ENDGAME_BG_HEIGHT 45
#define INFO_LEFT 2
#define INFO_TOP 10
#define INFO_WIDTH 50
#define INFO_HEIGHT 35
internal void render_endgame_bg_view(Console *console);
internal void render_info_view(Console *console);
internal void handle_event_endgame(UIScreen *activeScreen, SDL_Event event);
// Init / Show screen --
internal UIScreen *
screen_show_endgame()
{
List *subViews = list_new(NULL);
UIRect infoRect = {(16 * INFO_LEFT), (16 * INFO_TOP), (16 * INFO_WIDTH), (16 * INFO_HEIGHT)};
UIView *infoView = view_new(infoRect, INFO_WIDTH, INFO_HEIGHT,
"./terminal16x16.png", 0, 0x000000ff,
true, render_info_view);
list_insert_after(subViews, NULL, infoView);
UIRect bgRect = {0, 0, (16 * BG_WIDTH), (16 * BG_HEIGHT)};
UIView *bgView = view_new(bgRect, BG_WIDTH, BG_HEIGHT,
"./terminal16x16.png", 0, 0x000000ff,
true, render_endgame_bg_view);
list_insert_after(subViews, NULL, bgView);
UIScreen *endScreen = calloc(1, sizeof(UIScreen));
endScreen->views = subViews;
endScreen->activeView = infoView;
endScreen->handle_event = handle_event_endgame;
if (hofConfig == NULL) {
hofConfig = config_file_parse("hof.cfg");
}
return endScreen;
}
// Render Functions --
internal void
render_endgame_bg_view(Console *console)
{
// We should load and process the bg image only once, not on each render
local_persist BitmapImage *bgImage = NULL;
local_persist AsciiImage *aiImage = NULL;
if (bgImage == NULL) {
bgImage = image_load_from_file("./gameover.png");
aiImage = asciify_bitmap(console, bgImage);
}
if (asciiMode) {
view_draw_ascii_image_at(console, aiImage, 0, 0);
} else {
view_draw_image_at(console, bgImage, 0, 0);
}
}
internal void
render_info_view(Console *console)
{
// Stats recap
console_put_string_at(console, "-== HERO STATS ==-", 17, 0, 0xffd700ff, 0x00000000);
console_put_string_at(console, playerName, 18, 2, 0xffffffff, 0x00000000);
char *level = String_Create("Level:%d", currentLevelNumber);
console_put_string_at(console, level, 18, 4, 0xffd700ff, 0x00000000);
String_Destroy(level);
char *gems = String_Create("Gems:%d", gemsFoundTotal);
console_put_string_at(console, gems, 28, 4, 0x753aabff, 0x00000000);
String_Destroy(gems);
// Leaderboard
console_put_string_at(console, "-== HERO HALL OF FAME ==-", 14, 7, 0xaa0000ff, 0x00000000);
// Loop through all HoF entries, extract the data into a formatted string, and write to screen
i32 y = 9;
ListElement *e = list_head(hofConfig->entities);
while (e != NULL) {
ConfigEntity *entity = (ConfigEntity *)e->data;
char *name = config_entity_value(entity, "name");
char *level = config_entity_value(entity, "level");
char *gems = config_entity_value(entity, "gems");
char *date = config_entity_value(entity, "date");
char *nameString = String_Create("%20s", name);
console_put_string_at(console, nameString, 3, y, 0xe2f442ff, 0x00000000);
char *recordString = String_Create("%10s Level:%s Gems:%s", date, level, gems);
console_put_string_at(console, recordString, 23, y, 0xeeeeeeff, 0x00000000);
y += 2;
e = list_next(e);
}
// Instructions for active commands
console_put_string_at(console, "Start a (N)ew game", 18, 30, 0xbca285FF, 0x00000000);
console_put_string_at(console, "-or-", 25, 31, 0xbca285FF, 0x00000000);
console_put_string_at(console, "(ESC) to Quit", 20, 32, 0xbca285FF, 0x00000000);
}
// Event Handling --
internal void
handle_event_endgame(UIScreen *activeScreen, SDL_Event event)
{
if (event.type == SDL_KEYDOWN) {
SDL_Keycode key = event.key.keysym.sym;
switch (key) {
case SDLK_n: {
// Start a new game and transition to in-game screen
game_new();
ui_set_active_screen(screen_show_in_game());
currentlyInGame = true;
}
break;
case SDLK_ESCAPE: {
quit_game();
}
break;
default:
break;
}
}
}