Skip to content

Commit

Permalink
Remove hard-coding of menu position
Browse files Browse the repository at this point in the history
This allows the background function to set the position through setting
the cursor position before returning. This will eventually allow the
title screen code to be folded into the menu system. To do this we need
to add a `GetCursorPosition()` function.
  • Loading branch information
fragglet committed Nov 30, 2024
1 parent 241c028 commit a0874c7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
25 changes: 12 additions & 13 deletions src/swmenu.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ void FullscreenBackground(void *_title)

swposcur(1, 22);
swputs(" ESC - Exit Menu");

swposcur(0, 5);
}

static void DrawConfigOption(const struct menuitem *item)
Expand Down Expand Up @@ -160,20 +162,22 @@ static void DrawConfigOption(const struct menuitem *item)
static void DrawMenu(const struct menu *menu)
{
const struct menuitem *items = menu->items;
int i, y, keynum, said_key = 0;
int i, base_y, y, keynum;
char buttons[32];
int num_buttons = 0;

Vid_ClearBuf();
swposcur(0, 0);

if (menu->draw_background != NULL) {
menu->draw_background(menu->draw_background_data);
}

GetCursorPosition(NULL, &base_y);
swcolor(3);

for (i=0, y=0, keynum=0; items[i].label != NULL; ++i, ++y) {
char *suffix;
char *prefix, *suffix;
char buf[40];
int key;

Expand All @@ -182,32 +186,27 @@ static void DrawMenu(const struct menu *menu)
}

key = items[i].key;
prefix = i == 0 ? "Key:" : "";
suffix = "";

if (key == '1') {
key = menukeys[keynum];
++keynum;
suffix = ":";

if (!said_key) {
swposcur(0, 5+y);
swputs("Key:");
said_key = 1;
}
}
if (strstr(items[i].label, ">>>")) {
swcolor(2);
} else {
swcolor(3);
}
snprintf(buf, sizeof(buf), "%c - %s%s",
key, items[i].label, suffix);
snprintf(buf, sizeof(buf), "%-5s%c - %s%s",
prefix, key, items[i].label, suffix);

swposcur(5, 5+y);
swposcur(0, base_y + y);
swputs(buf);
swcolor(3);

if (strlen(buf) > 22) {
if (strlen(buf) > 27) {
++y;
}

Expand All @@ -219,7 +218,7 @@ static void DrawMenu(const struct menu *menu)
continue;
}
if (items[i].callback == ToggleConfigOption) {
swposcur(28, 5+y);
swposcur(28, base_y + y);
DrawConfigOption(&items[i]);
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/swtext.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,16 @@ void swposcur(int a, int b)
cur_y = b;
}

void GetCursorPosition(int *x, int *y)
{
if (x != NULL) {
*x = cur_x;
}
if (y != NULL) {
*y = cur_y;
}
}

int swgetc(void)
{
int i;
Expand Down
1 change: 1 addition & 0 deletions src/swtext.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ extern void swputs(const char *sp );
extern void swgets(char *s, int max);
extern void swcolor(int a);
extern void swposcur(int a, int b);
extern void GetCursorPosition(int *x, int *y);
extern int swgetc(void);
extern void swflush(void);

Expand Down

0 comments on commit a0874c7

Please sign in to comment.