From 26107501a9c975a7b39b22402316f3e8cd8d0d8e Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Thu, 1 Aug 2024 23:00:54 -0500 Subject: Move things out of main --- src/screen.c | 118 +++++++++++++++++++++++++++++++++-------------------------- src/screen.h | 5 ++- 2 files changed, 70 insertions(+), 53 deletions(-) diff --git a/src/screen.c b/src/screen.c index d695ee7..8393e07 100644 --- a/src/screen.c +++ b/src/screen.c @@ -93,7 +93,9 @@ static struct funcholder userfuncs[] = { }; void intercleanup(int signum) { - signum++; // So gcc doesn't yell at me + // Only want to handle interrupts + if(signum != SIGINT) + abort(); endwin(); error(0, 0, "[VX-GAMBLEGROUND] Caught interrupt"); @@ -110,24 +112,6 @@ static const struct sigaction handlers[] = { } }; -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; -} - -// Find the result of x + offset and constrict it to the range [min, max]. x must be >= min -static int rangemod(int x, int offset, int min, int max) { - return ((x - min + offset) % (max - min + 1)) + min; -} - -static int init_rgb_color(int colornum, int red, int green, int blue) { - int nred = normalize(red, RGB_MIN, RGB_MAX, CURSESCOLOR_MIN, CURSESCOLOR_MAX); - int ngreen = normalize(green, RGB_MIN, RGB_MAX, CURSESCOLOR_MIN, CURSESCOLOR_MAX); - int nblue = normalize(blue, RGB_MIN, RGB_MAX, CURSESCOLOR_MIN, CURSESCOLOR_MAX); - - return init_color(colornum, nred, ngreen, nblue); -} - // Initialize sodium, curses, and set the proper locale. Exits on error, returns 0 otherwise static int doinit(void) { if(sodium_init() < 0) @@ -201,6 +185,41 @@ static WINDOW* create_banner(int col, int randomnum) { return phrase; } +// I will figure out how to properly pass a void pointer eventually (if I care enough) +static int init_items(ITEM *items[], const char *menuopts[], size_t menuopts_size, struct funcholder usrptrs[]) { + for(size_t i = 0; i < menuopts_size; i++) { + items[i] = new_item(menuopts[i], NULL); + set_item_userptr(items[i], (void*)&usrptrs[i]); + } + items[menuopts_size] = NULL; + + return 0; +} + +static int init_custom_menu_format(WINDOW *menuholder, MENU *menu, const int fmtdim[2], Menu_Options toggleon, Menu_Options toggleoff) { + // Set menu options & format + set_menu_format(menu, fmtdim[0], fmtdim[1]); + menu_opts_on(menu, toggleon); + menu_opts_off(menu, toggleoff); + + int holder1 = -1, holder2 = -1; + getmaxyx(menuholder, holder1, holder2); + if(holder1 < 0 || holder2 < 0) { + endwin(); + error(1, errno, "[VX-GAMBLEGROUND] Could not get bounds for menu subwindow"); + } + set_menu_win(menu, menuholder); + set_menu_sub(menu, derwin(menuholder, holder1, holder2, 0, 0)); + + set_menu_mark(menu, NULL); + post_menu(menu); + wbkgd(menuholder, COLOR_PAIR(CCP_BANNER)); + set_menu_back(menu, COLOR_PAIR(CCP_BANNER)); + wnoutrefresh(menuholder); + + return 0; +} + int main() { doinit(); docolors(); @@ -234,44 +253,20 @@ int main() { } WINDOW *banner = create_banner(col, randomnum); + init_items(items, menu_choices, STATIC_ARRSIZE(menu_choices), userfuncs); - - // Set up menu items - for(long unsigned int i = 0; i < STATIC_ARRSIZE(menu_choices); i++) { - items[i] = new_item(menu_choices[i], NULL); - set_item_userptr(items[i], (void*)&userfuncs[i]); - } - items[STATIC_ARRSIZE(menu_choices)] = NULL; - + // No point in moving this into a function menu = new_menu(items); if(menu == NULL) { endwin(); error(1, errno, "Could not create menu"); } - // Set menu options & format - set_menu_format(menu, 1, col); - menu_opts_on(menu, O_ONEVALUE | O_IGNORECASE); - menu_opts_off(menu, O_SHOWDESC | O_NONCYCLIC); - + // Set up the menuholder & init everything menuholder = newwin(1, col, row - 1, 0); - keypad(menuholder, TRUE); - - - int holder1 = -1, holder2 = -1; - getmaxyx(menuholder, holder1, holder2); - if(holder1 < 0 || holder2 < 0) { - endwin(); - error(1, errno, "[VX-GAMBLEGROUND] Could not get bounds for menu subwindow"); - } - set_menu_win(menu, menuholder); - set_menu_sub(menu, derwin(menuholder, holder1, holder2, 0, 0)); + keypad(menuholder, TRUE); + init_custom_menu_format(menuholder, menu, (int []){1, col}, O_ONEVALUE | O_IGNORECASE, O_SHOWDESC | O_NONCYCLIC); - set_menu_mark(menu, NULL); - post_menu(menu); - wbkgd(menuholder, COLOR_PAIR(CCP_BANNER)); - set_menu_back(menu, COLOR_PAIR(CCP_BANNER)); - wnoutrefresh(menuholder); WINDOW *slots = newwin(row - 2, col, 1, 0); if(slots == NULL) { @@ -310,9 +305,6 @@ int main() { break; case KEY_ENTER: case FUCKED_UP_ENTER: // Enter - //wclear(menuholder); - //mvwaddstr(menuholder, 0, 0, "This shit brokey lmao"); - p = (struct funcholder *)item_userptr(current_item(menu)); switch(p->type) { case FH_SPIN: @@ -333,6 +325,9 @@ int main() { } break; + + default: + break; } } @@ -351,7 +346,7 @@ int main() { int spin(void *spinp) { struct spinp *p = (struct spinp *)spinp; - struct timespec sleeper = { + static const struct timespec sleeper = { .tv_sec = 0, .tv_nsec = 1000000000 /* Nanoseconds in 1 second */ / NUMCOLORPAIRS }; @@ -406,4 +401,23 @@ int quit(void *quitp) { exit(0); return 0; +} + + + +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; +} + +int rangemod(int x, int offset, int min, int max) { + return ((x - min + offset) % (max - min + 1)) + min; +} + +static int init_rgb_color(int colornum, int red, int green, int blue) { + int nred = normalize(red, RGB_MIN, RGB_MAX, CURSESCOLOR_MIN, CURSESCOLOR_MAX); + int ngreen = normalize(green, RGB_MIN, RGB_MAX, CURSESCOLOR_MIN, CURSESCOLOR_MAX); + int nblue = normalize(blue, RGB_MIN, RGB_MAX, CURSESCOLOR_MIN, CURSESCOLOR_MAX); + + return init_color(colornum, nred, ngreen, nblue); } \ No newline at end of file diff --git a/src/screen.h b/src/screen.h index c3deb00..5afbe6e 100644 --- a/src/screen.h +++ b/src/screen.h @@ -103,7 +103,10 @@ struct funcholder { }; // Converts value from within the range of [oldmin, oldmax] to a value within the range [newmin, newmax] -static float normalize(float value, float oldmin, float oldmax, float newmin, float newmax); +float normalize(float value, float oldmin, float oldmax, float newmin, float newmax); + +// Find the result of x + offset and constrain it to the range [min, max]. x must be >= min +int rangemod(int x, int offset, int min, int max); // 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); -- cgit v1.2.3