From 1d46925315b40b1bdbc44d1714c366589d153948 Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Sat, 27 Jul 2024 00:59:30 -0500 Subject: Lord forgive me for this terrible code --- src/screen.c | 77 ++++++++++++++++++++++++++++++++++++++++++++---------------- src/screen.h | 33 ++++++++++++++++++++++---- 2 files changed, 85 insertions(+), 25 deletions(-) (limited to 'src') diff --git a/src/screen.c b/src/screen.c index 77b1a3b..626b1b1 100644 --- a/src/screen.c +++ b/src/screen.c @@ -83,6 +83,12 @@ static const char *menu_choices[] = { "Quit" }; +static struct funcholder userfuncs[] = { + {.callback = spin, .type = FH_SPIN, .params = {.spinp = {.bannerwin = NULL, .phrasenum = 0}}}, + {.callback = buy, .type = FH_BUY, .params = {.buyp = {}}}, + {.callback = quit, .type = FH_QUIT, .params = {.quitp = {}}} +}; + static float normalize(float value, float oldmin, float oldmax, float newmin, float newmax) { // x(normal) = (b - a) * ((x - x(min)) / (max x - min x)) + a, where [a, b] is the new range return (newmax - newmin) * ((value - oldmin) / (oldmax - oldmin)) + newmin; @@ -132,7 +138,7 @@ static int docolors(void) { init_rgb_color(CC_WHITE, 255, 255, 255); init_pair(CCP_BANNER, CC_WHITE, CURSES_BLUE); - init_pair(CCP_TESTING, CC_RED, CC_WHITE); + init_pair(CCP_TESTING, CC_WHITE, CC_RED); } else { endwin(); error(1, ENOTSUP, "[VX-GAMBLEGROUND] Colors are not supported on your terminal"); @@ -141,6 +147,21 @@ static int docolors(void) { return 0; } +static WINDOW* create_banner(int col, int randomnum) { + // Create the banner window + WINDOW *phrase = newwin(1, col, 0, 0); + if(phrase == NULL) { + endwin(); + error(1, errno, "[VX-GAMBLEGROUND] Could not create banner window"); + } + wbkgd(phrase, COLOR_PAIR(1)); + mvwaddstr(phrase, 0, 1, "VX-GAMBLEGROUND: "); + waddstr(phrase, phrases[randomnum]); + wrefresh(phrase); + + return phrase; +} + int main() { doinit(); docolors(); @@ -152,7 +173,6 @@ int main() { // Variable definitions - struct funcholder funcholder[STATIC_ARRSIZE(menu_choices)]; // Stores the function pointers used for the menu ITEM *items[STATIC_ARRSIZE(menu_choices) + 1]; // An array of ITEM pointers large enough to store all the menu items int holder1 = -1, holder2 = -1; // Holders for the max yx of the menu subwindow struct funcholder *p = NULL; // Function pointer used for callbacks in the menu driver @@ -176,23 +196,13 @@ int main() { error(1, errno, "[VX-GAMBLEGROUND] Call to randombytes_uniform failed, can't get random phrase"); } - // Create the banner window - phrase = newwin(1, col, 0, 0); - if(phrase == NULL) { - endwin(); - error(1, errno, "[VX-GAMBLEGROUND] Could not create banner window"); - } - wbkgd(phrase, COLOR_PAIR(1)); - mvwaddstr(phrase, 0, 1, "VX-GAMBLEGROUND: "); - waddstr(phrase, phrases[randomnum]); - wrefresh(phrase); + WINDOW *banner = create_banner(col, randomnum); // Set up menu items for(long unsigned int i = 0; i < STATIC_ARRSIZE(menu_choices); i++) { - funcholder[i].callback = USERFUNCS[i]; items[i] = new_item(menu_choices[i], NULL); - set_item_userptr(items[i], (void*)(&funcholder[i])); + set_item_userptr(items[i], (void*)&userfuncs[i]); } items[STATIC_ARRSIZE(menu_choices)] = NULL; @@ -236,6 +246,9 @@ int main() { mvwaddstr(slots, (row - 2) / 2, (col / 2) - (strlen(sampletext) / 2), sampletext); wrefresh(slots); + userfuncs[0].params.spinp.bannerwin = banner; + userfuncs[0].params.spinp.phrasenum = randomnum; + // Get user input and deal with the menu while((c = wgetch(menuholder)) != KEY_F(4)) { switch(c) { @@ -260,7 +273,23 @@ int main() { //mvwaddstr(menuholder, 0, 0, "This shit brokey lmao"); p = (struct funcholder *)item_userptr(current_item(menu)); - p->callback(); + switch(p->type) { + case FH_SPIN: + p->callback((void*)&p->params.spinp); + break; + + case FH_BUY: + p->callback((void*)&p->params.buyp); + break; + + case FH_QUIT: + p->callback((void*)&p->params.quitp); + break; + + default: + endwin(); + error(1, ENOTSUP, "SHIT BROKE"); + } break; } @@ -278,21 +307,29 @@ int main() { } // Spin the wheel -int spin(void) { - endwin(); - error(1, 0, "spin"); +int spin(void *spinp) { + struct spinp *p = (struct spinp *)spinp; + + // Change the color to testing for the banner + wbkgd(p->bannerwin, COLOR_PAIR(CCP_TESTING)); + mvwaddstr(p->bannerwin, 0, 1, "VX-GAMBLEGROUND: "); + waddstr(p->bannerwin, phrases[p->phrasenum]); + wnoutrefresh(p->bannerwin); + + doupdate(); + return 0; } // Increase number of spins -int buy(void) { +int buy(void *buyp) { endwin(); error(1, 0, "buy"); return 0; } // Quit out -int quit(void) { +int quit(void *quitp) { endwin(); error(1, 0, "quit"); return 1; diff --git a/src/screen.h b/src/screen.h index 5d0837e..9419d5c 100644 --- a/src/screen.h +++ b/src/screen.h @@ -53,7 +53,31 @@ enum customcolor_pairs { }; struct funcholder { - int (*callback)(void); + int (*callback)(void*); + enum type { + FH_UNSPEC, + + FH_SPIN, + FH_BUY, + FH_QUIT, + + FH_TOOBIG + } type; + + union param { + struct spinp { + WINDOW *bannerwin; + int phrasenum; + } spinp; + + struct buyp { + + } buyp; + + struct quitp { + + } quitp; + } params; }; @@ -63,9 +87,8 @@ static float normalize(float value, float oldmin, float oldmax, float newmin, fl // Initialize a new curses color using standard rgb values (0-255) instead of curses' 0-1000 range static int init_rgb_color(int colornum, int red, int green, int blue); -int spin(void); -int buy(void); -int quit(void); -#define USERFUNCS (int (* [])(void)){spin, buy, quit} +int spin(void *spinp); +int buy(void *buyp); +int quit(void *quitp); #endif \ No newline at end of file -- cgit v1.2.3