Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

107 righe
5.4 KiB

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