Skip to content

Commit ba76991

Browse files
committed
Refactor how status bars are drawn
1 parent 6ad2712 commit ba76991

File tree

1 file changed

+29
-25
lines changed

1 file changed

+29
-25
lines changed

src/lib/engine.c

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <stdlib.h>
2020
#include <string.h>
2121
#include <stdbool.h>
22+
#include <assert.h>
2223

2324
#include "compress.h"
2425
#include "engine.h"
@@ -387,7 +388,7 @@ static void sub_19C7(uint8_t val, uint16_t di);
387388
static void sub_1A10();
388389
static void sub_1A72();
389390
static void draw_player_status(uint8_t val);
390-
static void sub_1BF8(uint8_t color, uint8_t y_adjust);
391+
static void draw_player_stat(uint8_t color, uint8_t y_adjust, uint16_t player_record_offset);
391392
static void sub_1C70(unsigned char *src_ptr);
392393
static void sub_280E();
393394
static void sub_28B0(uint16_t flags, unsigned char *src_ptr, const unsigned char *base);
@@ -4202,22 +4203,19 @@ static void draw_player_status(uint8_t val)
42024203
if (found == 0) {
42034204
// 1B53 (abnormal status not found)
42044205
uint8_t dl = 2; // health (red)
4205-
cpu.bx = 0x14; // Player record offset
42064206
al = 8;
42074207
cpu.ax = (cpu.ax & 0xFF00) | al;
4208-
sub_1BF8(dl, al);
4208+
draw_player_stat(dl, al, 0x14);
42094209

42104210
// 0x1B5D
42114211
dl = 3; // stun (green)
4212-
cpu.bx = 0x18; // Player record offset
42134212
al = 0x0B;
42144213
cpu.ax = (cpu.ax & 0xFF00) | al;
4215-
sub_1BF8(dl, al);
4214+
draw_player_stat(dl, al, 0x18);
42164215

42174216
dl = 4; // magic power (blue)
4218-
cpu.bx = 0x1C; // Player record offset
42194217
al = 0x0E;
4220-
sub_1BF8(dl, al);
4218+
draw_player_stat(dl, al, 0x1C);
42214219

42224220
al = byte_1BE5;
42234221
fill_color = al;
@@ -6503,17 +6501,22 @@ void run_engine()
65036501
}
65046502

65056503
// Extracts player record value and stores it in word_11C0.
6506-
static int sub_1C57(uint16_t offset)
6504+
// 0x1C57
6505+
static uint16_t get_player_record(uint16_t offset)
65076506
{
65086507
unsigned char *c960 = get_player_data_base();
65096508

6510-
cpu.ax = c960[player_base_offset - 0xC960 + offset];
6511-
cpu.ax += (c960[player_base_offset - 0xC960 + 1 + offset]) << 8;
6509+
uint16_t val = c960[player_base_offset - 0xC960 + offset];
6510+
val += (c960[player_base_offset - 0xC960 + 1 + offset]) << 8;
65126511

6513-
word_11C0 = cpu.ax;
6514-
return cpu.ax;
6512+
word_11C0 = val;
6513+
6514+
return val;
65156515
}
65166516

6517+
// Inputs: 11C0, 11C2, 11C4
6518+
// Side effects:
6519+
// sets 11C4, 11C6, and 11C8
65176520
static void sub_11A0(int set_11C4)
65186521
{
65196522
uint32_t result;
@@ -6592,25 +6595,26 @@ static void sub_11CE()
65926595
}
65936596
}
65946597

6595-
static void sub_1BF8(uint8_t color, uint8_t y_adjust)
6598+
// 0x1BF8
6599+
static void draw_player_stat(uint8_t color, uint8_t y_adjust, uint16_t player_record_offset)
65966600
{
65976601
uint16_t fill_color;
65986602

65996603
fill_color = color;
66006604
cpu.ax = draw_point.y + y_adjust;
66016605
g_linenum = cpu.ax; // line number
6602-
if (sub_1C57(cpu.bx) != 0) {
6603-
cpu.bx += 2; // now check max?
6604-
push_word(cpu.bx);
6605-
word_11C2 = 0x17; // max percentage?
6606-
sub_11A0(0);
6607-
cpu.bx = pop_word();
6608-
if (sub_1C57(cpu.bx) != 0) {
6609-
// Calculate percentage of 0x17 that ratio is.
6610-
sub_11CE();
6611-
cpu.ax = word_11C6;
6612-
cpu.ax++;
6613-
}
6606+
6607+
uint16_t cur_val = get_player_record(player_record_offset);
6608+
uint16_t max_val = get_player_record(player_record_offset + 2);
6609+
6610+
cpu.ax = cur_val;
6611+
6612+
// Calculate ratio
6613+
if (cur_val != 0 && max_val != 0) {
6614+
assert(cur_val <= max_val);
6615+
6616+
cpu.ax = (cur_val * 23) / max_val;
6617+
cpu.ax++;
66146618
}
66156619

66166620
// Draw color portion of status bar.

0 commit comments

Comments
 (0)