-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
1171 lines (1048 loc) ยท 40.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 ( notabug.org/Vandarin )
December 2023 ( github.com/mrbid )
Save your garden friends from the ghosts!
*/
#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
#include "inc/gl.h"
#define GLFW_INCLUDE_NONE
#include "inc/glfw3.h"
#include "inc/esAux4.h"
#include "inc/res.h"
#include "assets/intro.h"
#include "assets/terrain.h"
#include "assets/club.h"
#include "assets/ghost.h"
#include "assets/octo.h"
#include "assets/baby.h"
#include "assets/bones.h"
#include "assets/bunny.h"
#include "assets/cat.h"
#include "assets/ginger.h"
#include "assets/farmer.h"
#include "assets/foxy.h"
#include "assets/frog.h"
#include "assets/rat.h"
#include "assets/shroom.h"
#include "assets/robot.h"
#include "assets/teddy.h"
#include "assets/treeman.h"
#include "assets/dragon.h"
#include "assets/banana.h"
#ifdef QUALITY_MEDIUM
#include "assets/scene_d1.h"
#elif QUALITY_LOW
#include "assets/scene_d2.h"
#else
#include "assets/scene.h"
#endif
//*************************************
// globals
//*************************************
const char appTitle[]="โ๏ธ AI Generated Game โ๏ธ (Meshy.ai) ๐ค";
GLFWwindow* window;
uint winw=1024, winh=768;
float t=0.f, dt=0.f, lt=0.f, ltut=0.f, st=0.f, fc=0.f, lfct=0.f, aspect;
double mx,my,lx,ly,ww,wh;
// render state id's
GLint projection_id;
GLint modelview_id;
GLint position_id;
GLint normal_id;
GLint color_id;
GLint lightpos_id;
GLint opacity_id;
// render state
mat projection, view, model, modelview;
// models
ESModel mdlIntro;
ESModel mdlTerrain;
ESModel mdlScene; // duplicating mesh into a scene like this makes the file bigger at a disproportionate ratio to making the renderer more efficient, but it was just faster to do this.
ESModel mdlClub;
ESModel mdlGhost;
ESModel mdlOcto;
ESModel mdlBaby;
ESModel mdlBones;
ESModel mdlBunny;
ESModel mdlCat;
ESModel mdlGinger;
ESModel mdlFarmer;
ESModel mdlFoxy;
ESModel mdlFrog;
ESModel mdlRat;
ESModel mdlShroom;
ESModel mdlRobot;
ESModel mdlTeddy;
ESModel mdlTreeman;
ESModel mdlDragon;
ESModel mdlBanana;
// positions
const vec baby_pos = (vec){ 0.31471309065818787, -0.18127848207950592, -1.588475424796343e-05 };
const vec bones_pos = (vec){ 0.9441254138946533 , -4.923678398132324 , -1.8969178199768066e-05 };
const vec bunny_pos = (vec){ -0.1463201642036438 , -3.3715295791625977 , -1.341104507446289e-06 };
const vec cat_pos = (vec){ 2.409440517425537 , -0.08113411068916321, 0.0006199628114700317 };
const vec ginger_pos = (vec){ -3.5621275901794434 , 1.3744210004806519 , -4.3662264943122864e-05 };
const vec farmer_pos = (vec){ -5.638768672943115 , -2.3814315795898438 , -5.668585072271526e-05 };
const vec foxy_pos = (vec){ 0.3325018882751465 , 3.407064437866211 , -2.3096799850463867e-06 };
const vec frog_pos = (vec){ 2.815049886703491 , -2.359147787094116 , -0.0002002716064453125 };
const vec rat_pos = (vec){ -1.9573389291763306 , -4.990877628326416 , -5.9857964515686035e-05 };
const vec shroom_pos = (vec){ 2.9257962703704834 , 3.9158010482788086 , 6.631016731262207e-06 };
const vec robot_pos = (vec){ -3.2104883193969727 , 0 , -2.0265579223632812e-06 };
const vec teddy_pos = (vec){ -1.148323655128479 , -1.155029535293579 , -8.046627044677734e-06 };
const vec treeman_pos = (vec){ 3.7293591499328613 , 0.10551458597183228, -2.2292137145996094e-05 };
const vec dragon_pos = (vec){ -5.303407669067383 , -2.8256068229675293 , -9.742379188537598e-05 };
const vec banana_pos = (vec){ 3.863978624343872 , -3.625526189804077 , -2.092123031616211e-05 };
vec friend_pos[15];
// game vars
#define FAR_DISTANCE 128.f
#define NEWGAME_SEED 1337
char tts[32];
uint keystate[5] = {0};
// camera vars
uint focus_cursor = 0;
double sens = 0.003f;
float xrot = 0.f;
float yrot = d2PI;
vec look_dir, lookx, looky;
// player vars
float MOVE_SPEED = 1.6f;
vec pp;
uint bonking = 0.f;
// ghosts
#define MAX_GHOSTS 32
vec ghost_pos[MAX_GHOSTS];
vec ghost_dir[MAX_GHOSTS];
int ghost_tgt[MAX_GHOSTS];
float ghost_opa[MAX_GHOSTS];
// octo
vec octo_pos;
vec octo_dir;
int octo_tgt;
// stats
uint ghostsbonked = 0;
uint friendsalive = 15;
//*************************************
// utility functions
//*************************************
void timestamp(char* ts)
{
const time_t tt = time(0);
strftime(ts, 16, "%H:%M:%S", localtime(&tt));
}
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->vid);
glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(position_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdl->iid);
}
void modelBind3(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);
}
//*************************************
// game functions
//*************************************
void retargetGhost(uint i)
{
ghost_tgt[i] = esRand(0, 14);
if(friend_pos[ghost_tgt[i]].z <= -0.74f)
{
uint fail = 1;
for(uint j = 0; j < 15; j++)
{
if(friend_pos[j].z > -0.74f)
{
ghost_tgt[i] = j;
fail = 0;
break;
}
}
if(fail == 1)
{
ghost_tgt[i] = -1;
return;
}
}
ghost_dir[i] = friend_pos[ghost_tgt[i]];
vSub(&ghost_dir[i], ghost_dir[i], ghost_pos[i]);
ghost_dir[i].z = 0.f;
vNorm(&ghost_dir[i]);
}
void resetGhost(uint i)
{
const float radius = esRandFloat(20.f, 42.f);
const float angle = esRandFloat(-PI, PI);
ghost_pos[i] = (vec){radius*cosf(angle), radius*sinf(angle), -1.f};
retargetGhost(i);
ghost_opa[i] = 1.f;
}
void newGame(unsigned int seed)
{
srand(seed);
xrot = 0.f;
yrot = d2PI;
pp = (vec){0.f, 1.f, 0.f};
octo_pos = (vec){3.f, 1.f, 0.f};
octo_dir = (vec){0.f, -1.f, 0.f};
octo_tgt = -1;
lt = 0.f;
ltut = 0.f;
for(uint i = 0; i < 15; i++)
friend_pos[i].z = 0.f;
for(uint i = 0; i < MAX_GHOSTS; i++)
resetGhost(i);
ghostsbonked = 0;
friendsalive = 15;
char strts[16];
timestamp(&strts[0]);
printf("[%s] ๐ Game Start ๐ฌ [%u] ๐๐ซง๐๐ฅ๐ฆ๐.\n", strts, seed);
glfwSetWindowTitle(window, appTitle);
glfwSetTime(0.0);
}
void updateTitle()
{
if(t < 8.f){return;}
friendsalive = 0;
for(uint i = 0; i < 15; i++)
{
if(friend_pos[i].z > -0.369f){friendsalive++;}
}
char tmp[256];
sprintf(tmp, "๐ซ %u - ๐ป %u", friendsalive, ghostsbonked);
glfwSetWindowTitle(window, tmp);
}
//*************************************
// update & render
//*************************************
void main_loop()
{
//*************************************
// core logic
//*************************************
fc++;
glfwPollEvents();
t = (float)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
//*************************************
// game logic
//*************************************
// update title every second
if(t > ltut)
{
updateTitle();
ltut = t + 1.0f;
}
// forward & backward
if(keystate[2] == 1)
{
vec m;
vMulS(&m, look_dir, MOVE_SPEED * dt);
vSub(&pp, pp, m);
}
else if(keystate[3] == 1)
{
vec m;
vMulS(&m, look_dir, MOVE_SPEED * dt);
vAdd(&pp, pp, m);
}
// strafe left & right
if(keystate[0] == 1)
{
mGetViewX(&lookx, view);
vec m;
vMulS(&m, lookx, MOVE_SPEED * dt);
vSub(&pp, pp, m);
}
else if(keystate[1] == 1)
{
mGetViewX(&lookx, view);
vec m;
vMulS(&m, lookx, MOVE_SPEED * dt);
vAdd(&pp, pp, m);
}
// sprint
if(keystate[4] == 1)
{
MOVE_SPEED = 4.20f;
}
else
{
MOVE_SPEED = 1.60f;
}
// lock to floor
pp.z = -0.26f;
//*************************************
// camera
//*************************************
if(focus_cursor == 1)
{
glfwGetCursorPos(window, &mx, &my);
xrot += (lx-mx)*sens;
yrot += (ly-my)*sens;
if(yrot > 2.5f)
yrot = 2.5f;
if(yrot < 0.55f)
yrot = 0.55f;
lx = mx;
ly = my;
}
mIdent(&view);
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);
// get look dir/axes
mGetViewZ(&look_dir, view);
mGetViewX(&lookx, view);
mGetViewY(&looky, view);
//*************************************
// render
//*************************************
// clear buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
///
// shader program change
shadeFullbright(&position_id, &projection_id, &modelview_id, &color_id, &opacity_id);
glUniformMatrix4fv(projection_id, 1, GL_FALSE, (float*)&projection.m[0][0]);
// render terrain
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (float*)&view.m[0][0]);
modelBind(&mdlTerrain);
glUniform3f(color_id, 1.f, 1.f, 1.f);
glDrawElements(GL_TRIANGLES, terrain_numind, GL_UNSIGNED_BYTE, 0);
///
// shader program change
shadeLambert3(&position_id, &projection_id, &modelview_id, &lightpos_id, &normal_id, &color_id, &opacity_id);
glUniformMatrix4fv(projection_id, 1, GL_FALSE, (float*)&projection.m[0][0]);
// render scene
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (float*)&view.m[0][0]);
modelBind3(&mdlScene);
glDrawElements(GL_TRIANGLES, scene_numind, GL_UNSIGNED_INT, 0);
// baby
mIdent(&model);
mSetPos(&model, friend_pos[0]);
mMul(&modelview, &model, &view);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (float*)&modelview.m[0][0]);
modelBind3(&mdlBaby);
glDrawElements(GL_TRIANGLES, baby_numind, GL_UNSIGNED_SHORT, 0);
// bones
mIdent(&model);
mSetPos(&model, friend_pos[1]);
mMul(&modelview, &model, &view);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (float*)&modelview.m[0][0]);
modelBind3(&mdlBones);
glDrawElements(GL_TRIANGLES, bones_numind, GL_UNSIGNED_SHORT, 0);
// bunny
mIdent(&model);
mSetPos(&model, friend_pos[2]);
mMul(&modelview, &model, &view);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (float*)&modelview.m[0][0]);
modelBind3(&mdlBunny);
glDrawElements(GL_TRIANGLES, bunny_numind, GL_UNSIGNED_SHORT, 0);
// cat
mIdent(&model);
mSetPos(&model, friend_pos[3]);
mMul(&modelview, &model, &view);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (float*)&modelview.m[0][0]);
modelBind3(&mdlCat);
glDrawElements(GL_TRIANGLES, cat_numind, GL_UNSIGNED_SHORT, 0);
// ginger
mIdent(&model);
mSetPos(&model, friend_pos[4]);
mMul(&modelview, &model, &view);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (float*)&modelview.m[0][0]);
modelBind3(&mdlGinger);
glDrawElements(GL_TRIANGLES, ginger_numind, GL_UNSIGNED_SHORT, 0);
// farmer
mIdent(&model);
mSetPos(&model, friend_pos[5]);
mMul(&modelview, &model, &view);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (float*)&modelview.m[0][0]);
modelBind3(&mdlFarmer);
glDrawElements(GL_TRIANGLES, farmer_numind, GL_UNSIGNED_SHORT, 0);
// foxy
mIdent(&model);
mSetPos(&model, friend_pos[6]);
mMul(&modelview, &model, &view);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (float*)&modelview.m[0][0]);
modelBind3(&mdlFoxy);
glDrawElements(GL_TRIANGLES, foxy_numind, GL_UNSIGNED_SHORT, 0);
// frog
mIdent(&model);
mSetPos(&model, friend_pos[7]);
mMul(&modelview, &model, &view);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (float*)&modelview.m[0][0]);
modelBind3(&mdlFrog);
glDrawElements(GL_TRIANGLES, frog_numind, GL_UNSIGNED_SHORT, 0);
// rat
mIdent(&model);
mSetPos(&model, friend_pos[8]);
mMul(&modelview, &model, &view);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (float*)&modelview.m[0][0]);
modelBind3(&mdlRat);
glDrawElements(GL_TRIANGLES, rat_numind, GL_UNSIGNED_SHORT, 0);
// shroom
mIdent(&model);
mSetPos(&model, friend_pos[9]);
mMul(&modelview, &model, &view);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (float*)&modelview.m[0][0]);
modelBind3(&mdlShroom);
glDrawElements(GL_TRIANGLES, shroom_numind, GL_UNSIGNED_SHORT, 0);
// robot
mIdent(&model);
mSetPos(&model, friend_pos[10]);
mMul(&modelview, &model, &view);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (float*)&modelview.m[0][0]);
modelBind3(&mdlRobot);
glDrawElements(GL_TRIANGLES, robot_numind, GL_UNSIGNED_SHORT, 0);
// teddy
mIdent(&model);
mSetPos(&model, friend_pos[11]);
mMul(&modelview, &model, &view);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (float*)&modelview.m[0][0]);
modelBind3(&mdlTeddy);
glDrawElements(GL_TRIANGLES, teddy_numind, GL_UNSIGNED_SHORT, 0);
// treeman
mIdent(&model);
mSetPos(&model, friend_pos[12]);
mMul(&modelview, &model, &view);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (float*)&modelview.m[0][0]);
modelBind3(&mdlTreeman);
glDrawElements(GL_TRIANGLES, treeman_numind, GL_UNSIGNED_SHORT, 0);
// dragon
mIdent(&model);
mSetPos(&model, friend_pos[13]);
mMul(&modelview, &model, &view);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (float*)&modelview.m[0][0]);
modelBind3(&mdlDragon);
glDrawElements(GL_TRIANGLES, dragon_numind, GL_UNSIGNED_SHORT, 0);
// banana
mIdent(&model);
mSetPos(&model, friend_pos[14]);
mMul(&modelview, &model, &view);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (float*)&modelview.m[0][0]);
modelBind3(&mdlBanana);
glDrawElements(GL_TRIANGLES, banana_numind, GL_UNSIGNED_SHORT, 0);
// octo
mIdent(&model);
mSetDir(&model, octo_dir);
mSetPos(&model, octo_pos);
mMul(&modelview, &model, &view);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (float*)&modelview.m[0][0]);
modelBind3(&mdlOcto);
glDrawElements(GL_TRIANGLES, octo_numind, GL_UNSIGNED_SHORT, 0);
if(octo_tgt != -1)
{
const float xm = (friend_pos[octo_tgt].x - octo_pos.x);
const float ym = (friend_pos[octo_tgt].y - octo_pos.y);
if(xm*xm + ym*ym > 0.0001f)
{
vec inc = friend_pos[octo_tgt];
vSub(&inc, inc, octo_pos);
inc.z = 0.f;
vNorm(&inc);
octo_dir = inc;
vMulS(&inc, inc, 0.88888888f*dt);
vAdd(&octo_pos, octo_pos, inc);
}
else if(friend_pos[octo_tgt].z < 0.f)
{
friend_pos[octo_tgt].z += 0.15f*dt;
}
else
{
octo_tgt = -1;
updateTitle();
}
}
else
{
uint fail = 1;
for(uint i = 0; i < 15; i++)
{
if(friend_pos[i].z < -0.001f) // epsilon
{
octo_tgt = i;
fail = 0;
break;
}
}
if(fail == 1){octo_tgt = -1;}
}
// ghosts
for(uint i = 0; i < MAX_GHOSTS; i++)
{
// send ghost to another dimension? GHOST BUSTERS!?
if(ghost_tgt[i] == -1)
{
if(ghost_opa[i] > 0.f)
{
ghost_opa[i] -= 0.42f * dt;
}
else
{
resetGhost(i);
continue;
}
continue;
}
// target dead?
if(friend_pos[ghost_tgt[i]].z <= -0.74f)
retargetGhost(i); //ghost_tgt[i] = -1;
// rise from the grave
if(ghost_pos[i].z < -1.f){ghost_pos[i].z = -1.f;} // this is a quick fix to [1]
if(ghost_pos[i].z < 0.f)
{
ghost_pos[i].z += 0.24f*dt; // [1] some how causes z to go way below -1 at times
}
else
{
const float xm = (-pp.x - ghost_pos[i].x);
const float ym = (-pp.y - ghost_pos[i].y);
const float xm1 = (friend_pos[ghost_tgt[i]].x - ghost_pos[i].x);
const float ym1 = (friend_pos[ghost_tgt[i]].y - ghost_pos[i].y);
const float fgd = xm1*xm1 + ym1*ym1;
if(xm*xm + ym*ym > 0.17f && fgd > 0.1f)
{
vec inc = ghost_dir[i];
vMulS(&inc, ghost_dir[i], 0.3f*dt);
vAdd(&ghost_pos[i], ghost_pos[i], inc);
}
else if(fgd < 0.1f)
{
if(friend_pos[ghost_tgt[i]].z > -0.74f)
{
friend_pos[ghost_tgt[i]].z -= 0.03f*dt;
}
else
{
//ghost_tgt[i] = -1;
retargetGhost(i);
updateTitle();
}
}
}
// render
mIdent(&model);
mSetDir(&model, ghost_dir[i]);
mSetPos(&model, ghost_pos[i]);
mMul(&modelview, &model, &view);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (float*)&modelview.m[0][0]);
modelBind3(&mdlGhost);
glDrawElements(GL_TRIANGLES, ghost_numind, GL_UNSIGNED_SHORT, 0);
}
// do the transparent ghosts now
for(uint i = 0; i < MAX_GHOSTS; i++)
{
if(ghost_tgt[i] == -1)
{
if(ghost_opa[i] > 0.f)
{
ghost_opa[i] -= 0.42f * dt;
}
else
{
resetGhost(i);
continue;
}
mIdent(&model);
mSetDir(&model, ghost_dir[i]);
mSetPos(&model, ghost_pos[i]);
mMul(&modelview, &model, &view);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (float*)&modelview.m[0][0]);
modelBind3(&mdlGhost);
glUniform1f(opacity_id, ghost_opa[i]);
glEnable(GL_BLEND);
glEnable(GL_CULL_FACE);
glDrawElements(GL_TRIANGLES, ghost_numind, GL_UNSIGNED_SHORT, 0);
glDisable(GL_CULL_FACE);
glDisable(GL_BLEND);
}
}
///
// render bonking tool
mIdent(&model);
vec ld = look_dir;
ld.z = 0.f;
vMulS(&ld, ld, 0.08f); // far from camera
vec np = (vec){-pp.x, -pp.y, -pp.z};
vAdd(&np, np, ld);
// x offset
vec vx = lookx;
vMulS(&vx, vx, -0.08f);
vAdd(&np, np, vx);
// y offset
vec vy = looky;
vMulS(&vy, vy, 0.0788f); // 0.0388f or 0.0888f
vAdd(&np, np, vy);
// rotate to camera direction with slight offset and set pos
mScale(&model, 0.6f, 0.6f, 0.6f);
mRotZ(&model, -(xrot-PI));
if(bonking == 1){mRotY(&model, -0.6f); mRotX(&model, -0.6f);}
mSetPos(&model, np);
// render it
mMul(&modelview, &model, &view);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (float*)&modelview.m[0][0]);
modelBind3(&mdlClub);
glClear(GL_DEPTH_BUFFER_BIT);
glDrawElements(GL_TRIANGLES, club_numind, GL_UNSIGNED_SHORT, 0);
///
// render intro
if(t < 5.f)
{
// shader program change
shadeFullbright(&position_id, &projection_id, &modelview_id, &color_id, &opacity_id);
glUniformMatrix4fv(projection_id, 1, GL_FALSE, (float*)&projection.m[0][0]);
if(t < 4.f)
{
glUniform1f(opacity_id, 1.f);
}
else
{
glUniform1f(opacity_id, 1.f-(t-4.f));
}
glEnable(GL_BLEND);
{
vec ld = look_dir;
vMulS(&ld, ld, 1.79f);
vec np = (vec){-pp.x, -pp.y, -pp.z};
vAdd(&np, np, ld);
mIdent(&model);
mSetPos(&model, np);
mRotZ(&model, -xrot);
mMul(&modelview, &model, &view);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (float*)&modelview.m[0][0]);
modelBind(&mdlIntro);
glUniform3f(color_id, 0.854f, 0.335f, 1.f);
glDrawElements(GL_TRIANGLES, intro_numind, GL_UNSIGNED_SHORT, 0);
}
{
vec ld = look_dir;
vMulS(&ld, ld, 1.8f);
vec np = (vec){-pp.x, -pp.y, -pp.z};
vAdd(&np, np, ld);
vec vy = looky;
vMulS(&vy, vy, -0.004f);
vAdd(&np, np, vy);
mIdent(&model);
mSetPos(&model, np);
mRotZ(&model, -xrot);
mMul(&modelview, &model, &view);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (float*)&modelview.m[0][0]);
modelBind(&mdlIntro);
glUniform3f(color_id, 0.f, 0.f, 0.f);
glDrawElements(GL_TRIANGLES, intro_numind, GL_UNSIGNED_SHORT, 0);
}
glDisable(GL_BLEND);
}
///
// display render
glfwSwapBuffers(window);
}
//*************************************
// input
//*************************************
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if(action == GLFW_PRESS)
{
if( key == GLFW_KEY_A || key == GLFW_KEY_LEFT) { keystate[0] = 1; }
else if(key == GLFW_KEY_D || key == GLFW_KEY_RIGHT) { keystate[1] = 1; }
else if(key == GLFW_KEY_W || key == GLFW_KEY_UP) { keystate[2] = 1; }
else if(key == GLFW_KEY_S || key == GLFW_KEY_DOWN) { keystate[3] = 1; }
else if(key == GLFW_KEY_LEFT_SHIFT ||
key == GLFW_KEY_RIGHT_CONTROL) { keystate[4] = 1;}
else if(key == GLFW_KEY_N) // new game
{
timeTaken(0);
char strts[16];
timestamp(&strts[0]);
printf("[%s] ๐ฅถ Game End. ๐ฅ ๐\n", strts);
printf("[%s] ๐ซ เฆฌเฆจเงเฆงเงเฆฐเฆพ%u - ๐ป เฆฌเฆจเงเฆง %u\n", strts, friendsalive, ghostsbonked);
printf("[%s] โณ Time-Taken: %s or %g Seconds ๐ซฃ\n\n", strts, tts, t-st);
newGame(time(0));
}
else if(key == GLFW_KEY_ESCAPE) // toggle mouse focus
{
focus_cursor = 0;
#ifndef WEB
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
glfwGetCursorPos(window, &lx, &ly);
#endif
}
else if(key == GLFW_KEY_F) // show average fps
{
if(t-lfct > 2.0)
{
char strts[16];
timestamp(&strts[0]);
printf("[%s] FPS: %g\n", strts, fc/(t-lfct));
lfct = t;
fc = 0;
}
}
}
else if(action == GLFW_RELEASE)
{
if( key == GLFW_KEY_A || key == GLFW_KEY_LEFT) { keystate[0] = 0; }
else if(key == GLFW_KEY_D || key == GLFW_KEY_RIGHT) { keystate[1] = 0; }
else if(key == GLFW_KEY_W || key == GLFW_KEY_UP) { keystate[2] = 0; }
else if(key == GLFW_KEY_S || key == GLFW_KEY_DOWN) { keystate[3] = 0; }
else if(key == GLFW_KEY_LEFT_SHIFT ||
key == GLFW_KEY_RIGHT_CONTROL) { keystate[4] = 0;}
}
}
void mouse_button_callback(GLFWwindow* window, int button, int action, int mods)
{
if(action == GLFW_PRESS)
{
if(focus_cursor == 0)
{
focus_cursor = 1;
#ifndef WEB
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwGetCursorPos(window, &lx, &ly);
#endif
return;
}
if(button == GLFW_MOUSE_BUTTON_LEFT)
{
for(uint i = 0; i < MAX_GHOSTS; i++)
{
const float xm = (-pp.x - ghost_pos[i].x);
const float ym = (-pp.y - ghost_pos[i].y);
if(ghost_pos[i].z >= 0.f && xm*xm + ym*ym < 0.17f && ghost_tgt[i] != -1)
{
ghost_tgt[i] = -1;
bonking = 1;
ghostsbonked++;
updateTitle();
return;
}
}
}
else if(button == GLFW_MOUSE_BUTTON_RIGHT)
{
mIdent(&projection);
mPerspective(&projection, 30.0f, aspect, 0.01f, FAR_DISTANCE);
glUniformMatrix4fv(projection_id, 1, GL_FALSE, (float*)&projection.m[0][0]);
}
}
else if(action == GLFW_RELEASE)
{
if(button == GLFW_MOUSE_BUTTON_LEFT)
{
bonking = 0;
}
else if(button == GLFW_MOUSE_BUTTON_RIGHT)
{
mIdent(&projection);
mPerspective(&projection, 60.0f, aspect, 0.01f, FAR_DISTANCE);
glUniformMatrix4fv(projection_id, 1, GL_FALSE, (float*)&projection.m[0][0]);
}
}
}
void window_size_callback(GLFWwindow* window, int width, int height)
{
winw = width, winh = height;
glViewport(0, 0, winw, winh);
aspect = (float)winw / (float)winh;
ww = winw, wh = winh;
mIdent(&projection);
mPerspective(&projection, 60.0f, aspect, 0.01f, FAR_DISTANCE);
glUniformMatrix4fv(projection_id, 1, GL_FALSE, (float*)&projection.m[0][0]);
}
#ifdef WEB
EM_BOOL emscripten_resize_event(int eventType, const EmscriptenUiEvent *uiEvent, void *userData)
{
winw = uiEvent->documentBodyClientWidth;
winh = uiEvent->documentBodyClientHeight;
window_size_callback(window, winw, winh);
emscripten_set_canvas_element_size("canvas", winw, winh);
return EM_FALSE;
}
#endif
//*************************************
// process entry point
//*************************************
int main(int argc, char** argv)
{
// allow custom msaa level & mouse sensitivity
int msaa = 16;
if(argc >= 2){msaa = atoi(argv[1]);}
if(argc >= 3){sens = atof(argv[2]);}
// help
printf("----\n");
printf("๐ง James ๐ William ๐ Fletcher ๐ง (github.com/mrbid) ๐ง (notabug.org/Vandarin) ๐ท๏ธ\n");
printf("%s - Save ๐พ your ๐ garden ๐งธ friends ๐ซ from the ๐ป ghosts! ๐ฅธ\n", appTitle);
printf("----\n");
#ifndef WEB
printf("๐ค Two command line arguments, msaa 0-16, mouse sensitivity. ๐ซ \n");
printf("e.g; ./aigeneratedgame 16 0.003\n");
printf("----\n");
#endif
printf("๐ ESCAPE = Unlock Mouse ๐\n");
printf("๐ถ W,A,S,D / Arrow Keys = Move ๐\n");
printf("๐ L-SHIFT / R-CTRL = Sprint ๐บ\n");
printf("๐ฐ Left Click = BONK! ๐ฅฐ\n");
printf("๐ญ Right Click = Zoom ๐\n");
printf("----\n");
printf("๐ฆพ Every 3D ๐๐ญ๐คช๐ฅด๐คฏ model โ๏ธ was ๐ generated ๐ต๏ธ by a ๐งฎ tensor operation ๐ฑ, ๐ค machine learning, ๐พ artificial intelligence, โ ๏ธ cyberdyne inc ๐, but a ๐ฟ human ๐งฌ ๐ฝ programmed ๐ชฌ the ๐ธ game ๐น๏ธ and the เฅ lord ๐ created the game concept. ๐ https://meshy.ai โ๏ธ Keep in ๐ง mind I ๐ฝ๏ธ projected ๐ค all the ๐ฟ textures ๐ญ to Vertex ๐จ Colors ๐ง, if I had not ๐
done that ๐คซ the AI ๐งฌ๐ค generated ๐ฏ models would have ๐ looked ๐ a ๐๏ธ lot ๐ better. ๐คฏ Font ๐ซ is ๐ชผ https://www.fontspace.com/slimespooky-font-f84976 ๐ชผ by โจ jadaakbal โจ ([email protected]) ๐จ.\n");
printf("----\n");
printf("๐ Merry Christmas 2023! ๐งโโ๏ธ and a happy new year 2024! ๐
\n");
printf("----\n");
printf("๏ฎฉูจู๏ฎฉ๏ฎฉูจูโก๏ฎฉูจู๏ฎฉ๏ฎฉูจู %s ๐๏ธโค\n", glfwGetVersionString());
printf("----\n");
// init glfw
if(!glfwInit()){printf("glfwInit() failed. ๐คก\n"); exit(EXIT_FAILURE);}
#ifdef WEB
double width, height;
emscripten_get_element_css_size("body", &width, &height);
winw = (uint)width, winh = (uint)height;
#endif
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
glfwWindowHint(GLFW_SAMPLES, msaa);
window = glfwCreateWindow(winw, winh, appTitle, NULL, NULL);
if(!window)
{
printf("glfwCreateWindow() failed. ๐คก\n");
glfwTerminate();
exit(EXIT_FAILURE);
}
const GLFWvidmode* desktop = glfwGetVideoMode(glfwGetPrimaryMonitor());
#ifndef WEB
glfwSetWindowPos(window, (desktop->width/2)-(winw/2), (desktop->height/2)-(winh/2)); // center window on desktop
#endif
if(glfwRawMouseMotionSupported() == GLFW_TRUE){glfwSetInputMode(window, GLFW_RAW_MOUSE_MOTION, GLFW_TRUE);} // raw input, since it's an FPS
glfwSetWindowSizeCallback(window, window_size_callback);
glfwSetKeyCallback(window, key_callback);
glfwSetMouseButtonCallback(window, mouse_button_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
glfwSetWindowIcon(window, 1, &(GLFWimage){16, 16, (unsigned char*)icon_image});
//*************************************
// bind vertex and index buffers
//*************************************
// ***** BIND INTRO *****
esBind(GL_ARRAY_BUFFER, &mdlIntro.vid, intro_vertices, sizeof(intro_vertices), GL_STATIC_DRAW);
esBind(GL_ELEMENT_ARRAY_BUFFER, &mdlIntro.iid, intro_indices, sizeof(intro_indices), GL_STATIC_DRAW);
// ***** BIND TERRAIN *****
esBind(GL_ARRAY_BUFFER, &mdlTerrain.vid, terrain_vertices, sizeof(terrain_vertices), GL_STATIC_DRAW);
esBind(GL_ELEMENT_ARRAY_BUFFER, &mdlTerrain.iid, terrain_indices, sizeof(terrain_indices), GL_STATIC_DRAW);
// ***** BIND SCENE *****
esBind(GL_ARRAY_BUFFER, &mdlScene.vid, scene_vertices, sizeof(scene_vertices), GL_STATIC_DRAW);
esBind(GL_ARRAY_BUFFER, &mdlScene.nid, scene_normals, sizeof(scene_normals), GL_STATIC_DRAW);
esBind(GL_ARRAY_BUFFER, &mdlScene.cid, scene_colors, sizeof(scene_colors), GL_STATIC_DRAW);
esBind(GL_ELEMENT_ARRAY_BUFFER, &mdlScene.iid, scene_indices, sizeof(scene_indices), GL_STATIC_DRAW);
// ***** BIND CLUB *****
esBind(GL_ARRAY_BUFFER, &mdlClub.vid, club_vertices, sizeof(club_vertices), GL_STATIC_DRAW);