-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
926 lines (843 loc) · 27.1 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
/*
James William Fletcher ( github.com/mrbid )
June 2024
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define uint GLuint
#define sint GLint
#ifdef WEB
#include <emscripten.h>
#include <emscripten/html5.h>
#define GL_GLEXT_PROTOTYPES
#define EGL_EGLEXT_PROTOTYPES
#endif
#define GLAD_GL_IMPLEMENTATION
#include "inc/gl.h"
#define GLFW_INCLUDE_NONE
#include "inc/glfw3.h"
#define fTime() (float)glfwGetTime()
//#define GL_DEBUG
#define MAX_MODELS 63 // hard limit, be aware and increase if needed
#include "inc/esAux7.h"
#include "inc/matvec.h"
#include "inc/res.h"
#include "assets/high/sky.h" //0
#include "assets/high/water.h" //1
#include "assets/high/boat.h" //2
#include "assets/high/tux.h" //3
#include "assets/high/rod.h" //4
#include "assets/high/float.h" //5
#include "assets/high/splash.h" //6
#include "assets/high/fish/a0.h" //7
#include "assets/high/fish/a1.h" //8
#include "assets/high/fish/a2.h" //9
#include "assets/high/fish/a3.h" //10
#include "assets/high/fish/a4.h" //11
#include "assets/high/fish/a5.h" //12
#include "assets/high/fish/a6.h" //13
#include "assets/high/fish/a7.h" //14
#include "assets/high/fish/a8.h" //15
#include "assets/high/fish/a9.h" //16
#include "assets/high/fish/a10.h" //17
#include "assets/high/fish/a11.h" //18
#include "assets/high/fish/a12.h" //19
#include "assets/high/fish/a13.h" //20
#include "assets/high/fish/a14.h" //21
#include "assets/high/fish/b0.h" //22
#include "assets/high/fish/b1.h" //23
#include "assets/high/fish/b2.h" //24
#include "assets/high/fish/b3.h" //25
#include "assets/high/fish/b4.h" //26
#include "assets/high/fish/b5.h" //27
#include "assets/high/fish/b6.h" //28
#include "assets/high/fish/b7.h" //29
#include "assets/high/fish/b8.h" //30
#include "assets/high/fish/b9.h" //31
#include "assets/high/fish/b10.h" //32
#include "assets/high/fish/b11.h" //33
#include "assets/high/fish/b12.h" //34
#include "assets/high/fish/c0.h" //35
#include "assets/high/fish/c1.h" //36
#include "assets/high/fish/c2.h" //37
#include "assets/high/fish/c3.h" //38
#include "assets/high/fish/c4.h" //39
#include "assets/high/fish/c5.h" //40
#include "assets/high/fish/c6.h" //41
#include "assets/high/fish/c7.h" //42
#include "assets/high/fish/c8.h" //43
#include "assets/high/fish/c9.h" //44
#include "assets/high/fish/c10.h" //45
#include "assets/high/fish/c11.h" //46
#include "assets/high/fish/d0.h" //47
#include "assets/high/fish/d1.h" //48
#include "assets/high/fish/d2.h" //49
#include "assets/high/fish/d3.h" //50
#include "assets/high/fish/d4.h" //51
#include "assets/high/fish/d5.h" //52
#include "assets/high/fish/d6.h" //53
#include "assets/high/fish/d7.h" //54
#include "assets/high/fish/d8.h" //55
#include "assets/high/fish/d9.h" //56
#include "assets/high/fish/d10.h" //57
#include "assets/high/fish/d11.h" //58
#include "assets/high/fish/e1.h" //59
#include "assets/high/fish/e2.h" //60
#include "assets/high/fish/e3.h" //61
#include "assets/high/fish/h1.h" //62
//*************************************
// globals
//*************************************
const char appTitle[]="Tux Fishing";
GLFWwindow* window;
uint winw=1024, winh=768;
float t=0.f, dt=0.f, lt=0.f, fc=0.f, lfct=0.f, aspect;
// render state
mat projection, view, model, modelview;
// camera vars
uint focus_cursor = 0;
double sens = 0.003;
double mx,my,lx,ly;
float xrot = d2PI;
float yrot = 1.3f;
float dzoom = -0.3f;
float zoom = -3.3f;
// game vars
#define FAR_DISTANCE 16.f
uint ks[3]; // is rotate key pressed toggle
uint cast = 0; // is casting toggle
uint caught = 0; // total fish caught
float woff = 0.f; // wave offset
float pr = 0.f; // player rotation (yaw)
float rodr = 0.f; // fishing rod rotation (pitch)
vec fp = (vec){0.f, 0.f, 0.f}; // float position
float frx=0.f, fry=0.f, frr=0.f; // float return direction
int hooked = -1; // is a fish hooked, if so, its the ID of the fish.
float next_wild_fish = 0.f; // time for next wild fish discovery
int last_fish[2]={0}; // last fish id's shown in boat
uint lfi=0; // last fish index
float winning_fish = 0.f; // winning animation fish timer
uint winning_fish_id = 0; // winning fish id
uint what=0; // wearing hat?
float shoal_x[3]; // position of shoal
float shoal_y[3]; // position of shoal
uint shoal_lfi[3];// last fish id that jumped
float shoal_nt[3];// next shoal jump time
float shoal_r1[3];// jump rots
float shoal_r2[3];
float shoal_r3[3];
float caught_list[56]={0}; // tally of fish types caught
//*************************************
// utility functions
//*************************************
void timestamp(char* ts)
{
const time_t tt = time(0);
strftime(ts, 16, "%H:%M:%S", localtime(&tt));
}
void updateModelView()
{
mMul(&modelview, &model, &view);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (float*)&modelview.m[0][0]);
}
//*************************************
// game functions
//*************************************
uint ratioCaught()
{
uint r = 0;
for(uint i=0; i<56; i++){if(caught_list[i] == 1){r++;}}
return r;
}
void rndShoalPos(uint i)
{
const float ra = esRandFloat(-PI, PI);
const float rr = esRandFloat(2.3f, 3.6f);
shoal_x[i] = sinf(ra)*rr;
shoal_y[i] = cosf(ra)*rr;
if(what == 1){shoal_lfi[i] = (int)roundf(esRandFloat(7.f, 61.f));}
else {shoal_lfi[i] = (int)roundf(esRandFloat(7.f, 62.f));}
shoal_nt[i] = t + esRandFloat(6.5f, 16.f);
}
void resetGame(uint mode)
{
cast=0;
pr=0.f;
rodr=0.f;
fp=(vec){0.f, 0.f, 0.f};
frx=0.f;
fry=0.f;
frr=0.f;
hooked=-1;
last_fish[0]=-1;
last_fish[1]=-1;
lfi=0;
winning_fish=0.f;
winning_fish_id=0;
what=0;
next_wild_fish=t+esRandFloat(23.f,180.f);
caught=0;
//for(uint i=0; i<56; i++){caught_list[i]=0;}
memset(&caught_list[0], 0x00, sizeof(float)*56);
rndShoalPos(0);
rndShoalPos(1);
rndShoalPos(2);
if(mode == 1)
{
char strts[16];
timestamp(&strts[0]);
printf("[%s] Game Reset.\n", strts);
}
glfwSetWindowTitle(window, appTitle);
}
float getWaterHeight(float x, float y)
{
const uint imax = water_numvert*3;
int ci = -1;
float cid = 9999.f;
for(uint i=0; i < imax; i+=3)
{
const float xm = water_vertices[i] - x;
const float ym = water_vertices[i+1] - y;
const float nd = xm*xm + ym*ym;
if(nd < cid)
{
ci = i;
cid = nd;
}
}
if(ci != -1)
{
return water_vertices[ci+2];
}
return woff;
}
//*************************************
// update & render
//*************************************
void main_loop()
{
//*************************************
// core logic
//*************************************
fc++;
glfwPollEvents();
t = fTime();
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
//*************************************
// inputs
if(hooked == -1)
{
if(ks[0] == 1){pr -= 1.6f*dt;fp=(vec){0.f, 0.f, 0.f};}
if(ks[1] == 1){pr += 1.6f*dt;fp=(vec){0.f, 0.f, 0.f};}
if(ks[2] == 1){if(fabsf(pr-xrot) > 0.001f){pr += -(pr+xrot+d2PI)*0.016f;fp=(vec){0.f, 0.f, 0.f};}}
if(cast > 0)
{
if(rodr < 2.f){rodr += 1.5f*dt;}
const float trodr = (rodr+0.2f)*1.65f;
frx = sinf(pr+d2PI), fry = cosf(pr+d2PI), frr = -d2PI+pr;
fp.x = frx*trodr, fp.y = fry*trodr;
fp.z = getWaterHeight(fp.x, fp.y);
if(cast == 2){if(fabsf(pr-xrot) > 0.001f){pr += -(pr+xrot+d2PI)*0.016f;}}
}
else{if(rodr > 0.f){rodr -= 9.f*dt;}}
if(rodr < 0.f){rodr = 0.f;}
}
// water offset
woff = sinf(t*0.42f);
// camera
if(focus_cursor == 1)
{
glfwGetCursorPos(window, &mx, &my);
xrot += (float)((lx-mx)*sens);
yrot += (float)((ly-my)*sens);
if(yrot > 1.5f){yrot = 1.5f;}
if(yrot < 0.5f){yrot = 0.5f;}
lx = mx, ly = my;
}
mIdent(&view);
dzoom += (zoom-dzoom)*3.f*dt;
mSetPos(&view, (vec){0.f, -0.13f, dzoom});
mRotate(&view, yrot, 1.f, 0.f, 0.f);
mRotate(&view, xrot, 0.f, 0.f, 1.f);
//*************************************
// render
//*************************************
// clear buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
///
// render sky
shadeFullbright(&position_id, &projection_id, &modelview_id, &color_id, &lightness_id, &opacity_id);
glUniformMatrix4fv(projection_id, 1, GL_FALSE, (float*)&projection.m[0][0]);
glUniform1f(lightness_id, 1.f);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (float*)&view.m[0][0]);
esBindRenderF(0);
// render water
mIdent(&model);
mSetPos(&model, (vec){0.f, 0.f, 0.f});
mScale(&model, 1.f, 1.f, woff);
updateModelView();
esBindRenderF(1);
// glEnable(GL_BLEND);
// glUniform1f(opacity_id, 0.5f);
// mIdent(&model);
// mSetPos(&model, (vec){0.f, 0.f, 0.01f});
// mScale(&model, 1.f, 1.f, woff);
// updateModelView();
// esBindRenderF(1);
// glDisable(GL_BLEND);
// shade lambert
shadeLambert(&position_id, &projection_id, &modelview_id, &lightpos_id, &normal_id, &color_id, &ambient_id, &saturate_id, &opacity_id);
glUniformMatrix4fv(projection_id, 1, GL_FALSE, (float*)&projection.m[0][0]);
glUniform1f(ambient_id, 0.4f);
glUniform1f(saturate_id, 0.5f);
// render boat
mIdent(&model);
mSetPos(&model, (vec){0.f, 0.f, woff*-0.026f});
updateModelView();
esBindRender(2);
// render last catch(es)
if(last_fish[0] != -1)
{
mIdent(&model);
mSetPos(&model, (vec){0.f, -0.14f, 0.04f+(woff*-0.026f)});
updateModelView();
esBindRender(last_fish[0]);
}
if(last_fish[1] != -1)
{
mIdent(&model);
mSetPos(&model, (vec){0.02f, 0.2f, 0.05f+(woff*-0.026f)});
mRotZ(&model, 90.f*DEG2RAD);
updateModelView();
esBindRender(last_fish[1]);
}
// render tux
mIdent(&model);
mSetPos(&model, (vec){0.f, 0.f, woff*-0.026f});
mRotZ(&model, pr);
updateModelView();
esBindRender(3);
// render hat
if(what == 1)
{
mIdent(&model);
mSetPos(&model, (vec){0.f, 0.f, 0.196787f+(woff*-0.026f)});
mRotZ(&model, pr);
updateModelView();
esBindRender(62);
}
// render rod
mIdent(&model);
mSetPos(&model, (vec){0.f, 0.f, 0.125378f+(woff*-0.026f)});
mRotZ(&model, pr);
mRotX(&model, rodr);
updateModelView();
esBindRender(4);
// render float
if(fp.x != 0.f || fp.y != 0.f || fp.z != 0.f)
{
// is a fish hooked?
if(hooked != -1)
{
// reel it in
rodr = 0.8f;
const float rs = 0.32f*dt;
const float n1 = -fp.x*0.3f*dt;
const float n2 = -frx*rs;
const float o1 = -fp.y*0.3f*dt;
const float o2 = -fry*rs;
const float x1 = n2+o2, x2 = n1+o1;
if(x1*x1 < x2*x2)
{
fp.x += n1;
fp.y += o1;
}
else
{
fp.x += n2;
fp.y += o2;
}
fp.z = getWaterHeight(fp.x, fp.y);
// fp.x += -frx*rs;
// fp.y += -fry*rs;
// const float wh = getWaterHeight(fp.x, fp.y);
//if(fabsf(wh-fp.z) > 0.03f){fp.z=wh;} // lol
if(vMag(fp) < 0.1f)
{
winning_fish = t+4.f;
winning_fish_id = hooked;
fp = (vec){0.f, 0.f, 0.f};
if(hooked == 62){what=1;}else
{
last_fish[lfi] = hooked;
if(++lfi > 1){lfi=0;}
}
caught_list[hooked-7] = 1;
hooked = -1;
caught++;
char strts[16];
timestamp(&strts[0]);
printf("[%s] Fish Caught: %u (%u/56)\n", strts, caught, ratioCaught());
char tmp[256];
sprintf(tmp, "Tux 🐟 %u (%u/56) 🐟 Fishing", caught, ratioCaught());
glfwSetWindowTitle(window, tmp);
}
else
{
// render fish
mIdent(&model);
mSetPos(&model, (vec){fp.x, fp.y, fp.z*woff});
mRotZ(&model, frr);
updateModelView();
esBindRender(hooked);
}
}
else
{
if(t > next_wild_fish)
{
const float rc = esRandFloat(0.f, 100.f);
if(rc < 50.f) {hooked = (int)roundf(esRandFloat( 7.f, 21.f));}
else if(rc < 80.f){hooked = (int)roundf(esRandFloat(22.f, 34.f));}
else if(rc < 90.f){hooked = (int)roundf(esRandFloat(35.f, 46.f));}
else if(rc < 97.f){hooked = (int)roundf(esRandFloat(47.f, 58.f));}
else{hooked = 59;}
//hooked = (int)roundf(esRandFloat(7.f, 59.f));
next_wild_fish = t + esRandFloat(23.f, 180.f);
}
if(cast > 0){glEnable(GL_BLEND);glUniform1f(opacity_id, 0.5f);}
mIdent(&model);
mSetPos(&model, (vec){fp.x, fp.y, fp.z*woff});
updateModelView();
esBindRender(5);
if(cast > 0){glDisable(GL_BLEND);}
}
}
// render jumping fish
for(uint i=0; i<3; i++)
{
if(hooked == -1)
{
const float xm = fp.x - shoal_x[i];
const float ym = fp.y - shoal_y[i];
const float nd = xm*xm + ym*ym;
if(nd < 0.3f)
{
//printf("[%u] %f %f\n", i, nd, shoal_nt[i]);
if(shoal_nt[i]-t < -4.5f){hooked = shoal_lfi[i];}
}
}
if(shoal_nt[i]-t < -11.f){rndShoalPos(i);}
const float d = shoal_nt[i]-t;
if(d < 0.f && d >= -1.5f)
{
const float z = -0.03f+(0.33f*(fabsf(d)/1.5f));
const float wah = (getWaterHeight(shoal_x[i], shoal_y[i])*woff)-0.016f;
mIdent(&model);
mSetPos(&model, (vec){shoal_x[i], shoal_y[i], wah});
mRotZ(&model, t*0.3f);
updateModelView();
esBindRender(6);
mIdent(&model);
mSetPos(&model, (vec){shoal_x[i], shoal_y[i], z});
shoal_r1[i] += esRandFloat(0.1f, 0.6f)*dt;
shoal_r2[i] += esRandFloat(0.1f, 0.6f)*dt;
shoal_r3[i] += esRandFloat(0.1f, 0.6f)*dt;
mRotX(&model, shoal_r1[i]);
mRotY(&model, shoal_r2[i]);
mRotZ(&model, shoal_r3[i]);
updateModelView();
esBindRender(shoal_lfi[i]);
}
else if(d > -2.5f && d < -1.5f)
{
const float wah = (getWaterHeight(shoal_x[i], shoal_y[i])*woff)-0.016f;
mIdent(&model);
mSetPos(&model, (vec){shoal_x[i], shoal_y[i], wah});
mRotZ(&model, t*0.3f);
updateModelView();
esBindRender(6);
mIdent(&model);
mSetPos(&model, (vec){shoal_x[i], shoal_y[i], 0.3f});
shoal_r1[i] += esRandFloat(0.1f, 0.6f)*dt;
shoal_r2[i] += esRandFloat(0.1f, 0.6f)*dt;
shoal_r3[i] += esRandFloat(0.1f, 0.6f)*dt;
mRotX(&model, shoal_r1[i]);
mRotY(&model, shoal_r2[i]);
mRotZ(&model, shoal_r3[i]);
updateModelView();
esBindRender(shoal_lfi[i]);
}
else if(d > -5.5f && d < -2.5f)
{
const float z = 0.3f-(0.303f*(fabsf(d+2.5f)/1.5f));
const float wah = (getWaterHeight(shoal_x[i], shoal_y[i])*woff)-0.016f;
glEnable(GL_BLEND);
glUniform1f(opacity_id, d+4.5f);
mIdent(&model);
mSetPos(&model, (vec){shoal_x[i], shoal_y[i], wah});
mRotZ(&model, t*0.3f);
updateModelView();
esBindRender(6);
glDisable(GL_BLEND);
mIdent(&model);
mSetPos(&model, (vec){shoal_x[i], shoal_y[i], z});
shoal_r1[i] += esRandFloat(0.1f, 0.6f)*dt;
shoal_r2[i] += esRandFloat(0.1f, 0.6f)*dt;
shoal_r3[i] += esRandFloat(0.1f, 0.6f)*dt;
mRotX(&model, shoal_r1[i]);
mRotY(&model, shoal_r2[i]);
mRotZ(&model, shoal_r3[i]);
updateModelView();
esBindRender(shoal_lfi[i]);
}
}
// render winning fish
if(winning_fish > t)
{
const float d = winning_fish - t;
if(d < 1.f)
{
glEnable(GL_BLEND);
glUniform1f(opacity_id, d);
mIdent(&model);
mSetPos(&model, (vec){0.f, 0.f, 0.37f});
mScale1(&model, 3.f);
mRotZ(&model, t*2.1f);
updateModelView();
esBindRender(winning_fish_id);
glDisable(GL_BLEND);
}
else
{
mIdent(&model);
mSetPos(&model, (vec){0.f, 0.f, 0.37f});
mScale1(&model, 3.f);
mRotZ(&model, t*2.1f);
updateModelView();
esBindRender(winning_fish_id);
}
}
///
// display render
glfwSwapBuffers(window);
}
//*************************************
// input
//*************************************
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if(action == GLFW_PRESS)
{
if( key == GLFW_KEY_LEFT || key == GLFW_KEY_A){ks[0]=1;}
else if(key == GLFW_KEY_RIGHT || key == GLFW_KEY_D){ks[1]=1;}
else if(key == GLFW_KEY_SPACE) { cast=1;
next_wild_fish = t + esRandFloat(23.f, 180.f);}
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(key == GLFW_KEY_E)
// {
// shoal_nt[0] = t;
// shoal_lfi[0] = 62;
// //hooked = (int)roundf(esRandFloat(7.f, 59.f));
// }
// else if(key == GLFW_KEY_R) // reset game
// {
// resetGame(1);
// }
else if(key == GLFW_KEY_ESCAPE)
{
focus_cursor = 0;
#ifndef WEB
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
glfwGetCursorPos(window, &lx, &ly);
#endif
}
}
else if(action == GLFW_RELEASE)
{
if( key == GLFW_KEY_LEFT || key == GLFW_KEY_A){ks[0]=0;}
else if(key == GLFW_KEY_RIGHT || key == GLFW_KEY_D){ks[1]=0;}
else if(key == GLFW_KEY_SPACE) { cast=0;}
}
}
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
{
if(yoffset < 0.0){zoom += 0.12f * zoom;}else{zoom -= 0.12f * zoom;}
if(zoom > -0.73f){zoom = -0.73f;}else if(zoom < -5.f){zoom = -5.f;}
}
void mouse_button_callback(GLFWwindow* window, int button, int action, int mods)
{
if(action == GLFW_PRESS)
{
if(button == GLFW_MOUSE_BUTTON_LEFT)
{
if(focus_cursor == 0)
{
focus_cursor = 1;
#ifndef WEB
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwGetCursorPos(window, &lx, &ly);
#endif
}
else if(hooked == -1)
{
cast=2;
next_wild_fish = t + esRandFloat(23.f, 180.f);
}
}
else if(button == GLFW_MOUSE_BUTTON_RIGHT)
{
if(hooked == -1)
{
ks[2]=1;
next_wild_fish = t + esRandFloat(23.f, 180.f);
}
}
}
else if(action == GLFW_RELEASE)
{
if(button == GLFW_MOUSE_BUTTON_LEFT){cast=0;}
else if(button == GLFW_MOUSE_BUTTON_RIGHT){ks[2]=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;
mIdent(&projection);
mPerspective(&projection, 30.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;
}
long etse=0;
EM_BOOL emscripten_touchstart_event(int eventType, const EmscriptenTouchEvent *touchEvent, void *userData)
{
if(hooked == -1 && touchEvent->numTouches > 0)
{
cast=2;
etse=touchEvent->touches[0].clientX;
next_wild_fish=t+esRandFloat(23.f, 180.f);
}
return EM_FALSE;
}
EM_BOOL emscripten_touchmove_event(int eventType, const EmscriptenTouchEvent *touchEvent, void *userData)
{
if(touchEvent->numTouches > 0){xrot+=(((float)(etse-touchEvent->touches[0].clientX))/((float)winw))*0.032f;}
return EM_FALSE;
}
#endif
//*************************************
// process entry point
//*************************************
int main(int argc, char** argv)
{
int msaa = 16;
#ifdef WEB
focus_cursor = 0;
#endif
// help
printf("----\n");
printf("James William Fletcher (github.com/mrbid)\n");
printf("%s - 3D Fishing Game, with 55 species of fish!\n", appTitle);
printf("----\n");
#ifndef WEB
// allow custom msaa level
if(argc >= 2){msaa = atoi(argv[1]);}
printf("One command line argument, msaa 0-16.\n");
printf("e.g; ./tuxfishing 16\n");
printf("----\n");
#endif
printf("!! Press ESCAPE to release mouse lock.\n");
printf("Mouse = Rotate Camera, Scroll = Zoom Camera\n");
printf("A,D / Arrows / Right Click = Move Rod Cast Direction\n");
printf("Space / Left Click = Cast Rod, the higher the rod when you release space the farther the lure launches.\n");
printf("If you see a fish jump out of the water throw a lure after it and you will catch it straight away.\n");
printf("F = FPS to console.\n");
//printf("R = Reset game.\n");
printf("----\n");
printf("All assets generated using LUMA GENIE (https://lumalabs.ai/genie).\n");
printf("----\n");
printf("Tux made by Andy Cuccaro\n");
printf("https://andycuccaro.gumroad.com/\n");
printf("----\n");
printf("Fishing Rod made by Shedmon\n");
printf("https://sketchfab.com/shedmon\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
if(focus_cursor == 1)
{
glfwGetCursorPos(window, &lx, &ly);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
}
#endif
glfwSetWindowSizeCallback(window, window_size_callback);
glfwSetKeyCallback(window, key_callback);
glfwSetMouseButtonCallback(window, mouse_button_callback);
glfwSetScrollCallback(window, scroll_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
//*************************************
register_sky();
register_water();
register_boat();
register_tux();
register_rod();
register_float();
register_splash();
register_a0();
register_a1();
register_a2();
register_a3();
register_a4();
register_a5();
register_a6();
register_a7();
register_a8();
register_a9();
register_a10();
register_a11();
register_a12();
register_a13();
register_a14();
register_b0();
register_b1();
register_b2();
register_b3();
register_b4();
register_b5();
register_b6();
register_b7();
register_b8();
register_b9();
register_b10();
register_b11();
register_b12();
register_c0();
register_c1();
register_c2();
register_c3();
register_c4();
register_c5();
register_c6();
register_c7();
register_c8();
register_c9();
register_c10();
register_c11();
register_d0();
register_d1();
register_d2();
register_d3();
register_d4();
register_d5();
register_d6();
register_d7();
register_d8();
register_d9();
register_d10();
register_d11();
register_e1();
register_e2();
register_e3();
register_h1();
//*************************************
// configure render options
//*************************************
makeLambert();
makeFullbright();
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glClearColor(0.f, 0.f, 0.f, 0.f);
shadeFullbright(&position_id, &projection_id, &modelview_id, &color_id, &lightness_id, &opacity_id);
glUniformMatrix4fv(projection_id, 1, GL_FALSE, (float*)&projection.m[0][0]);
window_size_callback(window, winw, winh);
#ifdef GL_DEBUG
esDebug(1);
#endif
//*************************************
// execute update / render loop
//*************************************
// init
srand(time(0));
srandf(time(0));
t = fTime();
lfct = t;
// game init
resetGame(0);
// loop
#ifdef WEB
emscripten_set_touchstart_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, NULL, EM_FALSE, emscripten_touchstart_event);
emscripten_set_touchmove_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, NULL, EM_FALSE, emscripten_touchmove_event);
emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, NULL, EM_FALSE, emscripten_resize_event);
emscripten_set_main_loop(main_loop, 0, 1);
#else
while(!glfwWindowShouldClose(window)){main_loop();}
#endif
// done
glfwDestroyWindow(window);
glfwTerminate();
exit(EXIT_SUCCESS);
return 0;
}