-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgameEngine.c
More file actions
242 lines (204 loc) · 9 KB
/
Copy pathgameEngine.c
File metadata and controls
242 lines (204 loc) · 9 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
#include "headers/handleEvents.h"
#include "headers/mapEditor.h"
#include "headers/physics2d.h"
#include "utils.c"
void updateSpriteFrame(Agent *agent) {
const Uint8* keystate = SDL_GetKeyboardState(NULL);
// continuous-response keys
if (keystate[SDL_SCANCODE_A]) {
// agent->currentFramey = 1;
agent->currentFramex++;
agent->currentFramex %= 3;
agent->currentFramex++;
}
else if (keystate[SDL_SCANCODE_D]) {
// agent->currentFramey = 1;
agent->currentFramex++;
agent->currentFramex %= 3;
agent->currentFramex++;
}
else {
// agent->currentFramey = 0;
agent->currentFramex++;
agent->currentFramex = 0;
}
// if (keystate[SDL_SCANCODE_S]) {
// agent->currentFramex = 6;
// }
}
int main(int argc, char** argv) {
initEnvironment();
SDL_Window *window = SDL_CreateWindow("Game", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 640, SDL_WINDOW_OPENGL);
// create a renderer (accelerated and in-sync with the display referesh rate)
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
// load audio files
Mix_Chunk *jumpEffect = Mix_LoadWAV("audioFiles/Jump6.wav");
Mix_Chunk *laserEffect = Mix_LoadWAV("audioFiles/Laser_Shoot7.wav");
SDL_Rect player_dest;
Agent player;
int refGrid[20][25] = {
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1},
{1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1},
{1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1},
{1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1},
{1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
};
Platform *brickCoords = malloc(sizeof(Platform));
Coll *allRects = malloc(sizeof(Coll));
int index = 0;
int numRects = 0;
// int nTotal = sizeof(refGrid)/sizeof(int);
int nColumns = sizeof(refGrid[0])/sizeof(int);
int nRows = sizeof(refGrid)/sizeof(refGrid[0]);
// printf("#Rows: %d, #Columns: %d\n", nRows, nColumns);
for (int i =0; i < nRows; i++) {
for (int j = 0; j < nColumns; j++) {
if (refGrid[i][j] == 1) {
Platform *temp = (Platform*)realloc(brickCoords, ((index+2)*sizeof(Platform)));
if (temp != NULL) {
brickCoords =temp;
}
else {
printf("realloc failed\n");
}
// printf("j: %d, i: %d\n", j, i);
SDL_Rect dest = createRect(j*32, i*32, 32, 32);
brickCoords[index] = initPlatform("imageFiles/brick.png", dest, 1, renderer);
allRects = (Coll*)realloc(allRects, ((numRects+2)*sizeof(Coll)));
allRects[numRects] = brickCoords[index].coll;
numRects++;
index++;
}
else if (refGrid[i][j] == 2) {
player_dest = createRect(j*32, i*32, 32, 64);
player = initAgent("imageFiles/mario.png", player_dest, 0, 0, 0, renderer);
allRects = (Coll*)realloc(allRects, ((numRects+2)*sizeof(Coll)));
allRects[numRects] = player.coll;
numRects++;
}
}
}
// printf("Index: %d\n", index);
// printf("numRects: %d\n", numRects);
int done = 0;
int frameTime = 0;
// Coll testBrick;
// testBrick.leftEdge = 352;
// testBrick.top = 224;
// testBrick.rightEdge = 384;
// testBrick.bottom = 256;
int sum;
while(done == 0) {
done = eventHandler(&player);
float curr[4] = {0, 0, 0, 0};
float prev[4] = {0, 0, 0, 0};
// move(&player, collideRect(player.coll, testBrick));
player.dy += 0.5;
player.y += player.dy;
player.coll.bottom += player.dy;
player.coll.top += player.dy;
allRects[0] = player.coll;
sum = 0;
// gravity(&player);
// player.dy += 0.5;
// player.y += player.dy;
// allRects[0].bottom += player.dy;
// allRects[0].top += player.dy;
for (int i = 0; i < 1; i++) {
int r = numRects -1;
for (int j = i+1; j < numRects; j++) {
// comparison of two rectangles (for collision) go here
// printf("before - right: %0.1f left: %0.1f top: %0.1f bottom: %0.1f\n", allRects[i].rightEdge, allRects[i].leftEdge, allRects[i].top, allRects[i].bottom);
// printf("j: %d j-1: %d\n", j, j-1);
// memcpy(curr, collideRect(allRects[i], allRects[j]), sizeof(curr));
int currCount = 0;
int prevCount = 0;
// if (j-1 >= 1) {
memcpy(curr, collideRect(allRects[i], allRects[j]), sizeof(curr));
memcpy(prev, collideRect(allRects[i], allRects[r]), sizeof(prev));
// if ((curr[0] != 0 && curr[3] != 0) || (curr[2] != 0 && curr[3] != 0)) {
// currCount += 1;
// }
for (int k = 0; k < 4; k++) {
if (curr[k] != 0) {
currCount += 1;
}
if (prev[k] != 0) {
prevCount += 1;
}
}
if (currCount > 1) {
memcpy(curr, prev, sizeof(prev));
}
if (curr[0] != 0) {
allRects[i].leftEdge += -curr[0];
allRects[i].rightEdge += -curr[0];
}
if (curr[1] != 0) {
allRects[i].leftEdge += curr[1];
allRects[i].rightEdge += curr[1];
}
if (curr[2] != 0) {
allRects[i].top += -curr[2];
allRects[i].bottom += -curr[2];
player.dy = 0;
}
if (curr[3] != 0) {
allRects[i].top += curr[3];
allRects[i].bottom += curr[3];
player.dy = 0;
player.jumpAgain = true;
}
move(&player, curr);
r--;
// printf("after - right: %0.1f left: %0.1f top: %0.1f bottom: %0.1f\n", allRects[i].rightEdge, allRects[i].leftEdge, allRects[i].top, allRects[i].bottom);
sum += 1;
}
}
// printf("sum: %d\n", sum);
frameTime += 1;
if (FPS / frameTime == 8) {
frameTime = 0;
updateSpriteFrame(&player);
}
// clear screen by making it black
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
player_dest = createRect(player.x, player.y, player.sprite_w, player.sprite_h);
setCurrentSprite(&player, &player.currentSprite);
SDL_RenderCopyEx(renderer, player.sheetTexture, &player.currentSprite, &player_dest, 0, NULL, player.facingLeft);
for (int i = 0; i < index; i++) {
SDL_RenderCopy(renderer, brickCoords[i].pTexture, NULL, &brickCoords[i].block_dest);
}
SDL_RenderPresent(renderer);
SDL_Delay(10);
}
// release resources
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
Mix_FreeChunk(jumpEffect);
Mix_FreeChunk(laserEffect);
SDL_DestroyTexture(player.sheetTexture);
for (int i =0; i < sizeof(brickCoords)/sizeof(brickCoords[0]); i++) {
SDL_DestroyTexture(brickCoords[i].pTexture);
}
free(brickCoords);
free(allRects);
destroyEnvironment();
return 0;
}