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.

137 lines
5.0 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - 2d camera
  4. *
  5. * This example has been created using raylib 1.5 (www.raylib.com)
  6. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. *
  8. * Copyright (c) 2016 Ramon Santamaria (@raysan5)
  9. *
  10. ********************************************************************************************/
  11. #include "raylib.h"
  12. #define MAX_BUILDINGS 100
  13. int main(void)
  14. {
  15. // Initialization
  16. //--------------------------------------------------------------------------------------
  17. const int screenWidth = 800;
  18. const int screenHeight = 450;
  19. InitWindow(screenWidth, screenHeight, "raylib [core] example - 2d camera");
  20. Rectangle player = { 400, 280, 40, 40 };
  21. Rectangle buildings[MAX_BUILDINGS] = { 0 };
  22. Color buildColors[MAX_BUILDINGS] = { 0 };
  23. int spacing = 0;
  24. for (int i = 0; i < MAX_BUILDINGS; i++)
  25. {
  26. buildings[i].width = GetRandomValue(50, 200);
  27. buildings[i].height = GetRandomValue(100, 800);
  28. buildings[i].y = screenHeight - 130 - buildings[i].height;
  29. buildings[i].x = -6000 + spacing;
  30. spacing += buildings[i].width;
  31. buildColors[i] = (Color){ GetRandomValue(200, 240), GetRandomValue(200, 240), GetRandomValue(200, 250), 255 };
  32. }
  33. Camera2D camera = { 0 };
  34. camera.target = (Vector2){ player.x + 20, player.y + 20 };
  35. camera.offset = (Vector2){ 0, 0 };
  36. camera.rotation = 0.0f;
  37. camera.zoom = 1.0f;
  38. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  39. //--------------------------------------------------------------------------------------
  40. // Main game loop
  41. while (!WindowShouldClose()) // Detect window close button or ESC key
  42. {
  43. // Update
  44. //----------------------------------------------------------------------------------
  45. if (IsKeyDown(KEY_RIGHT))
  46. {
  47. player.x += 2; // Player movement
  48. camera.offset.x -= 2; // Camera displacement with player movement
  49. }
  50. else if (IsKeyDown(KEY_LEFT))
  51. {
  52. player.x -= 2; // Player movement
  53. camera.offset.x += 2; // Camera displacement with player movement
  54. }
  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(camera.target.x, -screenHeight*10, camera.target.x, screenHeight*10, GREEN);
  83. DrawLine(-screenWidth*10, camera.target.y, screenWidth*10, 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. }