-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.c
1689 lines (1501 loc) · 55.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)
July 2022 - January 2023
https://SeaPusher.com & https://pusha.one
A highly optimised and efficient coin pusher
implementation.
This was not made in a way that is easily extensible,
the code can actually seem kind of confusing to
initially understand.
Unlike the CoinPusher/SeaPusher release;
https://github.com/mrbid/coinpusher this version does not
allow custom framerates, just whatever your screen refresh
rate is set to. I just wanted to be a little simple/regular
in this release.
Also in this one I step the collisions 6 times per frame
rather than just once, which is 6x better quality, this
prevents noticable overlap at higher input speeds which
can now be set via the second argv.
*/
#include <SDL2/SDL_mouse.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#ifdef BUILD_GLFW
#include "inc/gl.h"
#define GLFW_INCLUDE_NONE
#include "inc/glfw3.h"
#define GL_DEBUG
#else
#include <SDL2/SDL.h>
#include <SDL2/SDL_opengles2.h>
#endif
#ifndef __x86_64__
#define NOSSE
#endif
// uncomment if you want that little bit more performance
//#define FAST_RAND
#ifdef FAST_RAND
#define SEIR_RAND
#endif
#include "esAux2.h"
#include "res.h"
#include "assets/scene.h"
#include "assets/coin.h"
#include "assets/coin_silver.h"
#include "assets/tux.h"
#include "assets/evil.h"
#include "assets/king.h"
#include "assets/ninja.h"
#include "assets/surf.h"
#include "assets/trip.h"
#include "assets/gameover.h"
#include "assets/rx.h"
#include "assets/sa.h"
#include "assets/ga.h"
#include "assets/console_menus.h"
#define uint GLushort // it's a short don't forget that
#define sint GLshort // and this.
#define f32 GLfloat
#define forceinline __attribute__((always_inline)) inline
//*************************************
// globals
//*************************************
char appTitle[] = "TuxPusher.com";
#ifdef BUILD_GLFW
GLFWwindow* wnd;
#else
SDL_Window* wnd;
SDL_GLContext glc;
SDL_Surface* s_icon = NULL;
SDL_Cursor* cross_cursor;
SDL_Cursor* beam_cursor;
#endif
uint cursor_state = 0;
uint winw = 1024, winh = 768;
f32 aspect;
f32 t = 0.f;
f32 dt = 0; // delta time
f32 fc = 0; // frame count
f32 lfct = 0; // last frame count time
f32 rww, ww, rwh, wh, ww2, wh2;
f32 uw, uh, uw2, uh2; // normalised pixel dpi
f32 touch_margin = 120.f;
f32 mx=0, my=0;
uint md=0;
f32 rst = 0.f;
uint ortho = 0;
// 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, 10.f, 13.f};
int csp = -1; // current shader program
ESModel mdlPlane;
ESModel mdlGameover;
ESModel mdlScene;
ESModel mdlCoin;
ESModel mdlCoinSilver;
ESModel mdlTux;
ESModel mdlEvil;
ESModel mdlKing;
ESModel mdlSurf;
ESModel mdlNinja;
ESModel mdlTrip;
ESModel mdlRX;
ESModel mdlSA;
ESModel mdlGA;
// game vars
f32 gold_stack = 64.f; // line 740+ defining these as float32 eliminates the need to cast in mTranslate()
f32 silver_stack = 64.f;// function due to the use of a float32 also in the for(f32 i;) loop.
uint active_coin = 0;
uint inmotion = 0;
f32 gameover = 0.f;
uint isnewcoin = 0;
f32 PUSH_SPEED = 1.6f;
typedef struct
{
f32 x, y, r;
signed char color;
} coin; // 4+4+4+1 = 13 bytes, 3 bytes padding to 16 byte cache line
#define MAX_COINS 130
coin coins[MAX_COINS] = {0};
// Bit flag based method for storing trophie states,
// 0b0000_0000 where the last bit denotes 1, second last denotes 2 etc like so:
// 0b0000_0001 = trophie 1
// 0b0000_0101 = trophie 1 and 3
char trophies_bits = 0;
#define trophies_set(x) trophies_bits |= (0b1 << x)
#define trophies_clear() trophies_bits = 0
#define trophies_get(x) (trophies_bits >> x) & 0b1
#define trophies_all() trophies_bits
//*************************************
// game functions
//*************************************
void timestamp(char* ts)
{
const time_t tt = time(0);
strftime(ts, 16, "%H:%M:%S", localtime(&tt));
}
forceinline f32 fRandFloat(const f32 min, const f32 max)
{
#ifdef FAST_RAND
return min + randf() * (max-min);
#else
return esRandFloat(min, max);
#endif
}
forceinline int fRand(const f32 min, const f32 max)
{
#ifdef FAST_RAND
return (int)((min + randf() * (max-min))+0.5f);
#else
return esRand(min, max);
#endif
}
forceinline f32 f32Time()
{
#ifdef BUILD_GLFW
return glfwGetTime();
#else
return ((f32)SDL_GetTicks())*0.001f;
#endif
}
void setActiveCoin(const uint color)
{
for(int i=0; i < MAX_COINS; i++)
{
if(coins[i].color == -1)
{
coins[i].color = color;
active_coin = i;
return;
}
}
}
void takeStack()
{
if(gameover != 0.f)
return;
if(silver_stack != 0.f)
{
// play a silver coin
isnewcoin = 1;
setActiveCoin(0);
inmotion = 1;
}
else if(gold_stack != 0.f)
{
// play a gold coin
isnewcoin = 2;
setActiveCoin(1);
inmotion = 1;
}
if(inmotion == 1)
{
if(mx < touch_margin)
{
coins[active_coin].x = -1.90433f;
coins[active_coin].y = -4.54055f;
return;
}
if(mx > ww-touch_margin)
{
coins[active_coin].x = 1.90433f;
coins[active_coin].y = -4.54055f;
return;
}
coins[active_coin].x = -1.90433f+(((mx-touch_margin)*rww)*3.80866f);
coins[active_coin].y = -4.54055f;
}
}
void injectFigure()
{
if(inmotion != 0)
return;
int fcn = -1;
for(int i=0; i < 3; i++)
{
if(coins[i].color == -1)
{
active_coin = i;
fcn = i;
coins[i].color = fRand(1, 6);
break;
}
}
if(fcn != -1)
{
coins[active_coin].x = fRandFloat(-1.90433f, 1.90433f);
coins[active_coin].y = -4.54055f;
inmotion = 1;
}
}
int insidePitch(const f32 x, const f32 y, const f32 r)
{
// off bottom
if(y < -4.03414f+r)
return 0;
// first left & right
if(y < -2.22855f)
{
if(x < (-2.22482f - (0.77267f*(fabsf(y+4.03414f) * 0.553835588f))) + r)
return 0;
else if(x > (2.22482f + (0.77267f*(fabsf(y+4.03414f) * 0.553835588f))) - r)
return 0;
}
else if(y < -0.292027f) // second left & right
{
if(x < (-2.99749f - (0.41114f*(fabsf(y+2.22855f) * 0.516389426f))) + r)
return 0;
else if(x > (2.99749f + (0.41114f*(fabsf(y+2.22855f) * 0.516389426f))) - r)
return 0;
}
else if(y < 1.45439f) // third left & right
{
if(x < -3.40863f + r)
return 0;
else if(x > 3.40863f - r)
return 0;
}
return 1;
}
int collision(int ci)
{
for(int i=0; i < MAX_COINS; i++)
{
if(i == ci || coins[i].color == -1){continue;}
const f32 xm = (coins[i].x - coins[ci].x);
const f32 ym = (coins[i].y - coins[ci].y);
const f32 radd = coins[i].r+coins[ci].r;
if(xm*xm + ym*ym < radd*radd)
return 1;
}
return 0;
}
uint stepCollisions()
{
uint was_collision = 0;
for(int i=0; i < MAX_COINS; i++)
{
if(coins[i].color == -1){continue;}
for(int j=0; j < MAX_COINS; j++)
{
if(i == j || coins[j].color == -1 || j == active_coin){continue;}
f32 xm = (coins[i].x - coins[j].x);
xm += fRandFloat(-0.01f, 0.01f); // add some random offset to our unit vector, very subtle but works so well!
const f32 ym = (coins[i].y - coins[j].y);
f32 d = xm*xm + ym*ym;
const f32 cr = coins[i].r+coins[j].r;
if(d < cr*cr)
{
d = sqrtps(d);
const f32 len = 1.f/d;
const f32 uy = (ym * len);
if(uy > 0.f){continue;} // best hack ever to massively simplify
const f32 m = d-cr;
coins[j].x += (xm * len) * m;
coins[j].y += uy * m;
// first left & right
if(coins[j].y < -2.22855f)
{
const f32 fl = (-2.22482f - (0.77267f*(fabsf(coins[j].y+4.03414f) * 0.553835588f))) + coins[j].r;
if(coins[j].x < fl)
{
coins[j].x = fl;
}
else
{
const f32 fr = ( 2.22482f + (0.77267f*(fabsf(coins[j].y+4.03414f) * 0.553835588f))) - coins[j].r;
if(coins[j].x > fr)
coins[j].x = fr;
}
}
else if(coins[j].y < -0.292027f) // second left & right
{
const f32 fl = (-2.99749f - (0.41114f*(fabsf(coins[j].y+2.22855f) * 0.516389426f))) + coins[j].r;
if(coins[j].x < fl)
{
coins[j].x = fl;
}
else
{
const f32 fr = (2.99749f + (0.41114f*(fabsf(coins[j].y+2.22855f) * 0.516389426f))) - coins[j].r;
if(coins[j].x > fr)
coins[j].x = fr;
}
}
else if(coins[j].y < 1.64f) // third left & right
{
const f32 fl = -3.40863f + coins[j].r;
if(coins[j].x < fl)
{
coins[j].x = fl;
}
else
{
const f32 fr = 3.40863f - coins[j].r;
if(coins[j].x > fr)
coins[j].x = fr;
}
}
else if(coins[j].y < 2.58397f) // first house goal
{
const f32 fl = (-3.40863f + (0.41113f*(fabsf(coins[j].y-1.45439f) * 0.885284796f)));
if(coins[j].x < fl)
{
coins[j].color = -1;
}
else
{
const f32 fr = (3.40863f - (0.41113f*(fabsf(coins[j].y-1.45439f) * 0.885284796f)));
if(coins[j].x > fr)
coins[j].color = -1;
}
}
else if(coins[j].y < 3.70642f) // second house goal
{
const f32 fl = (-2.9975f + (1.34581f*(fabsf(coins[j].y-2.58397f) * 0.890908281f)));
if(coins[j].x < fl)
{
coins[j].color = -1;
}
else
{
const f32 fr = (2.9975f - (1.34581f*(fabsf(coins[j].y-2.58397f) * 0.890908281f)));
if(coins[j].x > fr)
coins[j].color = -1;
}
}
else if(coins[j].y < 4.10583f) // silver goal
{
const f32 fl = (-1.65169f + (1.067374f*(fabsf(coins[j].y-3.70642f) * 2.503692947f)));
if(coins[j].x < fl)
{
if(j >= 0 && j <= 3)
{
if(trophies_get(coins[j].color-1)) // already have? then reward coins!
{
gold_stack += 6.f;
silver_stack += 6.f;
}
else
trophies_set(coins[j].color-1);
}
else
{
if(coins[j].color == 0)
silver_stack += 1.f;
else if(coins[j].color == 1)
silver_stack += 2.f;
}
coins[j].color = -1;
}
else
{
const f32 fr = (1.65169f - (1.067374f*(fabsf(coins[j].y-3.70642f) * 2.503692947f)));
if(coins[j].x > fr)
{
if(j >= 0 && j <= 3)
{
if(trophies_get(coins[j].color-1)) // already have? then reward coins!
{
gold_stack += 6.f;
silver_stack += 6.f;
}
else
trophies_set(coins[j].color-1);
}
else
{
if(coins[j].color == 0)
silver_stack += 1.f;
else if(coins[j].color == 1)
silver_stack += 2.f;
}
coins[j].color = -1;
}
}
}
else if(coins[j].y >= 4.31457f) // gold goal
{
if(coins[j].x >= -0.584316f && coins[j].x <= 0.584316f)
{
if(j >= 0 && j <= 3)
{
if(trophies_get(coins[j].color-1)) // already have? then reward coins!
{
gold_stack += 6.f;
silver_stack += 6.f;
}
else
trophies_set(coins[j].color-1);
}
else
{
if(coins[j].color == 0)
gold_stack += 1.f;
else if(coins[j].color == 1)
gold_stack += 2.f;
}
coins[j].color = -1;
}
else
{
if(j >= 0 && j <= 3)
{
if(trophies_get(coins[j].color-1)) // already have? then reward coins!
{
gold_stack += 6.f;
silver_stack += 6.f;
}
else
trophies_set(coins[j].color-1);
}
else
silver_stack += 1.f;
coins[j].color = -1;
}
}
was_collision++;
}
}
}
return was_collision;
}
void newGame()
{
// seed randoms
srand(time(0));
// defaults
gold_stack = 64.f;
silver_stack = 64.f;
active_coin = 0;
inmotion = 0;
gameover = 0.f;
trophies_clear();
for(int i=0; i < MAX_COINS; i++)
{
coins[i].color = -1;
coins[i].r = 0.3f;
}
// trophies
for(int i=0; i < 3; i++)
{
coins[i].color = fRand(1, 6);
coins[i].r = 0.36f;
coins[i].x = fRandFloat(-3.40863f, 3.40863f);
coins[i].y = fRandFloat(-4.03414f, 1.45439f-coins[i].r);
while(insidePitch(coins[i].x, coins[i].y, coins[i].r) == 0 || collision(i) == 1)
{
coins[i].x = fRandFloat(-3.40863f, 3.40863f);
coins[i].y = fRandFloat(-4.03414f, 1.45439f-coins[i].r);
}
}
// coins
const f32 lt = f32Time();
for(int i=3; i < MAX_COINS; i++)
{
coins[i].x = fRandFloat(-3.40863f, 3.40863f);
coins[i].y = fRandFloat(-4.03414f, 1.45439f-coins[i].r);
uint tl = 0;
while(insidePitch(coins[i].x, coins[i].y, coins[i].r) == 0 || collision(i) == 1)
{
coins[i].x = fRandFloat(-3.40863f, 3.40863f);
coins[i].y = fRandFloat(-4.03414f, 1.45439f-coins[i].r);
if(f32Time()-lt > 0.033){tl=1;break;} // 33ms timeout
}
if(tl==1){break;}
coins[i].color = fRand(0, 4);
if(coins[i].color > 1){coins[i].color = 0;}
}
// const int mc2 = MAX_COINS/2;
// for(int i=3; i < mc2; i++)
// {
// coins[i].x = fRandFloat(-3.40863f, 3.40863f);
// coins[i].y = fRandFloat(-4.03414f, 1.45439f-coins[i].r);
// while(insidePitch(coins[i].x, coins[i].y, coins[i].r) == 0 || collision(i) == 1)
// {
// coins[i].x = fRandFloat(-3.40863f, 3.40863f);
// coins[i].y = fRandFloat(-4.03414f, 1.45439f-coins[i].r);
// }
// coins[i].color = fRand(0, 4);
// if(coins[i].color > 1){coins[i].color = 0;}
// }
rst = f32Time(); // round start time
}
//*************************************
// render functions
//*************************************
forceinline void modelBind1(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);
}
forceinline 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);
}
void doPerspective()
{
glViewport(0, 0, winw, winh);
ww = (f32)winw;
wh = (f32)winh;
if(ortho == 1){touch_margin = ww*0.3076923192f;}
else{touch_margin = ww*0.2058590651f;}
rww = 1.f/(ww-touch_margin*2.f);
rwh = 1.f/wh;
ww2 = ww/2.f;
wh2 = wh/2.f;
uw = aspect/ww;
uh = 1.f/wh;
uw2 = aspect/ww2;
uh2 = 1.f/wh2;
mIdent(&projection);
if(ortho == 1)
mOrtho(&projection, -5.0f, 5.0f, -3.2f, 3.4f, 2.0f, 30.f);
else
{
if(winw > winh)
aspect = ww / wh;
else
aspect = wh / ww;
mPerspective(&projection, 30.0f, aspect, 2.0f, 30.f);
}
}
//*************************************
// update & render
//*************************************
void main_loop()
{
//*************************************
// time delta for interpolation
//*************************************
static f32 lt = 0;
t = f32Time();
dt = t-lt;
lt = t;
//*************************************
// input handling
//*************************************
#ifdef BUILD_GLFW
double tmx, tmy;
glfwGetCursorPos(wnd, &tmx, &tmy);
mx = (f32)tmx;
my = (f32)tmy;
#else
SDL_Event event;
while(SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_MOUSEMOTION:
mx = (f32)event.motion.x;
my = (f32)event.motion.y;
break;
case SDL_MOUSEBUTTONDOWN:
switch (event.button.button)
{
case SDL_BUTTON_LEFT:
if (inmotion != 0 || event.button.button != SDL_BUTTON_LEFT)
break;
takeStack();
md = 1;
if (gameover == 0.f)
break;
if(f32Time() <= gameover+3.0f)
break;
newGame();
if(PUSH_SPEED >= 32.f)
return;
PUSH_SPEED += 1.f;
char titlestr[256];
sprintf(titlestr, "TuxPusher [%.1f]", PUSH_SPEED);
SDL_SetWindowTitle(wnd, titlestr);
return;
case SDL_BUTTON_RIGHT:
static uint cs = 1;
cs = 1 - cs;
SDL_ShowCursor(cs);
}
break;
case SDL_MOUSEBUTTONUP:
if(event.button.button == SDL_BUTTON_LEFT)
md = 0;
break;
case SDL_KEYDOWN:
switch (event.key.keysym.sym)
{
case SDLK_f:
if(t-lfct <= 2.0)
break;
char strts[16];
timestamp(&strts[0]);
const f32 nfps = fc/(t-lfct);
printf("[%s] FPS: %g\n", strts, nfps);
lfct = t;
fc = 0;
break;
case SDLK_c:
ortho = 1 - ortho;
doPerspective();
break;
}
break;
case SDL_WINDOWEVENT:
if(event.window.event != SDL_WINDOWEVENT_RESIZED)
break;
winw = event.window.data1;
winh = event.window.data2;
doPerspective();
break;
case SDL_QUIT:
SDL_GL_DeleteContext(glc);
SDL_FreeSurface(s_icon);
SDL_FreeCursor(cross_cursor);
SDL_FreeCursor(beam_cursor);
SDL_DestroyWindow(wnd);
SDL_Quit();
exit(0);
}
}
#endif
//*************************************
// begin render
//*************************************
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//*************************************
// main render
//*************************************
#ifndef BUILD_GLFW
// // Cursor icon switching based on the mouse position.
// // Have a cross while over the playing field.
// switch (cursor_state)
// {
// case 0:
// if (mx <= ww-touch_margin+1.f && mx >= touch_margin-1.f)
// break;
// SDL_SetCursor(beam_cursor);
// cursor_state = 1;
// break;
// case 1:
// if (mx > touch_margin && mx < ww-touch_margin)
// SDL_SetCursor(cross_cursor);
// cursor_state = 0;
// break;
// }
// reverting this bit of code due to a bug
if(cursor_state == 0 && mx < touch_margin-1.f)
{
SDL_SetCursor(beam_cursor);
cursor_state = 1;
}
else if(cursor_state == 0 && mx > ww-touch_margin+1.f)
{
SDL_SetCursor(beam_cursor);
cursor_state = 1;
}
else if(cursor_state == 1 && mx > touch_margin && mx < ww-touch_margin)
{
SDL_SetCursor(cross_cursor);
cursor_state = 0;
}
#endif
// camera
mIdent(&view);
mTranslate(&view, 0.f, 0.f, -13.f);
if(ortho == 1)
mRotY(&view, 50.f*DEG2RAD);
else
mRotY(&view, 62.f*DEG2RAD);
// inject a new figure if time has come
injectFigure();
// prep scene for rendering
if(csp != 1)
{
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]);
glUniform3f(lightpos_id, lightpos.x, lightpos.y, lightpos.z);
csp = 1;
}
glUniform1f(opacity_id, 0.148f);
// render scene
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (f32*) &view.m[0][0]);
modelBind3(&mdlScene);
glDrawElements(GL_TRIANGLES, scene_numind, GL_UNSIGNED_SHORT, 0);
// detect gameover
if(gold_stack < 0.f){gold_stack = 0.f;}
if(silver_stack < 0.f){silver_stack = 0.f;}
if(gameover > 0.f && (gold_stack != 0.f || silver_stack != 0.f))
{
gameover = 0.f;
}
else if(gold_stack == 0.f && silver_stack == 0.f)
{
if(gameover == 0.f)
gameover = t+3.0f;
}
// coin
glUniform1f(opacity_id, 0.148f);
// targeting coin
if(gold_stack > 0.f || silver_stack > 0.f)
{
if(coins[active_coin].color == 1)
modelBind3(&mdlCoinSilver);
else
modelBind3(&mdlCoin);
if(inmotion == 0)
{
if(silver_stack > 0.f)
modelBind3(&mdlCoinSilver);
else
modelBind3(&mdlCoin);
mIdent(&model);
mScale(&model, 1.f, 1.f, 2.f);
if(mx < touch_margin)
mTranslate(&model, -1.90433f, -4.54055f, 0);
else if(mx > ww-touch_margin)
mTranslate(&model, 1.90433f, -4.54055f, 0);
else
mTranslate(&model, -1.90433f+(((mx-touch_margin)*rww)*3.80866f), -4.54055f, 0);
mMul(&modelview, &model, &view);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (f32*) &modelview.m[0][0]);
glDrawElements(GL_TRIANGLES, coin_numind, GL_UNSIGNED_SHORT, 0);
}
}
// do motion
if(inmotion == 1)
{
if(coins[active_coin].y < -3.73414f)
{
coins[active_coin].y += PUSH_SPEED * dt;
for(int i=0; i < 6; i++) // six seems enough
stepCollisions();
}
else
{
inmotion = 0;
if(isnewcoin > 0)
{
if(isnewcoin == 1)
silver_stack -= 1.f;
else
gold_stack -= 1.f;
isnewcoin = 0;
}
}
}
// gold stack
modelBind3(&mdlCoin);
f32 gss = gold_stack;
if(silver_stack == 0.f){gss -= 1.f;}
if(gss < 0.f){gss = 0.f;}
for(f32 i = 0.f; i < gss; i += 1.f)
{
mIdent(&model);
if(ortho == 0)
mTranslate(&model, -2.62939f, -4.54055f, 0.033f*i);
else
mTranslate(&model, -4.62939f, -4.54055f, 0.033f*i);
mMul(&modelview, &model, &view);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (f32*) &modelview.m[0][0]);
glDrawElements(GL_TRIANGLES, coin_numind, GL_UNSIGNED_SHORT, 0);
}
// silver stack
modelBind3(&mdlCoinSilver);
f32 sss = silver_stack-1.f;
if(sss < 0.f){sss = 0.f;}
for(f32 i = 0.f; i < sss; i += 1.f)
{
mIdent(&model);
if(ortho == 0)
mTranslate(&model, 2.62939f, -4.54055f, 0.033f*i);
else
mTranslate(&model, 4.62939f, -4.54055f, 0.033f*i);
mMul(&modelview, &model, &view);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (f32*) &modelview.m[0][0]);
glDrawElements(GL_TRIANGLES, coin_numind, GL_UNSIGNED_SHORT, 0);
}
// pitch coins
for(int i=3; i < MAX_COINS; i++)
{
if(coins[i].color == -1)
continue;
if(coins[i].color == 0)
modelBind3(&mdlCoinSilver);
else
modelBind3(&mdlCoin);
mIdent(&model);
mScale(&model, 1.f, 1.f, 2.f);
mTranslate(&model, coins[i].x, coins[i].y, 0.f);
mMul(&modelview, &model, &view);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (f32*) &modelview.m[0][0]);
glDrawElements(GL_TRIANGLES, coin_numind, GL_UNSIGNED_SHORT, 0);
}
// tux is fancy
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]);
glUniform3f(lightpos_id, lightpos.x, lightpos.y, lightpos.z);
glUniform1f(opacity_id, 0.148f);
// trophies
for(int i=0; i < 3; i++)
{
mIdent(&model);
mTranslate(&model, coins[i].x, coins[i].y, 0.f);
mMul(&modelview, &model, &view);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (f32*) &modelview.m[0][0]);
// Draw the base Tux model, this will be our base to add apparel onto
glUniform1f(opacity_id, 0.148f);
modelBind3(&mdlTux);
glDrawElements(GL_TRIANGLES, tux_numind, GL_UNSIGNED_SHORT, 0);
// Tux Skin Selection.
switch (coins[i].color) {
case 2:
glUniform1f(opacity_id, 0.5f);
modelBind3(&mdlEvil);
glDrawElements(GL_TRIANGLES, evil_numind, GL_UNSIGNED_BYTE, 0);
break;
case 3:
glUniform1f(opacity_id, 0.6f);
modelBind3(&mdlKing);
glDrawElements(GL_TRIANGLES, king_numind, GL_UNSIGNED_BYTE, 0);
break;
case 4: