-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsGraphics.h
More file actions
270 lines (237 loc) · 8.7 KB
/
csGraphics.h
File metadata and controls
270 lines (237 loc) · 8.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#ifndef CSGRAPHICS_H_INCLUDED
#define CSGRAPHICS_H_INCLUDED
/* ++ CoSprite 2D Engine ++
-- initCoSprite() error codes: --
error code 0: No error
error code 1: SDL systems failed to initialize
error code 2: Window could not be created
error code 3: Renderer failed to initialize
*/
#ifndef COSPRITE_VERSION
#define COSPRITE_VERSION_MAJOR 0
#define COSPRITE_VERSION_MINOR 14
#define COSPRITE_VERSION_PATCH 1
#define COSPRITE_VERSION "0.14.1"
#endif //COSPRITE_VERSION
#define SDL_MAIN_HANDLED 1
//#includes:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <ctype.h>
#include "SDL2/SDL.h" //This is included because it's an SDL2 program... duh
#include "SDL2/SDL_image.h" //This is included so we can use PNGs.
#include "SDL2/SDL_ttf.h" //This is included for text stuff
//#defines:
#ifndef bool
#define bool char
#define false 0
#define true 1
#define boolToString(bool) (bool ? "true" : "false")
#endif // bool
#ifndef NULL
#define NULL ((void*) 0)
#endif //NULL
#ifndef PI
#define PI (3.14159265359879)
#define radToDeg(x) (180.0 * (x) / PI)
#define degToRad(x) ((x) * PI / 180.0)
#endif // PI
#ifndef MAX_PATH
#define MAX_PATH (260)
#endif //MAX_PATH
//struct definitions:
typedef struct _cFont
{
TTF_Font* font;
int fontSize;
char* filepath;
} cFont;
typedef struct _coSprite
{
SDL_Window* window;
SDL_Renderer* mainRenderer;
cFont mainFont;
int windowW;
int windowH;
SDL_Color colorKey;
bool canDrawText;
} coSprite;
typedef struct _cDoubleRect
{
double x;
double y;
double w;
double h;
} cDoubleRect;
typedef struct _cDoublePt
{
double x;
double y;
} cDoublePt;
typedef struct _cDoubleVector
{
double magnitude;
double degrees;
} cDoubleVector;
typedef struct _cSprite
{
SDL_Texture* texture;
char textureFilepath[MAX_PATH];
int id;
cDoubleRect drawRect;
cDoubleRect srcClipRect;
cDoublePt center;
double scale;
SDL_RendererFlip flip;
double degrees;
int renderLayer; /**< 0 - not drawn. 1-`renderLayers` - drawn. Lower number = drawn later */
bool fixed; /**< if true, won't be affected by camera movement */
bool global; /**< if true, won't be destroyed by destroyCScene() */
void* subclass; /**< fill with any extraneous data or pointer to another struct */
} cSprite;
/*typedef struct _cCircle
{
cDoublePt pt;
double r;
cDoublePt center;
double scale;
SDL_RendererFlip flip;
double degrees;
int renderLayer; / **< 0 - not drawn. 1-5 - drawn. Lower number = drawn later * /
bool fixed; / **< if true, won't be affected by camera movement * /
} cCircle;*/
typedef struct _c2DModel
{ //essentially a 2D version of a wireframe model: A collection of sprites with relative coordinates
cSprite* sprites;
int numSprites;
cDoubleRect rect;
cDoublePt center;
double scale;
SDL_RendererFlip flip;
double degrees;
int renderLayer;
bool fixed; /**< if true, won't be affected by camera movement */
void* subclass;
} c2DModel;
typedef struct _cText
{
char* str;
SDL_Texture* texture;
cDoubleRect rect;
double maxW;
int renderLayer; /**< 0 - not drawn. 1-`renderLayers` - drawn. Lower number = drawn later */
SDL_Color textColor;
SDL_Color bgColor;
cFont* font;
double scale;
SDL_RendererFlip flip;
double degrees;
bool fixed; /**< if true, won't be affected by camera movement */
} cText;
typedef struct _cCamera
{
cDoubleRect rect;
double zoom;
double degrees;
int renderLayers; /**< default 5 */
} cCamera;
typedef struct _cResource
{
void* subclass;
void (*drawingRoutine)(void*, cCamera);
void (*cleanupRoutine)(void*);
int renderLayer; /**< 0 - not drawn. 1-`renderLayers` - drawn. Lower number = drawn later */
} cResource;
typedef struct _cScene
{
SDL_Color bgColor;
cCamera* camera;
cSprite** sprites;
int spriteCount;
c2DModel** models;
int modelCount;
cResource** resources;
int resCount;
cText** strings;
int stringCount;
} cScene;
typedef struct _cLogger
{
char* filepath;
char* dateTimeFormat; /**< strftime() compatible time format */
bool printToStdout; /**< If true, will also printf() your log events */
} cLogger;
//function prototypes:
//initialization
int initCoSprite();
void closeCoSprite();
bool loadIMG(char* imgPath, SDL_Texture** dest);
bool loadTTFont(char* filePath, TTF_Font** dest, int sizeInPts);
int* loadTextTexture(char* text, SDL_Texture** dest, int maxW, SDL_Color color, TTF_Font* font, bool isBlended);
//cSprite
void initCSprite(cSprite* sprite, SDL_Texture* texture, char* textureFilepath, int id, cDoubleRect drawRect, cDoubleRect srcClipRect, cDoublePt* center, double scale, SDL_RendererFlip flip, double degrees, bool fixed, bool global, void* subclass, int drawPriority);
void destroyCSprite(cSprite* sprite);
void drawCSprite(cSprite sprite, cCamera camera, bool update, bool fixedOverride);
//c2DModel
void initC2DModel(c2DModel* model, cSprite* sprites, int numSprites, cDoublePt position, cDoublePt* center, double scale, SDL_RendererFlip flip, double degrees, bool fixed, void* subclass, int drawPriority);
void destroyC2DModel(c2DModel* model);
void importC2DModel(c2DModel* model, char* filepath);
void exportC2DModel(c2DModel* model, char* filepath);
void sortCSpritesInModel(c2DModel* model);
void drawC2DModel(c2DModel model, cCamera camera, bool update);
//cText
void initCText(cText* text, char* str, cDoubleRect rect, double maxW, SDL_Color textColor, SDL_Color bgColor, cFont* font, double scale, SDL_RendererFlip flip, double degrees, bool fixed, int drawPriority);
void updateCText(cText* text, char* str);
void destroyCText(cText* text);
void drawCText(cText text, cCamera camera, bool update);
//cResource
void initCResource(cResource* res, void* subclass, void (*drawingRoutine)(void*, cCamera), void (*cleanupRoutine)(void*), int renderLayer);
void drawCResource(cResource* res, cCamera camera);
void destroyCResource(cResource* res);
//cCamera
void initCCamera(cCamera* camera, cDoubleRect rect, double zoom, double degrees, int renderLayers);
cDoublePt cWindowCoordToCameraCoord(cDoublePt pt, cCamera camera);
cDoublePt cCameraCoordToWindowCoord(cDoublePt pt, cCamera camera);
void destroyCCamera(cCamera* camera);
//cScene
void initCScene(cScene* scenePtr, SDL_Color bgColor, cCamera* camera, cSprite* sprites[], int spriteCount, c2DModel* models[], int modelCount, cResource* resources[], int resCount, cText* strings[], int stringCount);
int addSpriteToCScene(cScene* scenePtr, cSprite* sprite);
int removeSpriteFromCScene(cScene* scenePtr, cSprite* sprite, int index, bool free);
int add2DModelToCScene(cScene* scenePtr, c2DModel* model);
int remove2DModelFromCScene(cScene* scenePtr, c2DModel* model, int index, bool free);
int addTextToCScene(cScene* scenePtr, cText* text);
int removeTextFromCScene(cScene* scenePtr, cText* text, int index, bool free);
int addResourceToCScene(cScene* scenePtr, cResource* resource);
int removeResourceFromCScene(cScene* scenePtr, cResource* resource, int index, bool free);
void destroyCScene(cScene* scenePtr);
void drawCScene(cScene* scenePtr, bool clearScreen, bool redraw, int* frameCount, int* fps, int fpsCap);
void cSceneViewer(cScene* scene);
//cFont
bool initCFont(cFont* font, char* fontFilepath, int fontSize);
void destroyCFont(cFont* font);
//misc
void cSceneViewer(cScene* scene);
void drawText(char* input, int x, int y, int maxW, int maxH, SDL_Color color, bool render);
bool quickCDoubleRectCollision(cDoubleRect rect1, cDoubleRect rect2);
cDoubleVector checkCDoubleRectCollision(cDoubleRect rect1, cDoubleRect rect2);
cDoubleVector checkCSpriteCollision(cSprite sprite1, cSprite sprite2);
cDoubleVector checkC2DModelCollision(c2DModel model1, c2DModel model2, bool fast);
cDoublePt rotatePoint(cDoublePt pt, cDoublePt center, double degrees);
cDoubleVector addCDoubleVectors(cDoubleVector vector1, cDoubleVector vector2);
//file I/O
int createFile(char* filePath);
int checkFile(char* filePath);
int appendLine(char* filePath, char* stuff, bool addNewline);
int replaceLine(char* filePath, int lineNum, char* stuff, int maxLength, bool addNewline);
char* readLine(char* filePath, int lineNum, int maxLength, char** output);
//logging
void initCLogger(cLogger* logger, char* outFilepath, char* dateTimeFormat, bool printToStdout);
void cLogEvent(cLogger logger, char* entryType, char* brief, char* explanation);
void destroyCLogger(cLogger* logger);
//global variable declarations:
extern coSprite global;
extern Uint32 startTime; /**< not set in-engine; if you want to collect a framerate from drawCScene(), set this right before you start your loop */
#endif // CSGRAPHICS_H_INCLUDED