From 24f07aaf5a58aa1fc1d18a9eae4327f5a3f58014 Mon Sep 17 00:00:00 2001 From: Ray Date: Thu, 11 Apr 2019 17:40:13 +0200 Subject: [PATCH] new example: core_window_scale_letterbox --- examples/core/core_window_scale_letterbox.c | 88 ++++++++++++++++++ examples/core/core_window_scale_letterbox.png | Bin 0 -> 17967 bytes 2 files changed, 88 insertions(+) create mode 100644 examples/core/core_window_scale_letterbox.c create mode 100644 examples/core/core_window_scale_letterbox.png diff --git a/examples/core/core_window_scale_letterbox.c b/examples/core/core_window_scale_letterbox.c new file mode 100644 index 00000000..ea4a375a --- /dev/null +++ b/examples/core/core_window_scale_letterbox.c @@ -0,0 +1,88 @@ +/******************************************************************************************* +* +* raylib [core] example - window scale letterbox +* +* This example has been created using raylib 2.5 (www.raylib.com) +* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) +* +* Copyright (c) 2019 Anata and Ramon Santamaria (@raysan5) +* +********************************************************************************************/ + +#include "raylib.h" + +#define max(a, b) ((a)>(b)? (a) : (b)) +#define min(a, b) ((a)<(b)? (a) : (b)) + +int main() +{ + const int windowWidth = 800; + const int windowHeight = 450; + + // Enable config flags for resizable window and vertical synchro + SetConfigFlags(FLAG_WINDOW_RESIZABLE | FLAG_VSYNC_HINT); + InitWindow(windowWidth, windowHeight, "raylib [core] example - window scale letterbox"); + SetWindowMinSize(320, 240); + + int gameScreenWidth = 640; + int gameScreenHeight = 480; + + // Render texture initialization + RenderTexture2D target = LoadRenderTexture(gameScreenWidth, gameScreenHeight); + SetTextureFilter(target.texture, FILTER_BILINEAR); // Texture scale filter to use + + Color colors[10] = { 0 }; + for (int i = 0; i < 10; i++) colors[i] = (Color){ GetRandomValue(100, 250), GetRandomValue(50, 150), GetRandomValue(10, 100), 255 }; + + SetTargetFPS(60); + //-------------------------------------------------------------------------------------- + + // Main game loop + while( !WindowShouldClose() ) // Detect window close button or ESC key + { + // Update + //---------------------------------------------------------------------------------- + // Compute required framebuffer scaling + float scale = min((float)GetScreenWidth()/gameScreenWidth, (float)GetScreenHeight()/gameScreenHeight); + + if (IsKeyPressed(KEY_SPACE)) + { + // Recalculate random colors for the bars + for (int i = 0; i < 10; i++) colors[i] = (Color){ GetRandomValue(100, 250), GetRandomValue(50, 150), GetRandomValue(10, 100), 255 }; + } + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + ClearBackground(BLACK); + + // Draw everything in the render texture + BeginTextureMode(target); + + ClearBackground(RAYWHITE); // Clear render texture background color + + for (int i = 0; i < 10; i++) DrawRectangle(0, (gameScreenHeight/10)*i, gameScreenWidth, gameScreenHeight/10, colors[i]); + + DrawText("You can resize the window,\nand see the screen scaling!", 10, 25, 20, WHITE); + + EndTextureMode(); + + // Draw RenderTexture2D to window, properly scaled + DrawTexturePro(target.texture, (Rectangle){ 0.0f, 0.0f, (float)target.texture.width, (float)-target.texture.height }, + (Rectangle){ (GetScreenWidth() - ((float)gameScreenWidth*scale))*0.5, (GetScreenHeight() - ((float)gameScreenHeight*scale))*0.5, + (float)gameScreenWidth*scale, (float)gameScreenHeight*scale }, (Vector2){ 0, 0 }, 0.0f, WHITE); + + EndDrawing(); + //-------------------------------------------------------------------------------------- + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + UnloadRenderTexture(target); // Unload render texture + + CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- + + return 0; +} diff --git a/examples/core/core_window_scale_letterbox.png b/examples/core/core_window_scale_letterbox.png new file mode 100644 index 0000000000000000000000000000000000000000..5acf2d7cc28aa8c2c465f71cf748b755fd8b042e GIT binary patch literal 17967 zcmeHPXIPWjwoV8n0YVX^1StU&P?2I7Iv832MFz2eh;%U^T@=Ma2}H#p0g<90Q5*pW zHIyS|6cQqcbQBaNB7vx=NE0a|<%EDEVeopMduHa&xhFsPp5%KzzP})Ls_@wv%0DyqCmAO3t073u&z$z%1c}K$YW-S0d0^S>+=>JMxh^n!#w5#Lrf>b39UPfmXATUJgP)klIdrqJ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