-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
995 lines (854 loc) · 30.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
/*
James William Fletcher (github.com/mrbid)
December 2021
A simple bowling game with a festive twist.
Niggles:
Speaking English I know how to spell Colour,
however, I also recognise Color is less to type.
So I use Colour and Color interchangeably.
I use Color in var names that will be typed a lot,
such as in low level code, and I use Colour in
higher level code where it will be typed less.
Also ignoring reverse rendering of closest to farthest
objects because the gains are so minimal in this asset
set. Depth buffer culling gains almost non-existant.
I don't think I've missed any other opportunities I
have not listed above. Well I could re-export the mesh
so that I don't need to perform the camera rotations.
Notes:
Looks like the aliased_sin() trick doesn't work
on ARM - causes a buffer overrun and subsequent
segfault. Food for thought, also annoying, because
I only added the optimisation with the intention
of low end hardware like ARM benefiting from it.
*/
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define uint unsigned int
#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 "scene.h"
#include "dynamic.h"
#include "minball.h"
// configuration
// #define LEAK_CHECK
// #define LEAK_CHECK_BRUTE
// #define AUTOMATE
#ifdef LEAK_CHECK
const GLushort mlc = dynamic_numvert*3;
#endif
//*************************************
// 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;
GLint texcoord_id;
GLint sampler_id;
// render state matrices
mat projection;
mat view;
mat model;
mat modelview;
// render state inputs
vec lightpos = {0.f, 7.f, 0.f};
// models
ESModel mdlPlane;
GLuint tex_skyplane;
ESModel mdlScene;
ESModel mdlDynamic;
ESModel mdlMinball;
uint bindstate = 0;
// simulation / game vars
uint ground = 0; // current round number
uint score = 0; // total score
uint rscore = 0; // round score
uint penalty = 0; // penalty count
uint state = 0; // game state
double s0lt = 0; // time offset controls ball swing offset
vec bp; // ball position
f32 stepspeed = 0.f; // ball speed
f32 hardness = 0.f; // ball hardness
//*************************************
// utility functions
//*************************************
void timestamp(char* ts)
{
const time_t tt = time(0);
strftime(ts, 16, "%H:%M:%S", localtime(&tt));
}
#ifdef __x86_64__
float sine_wtable[65536] = {0};
static inline float aliased_sin(const float theta)
{
const unsigned short i = (unsigned short)(10430.37793f * theta);
return sine_wtable[i];
}
#endif
//*************************************
// generation functions
//*************************************
void blotColour2(f32 r, f32 g, f32 b, f32 rad)
{
const GLushort rci = esRand(0, dynamic_numvert-1) * 3;
#ifdef LEAK_CHECK
if(rci+2 >= mlc){printf("\n\nblotColour2() #0 LEAK: %u\n\n", (rci+2)-mlc); exit(0);}
#endif
const vec tv = (vec){dynamic_vertices[rci], dynamic_vertices[rci+1], dynamic_vertices[rci+2]};
if(tv.x > 9.6f) // not too close to spawn
return;
const GLushort tc = (dynamic_numvert-1)*3;
for(GLushort i = 0; i < tc; i += 3)
{
#ifdef LEAK_CHECK
if(i+2 >= mlc){printf("\n\nblotColour2() #1 LEAK: %u\n\n", (i+2)-mlc); exit(0);}
#endif
const vec nv = (vec){dynamic_vertices[i], dynamic_vertices[i+1], dynamic_vertices[i+2]};
if(vDist(tv, nv) < rad)
{
dynamic_colors[i] = r;
dynamic_colors[i+1] = g;
dynamic_colors[i+2] = b;
}
}
}
void blotColour(f32 r, f32 g, f32 b, GLushort streak)
{
const GLushort rci = esRand(0, dynamic_numvert-1-streak) * 3;
for(GLushort i = 0; i < streak; i++)
{
#ifdef LEAK_CHECK
if(rci+(i*3)+2 >= mlc){printf("\n\nblotColour() LEAK: %u\n\n", (rci+(i*3)+2)-mlc); exit(0);}
#endif
dynamic_colors[rci+(i*3)] = r;
dynamic_colors[rci+(i*3)+1] = g;
dynamic_colors[rci+(i*3)+2] = b;
}
}
void gNewRound()
{
// count & log
ground++;
char strts[16];
timestamp(&strts[0]);
printf("[%s] STARTING ROUND %u - SCORE %u - PENALTY %u\n", strts, ground, score, penalty);
if(ground > 1)
{
char title[256];
sprintf(title, "Snowling - ROUND %u - SCORE %u - PENALTY %u", ground, score, penalty);
glfwSetWindowTitle(window, title);
}
// reset
const GLushort tc = dynamic_numvert*3;
for(GLushort i = 0; i < tc; i++)
dynamic_colors[i] = 1.f;
// ice
for(uint i = 0; i < 6; i++)
{
if(esRand(0, 1000) < 500)
blotColour(0.f, 1.f, 1.f, 32);
else
blotColour2(0.f, 1.f, 1.f, esRandFloat(0.1f, 0.9f));
}
// boost
for(uint i = 0; i < 6; i++)
{
if(esRand(0, 1000) < 500)
blotColour(0.50196f, 0.00000f, 0.50196f, 6);
else
blotColour2(0.50196f, 0.00000f, 0.50196f, esRandFloat(0.1f, 0.3f));
}
// lava
for(uint i = 0; i < 2; i++)
{
if(esRand(0, 1000) < 500)
blotColour(0.81176f, 0.06275f, 0.12549f, 6);
else
blotColour2(0.81176f, 0.06275f, 0.12549f, esRandFloat(0.1f, 0.3f));
}
// rebind
glBindBuffer(GL_ARRAY_BUFFER, mdlDynamic.cid);
glBufferData(GL_ARRAY_BUFFER, sizeof(dynamic_colors), dynamic_colors, GL_STATIC_DRAW);
#ifdef AUTOMATE
stepspeed = 0.5f + randf()*6.f;
s0lt = t - randf()*x2PI;
#endif
#ifdef LEAK_CHECK_BRUTE
stepspeed = 130.f;
#endif
}
uint checkCollisions(const f32 bpy)
{
// #ifdef __arm__
// static double ccl = 0;
// if(t < ccl)
// return 0;
// else
// ccl = t + 0.05; // limit checkCollisions() execution frequency
// #endif
vec lbp = bp;
lbp.z -= bpy;
static double tt1 = 0, tt2 = 0, tt3 = 0;
static const GLushort tc = (dynamic_numvert-1)*3;
for(GLushort i = 0; i < tc; i+=3)
{
#ifdef LEAK_CHECK
if(i+2 >= mlc){printf("\n\ncheckCollisions() LEAK: %u\n\n", (i+2)-mlc); exit(0);}
#endif
if(t > tt1 && dynamic_colors[i] == 0.f && dynamic_colors[i+1] == 1.f && dynamic_colors[i+2] == 1.f)
{
const vec cp = {dynamic_vertices[i], dynamic_vertices[i+1], dynamic_vertices[i+2]};
if(vDistSq(cp, lbp) < 0.0169f)
{
hardness += 1.f;
char strts[16];
timestamp(&strts[0]);
printf("[%s] hit ice: %u\n", strts, (uint)hardness);
tt1 = t+0.3; // freq limiter
return 1;
}
}
else if(t > tt2 && dynamic_colors[i] == 0.50196f && dynamic_colors[i+1] == 0.f && dynamic_colors[i+2] == 0.50196f)
{
const vec cp = {dynamic_vertices[i], dynamic_vertices[i+1], dynamic_vertices[i+2]};
if(vDistSq(cp, lbp) < 0.0169f)
{
stepspeed *= 3.4f;
char strts[16];
timestamp(&strts[0]);
printf("[%s] hit boost: %.1f\n", strts, stepspeed);
tt2 = t+0.3; // freq limiter
return 2;
}
}
else if(t > tt3 && dynamic_colors[i] == 0.81176f && dynamic_colors[i+1] == 0.06275f && dynamic_colors[i+2] == 0.12549f)
{
const vec cp = {dynamic_vertices[i], dynamic_vertices[i+1], dynamic_vertices[i+2]};
if(vDistSq(cp, lbp) < 0.0169f)
{
penalty++;
char strts[16];
timestamp(&strts[0]);
printf("[%s] hit lava: %u\n", strts, penalty);
printf("[%s] ~~ PENALTY YOU GOT MELTED BY LAVA ~~\n", strts);
tt3 = t+0.3; // freq limiter
return 3;
}
}
}
return 0;
}
//*************************************
// render functions
//*************************************
void rSkyPlane()
{
bindstate = 0;
static mat skyplane_model = {0.f};
static f32 la = 0;
if(skyplane_model.m[0][0] == 0.f || la != aspect)
{
mIdent(&skyplane_model);
mTranslate(&skyplane_model, -40.f, 0.f, 0.f);
mRotY(&skyplane_model, -90.f*DEG2RAD);
mRotX(&skyplane_model, -90.f*DEG2RAD);
mScale(&skyplane_model, 40.f*aspect, 23.f*aspect, 0);
la = aspect;
}
mMul(&modelview, &skyplane_model, &view);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (f32*)&modelview.m[0][0]);
glBindBuffer(GL_ARRAY_BUFFER, mdlPlane.tid);
glVertexAttribPointer(texcoord_id, 2, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(texcoord_id);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex_skyplane);
glUniform1i(sampler_id, 0);
glBindBuffer(GL_ARRAY_BUFFER, mdlPlane.vid);
glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(position_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlPlane.iid);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0);
}
void rStaticScene()
{
bindstate = 0;
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (f32*) &view.m[0][0]);
glUniform3f(lightpos_id, lightpos.x, lightpos.y, lightpos.z);
glBindBuffer(GL_ARRAY_BUFFER, mdlScene.vid);
glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(position_id);
glBindBuffer(GL_ARRAY_BUFFER, mdlScene.nid);
glVertexAttribPointer(normal_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(normal_id);
glBindBuffer(GL_ARRAY_BUFFER, mdlScene.cid);
glVertexAttribPointer(color_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(color_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlScene.iid);
glDrawElements(GL_TRIANGLES, scene_numind, GL_UNSIGNED_SHORT, 0);
}
void rDynamicScene()
{
bindstate = 0;
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (f32*) &view.m[0][0]);
glUniform3f(lightpos_id, lightpos.x, lightpos.y, lightpos.z);
glBindBuffer(GL_ARRAY_BUFFER, mdlDynamic.vid);
glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(position_id);
glBindBuffer(GL_ARRAY_BUFFER, mdlDynamic.nid);
glVertexAttribPointer(normal_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(normal_id);
glBindBuffer(GL_ARRAY_BUFFER, mdlDynamic.cid);
glVertexAttribPointer(color_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(color_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlDynamic.iid);
glDrawElements(GL_TRIANGLES, dynamic_numind, GL_UNSIGNED_SHORT, 0);
}
void rMinballRGB(f32 x, f32 y, f32 z, f32 r, f32 g, f32 b, f32 s)
{
mIdent(&model);
mTranslate(&model, x, y, z);
if(s != 1.f)
mScale(&model, s, s, s);
mMul(&modelview, &model, &view);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (f32*) &modelview.m[0][0]);
glUniform3f(color_id, r, g, b);
glUniform3f(lightpos_id, lightpos.x, lightpos.y, lightpos.z);
if(bindstate == 0)
{
glBindBuffer(GL_ARRAY_BUFFER, mdlMinball.vid);
glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(position_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlMinball.iid);
bindstate = 1;
}
glDrawElements(GL_TRIANGLES, minball_numind, GL_UNSIGNED_SHORT, 0);
}
static inline void rMinball(f32 x, f32 y, f32 z, f32 s)
{
rMinballRGB(x, y, z, 1.f, 1.f, 1.f, s);
}
void rPin(f32 x, f32 y, f32 z, uint down)
{
if(down == 1)
{
rMinballRGB(x, y, z, 1.f, 0.f, 0.f, 2.2f);
}
else
{
rMinball(x, y, z, 2.2f);
rMinball(x, y, z+0.14f, 1.45f);
rMinball(x, y, z+0.24f, 0.9f);
}
}
void rPinSet()
{
rPin(-0.9, 0.f, 0.f, rscore >= 1 ? 1 : 0);
rPin(-1.2, -0.14f, 0.f, rscore >= 2 ? 1 : 0);
rPin(-1.2, 0.14f, 0.f, rscore >= 3 ? 1 : 0);
rPin(-1.5, 0.f, 0.f, rscore >= 4 ? 1 : 0);
rPin(-1.5, -0.28f, 0.f, rscore >= 5 ? 1 : 0);
rPin(-1.5, 0.28f, 0.f, rscore >= 6 ? 1 : 0);
rPin(-1.8, -0.14f, 0.f, rscore >= 7 ? 1 : 0);
rPin(-1.8, 0.14f, 0.f, rscore >= 8 ? 1 : 0);
rPin(-1.8, -0.42f, 0.f, rscore >= 9 ? 1 : 0);
rPin(-1.8, 0.42f, 0.f, rscore >= 10 ? 1 : 0);
}
//*************************************
// interpolators and steppers for simulation
//*************************************
typedef struct { f32 y,z; } lt;
static inline f32 lerp(lt* a, lt* b, f32 y)
{
return a->z + (((y - a->y) / (b->y - a->y)) * (b->z - a->z));
}
lt hlt[25];
f32 getHeight(f32 y)
{
y = fabs(y);
for(uint i = 0; i < 24; i++)
if(y >= hlt[i].y && y < hlt[i+1].y)
return lerp(&hlt[i], &hlt[i+1], y);
return -1.f; // bad times if this happens
}
static inline f32 smoothStepN(f32 v)
{
return v * v * (3.f - 2.f * v);
}
//*************************************
// update & render
//*************************************
void main_loop()
{
//*************************************
// time delta for interpolation
//*************************************
static double lt = 0;
const f32 dt = t-lt;
lt = t;
//*************************************
// camera control
//*************************************
static f32 camdist = -15.f;
mIdent(&view);
mTranslate(&view, 0.f, -0.5f, camdist);
mRotY(&view, 1.396263361f);
mRotZ(&view, 1.570796371f);
//*************************************
// joystick control
//*************************************
if(stepspeed == 0.f)
{
int count;
const float* axes = glfwGetJoystickAxes(GLFW_JOYSTICK_1, &count);
if(count >= 2)
{
static float bt = 0;
if(t > bt)
{
const unsigned char* buttons = glfwGetJoystickButtons(GLFW_JOYSTICK_1, &count);
if(count >= 2)
{
if(buttons[0] == GLFW_PRESS)
{
stepspeed = 0.5f + (((axes[1]*-1.f)+1.f)*0.5f)*4.5f;
if(axes[0] < -0.75f)
s0lt = t - 3.769911289f;
else if(axes[0] > 0.75f)
s0lt = t - 0.6283185482f;
else if(axes[0] < 0.f)
s0lt = t - PI;
else if(axes[0] >= 0.f)
s0lt = t;
bt = t + 0.3f;
}
else if(buttons[1] == GLFW_PRESS)
{
s0lt = t - randf()*x2PI;
stepspeed = 0.5f + (((axes[1]*-1.f)+1.f)*0.5f)*4.5f;
bt = t + 0.3f;
}
}
}
}
}
//*************************************
// begin render
//*************************************
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//*************************************
// main render
//*************************************
// render sky plane
shadeFullbrightT(&position_id, &projection_id, &modelview_id, &texcoord_id, &sampler_id);
glUniformMatrix4fv(projection_id, 1, GL_FALSE, (f32*)&projection.m[0][0]);
rSkyPlane();
// render static and dynamic scenes
shadeLambert3(&position_id, &projection_id, &modelview_id, &lightpos_id, &normal_id, &color_id, &opacity_id);
glUniformMatrix4fv(projection_id, 1, GL_FALSE, (f32*)&projection.m[0][0]);
glUniform1f(opacity_id, 1.0f);
rStaticScene();
rDynamicScene();
// only rendering icospheres from here on out
shadeLambert(&position_id, &projection_id, &modelview_id, &lightpos_id, &color_id, &opacity_id);
glUniformMatrix4fv(projection_id, 1, GL_FALSE, (f32*)&projection.m[0][0]);
glUniform1f(opacity_id, 1.0f);
rPinSet();
// simulate the bowling snowball & transition the game states
static double s1lt = 0;
static f32 x = 10.5f;
static f32 ms = -1.f;
if(state == 0) // bowl simulation state
{
if(stepspeed == 0.f)
{
rMinball(10.5f, 0.f-0.017f, 0.f, 1.f);
}
else
{
const f32 ddt = stepspeed * dt;
x -= ddt;
camdist += ddt;
if(x <= -1.f)
state = 1;
#ifdef __x86_64__
const f32 h = aliased_sin(t-s0lt)*(1.38f-((10.5f-x)*0.1f));
#else
const f32 h = sinf(t-s0lt)*(1.38f-((10.5f-x)*0.1f));
#endif
f32 ns = (10.5f-x)*0.4f;
if(ns < 1.f)
ns = 1.f;
lightpos.y = 7.f * smoothStepN(x*0.09523809701f);
bp.x = x;
bp.y = h-0.017f;
const f32 ho = ((1.f-(bp.x*0.09523809701f))*0.08f);
bp.z = getHeight(h) + ho;
rMinballRGB(bp.x, bp.y, bp.z, 1.f-(hardness*0.22f), 1.f, 1.f, ns);
if(checkCollisions(ho) == 3)
#ifndef LEAK_CHECK_BRUTE
state = 3;
#else
state = 2;
#endif
}
}
else if(state == 1) // pins knocked down freeze state
{
if(s1lt == 0)
{
// calc score
if(hardness > 0.f)
{
const f32 fscore = ((hardness * stepspeed) * 0.3f)+0.5f;
rscore = (uint)fscore;
if(rscore == 0)
{
char strts[16];
timestamp(&strts[0]);
printf("[%s] ~~ PENALTY YOU FAILED TO KNOCK DOWN ANY PINS ~~\n", strts);
penalty++;
s1lt = t + 1;
}
else if(rscore >= 10)
{
rscore = 20;
char strts[16];
timestamp(&strts[0]);
printf("[%s] !!! STRIKE !!! - ROUND %u - SCORE %u\n", strts, ground, rscore);
s1lt = t + 3;
}
else
{
char strts[16];
timestamp(&strts[0]);
printf("[%s] ~~ ROUND %u SCORE %u ~~\n", strts, ground, rscore);
s1lt = t + 1;
}
score += rscore;
}
else
{
char strts[16];
timestamp(&strts[0]);
printf("[%s] ~~ PENALTY YOU FAILED TO KNOCK DOWN ANY PINS ~~\n", strts);
penalty++;
s1lt = t + 1;
}
#ifdef AUTOMATE
s1lt = 0;
#endif
}
if(t > s1lt)
state = 2;
}
else if(state == 2) // new round state
{
// reset
lightpos.y = 7.f;
x = 10.5f;
camdist = -15.f;
stepspeed = 0.f;
hardness = 0.f;
state = 0;
s1lt = 0;
rscore = 0;
ms = -1.f;
gNewRound();
}
else if(state == 3) // hit lava, melting state (worst animation ever)
{
static f32 rsms = 0.f;
if(ms == -1.f)
{
ms = (10.5f-x)*0.4f;
rsms = 1.f/ms;
if(ms < 1.f)
ms = 1.f;
}
ms -= 1.f * dt;
if(ms <= 0.f)
state = 2;
bp.z -= 0.075f * dt;
rMinballRGB(bp.x, bp.y, bp.z, 1.f, 1.f - (1.f-ms*rsms), 1.f - (1.f-ms*rsms), ms);
}
//*************************************
// swap buffers / display render
//*************************************
glfwSwapBuffers(window);
}
//*************************************
// Input Handelling
//*************************************
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
static uint tab = 0;
static f32 ls = 3.f;
// debug reset
//if(key == GLFW_KEY_SPACE){state = 2;}
// speed selector
if((key == GLFW_KEY_TAB || key == GLFW_KEY_SPACE) && action == GLFW_PRESS)
tab = 1;
else if((key == GLFW_KEY_TAB || key == GLFW_KEY_SPACE) && action == GLFW_RELEASE)
tab = 0;
if(tab == 1)
{
if(key == GLFW_KEY_1)
ls = 0.5f;
else if(key == GLFW_KEY_2)
ls = 1.0f;
else if(key == GLFW_KEY_3)
ls = 1.5f;
else if(key == GLFW_KEY_4)
ls = 2.0f;
else if(key == GLFW_KEY_5)
ls = 2.5f;
else if(key == GLFW_KEY_6)
ls = 3.0f;
else if(key == GLFW_KEY_7)
ls = 3.5f;
else if(key == GLFW_KEY_8)
ls = 4.0f;
else if(key == GLFW_KEY_9)
ls = 4.5f;
else if(key == GLFW_KEY_0)
ls = 5.0f;
static f32 lls = 0.f;
if(lls != ls)
{
char strts[16];
timestamp(&strts[0]);
printf("[%s] Launch Speed: %.1f\n", strts, ls);
lls = ls;
}
}
// launchers
if(stepspeed == 0.f && tab == 0)
{
// center
if(key == GLFW_KEY_5){ stepspeed = ls; s0lt = t - PI; return; }
if(key == GLFW_KEY_6){ stepspeed = ls; s0lt = t; return; }
// inner
if(key == GLFW_KEY_4){ stepspeed = ls; s0lt = t - (0.6283185482f * 4.0f); return; }
if(key == GLFW_KEY_7){ stepspeed = ls; s0lt = t - (0.6283185482f * 9.0f); return; }
// just off center
if(key == GLFW_KEY_3){ stepspeed = ls; s0lt = t - (0.6283185482f * 6.0f); return; }
if(key == GLFW_KEY_8){ stepspeed = ls; s0lt = t - 0.6283185482f; return; }
// wide center
if(key == GLFW_KEY_2){ stepspeed = ls; s0lt = t - (0.6283185482f * 7.0f); return; }
if(key == GLFW_KEY_9){ stepspeed = ls; s0lt = t - (0.6283185482f * 2.0f); return; }
// super wide
if(key == GLFW_KEY_1){ stepspeed = ls; s0lt = t - (0.6283185482f * 8.0f); return; }
if(key == GLFW_KEY_0){ stepspeed = ls; s0lt = t - (0.6283185482f * 3.0f); return; }
// simple
if(key == GLFW_KEY_LEFT) { stepspeed = 1.5f; s0lt = t - 3.3f; return; }
if(key == GLFW_KEY_RIGHT){ stepspeed = 1.5f; s0lt = t - 6.4f; return; }
if(key == GLFW_KEY_UP) { stepspeed = 4.5f; s0lt = t - 9.9f; return; }
if(key == GLFW_KEY_DOWN) { stepspeed = 4.5f; s0lt = t; return; }
// random
if(key == GLFW_KEY_R || key == GLFW_KEY_RIGHT_SHIFT)
{
stepspeed = 0.5f + randf()*6.f; // 1.5f higher possible speed with random
s0lt = t - randf()*x2PI;
return;
}
}
// 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);
ww = winw;
wh = winh;
aspect = ww / wh;
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);
}
void mouse_button_callback(GLFWwindow* window, int button, int action, int mods)
{
if(button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS)
{
if(stepspeed == 0.f)
{
stepspeed = 0.5f + randf()*6.f;
s0lt = t - randf()*x2PI;
}
}
}
//*************************************
// Process Entry Point
//*************************************
int main(int argc, char** argv)
{
// help
printf("Snowling, A simple bowling game with a festive twist.\n");
printf("James William Fletcher (github.com/mrbid)\n\n");
printf("The background photo of the Austrian Alps was taken by Karl Köhler:\nhttps://unsplash.com/photos/N_MXyBUV5hU\nhttps://unsplash.com/@karlkoehler\n\n");
printf("The premise is simple; you must launch the snowball at the pins and knock them down, seems easy right? Well, your snowball wont knock any pins down unless you roll over some ice first to harden it up. There are three obstacles in the game:\n\n");
printf("Ice (color: Aqua)\n - Hardens your snowball to pack a heavier punch on the pins.\n\n");
printf("Boost (color: Purple)\n - Increases the speed of your snowball to blow a heavier punch on the pins.\n\n");
printf("Lava (color: Red)\n - Melts your snowball; instant penalty.\n\n");
printf("Key Bindings:\n");
printf(" - [TAB/SPACE + 0-9] Select launch speed.\n");
printf(" - [0-9] Launch ball at one of 10 different angles, 5-1 for left hand launches and 6-0 for right hand launches.\n");
printf(" - [R/LSHIFT/SPACE] Launch ball at a random angle and speed.\n");
printf(" - [Left, Right, Up, Down] Launch ball at four quick selection angles and speeds.\n\n");
printf("Joypad Bindings:\n");
printf(" - [Stick 1 - Left to Right] Launch Angle\n");
printf(" - [Stick 1 - Up and Down] Launch Speed\n");
printf(" - [Button 1] Launch\n");
printf(" - [Button 2] Random Launch\n\n");
printf("I tend to just use [UP, DOWN, LEFT, RIGHT] and [R] when I play, but if you are competitive you will want to get accustomed with using [TAB + 0-9] & [0-9].\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, "Snowling", 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);
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
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
srandf(time(0));
#ifdef __x86_64__
// gen sine table
for(int i = 0; i < 65536; i++)
sine_wtable[i] = sinf(i * 9.587380191e-05f); // 9.587380191e-05f = x2PIf / 65536.f;
#endif
// gen height table
hlt[0].y = 0; hlt[0].z = 0.016813;
hlt[1].y = 0.102108; hlt[1].z = 0.018762;
hlt[2].y = 0.191487; hlt[2].z = 0.024604;
hlt[3].y = 0.280097; hlt[3].z = 0.034311;
hlt[4].y = 0.367561; hlt[4].z = 0.047843;
hlt[5].y = 0.453504; hlt[5].z = 0.065142;
hlt[6].y = 0.537558; hlt[6].z = 0.086133;
hlt[7].y = 0.619362; hlt[7].z = 0.110728;
hlt[8].y = 0.698569; hlt[8].z = 0.138821;
hlt[9].y = 0.774836; hlt[9].z = 0.170289;
hlt[10].y = 0.847838; hlt[10].z = 0.205001;
hlt[11].y = 0.917262; hlt[11].z = 0.242807;
hlt[12].y = 0.982812; hlt[12].z = 0.283543;
hlt[13].y = 1.04421; hlt[13].z = 0.327039;
hlt[14].y = 1.10118; hlt[14].z = 0.373106;
hlt[15].y = 1.15349; hlt[15].z = 0.421545;
hlt[16].y = 1.20092; hlt[16].z = 0.472152;
hlt[17].y = 1.24325; hlt[17].z = 0.524709;
hlt[18].y = 1.28032; hlt[18].z = 0.57899;
hlt[19].y = 1.31195; hlt[19].z = 0.634764;
hlt[20].y = 1.33803; hlt[20].z = 0.69179;
hlt[21].y = 1.35843; hlt[21].z = 0.749827;
hlt[22].y = 1.37305; hlt[22].z = 0.808624;
hlt[23].y = 1.38185; hlt[23].z = 0.867931;
hlt[24].y = 1.38479; hlt[24].z = 0.927491;
//*************************************
// projection
//*************************************
window_size_callback(window, winw, winh);
//*************************************
// bind vertex and index buffers
//*************************************
// ***** BIND SKY PLANE *****
f32 plane_vert[] = {1.000000,-1.000000,0.000000,-1.000000,1.000000,0.000000,-1.000000,-1.000000,0.000000,1.000000,1.000000,0.000000};
GLushort plane_indi[] = {0,1,2,0,3,1};
esBindModel(&mdlPlane, plane_vert, 9, plane_indi, 6);
f32 plane_texc[] = {1.f,1.f, 0.f,0.f, 0.f,1.f, 1.f,0.f};
esBind(GL_ARRAY_BUFFER, &mdlPlane.tid, plane_texc, sizeof(plane_texc), GL_STATIC_DRAW);
tex_skyplane = esLoadTexture(alpinebg.width, alpinebg.height, &alpinebg.pixel_data[0]);
// ***** BIND STATIC 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 DYNAMIC SCENE *****
esBind(GL_ARRAY_BUFFER, &mdlDynamic.vid, dynamic_vertices, sizeof(dynamic_vertices), GL_STATIC_DRAW);
esBind(GL_ARRAY_BUFFER, &mdlDynamic.nid, dynamic_normals, sizeof(dynamic_normals), GL_STATIC_DRAW);
esBind(GL_ARRAY_BUFFER, &mdlDynamic.cid, dynamic_colors, sizeof(dynamic_colors), GL_STATIC_DRAW);
esBind(GL_ELEMENT_ARRAY_BUFFER, &mdlDynamic.iid, dynamic_indices, sizeof(dynamic_indices), GL_STATIC_DRAW);
// ***** BIND MIN BALL *****
esBind(GL_ARRAY_BUFFER, &mdlMinball.vid, minball_vertices, sizeof(minball_vertices), GL_STATIC_DRAW);
esBind(GL_ELEMENT_ARRAY_BUFFER, &mdlMinball.iid, minball_indices, sizeof(minball_indices), GL_STATIC_DRAW);
//*************************************
// compile & link shader programs
//*************************************
// makeAllShaders();
makeLambert();
makeLambert3();
makeFullbrightT();
//*************************************
// configure render options
//*************************************
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glClearColor(0.20000f, 0.34510f, 0.48627f, 0.0);
//*************************************
// execute update / render loop
//*************************************
// new round
gNewRound();
// reset
t = glfwGetTime();
// event loop
while(!glfwWindowShouldClose(window))
{
t = glfwGetTime();
glfwPollEvents();
main_loop();
}
// done
glfwDestroyWindow(window);
glfwTerminate();
exit(EXIT_SUCCESS);
return 0;
}