-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathengine.c
232 lines (189 loc) · 5.76 KB
/
engine.c
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
#include <time.h>
#include "engine.h"
#include "list.h"
#include "sound.h"
#include "texture.h"
#include "tileObject.h"
#include "level.h"
#include "physBody.h"
#include "creature.h"
//#include "corpse.h"
//#include "lump.h"
//#include "surprise.h"
#include "player.h"
/* âðåìÿ â íà÷àëå è â êîíöå êàäðà */
unsigned long int startTick, endTick;
/* ñ÷åò÷èê êàäðîâ è FPS */
unsigned short int fpsCounter;
float delayTime = 0.0f;
extern float deltaTime;
extern uint16 fps;
extern cpVect cameraPos;
SDL_Window* sdlWindow;
SDL_Renderer* sdlRenderer;
bool EngineStart()
{
startTick = endTick = SDL_GetPerformanceCounter();
deltaTime = delayTime = 0.0f;
fps = fpsCounter = 0;
//if (SDL_Init (SDL_INIT_VIDEO | SDL_INIT_EVENTS | SDL_INIT_TIMER) != 0)
if (SDL_Init (SDL_INIT_VIDEO) != 0)
{
printf ("SDL Error: %s\n", SDL_GetError());
return false;
}
/* create lists */
/*physObjects = ListCreate();
lumps = ListCreate();
surprises = ListCreate();
corpses = ListCreate();*/
backTiles = ListCreate();
frontTiles = ListCreate();
creatures = ListCreate();
return true;
}
void EngineStop()
{
if (sdlWindow)
SDL_DestroyWindow (sdlWindow);
if (sdlRenderer)
SDL_DestroyRenderer (sdlRenderer);
SDL_Quit();
/* free lists */
/*ListDestroy (&physObjects);
ListDestroy (&lumps);
ListDestroy (&surprises);
ListDestroy (&corpses);*/
ListDestroy (&backTiles);
ListDestroy (&frontTiles);
ListDestroy (&creatures);
printf ("Engine stopped.\n");
}
bool EngineCreateWindow (const char* title, int x, int y, int w, int h, unsigned long flags)
{
sdlWindow = SDL_CreateWindow (title, x, y, w, h, flags);
if (!sdlWindow)
printf ("SDL Error: %s\n", SDL_GetError());
return (sdlWindow != NULL);
}
inline void EngineSetWindowTitle (const char* title)
{
SDL_SetWindowTitle (sdlWindow, title);
}
bool EngineCreateRenderer (int index, unsigned long flags)
{
if (sdlWindow == NULL)
return false;
sdlRenderer = SDL_CreateRenderer (sdlWindow, index, flags);
if (!sdlRenderer)
printf ("SDL Error: %s\n", SDL_GetError());
return (sdlRenderer != NULL);
}
bool EngineInitAudio()
{
audioSystemLoaded = false;
if (SDL_Init (SDL_INIT_AUDIO) != 0)
{
printf ("SDL Error: %s\n", SDL_GetError());
return false;
}
audioSystemLoaded = (Mix_OpenAudio (22050, MIX_DEFAULT_FORMAT, 2, 1024) >= 0);
return audioSystemLoaded;
}
inline SDL_Texture* EngineLoadTexture (const char* fileName)
{
return IMG_LoadTexture(sdlRenderer, fileName);
}
/*SDL_Texture* EngineLoadTextureFromStrip (SDL_Texture* srcTexture, const SDL_Rect* srcRect)
{
if (srcTexture == NULL || srcRect == NULL)
return NULL;
// prepare new SDL_Texture and rect
SDL_Texture* dstTexture = SDL_CreateTexture (sdlRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, srcRect->w, srcRect->h);
SDL_Rect dstRect;
dstRect.x = dstRect.y = 0;
dstRect.w = srcRect->w;
dstRect.h = srcRect->h;
// render to new texture
SDL_SetRenderTarget (sdlRenderer, dstTexture);
//SDL_SetRenderDrawColor (sdlRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_SetRenderDrawColor (sdlRenderer, 0, 0, 0, 0);
SDL_RenderClear (sdlRenderer);
SDL_RenderCopy (sdlRenderer, srcTexture, srcRect, &dstRect);
SDL_RenderPresent (sdlRenderer);
SDL_RenderClear (sdlRenderer);
SDL_SetRenderTarget (sdlRenderer, NULL);
return dstTexture;
}*/
inline void EngineRenderClear()
{
SDL_SetRenderDrawColor (sdlRenderer, 0, 0, 0, 0xFF);
SDL_RenderClear(sdlRenderer);
}
void EngineRenderLine (int x1, int y1, int x2, int y2, ubyte r, ubyte g, ubyte b, ubyte a)
{
SDL_SetRenderDrawColor (sdlRenderer, r, g, b, a);
SDL_RenderDrawLine (sdlRenderer, x1, y1, x2, y2);
}
void EngineRenderImage (SDL_Texture* texture, const SDL_Rect* rect, bool flipX)
{
if (texture == NULL || rect == NULL)
return;
if (!flipX)
{
SDL_RenderCopy (sdlRenderer, texture, NULL, rect);
}
else
{
SDL_Point point;
point.x = point.y = 0;
SDL_RenderCopyEx (sdlRenderer, texture, NULL, rect, 0.0, &point, SDL_FLIP_HORIZONTAL);
}
}
void EngineRenderImageEx (SDL_Texture* texture, const SDL_Rect* rect,
const double angle,
//const SVector2f* center,
const cpVect* center,
const SDL_RendererFlip flip)
{
if (texture == NULL || rect == NULL || center == NULL)
return;
SDL_Point point;
point.x = (int)(center->x);
point.y = (int)(center->y);
SDL_RenderCopyEx (sdlRenderer, texture, NULL, rect, angle, &point, flip);
}
void EngineRenderTile (SDL_Texture* tiles, const SDL_Rect* srcRect, const SDL_Rect* dstRect)
{
if (tiles == NULL || srcRect == NULL || dstRect == NULL)
return;
SDL_RenderCopy (sdlRenderer, tiles, srcRect, dstRect);
}
inline void EngineRenderPresent()
{
SDL_RenderPresent(sdlRenderer);
}
void EngineUpdateTime()
{
startTick = SDL_GetPerformanceCounter();
deltaTime = (float)(startTick - endTick) / SDL_GetPerformanceFrequency();
endTick = startTick;
fpsCounter++;
delayTime += deltaTime;
if (delayTime >= 1.0f)
{
delayTime -= 1.0f;
fps = fpsCounter;
fpsCounter = 0;
}
}
void EngineClearAllInstances()
{
TileObjectClearAll();
CreatureClearAll();
/*CorpseClearAll();
LumpClearAll();
SurpriseClearAll();
PhysObjectClearAll();*/
player = NULL;
}