core_window_letterbox.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - window scale letterbox (and virtual mouse)
  4. *
  5. * Example complexity rating: [★★☆☆] 2/4
  6. *
  7. * Example originally created with raylib 2.5, last time updated with raylib 4.0
  8. *
  9. * Example contributed by Anata (@anatagawa) and reviewed by Ramon Santamaria (@raysan5)
  10. *
  11. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  12. * BSD-like license that allows static linking with closed source software
  13. *
  14. * Copyright (c) 2019-2024 Anata (@anatagawa) and Ramon Santamaria (@raysan5)
  15. *
  16. ********************************************************************************************/
  17. #include "raylib.h"
  18. #include "raymath.h" // Required for: Vector2Clamp()
  19. #define MAX(a, b) ((a)>(b)? (a) : (b))
  20. #define MIN(a, b) ((a)<(b)? (a) : (b))
  21. //------------------------------------------------------------------------------------
  22. // Program main entry point
  23. //------------------------------------------------------------------------------------
  24. int main(void)
  25. {
  26. const int windowWidth = 800;
  27. const int windowHeight = 450;
  28. // Enable config flags for resizable window and vertical synchro
  29. SetConfigFlags(FLAG_WINDOW_RESIZABLE | FLAG_VSYNC_HINT);
  30. InitWindow(windowWidth, windowHeight, "raylib [core] example - window scale letterbox");
  31. SetWindowMinSize(320, 240);
  32. int gameScreenWidth = 640;
  33. int gameScreenHeight = 480;
  34. // Render texture initialization, used to hold the rendering result so we can easily resize it
  35. RenderTexture2D target = LoadRenderTexture(gameScreenWidth, gameScreenHeight);
  36. SetTextureFilter(target.texture, TEXTURE_FILTER_BILINEAR); // Texture scale filter to use
  37. Color colors[10] = { 0 };
  38. for (int i = 0; i < 10; i++) colors[i] = (Color){ GetRandomValue(100, 250), GetRandomValue(50, 150), GetRandomValue(10, 100), 255 };
  39. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  40. //--------------------------------------------------------------------------------------
  41. // Main game loop
  42. while (!WindowShouldClose()) // Detect window close button or ESC key
  43. {
  44. // Update
  45. //----------------------------------------------------------------------------------
  46. // Compute required framebuffer scaling
  47. float scale = MIN((float)GetScreenWidth()/gameScreenWidth, (float)GetScreenHeight()/gameScreenHeight);
  48. if (IsKeyPressed(KEY_SPACE))
  49. {
  50. // Recalculate random colors for the bars
  51. for (int i = 0; i < 10; i++) colors[i] = (Color){ GetRandomValue(100, 250), GetRandomValue(50, 150), GetRandomValue(10, 100), 255 };
  52. }
  53. // Update virtual mouse (clamped mouse value behind game screen)
  54. Vector2 mouse = GetMousePosition();
  55. Vector2 virtualMouse = { 0 };
  56. virtualMouse.x = (mouse.x - (GetScreenWidth() - (gameScreenWidth*scale))*0.5f)/scale;
  57. virtualMouse.y = (mouse.y - (GetScreenHeight() - (gameScreenHeight*scale))*0.5f)/scale;
  58. virtualMouse = Vector2Clamp(virtualMouse, (Vector2){ 0, 0 }, (Vector2){ (float)gameScreenWidth, (float)gameScreenHeight });
  59. // Apply the same transformation as the virtual mouse to the real mouse (i.e. to work with raygui)
  60. //SetMouseOffset(-(GetScreenWidth() - (gameScreenWidth*scale))*0.5f, -(GetScreenHeight() - (gameScreenHeight*scale))*0.5f);
  61. //SetMouseScale(1/scale, 1/scale);
  62. //----------------------------------------------------------------------------------
  63. // Draw
  64. //----------------------------------------------------------------------------------
  65. // Draw everything in the render texture, note this will not be rendered on screen, yet
  66. BeginTextureMode(target);
  67. ClearBackground(RAYWHITE); // Clear render texture background color
  68. for (int i = 0; i < 10; i++) DrawRectangle(0, (gameScreenHeight/10)*i, gameScreenWidth, gameScreenHeight/10, colors[i]);
  69. DrawText("If executed inside a window,\nyou can resize the window,\nand see the screen scaling!", 10, 25, 20, WHITE);
  70. DrawText(TextFormat("Default Mouse: [%i , %i]", (int)mouse.x, (int)mouse.y), 350, 25, 20, GREEN);
  71. DrawText(TextFormat("Virtual Mouse: [%i , %i]", (int)virtualMouse.x, (int)virtualMouse.y), 350, 55, 20, YELLOW);
  72. EndTextureMode();
  73. BeginDrawing();
  74. ClearBackground(BLACK); // Clear screen background
  75. // Draw render texture to screen, properly scaled
  76. DrawTexturePro(target.texture, (Rectangle){ 0.0f, 0.0f, (float)target.texture.width, (float)-target.texture.height },
  77. (Rectangle){ (GetScreenWidth() - ((float)gameScreenWidth*scale))*0.5f, (GetScreenHeight() - ((float)gameScreenHeight*scale))*0.5f,
  78. (float)gameScreenWidth*scale, (float)gameScreenHeight*scale }, (Vector2){ 0, 0 }, 0.0f, WHITE);
  79. EndDrawing();
  80. //--------------------------------------------------------------------------------------
  81. }
  82. // De-Initialization
  83. //--------------------------------------------------------------------------------------
  84. UnloadRenderTexture(target); // Unload render texture
  85. CloseWindow(); // Close window and OpenGL context
  86. //--------------------------------------------------------------------------------------
  87. return 0;
  88. }