Dumb-ways-to-memorize
2D game
graphics.h
Go to the documentation of this file.
1 #ifndef __GRAPHICS_H
2 #define __GRAPHICS_H
3 
4 #include <SDL.h>
5 #include "globals.h"
6 
7 #define GAME_NAME "Dumb Ways to Memorize"
8 #define SCREEN_STRING "Screen Dimensions"
9 #define SCREEN_RES_W 1280
10 #define SCREEN_RES_H 720
11 #define MAX_SPRITES 500
12 #define MAX_ANIMATIONS 20
13 
14 /** Defines Frame , which is an alias for Position of the start of the sprite */
15 typedef struct
16 {
18 }Frame;
19 
20 typedef struct sprite_s sprite_t;
21 
22 /**
23  * The structure of our sprites.
24  *
25  * @author Anthony Rios
26  * @date 3/30/2016
27  */
28 
29 struct sprite_s
30 {
31  Frame mAnimations[MAX_ANIMATIONS]; /**< The animation positions of the given sprite*/
32  SDL_Texture *mTexture; /**< The texture of the sprite */
33  vec2_t mSize; /**< The size of the sprite width + height */
34  int mFrames; /**< The number of frames in this sprite */
35  int refCount; /**< Number of references, in the engine */
36  char *name; /**< The name of the sprite's location */
37 
38 };
39 
40 /**
41  * Init graphics system.
42  * Calls SDL_Init and allocates memory for the sprites.
43  *
44  * @return 0 if good, -1 if failure.
45  *
46  * @author Anthony Rios
47  * @date 3/16/2016
48  */
49 int InitGraphics();
50 
51 /**
52  * Shutdown graphics system, which frees all sprite data.
53  *
54  * @author Anthony Rios
55  * @date 3/30/2016
56  */
57 void ShutdownGraphics();
58 
59 /**
60  * Loads an animation based on frame_width & frame_height.
61  *
62  * @param frame_width Width of the frame.
63  * @param frame_height Height of the frame.
64  * @param width The width.
65  * @param height The height.
66  *
67  * @return null if it fails, else the animation.
68  *
69  * @author Anthony Rios
70  * @date 3/16/2016
71  */
72 Frame *LoadAnimation(int frame_width, int frame_height, int width, int height);
73 
74 /**
75  * Searches for the first sprite.
76  *
77  * @param name The name of the sprite file.
78  * @param [in,out] position If non-null, the position.
79  *
80  * @return null if it fails, else the found sprite.
81  *
82  * @author Anthony Rios
83  * @date 3/16/2016
84  */
85 sprite_t *FindSprite(const char *name, int *position);
86 
87 /**
88  * Searches for the first free sprite in sprite system memory.
89  *
90  * @param [in,out] position If non-null, the position.
91  *
92  * @return null if it fails, else the found free sprite.
93  *
94  * @author Anthony Rios
95  * @date 3/16/2016
96  */
97 sprite_t *FindFreeSprite(int *position);
98 
99 /**
100  * Loads a sprite.
101  *
102  * @param name The file name of the image to load.
103  * @param flags The flags.
104  *
105  * @return null if it fails, else the sprite.
106  *
107  * @author Anthony Rios
108  * @date 3/16/2016
109  */
110 sprite_t *LoadSprite(const char *name, int flags);
111 
112 /**
113  * Draw the sprite. If mCurrentFrame not set, draws the first frame (0,0).
114  * If no position given, will draw as if was background.
115  *
116  * @param [in,out] sprite If non-null, the sprite.
117  * @param [in,out] frame If non-null, the frame number to draw.
118  * @param [in,out] position If non-null, the position.
119  * @param [in,out] renderer If non-null, the renderer.
120  *
121  * @return 0 if good, -1 if failure
122  *
123  * @author Anthony Rios
124  * @date 3/16/2016
125  */
126 int DrawSprite(sprite_t *sprite, int* frame, vec2_t * position, SDL_Renderer * renderer);
127 
128 /**
129  * Free the sprite memory, if recfcount - 1 > 0 nothing happens.
130  *
131  * @param [in,out] sprite If non-null, the sprite.
132  *
133  * @author Anthony Rios
134  * @date 3/16/2016
135  */
136 void FreeSprite(sprite_t *sprite);
137 
138 //Unused
139 void IncrementFrame(sprite_t *sprite);
140 
141 /**
142  * Sdl set rectangle dimensions.
143  *
144  * @param [in,out] rect If non-null, the rectangle to set.
145  * @param x The x coordinate.
146  * @param y The y coordinate.
147  * @param w The width.
148  * @param h The height.
149  *
150  * @author Anthony Rios
151  * @date 3/29/2016
152  */
153 void SDL_SetRect(SDL_Rect *rect, int x, int y, int w, int h);
154 
155 extern sprite_t *gSprites;
156 extern int gLastSprite;
157 extern SDL_Window *gWindow;
158 extern SDL_Renderer *gRenderer;
159 extern SDL_Surface *gRedSurface;
160 extern SDL_Texture *gRedTexture;
161 extern SDL_Renderer *gRedRenderer;
162 
163 #endif
sprite_t * FindSprite(const char *name, int *position)
Definition: graphics.c:209
int DrawSprite(sprite_t *sprite, int *frame, vec2_t *position, SDL_Renderer *renderer)
Definition: graphics.c:152
SDL_Renderer * gRedRenderer
Definition: graphics.c:13
int InitGraphics()
Definition: graphics.c:19
int refCount
Definition: graphics.h:35
Definition: graphics.h:15
char * name
Definition: graphics.h:36
vec2_t mSize
Definition: graphics.h:33
void SDL_SetRect(SDL_Rect *rect, int x, int y, int w, int h)
Definition: graphics.c:276
#define MAX_ANIMATIONS
Definition: graphics.h:12
vec2_t Position
Definition: graphics.h:17
SDL_Surface * gRedSurface
Definition: graphics.c:11
sprite_t * gSprites
Definition: graphics.c:14
sprite_t * LoadSprite(const char *name, int flags)
Definition: graphics.c:107
SDL_Window * gWindow
Definition: graphics.c:9
int mFrames
Definition: graphics.h:34
void ShutdownGraphics()
Definition: graphics.c:92
int gLastSprite
Definition: graphics.c:15
SDL_Renderer * gRenderer
Definition: graphics.c:10
SDL_Texture * gRedTexture
Definition: graphics.c:12
void FreeSprite(sprite_t *sprite)
Definition: graphics.c:246
char * name
Definition: parseobject.h:24
sprite_t * FindFreeSprite(int *position)
Definition: graphics.c:226
void IncrementFrame(sprite_t *sprite)
Definition: graphics.c:262
SDL_Texture * mTexture
Definition: graphics.h:32
Frame * LoadAnimation(int frame_width, int frame_height, int width, int height)
Definition: graphics.c:190
Definition: globals.h:19