summaryrefslogtreecommitdiff
path: root/src/screen.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/screen.c')
-rw-r--r--src/screen.c118
1 files changed, 66 insertions, 52 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[] = {
93}; 93};
94 94
95void intercleanup(int signum) { 95void intercleanup(int signum) {
96 signum++; // So gcc doesn't yell at me 96 // Only want to handle interrupts
97 if(signum != SIGINT)
98 abort();
97 99
98 endwin(); 100 endwin();
99 error(0, 0, "[VX-GAMBLEGROUND] Caught interrupt"); 101 error(0, 0, "[VX-GAMBLEGROUND] Caught interrupt");
@@ -110,24 +112,6 @@ static const struct sigaction handlers[] = {
110 } 112 }
111}; 113};
112 114
113static float normalize(float value, float oldmin, float oldmax, float newmin, float newmax) {
114 // x(normal) = (b - a) * ((x - x(min)) / (max x - min x)) + a, where [a, b] is the new range
115 return (newmax - newmin) * ((value - oldmin) / (oldmax - oldmin)) + newmin;
116}
117
118// Find the result of x + offset and constrict it to the range [min, max]. x must be >= min
119static int rangemod(int x, int offset, int min, int max) {
120 return ((x - min + offset) % (max - min + 1)) + min;
121}
122
123static int init_rgb_color(int colornum, int red, int green, int blue) {
124 int nred = normalize(red, RGB_MIN, RGB_MAX, CURSESCOLOR_MIN, CURSESCOLOR_MAX);
125 int ngreen = normalize(green, RGB_MIN, RGB_MAX, CURSESCOLOR_MIN, CURSESCOLOR_MAX);
126 int nblue = normalize(blue, RGB_MIN, RGB_MAX, CURSESCOLOR_MIN, CURSESCOLOR_MAX);
127
128 return init_color(colornum, nred, ngreen, nblue);
129}
130
131// Initialize sodium, curses, and set the proper locale. Exits on error, returns 0 otherwise 115// Initialize sodium, curses, and set the proper locale. Exits on error, returns 0 otherwise
132static int doinit(void) { 116static int doinit(void) {
133 if(sodium_init() < 0) 117 if(sodium_init() < 0)
@@ -201,6 +185,41 @@ static WINDOW* create_banner(int col, int randomnum) {
201 return phrase; 185 return phrase;
202} 186}
203 187
188// I will figure out how to properly pass a void pointer eventually (if I care enough)
189static int init_items(ITEM *items[], const char *menuopts[], size_t menuopts_size, struct funcholder usrptrs[]) {
190 for(size_t i = 0; i < menuopts_size; i++) {
191 items[i] = new_item(menuopts[i], NULL);
192 set_item_userptr(items[i], (void*)&usrptrs[i]);
193 }
194 items[menuopts_size] = NULL;
195
196 return 0;
197}
198
199static int init_custom_menu_format(WINDOW *menuholder, MENU *menu, const int fmtdim[2], Menu_Options toggleon, Menu_Options toggleoff) {
200 // Set menu options & format
201 set_menu_format(menu, fmtdim[0], fmtdim[1]);
202 menu_opts_on(menu, toggleon);
203 menu_opts_off(menu, toggleoff);
204
205 int holder1 = -1, holder2 = -1;
206 getmaxyx(menuholder, holder1, holder2);
207 if(holder1 < 0 || holder2 < 0) {
208 endwin();
209 error(1, errno, "[VX-GAMBLEGROUND] Could not get bounds for menu subwindow");
210 }
211 set_menu_win(menu, menuholder);
212 set_menu_sub(menu, derwin(menuholder, holder1, holder2, 0, 0));
213
214 set_menu_mark(menu, NULL);
215 post_menu(menu);
216 wbkgd(menuholder, COLOR_PAIR(CCP_BANNER));
217 set_menu_back(menu, COLOR_PAIR(CCP_BANNER));
218 wnoutrefresh(menuholder);
219
220 return 0;
221}
222
204int main() { 223int main() {
205 doinit(); 224 doinit();
206 docolors(); 225 docolors();
@@ -234,44 +253,20 @@ int main() {
234 } 253 }
235 254
236 WINDOW *banner = create_banner(col, randomnum); 255 WINDOW *banner = create_banner(col, randomnum);
256 init_items(items, menu_choices, STATIC_ARRSIZE(menu_choices), userfuncs);
237 257
238 258 // No point in moving this into a function
239 // Set up menu items
240 for(long unsigned int i = 0; i < STATIC_ARRSIZE(menu_choices); i++) {
241 items[i] = new_item(menu_choices[i], NULL);
242 set_item_userptr(items[i], (void*)&userfuncs[i]);
243 }
244 items[STATIC_ARRSIZE(menu_choices)] = NULL;
245
246 menu = new_menu(items); 259 menu = new_menu(items);
247 if(menu == NULL) { 260 if(menu == NULL) {
248 endwin(); 261 endwin();
249 error(1, errno, "Could not create menu"); 262 error(1, errno, "Could not create menu");
250 } 263 }
251 264
252 // Set menu options & format 265 // Set up the menuholder & init everything
253 set_menu_format(menu, 1, col);
254 menu_opts_on(menu, O_ONEVALUE | O_IGNORECASE);
255 menu_opts_off(menu, O_SHOWDESC | O_NONCYCLIC);
256
257 menuholder = newwin(1, col, row - 1, 0); 266 menuholder = newwin(1, col, row - 1, 0);
258 keypad(menuholder, TRUE); 267 keypad(menuholder, TRUE);
259 268 init_custom_menu_format(menuholder, menu, (int []){1, col}, O_ONEVALUE | O_IGNORECASE, O_SHOWDESC | O_NONCYCLIC);
260
261 int holder1 = -1, holder2 = -1;
262 getmaxyx(menuholder, holder1, holder2);
263 if(holder1 < 0 || holder2 < 0) {
264 endwin();
265 error(1, errno, "[VX-GAMBLEGROUND] Could not get bounds for menu subwindow");
266 }
267 set_menu_win(menu, menuholder);
268 set_menu_sub(menu, derwin(menuholder, holder1, holder2, 0, 0));
269 269
270 set_menu_mark(menu, NULL);
271 post_menu(menu);
272 wbkgd(menuholder, COLOR_PAIR(CCP_BANNER));
273 set_menu_back(menu, COLOR_PAIR(CCP_BANNER));
274 wnoutrefresh(menuholder);
275 270
276 WINDOW *slots = newwin(row - 2, col, 1, 0); 271 WINDOW *slots = newwin(row - 2, col, 1, 0);
277 if(slots == NULL) { 272 if(slots == NULL) {
@@ -310,9 +305,6 @@ int main() {
310 break; 305 break;
311 306
312 case KEY_ENTER: case FUCKED_UP_ENTER: // Enter 307 case KEY_ENTER: case FUCKED_UP_ENTER: // Enter
313 //wclear(menuholder);
314 //mvwaddstr(menuholder, 0, 0, "This shit brokey lmao");
315
316 p = (struct funcholder *)item_userptr(current_item(menu)); 308 p = (struct funcholder *)item_userptr(current_item(menu));
317 switch(p->type) { 309 switch(p->type) {
318 case FH_SPIN: 310 case FH_SPIN:
@@ -333,6 +325,9 @@ int main() {
333 } 325 }
334 326
335 break; 327 break;
328
329 default:
330 break;
336 } 331 }
337 } 332 }
338 333
@@ -351,7 +346,7 @@ int main() {
351int spin(void *spinp) { 346int spin(void *spinp) {
352 struct spinp *p = (struct spinp *)spinp; 347 struct spinp *p = (struct spinp *)spinp;
353 348
354 struct timespec sleeper = { 349 static const struct timespec sleeper = {
355 .tv_sec = 0, 350 .tv_sec = 0,
356 .tv_nsec = 1000000000 /* Nanoseconds in 1 second */ / NUMCOLORPAIRS 351 .tv_nsec = 1000000000 /* Nanoseconds in 1 second */ / NUMCOLORPAIRS
357 }; 352 };
@@ -406,4 +401,23 @@ int quit(void *quitp) {
406 exit(0); 401 exit(0);
407 402
408 return 0; 403 return 0;
404}
405
406
407
408float normalize(float value, float oldmin, float oldmax, float newmin, float newmax) {
409 // x(normal) = (b - a) * ((x - x(min)) / (max x - min x)) + a, where [a, b] is the new range
410 return (newmax - newmin) * ((value - oldmin) / (oldmax - oldmin)) + newmin;
411}
412
413int rangemod(int x, int offset, int min, int max) {
414 return ((x - min + offset) % (max - min + 1)) + min;
415}
416
417static int init_rgb_color(int colornum, int red, int green, int blue) {
418 int nred = normalize(red, RGB_MIN, RGB_MAX, CURSESCOLOR_MIN, CURSESCOLOR_MAX);
419 int ngreen = normalize(green, RGB_MIN, RGB_MAX, CURSESCOLOR_MIN, CURSESCOLOR_MAX);
420 int nblue = normalize(blue, RGB_MIN, RGB_MAX, CURSESCOLOR_MIN, CURSESCOLOR_MAX);
421
422 return init_color(colornum, nred, ngreen, nblue);
409} \ No newline at end of file 423} \ No newline at end of file