core_2d_camera.bmx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. SuperStrict
  2. Framework Ray.Lib
  3. Const MAX_BUILDINGS:Int = 100
  4. Const screenWidth:Int = 800
  5. Const screenHeight:Int = 450
  6. InitWindow(screenWidth, screenHeight, "raylib [core] example - 2d camera")
  7. Local player:RRectangle = New RRectangle(400, 280, 40, 40)
  8. Local buildings:RRectangle[MAX_BUILDINGS]
  9. Local buildColors:RColor[MAX_BUILDINGS]
  10. Local spacing:Int = 0
  11. For Local i:Int = 0 Until MAX_BUILDINGS
  12. buildings[i].width = GetRandomValue(50, 200)
  13. buildings[i].height = GetRandomValue(100, 800)
  14. buildings[i].y = screenHeight - 130 - buildings[i].height
  15. buildings[i].x = -6000 + spacing
  16. spacing :+ buildings[i].width
  17. buildColors[i] = New RColor(GetRandomValue(200, 240), GetRandomValue(200, 240), GetRandomValue(200, 250), 255)
  18. Next
  19. Local camera:RCamera2D = New RCamera2D()
  20. camera.target = New RVector2(player.x + 20, player.y + 20)
  21. camera.offset = New RVector2(screenWidth/2, screenHeight/2)
  22. camera.Rotation = 0.0
  23. camera.zoom = 1.0
  24. SetTargetFPS(60) ' Set our game to run at 60 frames-per-second
  25. '--------------------------------------------------------------------------------------
  26. ' Main game loop
  27. While Not WindowShouldClose() ' Detect window close button or ESC key
  28. ' Update
  29. '----------------------------------------------------------------------------------
  30. ' Player movement
  31. If IsKeyDown(KEY_RIGHT) Then
  32. player.x :+ 2
  33. Else If IsKeyDown(KEY_LEFT) Then
  34. player.x :- 2
  35. End If
  36. ' Camera target follows player
  37. camera.target = New RVector2(player.x + 20, player.y + 20)
  38. ' Camera rotation controls
  39. If IsKeyDown(KEY_A) Then
  40. camera.Rotation :- 1
  41. Else If IsKeyDown(KEY_S) Then
  42. camera.Rotation :+ 1
  43. End If
  44. ' Limit camera rotation to 80 degrees (-40 to 40)
  45. If camera.Rotation > 40 Then
  46. camera.Rotation = 40
  47. Else If camera.Rotation < -40 Then
  48. camera.Rotation = -40
  49. End If
  50. ' Camera zoom controls
  51. camera.zoom :+ Float(GetMouseWheelMove() * 0.05)
  52. If camera.zoom > 3.0 Then
  53. camera.zoom = 3.0
  54. Else If camera.zoom < 0.1 Then
  55. camera.zoom = 0.1
  56. End If
  57. ' Camera reset (zoom and rotation)
  58. If IsKeyPressed(KEY_R) Then
  59. camera.zoom = 1.0
  60. camera.Rotation = 0.0
  61. End If
  62. '----------------------------------------------------------------------------------
  63. ' Draw
  64. '----------------------------------------------------------------------------------
  65. BeginDrawing()
  66. ClearBackground(RAYWHITE)
  67. BeginMode2D(camera)
  68. DrawRectangle(-6000, 320, 13000, 8000, DARKGRAY)
  69. For Local i:Int = 0 Until MAX_BUILDINGS
  70. DrawRectangleRec(buildings[i], buildColors[i])
  71. Next
  72. DrawRectangleRec(player, RED)
  73. DrawLine(Int(camera.target.x), -screenHeight * 10, Int(camera.target.x), screenHeight * 10, GREEN)
  74. DrawLine(-screenWidth * 10, Int(camera.target.y), screenWidth * 10, Int(camera.target.y), GREEN)
  75. EndMode2D()
  76. DrawText("SCREEN AREA", 640, 10, 20, RED)
  77. DrawRectangle(0, 0, screenWidth, 5, RED)
  78. DrawRectangle(0, 5, 5, screenHeight - 10, RED)
  79. DrawRectangle(screenWidth - 5, 5, 5, screenHeight - 10, RED)
  80. DrawRectangle(0, screenHeight - 5, screenWidth, 5, RED)
  81. DrawRectangle( 10, 10, 250, 113, Fade(SKYBLUE, 0.5))
  82. DrawRectangleLines( 10, 10, 250, 113, BLUE)
  83. DrawText("Free 2d camera controls:", 20, 20, 10, BLACK)
  84. DrawText("- Right/Left to move Offset", 40, 40, 10, DARKGRAY)
  85. DrawText("- Mouse Wheel to Zoom in-out", 40, 60, 10, DARKGRAY)
  86. DrawText("- A / S to Rotate", 40, 80, 10, DARKGRAY)
  87. DrawText("- R to reset Zoom and Rotation", 40, 100, 10, DARKGRAY)
  88. EndDrawing()
  89. '----------------------------------------------------------------------------------
  90. Wend
  91. ' De-Initialization
  92. '--------------------------------------------------------------------------------------
  93. CloseWindow() ' Close window and OpenGL context
  94. '--------------------------------------------------------------------------------------