Skip to content

Commit 17b9267

Browse files
committed
Add window scaling
1 parent d33f12d commit 17b9267

File tree

2 files changed

+69
-10
lines changed

2 files changed

+69
-10
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
build/*
22
*.pdb
3+
/save.me

src/main.c

+68-10
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#undef RAYGUI_IMPLEMENTATION
1313

1414
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
15+
#define MIN(a, b) ((a)<(b)? (a) : (b))
1516

1617
typedef enum EquationType
1718
{
@@ -500,6 +501,8 @@ const int screenWidth = 800;
500501
const int screenHeight = 450;
501502
Scene scene;
502503
Savegame save;
504+
RenderTexture2D screen;
505+
float scale = 1.0f;
503506

504507
void menu(void);
505508
void tutorial(void);
@@ -512,10 +515,17 @@ void level_draw(GameState *state);
512515

513516
int main(void)
514517
{
518+
SetConfigFlags(FLAG_WINDOW_RESIZABLE | FLAG_VSYNC_HINT);
515519
InitWindow(screenWidth, screenHeight, "A puzzling tower defense game for beautiful math nerds.");
520+
SetWindowMinSize(screenWidth, screenHeight);
516521

517522
SetTargetFPS(60);
518523
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
519529

520530
load_progress(&save, SAVE_FILE);
521531

@@ -561,12 +571,39 @@ int main(void)
561571

562572
state_free(&state);
563573

574+
UnloadRenderTexture(screen);
575+
564576
// De-Initialization
565577
CloseWindow();
566578

567579
return 0;
568580
}
569581

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+
570607
void menu(void)
571608
{
572609
bool sceneChange = false;
@@ -580,7 +617,10 @@ void menu(void)
580617
// Main game loop
581618
while (!WindowShouldClose() && !sceneChange) // Detect window close button or ESC key
582619
{
583-
BeginDrawing();
620+
UpdateGlobalScaling();
621+
622+
// Draw onto texture unscaled
623+
BeginTextureMode(screen);
584624

585625
ClearBackground(LIGHTGRAY);
586626

@@ -637,7 +677,9 @@ void menu(void)
637677

638678
GuiUnlock();
639679

640-
EndDrawing();
680+
EndTextureMode();
681+
682+
DrawScreenScaled();
641683
}
642684
}
643685

@@ -657,14 +699,16 @@ void tutorial(void)
657699
// Main game loop
658700
while (!WindowShouldClose() && !sceneChange) // Detect window close button or ESC key
659701
{
702+
UpdateGlobalScaling();
703+
660704
if (IsKeyPressed(KEY_ESCAPE))
661705
{
662706
scene = SC_MENU;
663707
sceneChange = true;
664708
break;
665709
}
666710

667-
BeginDrawing();
711+
BeginTextureMode(screen);
668712

669713
ClearBackground(LIGHTGRAY);
670714

@@ -704,7 +748,9 @@ void tutorial(void)
704748

705749
GuiUnlock();
706750

707-
EndDrawing();
751+
EndTextureMode();
752+
753+
DrawScreenScaled();
708754
}
709755
}
710756

@@ -717,14 +763,16 @@ void level_select(GameState *state)
717763
// Main game loop
718764
while (!WindowShouldClose() && !sceneChange) // Detect window close button or ESC key
719765
{
766+
UpdateGlobalScaling();
767+
720768
if (IsKeyPressed(KEY_ESCAPE))
721769
{
722770
scene = SC_MENU;
723771
sceneChange = true;
724772
break;
725773
}
726774

727-
BeginDrawing();
775+
BeginTextureMode(screen);
728776

729777
ClearBackground(LIGHTGRAY);
730778

@@ -791,7 +839,9 @@ void level_select(GameState *state)
791839

792840
GuiUnlock();
793841

794-
EndDrawing();
842+
EndTextureMode();
843+
844+
DrawScreenScaled();
795845
}
796846
}
797847

@@ -827,6 +877,8 @@ void level(GameState *state)
827877
// Main game loop
828878
while (!WindowShouldClose() && !sceneChange)
829879
{
880+
UpdateGlobalScaling();
881+
830882
// ------------------ Input ------------------
831883
if (IsKeyPressed(KEY_ESCAPE))
832884
{
@@ -918,7 +970,7 @@ void level(GameState *state)
918970
}
919971

920972
// ------------------ Draw ------------------
921-
BeginDrawing();
973+
BeginTextureMode(screen);
922974

923975
ClearBackground(LIGHTGRAY);
924976

@@ -1089,7 +1141,9 @@ void level(GameState *state)
10891141
}
10901142
}
10911143

1092-
EndDrawing();
1144+
EndTextureMode();
1145+
1146+
DrawScreenScaled();
10931147
}
10941148
}
10951149

@@ -1319,6 +1373,8 @@ void playground(GameState *state)
13191373
// Main game loop
13201374
while (!WindowShouldClose() && !sceneChange)
13211375
{
1376+
UpdateGlobalScaling();
1377+
13221378
// ------------------ Input ------------------
13231379
if (IsKeyPressed(KEY_ESCAPE))
13241380
{
@@ -1383,7 +1439,7 @@ void playground(GameState *state)
13831439
}
13841440

13851441
// ------------------ Draw ------------------
1386-
BeginDrawing();
1442+
BeginTextureMode(screen);
13871443

13881444
ClearBackground(LIGHTGRAY);
13891445

@@ -1575,6 +1631,8 @@ void playground(GameState *state)
15751631
#endif
15761632

15771633
GuiUnlock();
1578-
EndDrawing();
1634+
EndTextureMode();
1635+
1636+
DrawScreenScaled();
15791637
}
15801638
}

0 commit comments

Comments
 (0)