/
functions.c
346 lines (300 loc) · 6.87 KB
/
functions.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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#include "functions.h"
SDL_Texture* createTexture(char nameImage[], SDL_Renderer* renderer)
{
// takes an image address and a renderer as input and converts everything to a texture
SDL_Surface* img = IMG_Load(nameImage);
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, img);
SDL_FreeSurface(img);
return texture;
}
SDL_Rect updateCharacterPosition(enum DIRECTION keyPressed, SDL_Rect destPos) // even if all it does is moving a texture
{
switch(keyPressed)
{
//case UP: destPos.y -= srcPos.h; break;
//case DOWN: destPos.y += srcPos.h; break;
case RIGHT: destPos.x += 4; break;
case LEFT: destPos.x -= 4; break;
default: destPos.x +=0; break;
}
if (destPos.x > WIDTH - 20)
{
destPos.x = 0;
}
else if (destPos.x < 0)
{
destPos.x = WIDTH - 20;
}
return destPos;
}
Player initializePlayer(char nameImageIn[], SDL_Renderer* rendererIn)
{
Player player;
// basically the constructor of the player
player.src.w = SPRITEBLOCK_W;
player.src.h = SPRITEBLOCK_H;
player.src.x = 0;
player.src.y = 0;
player.dst.x = WIDTH/2;
player.dst.y = HEIGHT - (HEIGHT/10);
player.dst.w = 30;//playerIn.src.w;
player.dst.h = 30;//playerIn.src.h;
player.texture = createTexture(nameImageIn, rendererIn);
return player;
}
Bullet initializeBullet(char nameImageIn[], SDL_Renderer* rendererIn,int speed)
{
// initializes the bullet, basically a constructor
Bullet bullet;
bullet.srcPos.x = 0;
bullet.srcPos.y = 0;
bullet.srcPos.w = SPRITEBLOCK_W;
bullet.srcPos.h = SPRITEBLOCK_H;
bullet.dstPos.w = 15;
bullet.dstPos.h = 15;
bullet.active = 0;
bullet.speed = speed;
bullet.texture = createTexture(nameImageIn, rendererIn);
return bullet;
}
SDL_Rect updateBulletPosition(Bullet bullet, int direction)
{
// updates the position of the bullet once shot
if (direction == 1)
bullet.dstPos.y -= bullet.speed;
else
bullet.dstPos.y += bullet.speed;
return bullet.dstPos;
}
int checkCollision(Bullet bullet, Invader invaders[ROWS][COLS])
{
/* this is the function that checks for collision between the bullet and the Invader
* if there is a collision the invader is set inactive
* the function will return 1 if there is a collision and 0 if there isn't one
* the return values will be used in the main to deactivate the bullet
*/
for(int r=0; r < ROWS; ++r)
{
for(int c=0; c < COLS; ++c)
{
if (SDL_HasIntersection(&invaders[r][c].pos, &bullet.dstPos) == SDL_TRUE && invaders[r][c].active == 1)
{
invaders[r][c].active = 0;
return 1;
}
}
}
return 0;
}
int DestroyBullet(Bullet bullet)
{
if (bullet.dstPos.y < 0 || bullet.dstPos.y > HEIGHT )
return 0;
else
return 1;
}
int getLeftMostInvaderColumn(Invader invaders[ROWS][COLS])
{
/*
* returns the biggest index of the invaders matrix
* this is useful for collision with the screen
*/
int min = COLS;
for(int r = 0; r < ROWS; ++r)
{
for(int c = 0; c < COLS; ++c)
{
if (invaders[r][c].active == 1 && c < min)
min = c;
}
}
return min;
}
int getRightMostInvaderColumn(Invader invaders[ROWS][COLS])
{
/*
* returns the smallest index of the invaders matrix
* this is useful for collision with the screen
*/
int max = 0;
for(int r = 0; r < ROWS; ++r)
{
for(int c = 0; c < COLS; ++c)
{
if (invaders[r][c].active == 1 && c > max)
max = c;
}
}
return max;
}
// this function needs to be merged with Delay()
int UpdateSprite()
{
static int block;
static int frame = 0;
int speed = 2;
if (block % (SPRITEBLOCK_H/speed) == 0)
{
frame = (block*speed);
}
block++;
if (block == ((SPRITEBLOCK_H)*SPRITEBLOCKS)/speed)
block = 0;
return frame;
}
int Delay()
{
/*
* this function needs to be deleted and merged with the UPDATESPRITE function,
* they really do the same thing, I just need to make it more general
*/
static int block;
static int frame = 0;
int speed = 20;
if (block % (SPRITEBLOCK_H/speed) == 0)
{
frame = (block*speed);
}
block++;
if (block == ((SPRITEBLOCK_H)*SPRITEBLOCKS)/speed)
block = 0;
return frame;
}
int countActiveInvaders(Invader invaders[ROWS][COLS])
{
int count = 0;
for(int r=0; r<ROWS; ++r)
{
for(int c=0; c<COLS; ++c)
{
if (invaders[r][c].active == 1)
{
++count;
}
}
}
return count;
}
// // // // // // // // no need to keep these two functions anymore
void activeInvaderList(Invader invaders[ROWS][COLS], Invader retList[])
{
int count = 0;
for(int r=0; r<ROWS; ++r)
{
for(int c=0; c<COLS; ++c)
{
if (invaders[r][c].active == 1)
{
retList[count] = invaders[r][c];
count++;
}
}
}
}
void closestInvaderList(Invader invaders[ROWS][COLS], Invader retList[])
{
int count = 0;
int closest[COLS];
for (int i = 0; i < COLS; ++i)
{
closest[i] = 255;
}
for(int r = ROWS-1; r >= 0; --r)
{
for(int c = 0; c < COLS; ++c)
{
int isclosest = 0;
if (invaders[r][c].active == 1 && r == ROWS-1)
{
invaders[r][c].closest = 1;
isclosest = 1;
closest[c] = r;
}
else if (invaders[r+1][c].active == 0 && invaders[r][c].active == 1 && closest[c] == 255)
{
invaders[r][c].closest = 1;
isclosest = 1;
closest[c] = r;
}
if (isclosest != 0)
{
++count;
}
}
}
for (int j = 0; j < COLS; ++j)
{
if(closest[j] != 255)
{
int x = (int)closest[j];
retList[j] = invaders[x][j];
}
}
}
// // // // // // // //
int ShootingInvaders(Invader invaders[ROWS][COLS], Invader retList[])
{
/*
* this functions couts the closest invaders to the player
* this function is useful because I only want the closest invaders to shoot at the player
* the function controls returns the number of the "closest" invaders and set this ones to "closest"
*/
int count = 0;
int closest[COLS];
for (int i = 0; i < COLS; ++i)
{
closest[i] = 255;
}
// here I am doing a (backward) for loop to check the closest one
// I am doing it backwards because it is easier that the closest one has the biggest index
for(int r = ROWS-1; r >= 0; --r)
{
for(int c = 0; c < COLS; ++c)
{
int isclosest = 0;
if (invaders[r][c].active == 1 && r == ROWS-1)
{
invaders[r][c].closest = 1;
isclosest = 1;
closest[c] = r;
}
else if (invaders[r+1][c].active == 0 && invaders[r][c].active == 1 && closest[c] == 255)
{
invaders[r][c].closest = 1;
isclosest = 1;
closest[c] = r;
}
if (isclosest != 0)
{
++count;
}
}
int index = 0;
for (int j = 0; j < COLS; ++j)
{
if(closest[j] != 255)
{
int x = (int)closest[j];
retList[index] = invaders[x][j];
++index;
}
}
}
if (count > 0)
return count;
else return 1; // I need to do this check in order to not make the program crash :(
}
SDL_Rect invaderShoot(Invader invaders[ROWS][COLS])
{
srand((int)time(NULL));
SDL_Rect ret;
Invader activeInvaders[COLS];
int count = ShootingInvaders(invaders, activeInvaders);
int r = (rand()) %count;
if (r >= 0 && r < count)
{
if (activeInvaders[r].active == 1)
ret = activeInvaders[r].pos;
}
return ret;
}