You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
3.9 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - window scale letterbox
  4. *
  5. * This example has been created using raylib 2.5 (www.raylib.com)
  6. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. *
  8. * Example contributed by Anata (@anatagawa) and reviewed by Ramon Santamaria (@raysan5)
  9. *
  10. * Copyright (c) 2019 Anata (@anatagawa) and Ramon Santamaria (@raysan5)
  11. *
  12. ********************************************************************************************/
  13. #include "raylib.h"
  14. #define max(a, b) ((a)>(b)? (a) : (b))
  15. #define min(a, b) ((a)<(b)? (a) : (b))
  16. int main(void)
  17. {
  18. const int windowWidth = 800;
  19. const int windowHeight = 450;
  20. // Enable config flags for resizable window and vertical synchro
  21. SetConfigFlags(FLAG_WINDOW_RESIZABLE | FLAG_VSYNC_HINT);
  22. InitWindow(windowWidth, windowHeight, "raylib [core] example - window scale letterbox");
  23. SetWindowMinSize(320, 240);
  24. int gameScreenWidth = 640;
  25. int gameScreenHeight = 480;
  26. // Render texture initialization
  27. RenderTexture2D target = LoadRenderTexture(gameScreenWidth, gameScreenHeight);
  28. SetTextureFilter(target.texture, FILTER_BILINEAR); // Texture scale filter to use
  29. Color colors[10] = { 0 };
  30. for (int i = 0; i < 10; i++) colors[i] = (Color){ GetRandomValue(100, 250), GetRandomValue(50, 150), GetRandomValue(10, 100), 255 };
  31. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  32. //--------------------------------------------------------------------------------------
  33. // Main game loop
  34. while (!WindowShouldClose()) // Detect window close button or ESC key
  35. {
  36. // Update
  37. //----------------------------------------------------------------------------------
  38. // Compute required framebuffer scaling
  39. float scale = min((float)GetScreenWidth()/gameScreenWidth, (float)GetScreenHeight()/gameScreenHeight);
  40. if (IsKeyPressed(KEY_SPACE))
  41. {
  42. // Recalculate random colors for the bars
  43. for (int i = 0; i < 10; i++) colors[i] = (Color){ GetRandomValue(100, 250), GetRandomValue(50, 150), GetRandomValue(10, 100), 255 };
  44. }
  45. //----------------------------------------------------------------------------------
  46. // Draw
  47. //----------------------------------------------------------------------------------
  48. BeginDrawing();
  49. ClearBackground(BLACK);
  50. // Draw everything in the render texture
  51. BeginTextureMode(target);
  52. ClearBackground(RAYWHITE); // Clear render texture background color
  53. for (int i = 0; i < 10; i++) DrawRectangle(0, (gameScreenHeight/10)*i, gameScreenWidth, gameScreenHeight/10, colors[i]);
  54. DrawText("If executed inside a window,\nyou can resize the window,\nand see the screen scaling!", 10, 25, 20, WHITE);
  55. EndTextureMode();
  56. // Draw RenderTexture2D to window, properly scaled
  57. DrawTexturePro(target.texture, (Rectangle){ 0.0f, 0.0f, (float)target.texture.width, (float)-target.texture.height },
  58. (Rectangle){ (GetScreenWidth() - ((float)gameScreenWidth*scale))*0.5, (GetScreenHeight() - ((float)gameScreenHeight*scale))*0.5,
  59. (float)gameScreenWidth*scale, (float)gameScreenHeight*scale }, (Vector2){ 0, 0 }, 0.0f, WHITE);
  60. EndDrawing();
  61. //--------------------------------------------------------------------------------------
  62. }
  63. // De-Initialization
  64. //--------------------------------------------------------------------------------------
  65. UnloadRenderTexture(target); // Unload render texture
  66. CloseWindow(); // Close window and OpenGL context
  67. //--------------------------------------------------------------------------------------
  68. return 0;
  69. }