core_smooth_pixelperfect.c 4.7 KB

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