Dumb-ways-to-memorize
2D game
Functions | Variables
graphics.c File Reference
#include "globals.h"
#include "graphics.h"
#include <SDL.h>
#include <SDL_image.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
+ Include dependency graph for graphics.c:

Go to the source code of this file.

Functions

int InitGraphics ()
 
void ShutdownGraphics ()
 
sprite_t * LoadSprite (const char *name, int flags)
 
int DrawSprite (sprite_t *sprite, int *frame, vec2_t *position, SDL_Renderer *renderer)
 
FrameLoadAnimation (int frame_width, int frame_height, int width, int height)
 
sprite_t * FindSprite (const char *name, int *position)
 
sprite_t * FindFreeSprite (int *position)
 
void FreeSprite (sprite_t *sprite)
 
void IncrementFrame (sprite_t *sprite)
 
void SDL_SetRect (SDL_Rect *rect, int x, int y, int w, int h)
 

Variables

SDL_Window * gWindow = NULL
 
SDL_Renderer * gRenderer = NULL
 
SDL_Surface * gRedSurface = NULL
 
SDL_Texture * gRedTexture = NULL
 
SDL_Renderer * gRedRenderer = NULL
 
sprite_t * gSprites = NULL
 
int gLastSprite = 0
 
int gScreenWidth = 0
 
int gScreenHeight = 0
 

Function Documentation

int DrawSprite ( sprite_t *  sprite,
int *  frame,
vec2_t position,
SDL_Renderer *  renderer 
)

Draw the sprite. If mCurrentFrame not set, draws the first frame (0,0). If no position given, will draw as if was background.

Parameters
[in,out]spriteIf non-null, the sprite.
[in,out]frameIf non-null, the frame number to draw.
[in,out]positionIf non-null, the position.
[in,out]rendererIf non-null, the renderer.
Returns
0 if good, -1 if failure
Author
Anthony Rios
Date
3/16/2016

Definition at line 152 of file graphics.c.

References SDL_SetRect(), vec2_t::x, and vec2_t::y.

Referenced by DrawGeneric(), DrawLevel(), DrawMenuByNum(), DrawMenuByState(), and DrawSplash().

153 {
154  SDL_Rect src, dst;
155  int zero = 0;
156  if(!sprite)
157  {
158  printf("Null sprite given \n");
159  return -1;
160  }
161  if(!frame)
162  {
163  frame = &zero;
164  }
165  if( *frame >= sprite->mFrames )
166  {
167  *frame = 0;
168  }
169 
170  SDL_SetRect(&src, sprite->mAnimations[*frame].Position.x, sprite->mAnimations[*frame].Position.y, sprite->mSize.x, sprite->mSize.y);
171 
172  *frame = *frame+1;
173 
174  if(!position)
175  {
176  SDL_SetRect(&dst, 0, 0, 0, 0);
177  SDL_GetRendererOutputSize(renderer, &dst.w, &dst.h);
178  } else {
179  SDL_SetRect(&dst, position->x, position->y, sprite->mSize.x, sprite->mSize.y);
180  }
181 
182 
183  if( SDL_RenderCopy(renderer, sprite->mTexture, &src, &dst) )
184  {
185  printf("Could not draw sprite %s : %s", sprite->name, SDL_GetError());
186  }
187  return 0;
188 }
int y
Definition: globals.h:22
void SDL_SetRect(SDL_Rect *rect, int x, int y, int w, int h)
Definition: graphics.c:276
int x
Definition: globals.h:21
sprite_t* FindFreeSprite ( int *  position)

Searches for the first free sprite in sprite system memory.

Parameters
[in,out]positionIf non-null, the position.
Returns
null if it fails, else the found free sprite.
Author
Anthony Rios
Date
3/16/2016

Definition at line 226 of file graphics.c.

References gLastSprite, gSprites, and MAX_SPRITES.

Referenced by LoadSprite().

227 {
228  int i;
229  for(i = gLastSprite; i < MAX_SPRITES; i++)
230  {
231  if(gSprites[i].refCount == 0)
232  {
233  return &gSprites[i];
234  }
235  }
236  for(i = 0; i < gLastSprite; i++)
237  {
238  if(gSprites[i].refCount == 0)
239  {
240  return &gSprites[i];
241  }
242  }
243  return NULL;
244 }
int gLastSprite
Definition: graphics.c:15
#define MAX_SPRITES
Definition: graphics.h:11
sprite_t * gSprites
Definition: graphics.c:14
sprite_t* FindSprite ( const char *  name,
int *  position 
)

Searches for the first sprite.

Parameters
nameThe name of the sprite file.
[in,out]positionIf non-null, the position.
Returns
null if it fails, else the found sprite.
Author
Anthony Rios
Date
3/16/2016

Definition at line 209 of file graphics.c.

References gSprites, and MAX_SPRITES.

Referenced by FreeSprite(), and LoadSprite().

210 {
211  int i;
212  for(i = 0; i < MAX_SPRITES; i++)
213  {
214  if(gSprites[i].refCount == 0)
215  continue;
216  if(!strcmp(gSprites[i].name, name))
217  {
218  if(position)
219  *position = i;
220  return &gSprites[i];
221  }
222  }
223  return NULL;
224 }
#define MAX_SPRITES
Definition: graphics.h:11
sprite_t * gSprites
Definition: graphics.c:14
void FreeSprite ( sprite_t *  sprite)

Free the sprite memory, if recfcount - 1 > 0 nothing happens.

Parameters
[in,out]spriteIf non-null, the sprite.
Author
Anthony Rios
Date
3/16/2016

Definition at line 246 of file graphics.c.

References FindSprite(), and gSprites.

Referenced by ShutdownGraphics().

247 {
248  int position;
249  if(sprite == FindSprite(sprite->name, &position))
250  {
251  sprite->refCount--;
252  if(sprite->refCount > 0)
253  return;
254  }
255  SDL_DestroyTexture(sprite->mTexture);
256  free(sprite->name);
257  memset(&gSprites[position], 0, sizeof(sprite_t) );
258 
259 }
sprite_t * FindSprite(const char *name, int *position)
Definition: graphics.c:209
sprite_t * gSprites
Definition: graphics.c:14
void IncrementFrame ( sprite_t *  sprite)

Definition at line 262 of file graphics.c.

References CountMem().

263 {
264  int frames;
265  if(!sprite)
266  {
267  return;
268  }
269  frames = CountMem(&sprite->mAnimations[0], sizeof(Frame));
270  if(!frames)
271  {
272  return;
273  }
274 }
int CountMem(void *src, int size_type)
Definition: mymath.c:51
Definition: graphics.h:15
int InitGraphics ( )

Init graphics system. Calls SDL_Init and allocates memory for the sprites.

Returns
0 if good, -1 if failure.
Author
Anthony Rios
Date
3/16/2016

Definition at line 19 of file graphics.c.

References GAME_NAME, gLastSprite, gRedRenderer, gRedSurface, gRedTexture, gRenderer, gScreenHeight, gScreenWidth, gSprites, gWindow, MAX_SPRITES, SCREEN_RES_H, SCREEN_RES_W, and ShutdownGraphics().

Referenced by Setup().

20 {
21  Uint32 flags = 0;
22  SDL_DisplayMode current;
23  if(SDL_Init(SDL_INIT_EVERYTHING))
24  {
25  printf("SDL can't initialize : %s", SDL_GetError());
26  exit(-1);
27  }
28  if(SDL_GetDisplayMode(0,0,&current))
29  {
30  printf("No video mode: %s", SDL_GetError());
31  exit(-1);
32  }
33 
34  //Create Window based on either default or set screenWidth / Height
36  {
37  if( (gWindow = SDL_CreateWindow(GAME_NAME, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, gScreenWidth, gScreenHeight, flags )) == NULL )
38  {
39  printf("Can't create window %s", SDL_GetError());
40  exit(-1);
41  }
42  } else {
43  if( (gWindow = SDL_CreateWindow(GAME_NAME, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_RES_W, SCREEN_RES_H, flags )) == NULL )
44  {
45  printf("Can't create window %s", SDL_GetError());
46  exit(-1);
47  }
50  }
51 
52  //Main Rendering Context
53  SDL_SetWindowTitle(gWindow, "Dumb Ways to Memorize");
54 
55  if( (gRenderer = SDL_CreateRenderer(gWindow, -1, flags)) == NULL)
56  {
57  printf("Can't create renderer %s ", SDL_GetError());
58  exit(-1);
59  }
60 
61  //Red Renderer
62  if ( (gRedSurface = SDL_CreateRGBSurface(0,gScreenWidth,gScreenHeight,32,0,0,0,0)) == NULL)
63  {
64  printf("Can't create red surface : %s \n", SDL_GetError());
65  }
66  if( (gRedRenderer = SDL_CreateSoftwareRenderer(gRedSurface)) == NULL)
67  {
68  printf("Can't create red renderer : %s \n", SDL_GetError());
69  }
70  if( SDL_SetRenderDrawColor(gRedRenderer, 0xFF, 0, 0, 0xFF) )
71  {
72  printf("Can't set red renderer : %s \n", SDL_GetError());
73  }
74  SDL_RenderClear(gRedRenderer);
75  if( (gRedTexture = SDL_CreateTextureFromSurface(gRenderer, gRedSurface)) == NULL)
76  {
77  printf("Can't create red texture : %s \n", SDL_GetError());
78  }
79  gSprites = (sprite_t*) malloc(sizeof(sprite_t)*MAX_SPRITES);
80  if(!gSprites)
81  {
82  printf("Could not allocate sprites");
83  return -1;
84  }
85  memset(gSprites, 0, sizeof(sprite_t)*MAX_SPRITES);
86  gLastSprite = 0;
87  atexit(SDL_Quit);
88  atexit(ShutdownGraphics);
89  return 0;
90 }
void ShutdownGraphics()
Definition: graphics.c:92
int gLastSprite
Definition: graphics.c:15
SDL_Renderer * gRenderer
Definition: graphics.c:10
SDL_Window * gWindow
Definition: graphics.c:9
#define GAME_NAME
Definition: graphics.h:7
int gScreenWidth
Definition: graphics.c:16
#define MAX_SPRITES
Definition: graphics.h:11
SDL_Texture * gRedTexture
Definition: graphics.c:12
int gScreenHeight
Definition: graphics.c:17
SDL_Surface * gRedSurface
Definition: graphics.c:11
SDL_Renderer * gRedRenderer
Definition: graphics.c:13
#define SCREEN_RES_W
Definition: graphics.h:9
#define SCREEN_RES_H
Definition: graphics.h:10
sprite_t * gSprites
Definition: graphics.c:14
Frame* LoadAnimation ( int  frame_width,
int  frame_height,
int  width,
int  height 
)

Loads an animation based on frame_width & frame_height.

Parameters
frame_widthWidth of the frame.
frame_heightHeight of the frame.
widthThe width.
heightThe height.
Returns
null if it fails, else the animation.
Author
Anthony Rios
Date
3/16/2016

Definition at line 190 of file graphics.c.

References Frame::Position, vec2_t::x, and vec2_t::y.

Referenced by ParseToEntity().

191 {
192  Frame *retVal;
193  int i,j, rows, cols;
194  cols = frame_width ? width/frame_width : 1;
195  rows = frame_height ? height/frame_height : 1;
196  retVal = (Frame*) malloc(sizeof(Frame)*(rows*cols+1));
197  for(i = 0; i < rows; i++)
198  {
199  for(j = 0; j < cols; j++)
200  {
201  retVal[i*rows + j].Position.x = j*frame_width;
202  retVal[i*rows + j].Position.y = i*frame_height;
203  }
204  }
205  memset(&retVal[rows*cols+1], 0, sizeof(Frame));
206  return retVal;
207 }
int y
Definition: globals.h:22
Definition: graphics.h:15
vec2_t Position
Definition: graphics.h:17
int x
Definition: globals.h:21
sprite_t* LoadSprite ( const char *  name,
int  flags 
)

Loads a sprite.

Parameters
nameThe file name of the image to load.
flagsThe flags.
Returns
null if it fails, else the sprite.
Author
Anthony Rios
Date
3/16/2016

Definition at line 107 of file graphics.c.

References FindFreeSprite(), FindSprite(), gLastSprite, gRenderer, gSprites, and MAX_SPRITES.

Referenced by AddSpritesToEnt(), LoadLevel(), LoadMenu(), and Update().

108 {
109  SDL_Surface *temp;
110  sprite_t *check;
111  int position;
112  if(!gSprites)
113  {
114  return NULL;
115  }
116  if(abs(gLastSprite) > MAX_SPRITES)
117  {
118  gLastSprite = 0;
119  }
120  if(!name)
121  {
122  printf("No name given to load sprite");
123  return NULL;
124  }
125  if( (check = FindSprite(name, NULL)) != NULL)
126  {
127  check->refCount++;
128  return check;
129  }
130  if( (check = FindFreeSprite(&position)) == NULL)
131  {
132  printf("No free sprites");
133  return NULL;
134  }
135  temp = IMG_Load(name);
136  if(!temp)
137  {
138  printf("Could not load image : %s \n", name);
139  return NULL;
140  }
141  check->mSize.x = temp->w;
142  check->mSize.y = temp->h;
143  check->mTexture = SDL_CreateTextureFromSurface(gRenderer, temp);
144  check->name = strdup(name);
145  check->refCount = 1;
146  check->mFrames = 0;
147  SDL_FreeSurface(temp);
148  gLastSprite = position;
149  return check;
150 }
int gLastSprite
Definition: graphics.c:15
SDL_Renderer * gRenderer
Definition: graphics.c:10
#define MAX_SPRITES
Definition: graphics.h:11
sprite_t * FindFreeSprite(int *position)
Definition: graphics.c:226
sprite_t * FindSprite(const char *name, int *position)
Definition: graphics.c:209
sprite_t * gSprites
Definition: graphics.c:14
void SDL_SetRect ( SDL_Rect *  rect,
int  x,
int  y,
int  w,
int  h 
)

Sdl set rectangle dimensions.

Parameters
[in,out]rectIf non-null, the rectangle to set.
xThe x coordinate.
yThe y coordinate.
wThe width.
hThe height.
Author
Anthony Rios
Date
3/29/2016

Definition at line 276 of file graphics.c.

Referenced by DrawSprite(), and ProcessMenuItemsByType().

277 {
278  rect->x = x;
279  rect->y = y;
280  rect->w = w;
281  rect->h = h;
282 }
void ShutdownGraphics ( )

Shutdown graphics system, which frees all sprite data.

Author
Anthony Rios
Date
3/30/2016

Definition at line 92 of file graphics.c.

References FreeSprite(), gSprites, and MAX_SPRITES.

Referenced by InitGraphics().

93 {
94  int i;
95  for(i = 0; i < MAX_SPRITES; i++)
96  {
97  if(!gSprites[i].refCount)
98  {
99  continue;
100  }
101  FreeSprite(&gSprites[i]);
102 
103  }
104  free(gSprites);
105 }
void FreeSprite(sprite_t *sprite)
Definition: graphics.c:246
#define MAX_SPRITES
Definition: graphics.h:11
sprite_t * gSprites
Definition: graphics.c:14

Variable Documentation

int gLastSprite = 0

Definition at line 15 of file graphics.c.

Referenced by FindFreeSprite(), InitGraphics(), and LoadSprite().

SDL_Renderer* gRedRenderer = NULL

Definition at line 13 of file graphics.c.

Referenced by InitGraphics().

SDL_Surface* gRedSurface = NULL

Definition at line 11 of file graphics.c.

Referenced by InitGraphics().

SDL_Texture* gRedTexture = NULL

Definition at line 12 of file graphics.c.

Referenced by DrawMenuByNum(), DrawMenuByState(), and InitGraphics().

SDL_Renderer* gRenderer = NULL
int gScreenHeight = 0

Te set Height of the screen

Definition at line 17 of file graphics.c.

Referenced by ApplyBounds(), InitGraphics(), LoadGameData(), and ProcessMenuItemsByType().

int gScreenWidth = 0

The set Width of the screen

Definition at line 16 of file graphics.c.

Referenced by ApplyBounds(), InitGraphics(), LoadGameData(), and ProcessMenuItemsByType().

sprite_t* gSprites = NULL
SDL_Window* gWindow = NULL

Definition at line 9 of file graphics.c.

Referenced by InitGraphics().