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.

136 lines
5.1 KiB

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