Skip to content

Commit

Permalink
code review: add REQUESTER_ITEM
Browse files Browse the repository at this point in the history
  • Loading branch information
walkawayy committed Jun 30, 2024
1 parent bd31f66 commit 3997055
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 48 deletions.
2 changes: 1 addition & 1 deletion src/game/console_cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@ static COMMAND_RESULT Console_Cmd_LoadGame(const char *args)
return CR_FAILURE;
}

if (g_SavegameRequester.is_item_blocked[slot_idx]) {
if (g_SavegameRequester.items[slot_idx].is_blocked) {
Console_Log(GS(OSD_LOAD_GAME_FAIL_UNAVAILABLE_SLOT), slot_num);
return CR_FAILURE;
}
Expand Down
24 changes: 9 additions & 15 deletions src/game/option/option_passport.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,10 @@ static REQUEST_INFO m_NewGameRequester = {
.pix_width = 162,
.line_height = TEXT_HEIGHT + 7,
.is_blockable = false,
.is_item_blocked = NULL,
.x = 0,
.y = 0,
.heading_text = NULL,
.item_texts = NULL,
0,
.items = NULL,
};

static REQUEST_INFO m_SelectLevelRequester = {
Expand All @@ -76,12 +74,10 @@ static REQUEST_INFO m_SelectLevelRequester = {
.pix_width = 292,
.line_height = TEXT_HEIGHT + 7,
.is_blockable = false,
.is_item_blocked = NULL,
.x = 0,
.y = -32,
.heading_text = NULL,
.item_texts = NULL,
0,
.items = NULL,
};

REQUEST_INFO g_SavegameRequester = {
Expand All @@ -94,12 +90,10 @@ REQUEST_INFO g_SavegameRequester = {
.pix_width = 292,
.line_height = TEXT_HEIGHT + 7,
.is_blockable = false,
.is_item_blocked = NULL,
.x = 0,
.y = -32,
.heading_text = NULL,
.item_texts = NULL,
0,
.items = NULL,
};

static void Option_PassportInitText(void);
Expand Down Expand Up @@ -409,7 +403,7 @@ static void Option_PassportLoadGame(void)
m_PassportStatus.mode = PASSPORT_MODE_LOAD_GAME;
}
} else if (m_PassportStatus.mode == PASSPORT_MODE_LOAD_GAME) {
if (!g_SavegameRequester.is_item_blocked[g_SavegameRequester.requested]
if (!g_SavegameRequester.items[g_SavegameRequester.requested].is_blocked
|| !g_SavegameRequester.is_blockable) {
if (g_InputDB.menu_right) {
g_GameInfo.current_save_slot = g_SavegameRequester.requested;
Expand All @@ -426,10 +420,10 @@ static void Option_PassportLoadGame(void)
Text_SetPos(
m_Text[TEXT_LEVEL_ARROW_RIGHT], 130,
g_SavegameRequester
.texts
.items
[g_SavegameRequester.requested
- g_SavegameRequester.line_offset]
->pos.y);
.content->pos.y);
Text_Hide(m_Text[TEXT_LEVEL_ARROW_RIGHT], false);
} else {
Text_Hide(m_Text[TEXT_LEVEL_ARROW_RIGHT], true);
Expand All @@ -440,7 +434,7 @@ static void Option_PassportLoadGame(void)
Text_Hide(m_Text[TEXT_LEVEL_ARROW_RIGHT], true);
}

if (g_SavegameRequester.is_item_blocked[g_SavegameRequester.requested]
if (g_SavegameRequester.items[g_SavegameRequester.requested].is_blocked
&& g_SavegameRequester.is_blockable) {
Text_Hide(m_Text[TEXT_LEVEL_ARROW_RIGHT], true);
}
Expand All @@ -465,10 +459,10 @@ static void Option_PassportSelectLevel(void)
Text_SetPos(
m_Text[TEXT_LEVEL_ARROW_LEFT], -130,
m_SelectLevelRequester
.texts
.items
[m_SelectLevelRequester.requested
- m_SelectLevelRequester.line_offset]
->pos.y);
.content->pos.y);
Text_Hide(m_Text[TEXT_LEVEL_ARROW_LEFT], false);
} else {
Text_Hide(m_Text[TEXT_LEVEL_ARROW_LEFT], true);
Expand Down
4 changes: 1 addition & 3 deletions src/game/phase/phase_pause.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,10 @@ static REQUEST_INFO m_PauseRequester = {
.pix_width = 160,
.line_height = TEXT_HEIGHT + 7,
.is_blockable = false,
.is_item_blocked = NULL,
.x = 0,
.y = 0,
.heading_text = NULL,
.item_texts = NULL,
0,
.items = NULL,
};

static void Phase_Pause_RemoveText(void);
Expand Down
53 changes: 27 additions & 26 deletions src/game/requester.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,23 @@ static void Requester_SetItem(
REQUEST_INFO *req, const int32_t idx, const bool is_blocked,
const char *const fmt, va_list va)
{
if (req->item_texts[idx]) {
Memory_FreePointer(&req->item_texts[idx]);
if (req->items[idx].content_text) {
Memory_FreePointer(&req->items[idx].content_text);
}

va_list va_dup;
va_copy(va_dup, va);
const size_t out_size = vsnprintf(NULL, 0, fmt, va) + 1;
req->item_texts[idx] = Memory_Alloc(sizeof(char) * out_size);
vsnprintf(req->item_texts[idx], out_size, fmt, va_dup);
req->is_item_blocked[idx] = is_blocked;
req->items[idx].content_text = Memory_Alloc(sizeof(char) * out_size);
vsnprintf(req->items[idx].content_text, out_size, fmt, va_dup);
req->items[idx].is_blocked = is_blocked;
va_end(va_dup);
}

void Requester_Init(REQUEST_INFO *req, const uint16_t max_items)
{
req->max_items = max_items;
req->is_item_blocked = Memory_Alloc(sizeof(bool) * max_items);
req->item_texts = Memory_Alloc(sizeof(char *) * max_items);
req->items = Memory_Alloc(sizeof(REQUESTER_ITEM) * max_items);
Requester_ClearTextstrings(req);
}

Expand All @@ -50,11 +49,10 @@ void Requester_Shutdown(REQUEST_INFO *req)
Requester_ClearTextstrings(req);

Memory_FreePointer(&req->heading_text);
Memory_FreePointer(&req->is_item_blocked);
for (int i = 0; i < req->max_items; i++) {
Memory_FreePointer(&req->item_texts[i]);
Memory_FreePointer(&req->items[i].content_text);
}
Memory_FreePointer(&req->item_texts);
Memory_FreePointer(&req->items);
}

void Requester_ClearTextstrings(REQUEST_INFO *req)
Expand All @@ -68,9 +66,9 @@ void Requester_ClearTextstrings(REQUEST_INFO *req)
Text_Remove(req->moredown);
req->moredown = NULL;

for (int i = 0; i < MAX_REQLINES; i++) {
Text_Remove(req->texts[i]);
req->texts[i] = NULL;
for (int i = 0; i < req->max_items; i++) {
Text_Remove(req->items[i].content);
req->items[i].content = NULL;
}

req->items_used = 0;
Expand Down Expand Up @@ -157,35 +155,38 @@ int32_t Requester_Display(REQUEST_INFO *req)
}

for (int i = 0; i < line_qty; i++) {
if (!req->texts[i]) {
req->texts[i] = Text_Create(
if (!req->items[i].content) {
req->items[i].content = Text_Create(
0, line_one_off + req->line_height * i,
req->item_texts[req->line_offset + i]);
Text_CentreH(req->texts[i], 1);
Text_AlignBottom(req->texts[i], 1);
req->items[req->line_offset + i].content_text);
Text_CentreH(req->items[i].content, 1);
Text_AlignBottom(req->items[i].content, 1);
}
if (req->line_offset + i == req->requested) {
Text_AddBackground(
req->texts[i], req->pix_width - BOX_PADDING - 1 * BOX_BORDER, 0,
0, 0, TS_REQUESTED);
Text_AddOutline(req->texts[i], true, TS_REQUESTED);
req->items[i].content,
req->pix_width - BOX_PADDING - 1 * BOX_BORDER, 0, 0, 0,
TS_REQUESTED);
Text_AddOutline(req->items[i].content, true, TS_REQUESTED);
} else {
Text_RemoveBackground(req->texts[i]);
Text_RemoveOutline(req->texts[i]);
Text_RemoveBackground(req->items[i].content);
Text_RemoveOutline(req->items[i].content);
}
}

if (req->line_offset != req->line_old_offset) {
for (int i = 0; i < line_qty; i++) {
if (req->texts[i]) {
if (req->items[i].content) {
Text_ChangeText(
req->texts[i], req->item_texts[req->line_offset + i]);
req->items[i].content,
req->items[req->line_offset + i].content_text);
;
}
}
}

if (g_InputDB.menu_confirm) {
if (req->is_item_blocked[req->requested] && req->is_blockable) {
if (req->is_blockable && req->items[req->requested].is_blocked) {
g_Input = (INPUT_STATE) { 0 };
return 0;
} else {
Expand Down
10 changes: 7 additions & 3 deletions src/global/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -1822,6 +1822,12 @@ typedef struct BOX_INFO {
int16_t overlap_index;
} BOX_INFO;

typedef struct {
bool is_blocked;
char *content_text;
TEXTSTRING *content;
} REQUESTER_ITEM;

typedef struct REQUEST_INFO {
uint16_t items_used;
uint16_t max_items;
Expand All @@ -1832,16 +1838,14 @@ typedef struct REQUEST_INFO {
uint16_t pix_width;
uint16_t line_height;
bool is_blockable;
bool *is_item_blocked;
int16_t x;
int16_t y;
char *heading_text;
char **item_texts;
TEXTSTRING *heading;
TEXTSTRING *background;
TEXTSTRING *moreup;
TEXTSTRING *moredown;
TEXTSTRING *texts[MAX_REQLINES];
REQUESTER_ITEM *items;
} REQUEST_INFO;

typedef struct IMOTION_INFO {
Expand Down

0 comments on commit 3997055

Please sign in to comment.