core_2d_camera.c 5.2 KB

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