Menu.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include <stdlib.h>
  2. #include "Menu.h"
  3. menu CreateMenu() {
  4. menu m = NULL;
  5. m = malloc(sizeof(Menu));
  6. m->choices = NULL;
  7. m->choices = calloc( NCHOICES + 1, sizeof(item) );
  8. if( m->choices == NULL ) return NULL;
  9. int i;
  10. for( i = 0; i < NCHOICES; ++i ) {
  11. m->choices[i] = new_item( MenuEntries[i].name, MenuEntries[i].description );
  12. }
  13. m->choices[NCHOICES] = NULL;
  14. m->framework = new_menu( m->choices );
  15. m->win = CreateWindow( 10, 40, 10, 20 );
  16. keypad( m->win, TRUE );
  17. set_menu_win( m->framework, m->win );
  18. set_menu_sub( m->framework, derwin( m->win, 8, 36, 2, 2 ) );
  19. set_menu_mark( m->framework, "" );
  20. box( m->win, 0, 0 );
  21. wrefresh(m->win);
  22. return m;
  23. }
  24. void PurgeMenu( menu m ) {
  25. int i = 0;
  26. while( m->choices[i] != NULL ) {
  27. DESTROY(m->choices[i]);
  28. ++i;
  29. }
  30. DESTROY(m->framework);
  31. }
  32. void LaunchMenu() {
  33. menu m = CreateMenu();
  34. post_menu(m->framework);
  35. int c;
  36. int quit = FALSE;
  37. item curr = malloc(sizeof(ITEM));
  38. wrefresh(m->win);
  39. while( !quit ) {
  40. c = getch();
  41. switch(c) {
  42. case KEY_DOWN: case 'j':
  43. menu_driver(m->framework, REQ_DOWN_ITEM);
  44. break;
  45. case KEY_UP: case 'k':
  46. menu_driver(m->framework, REQ_UP_ITEM);
  47. break;
  48. case 10: // Enter
  49. if(!curr) return;
  50. curr = current_item(m->framework);
  51. if( strcmp( "Quit", item_name(curr) ) == 0 ) {
  52. quit = TRUE;
  53. } else if( strcmp( "Load", item_name(curr) ) == 0 ) {
  54. unpost_menu(m->framework);
  55. RunGame(TRUE); // Load = TRUE.
  56. post_menu(m->framework);
  57. } else if( strcmp( "New", item_name(curr) ) == 0 ) {
  58. unpost_menu(m->framework);
  59. RunGame(FALSE); // Load = FALSE.
  60. post_menu(m->framework);
  61. }
  62. break;
  63. }
  64. wrefresh(m->win);
  65. }
  66. DESTROY(curr);
  67. PurgeMenu(m);
  68. DESTROY(m);
  69. }