-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
896 lines (848 loc) · 22.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
#include <GL/glut.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#define TIMER_ID 0
#define TIMER_INTERVAL 20
#define UNUSED_ARG(x) ((void)(x))
static int win_width, win_height;
static bool animation_ongoing = false;
/* Svi moguci pokreti heroja u igri
*/
typedef enum {
PASSIVE,
NORTH,
EAST,
SOUTH,
WEST
} hero_motion;
/* Informacije o heroju
*/
typedef struct {
float position_x;
float position_y;
float actual_position_x;
float actual_position_y;
int size;
hero_motion direction;
float velocity;
bool drawing_wall;
bool last_maybe_matrix_check;
} hero_info;
typedef enum {
FIELD, // nema zida, trebalo bi da bude popunjeno zidom
BALL_FIELD, // ovo je jedina teritorija koja nije popunjena zidom
WALL, // popunjena zidom
MAYBE // jos uvek se gradi zid
} territory_state;
/* Informacije o loptama
*/
typedef struct {
float position_x;
float position_y;
float actual_position_x;
float actual_position_y;
float velocity_x;
float velocity_y; // brzina je norma vektora
float old_velocity_x;
float old_velocity_y; // brzina je norma vektora
int velocity_on_last_collision;
bool wall_horizontal_collision;
bool wall_vertical_collision;
} ball_info;
/* Informacije o platformi
*/
typedef struct {
int size_x;
int size_y;
} field_info;
static hero_info hero;
static ball_info ball;
static field_info field;
static territory_state **walls_and_territories;
static void game_info_initialization();
static void deinitialize_game();
static void update_actual_position();
static void change_maybes_and_fill();
static bool collision_detection();
static bool ball_collision_detection();
static void set_ball_fields(int x, int y);
static void draw_hero();
static void draw_field();
static void draw_ball();
static void on_display(void);
static void on_keyboard(unsigned char key, int x, int y);
static void on_reshape(int width, int height);
static void on_timer(int value);
static void test(){
for(int i = 0; i < field.size_y * 10; i += 10){
for(int j = 0; j < field.size_x * 10; j += 10){
switch(walls_and_territories[i][j]){
case FIELD:
printf("O ");
break;
case WALL:
printf("1 ");
break;
case MAYBE:
printf("v ");
break;
case BALL_FIELD:
printf("B ");
break;
default:
break;
}
}
puts("");
}
puts("**************");
}
static void test_field(){
int temp_x = hero.position_x * 10;
int temp_y = hero.position_y * 10;
for(int i = temp_y; i < temp_y + 10; ++i){
for(int j = temp_x; j < temp_x + 10; ++j){
switch(walls_and_territories[i][j]){
case FIELD:
printf("O ");
break;
case WALL:
printf("1 ");
break;
case MAYBE:
printf("v ");
break;
case BALL_FIELD:
printf("B ");
break;
default:
break;
}
}
puts("");
}
printf("##################################\n");
}
static void game_info_initialization()
{
field.size_x = 20;
field.size_y = 20;
hero.velocity = 0.5; // (0.1, +inf) nista izmedju .10...01 - 0.199...99
hero.position_x = (field.size_x - 1) / 2.0;
hero.position_y = 0;
hero.size = 1;
hero.direction = PASSIVE;
hero.drawing_wall = false;
hero.last_maybe_matrix_check = false;
ball.position_x = 2;
ball.position_y = 8;
ball.velocity_x = 0.1;
ball.velocity_y = ball.velocity_x;
ball.velocity_on_last_collision = 1;
/* Alokacija i inicijalizacija pojedinih polja za pocetni oblik zida*/
walls_and_territories = malloc(sizeof(*walls_and_territories) * field.size_x * field.size_y * 100);
if(NULL == walls_and_territories){
perror("aloc walls_and_territories");
exit(EXIT_FAILURE);
}
for(int i = 0; i < field.size_y * 10; ++i){
walls_and_territories[i] = malloc(sizeof(*walls_and_territories[i]) * field.size_x * 10);
if(NULL == walls_and_territories[i]){
perror("aloc walls_and_territories inside for");
exit(EXIT_FAILURE);
}
for(int j = 0; j < field.size_x * 10; ++j){
if(i < 10 || j < 10 ||
(i >= (field.size_x - 1) * 10 && i < field.size_x * 10) ||
(j >= (field.size_y - 1) * 10 && j < field.size_y * 10)){
walls_and_territories[i][j] = WALL;
}else{
walls_and_territories[i][j] = FIELD;
}
}
}
update_actual_position();
}
static void deinitialize_game()
{
for(int i = 0; i < field.size_y * 10; ++i){
free(walls_and_territories[i]);
}
free(walls_and_territories);
}
/* Vrsi se izracunavanje koordinata u prostoru.
* Pozicija za heroja (0, 0) je u donjem levom uglu
*/
static void update_actual_position()
{
hero.actual_position_x = (field.size_x - 1) / 2.0 - hero.position_x;
hero.actual_position_y = -(field.size_y - 1) / 2.0 + hero.position_y;
ball.actual_position_x = (field.size_x - 1) / 2.0 - ball.position_x + 0.5;
ball.actual_position_y = -(field.size_y - 1) / 2.0 + ball.position_y - 0.5;
int temp_x = hero.position_x * 10;
int temp_y = hero.position_y * 10;
/* TODO dodati novo stanje maybe pored true/false za "moguci" zid
*/
/* Popunjavanje zida na osnovu pozicije heroja */
if(hero.drawing_wall){
// animation_ongoing =false;// DEBUGGIN'
bool whole_maybe_matrix = true;
for(int i = temp_y; i < temp_y + 10; ++i){
for(int j = temp_x; j < temp_x + 10; ++j){
switch(walls_and_territories[i][j]){
case FIELD:
whole_maybe_matrix = false;
break;
case WALL:
whole_maybe_matrix = false;
break;
case MAYBE:
break;
default:
break;
}
}
}
// OKs
// 000 vvv
// vvv vvv
// vvv 000
//
// 0vv vv0
// 0vv vv0
// 0vv vv0
//
// vvv 000
// 000 000
// 000 vvv
//
// v00 00v
// v00 00v
// v00 00v
//
// i.e. gameover
// v v v v v v v v v v
// v v v v v v v v v v
// v v v v v v v v v v
// v v v v v v v v v v
// v v v v v v v v v v
// v v v v v v v v v v
// v v v v v v O O O O
// v v v v v v O O O O
// v v v v v v O O O O
// v v v v v v O O O O
bool whole_line = true;
territory_state temp;
/*
*/
for(int i = temp_y; i < temp_y + 10; ++i){
temp = walls_and_territories[i][temp_x];
for(int j = temp_x; j < temp_x + 10; ++j){
if(walls_and_territories[i][j] != temp){ // FIXME da radi samo sa MAYBE i FIELD, a WALL ignorise
whole_line = false;
break;
}
}
if(!whole_line) break;
}
if(!whole_line){
for(int j = temp_x; j < temp_x + 10; ++j){
temp = walls_and_territories[temp_y][j];
for(int i = temp_y; i < temp_y + 10; ++i){
if(walls_and_territories[i][j] != temp){ // FIXME kao gore navedeno
// GAME OVER case
// cosak potencijalnog zida sa nekim drugim se sudara
deinitialize_game();
game_info_initialization();
}
}
}
}
//test_field();
//
// TODO drawing stops
// v v v v v v O O 1 1
// v v v v v v O O 1 1
// v v v v v v O O 1 1
// v v v v v v O O 1 1
// v v v v v v O O 1 1
// v v v v v v O O 1 1
// v v v v v v O O 1 1
// v v v v v v O O 1 1
// v v v v v v O O 1 1
// v v v v v v O O 1 1
//
if(hero.last_maybe_matrix_check && whole_maybe_matrix){
// GAME OVER CASE
// presek dva potencijalna zida
deinitialize_game();
game_info_initialization();
return;
}else{
hero.last_maybe_matrix_check = whole_maybe_matrix;
}
}
// FIXME zavrsava se drawing_wall u gornjoj implementaciji, ne u donjoj
temp_x = hero.position_x * 10;
temp_y = hero.position_y * 10;
bool maybe_exists = false;
for(int i = temp_y; i < temp_y + 10; ++i){
for(int j = temp_x; j < temp_x + 10; ++j){
if(walls_and_territories[i][j] == FIELD){
if(!hero.drawing_wall) hero.drawing_wall = true;
maybe_exists = true;
walls_and_territories[i][j] = MAYBE;
}else if(walls_and_territories[i][j] == MAYBE){
maybe_exists = true;
}
}
}
if(walls_and_territories[temp_y][temp_x] == WALL && !maybe_exists && hero.drawing_wall){
hero.drawing_wall = false;
change_maybes_and_fill();
}
// test(); // DEBUGGIN'
}
static void change_maybes_and_fill(){
for(int i = 0; i < field.size_y * 10; ++i){
for(int j = 0; j < field.size_x * 10; ++j){
if(walls_and_territories[i][j] == MAYBE){
walls_and_territories[i][j] = WALL;
}
}
}
/* dfs menheten rastojanje po lopti, postavljanje stanja loptine teritorije */
printf("%f , %f\n", ball.position_x, ball.position_y);
set_ball_fields((int)ball.position_x, (int)ball.position_y);
test();
/* popunjavanje obicnih polja */
for(int i = 0; i < field.size_y * 10; ++i){
for(int j = 0; j < field.size_x * 10; ++j){
if(walls_and_territories[i][j] == FIELD){
walls_and_territories[i][j] = WALL;
/* TODO Uvodjenje novog stanja za animaciju uzdizanja zida u buducnosti */
}
}
}
/* reset loptine teritorije u obicna polja*/
for(int i = 0; i < field.size_y * 10; ++i){
for(int j = 0; j < field.size_x * 10; ++j){
if(walls_and_territories[i][j] == BALL_FIELD){
walls_and_territories[i][j] = FIELD;
/* TODO Uvodjenje novog stanja za animaciju uzdizanja zida u buducnosti */
}
}
}
}
// FIXME Zna da pretera(crta gde ne treba), problem je tip podataka argumenata verovatno(zaokruzivanje)
// NOTE ovde smetaju one vrednosti brzine kretanja heroja sto su izmedju 0.1 i 0.2
static void set_ball_fields(int x, int y)
{
int temp_x = x * 10;
int temp_y = y * 10;
if(walls_and_territories[temp_y][temp_x] == WALL){
return;
}
if(walls_and_territories[temp_y][temp_x] == BALL_FIELD){
return;
}
for(int i = temp_y; i < temp_y + 10; ++i){
for(int j = temp_x; j < temp_x + 10; ++j){
if(walls_and_territories[i][j] == FIELD){
walls_and_territories[i][j] = BALL_FIELD;
}
}
}
set_ball_fields(x + 1, y);
set_ball_fields(x, y + 1);
set_ball_fields(x - 1, y);
set_ball_fields(x, y - 1);
}
/* Provera za one timer */
static bool collision_detection()
{
if(hero.position_x < 0){
hero.position_x = 0;
return true;
}else if(hero.position_x > field.size_x - 1){
hero.position_x = field.size_x - 1;
return true;
}
if(hero.position_y < 0){
hero.position_y = 0;
return true;
}else if(hero.position_y > field.size_y - 1){
hero.position_y = field.size_y - 1;
return true;
}
return false;
}
// TODO
// Obrada kolizija izmedju vise lopti
static bool ball_collision_detection()
{
/* prilagodjavanje za matricu koja predstavlja postavljene zidove */
/* uzima se informacija radi provera buduce pozicije lopte */
int temp_x = ((ball.position_x) + ball.velocity_x) * 10 ;
int temp_y = ((ball.position_y) + ball.velocity_y) * 10 ;
if(walls_and_territories[temp_y][temp_x] == MAYBE ||
/* GAME OVER CASE TODO
* Ovo je za sada ovako, ubaciti mogucnost da se izgubi igra ako se dotakne potencijalni zid
* ili da neko vreme dok ne postane potencijalni zid - pravi zid
*/
walls_and_territories[temp_y][temp_x] == WALL){
/* Resavanje iscrtavanja ulaska lopte u koliziju */
ball.position_x -= ball.velocity_x;
ball.position_y -= ball.velocity_y;
/* Gledanje situacije pre nego sto je lopta usla u koliziju*/
temp_x -= ball.velocity_x * 10;
temp_y -= ball.velocity_y * 10;
/****
*1
*
*7
***/
/* Gledanje slucajeva kada je
* lopta udarila u horizontalnu/vertikalnu stranu zida
* suzbijanje situacije kada je zid 2 puta
* udarena horizontalna/vertikalna strana zida.
*/
if((walls_and_territories[temp_y + 10][temp_x + 10]) == WALL &&
(walls_and_territories[temp_y - 10][temp_x + 10]) == WALL){
if(ball.wall_vertical_collision) ball.velocity_y *= -1;
ball.wall_vertical_collision = true;
ball.wall_horizontal_collision = false;
}
/****
* 3
*
* 9
***/
else if((walls_and_territories[temp_y + 10][temp_x - 10]) == WALL &&
(walls_and_territories[temp_y - 10][temp_x - 10]) == WALL){
if(ball.wall_vertical_collision) ball.velocity_y *= -1;
ball.wall_vertical_collision = true;
ball.wall_horizontal_collision = false;
}
/****
*
*
*7 9
****/
else if((walls_and_territories[temp_y - 10][temp_x - 10]) &&
(walls_and_territories[temp_y - 10][temp_x + 10])){
if(ball.wall_horizontal_collision)
ball.velocity_x *= -1;
ball.wall_vertical_collision = false;
ball.wall_horizontal_collision = true;
}
/***
*1 3
*
***/
else if((walls_and_territories[temp_y + 10][temp_x - 10]) == WALL &&
(walls_and_territories[temp_y + 10][temp_x + 10]) == WALL
){
if(ball.wall_horizontal_collision)
ball.velocity_x *= -1;
ball.wall_vertical_collision = false;
ball.wall_horizontal_collision = true;
}
/*** DEBUG U sluzbi debagovanja
* 123
* 456
* 789
***
else if((temp_x - 10 > 0)&&(temp_y + 10 < field.size_y * 10)){
printf("%d", walls_and_territories[temp_y + 10][temp_x - 10]);
}else{
putchar('x');
}
if((temp_y + 10 < field.size_y * 10)){
printf("%d", walls_and_territories[temp_y + 10][temp_x]);
}else{
putchar('x');
}
if((temp_x + 10 < field.size_x * 10)&&(temp_y + 10 < field.size_y * 10)){
printf("%d", walls_and_territories[temp_y + 10][temp_x + 10]);
}else{
putchar('x');
}
puts("");
if((temp_x - 10 > 0)){
printf("%d", walls_and_territories[temp_y][temp_x - 10]);
}else{
putchar('x');
}
printf("%d", walls_and_territories[temp_y][temp_x]);
if((temp_x + 10 < field.size_x * 10)){
printf("%d", walls_and_territories[temp_y][temp_x + 10]);
}else{
putchar('x');
}
puts("");
if((temp_x - 10 > 0)&&(temp_y - 10 > 0)){
printf("%d", walls_and_territories[temp_y - 10][temp_x - 10]);
}else{
putchar('x');
}
if((temp_y - 10 > 0)){
printf("%d", walls_and_territories[temp_y - 10][temp_x]);
}else{
putchar('x');
}
if((temp_x + 10 < field.size_x * 10)&&(temp_y - 10 > 0)){
printf("%d", walls_and_territories[temp_y - 10][temp_x + 10]);
}else{
putchar('x');
}
puts("");
****** DEBUG */
// vx>0 vy>0 velocity_on_last_collision
// 0 0 0
// 0 1 1
// 1 0 2
// 1 1 3
// 1 3 2 0 Clockwise motion
// 2 3 1 0 Counter-Clockwise motion
if(ball.velocity_x > 0 && ball.velocity_y > 0){ // 3
if(ball.velocity_on_last_collision == 1){
ball.velocity_x *= 1;
ball.velocity_y *= -1;
}else if(ball.velocity_on_last_collision == 2){
ball.velocity_x *= -1;
ball.velocity_y *= 1;
}
ball.velocity_on_last_collision = 3;
}else if(ball.velocity_x > 0 && ball.velocity_y < 0){ // 2
if(ball.velocity_on_last_collision == 3){
ball.velocity_x *= -1;
ball.velocity_y *= 1;
}else if(ball.velocity_on_last_collision == 0){
ball.velocity_x *= 1;
ball.velocity_y *= -1;
}
ball.velocity_on_last_collision = 2;
}else if(ball.velocity_x < 0 && ball.velocity_y < 0){ // 0
if(ball.velocity_on_last_collision == 2){
ball.velocity_x *= 1;
ball.velocity_y *= -1;
}else if(ball.velocity_on_last_collision == 1){
ball.velocity_x *= -1;
ball.velocity_y *= 1;
}
ball.velocity_on_last_collision = 0;
}else if(ball.velocity_x < 0 && ball.velocity_y > 0){ // 1
if(ball.velocity_on_last_collision == 0){
ball.velocity_x *= -1;
ball.velocity_y *= 1;
}else if(ball.velocity_on_last_collision == 3){
ball.velocity_x *= 1;
ball.velocity_y *= -1;
}
ball.velocity_on_last_collision = 1;
}
return true;
}
return false;
}
/* Iscrtavanja komponenti iz igrice, podesavanje refleksija svetlosti */
static void draw_hero()
{
glMatrixMode(GL_MODELVIEW);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
GLfloat ambient_coeffs[] = {0.7, 0.1, 0.1, 1};
GLfloat diffuse_coeffs[] = {1, 0, 0, 1};
glMaterialfv(GL_FRONT, GL_AMBIENT, ambient_coeffs);
glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse_coeffs);
update_actual_position();
glPushMatrix();
glTranslatef(hero.actual_position_x, hero.actual_position_y, 0);
glutSolidCube(hero.size);
glPopMatrix();
}
static void draw_ball()
{
glMatrixMode(GL_MODELVIEW);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
GLfloat ambient_coeffs[] = {0.1, 0.8, 0.1, 1};
GLfloat diffuse_coeffs[] = {0, 1, 0, 1};
glMaterialfv(GL_FRONT, GL_AMBIENT, ambient_coeffs);
glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse_coeffs);
update_actual_position();
glPushMatrix();
glTranslatef(ball.actual_position_x, ball.actual_position_y, -1);
glutSolidSphere(0.5, 20, 20);
glPopMatrix();
}
void draw_wall_part()
{
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glScalef(0.1, 0.1, 1);
glutSolidCube(1);
}
/*TODO
* popunjavanje povrsine koja je osvojena
*/
static void draw_field()
{
glTranslatef(0, 0, 1);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
GLfloat ambient_coeffs1[] = {0.1, 0.2, 0.2, 1};
GLfloat diffuse_coeffs1[] = {0, 0.1, 0.7, 1};
glMaterialfv(GL_FRONT, GL_AMBIENT, ambient_coeffs1);
glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse_coeffs1);
/* Ubaci flood algorithm
* promenljiva unutrasnjosti zida
* 2 promenljive za pocetak i kraj zida po x-osi
*
* ****** ****
* ^
* ^- polovina
* glScalef(polovina, 1, 1)
* ^- za ovu dole drugu ideju
* */
/* TODO Animacija novonastalih zidova (potrebna su nova stanja za walls_and_territories) */
bool inside_wall = false;
/* Float zato sto je korisno pri prosledjivanju
* argumentu afine transformacije bez odsecanja decimala */
float wall_x_start;
float wall_x_stop;
float wall_x_middle;
for(int i = 0; i < field.size_y * 10; ++i){
for(int j = 0; j <= field.size_x * 10; ++j){
if(walls_and_territories[i][j] == WALL && !(j == field.size_x * 10)){
if(!inside_wall){
wall_x_start = j;
}
inside_wall = true;
}else{
if(inside_wall){
wall_x_stop = j;
wall_x_middle = (wall_x_stop - wall_x_start) / 2.0;
// printf("%d , %d :: %d\n", wall_x_start, wall_x_stop, wall_x_middle);
// for debuggin' stuff
glPushMatrix();
glTranslatef(- (wall_x_start + wall_x_middle) / 10.0 + (field.size_x - 1) / 2.0 + 0.45,
i / 10.0 + -(field.size_y - 1) / 2.0 - 0.45,
0);
glScalef(2* wall_x_middle, 1, 1);
draw_wall_part();
glPopMatrix();
}
inside_wall = false;
}
}
}
/***
* Iscrtavanje "potencijalnih" zidova
***/
GLfloat ambient_coeffs3[] = {0.2, 0.2, 0.1, 1};
GLfloat diffuse_coeffs3[] = {0.7, 0.7, 0, 1};
glMaterialfv(GL_FRONT, GL_AMBIENT, ambient_coeffs3);
glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse_coeffs3);
inside_wall = false;
for(int i = 0; i < field.size_y * 10; ++i){
for(int j = 0; j <= field.size_x * 10; ++j){
if(walls_and_territories[i][j] == MAYBE && !(j == field.size_x * 10)){
if(!inside_wall){
wall_x_start = j;
}
inside_wall = true;
}else{
if(inside_wall){
wall_x_stop = j;
wall_x_middle = (wall_x_stop - wall_x_start) / 2.0;
// printf("%d , %d :: %d\n", wall_x_start, wall_x_stop, wall_x_middle);
// for debuggin' stuff
glPushMatrix();
glTranslatef(- (wall_x_start + wall_x_middle) / 10.0 + (field.size_x - 1) / 2.0 + 0.45,
i / 10.0 + -(field.size_y - 1) / 2.0 - 0.45,
0);
glScalef(2* wall_x_middle, 1, 1);
draw_wall_part();
glPopMatrix();
}
inside_wall = false;
}
}
}
/******
*
* MNOGO GORA IMPLEMENTACIJA
*
* ****
for(int i = 0; i < field.size_y * 10; ++i){
for(int j = 0; j < field.size_x * 10; ++j){
if(walls_and_territories[i][j] == WALL){
glPushMatrix();
glTranslatef(- j / 10.0 + (field.size_x - 1) / 2.0 + 0.45,
i / 10.0 + -(field.size_y - 1) / 2.0 - 0.45,
0);
draw_wall_part();
glPopMatrix();
}
}
}
*/
/* Donja platforma */
glTranslatef(0, 0, 1);
GLfloat diffuse_coeffs2[] = {0.5, 0.5, 0.5, 1};
glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse_coeffs2);
glPushMatrix();
glScalef(field.size_x, field.size_y, 1);
glutSolidCube(1);
glPopMatrix();
}
static void on_display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
gluLookAt(0, -field.size_y, -field.size_y,
0, 0, 0,
0, 1, 0);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_LIGHTING);
/* Svetlo global:no */
glEnable(GL_LIGHT0);
/* Postavlja se jedinicna matrica zbog pozicije direkcionog svetla
* i celokupnog zdravlja iscrtavanja
*/
glLoadIdentity();
float light_position[] = {-1, 0.1, -1};
/* Boje svetla uvek iste */
float light_ambient[] = {0.3, 0.3, 0.3, 1};
float light_diffuse[] = {0.7, 0.7, 0.7, 1};
float light_specular[] = {0.9, 0.9, 0.9, 1};
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
GLfloat ambient_coeffs[] = {0.3, 0.1, 0.1, 1};
GLfloat diffuse_coeffs[] = {1, 0, 0, 1};
GLfloat specular_coeffs[] = {1, 1, 1, 1};
GLfloat shininess = 20;
glMaterialfv(GL_FRONT, GL_AMBIENT, ambient_coeffs);
glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse_coeffs);
glMaterialfv(GL_FRONT, GL_SPECULAR, specular_coeffs);
glMaterialf(GL_FRONT, GL_SHININESS, shininess);
glShadeModel(GL_SMOOTH);
draw_hero();
draw_field();
draw_ball();
glutSwapBuffers();
}
static void on_keyboard(unsigned char key, int x, int y)
{
UNUSED_ARG(x);
UNUSED_ARG(y);
switch (key) {
case 27:
deinitialize_game();
exit(0);
break;
case ' ':
animation_ongoing = !animation_ongoing;
break;
case 'w':
case 'W':
animation_ongoing = true;
hero.direction = NORTH;
break;
case 's':
case 'S':
animation_ongoing = true;
hero.direction = SOUTH;
break;
case 'a':
case 'A':
animation_ongoing = true;
hero.direction = WEST;
break;
case 'd':
case 'D':
animation_ongoing = true;
hero.direction = EAST;
break;
case 'r':
case 'R':
deinitialize_game();
game_info_initialization();
break;
default:
break;
}
}
/* Namesta se pozicija iscrtavanja po prozoru,
* proporcija display-a i vidljivost.
* Namesta se jedinicna matrica da se ne bi primenjivala
* afina transformacija vise puta pri
* visestrukoj izmeni velicine prozora.
*/
static void on_reshape(int width, int height)
{
win_height = height;
win_width = width;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, (float)width / height, 1, 100);
}
/* Onde gde se realizuje animacija
*/
static void on_timer(int value)
{
if(value != TIMER_ID){
return;
}
if(!animation_ongoing){
glutTimerFunc(TIMER_INTERVAL, on_timer, TIMER_ID);
return;
}
switch(hero.direction){
case NORTH:
hero.position_y += hero.velocity;
break;
case EAST:
hero.position_x += hero.velocity;
break;
case SOUTH:
hero.position_y -= hero.velocity;
break;
case WEST:
hero.position_x -= hero.velocity;
break;
default:
break;
}
ball.position_x += ball.velocity_x;
ball.position_y += ball.velocity_y;
if(collision_detection()){
hero.direction = PASSIVE;
}
ball_collision_detection();
glutPostRedisplay();
glutTimerFunc(TIMER_INTERVAL, on_timer, TIMER_ID);
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowPosition(200, 100);
glutInitWindowSize(500,500);
glutCreateWindow("flosXONIX");
glutKeyboardFunc(on_keyboard);
glutReshapeFunc(on_reshape);
glutDisplayFunc(on_display);
glutTimerFunc(TIMER_INTERVAL, on_timer, TIMER_ID);
glClearColor(0.75, 0.65, 0.55, 0);
glEnable(GL_DEPTH_TEST);
game_info_initialization();
glutMainLoop();
return 0;
}