core_2d_camera.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - 2d camera
  4. *
  5. * Example complexity rating: [★★☆☆] 2/4
  6. *
  7. * Example originally created with raylib 1.5, last time updated with raylib 3.0
  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) 2016-2025 Ramon Santamaria (@raysan5)
  13. *
  14. ********************************************************************************************/
  15. #include "raylib.h"
  16. #include <math.h>
  17. #define MAX_BUILDINGS 100
  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. InitWindow(screenWidth, screenHeight, "raylib [core] example - 2d camera");
  28. Rectangle player = { 400, 280, 40, 40 };
  29. Rectangle buildings[MAX_BUILDINGS] = { 0 };
  30. Color buildColors[MAX_BUILDINGS] = { 0 };
  31. int spacing = 0;
  32. for (int i = 0; i < MAX_BUILDINGS; i++)
  33. {
  34. buildings[i].width = (float)GetRandomValue(50, 200);
  35. buildings[i].height = (float)GetRandomValue(100, 800);
  36. buildings[i].y = screenHeight - 130.0f - buildings[i].height;
  37. buildings[i].x = -6000.0f + spacing;
  38. spacing += (int)buildings[i].width;
  39. buildColors[i] = (Color){
  40. (unsigned char)GetRandomValue(200, 240),
  41. (unsigned char)GetRandomValue(200, 240),
  42. (unsigned char)GetRandomValue(200, 250),
  43. 255};
  44. }
  45. Camera2D camera = { 0 };
  46. camera.target = (Vector2){ player.x + 20.0f, player.y + 20.0f };
  47. camera.offset = (Vector2){ screenWidth/2.0f, screenHeight/2.0f };
  48. camera.rotation = 0.0f;
  49. camera.zoom = 1.0f;
  50. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  51. //--------------------------------------------------------------------------------------
  52. // Main game loop
  53. while (!WindowShouldClose()) // Detect window close button or ESC key
  54. {
  55. // Update
  56. //----------------------------------------------------------------------------------
  57. // Player movement
  58. if (IsKeyDown(KEY_RIGHT)) player.x += 2;
  59. else if (IsKeyDown(KEY_LEFT)) player.x -= 2;
  60. // Camera target follows player
  61. camera.target = (Vector2){ player.x + 20, player.y + 20 };
  62. // Camera rotation controls
  63. if (IsKeyDown(KEY_A)) camera.rotation--;
  64. else if (IsKeyDown(KEY_S)) camera.rotation++;
  65. // Limit camera rotation to 80 degrees (-40 to 40)
  66. if (camera.rotation > 40) camera.rotation = 40;
  67. else if (camera.rotation < -40) camera.rotation = -40;
  68. // Camera zoom controls
  69. // Uses log scaling to provide consistent zoom speed
  70. camera.zoom = expf(logf(camera.zoom) + ((float)GetMouseWheelMove()*0.1f));
  71. if (camera.zoom > 3.0f) camera.zoom = 3.0f;
  72. else if (camera.zoom < 0.1f) camera.zoom = 0.1f;
  73. // Camera reset (zoom and rotation)
  74. if (IsKeyPressed(KEY_R))
  75. {
  76. camera.zoom = 1.0f;
  77. camera.rotation = 0.0f;
  78. }
  79. //----------------------------------------------------------------------------------
  80. // Draw
  81. //----------------------------------------------------------------------------------
  82. BeginDrawing();
  83. ClearBackground(RAYWHITE);
  84. BeginMode2D(camera);
  85. DrawRectangle(-6000, 320, 13000, 8000, DARKGRAY);
  86. for (int i = 0; i < MAX_BUILDINGS; i++) DrawRectangleRec(buildings[i], buildColors[i]);
  87. DrawRectangleRec(player, RED);
  88. DrawLine((int)camera.target.x, -screenHeight*10, (int)camera.target.x, screenHeight*10, GREEN);
  89. DrawLine(-screenWidth*10, (int)camera.target.y, screenWidth*10, (int)camera.target.y, GREEN);
  90. EndMode2D();
  91. DrawText("SCREEN AREA", 640, 10, 20, RED);
  92. DrawRectangle(0, 0, screenWidth, 5, RED);
  93. DrawRectangle(0, 5, 5, screenHeight - 10, RED);
  94. DrawRectangle(screenWidth - 5, 5, 5, screenHeight - 10, RED);
  95. DrawRectangle(0, screenHeight - 5, screenWidth, 5, RED);
  96. DrawRectangle( 10, 10, 250, 113, Fade(SKYBLUE, 0.5f));
  97. DrawRectangleLines( 10, 10, 250, 113, BLUE);
  98. DrawText("Free 2D camera controls:", 20, 20, 10, BLACK);
  99. DrawText("- Right/Left to move player", 40, 40, 10, DARKGRAY);
  100. DrawText("- Mouse Wheel to Zoom in-out", 40, 60, 10, DARKGRAY);
  101. DrawText("- A / S to Rotate", 40, 80, 10, DARKGRAY);
  102. DrawText("- R to reset Zoom and Rotation", 40, 100, 10, DARKGRAY);
  103. EndDrawing();
  104. //----------------------------------------------------------------------------------
  105. }
  106. // De-Initialization
  107. //--------------------------------------------------------------------------------------
  108. CloseWindow(); // Close window and OpenGL context
  109. //--------------------------------------------------------------------------------------
  110. return 0;
  111. }