1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- #include <stdlib.h>
- #include "Menu.h"
- menu CreateMenu() {
- menu m = NULL;
- m = malloc(sizeof(Menu));
- m->choices = NULL;
- m->choices = calloc( NCHOICES + 1, sizeof(item) );
- if( m->choices == NULL ) return NULL;
- int i;
- for( i = 0; i < NCHOICES; ++i ) {
- m->choices[i] = new_item( MenuEntries[i].name, MenuEntries[i].description );
- }
- m->choices[NCHOICES] = NULL;
- m->framework = new_menu( m->choices );
- m->win = CreateWindow( 10, 40, 10, 20 );
- keypad( m->win, TRUE );
- set_menu_win( m->framework, m->win );
- set_menu_sub( m->framework, derwin( m->win, 8, 36, 2, 2 ) );
- set_menu_mark( m->framework, "" );
- box( m->win, 0, 0 );
- wrefresh(m->win);
- return m;
- }
- void PurgeMenu( menu m ) {
- int i = 0;
- while( m->choices[i] != NULL ) {
- DESTROY(m->choices[i]);
- ++i;
- }
- DESTROY(m->framework);
- }
- void LaunchMenu() {
- menu m = CreateMenu();
- post_menu(m->framework);
- int c;
- int quit = FALSE;
- item curr = malloc(sizeof(ITEM));
- wrefresh(m->win);
- while( !quit ) {
- c = getch();
- switch(c) {
- case KEY_DOWN: case 'j':
- menu_driver(m->framework, REQ_DOWN_ITEM);
- break;
- case KEY_UP: case 'k':
- menu_driver(m->framework, REQ_UP_ITEM);
- break;
- case 10: // Enter
- if(!curr) return;
- curr = current_item(m->framework);
- if( strcmp( "Quit", item_name(curr) ) == 0 ) {
- quit = TRUE;
- } else if( strcmp( "Load", item_name(curr) ) == 0 ) {
- unpost_menu(m->framework);
- RunGame(TRUE); // Load = TRUE.
- post_menu(m->framework);
- } else if( strcmp( "New", item_name(curr) ) == 0 ) {
- unpost_menu(m->framework);
- RunGame(FALSE); // Load = FALSE.
- post_menu(m->framework);
- }
- break;
- }
- wrefresh(m->win);
- }
- DESTROY(curr);
- PurgeMenu(m);
- DESTROY(m);
- }
|