Dumb-ways-to-memorize
2D game
Data Structures | Macros | Enumerations | Functions | Variables
menu.h File Reference
#include "globals.h"
#include "graphics.h"
#include "parseobject.h"
+ Include dependency graph for menu.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  menu_item_t
 
struct  menu_t
 

Macros

#define MENU_ITEM_MAX   20
 
#define MAX_MENUS   10
 
#define MENU_BACKGROUND   "Background"
 
#define MENU_ITEMS   "Items"
 
#define MENU_ITEM_SPRITE   "sprite"
 
#define MENU_ITEM_TEXT   "text"
 
#define MENU_ITEM_LINK   "link"
 
#define MENU_ITEM_EXTRA   "extra"
 
#define MENU_TYPE   "Type"
 
#define MENU_TYPE_STR_POWER   "power_select"
 
#define MENU_TYPE_STR_CHOOSE   "choose_power"
 
#define MENU_TYPE_STR_H   "horizontal"
 
#define MENU_TYPE_STR_V   "vertical"
 
#define MENU_TYPE_STR_GRID   "grid"
 

Enumerations

enum  menu_type_t {
  MENU_TYPE_NULL = 0x0,
  MENU_TYPE_H = 0x1,
  MENU_TYPE_V = 0x2,
  MENU_TYPE_GRID = 0x4,
  MENU_TYPE_POWER = 0x8,
  MENU_TYPE_CHOOSE = 0x10,
  MENU_TYPE_MAX = 0x20
}
 

Functions

int InitMenuSystem ()
 
menu_t * LoadMenu (object_t *object, char *g_str, GameState curr_state, GameState previous_state)
 
menu_t * FindMenuFromGameState (GameState curr_state)
 
menu_t * FindFreeMenu ()
 
menu_item_t * FindMenuItem (menu_t *menu, char *item)
 

Variables

menu_t * gMenus
 

Macro Definition Documentation

#define MAX_MENUS   10

Definition at line 9 of file menu.h.

Referenced by FindFreeMenu(), FindMenuFromGameState(), and InitMenuSystem().

#define MENU_BACKGROUND   "Background"

Definition at line 10 of file menu.h.

Referenced by LoadMenu().

#define MENU_ITEM_EXTRA   "extra"

Definition at line 15 of file menu.h.

Referenced by LoadMenu().

#define MENU_ITEM_LINK   "link"

Definition at line 14 of file menu.h.

Referenced by LoadMenu().

#define MENU_ITEM_MAX   20

Definition at line 8 of file menu.h.

Referenced by LoadMenu().

#define MENU_ITEM_SPRITE   "sprite"

Definition at line 12 of file menu.h.

Referenced by LoadMenu().

#define MENU_ITEM_TEXT   "text"

Definition at line 13 of file menu.h.

Referenced by LoadMenu().

#define MENU_ITEMS   "Items"

Definition at line 11 of file menu.h.

Referenced by LoadMenu().

#define MENU_TYPE   "Type"

Definition at line 16 of file menu.h.

Referenced by LoadMenu().

#define MENU_TYPE_STR_CHOOSE   "choose_power"

Definition at line 18 of file menu.h.

Referenced by LoadMenu(), and StrToMenuType().

#define MENU_TYPE_STR_GRID   "grid"

Definition at line 21 of file menu.h.

Referenced by StrToMenuType().

#define MENU_TYPE_STR_H   "horizontal"

Definition at line 19 of file menu.h.

Referenced by StrToMenuType().

#define MENU_TYPE_STR_POWER   "power_select"

Definition at line 17 of file menu.h.

Referenced by LoadMenu(), and StrToMenuType().

#define MENU_TYPE_STR_V   "vertical"

Definition at line 20 of file menu.h.

Referenced by LoadMenu(), and StrToMenuType().

Enumeration Type Documentation

Defines the enum for what type of menu it is

Enumerator
MENU_TYPE_NULL 
MENU_TYPE_H 

An enum constant representing the menu type - horizontal

MENU_TYPE_V 

An enum constant representing the menu type - vertical

MENU_TYPE_GRID 

An enum constant representing the menu type - grid

MENU_TYPE_POWER 

An enum constant representing the menu type - half circle for power selection

MENU_TYPE_CHOOSE 

An enum constant representing the menu - same as power, but used to differ the menu operations

MENU_TYPE_MAX 

Definition at line 24 of file menu.h.

25 {
26  MENU_TYPE_NULL = 0x0,
27  MENU_TYPE_H = 0x1, /**< An enum constant representing the menu type - horizontal */
28  MENU_TYPE_V = 0x2, /**< An enum constant representing the menu type - vertical */
29  MENU_TYPE_GRID = 0x4, /**< An enum constant representing the menu type - grid */
30  MENU_TYPE_POWER = 0x8, /**< An enum constant representing the menu type - half circle for power selection*/
31  MENU_TYPE_CHOOSE = 0x10, /**< An enum constant representing the menu - same as power, but used to differ the menu operations */
32  MENU_TYPE_MAX = 0x20
33 

Function Documentation

menu_t* FindFreeMenu ( )

Searches for the first free menu slot in gMenus, used for loading new menus.

Returns
null if it fails, else the found free menu.
Author
Anthony Rios
Date
3/30/2016

Definition at line 659 of file menu.c.

References gMenus, and MAX_MENUS.

Referenced by LoadMenu().

660 {
661  int i, *menuCmp;
662  if(!gMenus)
663  {
664  printf("Menu System not initialized");
665  return NULL;
666  }
667  menuCmp = (int*) malloc(sizeof(menu_t));
668  memset(menuCmp, 0, sizeof(menu_t));
669  for(i = 0; i < MAX_MENUS; i++)
670  {
671  if(!memcmp(menuCmp, &gMenus[i], sizeof(menu_t)))
672  {
673  return &gMenus[i];
674  }
675  }
676  return NULL;
677 }
menu_t* FindMenuFromGameState ( GameState  curr_state)

Searches for the first menu from game state in gMenus.

Parameters
curr_stateState of the curr.
Returns
null if it fails, else the found menu from game state.
Author
Anthony Rios
Date
3/16/2016

Definition at line 637 of file menu.c.

References gMenus, MAX_MENUS, and Update().

Referenced by LoadMenu().

638 {
639  int i;
640  if(!gMenus)
641  {
642  printf("Menu Sys not Initialized");
643  return NULL;
644  }
645  for(i = 0; i < MAX_MENUS; i++)
646  {
647  if(!gMenus[i].Update)
648  {
649  continue;
650  }
651  if(gMenus[i].mCurrentState == curr_state)
652  {
653  return &gMenus[i];
654  }
655  }
656  return NULL;
657 }
void Update()
Definition: game.c:458
menu_item_t* FindMenuItem ( menu_t *  menu,
char *  item 
)

Searches for the first menu item, with name of str given.

Parameters
[in,out]menuIf non-null, the menu.
[in,out]itemIf non-null, the item.
Returns
null if it fails, else the found menu item.
Author
Anthony Rios
Date
3/30/2016

Definition at line 679 of file menu.c.

References CountMem().

Referenced by LoadMenu().

680 {
681  int i, items;
682  items = CountMem(menu->mItems, sizeof(menu_item_t));
683  for(i = 0; i < items; i++)
684  {
685  if(!menu->mItems[i].Name)
686  {
687  continue;
688  }
689  if(!strcmp(item, menu->mItems[i].Name))
690  {
691  return &menu->mItems[i];
692  }
693  }
694  return NULL;
695 }
int CountMem(void *src, int size_type)
Definition: mymath.c:51
int InitMenuSystem ( )

Allocate memor for menu system, gMenus is the menu list.

Author
Anthony Rios
Date
3/16/2016

Definition at line 299 of file menu.c.

References gMenus, and MAX_MENUS.

Referenced by LoadMenuData(), and Setup().

300 {
301  if(gMenus)
302  {
303  printf("gMenus already initialized");
304  return -1;
305  }
306  gMenus = (menu_t*) malloc(sizeof(menu_t)*MAX_MENUS);
307  if(!gMenus)
308  {
309  printf("Allocate menu error");
310  return -1;
311  }
312  memset(gMenus, 0, sizeof(menu_t)*MAX_MENUS);
313  return 0;
314 }
menu_t* LoadMenu ( object_t *  object,
char *  g_str,
GameState  curr_state,
GameState  previous_state 
)

Loads a menu.

Parameters
[in,out]objectIf non-null, the object menu was parsed to.
[in,out]g_strIf non-null, the string.
curr_stateState to assign the menu.
previous_stateState before this menu.
Returns
0 if loads correctly, -1 if it doesn't.
Author
Anthony Rios
Date
3/16/2016

Definition at line 478 of file menu.c.

References CountMem(), CountObjectChildren(), DrawMenuByNum(), DrawMenuByState(), FindFreeMenu(), FindMenuFromGameState(), FindMenuItem(), FindObject(), FindValue(), gSelectedPowerUps, GUESS, LoadSprite(), MENU_BACKGROUND, MENU_ITEM_EXTRA, MENU_ITEM_LINK, MENU_ITEM_MAX, MENU_ITEM_SPRITE, MENU_ITEM_STATE_NOT_SELECTED, MENU_ITEM_STATE_SELECTED, MENU_ITEM_TEXT, MENU_ITEMS, MENU_TYPE, MENU_TYPE_STR_CHOOSE, MENU_TYPE_STR_POWER, MENU_TYPE_STR_V, PLAYING, ProcessMenuItemsByType(), SPLASH, StrToGameState(), StrToMenuType(), UpdatePowerSelectMenu(), UpdatePowerUpMenu(), and UpdateVerticalMenu().

Referenced by LoadMenuData(), and UpdatePowerUpMenu().

479 {
480  menu_t *menu, *ref_menu;
481  menu_item_t *ref_menu_item;
482  object_t *temp_obj;
483  char *temp_str, *type_str, *bg_str;
484  int i, temp_i, choose_i;
485 
486  //Check Vars given
487  if(!object || !g_str)
488  {
489  printf("No object / str given to load menu \n");
490  return NULL;
491  }
492 
493  //Look for free menu slot
494  menu = FindFreeMenu();
495  if(!menu)
496  {
497  printf("Could not find free menu \n");
498  return NULL;
499  }
500 
501  menu->mPreviousState = previous_state;
502  menu->mCurrentState = curr_state;
503 
504  temp_obj = FindObject(object, MENU_ITEMS);
505  if(!temp_obj)
506  {
507  printf("No items in menu %s \n", object->name);
508  return NULL;
509  }
510 
511  //Load Background
512  bg_str = FindValue(object, MENU_BACKGROUND, g_str);
513  if(!bg_str)
514  {
515  printf("No background loaded for menu %s \n", object->name);
516  } else
517  {
518  menu->mBackground = LoadSprite(bg_str, 0);
519  }
520 
521  //Check menu type
522  type_str = FindValue(object, MENU_TYPE, g_str);
523  if(!type_str)
524  {
525  printf("Not found menu layout type for %s. Switching to default vertical layout", object->name);
526  type_str = strdup(MENU_TYPE_STR_V);
527  }
528 
529  //Load Items
530  temp_i = CountObjectChildren(temp_obj);
531  if(temp_i >= MENU_ITEM_MAX)
532  {
533  printf("Max menu items for menu %s \n", object->name);
534  temp_i = MENU_ITEM_MAX -1;
535  }
536 
537  menu->mItemCount = temp_i;
538  for(i = 0; i < temp_i; i++)
539  {
540  menu->mItems[i].State = MENU_ITEM_STATE_NOT_SELECTED;
541  menu->mItems[i].Info = NULL;
542 
543  temp_str = FindValue(&temp_obj->children[i], MENU_ITEM_SPRITE, g_str);
544  if(temp_str)
545  {
546  menu->mItems[i].Image = LoadSprite(temp_str, 0);
547  }
548 
549  temp_str = FindValue(&temp_obj->children[i], MENU_ITEM_TEXT, g_str);
550  if(temp_str)
551  {
552  menu->mItems[i].Name = temp_str;
553  }
554 
555  temp_str = FindValue(&temp_obj->children[i], MENU_ITEM_LINK, g_str);
556  if(temp_str)
557  {
558  menu->mItems[i].NextState = StrToGameState(temp_str);
559  if(temp_str) free(temp_str);
560  } else
561  {
562  menu->mItems[i].NextState = SPLASH;
563  }
564 
565  temp_str = FindValue(&temp_obj->children[i], MENU_ITEM_EXTRA, g_str);
566  if(temp_str)
567  {
568  menu->mItems[i].Info = temp_str;
569  if(!menu->mItems[i].Name)
570  {
571  menu->mItems[i].Name = temp_str;
572  }
573  } else
574  {
575  menu->mItems[i].Info = NULL;
576  }
577 
578  }
579 
580  //Load extra items
581  if(!strcmp(type_str, MENU_TYPE_STR_CHOOSE))
582  {
583 
584  choose_i = CountMem(gSelectedPowerUps, sizeof(char*));
585  ref_menu = FindMenuFromGameState(GUESS);
586  if(!ref_menu)
587  {
588  printf("Trouble loading choose menu , no GUESS menu found \n");
589  }
590  else
591  {
592  for(i = 0; i < choose_i; i++)
593  {
594  ref_menu_item = FindMenuItem(ref_menu, gSelectedPowerUps[i]);
595  if(!ref_menu_item)
596  {
597  continue;
598  }
599  memcpy(&menu->mItems[menu->mItemCount+i], ref_menu_item, sizeof(menu_item_t));
600  menu->mItems[menu->mItemCount+i].State = MENU_ITEM_STATE_NOT_SELECTED;
601  menu->mItems[menu->mItemCount+i].NextState = PLAYING;
602  }
603  menu->mItemCount += choose_i;
604  }
605 
606  }
607 
608  //Calculate Menu Item Positions
609  ProcessMenuItemsByType(menu->mItems, (menu_type_t) StrToMenuType(type_str));
610  menu->mSelectedItem = menu->mItems;
611  menu->mSelectedItem->State = MENU_ITEM_STATE_SELECTED;
612  //gCurrentSelectedItem = 0;
613 
614  //Set Up Different Menu Types
615  if(!strcmp(type_str, MENU_TYPE_STR_V))
616  {
617  menu->Update = UpdateVerticalMenu;
618  menu->Draw = DrawMenuByNum;
619  } else if(!strcmp(type_str, MENU_TYPE_STR_POWER))
620  {
621  menu->Update = UpdatePowerUpMenu;
622  menu->Draw = DrawMenuByState;
623  } else if (!strcmp(type_str, MENU_TYPE_STR_CHOOSE))
624  {
625  menu->Update = UpdatePowerSelectMenu;
626  menu->Draw = DrawMenuByState;
627  } else
628  {
629  menu->Update = UpdateVerticalMenu;
630  menu->Draw = DrawMenuByNum;
631  }
632 
633  return menu;
634 
635 }
int CountMem(void *src, int size_type)
Definition: mymath.c:51
Definition: globals.h:87
int CountObjectChildren(object_t *obj)
Definition: parseobject.c:170
GameState StrToGameState(char *str)
Definition: mystrings.c:274
object_t * FindObject(object_t *obj, char *name)
Definition: parseobject.c:120
char ** gSelectedPowerUps
Definition: game.c:28
sprite_t * LoadSprite(const char *name, int flags)
Definition: graphics.c:107
char * FindValue(struct object_s *obj, char *key, char *g_str)
Definition: mystrings.c:53
Definition: globals.h:85

Variable Documentation

menu_t* gMenus