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.

116 lines
5.4 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - window scale letterbox (and virtual mouse)
  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. // Clamp Vector2 value with min and max and return a new vector2
  17. // NOTE: Required for virtual mouse, to clamp inside virtual game size
  18. Vector2 ClampValue(Vector2 value, Vector2 min, Vector2 max)
  19. {
  20. Vector2 result = value;
  21. result.x = (result.x > max.x)? max.x : result.x;
  22. result.x = (result.x < min.x)? min.x : result.x;
  23. result.y = (result.y > max.y)? max.y : result.y;
  24. result.y = (result.y < min.y)? min.y : result.y;
  25. return result;
  26. }
  27. int main(void)
  28. {
  29. const int windowWidth = 800;
  30. const int windowHeight = 450;
  31. // Enable config flags for resizable window and vertical synchro
  32. SetConfigFlags(FLAG_WINDOW_RESIZABLE | FLAG_VSYNC_HINT);
  33. InitWindow(windowWidth, windowHeight, "raylib [core] example - window scale letterbox");
  34. SetWindowMinSize(320, 240);
  35. int gameScreenWidth = 640;
  36. int gameScreenHeight = 480;
  37. // Render texture initialization, used to hold the rendering result so we can easily resize it
  38. RenderTexture2D target = LoadRenderTexture(gameScreenWidth, gameScreenHeight);
  39. SetTextureFilter(target.texture, FILTER_BILINEAR); // Texture scale filter to use
  40. Color colors[10] = { 0 };
  41. for (int i = 0; i < 10; i++) colors[i] = (Color){ GetRandomValue(100, 250), GetRandomValue(50, 150), GetRandomValue(10, 100), 255 };
  42. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  43. //--------------------------------------------------------------------------------------
  44. // Main game loop
  45. while (!WindowShouldClose()) // Detect window close button or ESC key
  46. {
  47. // Update
  48. //----------------------------------------------------------------------------------
  49. // Compute required framebuffer scaling
  50. float scale = min((float)GetScreenWidth()/gameScreenWidth, (float)GetScreenHeight()/gameScreenHeight);
  51. if (IsKeyPressed(KEY_SPACE))
  52. {
  53. // Recalculate random colors for the bars
  54. for (int i = 0; i < 10; i++) colors[i] = (Color){ GetRandomValue(100, 250), GetRandomValue(50, 150), GetRandomValue(10, 100), 255 };
  55. }
  56. // Update virtual mouse (clamped mouse value behind game screen)
  57. Vector2 mouse = GetMousePosition();
  58. Vector2 virtualMouse = { 0 };
  59. virtualMouse.x = (mouse.x - (GetScreenWidth() - (gameScreenWidth*scale))*0.5f)/scale;
  60. virtualMouse.y = (mouse.y - (GetScreenHeight() - (gameScreenHeight*scale))*0.5f)/scale;
  61. virtualMouse = ClampValue(virtualMouse, (Vector2){ 0, 0 }, (Vector2){ gameScreenWidth, gameScreenHeight });
  62. // Apply the same transformation as the virtual mouse to the real mouse (i.e. to work with raygui)
  63. //SetMouseOffset(-(GetScreenWidth() - (gameScreenWidth*scale))*0.5f, -(GetScreenHeight() - (gameScreenHeight*scale))*0.5f);
  64. //SetMouseScale(1/scale, 1/scale);
  65. //----------------------------------------------------------------------------------
  66. // Draw
  67. //----------------------------------------------------------------------------------
  68. BeginDrawing();
  69. ClearBackground(BLACK);
  70. // Draw everything in the render texture, note this will not be rendered on screen, yet
  71. BeginTextureMode(target);
  72. ClearBackground(RAYWHITE); // Clear render texture background color
  73. for (int i = 0; i < 10; i++) DrawRectangle(0, (gameScreenHeight/10)*i, gameScreenWidth, gameScreenHeight/10, colors[i]);
  74. DrawText("If executed inside a window,\nyou can resize the window,\nand see the screen scaling!", 10, 25, 20, WHITE);
  75. DrawText(TextFormat("Default Mouse: [%i , %i]", (int)mouse.x, (int)mouse.y), 350, 25, 20, GREEN);
  76. DrawText(TextFormat("Virtual Mouse: [%i , %i]", (int)virtualMouse.x, (int)virtualMouse.y), 350, 55, 20, YELLOW);
  77. EndTextureMode();
  78. // Draw RenderTexture2D to window, properly scaled
  79. DrawTexturePro(target.texture, (Rectangle){ 0.0f, 0.0f, (float)target.texture.width, (float)-target.texture.height },
  80. (Rectangle){ (GetScreenWidth() - ((float)gameScreenWidth*scale))*0.5f, (GetScreenHeight() - ((float)gameScreenHeight*scale))*0.5f,
  81. (float)gameScreenWidth*scale, (float)gameScreenHeight*scale }, (Vector2){ 0, 0 }, 0.0f, WHITE);
  82. EndDrawing();
  83. //--------------------------------------------------------------------------------------
  84. }
  85. // De-Initialization
  86. //--------------------------------------------------------------------------------------
  87. UnloadRenderTexture(target); // Unload render texture
  88. CloseWindow(); // Close window and OpenGL context
  89. //--------------------------------------------------------------------------------------
  90. return 0;
  91. }