-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.c
1420 lines (1266 loc) · 42.5 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)
April 2022 - October 2023
Info:
PoryDrive, a simple 3D driving game where you have to catch Porygon!
Keyboard:
ESCAPE = Unlock Mouse
N = New Game and Car Color
W,A,S,D = Drive Car
Space = Brake
L-Shift = Boost
C = Random Car Colors
R = Accelerate/step DNA color peel
F = FPS to console
P = Player stats to console
O = Toggle auto drive
Mouse:
RIGHT CLICK/MOUSE4 = Zoom Snap Close/Ariel
Scroll = Zoom in/out
Notes:
The predecessors where different;
https://github.com/mrbid/porydrive
https://github.com/PoryDrive/PoryDriveFNN (this was my favorite version)
*/
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#ifdef WEB
#include <emscripten.h>
#include <emscripten/html5.h>
#define GL_GLEXT_PROTOTYPES
#define EGL_EGLEXT_PROTOTYPES
#endif
#define uint GLushort
#define sint GLshort
#define f32 GLfloat
#include "inc/gl.h"
#define GLFW_INCLUDE_NONE
#include "inc/glfw3.h"
#include "inc/esAux4.h"
#include "inc/res.h"
#include "assets/purplecube.h"
#include "assets/porygon.h"
#include "assets/dna.h"
#include "assets/body.h"
#include "assets/windows.h"
#include "assets/wheel.h"
//*************************************
// globals
//*************************************
GLFWwindow* window;
uint winw = 1024, winh = 768;
double t = 0; // time
f32 dt = 0; // delta time
double fc = 0; // frame count
double lfct = 0;// last frame count time
f32 aspect;
double x,y,lx,ly,ww,wh;
// render state id's
GLint projection_id;
GLint modelview_id;
GLint position_id;
GLint lightpos_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
sint bindstate = -1;
sint bindstate2 = -1;
uint keystate[6] = {0};
ESModel mdlPurpleCube;
GLuint mdlBlueCubeColors;
ESModel mdlPorygon;
ESModel mdlDNA;
ESModel mdlBody;
GLuint mdlBodyColors2;
ESModel mdlWindows;
ESModel mdlWheel;
f32 sc1, sc2, sc3, bc1, bc2, bc3;
// game vars
#define FAR_DISTANCE 60.f
#define NEWGAME_SEED 1337
double st=0; // start time
char tts[32];// time taken string
// camera vars
uint focus_cursor = 1;
double sens = 0.003f;
f32 xrot = PI;
f32 yrot = 1.3f;
f32 zoom = -0.3f;
// player vars
f32 pr; // rotation
f32 sr; // steering rotation
vec pp; // position
vec pv; // velocity
vec pd; // wheel direction
vec pbd;// body direction
f32 sp; // speed
uint cp;// collected porygon count
uint cc;// collision count
f32 pc = 0.f;// is player colliding
f32 bs;// boost seconds
f32 bss;// boost start time
// basic autodrive
uint auto_drive=0;
f32 ad_min_dstep = 0.01f;
f32 ad_max_dstep = 0.06f;
f32 ad_min_speedswitch = 2.f;
f32 ad_maxspeed_reductor = 0.5f;
// porygon vars
vec zp; // position
vec zd; // direction
f32 zr; // rotation
f32 zs; // speed
double za;// alive state
f32 zt; // twitch radius
// configurable vars
f32 maxspeed = 0.0265f;
f32 acceleration = 0.0028f;
f32 inertia = 0.0022f;
f32 drag = 0.00038f;
f32 steeringspeed = 0.04f;
f32 steerinertia = 120.f;
f32 minsteer = 0.3f;
f32 maxsteer = 0.36f;
f32 steering_deadzone = 0.033f;
f32 steeringtransfer = 0.023f;
f32 steeringtransferinertia = 280.f;
f32 suspension_pitch = 3.f;
f32 suspension_pitch_limit = 0.06f;
f32 suspension_roll = 30.f;
f32 suspension_roll_limit = 0.16f;
uint sticky_collisions = 0;
char cname[256] = {0};
//*************************************
// utility functions
//*************************************
void timestamp(char* ts)
{
const time_t tt = time(0);
strftime(ts, 16, "%H:%M:%S", localtime(&tt));
}
#ifndef WEB
void loadConfig(uint type)
{
FILE* f = fopen("config.txt", "r");
if(f)
{
sprintf(cname, "config.txt");
if(type == 1)
{
char strts[16];
timestamp(&strts[0]);
printf("[%s] CONFIG: config.txt loaded.\n", strts);
}
else
printf("\nDetected config.txt loading settings...\n");
char line[256];
while(fgets(line, 256, f) != NULL)
{
char set[64];
memset(set, 0, 64);
f32 val;
if(sscanf(line, "%63s %f", set, &val) == 2)
{
if(type == 0)
printf("Setting Loaded: %s %g\n", set, val);
// car physics
if(strcmp(set, "maxspeed") == 0){maxspeed = val;}
if(strcmp(set, "acceleration") == 0){acceleration = val;}
if(strcmp(set, "inertia") == 0){inertia = val;}
if(strcmp(set, "drag") == 0){drag = val;}
if(strcmp(set, "steeringspeed") == 0){steeringspeed = val;}
if(strcmp(set, "steerinertia") == 0){steerinertia = val;}
if(strcmp(set, "minsteer") == 0){minsteer = val;}
if(strcmp(set, "maxsteer") == 0){maxsteer = val;}
if(strcmp(set, "steering_deadzone") == 0){steering_deadzone = val;}
if(strcmp(set, "steeringtransfer") == 0){steeringtransfer = val;}
if(strcmp(set, "steeringtransferinertia") == 0){steeringtransferinertia = val;}
if(strcmp(set, "suspension_pitch") == 0){suspension_pitch = val;}
if(strcmp(set, "suspension_pitch_limit") == 0){suspension_pitch_limit = val;}
if(strcmp(set, "suspension_roll") == 0){suspension_roll = val;}
if(strcmp(set, "suspension_roll_limit") == 0){suspension_roll_limit = val;}
if(strcmp(set, "sticky_collisions") == 0){sticky_collisions = (uint)val;}
// auto drive
if(strcmp(set, "ad_min_dstep") == 0){ad_min_dstep = val;}
if(strcmp(set, "ad_max_dstep") == 0){ad_max_dstep = val;}
if(strcmp(set, "ad_min_speedswitch") == 0){ad_min_speedswitch = val;}
if(strcmp(set, "ad_maxspeed_reductor") == 0){ad_maxspeed_reductor = val;}
}
}
fclose(f);
}
else
{
if(type == 1)
{
char strts[16];
timestamp(&strts[0]);
printf("[%s] CONFIG: No config.txt file detected.\n", strts);
}
}
}
#endif
void timeTaken(uint ss)
{
if(ss == 1)
{
const double tt = t-st;
if(tt < 60.0)
sprintf(tts, "%.0f Sec", tt);
else if(tt < 3600.0)
sprintf(tts, "%.2f Min", tt * 0.016666667);
else if(tt < 216000.0)
sprintf(tts, "%.2f Hr", tt * 0.000277778);
else if(tt < 12960000.0)
sprintf(tts, "%.2f Days", tt * 0.00000463);
}
else
{
const double tt = t-st;
if(tt < 60.0)
sprintf(tts, "%.0f Seconds", tt);
else if(tt < 3600.0)
sprintf(tts, "%.2f Minutes", tt * 0.016666667);
else if(tt < 216000.0)
sprintf(tts, "%.2f Hours", tt * 0.000277778);
else if(tt < 12960000.0)
sprintf(tts, "%.2f Days", tt * 0.00000463);
}
}
//*************************************
// render functions
//*************************************
void modelBind(const ESModel* mdl)
{
glBindBuffer(GL_ARRAY_BUFFER, mdl->cid);
glVertexAttribPointer(color_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(color_id);
glBindBuffer(GL_ARRAY_BUFFER, mdl->vid);
glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(position_id);
glBindBuffer(GL_ARRAY_BUFFER, mdl->nid);
glVertexAttribPointer(normal_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(normal_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdl->iid);
}
#ifndef WEB
void iterBody()
{
static const uint mi = body_numvert*3;
static f32 cd = 1.f;
for(uint i = 0; i < mi; i++)
{
body_colors2[i] += fRandFloat(0.1f, 0.6f) * cd;
if(body_colors2[i] >= 1.f)
cd = -1.f;
else if(body_colors2[i] <= 0.f)
cd = 1.f;
}
esRebind(GL_ARRAY_BUFFER, &mdlBodyColors2, body_colors2, sizeof(body_colors2), GL_STATIC_DRAW);
}
#endif
void iterDNA()
{
static const uint mi = dna_numvert*3;
static f32 cd = 1.f;
for(uint i = 0; i < mi; i++)
{
dna_colors[i] += fRandFloat(0.1f, 0.6f) * cd;
if(dna_colors[i] >= 1.f)
cd = -1.f;
else if(dna_colors[i] <= 0.f)
cd = 1.f;
}
esRebind(GL_ARRAY_BUFFER, &mdlDNA.cid, dna_colors, sizeof(dna_colors), GL_STATIC_DRAW);
#ifndef WEB
iterBody();
#endif
}
void rCube(f32 x, f32 y)
{
mIdent(&model);
mTranslate(&model, x, y, 0.f);
mMul(&modelview, &model, &view);
glUniform1f(opacity_id, 1.0f);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (f32*) &modelview.m[0][0]);
if(bindstate != 1)
{
glBindBuffer(GL_ARRAY_BUFFER, mdlPurpleCube.vid);
glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(position_id);
glBindBuffer(GL_ARRAY_BUFFER, mdlPurpleCube.nid);
glVertexAttribPointer(normal_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(normal_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlPurpleCube.iid);
bindstate = 1;
bindstate2 = -1;
}
// cube collisions
const f32 dlap = vDistLa(zp, (vec){x, y, 0.f}); // porygon
if(dlap < 0.15f)
{
vec nf;
vSub(&nf, zp, (vec){x, y, 0.f});
vNorm(&nf);
vMulS(&nf, nf, 0.15f-dlap);
vAdd(&zp, zp, nf);
}
// if car is moving compute collisions
if(sp > inertia || sp < -inertia)
{
// front collision cube point
vec cp1 = pp;
vec cd1 = pbd;
vMulS(&cd1, cd1, 0.0525f);
vAdd(&cp1, cp1, cd1);
// back collision cube point
vec cp2 = pp;
vec cd2 = pbd;
vMulS(&cd2, cd2, -0.0525f);
vAdd(&cp2, cp2, cd2);
// do Axis-Aligned Cube collisions for points against rCube() being rendered
const f32 dla1 = vDistLa(cp1, (vec){x, y, 0.f}); // front car
const f32 dla0 = vDistLa(pp, (vec){x, y, 0.f}); // center car
const f32 dla2 = vDistLa(cp2, (vec){x, y, 0.f}); // back car
if(dla1 <= 0.097f)
{
vec nf;
vSub(&nf, pp, (vec){x, y, 0.f});
vNorm(&nf);
vMulS(&nf, nf, 0.097f-dla1);
vAdd(&pv, pv, nf);
if(sticky_collisions){sp *= 0.5f;}
}
else if(dla0 <= 0.097f)
{
vec nf;
vSub(&nf, pp, (vec){x, y, 0.f});
vNorm(&nf);
vMulS(&nf, nf, 0.097f-dla0);
vAdd(&pv, pv, nf);
if(sticky_collisions){sp *= 0.5f;}
}
else if(dla2 <= 0.097f)
{
vec nf;
vSub(&nf, pp, (vec){x, y, 0.f});
vNorm(&nf);
vMulS(&nf, nf, 0.097f-dla2);
vAdd(&pv, pv, nf);
if(sticky_collisions){sp *= 0.5f;}
}
}
// check to see if cube needs to be blue
const f32 dla = vDist(pp, (vec){x, y, 0.f}); // worth it to prevent the flicker
// official colliding count
static f32 colliding = 0.f;
if(dla <= 0.13f)
{
if(colliding == 0.f)
{
colliding = x*y+x;
cc++;
// char strts[16];
// timestamp(&strts[0]);
// printf("[%s] Collisions: %u\n", strts, cc);
}
}
else if(x*y+x == colliding)
{
colliding = 0.f;
}
// player colliding
if(dla <= 0.17f)
{
bs += 0.006f; // little boost
if(pc == 0.f)
{
pc = x*y+x;
cc++;
// char strts[16];
// timestamp(&strts[0]);
// printf("[%s] Collisions: %u\n", strts, cc);
}
}
else if(x*y+x == pc)
{
pc = 0.f;
}
const uint collision = (dla < 0.17f || dlap < 0.16f);
if(collision == 1 && bindstate2 <= 1)
{
glBindBuffer(GL_ARRAY_BUFFER, mdlBlueCubeColors);
glVertexAttribPointer(color_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(color_id);
bindstate2 = 2;
}
else if(collision == 0 && bindstate2 != 1)
{
glBindBuffer(GL_ARRAY_BUFFER, mdlPurpleCube.cid);
glVertexAttribPointer(color_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(color_id);
bindstate2 = 1;
}
glDrawElements(GL_TRIANGLES, purplecube_numind, GL_UNSIGNED_SHORT, 0);
}
void rPorygon(f32 x, f32 y, f32 r)
{
bindstate = -1;
mIdent(&model);
mTranslate(&model, x, y, 0.f);
mRotZ(&model, r);
const f32 zss = 4.f + (8.f*(1.f-(zs*111.111111111f)));
if(za != 0.0)
mScale(&model, zss, zss, 0.1f);
else
mScale(&model, zss, zss, zss);
mMul(&modelview, &model, &view);
// returns direction
mGetDirY(&zd, model);
vInv(&zd);
if(za != 0.0)
glUniform1f(opacity_id, (za-t)/6.0);
else
glUniform1f(opacity_id, 1.0f);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (f32*) &modelview.m[0][0]);
modelBind(&mdlPorygon);
if(za != 0.0)
glEnable(GL_BLEND);
glDrawElements(GL_TRIANGLES, porygon_numind, GL_UNSIGNED_BYTE, 0);;
if(za != 0.0)
glDisable(GL_BLEND);
}
void rDNA(f32 x, f32 y, f32 z)
{
static f32 dr = 0.f;
dr += 1.f * dt;
bindstate = -1;
mIdent(&model);
mTranslate(&model, x, y, z);
mRotZ(&model, dr);
mMul(&modelview, &model, &view);
glUniform1f(opacity_id, 1.0f);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (f32*) &modelview.m[0][0]);
modelBind(&mdlDNA);
glDrawElements(GL_TRIANGLES, dna_numind, GL_UNSIGNED_SHORT, 0);
}
void rCar(f32 x, f32 y, f32 z, f32 rx)
{
bindstate = -1;
// opaque
glUniform1f(opacity_id, 1.0f);
// wheel spin speed
static f32 wr = 0.f;
const f32 speed = sp * 33.f;
if(sp > inertia || sp < -inertia)
wr += speed;
// wheel; front left
mIdent(&model);
mTranslate(&model, x, y, z);
mRotZ(&model, -rx);
mTranslate(&model, 0.026343f, -0.054417f, 0.012185f);
mRotZ(&model, sr);
// returns direction
mGetDirY(&pd, model);
vInv(&pd);
//
mRotY(&model, -wr);
mMul(&modelview, &model, &view);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (f32*)&modelview.m[0][0]);
modelBind(&mdlWheel);
glDrawElements(GL_TRIANGLES, wheel_numind, GL_UNSIGNED_SHORT, 0);
// wheel; back left
mIdent(&model);
mTranslate(&model, x, y, z);
mRotZ(&model, -rx);
mTranslate(&model, 0.026343f, 0.045294f, 0.012185f);
mRotY(&model, -wr);
mMul(&modelview, &model, &view);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (f32*)&modelview.m[0][0]);
modelBind(&mdlWheel);
glDrawElements(GL_TRIANGLES, wheel_numind, GL_UNSIGNED_SHORT, 0);
// wheel; front right
mIdent(&model);
mRotZ(&model, PI);
mTranslate(&model, -x, -y, -z);
mRotZ(&model, -rx);
mTranslate(&model, 0.026343f, 0.054417f, 0.012185f);
mRotZ(&model, sr);
mRotY(&model, wr);
mMul(&modelview, &model, &view);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (f32*)&modelview.m[0][0]);
modelBind(&mdlWheel);
glDrawElements(GL_TRIANGLES, wheel_numind, GL_UNSIGNED_SHORT, 0);
// wheel; back right
mIdent(&model);
mRotZ(&model, PI);
mTranslate(&model, -x, -y, -z);
mRotZ(&model, -rx);
mTranslate(&model, 0.026343f, -0.045294f, 0.012185f);
mRotY(&model, wr);
mMul(&modelview, &model, &view);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (f32*)&modelview.m[0][0]);
modelBind(&mdlWheel);
glDrawElements(GL_TRIANGLES, wheel_numind, GL_UNSIGNED_SHORT, 0);
// body & window matrix
mIdent(&model);
mTranslate(&model, x, y, z);
mRotZ(&model, -rx);
// returns direction
mGetDirY(&pbd, model);
vInv(&pbd);
//
f32 sy = sp*suspension_pitch;
if(fabsf(sy) < inertia)
{
sy = 0.f;
}
else
{
if(sy > suspension_pitch_limit){sy = suspension_pitch_limit;}
if(sy < -suspension_pitch_limit){sy = -suspension_pitch_limit;}
}
mRotY(&model, sy);
f32 sx = sr*suspension_roll*sp; // turning suspension
static f32 lsx = 0.f;
if(fabsf(sp) < inertia)
{
sx = lsx;
}
if(sx > suspension_roll_limit){sx = suspension_roll_limit;}
if(sx < -suspension_roll_limit){sx = -suspension_roll_limit;}
lsx = sx;
mRotX(&model, sx);
mMul(&modelview, &model, &view);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (f32*) &modelview.m[0][0]);
// body
modelBind(&mdlBody);
#ifdef WEB
if(pc == 0.f)
{
glDisable(GL_CULL_FACE);
glDrawElements(GL_TRIANGLES, body_numind, GL_UNSIGNED_SHORT, 0);
glEnable(GL_CULL_FACE);
}
#else
if(pc == 0.f && cp > 0)
{
glDisable(GL_CULL_FACE);
glDrawElements(GL_TRIANGLES, body_numind, GL_UNSIGNED_SHORT, 0);
glEnable(GL_CULL_FACE);
}
else
{
if(cp == 0)
{
glDisable(GL_CULL_FACE);
glDrawElements(GL_TRIANGLES, body_numind, GL_UNSIGNED_SHORT, 0);
glEnable(GL_CULL_FACE);
}
else
{
if(cp > 1)
{
glDisable(GL_CULL_FACE);
glDrawElements(GL_TRIANGLES, body_numind, GL_UNSIGNED_SHORT, 0);
glEnable(GL_CULL_FACE);
glBindBuffer(GL_ARRAY_BUFFER, mdlBodyColors2);
glVertexAttribPointer(color_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(color_id);
}
glDisable(GL_DEPTH_TEST);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glDrawElements(GL_TRIANGLES, body_numind, GL_UNSIGNED_SHORT, 0);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glEnable(GL_DEPTH_TEST);
}
}
#endif
// transparent
glUniform1f(opacity_id, 0.3f);
// windows
modelBind(&mdlWindows);
glEnable(GL_BLEND);
glDepthMask(GL_FALSE);
glDrawElements(GL_TRIANGLES, windows_numind, GL_UNSIGNED_SHORT, 0);
glDisable(GL_BLEND);
glDepthMask(GL_TRUE);
}
//*************************************
// game functions
//*************************************
void newGame(unsigned int seed)
{
srand(seed);
srandf(seed);
char strts[16];
timestamp(&strts[0]);
printf("[%s] Game Start [%u].\n", strts, seed);
glfwSetWindowTitle(window, "PoryDrive");
pp = (vec){0.f, 0.f, 0.f};
pv = (vec){0.f, 0.f, 0.f};
pd = (vec){0.f, 0.f, 0.f};
st = t;
cp = 0;
cc = 0;
pr = 0.f;
sr = 0.f;
sp = 0.f;
bs = 1.f;
bss = 0.f;
zp = (vec){esRandFloat(-18.f, 18.f), esRandFloat(-18.f, 18.f), 0.f};
zs = 0.002f;
za = 0.0;
zt = 0.08f;
// random car body colors
if(seed != 1337)
{
const f32 nbc1 = esRandFloat(0.f, 1.f);
const f32 nbc2 = esRandFloat(0.f, 1.f);
const f32 nbc3 = esRandFloat(0.f, 1.f);
const f32 nsc1 = esRandFloat(0.f, 1.f);
const f32 nsc2 = esRandFloat(0.f, 1.f);
const f32 nsc3 = esRandFloat(0.f, 1.f);
const uint maxv = body_numvert*3;
for(uint v = 0; v < maxv; v+=3)
{
if( body_colors[v] == sc1 && // seats
body_colors[v+1] == sc2 &&
body_colors[v+2] == sc3 )
{
body_colors[v] = nsc1;
body_colors[v+1] = nsc2;
body_colors[v+2] = nsc3;
}
if( body_colors[v] == bc1 && // body
body_colors[v+1] == bc2 &&
body_colors[v+2] == bc3 )
{
body_colors[v] = nbc1;
body_colors[v+1] = nbc2;
body_colors[v+2] = nbc3;
}
}
bc1 = nbc1, bc2 = nbc2, bc3 = nbc3;
sc1 = nsc1, sc2 = nsc2, sc3 = nsc3;
esRebind(GL_ARRAY_BUFFER, &mdlBody.cid, body_colors, sizeof(body_colors), GL_STATIC_DRAW);
}
}
//*************************************
// update & render
//*************************************
void main_loop()
{
glfwPollEvents();
fc++;
//*************************************
// time delta for interpolation
//*************************************
static f32 lt = 0;
t = glfwGetTime();
dt = t-lt;
lt = t;
#ifdef WEB
EmscriptenPointerlockChangeEvent e;
if(emscripten_get_pointerlock_status(&e) == EMSCRIPTEN_RESULT_SUCCESS)
{
if(focus_cursor == 0 && e.isActive == 1)
{
glfwGetCursorPos(window, &lx, &ly);
}
focus_cursor = e.isActive;
}
#endif
static f32 lbs = 0.f; // increase boost
if(t > lbs)
{
#ifdef WEB
bs += 0.02f;
#else
bs += 0.01f;
#endif
if(bs > 5.f){bs = 5.f;}
lbs = t+0.1f;
}
//*************************************
// 60fps limited code
//*************************************
static f32 lut = 0.f;
if(t > lut)
{
// keystates
f32 tr = maxsteer * ((maxspeed-sp) * steerinertia);
if(tr < minsteer){tr = minsteer;}
if(keystate[0] == 1)
{
sr -= steeringspeed;
if(sr < -tr){sr += steeringspeed;}
}
if(keystate[1] == 1)
{
sr += steeringspeed;
if(sr > tr){sr -= steeringspeed;}
}
if(keystate[0] == 0 && keystate[1] == 0)
{
if(sr > steering_deadzone)
sr -= steeringspeed;
else if(sr < -steering_deadzone)
sr += steeringspeed;
else
sr = 0.f;
}
if(keystate[4] == 1)
{
sp *= 0.90f;
}
else
{
if(keystate[2] == 1)
{
f32 tacc = acceleration;
if(keystate[5] == 1 && bs > 0.f && bss != 0.f) // boost
{
bs -= t-bss;
bss = t;
tacc *= 14.f;
}
else if(keystate[5] == 1 && bss == 0.f)
{
bss = t;
}
if(bs < 0.f){bs = 0.f;}
//printf("boost: %g\n", bs);
vec inc;
sp += tacc;
vMulS(&inc, pd, tacc);
vAdd(&pv, pv, inc);
}
if(keystate[3] == 1)
{
vec inc;
sp -= acceleration;
vMulS(&inc, pd, -acceleration);
vAdd(&pv, pv, inc);
}
}
// update title bar stats
static double ltut = 3.0;
if(t > ltut)
{
timeTaken(1);
char title[512];
const f32 dsp = fabsf(sp*(1.f/maxspeed)*130.f);
if(cname[0] != 0x00)
sprintf(title, "%u Pory | %s | %.f MPH | %s", cp, tts, dsp, cname);
else
sprintf(title, "%u Pory | %s | %.f MPH", cp, tts, dsp);
glfwSetWindowTitle(window, title);
ltut = t + 1.0;
}
// auto drive
if(auto_drive == 1)
{
vec lad = pp;
vSub(&lad, lad, zp);
vNorm(&lad);
const f32 as = fabsf(vDot(pbd, lad)+1.f) * 0.5f;
static f32 ld = 0.f, td = 1.f;
const f32 d = vDist(pp, zp);
f32 ds = d * 0.01f;
if(ds < ad_min_dstep){ds = ad_min_dstep;}
else if(ds > ad_max_dstep){ds = ad_max_dstep;}
if(fabsf(ld-d) > ds && ld < d){td *= -1.f;}
ld = d;
sr = (tr * as) * td;
if(d < ad_min_speedswitch)
sp = maxspeed * (d*ad_maxspeed_reductor)+0.003f;
else
sp = maxspeed;
}
// simulate car
if(sp > 0.f)
sp -= drag;
else
sp += drag;
if(fabsf(sp) > maxspeed)
{
if(sp > 0.f)
sp = maxspeed;
else
sp = -maxspeed;
}
if(sp > inertia || sp < -inertia)
{
vAdd(&pp, pp, pv);
vMulS(&pv, pd, sp);
pr -= sr * steeringtransfer * (sp*steeringtransferinertia);
}
if(pp.x > 17.5f){pp.x = 17.5f;}
else if(pp.x < -17.5f){pp.x = -17.5f;}
if(pp.y > 17.5f){pp.y = 17.5f;}
else if(pp.y < -17.5f){pp.y = -17.5f;}
// simulate porygon
if(za == 0.0)
{
vec inc;
vMulS(&inc, zd, zs);
vAdd(&zp, zp, inc);
zr += fRandFloat(-zt, zt);
if(zp.x > 17.5f){zp.x = 17.5f; zr = fRandFloat(-PI, PI);}
else if(zp.x < -17.5f){zp.x = -17.5f; zr = fRandFloat(-PI, PI);}
if(zp.y > 17.5f){zp.y = 17.5f; zr = fRandFloat(-PI, PI);}
else if(zp.y < -17.5f){zp.y = -17.5f; zr = fRandFloat(-PI, PI);}
// front collision cube point
vec cp1 = pp;
vec cd1 = pbd;
vMulS(&cd1, cd1, 0.0525f);
vAdd(&cp1, cp1, cd1);
// back collision cube point
vec cp2 = pp;
vec cd2 = pbd;
vMulS(&cd2, cd2, -0.0525f);
vAdd(&cp2, cp2, cd2);
// do Axis-Aligned Cube collisions for both points against porygon
const f32 dla1 = vDistLa(cp1, zp); // front car
const f32 dla2 = vDistLa(cp2, zp); // back car
if(dla1 < 0.14f || dla2 < 0.14f)
{
cp++;
za = t+6.0;
iterDNA();
char strts[16];
timestamp(&strts[0]);
printf("[%s] Porygon collected: %u, collisions: %u\n", strts, cp, cc);
cc = 0;
}
}
else if(t > za)
{
srand(time(0));
zp = (vec){esRandFloat(-18.f, 18.f), esRandFloat(-18.f, 18.f), 0.f};
zs = esRandFloat(0.002f, 0.009f);
zt = esRandFloat(0.08f, 0.16f);
za = 0.0;
}
lut = t + 0.016666667f;
}
//*************************************
// camera
//*************************************
if(focus_cursor == 1)
{
glfwGetCursorPos(window, &x, &y);
xrot += (lx-x)*sens;
yrot += (ly-y)*sens;
if(yrot > 1.5f)
yrot = 1.5f;
if(yrot < 0.5f)
yrot = 0.5f;
lx = x, ly = y;
}
mIdent(&view);
mSetPos(&view, (vec){0.f, -0.033f, zoom});
mRotate(&view, yrot, 1.f, 0.f, 0.f);
mRotate(&view, xrot, 0.f, 0.f, 1.f);
mTranslate(&view, -pp.x, -pp.y, -pp.z);
//*************************************
// begin render
//*************************************
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//*************************************