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.

121 lines
5.0 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - Smooth Pixel-perfect camera
  4. *
  5. * Example originally created with raylib 3.7, last time updated with raylib 4.0
  6. *
  7. * Example contributed by Giancamillo Alessandroni (@NotManyIdeasDev) and
  8. * reviewed by Ramon Santamaria (@raysan5)
  9. *
  10. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  11. * BSD-like license that allows static linking with closed source software
  12. *
  13. * Copyright (c) 2021-2024 Giancamillo Alessandroni (@NotManyIdeasDev) and Ramon Santamaria (@raysan5)
  14. *
  15. ********************************************************************************************/
  16. #include "raylib.h"
  17. #include <math.h> // Required for: sinf(), cosf()
  18. //------------------------------------------------------------------------------------
  19. // Program main entry point
  20. //------------------------------------------------------------------------------------
  21. int main(void)
  22. {
  23. // Initialization
  24. //--------------------------------------------------------------------------------------
  25. const int screenWidth = 800;
  26. const int screenHeight = 450;
  27. const int virtualScreenWidth = 160;
  28. const int virtualScreenHeight = 90;
  29. const float virtualRatio = (float)screenWidth/(float)virtualScreenWidth;
  30. InitWindow(screenWidth, screenHeight, "raylib [core] example - smooth pixel-perfect camera");
  31. Camera2D worldSpaceCamera = { 0 }; // Game world camera
  32. worldSpaceCamera.zoom = 1.0f;
  33. Camera2D screenSpaceCamera = { 0 }; // Smoothing camera
  34. screenSpaceCamera.zoom = 1.0f;
  35. RenderTexture2D target = LoadRenderTexture(virtualScreenWidth, virtualScreenHeight); // This is where we'll draw all our objects.
  36. Rectangle rec01 = { 70.0f, 35.0f, 20.0f, 20.0f };
  37. Rectangle rec02 = { 90.0f, 55.0f, 30.0f, 10.0f };
  38. Rectangle rec03 = { 80.0f, 65.0f, 15.0f, 25.0f };
  39. // The target's height is flipped (in the source Rectangle), due to OpenGL reasons
  40. Rectangle sourceRec = { 0.0f, 0.0f, (float)target.texture.width, -(float)target.texture.height };
  41. Rectangle destRec = { -virtualRatio, -virtualRatio, screenWidth + (virtualRatio*2), screenHeight + (virtualRatio*2) };
  42. Vector2 origin = { 0.0f, 0.0f };
  43. float rotation = 0.0f;
  44. float cameraX = 0.0f;
  45. float cameraY = 0.0f;
  46. SetTargetFPS(60);
  47. //--------------------------------------------------------------------------------------
  48. // Main game loop
  49. while (!WindowShouldClose()) // Detect window close button or ESC key
  50. {
  51. // Update
  52. //----------------------------------------------------------------------------------
  53. rotation += 60.0f*GetFrameTime(); // Rotate the rectangles, 60 degrees per second
  54. // Make the camera move to demonstrate the effect
  55. cameraX = (sinf((float)GetTime())*50.0f) - 10.0f;
  56. cameraY = cosf((float)GetTime())*30.0f;
  57. // Set the camera's target to the values computed above
  58. screenSpaceCamera.target = (Vector2){ cameraX, cameraY };
  59. // Round worldSpace coordinates, keep decimals into screenSpace coordinates
  60. worldSpaceCamera.target.x = truncf(screenSpaceCamera.target.x);
  61. screenSpaceCamera.target.x -= worldSpaceCamera.target.x;
  62. screenSpaceCamera.target.x *= virtualRatio;
  63. worldSpaceCamera.target.y = truncf(screenSpaceCamera.target.y);
  64. screenSpaceCamera.target.y -= worldSpaceCamera.target.y;
  65. screenSpaceCamera.target.y *= virtualRatio;
  66. //----------------------------------------------------------------------------------
  67. // Draw
  68. //----------------------------------------------------------------------------------
  69. BeginTextureMode(target);
  70. ClearBackground(RAYWHITE);
  71. BeginMode2D(worldSpaceCamera);
  72. DrawRectanglePro(rec01, origin, rotation, BLACK);
  73. DrawRectanglePro(rec02, origin, -rotation, RED);
  74. DrawRectanglePro(rec03, origin, rotation + 45.0f, BLUE);
  75. EndMode2D();
  76. EndTextureMode();
  77. BeginDrawing();
  78. ClearBackground(RED);
  79. BeginMode2D(screenSpaceCamera);
  80. DrawTexturePro(target.texture, sourceRec, destRec, origin, 0.0f, WHITE);
  81. EndMode2D();
  82. DrawText(TextFormat("Screen resolution: %ix%i", screenWidth, screenHeight), 10, 10, 20, DARKBLUE);
  83. DrawText(TextFormat("World resolution: %ix%i", virtualScreenWidth, virtualScreenHeight), 10, 40, 20, DARKGREEN);
  84. DrawFPS(GetScreenWidth() - 95, 10);
  85. EndDrawing();
  86. //----------------------------------------------------------------------------------
  87. }
  88. // De-Initialization
  89. //--------------------------------------------------------------------------------------
  90. UnloadRenderTexture(target); // Unload render texture
  91. CloseWindow(); // Close window and OpenGL context
  92. //--------------------------------------------------------------------------------------
  93. return 0;
  94. }