-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
1084 lines (896 loc) · 35.4 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
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
James William Fletcher (github.com/mrbid)
December 2021
A simple 3D snowboarding game.
*/
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define uint GLushort
#define f32 GLfloat
#include "gl.h"
#define GLFW_INCLUDE_NONE
#include "glfw3.h"
#ifndef __x86_64__
#define NOSSE
#endif
#define SEIR_RAND
#include "esAux2.h"
#include "res.h"
#include "tunnel.h"
#include "dead.h"
#include "three.h"
#include "two.h"
#include "one.h"
#include "lslick.h"
#include "mslick.h"
#include "rslick.h"
#include "lgem.h"
#include "mgem.h"
#include "rgem.h"
#include "lboost.h"
#include "mboost.h"
#include "rboost.h"
#include "lboarder.h"
#include "mboarder.h"
#include "rboarder.h"
//*************************************
// globals
//*************************************
GLFWwindow* window;
uint winw = 1024;
uint winh = 768;
double t = 0;
f32 aspect;
double ww, wh, ww2, wh2;
double uw, uh, uw2, uh2; // normalised pixel dpi
// render state id's
GLint projection_id;
GLint modelview_id;
GLint position_id;
GLint lightpos_id;
GLint solidcolor_id;
GLint color_id;
GLint opacity_id;
GLint normal_id;
// render state matrices
mat projection;
mat view;
mat model;
mat modelview;
// render state inputs
vec lightpos = {0.f, 0.f, 0.f};
// models
uint bindstate = 0;
ESModel mdlTunnel;
ESModel mdlDead;
ESModel mdlThree;
ESModel mdlTwo;
ESModel mdlOne;
ESModel mdlLslick;
ESModel mdlMslick;
ESModel mdlRslick;
ESModel mdlLgem;
ESModel mdlMgem;
ESModel mdlRgem;
ESModel mdlLboost;
ESModel mdlMboost;
ESModel mdlRboost;
ESModel mdlLboarder;
ESModel mdlMboarder;
ESModel mdlRboarder;
// game vars
f32 dt = 0;
double rt = 0;
uint state = 3;
f32 camdist = -5.0f;
uint keystate[2] = {0};
uint player_x = 1;
uint score = 0;
#define ARRAY_MAX 6
typedef struct
{
f32 z;
uint x;
uint s;
} gi;
uint max_slicks = 0;
uint max_gems = 0;
uint max_boosts = 0;
gi array_slick[ARRAY_MAX];
gi array_gem[ARRAY_MAX];
gi array_boost[ARRAY_MAX];
#define NEWGAME_SEED 1337
#define MINSPEED -9.f
#define MAXSPEED -32.f
#define DRAGRATE 3.3f
f32 pz = 0.f;
f32 ps = MINSPEED;
f32 pr = 0.f;
f32 po = 1.0f;
vec c1 = {1.000,0.325,0.251};
vec c2 = {0.404,1.000,0.663};
vec c3 = {0.827,0.400,1.000};
//*************************************
// utility functions
//*************************************
void timestamp(char* ts)
{
const time_t tt = time(0);
strftime(ts, 16, "%H:%M:%S", localtime(&tt));
}
//*************************************
// render functions
//*************************************
void rTunnel()
{
bindstate = 0;
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (f32*) &view.m[0][0]);
glUniform1f(opacity_id, 1.0f);
glBindBuffer(GL_ARRAY_BUFFER, mdlTunnel.vid);
glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(position_id);
glBindBuffer(GL_ARRAY_BUFFER, mdlTunnel.nid);
glVertexAttribPointer(normal_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(normal_id);
glBindBuffer(GL_ARRAY_BUFFER, mdlTunnel.cid);
glVertexAttribPointer(color_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(color_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlTunnel.iid);
glDrawElements(GL_TRIANGLES, tunnel_numind, GL_UNSIGNED_SHORT, 0);
}
void rDead(f32 z)
{
bindstate = 0;
mIdent(&model);
mTranslate(&model, 0.f, 0.f, z);
mMul(&modelview, &model, &view);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (f32*) &modelview.m[0][0]);
glUniform1f(opacity_id, 1.0f);
glBindBuffer(GL_ARRAY_BUFFER, mdlDead.vid);
glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(position_id);
glBindBuffer(GL_ARRAY_BUFFER, mdlDead.nid);
glVertexAttribPointer(normal_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(normal_id);
glBindBuffer(GL_ARRAY_BUFFER, mdlDead.cid);
glVertexAttribPointer(color_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(color_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlDead.iid);
glDrawElements(GL_TRIANGLES, dead_numind, GL_UNSIGNED_SHORT, 0);
}
void rCountDown(f32 z, uint c)
{
bindstate = 0;
mIdent(&model);
mTranslate(&model, 0.f, 0.f, z);
mMul(&modelview, &model, &view);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (f32*) &modelview.m[0][0]);
glUniform1f(opacity_id, 1.0f);
if(c == 3)
{
glBindBuffer(GL_ARRAY_BUFFER, mdlThree.vid);
glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(position_id);
glBindBuffer(GL_ARRAY_BUFFER, mdlThree.nid);
glVertexAttribPointer(normal_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(normal_id);
glBindBuffer(GL_ARRAY_BUFFER, mdlThree.cid);
glVertexAttribPointer(color_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(color_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlThree.iid);
glDrawElements(GL_TRIANGLES, three_numind, GL_UNSIGNED_SHORT, 0);
}
else if(c == 2)
{
glBindBuffer(GL_ARRAY_BUFFER, mdlTwo.vid);
glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(position_id);
glBindBuffer(GL_ARRAY_BUFFER, mdlTwo.nid);
glVertexAttribPointer(normal_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(normal_id);
glBindBuffer(GL_ARRAY_BUFFER, mdlTwo.cid);
glVertexAttribPointer(color_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(color_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlTwo.iid);
glDrawElements(GL_TRIANGLES, two_numind, GL_UNSIGNED_SHORT, 0);
}
else if(c == 1)
{
glBindBuffer(GL_ARRAY_BUFFER, mdlOne.vid);
glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(position_id);
glBindBuffer(GL_ARRAY_BUFFER, mdlOne.nid);
glVertexAttribPointer(normal_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(normal_id);
glBindBuffer(GL_ARRAY_BUFFER, mdlOne.cid);
glVertexAttribPointer(color_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(color_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlOne.iid);
glDrawElements(GL_TRIANGLES, one_numind, GL_UNSIGNED_SHORT, 0);
}
}
void rSlick(f32 z, uint x)
{
mIdent(&model);
mTranslate(&model, 0.f, 0.f, z);
mMul(&modelview, &model, &view);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (f32*) &modelview.m[0][0]);
glUniform1f(opacity_id, 1.0f);
if(x == 0)
{
if(bindstate != 1)
{
glBindBuffer(GL_ARRAY_BUFFER, mdlLslick.vid);
glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(position_id);
glBindBuffer(GL_ARRAY_BUFFER, mdlLslick.nid);
glVertexAttribPointer(normal_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(normal_id);
glBindBuffer(GL_ARRAY_BUFFER, mdlLslick.cid);
glVertexAttribPointer(color_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(color_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlLslick.iid);
bindstate = 1;
}
glDrawElements(GL_TRIANGLES, lslick_numind, GL_UNSIGNED_SHORT, 0);
}
else if(x == 1)
{
if(bindstate != 2)
{
glBindBuffer(GL_ARRAY_BUFFER, mdlMslick.vid);
glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(position_id);
glBindBuffer(GL_ARRAY_BUFFER, mdlMslick.nid);
glVertexAttribPointer(normal_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(normal_id);
glBindBuffer(GL_ARRAY_BUFFER, mdlMslick.cid);
glVertexAttribPointer(color_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(color_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlMslick.iid);
bindstate = 2;
}
glDrawElements(GL_TRIANGLES, mslick_numind, GL_UNSIGNED_SHORT, 0);
}
else if(x == 2)
{
if(bindstate != 3)
{
glBindBuffer(GL_ARRAY_BUFFER, mdlRslick.vid);
glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(position_id);
glBindBuffer(GL_ARRAY_BUFFER, mdlRslick.nid);
glVertexAttribPointer(normal_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(normal_id);
glBindBuffer(GL_ARRAY_BUFFER, mdlRslick.cid);
glVertexAttribPointer(color_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(color_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlRslick.iid);
bindstate = 3;
}
glDrawElements(GL_TRIANGLES, rslick_numind, GL_UNSIGNED_SHORT, 0);
}
}
void rGem(f32 z, uint x)
{
mIdent(&model);
mTranslate(&model, 0.f, 0.f, z);
mMul(&modelview, &model, &view);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (f32*) &modelview.m[0][0]);
glUniform1f(opacity_id, 1.0f);
if(x == 0)
{
if(bindstate != 4)
{
glBindBuffer(GL_ARRAY_BUFFER, mdlLgem.vid);
glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(position_id);
glBindBuffer(GL_ARRAY_BUFFER, mdlLgem.nid);
glVertexAttribPointer(normal_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(normal_id);
glBindBuffer(GL_ARRAY_BUFFER, mdlLgem.cid);
glVertexAttribPointer(color_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(color_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlLgem.iid);
bindstate = 4;
}
glDrawElements(GL_TRIANGLES, lgem_numind, GL_UNSIGNED_SHORT, 0);
}
else if(x == 1)
{
if(bindstate != 5)
{
glBindBuffer(GL_ARRAY_BUFFER, mdlMgem.vid);
glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(position_id);
glBindBuffer(GL_ARRAY_BUFFER, mdlMgem.nid);
glVertexAttribPointer(normal_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(normal_id);
glBindBuffer(GL_ARRAY_BUFFER, mdlMgem.cid);
glVertexAttribPointer(color_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(color_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlMgem.iid);
bindstate = 5;
}
glDrawElements(GL_TRIANGLES, mgem_numind, GL_UNSIGNED_SHORT, 0);
}
else if(x == 2)
{
if(bindstate != 6)
{
glBindBuffer(GL_ARRAY_BUFFER, mdlRgem.vid);
glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(position_id);
glBindBuffer(GL_ARRAY_BUFFER, mdlRgem.nid);
glVertexAttribPointer(normal_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(normal_id);
glBindBuffer(GL_ARRAY_BUFFER, mdlRgem.cid);
glVertexAttribPointer(color_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(color_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlRgem.iid);
bindstate = 6;
}
glDrawElements(GL_TRIANGLES, rgem_numind, GL_UNSIGNED_SHORT, 0);
}
}
void rBoost(f32 z, uint x)
{
mIdent(&model);
mTranslate(&model, 0.f, 0.f, z);
mMul(&modelview, &model, &view);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (f32*) &modelview.m[0][0]);
glUniform1f(opacity_id, 1.0f);
if(x == 0)
{
if(bindstate != 7)
{
glBindBuffer(GL_ARRAY_BUFFER, mdlLboost.vid);
glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(position_id);
glBindBuffer(GL_ARRAY_BUFFER, mdlLboost.nid);
glVertexAttribPointer(normal_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(normal_id);
glBindBuffer(GL_ARRAY_BUFFER, mdlLboost.cid);
glVertexAttribPointer(color_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(color_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlLboost.iid);
bindstate = 7;
}
glDrawElements(GL_TRIANGLES, lboost_numind, GL_UNSIGNED_SHORT, 0);
}
else if(x == 1)
{
if(bindstate != 8)
{
glBindBuffer(GL_ARRAY_BUFFER, mdlMboost.vid);
glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(position_id);
glBindBuffer(GL_ARRAY_BUFFER, mdlMboost.nid);
glVertexAttribPointer(normal_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(normal_id);
glBindBuffer(GL_ARRAY_BUFFER, mdlMboost.cid);
glVertexAttribPointer(color_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(color_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlMboost.iid);
bindstate = 8;
}
glDrawElements(GL_TRIANGLES, mboost_numind, GL_UNSIGNED_SHORT, 0);
}
else if(x == 2)
{
if(bindstate != 9)
{
glBindBuffer(GL_ARRAY_BUFFER, mdlRboost.vid);
glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(position_id);
glBindBuffer(GL_ARRAY_BUFFER, mdlRboost.nid);
glVertexAttribPointer(normal_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(normal_id);
glBindBuffer(GL_ARRAY_BUFFER, mdlRboost.cid);
glVertexAttribPointer(color_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(color_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlRboost.iid);
bindstate = 9;
}
glDrawElements(GL_TRIANGLES, rboost_numind, GL_UNSIGNED_SHORT, 0);
}
}
void rBoarder(f32 z, uint x, f32 r)
{
mIdent(&model);
mTranslate(&model, 0.f, 0.f, z);
if(x == 1)
mRotX(&model, r);
mMul(&modelview, &model, &view);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (f32*) &modelview.m[0][0]);
if(po < 1.0f)
po += 1.0f * dt;
else
po = 1.0f;
glUniform1f(opacity_id, po);
if(po != 1.0f)
glEnable(GL_BLEND);
if(x == 0)
{
glBindBuffer(GL_ARRAY_BUFFER, mdlLboarder.vid);
glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(position_id);
glBindBuffer(GL_ARRAY_BUFFER, mdlLboarder.nid);
glVertexAttribPointer(normal_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(normal_id);
glBindBuffer(GL_ARRAY_BUFFER, mdlLboarder.cid);
glVertexAttribPointer(color_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(color_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlLboarder.iid);
glDrawElements(GL_TRIANGLES, lboarder_numind, GL_UNSIGNED_SHORT, 0);
}
else if(x == 1)
{
glBindBuffer(GL_ARRAY_BUFFER, mdlMboarder.vid);
glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(position_id);
glBindBuffer(GL_ARRAY_BUFFER, mdlMboarder.nid);
glVertexAttribPointer(normal_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(normal_id);
glBindBuffer(GL_ARRAY_BUFFER, mdlMboarder.cid);
glVertexAttribPointer(color_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(color_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlMboarder.iid);
glDrawElements(GL_TRIANGLES, mboarder_numind, GL_UNSIGNED_SHORT, 0);
}
else if(x == 2)
{
glBindBuffer(GL_ARRAY_BUFFER, mdlRboarder.vid);
glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(position_id);
glBindBuffer(GL_ARRAY_BUFFER, mdlRboarder.nid);
glVertexAttribPointer(normal_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(normal_id);
glBindBuffer(GL_ARRAY_BUFFER, mdlRboarder.cid);
glVertexAttribPointer(color_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(color_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlRboarder.iid);
glDrawElements(GL_TRIANGLES, rboarder_numind, GL_UNSIGNED_SHORT, 0);
}
if(po != 1.0f)
glDisable(GL_BLEND);
}
//*************************************
// update & render
//*************************************
void main_loop()
{
//*************************************
// time delta for interpolation
//*************************************
static double lt = 0;
dt = t-lt;
lt = t;
//*************************************
// camera
//*************************************
mIdent(&view);
mTranslate(&view, 0.f, -2.f, camdist);
//*************************************
// begin render
//*************************************
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//*************************************
// main render
//*************************************
if(state == 0) // next tunnel
{
// new slick array
max_slicks = esRand(0, ARRAY_MAX);
const uint empty_lane = esRand(0, 2);
for(uint i = 0; i < max_slicks; i++)
{
array_slick[i].z = esRandFloat(-41.f, -70.f);
array_slick[i].x = esRand(0, 3);
if(array_slick[i].x == empty_lane)
array_slick[i].s = 0;
else
array_slick[i].s = 1;
}
// new gem array
max_gems = esRand(0, ARRAY_MAX);
for(uint i = 0; i < max_gems; i++)
{
array_gem[i].z = esRandFloat(-11.f, -70.f);
array_gem[i].x = esRand(0, 3);
array_gem[i].s = 1;
}
// new boost array
max_boosts = esRand(0, ARRAY_MAX/2);
for(uint i = 0; i < max_boosts; i++)
{
array_boost[i].z = esRandFloat(-11.f, -70.f);
array_boost[i].x = esRand(0, 3);
array_boost[i].s = 1;
}
// reset vars
pz = 0;
pr = 0.f;
// transition tunnel colours
vec cn;
cn.x = esRandFloat(0.f, 1.f);
cn.y = esRandFloat(0.f, 1.f);
cn.z = esRandFloat(0.f, 1.f);
const uint mi = tunnel_numvert*3;
for(uint i = 0; i < mi; i += 3)
{
if(tunnel_colors[i] == c1.x && tunnel_colors[i+1] == c1.y && tunnel_colors[i+2] == c1.z)
{
tunnel_colors[i] = c2.x;
tunnel_colors[i+1] = c2.y;
tunnel_colors[i+2] = c2.z;
}
else if(tunnel_colors[i] == c2.x && tunnel_colors[i+1] == c2.y && tunnel_colors[i+2] == c2.z)
{
tunnel_colors[i] = c3.x;
tunnel_colors[i+1] = c3.y;
tunnel_colors[i+2] = c3.z;
}
else if(tunnel_colors[i] == c3.x && tunnel_colors[i+1] == c3.y && tunnel_colors[i+2] == c3.z)
{
tunnel_colors[i] = cn.x;
tunnel_colors[i+1] = cn.y;
tunnel_colors[i+2] = cn.z;
}
}
c1.x = c2.x;
c1.y = c2.y;
c1.z = c2.z;
c2.x = c3.x;
c2.y = c3.y;
c2.z = c3.z;
c3.x = cn.x;
c3.y = cn.y;
c3.z = cn.z;
glBindBuffer(GL_ARRAY_BUFFER, mdlTunnel.cid);
glBufferData(GL_ARRAY_BUFFER, sizeof(tunnel_colors), tunnel_colors, GL_STATIC_DRAW);
// reposition next tunnel angle
const f32 r1 = esRandFloat(-6.f, 6.f);
const f32 r2 = esRandFloat(-91.f, -130.f);
for(uint i = 0; i < mi; i += 3)
{
if(tunnel_vertices[i+2] < -91.f)
{
tunnel_vertices[i] += -12.f * sinf(r1);
tunnel_vertices[i+1] += -12.f * cosf(r1);
tunnel_vertices[i+2] = r2;
}
}
glBindBuffer(GL_ARRAY_BUFFER, mdlTunnel.vid);
glBufferData(GL_ARRAY_BUFFER, sizeof(tunnel_vertices), tunnel_vertices, GL_STATIC_DRAW);
// next state
state = 1;
// log
char strts[16];
timestamp(&strts[0]);
printf("[%s] New Tunnel\n", strts);
}
else if(state == 1) // simulate game
{
// render slicks
for(uint i = 0; i < max_slicks; i++)
{
if(array_slick[i].s == 1)
{
rSlick(array_slick[i].z, array_slick[i].x);
if(player_x == array_slick[i].x && pz < array_slick[i].z+0.75f && pz > array_slick[i].z-0.75f)
{
rt = t+3;
state = 2;
array_slick[i].s = 0;
char strts[16];
timestamp(&strts[0]);
printf("[%s] Slick: %.2f %u %u\n", strts, array_slick[i].z, array_slick[i].x, array_slick[i].s);
printf("[%s] >>> YOU DIED, SCORE %u <<<\n", strts, score);
}
}
}
// render gems
for(uint i = 0; i < max_gems; i++)
{
if(array_gem[i].s == 1)
{
rGem(array_gem[i].z, array_gem[i].x);
if(player_x == array_gem[i].x && pz < array_gem[i].z+0.3f && pz > array_gem[i].z-0.3f)
{
array_gem[i].s = 0;
score++;
po = 0.3f;
char title[256];
sprintf(title, "Snowboarder - SCORE %u", score);
glfwSetWindowTitle(window, title);
char strts[16];
timestamp(&strts[0]);
printf("[%s] Gem: %.2f %u %u\n", strts, array_gem[i].z, array_gem[i].x, array_gem[i].s);
}
}
}
// render boosts
for(uint i = 0; i < max_boosts; i++)
{
rBoost(array_boost[i].z, array_boost[i].x);
if(array_boost[i].s == 1 && player_x == array_boost[i].x && pz < array_boost[i].z+1.6f && pz > array_boost[i].z-1.6f)
{
ps *= 2.3f;
array_boost[i].s = 0;
char strts[16];
timestamp(&strts[0]);
printf("[%s] Boost: %.2f %u %u\n", strts, array_boost[i].z, array_boost[i].x, array_boost[i].s);
}
}
// move player
pz += ps * dt;
if(ps < MINSPEED)
ps += DRAGRATE * dt;
else
ps = MINSPEED;
if(ps < MAXSPEED)
ps = MAXSPEED;
camdist = -pz + -5.0f;
rBoarder(pz, player_x, pr);
// reset
if(pz < -80.f)
state = 0;
}
else if(state == 2) // you died
{
pr += 1.2f * dt;
rBoarder(pz, 1, pr);
rDead(pz-6.0f);
if(t > rt)
{
// reset game
srand(NEWGAME_SEED);
glfwSetWindowTitle(window, "Snowboarder");
ps = MINSPEED;
score = 0;
state = 3;
rt = t + 3;
pz = 0.f;
camdist = -5.0f;
char strts[16];
timestamp(&strts[0]);
printf("[%s] *** New Game\n", strts);
}
}
else if(state == 3) // round count down
{
static double lt = 0;
static f32 z = 0.f;
const double ft = floor(rt-t);
if(lt != ft)
{
z = 0.f;
lt = ft;
}
z -= 6.0f * dt;
rCountDown(z, rt-t+1);
if(t > rt)
state = 0;
}
// render tunnel
rTunnel();
//*************************************
// swap buffers / display render
//*************************************
glfwSwapBuffers(window);
}
//*************************************
// Input Handelling
//*************************************
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
// control
if(action == GLFW_PRESS)
{
if(key == GLFW_KEY_LEFT){ keystate[0] = 1; player_x = 0; }
else if(key == GLFW_KEY_RIGHT){ keystate[1] = 1; player_x = 2; }
else if(key == GLFW_KEY_R) // random seed
{
const unsigned int nr = time(0);
srand(nr);
char strts[16];
timestamp(&strts[0]);
printf("[%s] New Random Seed: %u\n", strts, nr);
}
}
else if(action == GLFW_RELEASE)
{
if(key == GLFW_KEY_LEFT){ keystate[0] = 0; }
else if(key == GLFW_KEY_RIGHT){ keystate[1] = 0; }
if(keystate[0] == 1){ player_x = 0; }
else if(keystate[1] == 1){ player_x = 2; }
else { player_x = 1; }
}
// quit
if(key == GLFW_KEY_ESCAPE)
glfwSetWindowShouldClose(window, GLFW_TRUE);
}
void window_size_callback(GLFWwindow* window, int width, int height)
{
winw = width;
winh = height;
glViewport(0, 0, winw, winh);
aspect = (f32)winw / (f32)winh;
ww = winw;
wh = winh;
ww2 = ww/2;
wh2 = wh/2;
uw = (double)aspect / ww;
uh = 1 / wh;
uw2 = (double)aspect / ww2;
uh2 = 1 / wh2;
mIdent(&projection);
mPerspective(&projection, 60.0f, aspect, 1.0f, 160.0f);
}
//*************************************
// Process Entry Point
//*************************************
int main(int argc, char** argv)
{
// help
printf("Snowboarder, A simple snowboarding game.\n");
printf("James William Fletcher (github.com/mrbid)\n\n");
printf("Use your keyboard arrow keys to move from left to right.\n\n");
// init glfw
if(!glfwInit()){exit(EXIT_FAILURE);}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
glfwWindowHint(GLFW_SAMPLES, 16);
window = glfwCreateWindow(winw, winh, "Snowboarder", NULL, NULL);
if(!window)
{
glfwTerminate();
exit(EXIT_FAILURE);
}
const GLFWvidmode* desktop = glfwGetVideoMode(glfwGetPrimaryMonitor());
glfwSetWindowPos(window, (desktop->width/2)-(winw/2), (desktop->height/2)-(winh/2)); // center window on desktop
glfwSetWindowSizeCallback(window, window_size_callback);
glfwSetKeyCallback(window, key_callback);
glfwMakeContextCurrent(window);
gladLoadGL(glfwGetProcAddress);
glfwSwapInterval(1); // 0 for immediate updates, 1 for updates synchronized with the vertical retrace, -1 for adaptive vsync
// set icon
unsigned char* ipd = (unsigned char*)&icon_image2.pixel_data;
srand(time(0));
const uint r = esRand(0, 2);
if(r == 0) {ipd = (unsigned char*)&icon_image.pixel_data;}
else if(r == 1) {ipd = (unsigned char*)&icon_image1.pixel_data;}
else {ipd = (unsigned char*)&icon_image2.pixel_data;}
glfwSetWindowIcon(window, 1, &(GLFWimage){16, 16, ipd});
// seed random
srand(NEWGAME_SEED);
//*************************************
// projection
//*************************************
window_size_callback(window, winw, winh);
//*************************************
// bind vertex and index buffers
//*************************************
// ***** BIND TUNNEL *****
esBind(GL_ARRAY_BUFFER, &mdlTunnel.vid, tunnel_vertices, sizeof(tunnel_vertices), GL_STATIC_DRAW);
esBind(GL_ARRAY_BUFFER, &mdlTunnel.nid, tunnel_normals, sizeof(tunnel_normals), GL_STATIC_DRAW);
esBind(GL_ARRAY_BUFFER, &mdlTunnel.cid, tunnel_colors, sizeof(tunnel_colors), GL_STATIC_DRAW);
esBind(GL_ELEMENT_ARRAY_BUFFER, &mdlTunnel.iid, tunnel_indices, sizeof(tunnel_indices), GL_STATIC_DRAW);
// ***** BIND DEAD *****
esBind(GL_ARRAY_BUFFER, &mdlDead.vid, dead_vertices, sizeof(dead_vertices), GL_STATIC_DRAW);
esBind(GL_ARRAY_BUFFER, &mdlDead.nid, dead_normals, sizeof(dead_normals), GL_STATIC_DRAW);
esBind(GL_ARRAY_BUFFER, &mdlDead.cid, dead_colors, sizeof(dead_colors), GL_STATIC_DRAW);
esBind(GL_ELEMENT_ARRAY_BUFFER, &mdlDead.iid, dead_indices, sizeof(dead_indices), GL_STATIC_DRAW);
// ***** BIND THREE *****
esBind(GL_ARRAY_BUFFER, &mdlThree.vid, three_vertices, sizeof(three_vertices), GL_STATIC_DRAW);
esBind(GL_ARRAY_BUFFER, &mdlThree.nid, three_normals, sizeof(three_normals), GL_STATIC_DRAW);
esBind(GL_ARRAY_BUFFER, &mdlThree.cid, three_colors, sizeof(three_colors), GL_STATIC_DRAW);
esBind(GL_ELEMENT_ARRAY_BUFFER, &mdlThree.iid, three_indices, sizeof(three_indices), GL_STATIC_DRAW);
// ***** BIND TWO *****
esBind(GL_ARRAY_BUFFER, &mdlTwo.vid, two_vertices, sizeof(two_vertices), GL_STATIC_DRAW);
esBind(GL_ARRAY_BUFFER, &mdlTwo.nid, two_normals, sizeof(two_normals), GL_STATIC_DRAW);
esBind(GL_ARRAY_BUFFER, &mdlTwo.cid, two_colors, sizeof(two_colors), GL_STATIC_DRAW);
esBind(GL_ELEMENT_ARRAY_BUFFER, &mdlTwo.iid, two_indices, sizeof(two_indices), GL_STATIC_DRAW);
// ***** BIND ONE *****
esBind(GL_ARRAY_BUFFER, &mdlOne.vid, one_vertices, sizeof(one_vertices), GL_STATIC_DRAW);
esBind(GL_ARRAY_BUFFER, &mdlOne.nid, one_normals, sizeof(one_normals), GL_STATIC_DRAW);
esBind(GL_ARRAY_BUFFER, &mdlOne.cid, one_colors, sizeof(one_colors), GL_STATIC_DRAW);
esBind(GL_ELEMENT_ARRAY_BUFFER, &mdlOne.iid, one_indices, sizeof(one_indices), GL_STATIC_DRAW);
// ***** BIND LSLICK *****
esBind(GL_ARRAY_BUFFER, &mdlLslick.vid, lslick_vertices, sizeof(lslick_vertices), GL_STATIC_DRAW);
esBind(GL_ARRAY_BUFFER, &mdlLslick.nid, lslick_normals, sizeof(lslick_normals), GL_STATIC_DRAW);
esBind(GL_ARRAY_BUFFER, &mdlLslick.cid, lslick_colors, sizeof(lslick_colors), GL_STATIC_DRAW);
esBind(GL_ELEMENT_ARRAY_BUFFER, &mdlLslick.iid, lslick_indices, sizeof(lslick_indices), GL_STATIC_DRAW);
// ***** BIND MSLICK *****
esBind(GL_ARRAY_BUFFER, &mdlMslick.vid, mslick_vertices, sizeof(mslick_vertices), GL_STATIC_DRAW);
esBind(GL_ARRAY_BUFFER, &mdlMslick.nid, mslick_normals, sizeof(mslick_normals), GL_STATIC_DRAW);
esBind(GL_ARRAY_BUFFER, &mdlMslick.cid, mslick_colors, sizeof(mslick_colors), GL_STATIC_DRAW);
esBind(GL_ELEMENT_ARRAY_BUFFER, &mdlMslick.iid, mslick_indices, sizeof(mslick_indices), GL_STATIC_DRAW);
// ***** BIND RSLICK *****
esBind(GL_ARRAY_BUFFER, &mdlRslick.vid, rslick_vertices, sizeof(rslick_vertices), GL_STATIC_DRAW);
esBind(GL_ARRAY_BUFFER, &mdlRslick.nid, rslick_normals, sizeof(rslick_normals), GL_STATIC_DRAW);
esBind(GL_ARRAY_BUFFER, &mdlRslick.cid, rslick_colors, sizeof(rslick_colors), GL_STATIC_DRAW);
esBind(GL_ELEMENT_ARRAY_BUFFER, &mdlRslick.iid, rslick_indices, sizeof(rslick_indices), GL_STATIC_DRAW);
// ***** BIND LGEM *****
esBind(GL_ARRAY_BUFFER, &mdlLgem.vid, lgem_vertices, sizeof(lgem_vertices), GL_STATIC_DRAW);
esBind(GL_ARRAY_BUFFER, &mdlLgem.nid, lgem_normals, sizeof(lgem_normals), GL_STATIC_DRAW);
esBind(GL_ARRAY_BUFFER, &mdlLgem.cid, lgem_colors, sizeof(lgem_colors), GL_STATIC_DRAW);
esBind(GL_ELEMENT_ARRAY_BUFFER, &mdlLgem.iid, lgem_indices, sizeof(lgem_indices), GL_STATIC_DRAW);
// ***** BIND MGEM *****
esBind(GL_ARRAY_BUFFER, &mdlMgem.vid, mgem_vertices, sizeof(mgem_vertices), GL_STATIC_DRAW);
esBind(GL_ARRAY_BUFFER, &mdlMgem.nid, mgem_normals, sizeof(mgem_normals), GL_STATIC_DRAW);
esBind(GL_ARRAY_BUFFER, &mdlMgem.cid, mgem_colors, sizeof(mgem_colors), GL_STATIC_DRAW);
esBind(GL_ELEMENT_ARRAY_BUFFER, &mdlMgem.iid, mgem_indices, sizeof(mgem_indices), GL_STATIC_DRAW);