-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
357 lines (295 loc) · 6.8 KB
/
main.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
345
346
347
348
349
350
351
352
#include <stdlib.h>
#include <GL/glut.h>
#include <stdio.h>
#include "map.h"
#include "structures.h"
#include "basic_draws.h"
#include "image.h"
#include "invaders.h"
#include <math.h>
#define UNUSED(x) ((void)x)
//timeri
#define game_timer 0
#define countdown_timer 1
#define PI 3.14159265
//F-je pomocne
int objectX();
int objectY();
// callback fje - deklaracija
static void on_keyboard(unsigned char key, int x, int y);
static void on_reshape(int width, int height);
static void on_display(void);
static void on_timer(int timer_id);
static float px = 5;
static float py = 5;
static float pz = 0;
static float rot = MAP_SIZE /2;
//pomocni parametri
static int lockCamera = 0;
static int lastDirection = 0;
static int animation = 0;
static short final_attack =0;
static short end = 0;
static int time_left = 2;
static float final_x = 0;
static float final_y = 0;
int main(int argc, char **argv)
{
//inicijalizacija
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
//prozor
glutInitWindowSize(600, 600);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
//callback fje
glutKeyboardFunc(on_keyboard);
glutReshapeFunc(on_reshape);
glutDisplayFunc(on_display);
// GL inicijalizacija
glClearColor(0,1,1, 0);
glEnable(GL_DEPTH_TEST);
glLineWidth(2);
//inicijalizacija programa
initMapMatrix();
initStructures();
initTextures();
initInvaders();
glutMainLoop();
return 0;
}
static void on_keyboard(unsigned char key, int x, int y)
{
UNUSED(x);
UNUSED(y);
unsigned char k;
if(final_attack){
if(key != 27 && key != 32){
k = 0;
}
else{
k = key;
}
}else{
k = key;
}
switch (k) {
case 27:
/* Zavrsava se program. */
exit(0);
break;
//kretanje igraca
case 'd':
mapMovePlayer(-1,0);
lastDirection = MOVE_RIGHT;
on_display();
break;
case 'a':
mapMovePlayer(1,0);
lastDirection = MOVE_LEFT;
on_display();
break;
case 'w':
mapMovePlayer(0,1);
lastDirection = MOVE_FORWARD;
on_display();
break;
case 's':
mapMovePlayer(0,-1);
lastDirection = MOVE_BACKWARD;
on_display();
break;
// SPACE dugme - u slicaju da kamera nije zakljucana pozicionira kameru na igraca
case 32:
pz = getY()-4;
rot = getX();
px = getX() -1;
on_display();
break;
//postavljanje kule
case 't':
structPutTower(objectX(),objectY());
on_display();
break;
//postavljanej zida
case 'z':
structPutWall(objectX(),objectY());
on_display();
break;
//postavljanje Posta
case 'p':
structPutPost(objectX(),objectY());
on_display();
break;
//"zakljucavanje/otkljucavanje kamere"
case 'c':
if(lockCamera == 1)
lockCamera =0;
else
lockCamera = 1;
break;
//resetovanje postavljenih objekata
case 'r':
initStructures();
initMapMatrix();
initInvaders();
glutPostRedisplay();
break;
//zapocinjanje napada
case 'f':
glutTimerFunc(1000,on_timer,countdown_timer);
break;
}
}
//prilikom pokusaja da se promeni velicina prozora, ista se ne menja
static void on_reshape(int width, int height)
{
UNUSED(width);
UNUSED(height);
//f-ja koja postavlja velicinu prozora
glutReshapeWindow(800,600);
}
static void on_display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0, 0, 800, 600);
// matrica projekcije
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(
90,
800/(float)600,
0.2, 50);
// Kamera
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
if (lockCamera == 1){
pz = getY()-4;
rot = getX();
px = getX() -1 ;
}
gluLookAt(
px,py,pz,
rot, 0,MAP_SIZE,
0, 1, 0
);
int i;
if(animation == 1){
int i;
for(i=0;i<MAX_EAGLES;i++){
riseEagle();
}
for(i=0;i<MAX_TROUPERS;i++){
riseTrouper();
}
animation = 0;
}
for(i=0;i<MAX_TROUPERS;i++){
if(tIsAlive(i)){
drawTrouper(tGetX(i),tGetY(i));
}
}
for(i=0;i<MAX_EAGLES;i++){
if(eIsAlive(i)){
drawEagle(eGetX(i),eGetY(i));
}
}
/*Iscrtavanje mape i njenih objekata*/
drawMap();
drawNexus();
if(final_attack == 0){
drawPlayer(getX(),getY(),lastDirection);
}
else{
glPushMatrix();
glTranslatef(0,1,0);
drawPlayer(final_x,final_y,0);
glPopMatrix();
}
//Provera i iscrtavanje postojecih kula
for(i=0;i<MAX_TOWERS;i++){
if(getTowerX(i) == -1)
continue;
else{
drawTower(getTowerX(i),getTowerY(i));
}
}
//iscrtavanej postojecih zidova
for(i=0;i<MAX_WALLS;i++){
if(getWallX(i) == -1){
continue;}
else{
drawWall(getWallX(i),getWallY(i));
}
}
//iscrtavanje postojecih postova
for(i=0;i<MAX_POSTS;i++){
if(getPostX(i) == -1){
continue;}
else{
drawPost(getPostX(i),getPostY(i));
}
}
drawMenu();
glutSwapBuffers();
}
//za odredjivanje tacnog mesta objekta u odnosu na rotaciju igraca
int objectX(){
switch (lastDirection){
case 0:
return getX();
case 1:
return getX()+1;
case 2:
return getX();
case 3:
return getX() -1;
}
return -1;
}
int objectY(){
switch (lastDirection){
case 0:
return getY()+1;
case 1:
return getY();
case 2:
return getY()-1;
case 3:
return getY();
}
return -1;
}
/*timer funkcija
2 timera:
1. za zavrsni napad
2. odbrojavanje pred konacni napad i podesavanje okruzenja*/
static void on_timer(int timer_id){
if(timer_id == game_timer){
gameLoop();
glutPostRedisplay();
end = checkGame();
if(end != 0){
printf("end:%d\n",end);
endGame(end);
}else{
glutTimerFunc(1000,on_timer,timer_id);
}
}else if(timer_id == countdown_timer){
time_left -=1;
glutPostRedisplay();
if(time_left == 0){
glutTimerFunc(1000,on_timer,game_timer);
final_attack = 1;
prepareForAttack();
lockCamera = 0;
px = -1;
pz = -4;
}
else{
animation = 1;
glutTimerFunc(1000,on_timer,countdown_timer);
}
}
return;
}