Dumb-ways-to-memorize
2D game
Functions | Variables
game.c File Reference
#include "globals.h"
#include "player_controller.h"
#include "game.h"
#include "entity.h"
#include "mystrings.h"
#include "parseobject.h"
#include "parseentity.h"
#include "parsepowerup.h"
#include "parselevel.h"
#include "dumb_physics.h"
#include "player.h"
#include "graphics.h"
#include <SDL.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "menu.h"
+ Include dependency graph for game.c:

Go to the source code of this file.

Functions

int LoadGameData ()
 
int LoadEntityData ()
 
int LoadLevelData ()
 
int LoadPowerUpData ()
 
int LoadMenuData ()
 
int SelectLevels ()
 
void RandomizeSelectedLevels ()
 
int LoadSelectedLevel (int level)
 
void Poll ()
 
void UpdatePlaying ()
 
void Update ()
 
void DrawSplash ()
 
void DrawStart ()
 
void DrawGuess ()
 
void DrawChoose ()
 
void DrawPlaying ()
 
void Draw ()
 
int Setup ()
 
int Run ()
 
void Shutdown ()
 

Variables

int exitRequest = 0
 
int gDelay = 0
 
int gLevelsPerGame = LEVELS_DEFAULT
 
jsmn_parser gParser
 
char ** gLevels = NULL
 
char ** gSelectedLevels = NULL
 
char ** gSelectedPowerUps = NULL
 
char ** gUsedPowerUps = NULL
 
jsmntok_tgGameTokens
 
jsmntok_tgEntityTokens
 
jsmntok_tgLevelTokens
 
object_t * gGameObject
 
object_t * gEntityObject
 
object_t * gLevelObject
 
char * gGameData
 
char * gEntityData
 
char * gLevelData
 
char * gEntitiesFile = NULL
 
char * gPowerUpsFile = NULL
 
entity_t * gEntityDictionary
 
power_t * gPowerUps
 
char * gCurrentPowerUpName = NULL
 
GameState gGameState = SPLASH
 
sprite_t * gSplash = NULL
 
vec2_t gZeroPos = {0,0}
 
SDL_Event gEventQ
 
SDL_GameController * gController = NULL
 
SDL_GameControllerButton gButtonQ
 
unsigned int gCurrentTime = 0
 

Function Documentation

void Draw ( )

Definition at line 547 of file game.c.

References CHOOSE, DrawChoose(), DrawGuess(), DrawPlaying(), DrawSplash(), DrawStart(), gGameState, gRenderer, GUESS, PLAYING, SPLASH, and START.

Referenced by DrawEntities(), and Run().

548 {
549  SDL_RenderClear(gRenderer);
550  switch(gGameState)
551  {
552  case(SPLASH):
553  {
554  DrawSplash();
555  break;
556  }
557  case(START):
558  {
559  DrawStart();
560  break;
561  }
562  case(CHOOSE):
563  {
564  DrawChoose();
565  break;
566  }
567  case(GUESS):
568  {
569  DrawGuess();
570  break;
571  }
572  case(PLAYING):
573  {
574  DrawPlaying();
575  break;
576  }
577  default:
578  break;
579  }
580  SDL_RenderPresent(gRenderer);
581  return;
582 }
Definition: globals.h:87
SDL_Renderer * gRenderer
Definition: graphics.c:10
Definition: globals.h:88
void DrawPlaying()
Definition: game.c:763
void DrawGuess()
Definition: game.c:727
GameState gGameState
Definition: game.c:44
Definition: globals.h:86
void DrawChoose()
Definition: game.c:745
void DrawStart()
Definition: game.c:709
void DrawSplash()
Definition: game.c:691
Definition: globals.h:85
void DrawChoose ( )

Draws the screen for the player to choose which powerUp he wants for this level.

Author
Anthony Rios
Date
3/29/2016

Definition at line 745 of file game.c.

References exitRequest, and gMenus.

Referenced by Draw(), and Update().

746 {
747  if(!gMenus)
748  {
749  printf("No menus loaded");
750  exitRequest = 1;
751  return;
752  }
753  gMenus[2].Draw(&gMenus[2]);
754 
755 }
int exitRequest
Definition: game.c:22
void DrawGuess ( )

Draws the screen to player for powers to choose for this run.

Author
Anthony Rios
Date
3/29/2016

Definition at line 727 of file game.c.

References exitRequest, and gMenus.

Referenced by Draw(), and Update().

728 {
729  if(!gMenus)
730  {
731  printf("No menus loaded");
732  exitRequest = 1;
733  return;
734  }
735  gMenus[1].Draw(&gMenus[1]);
736  return;
737 }
int exitRequest
Definition: game.c:22
void DrawPlaying ( )

Draws the main game state, which is playing the game.

Author
Anthony Rios
Date
3/29/2016

Definition at line 763 of file game.c.

References DrawEntities(), and DrawLevel().

Referenced by Draw(), and Update().

764 {
765  DrawLevel();
766  DrawEntities();
767  return;
768 }
void DrawEntities()
Definition: entity.c:264
void DrawLevel()
Definition: parselevel.c:248
void DrawSplash ( )

Draws the splash screen.

Author
Anthony Rios
Date
3/29/2016

Definition at line 691 of file game.c.

References DrawSprite(), gRenderer, and gSplash.

Referenced by Draw(), and Update().

692 {
693  if(gSplash)
694  {
695  if(DrawSprite(gSplash, NULL, &gZeroPos, gRenderer))
696  {
697  printf("Couldn't draw splash: %s \n", SDL_GetError());
698  }
699  }
700  return;
701 }
SDL_Renderer * gRenderer
Definition: graphics.c:10
vec2_t gZeroPos
Definition: game.c:46
sprite_t * gSplash
Definition: game.c:45
int DrawSprite(sprite_t *sprite, int *frame, vec2_t *position, SDL_Renderer *renderer)
Definition: graphics.c:152
void DrawStart ( )

Draws the start screen.

Author
Anthony Rios
Date
3/29/2016

Definition at line 709 of file game.c.

References exitRequest, and gMenus.

Referenced by Draw(), and Update().

710 {
711  if(!gMenus)
712  {
713  printf("No menus loaded");
714  exitRequest = 1;
715  return;
716  }
717  gMenus[0].Draw(&gMenus[0]);
718  return;
719 }
int exitRequest
Definition: game.c:22
int LoadEntityData ( )

Definition at line 96 of file game.c.

References ConvertFileToUseable(), CountMem(), FindKey(), FindNextFreeCachePos(), FindObject(), gEntitiesFile, gEntityData, gEntityObject, gGameData, gGameObject, Hazards_str, ParseToEntity(), ParseToObject(), ParseToStringArray(), and PrintObject().

Referenced by Setup().

97 {
98  int i, objects;
99  entity_t *temp_ent, *pos_ent;
100 
101  //Init Entity Parse
103  {
104  printf("Failure to Parse Entity Data \n");
105  return -1;
106  }
108  printf("Size of Entity tokens: %d \n", CountMem(gEntityTokens, sizeof(jsmntok_t)));
111 
112  //Set Entity Dictionary
113  objects = CountMem(gEntityObject->children, sizeof(object_t));
114 
115  for(i = 0; i < objects; i++)
116  {
117  //Set sprites for each object
118  if(FindKey(gEntityObject->children[i].keys, "sprite(s)", gEntityData))
119  {
120  temp_ent = ParseToEntity(&gEntityObject->children[i], gEntityData);
121  if(!temp_ent) continue;
122 
123  pos_ent = FindNextFreeCachePos();
124  memcpy(pos_ent, temp_ent, sizeof(entity_t));
125 
126  //if(temp) free(temp);
127  } else if ( FindObject(&gEntityObject->children[i], "sprite(s)") )
128  {
129  temp_ent = ParseToEntity(&gEntityObject->children[i], gEntityData);
130  if(!temp_ent) continue;
131 
132  pos_ent = FindNextFreeCachePos();
133  memcpy(pos_ent, temp_ent, sizeof(entity_t));
134  //if(temp) free(temp);
135  }
136  }
137  return 0;
138 }
Definition: jsmn.h:40
int CountMem(void *src, int size_type)
Definition: mymath.c:51
object_t * FindObject(object_t *obj, char *name)
Definition: parseobject.c:120
int ConvertFileToUseable(char *fileName, jsmn_parser *parser, char **stringStorage, jsmntok_t **jsmnStorage)
Definition: mystrings.c:192
jsmntok_t * gEntityTokens
Definition: game.c:31
char * gEntitiesFile
Definition: game.c:39
object_t * gGameObject
Definition: game.c:33
jsmntok_t * FindKey(jsmntok_t *token, char *key, char *g_str)
Definition: mystrings.c:10
object_t * gEntityObject
Definition: game.c:34
entity_t * ParseToEntity(object_t *object, char *str)
Definition: parseentity.c:70
void PrintObject(object_t *obj, char *g_str)
Definition: parseobject.c:188
char ** Hazards_str
Definition: entity.c:13
jsmn_parser gParser
Definition: game.c:25
char * gEntityData
Definition: game.c:37
char ** ParseToStringArray(struct object_s *object, char *str)
char * gGameData
Definition: game.c:36
object_t * ParseToObject(jsmntok_t *token, char *g_str)
Definition: parseobject.c:8
entity_t * FindNextFreeCachePos()
Definition: entity.c:250
int LoadGameData ( )

Loads game data from GameData.json, stored in gGameData.

Returns
state 0 on success, -1 on error.
Author
Anthony Rios
Date
1/31/2016

Definition at line 60 of file game.c.

References ConvertFileToUseable(), ENTITIES_FILE_STR, FindObject(), gEntitiesFile, gGameData, gGameObject, gPowerUpsFile, gScreenHeight, gScreenWidth, JsmnToString(), JSON_FILE, ParseToObject(), ParseToVec2(), POWER_UPS_STR, PrintObject(), SCREEN_STRING, vec2_t::x, and vec2_t::y.

Referenced by Setup().

61 {
62  vec2_t *screen;
63  object_t *temp_obj;
64  //Init GameData Parse
66  {
67  printf("Failure to Parse GameData");
68  return -1;
69  }
70 
71  //Debug Checks & Object Parse
73  if(!gGameObject)
74  {
75  printf("Object parse error");
76  return -1;
77  }
78 
79  //Set Screen Dims
81  if(temp_obj)
82  {
83  screen = ParseToVec2(temp_obj, gGameData);
84  gScreenWidth = screen->x;
85  gScreenHeight = screen->y;
86  printf("New Screen resolution : %d %d \n", gScreenWidth, gScreenHeight);
87  free(screen);
88  }
92 
93  return 0;
94 }
#define POWER_UPS_STR
Definition: globals.h:114
#define SCREEN_STRING
Definition: graphics.h:8
int y
Definition: globals.h:22
object_t * FindObject(object_t *obj, char *name)
Definition: parseobject.c:120
char * JsmnToString(jsmntok_t *token, char *g_str)
Definition: mystrings.c:34
vec2_t * ParseToVec2(struct object_s *object, char *str)
int ConvertFileToUseable(char *fileName, jsmn_parser *parser, char **stringStorage, jsmntok_t **jsmnStorage)
Definition: mystrings.c:192
int gScreenWidth
Definition: graphics.c:16
int gScreenHeight
Definition: graphics.c:17
char * gEntitiesFile
Definition: game.c:39
object_t * gGameObject
Definition: game.c:33
void PrintObject(object_t *obj, char *g_str)
Definition: parseobject.c:188
#define ENTITIES_FILE_STR
Definition: globals.h:106
#define JSON_FILE
Definition: game.h:7
jsmntok_t * gGameTokens
Definition: game.c:30
char * gPowerUpsFile
Definition: game.c:40
jsmn_parser gParser
Definition: game.c:25
int x
Definition: globals.h:21
char * gGameData
Definition: game.c:36
object_t * ParseToObject(jsmntok_t *token, char *g_str)
Definition: parseobject.c:8
Definition: globals.h:19
int LoadLevelData ( )

Loads level file names, and how many levels per game.

Returns
The level data.
Author
Anthony Rios
Date
3/29/2016

Definition at line 148 of file game.c.

References CountMem(), FindKey(), FindObject(), gGameData, gGameObject, gLevels, gLevelsPerGame, JsmnToInt(), JsmnToString(), LEVELS_DEFAULT, and LEVELS_NOT_DEFAULT.

Referenced by Setup().

149 {
150  object_t *levelObj;
151  int i, lvlInt;
152 
153  //Find Levels
154  levelObj = FindObject(gGameObject, "Levels");
156  if(gLevelsPerGame == -1)
157  {
159  if(!gLevelsPerGame)
161  }
162  if(!levelObj)
163  {
164  printf("No levels found in gameObject");
165  return -1;
166  }
167 
168  //Alloc
169  lvlInt = CountMem(levelObj->values, sizeof(jsmntok_t));
170  gLevels = (char**) malloc(sizeof(char*) * (lvlInt + 1));
171 
172  //Load
173  for(i = 0; i < lvlInt; i++)
174  {
175  gLevels[i] = JsmnToString(&levelObj->values[i], gGameData);
176  if(!gLevels[i])
177  {
178  continue;
179  }
180  printf("Level : %s \n", gLevels[i]);
181  }
182 
183  gLevels[lvlInt] = NULL;
184  return 0;
185 }
Definition: jsmn.h:40
char ** gLevels
Definition: game.c:26
int CountMem(void *src, int size_type)
Definition: mymath.c:51
object_t * FindObject(object_t *obj, char *name)
Definition: parseobject.c:120
char * JsmnToString(jsmntok_t *token, char *g_str)
Definition: mystrings.c:34
#define LEVELS_NOT_DEFAULT
Definition: game.h:10
object_t * gGameObject
Definition: game.c:33
jsmntok_t * FindKey(jsmntok_t *token, char *key, char *g_str)
Definition: mystrings.c:10
void JsmnToInt(jsmntok_t *token, char *str, int *dst)
Definition: mystrings.c:84
#define LEVELS_DEFAULT
Definition: game.h:12
jsmntok_t * gGameTokens
Definition: game.c:30
char * gGameData
Definition: game.c:36
int gLevelsPerGame
Definition: game.c:24
int LoadMenuData ( )

Loads menu data, from files given, which are put in gMenus

Returns
The menu data.
Author
Anthony Rios
Date
3/29/2016

Definition at line 250 of file game.c.

References ConvertFileToUseable(), CountMem(), FindObject(), FindValue(), gGameData, gGameObject, gMenus, InitMenuSystem(), JsmnToString(), LoadMenu(), ParseToObject(), PrintObject(), START, and StrToGameState().

Referenced by Setup().

251 {
252  int i, menuCount;
253  char *menuData, *menuLink, *temp_str;
254  jsmntok_t *menuTok;
255  object_t *menus, *menuObj;
256  if(!gMenus)
257  {
258  InitMenuSystem();
259  }
260  menus = FindObject(gGameObject, "Menus");
261  menuCount = CountMem(menus->values, sizeof(jsmntok_t)) - 1;
262  for(i = 0; i < menuCount; i++)
263  {
264  temp_str = JsmnToString(&menus->values[i], gGameData);
265  ConvertFileToUseable(temp_str, NULL, &menuData, &menuTok);
266  if(!menuData || !menuTok)
267  {
268  continue;
269  }
270  menuObj = ParseToObject(menuTok, menuData);
271  if(!menuObj)
272  {
273  free(temp_str);
274  continue;
275  }
276  PrintObject(menuObj, menuData);
277  menuObj->name = temp_str;
278  menuLink = FindValue(menuObj, "link", menuData);
279  if(!menuLink)
280  {
281  continue;
282  }
283 
284  LoadMenu(menuObj, menuData, StrToGameState(menuLink), START);
285  }
286  return 0;
287 }
Definition: jsmn.h:40
int CountMem(void *src, int size_type)
Definition: mymath.c:51
GameState StrToGameState(char *str)
Definition: mystrings.c:274
object_t * FindObject(object_t *obj, char *name)
Definition: parseobject.c:120
char * JsmnToString(jsmntok_t *token, char *g_str)
Definition: mystrings.c:34
int ConvertFileToUseable(char *fileName, jsmn_parser *parser, char **stringStorage, jsmntok_t **jsmnStorage)
Definition: mystrings.c:192
object_t * gGameObject
Definition: game.c:33
void PrintObject(object_t *obj, char *g_str)
Definition: parseobject.c:188
char * FindValue(struct object_s *obj, char *key, char *g_str)
Definition: mystrings.c:53
char * gGameData
Definition: game.c:36
object_t * ParseToObject(jsmntok_t *token, char *g_str)
Definition: parseobject.c:8
Definition: globals.h:86
int LoadPowerUpData ( )

After Load GameData, Before Menu. Loads the power_ups in the file given.

Returns
The power up data.
Author
Anthony Rios
Date
3/29/2016

Definition at line 196 of file game.c.

References ConvertFileToUseable(), CountMem(), FindObject(), gGameData, gGameObject, gPowerUps, gPowerUpsFile, JsmnToString(), ParseToObject(), and ParseToPowerUp().

Referenced by Setup().

197 {
198  jsmntok_t *power_tok;
199  object_t *powers, *temp_obj;
200  power_t *temp_power;
201  char *power_str;
202  int powerCount, i;
203 
204  temp_obj = FindObject(gGameObject, "PowerUps");
205  if(!temp_obj)
206  {
207  printf("Could not find powerups in GameData");
208  return -1;
209  }
210 
211  gPowerUpsFile = JsmnToString(temp_obj->values, gGameData);
212  ConvertFileToUseable(gPowerUpsFile, NULL, &power_str, &power_tok);
213  if(!power_str || !power_tok)
214  {
215  printf("Could not load power ups file : %s \n", gPowerUpsFile );
216  }
217 
218  powers = ParseToObject(power_tok, power_str);
219  powerCount = CountMem(powers->children, sizeof(object_t));
220  gPowerUps = (power_t*) malloc(sizeof(power_t)*( powerCount+1 ));
221  if(!gPowerUps)
222  {
223  printf("Could not allocate power ups");
224  return -1;
225  }
226 
227  for(i = 0; i < powerCount; i++)
228  {
229  temp_power = ParseToPowerUp(&powers->children[i], power_str);
230  if(!temp_power)
231  {
232  printf("Power up %d could not be loaded \n", i);
233  continue;
234  }
235  gPowerUps[i] = *temp_power;
236  if(temp_power) free(temp_power);
237  }
238  memset(&gPowerUps[powerCount], 0, sizeof(power_t));
239  return 0;
240 }
Definition: jsmn.h:40
int CountMem(void *src, int size_type)
Definition: mymath.c:51
power_t * ParseToPowerUp(object_t *power, char *g_str)
Definition: parsepowerup.c:157
object_t * FindObject(object_t *obj, char *name)
Definition: parseobject.c:120
char * JsmnToString(jsmntok_t *token, char *g_str)
Definition: mystrings.c:34
int ConvertFileToUseable(char *fileName, jsmn_parser *parser, char **stringStorage, jsmntok_t **jsmnStorage)
Definition: mystrings.c:192
power_t * gPowerUps
Definition: game.c:42
object_t * gGameObject
Definition: game.c:33
char * gPowerUpsFile
Definition: game.c:40
char * gGameData
Definition: game.c:36
object_t * ParseToObject(jsmntok_t *token, char *g_str)
Definition: parseobject.c:8
int LoadSelectedLevel ( int  level)

Loads selected level.

Parameters
levelThe level.
Returns
The selected level.
See also
LoadLevel , in parselevel.c
Author
Anthony Rios
Date
3/29/2016

Definition at line 390 of file game.c.

References ConvertFileToUseable(), gLevelData, gLevelObject, gSelectedLevels, LoadLevel(), and ParseToObject().

Referenced by UpdatePowerSelectMenu().

391 {
392  if(!gSelectedLevels)
393  {
394  printf("Levels not Selected \n");
395  return -1;
396  }
397  if(!gSelectedLevels[level])
398  {
399  printf("Level %d not found \n", level);
400  return -1;
401  }
403  if(!gLevelData || !gLevelTokens)
404  {
405  printf("Unable to parse level %s \n", gSelectedLevels[level]);
406  }
408  if(!gLevelObject)
409  {
410  printf("Unable to parse level %s to object \n", gSelectedLevels[level]);
411  }
412 
414  {
415  perror("Unable to Load level \n");
416  return -1;
417  }
418  return 0;
419 }
char ** gSelectedLevels
Definition: game.c:27
int ConvertFileToUseable(char *fileName, jsmn_parser *parser, char **stringStorage, jsmntok_t **jsmnStorage)
Definition: mystrings.c:192
char * gLevelData
Definition: game.c:38
int LoadLevel(object_t *level, char *g_str)
Definition: parselevel.c:11
jsmntok_t * gLevelTokens
Definition: game.c:32
jsmn_parser gParser
Definition: game.c:25
object_t * gLevelObject
Definition: game.c:35
object_t * ParseToObject(jsmntok_t *token, char *g_str)
Definition: parseobject.c:8
void Poll ( )

Polls for all events and handles them.

Author
Anthony Rios
Date
3/29/2016

Definition at line 427 of file game.c.

References BUTTON_NO_INPUT, exitRequest, gButtonQ, gController, gEventQ, and UpdatePlaying().

Referenced by Run().

428 {
429  if( SDL_PollEvent(&gEventQ) )
430  {
431  if(gEventQ.type == SDL_CONTROLLERDEVICEADDED || gEventQ.type == SDL_CONTROLLERDEVICEREMOVED)
432  {
433  gController = SDL_GameControllerOpen(0);
434  }
435  if(gEventQ.type == SDL_CONTROLLERBUTTONDOWN)
436  {
437  gButtonQ = (SDL_GameControllerButton) gEventQ.cbutton.button;
438  printf("Button Pressed : %d \n", gButtonQ);
439  } else
440  {
441  gButtonQ = (SDL_GameControllerButton) BUTTON_NO_INPUT;
442  }
443 
444  if( gEventQ.type == SDL_QUIT)
445  {
446  exitRequest = 1;
447  return;
448  }
449 
450  }
451 
452  return;
453 }
SDL_GameController * gController
Definition: game.c:48
SDL_GameControllerButton gButtonQ
Definition: game.c:49
#define BUTTON_NO_INPUT
int exitRequest
Definition: game.c:22
SDL_Event gEventQ
Definition: game.c:47
void RandomizeSelectedLevels ( )

Randomize selected levels.

Author
Anthony Rios
Date
2/19/2016

Definition at line 343 of file game.c.

References CompareMemToMemArray(), CountMem(), gLevels, gLevelsPerGame, and gSelectedLevels.

344 {
345  int i, rand_i, *no_repeats;
346  char **slevel_copy;
347 
348  if (CountMem(gLevels, sizeof(char*)) < gLevelsPerGame)
349  {
350  printf("Levels less than min, Easy Mode - No Rand");
351  return;
352  }
353 
354  //Get a copy of gSelected
355  slevel_copy = (char**) malloc(sizeof(char*)*(gLevelsPerGame+1));
356  memcpy(slevel_copy, gSelectedLevels, sizeof(char*)*gLevelsPerGame);
357  slevel_copy[gLevelsPerGame] = 0;
358 
359  //No Repeats
360  no_repeats = (int*) malloc(sizeof(int)*gLevelsPerGame+1);
361  memset(no_repeats, 0, sizeof(int)*(gLevelsPerGame+1));
362 
363  //Randomize
364  for(i = 0; gSelectedLevels[i]; i++)
365  {
366  rand_i = rand()%gLevelsPerGame;
367  while(!CompareMemToMemArray(&rand_i, no_repeats, sizeof(int), gLevelsPerGame ))
368  {
369  rand_i = rand()%gLevelsPerGame;
370  }
371  no_repeats[i] = rand_i;
372  gSelectedLevels[i] = slevel_copy[rand_i];
373  }
374 
375  free(no_repeats);
376  free(slevel_copy);
377 }
char ** gLevels
Definition: game.c:26
int CountMem(void *src, int size_type)
Definition: mymath.c:51
char ** gSelectedLevels
Definition: game.c:27
int CompareMemToMemArray(void *mem, void *mem_array, int size_type, int size_array)
Definition: mymath.c:77
int gLevelsPerGame
Definition: game.c:24
int Run ( )

Runs the main game loop.

Returns
An int.
Author
Anthony Rios
Date
3/29/2016

Definition at line 666 of file game.c.

References Draw(), exitRequest, FRAME_DELAY, gCurrentTime, gDelay, Poll(), and Update().

Referenced by main().

667 {
668  while(!exitRequest)
669  {
670  Poll();
671  Update();
672  Draw();
673  gDelay = SDL_GetTicks() - gCurrentTime;
674  SDL_Delay(gDelay > FRAME_DELAY ? 0 : gDelay);
675  }
676  return 0;
677 }
int gDelay
Definition: game.c:23
unsigned int gCurrentTime
Definition: game.c:50
void Poll()
Definition: game.c:427
#define FRAME_DELAY
Definition: globals.h:135
int exitRequest
Definition: game.c:22
void Update()
Definition: game.c:458
void Draw()
Definition: game.c:547
int SelectLevels ( )

Select the levels randomly from the available levels, stores in gSelectedLevels.

Returns
0 on success, -1 on error
Author
Anthony Rios
Date
1/31/2016

Definition at line 297 of file game.c.

References CompareMemToMemArray(), gLevels, gLevelsPerGame, and gSelectedLevels.

Referenced by Setup().

298 {
299  int i, rand_i, *no_repeats, type_i;
300  int levels = 0;
301 
302  //Count
303  while(gLevels[levels])
304  {
305  levels++;
306  }
307  type_i = sizeof(int);
308 
309  //Alloc
310  gSelectedLevels = (char**) malloc(sizeof(char*)*levels+1);
311  no_repeats = (int*) malloc(sizeof(int)*(levels+1));
312  if(!gSelectedLevels) return -1;
313  if(!no_repeats) return -1;
314 
315  //Select
316  memset(no_repeats, 0, sizeof(int));
317  for (i = 0; i < gLevelsPerGame; i++)
318  {
319  rand_i = rand()%levels;
320  if(i >= levels)
321  {
322  break;
323  }
324  while(!CompareMemToMemArray(&rand_i, no_repeats, type_i, levels ))
325  {
326  rand_i = rand()%levels;
327  }
328  no_repeats[i] = rand_i;
329  gSelectedLevels[i] = gLevels[rand_i];
330  }
331  gSelectedLevels[i] = NULL;
332 
333  free(no_repeats);
334  return 0;
335 }
char ** gLevels
Definition: game.c:26
char ** gSelectedLevels
Definition: game.c:27
int CompareMemToMemArray(void *mem, void *mem_array, int size_type, int size_array)
Definition: mymath.c:77
int gLevelsPerGame
Definition: game.c:24
int Setup ( )

Loads files and images for game.

Returns
state returns 0 on success, -1 on error
Author
Anthony Rios
Date
1/31/2016

Definition at line 592 of file game.c.

References gController, InitAISystem(), InitEntitySystem(), InitGraphics(), InitMenuSystem(), LoadEntityData(), LoadGameData(), LoadLevelData(), LoadMenuData(), LoadPowerUpData(), and SelectLevels().

Referenced by main().

593 {
594  //sprite_t *test_sprite;
595  //vec2_t test_vec = {0,0};
596  srand(SDL_GetTicks());
597  //atexit(Shutdown);
598 
599  if(LoadGameData())
600  {
601  perror("Loading game data went wrong");
602  return -1;
603  }
604  if(InitGraphics())
605  {
606  perror("Initializing entity system went wrong");
607  return -1;
608  }
609  if(InitEntitySystem())
610  {
611  perror("Initializing entity system went wrong");
612  return -1;
613  }
614  if(InitMenuSystem())
615  {
616  perror("Initialize Menu system went wrong");
617  return -1;
618  }
619  if(InitAISystem())
620  {
621  perror("Initialize AI system went wrong");
622  return -1;
623  }
624  if(LoadEntityData())
625  {
626  perror("Loading entity data went wrong");
627  return -1;
628  }
629  if(LoadMenuData())
630  {
631  perror("Shit Happens");
632  return -1;
633  }
634  if(LoadLevelData())
635  {
636  perror("Loading level data went wrong");
637  return -1;
638  }
639  if(LoadPowerUpData())
640  {
641  perror("Loading entity data went wrong");
642  return -1;
643  }
644  if(SelectLevels())
645  {
646  perror("Selecting levels went wrong");
647  return -1;
648  }
649  gController = SDL_GameControllerOpen(0);
650  //PrintObject(gLevelObject, gLevelData);
651  //test_sprite = LoadSprite("Sprite/UI/NESController.png",0);
652  //test_sprite->mCurrentFrame = LoadAnimation(test_sprite->mSize.x, test_sprite->mSize.y, test_sprite->mSize.x, test_sprite->mSize.y);
653  //FreeSprite(test_sprite);
654 
655  return 0;
656 }
int LoadMenuData()
Definition: game.c:250
int LoadGameData()
Definition: game.c:60
SDL_GameController * gController
Definition: game.c:48
int InitGraphics()
Definition: graphics.c:19
int InitEntitySystem()
Definition: entity.c:217
int LoadEntityData()
Definition: game.c:96
int LoadLevelData()
Definition: game.c:148
int SelectLevels()
Definition: game.c:297
int InitAISystem()
Definition: ai_interpret.c:646
int LoadPowerUpData()
Definition: game.c:196
void Shutdown ( )

Definition at line 679 of file game.c.

Referenced by main().

680 {
681 
682  return;
683 }
void Update ( )

Definition at line 458 of file game.c.

References CHOOSE, DrawChoose(), DrawGuess(), DrawPlaying(), DrawSplash(), DrawStart(), exitRequest, FindValue(), gButtonQ, gCurrentTime, gGameData, gGameObject, gGameState, gMenus, gSplash, GUESS, LoadSprite(), PLAYING, SPLASH, SPLASH_SCREEN, START, and UpdatePlaying().

Referenced by FindMenuFromGameState(), and Run().

459 {
460  char *splash;
461  gCurrentTime = SDL_GetTicks();
462  switch(gGameState)
463  {
464  case(SPLASH):
465  {
466  if(!gSplash)
467  {
469  if(!splash)
470  {
471  printf("SplashScreen key not found in gameData \n");
472  } else {
473  gSplash = LoadSprite(splash, 0);
474  if(!gSplash)
475  {
476  printf("Splash screen could not be loaded \n");
477  }
478  }
479  }
480  //If you pressed a button
481  if(gButtonQ != -1)
482  {
483  gGameState = START;
484  }
485  break;
486  }
487  case(START):
488  {
489  if(!gMenus)
490  {
491  printf("No menus loaded");
492  exitRequest = 1;
493  return;
494  }
495  if(gButtonQ != -1)
496  {
497  gMenus[0].Update(&gMenus[0], gButtonQ);
498  }
499 
500  break;
501  }
502  case(GUESS):
503  {
504  if(!gMenus)
505  {
506  printf("No menus loaded");
507  exitRequest = 1;
508  return;
509  }
510  if(gButtonQ != -1)
511  {
512  gMenus[1].Update(&gMenus[1], gButtonQ);
513  }
514  break;
515  }
516  case(CHOOSE):
517  {
518  if(!gMenus)
519  {
520  printf("No menus loaded");
521  exitRequest = 1;
522  return;
523  }
524  if(gButtonQ != -1)
525  {
526  gMenus[2].Update(&gMenus[2], gButtonQ);
527  }
528  break;
529  }
530  case(PLAYING):
531  {
532  UpdatePlaying();
533  break;
534  }
535  default:
536  break;
537  }
538 
539 }
Definition: globals.h:87
#define SPLASH_SCREEN
Definition: game.h:9
unsigned int gCurrentTime
Definition: game.c:50
void UpdatePlaying()
Definition: game.c:776
SDL_GameControllerButton gButtonQ
Definition: game.c:49
sprite_t * gSplash
Definition: game.c:45
Definition: globals.h:88
object_t * gGameObject
Definition: game.c:33
GameState gGameState
Definition: game.c:44
sprite_t * LoadSprite(const char *name, int flags)
Definition: graphics.c:107
int exitRequest
Definition: game.c:22
char * FindValue(struct object_s *obj, char *key, char *g_str)
Definition: mystrings.c:53
char * gGameData
Definition: game.c:36
Definition: globals.h:86
Definition: globals.h:85
void UpdatePlaying ( )

Updates the playing game state.

Author
Anthony Rios
Date
3/29/2016

Definition at line 776 of file game.c.

References RunEntities(), and RunPhysics().

Referenced by Poll(), and Update().

777 {
778  RunEntities();
779  RunPhysics();
780 }
void RunPhysics()
Definition: dumb_physics.c:9
void RunEntities()
Definition: entity.c:281

Variable Documentation

int exitRequest = 0

The integer to be changed to exit

Definition at line 22 of file game.c.

Referenced by DrawChoose(), DrawGuess(), DrawStart(), InitPlayer(), Poll(), Run(), and Update().

SDL_GameControllerButton gButtonQ

The button qeueu updated with the current button pressed

Definition at line 49 of file game.c.

Referenced by Poll(), ThinkPlayer(), and Update().

SDL_GameController* gController = NULL

The controller

Definition at line 48 of file game.c.

Referenced by Poll(), Setup(), and ThinkPlayer().

char* gCurrentPowerUpName = NULL

The current power up name

Definition at line 43 of file game.c.

Referenced by UpdatePowerSelectMenu().

unsigned int gCurrentTime = 0

The current time , updated from last update call

Definition at line 50 of file game.c.

Referenced by InitPlayer(), JumpAI(), MoveAI(), NothingAI(), Run(), RunEntities(), ThinkGeneric(), ThinkPlayer(), Update(), and WalkAI().

int gDelay = 0

Definition at line 23 of file game.c.

Referenced by Run().

char* gEntitiesFile = NULL

The name of the entities file

Definition at line 39 of file game.c.

Referenced by LoadEntityData(), and LoadGameData().

char* gEntityData

The current parsed string of the entity file

Definition at line 37 of file game.c.

Referenced by LoadEntityData().

entity_t* gEntityDictionary

Entities loaded from files AKA cached entities

Definition at line 41 of file game.c.

Referenced by FindCachedEntity(), FindNextFreeCachePos(), and InitEntitySystem().

object_t* gEntityObject

The entity object

Definition at line 34 of file game.c.

Referenced by LoadEntityData().

jsmntok_t* gEntityTokens

The entity jsmn tokens

Definition at line 31 of file game.c.

SDL_Event gEventQ

The event qeueu update with all SDL_Events

Definition at line 47 of file game.c.

Referenced by Poll().

char* gGameData

Game Data File - holding the contents of file via string

Definition at line 36 of file game.c.

Referenced by LoadEntityData(), LoadGameData(), LoadLevelData(), LoadMenuData(), LoadPowerUpData(), Update(), and UpdatePowerUpMenu().

object_t* gGameObject

The game object

Definition at line 33 of file game.c.

Referenced by LoadEntityData(), LoadGameData(), LoadLevelData(), LoadMenuData(), LoadPowerUpData(), Update(), and UpdatePowerUpMenu().

GameState gGameState = SPLASH

State of the game

Definition at line 44 of file game.c.

Referenced by Draw(), ThinkPlayer(), TouchGoal(), Update(), UpdatePowerSelectMenu(), UpdatePowerUpMenu(), and UpdateVerticalMenu().

jsmntok_t* gGameTokens

Tokens for GameData

Definition at line 30 of file game.c.

char* gLevelData

Information describing the current level

Definition at line 38 of file game.c.

Referenced by LoadSelectedLevel().

object_t* gLevelObject

The current level object

Definition at line 35 of file game.c.

Referenced by LoadSelectedLevel().

char** gLevels = NULL

The level names

Definition at line 26 of file game.c.

Referenced by LoadLevelData(), RandomizeSelectedLevels(), and SelectLevels().

int gLevelsPerGame = LEVELS_DEFAULT

The levels per game

Definition at line 24 of file game.c.

Referenced by LoadLevelData(), RandomizeSelectedLevels(), SelectLevels(), and UpdatePowerUpMenu().

jsmntok_t* gLevelTokens

The level jsmn tokens

Definition at line 32 of file game.c.

jsmn_parser gParser

The global jsmn parser

Definition at line 25 of file game.c.

power_t* gPowerUps

The loaded power ups

Definition at line 42 of file game.c.

Referenced by FindPower(), InitPlayer(), LoadPowerUpData(), and UpdatePowerUpMenu().

char* gPowerUpsFile = NULL

The name of the power ups file

Definition at line 40 of file game.c.

Referenced by LoadGameData(), and LoadPowerUpData().

char** gSelectedLevels = NULL

The selected levels to load

Definition at line 27 of file game.c.

Referenced by LoadSelectedLevel(), RandomizeSelectedLevels(), and SelectLevels().

char** gSelectedPowerUps = NULL

The power ups the player selects

Definition at line 28 of file game.c.

Referenced by LoadMenu(), TouchGoal(), UpdatePowerSelectMenu(), and UpdatePowerUpMenu().

sprite_t* gSplash = NULL

The splash screen sprite

Definition at line 45 of file game.c.

Referenced by DrawSplash(), and Update().

char** gUsedPowerUps = NULL

The used power ups for this game run

Definition at line 29 of file game.c.

Referenced by TouchGoal(), UpdatePowerSelectMenu(), and UpdatePowerUpMenu().

vec2_t gZeroPos = {0,0}

Definition at line 46 of file game.c.

Referenced by InitPlayer().