/
window.c
420 lines (362 loc) · 12.4 KB
/
window.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
* StickGBC by Matt Comben is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.
* To view a copy of this license, visit
* http://creativecommons.org/licenses/by-nc-nd/4.0/.
*/
#pragma bank=3
#include <gb/gb.h>
#include <gb/drawing.h>
#include "game_constants.h"
#include "game_state.h"
#include "main.h"
#include "menu_config.h"
const UINT8 window_digit_b2d_lookup[11U][32U] = {
// digit 1
{1, 2, 4, 8, 6, 2, 4, 8, 6, 2, 4, 8, 6, 2, 4, 8, 6, 2, 4, 8, 6, 2, 4, 8, 6, 2, 4, 8, 6, 2, 4, 8},
// digit 2
{0, 0, 0, 0, 1, 3, 6, 2, 5, 1, 2, 4, 9, 9, 8, 6, 3, 7, 4, 8, 7, 5, 0, 0, 1, 3, 6, 2, 5, 1, 2, 4},
// digit 3
{0, 0, 0, 0, 0, 0, 0, 1, 2, 5, 0, 0, 0, 1, 3, 7, 5, 0, 1, 2, 5, 1, 3, 6, 2, 4, 8, 7, 4, 9, 8, 6},
// digit 4
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4, 8, 6, 2, 5, 1, 2, 4, 8, 7, 4, 8, 7, 4, 8, 7, 5, 0, 1, 3},
// digit 5
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 6, 3, 6, 2, 4, 9, 9, 8, 7, 5, 0, 1, 3, 7, 4, 8},
// digit 6
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 5, 0, 0, 1, 3, 7, 5, 1, 2, 4, 8, 7, 4},
// digit 7
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4, 8, 6, 3, 7, 4, 8, 6, 3, 7},
// digit 8
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 6, 3, 6, 3, 7, 4},
// digit 9
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 5, 0, 1},
// digit 10
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2},
// digit 11
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
};
/*
* show_balance
*
* Display full balance on-screen in window
*
* @param itx_x Starting X tile to write to
* @param itx_y Tile Y index to display. If set to 0, tile will be written to window, otherwise to background
*
* @returns X location of dollar symbol
*/
UINT8 show_balance(UINT8 itx_x, UINT8 itx_y)
{
// These must all be 16-bit digits, as this
// causes incorrect calculations
UINT16 digit_itx;
UINT16 bit_itx;
UINT16 digit_to_display;
UINT16 remainder;
UINT16 bit_mask;
UINT8 dollar_itx_x;
UINT8 digit_tiles[WINDOW_MAX_DIGITS_BALANCE];
remainder = 0U;
for (digit_itx = 0; digit_itx != WINDOW_MAX_DIGITS_BALANCE; digit_itx ++)
{
digit_to_display = remainder;
bit_mask = 1U;
for (bit_itx = 0; bit_itx != BALANCE_BIT_LENGTH; bit_itx ++)
{
// Check if the current bit (in lower or upper balance, depending on
// current bit itx) is set
if (game_state.balance[bit_itx / 16U] & bit_mask)
{
digit_to_display += window_digit_b2d_lookup[digit_itx][bit_itx];
}
// If the end of first or second balance int has been reached,
// move back to masking first bit
if (bit_itx == 15U || bit_itx == 31U)
{
bit_mask = 1U;
}
else
{
// Otherwise, push bit mask in by 1.
bit_mask <<= 1;
}
}
digit_tiles[digit_itx] = digit_to_display % 10;
remainder = digit_to_display / 10U;
}
// Iterate through each of the digits in reverse, until a non-0 value has been found
// Re-use variables:
// bit_mask - whether a digit has been found
bit_mask = 0U;
for (digit_itx = WINDOW_MAX_DIGITS_BALANCE; digit_itx != 0; digit_itx --)
{
// If the current digit is non-0, a digit has already been found
// or this is the last digit, process the digit to be displayed
if (digit_tiles[digit_itx - 1] != 0 || bit_mask != 0 || digit_itx == 1)
{
if (bit_mask == 0U)
{
// Draw dollar sign
dollar_itx_x = itx_x - digit_itx;
tile_data[0] = MENU_TILE_DOLLAR;
if (itx_y == 0U)
set_win_tiles(dollar_itx_x, 0U, 1, 1, &(tile_data[0]));
else
set_bkg_tiles(dollar_itx_x, itx_y, 1, 1, &(tile_data[0]));
}
bit_mask = 1U;
// Draw number
tile_data[0] = MENU_TILE_0 + digit_tiles[digit_itx - 1];
}
else
{
// Otherwise, blank tile
tile_data[0] = 0x00;
}
// Display current tile
if (itx_y == 0U)
set_win_tiles(itx_x - (digit_itx - 1), 0U, 1, 1, &(tile_data[0]));
else
set_bkg_tiles(itx_x - (digit_itx - 1), itx_y, 1, 1, &(tile_data[0]));
}
return dollar_itx_x;
}
UINT8 show_number(UINT8 start_x, UINT8 start_y, UINT8 max_digits, unsigned int value)
{
UINT8 itx_x;
UINT8 itx;
UBYTE tile_data;
UINT8 last_digit_x_pos;
unsigned int current_digit = 0U;
itx_x = start_x + max_digits;
last_digit_x_pos = itx_x;
for (itx = 0; itx != max_digits; itx ++)
{
// If on last iteration, update digit with remainder
if (value != 0U || current_digit != 0U)
{
if (itx == (max_digits - 1U))
{
current_digit = value;
}
else
{
current_digit = value % 10U;
// Update remainder
value = value / 10U;
}
}
if (value == 0U && current_digit == 0U && itx != 0)
{
tile_data = 0x00;
}
else
{
tile_data = MENU_TILE_0 + current_digit;
last_digit_x_pos -= 1U;
}
// Display current digit
set_bkg_tiles(itx_x, start_y, 1, 1, &tile_data);
VBK_REG = 1;
// Mark as using palette 1 and high bank of tiles
tile_data = 0x1;
set_bkg_tiles(itx_x, start_y, 1, 1, &(tile_data));
VBK_REG = 0;
// Prepare for next digit
itx_x -= 1U;
}
return last_digit_x_pos;
}
void show_signed_number(UINT8 start_x, UINT8 start_y, UINT8 max_digits, INT8 value)
{
unsigned int pos_value;
UBYTE tile_data;
BOOLEAN is_negative = 0U;
if (value < 0)
{
is_negative = 1U;
pos_value = 0U - value;
}
else
pos_value = value;
// Show number using max_digits, except one, which will be used for negative symbol.
start_x = show_number(start_x + 1U, start_y, max_digits - 1U, pos_value);
tile_data = 0x00U;
if (is_negative)
tile_data = MENU_TILE_DASH;
set_bkg_tiles(start_x, start_y, 1, 1, &tile_data);
}
void update_window()
{
unsigned int current_digit, remainder, itx, itx_x;
unsigned short shown_symbol;
UBYTE clock_tile_data[5U];
// Screen is 20 tiles wide.
// Window is layed out as:
// Row 1:
// 2 tiles for top-half of clock
// 6 tiles for days passed, 5 numerics with symbol with right padding.
// 10 tiles for money, left padded (so starts by appearing in last 4 tiles). This allows for 100,000,000 with dollar symbol and 1,000,000,000 without.
// Row 2:
// 2 tiles for bottom-half of clock
// HP:
// - 5 tiles for HP
// - 1 tile for '/'
// - 5 tiles for max HP
// - 1 tile for HP symbol
// All of the above HP-stats are left-padded together.
VBK_REG = 0;
// CLOCK
tile_data[0] = MENU_TILE_CLOCK;
tile_data[1] = MENU_TILE_CLOCK;
tile_data[2] = MENU_TILE_CLOCK;
tile_data[3] = MENU_TILE_CLOCK;
set_win_tiles(0U, 0U, 2, 2, &(tile_data[0]));
VBK_REG = 1;
tile_data[0] = 0x21U;
set_win_tiles(0U, 0U, 1, 1, &(tile_data[0]));
tile_data[0] = 0x61U;
tile_data[1] = 0x41U;
set_win_tiles(0U, 1U, 2, 1, &(tile_data[0]));
VBK_REG = 0;
// Set 24 hour time e.g. 17:00
clock_tile_data[0] = MENU_TILE_0 + game_state.hour / 10U;
clock_tile_data[1] = MENU_TILE_0 + game_state.hour % 10U;
clock_tile_data[2] = MENU_TILE_COLON;
clock_tile_data[3] = MENU_TILE_0;
clock_tile_data[4] = MENU_TILE_0;
// Display time
set_win_tiles(2U, 1U, 5, 1, &clock_tile_data);
// DAYS PASSED
remainder = game_state.days_passed;
// Start at WINDOW_MAX_DIGITS_DAYS + margin from left
itx_x = WINDOW_MAX_DIGITS_DAYS + 1U;
for (itx = 0; itx != WINDOW_MAX_DIGITS_DAYS; itx ++)
{
// If on last iteration, update digit with remainder
if (itx == (WINDOW_MAX_DIGITS_DAYS - 1U))
{
current_digit = remainder;
}
else
{
current_digit = remainder % 10U;
// Update remainder
remainder = remainder / 10U;
}
if (remainder == 0U && current_digit == 0U && itx != 0)
break;
// Display current digit
tile_data[0] = MENU_TILE_0 + current_digit;
set_win_tiles(itx_x, 0U, 1, 1, &(tile_data[0]));
// Prepare for next digit
itx_x -= 1U;
}
// BALANCE
show_balance(3U + WINDOW_MAX_DIGITS_DAYS + WINDOW_MAX_DIGITS_BALANCE, 0U);
// HP
// Start at right hand side
itx_x = SCREEN_WIDTH_TILES - 2U;
// Draw HP symbol
tile_data[0] = MENU_TILE_HP;
set_win_tiles(itx_x, 1U, 1, 1, &(tile_data[0]));
itx_x -= 1U;
shown_symbol = 0U;
remainder = game_state.max_hp;
for (itx = 0; itx != WINDOW_MAX_DIGITS_HP; itx ++)
{
// If on last iteration, update digit with remainder
if (remainder != 0U || current_digit != 0U)
{
if (itx == (WINDOW_MAX_DIGITS_HP - 1U))
{
current_digit = remainder;
} else {
current_digit = remainder % 10U;
// Update remainder
remainder = remainder / 10U;
}
}
if (remainder == 0U && current_digit == 0U && itx != 0)
{
// Display slash symbol, if not already shown
if (shown_symbol == 0U)
{
tile_data[0] = MENU_TILE_SLASH;
shown_symbol = 1U;
// Once complete with max_hp, continue to actual HP value
remainder = game_state.hp;
// Set itx back to start. When using high number values (5 digits for each), then this may cause an issue
// with failing to remove the remaining digits.
itx = 0;
}
else
{
// Otherwise break
tile_data[0] = 0x00;
}
}
else
{
tile_data[0] = MENU_TILE_0 + current_digit;
}
// Display current digit
set_win_tiles(itx_x, 1U, 1, 1, &(tile_data[0]));
// Prepare for next digit
itx_x -= 1U;
}
}
void setup_window()
{
UBYTE tile_data[2U];
unsigned int itx_x, itx_y;
// Set transparency for all tiles
tile_data[0] = 0x00U;
// bit 0-2 palette
// bit 3 - tile bank
// bit 4 - unused
// bit 5 - horizontal flip
// bit 6 - verical flip
// bit 7 Set piority flag and color palette to 1
tile_data[1] = 0x81U;
for (itx_x = 0U; itx_x != SCREEN_WIDTH_TILES; itx_x ++)
{
for (itx_y = 0U; itx_y != SCREEN_HEIGHT_TILES; itx_y ++)
{
VBK_REG = 0;
set_win_tiles(itx_x, itx_y, 1, 1, &(tile_data[0]));
VBK_REG = 1;
set_win_tiles(itx_x, itx_y, 1, 1, &(tile_data[1]));
}
}
VBK_REG = 0;
// Setup borders
tile_data[0] = 0U;
set_win_tiles(0U, 0U, 1U, 1U, &(tile_data[0]));
set_win_tiles(0U, 1U, 1U, 1U, &(tile_data[0]));
set_win_tiles(19U, 0U, 1U, 1U, &(tile_data[0]));
set_win_tiles(19U, 1U, 1U, 1U, &(tile_data[0]));
// Setup 'days''
tile_data[0] = MENU_TILE_DA;
set_win_tiles(WINDOW_MAX_DIGITS_DAYS + 2U, 0U, 1U, 1U, &(tile_data[0]));
tile_data[0] = MENU_TILE_YS;
set_win_tiles(WINDOW_MAX_DIGITS_DAYS + 3U, 0U, 1U, 1U, &(tile_data[0]));
// Move window up to only display 2 rows at top of screen
move_win(7, (SCREEN_HEIGHT_TILES - 2U) << 3);
}
/*
* show_stats_screen
*
* Show game statistics screen
*/
void show_stats_screen()
{
game_state.current_building = S_B_STATS;
setup_building_menu(1U, ROM_BANK_BUILDING_MENU);
// Update tiles for each of the stats to display the current values
// Intelligence
// MENU_ITEM_SCREEN_OFFSET_LEFT, MENU_ITEM_SCREEN_OFFSET_TOP + 3U (for second item)
show_number(3U, 6U, 3U, game_state.intelligence);
show_number(11U, 6U, 3U, game_state.strength);
show_number(3U, 9U, 3U, game_state.charm);
show_signed_number(11U, 9U, 3U, game_state.karma);
}