12
12
#undef RAYGUI_IMPLEMENTATION
13
13
14
14
#define ARRAY_SIZE (x ) (sizeof(x) / sizeof(x[0]))
15
+ #define MIN (a , b ) ((a)<(b)? (a) : (b))
15
16
16
17
typedef enum EquationType
17
18
{
@@ -500,6 +501,8 @@ const int screenWidth = 800;
500
501
const int screenHeight = 450 ;
501
502
Scene scene ;
502
503
Savegame save ;
504
+ RenderTexture2D screen ;
505
+ float scale = 1.0f ;
503
506
504
507
void menu (void );
505
508
void tutorial (void );
@@ -512,10 +515,17 @@ void level_draw(GameState *state);
512
515
513
516
int main (void )
514
517
{
518
+ SetConfigFlags (FLAG_WINDOW_RESIZABLE | FLAG_VSYNC_HINT );
515
519
InitWindow (screenWidth , screenHeight , "A puzzling tower defense game for beautiful math nerds." );
520
+ SetWindowMinSize (screenWidth , screenHeight );
516
521
517
522
SetTargetFPS (60 );
518
523
SetExitKey (0 ); // disable close on ESC
524
+
525
+ // Render texture initialization, used to hold the rendering result so we can easily resize it
526
+ screen = LoadRenderTexture (screenWidth , screenHeight );
527
+ assert (screen .id != 0 );
528
+ SetTextureFilter (screen .texture , TEXTURE_FILTER_BILINEAR ); // Texture scale filter to use
519
529
520
530
load_progress (& save , SAVE_FILE );
521
531
@@ -561,12 +571,39 @@ int main(void)
561
571
562
572
state_free (& state );
563
573
574
+ UnloadRenderTexture (screen );
575
+
564
576
// De-Initialization
565
577
CloseWindow ();
566
578
567
579
return 0 ;
568
580
}
569
581
582
+ void UpdateGlobalScaling ()
583
+ {
584
+ if (!IsWindowResized ())
585
+ return ;
586
+
587
+ scale = MIN ((float )GetScreenWidth ()/screenWidth , (float )GetScreenHeight ()/screenHeight );
588
+
589
+ SetMouseOffset (- (GetScreenWidth () - (screenWidth * scale ))* 0.5f , - (GetScreenHeight () - (screenHeight * scale ))* 0.5f );
590
+ SetMouseScale (1 /scale , 1 /scale );
591
+ }
592
+
593
+ void DrawScreenScaled ()
594
+ {
595
+ BeginDrawing ();
596
+
597
+ ClearBackground (BLACK ); // Clear screen background
598
+
599
+ // Draw render texture to screen, properly scaled
600
+ DrawTexturePro (screen .texture , (Rectangle ){ 0.0f , 0.0f , (float )screenWidth , (float )- screenHeight },
601
+ (Rectangle ){ (GetScreenWidth () - ((float )screenWidth * scale ))* 0.5f , (GetScreenHeight () - ((float )screenHeight * scale ))* 0.5f ,
602
+ (float )screenWidth * scale , (float )screenHeight * scale }, (Vector2 ){ 0 , 0 }, 0.0f , WHITE );
603
+
604
+ EndDrawing ();
605
+ }
606
+
570
607
void menu (void )
571
608
{
572
609
bool sceneChange = false;
@@ -580,7 +617,10 @@ void menu(void)
580
617
// Main game loop
581
618
while (!WindowShouldClose () && !sceneChange ) // Detect window close button or ESC key
582
619
{
583
- BeginDrawing ();
620
+ UpdateGlobalScaling ();
621
+
622
+ // Draw onto texture unscaled
623
+ BeginTextureMode (screen );
584
624
585
625
ClearBackground (LIGHTGRAY );
586
626
@@ -637,7 +677,9 @@ void menu(void)
637
677
638
678
GuiUnlock ();
639
679
640
- EndDrawing ();
680
+ EndTextureMode ();
681
+
682
+ DrawScreenScaled ();
641
683
}
642
684
}
643
685
@@ -657,14 +699,16 @@ void tutorial(void)
657
699
// Main game loop
658
700
while (!WindowShouldClose () && !sceneChange ) // Detect window close button or ESC key
659
701
{
702
+ UpdateGlobalScaling ();
703
+
660
704
if (IsKeyPressed (KEY_ESCAPE ))
661
705
{
662
706
scene = SC_MENU ;
663
707
sceneChange = true;
664
708
break ;
665
709
}
666
710
667
- BeginDrawing ( );
711
+ BeginTextureMode ( screen );
668
712
669
713
ClearBackground (LIGHTGRAY );
670
714
@@ -704,7 +748,9 @@ void tutorial(void)
704
748
705
749
GuiUnlock ();
706
750
707
- EndDrawing ();
751
+ EndTextureMode ();
752
+
753
+ DrawScreenScaled ();
708
754
}
709
755
}
710
756
@@ -717,14 +763,16 @@ void level_select(GameState *state)
717
763
// Main game loop
718
764
while (!WindowShouldClose () && !sceneChange ) // Detect window close button or ESC key
719
765
{
766
+ UpdateGlobalScaling ();
767
+
720
768
if (IsKeyPressed (KEY_ESCAPE ))
721
769
{
722
770
scene = SC_MENU ;
723
771
sceneChange = true;
724
772
break ;
725
773
}
726
774
727
- BeginDrawing ( );
775
+ BeginTextureMode ( screen );
728
776
729
777
ClearBackground (LIGHTGRAY );
730
778
@@ -791,7 +839,9 @@ void level_select(GameState *state)
791
839
792
840
GuiUnlock ();
793
841
794
- EndDrawing ();
842
+ EndTextureMode ();
843
+
844
+ DrawScreenScaled ();
795
845
}
796
846
}
797
847
@@ -827,6 +877,8 @@ void level(GameState *state)
827
877
// Main game loop
828
878
while (!WindowShouldClose () && !sceneChange )
829
879
{
880
+ UpdateGlobalScaling ();
881
+
830
882
// ------------------ Input ------------------
831
883
if (IsKeyPressed (KEY_ESCAPE ))
832
884
{
@@ -918,7 +970,7 @@ void level(GameState *state)
918
970
}
919
971
920
972
// ------------------ Draw ------------------
921
- BeginDrawing ( );
973
+ BeginTextureMode ( screen );
922
974
923
975
ClearBackground (LIGHTGRAY );
924
976
@@ -1089,7 +1141,9 @@ void level(GameState *state)
1089
1141
}
1090
1142
}
1091
1143
1092
- EndDrawing ();
1144
+ EndTextureMode ();
1145
+
1146
+ DrawScreenScaled ();
1093
1147
}
1094
1148
}
1095
1149
@@ -1319,6 +1373,8 @@ void playground(GameState *state)
1319
1373
// Main game loop
1320
1374
while (!WindowShouldClose () && !sceneChange )
1321
1375
{
1376
+ UpdateGlobalScaling ();
1377
+
1322
1378
// ------------------ Input ------------------
1323
1379
if (IsKeyPressed (KEY_ESCAPE ))
1324
1380
{
@@ -1383,7 +1439,7 @@ void playground(GameState *state)
1383
1439
}
1384
1440
1385
1441
// ------------------ Draw ------------------
1386
- BeginDrawing ( );
1442
+ BeginTextureMode ( screen );
1387
1443
1388
1444
ClearBackground (LIGHTGRAY );
1389
1445
@@ -1575,6 +1631,8 @@ void playground(GameState *state)
1575
1631
#endif
1576
1632
1577
1633
GuiUnlock ();
1578
- EndDrawing ();
1634
+ EndTextureMode ();
1635
+
1636
+ DrawScreenScaled ();
1579
1637
}
1580
1638
}
0 commit comments