core_smooth_pixelperfect.c 5.1 KB

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