2016-03-05 17:40:08 +03:00
|
|
|
/*******************************************************************************************
|
|
|
|
*
|
|
|
|
* raylib [core] example - 2d camera
|
|
|
|
*
|
|
|
|
* This example has been created using raylib 1.5 (www.raylib.com)
|
|
|
|
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2016 Ramon Santamaria (@raysan5)
|
|
|
|
*
|
|
|
|
********************************************************************************************/
|
|
|
|
|
|
|
|
#include "raylib.h"
|
|
|
|
|
2016-06-03 14:22:24 +03:00
|
|
|
#define MAX_BUILDINGS 100
|
|
|
|
|
2016-03-05 17:40:08 +03:00
|
|
|
int main()
|
|
|
|
{
|
|
|
|
// Initialization
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
int screenWidth = 800;
|
|
|
|
int screenHeight = 450;
|
|
|
|
|
|
|
|
InitWindow(screenWidth, screenHeight, "raylib [core] example - 2d camera");
|
|
|
|
|
2016-06-03 14:22:24 +03:00
|
|
|
Rectangle player = { 400, 280, 40, 40 };
|
2016-06-30 01:26:56 +03:00
|
|
|
Rectangle buildings[MAX_BUILDINGS];
|
|
|
|
Color buildColors[MAX_BUILDINGS];
|
2016-06-03 14:22:24 +03:00
|
|
|
|
|
|
|
int spacing = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < MAX_BUILDINGS; i++)
|
|
|
|
{
|
|
|
|
buildings[i].width = GetRandomValue(50, 200);
|
|
|
|
buildings[i].height = GetRandomValue(100, 800);
|
|
|
|
buildings[i].y = screenHeight - 130 - buildings[i].height;
|
|
|
|
buildings[i].x = -6000 + spacing;
|
|
|
|
|
|
|
|
spacing += buildings[i].width;
|
|
|
|
|
|
|
|
buildColors[i] = (Color){ GetRandomValue(200, 240), GetRandomValue(200, 240), GetRandomValue(200, 250), 255 };
|
|
|
|
}
|
|
|
|
|
2016-03-05 17:40:08 +03:00
|
|
|
Camera2D camera;
|
|
|
|
|
2016-06-03 14:22:24 +03:00
|
|
|
camera.target = (Vector2){ player.x + 20, player.y + 20 };
|
2016-03-27 19:33:54 +03:00
|
|
|
camera.offset = (Vector2){ 0, 0 };
|
2016-03-05 17:40:08 +03:00
|
|
|
camera.rotation = 0.0f;
|
|
|
|
camera.zoom = 1.0f;
|
|
|
|
|
|
|
|
SetTargetFPS(60);
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Main game loop
|
|
|
|
while (!WindowShouldClose()) // Detect window close button or ESC key
|
|
|
|
{
|
|
|
|
// Update
|
|
|
|
//----------------------------------------------------------------------------------
|
2016-06-03 14:22:24 +03:00
|
|
|
if (IsKeyDown(KEY_RIGHT))
|
|
|
|
{
|
|
|
|
player.x += 2; // Player movement
|
|
|
|
camera.offset.x -= 2; // Camera displacement with player movement
|
|
|
|
}
|
|
|
|
else if (IsKeyDown(KEY_LEFT))
|
|
|
|
{
|
|
|
|
player.x -= 2; // Player movement
|
|
|
|
camera.offset.x += 2; // Camera displacement with player movement
|
|
|
|
}
|
2016-03-27 19:33:54 +03:00
|
|
|
|
|
|
|
// Camera target follows player
|
|
|
|
camera.target = (Vector2){ player.x + 20, player.y + 20 };
|
2016-03-05 17:40:08 +03:00
|
|
|
|
2016-06-03 14:22:24 +03:00
|
|
|
// Camera rotation controls
|
|
|
|
if (IsKeyDown(KEY_A)) camera.rotation--;
|
|
|
|
else if (IsKeyDown(KEY_S)) camera.rotation++;
|
2016-03-05 17:40:08 +03:00
|
|
|
|
2016-06-03 14:22:24 +03:00
|
|
|
// Limit camera rotation to 80 degrees (-40 to 40)
|
|
|
|
if (camera.rotation > 40) camera.rotation = 40;
|
|
|
|
else if (camera.rotation < -40) camera.rotation = -40;
|
2016-03-27 19:33:54 +03:00
|
|
|
|
2016-06-03 14:22:24 +03:00
|
|
|
// Camera zoom controls
|
2016-03-27 19:33:54 +03:00
|
|
|
camera.zoom += ((float)GetMouseWheelMove()*0.05f);
|
|
|
|
|
2016-06-03 14:22:24 +03:00
|
|
|
if (camera.zoom > 3.0f) camera.zoom = 3.0f;
|
|
|
|
else if (camera.zoom < 0.1f) camera.zoom = 0.1f;
|
|
|
|
|
|
|
|
// Camera reset (zoom and rotation)
|
|
|
|
if (IsKeyPressed(KEY_R))
|
2016-03-27 19:33:54 +03:00
|
|
|
{
|
|
|
|
camera.zoom = 1.0f;
|
|
|
|
camera.rotation = 0.0f;
|
|
|
|
}
|
2016-03-05 17:40:08 +03:00
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Draw
|
|
|
|
//----------------------------------------------------------------------------------
|
2016-06-03 14:22:24 +03:00
|
|
|
BeginDrawing();
|
|
|
|
|
2016-03-05 17:40:08 +03:00
|
|
|
ClearBackground(RAYWHITE);
|
2016-06-03 14:22:24 +03:00
|
|
|
|
|
|
|
Begin2dMode(camera);
|
2016-03-05 17:40:08 +03:00
|
|
|
|
2016-06-03 14:22:24 +03:00
|
|
|
DrawRectangle(-6000, 320, 13000, 8000, DARKGRAY);
|
|
|
|
|
|
|
|
for (int i = 0; i < MAX_BUILDINGS; i++) DrawRectangleRec(buildings[i], buildColors[i]);
|
|
|
|
|
|
|
|
DrawRectangleRec(player, RED);
|
|
|
|
|
|
|
|
DrawRectangle(camera.target.x, -500, 1, screenHeight*4, GREEN);
|
|
|
|
DrawRectangle(-500, camera.target.y, screenWidth*4, 1, GREEN);
|
|
|
|
|
|
|
|
End2dMode();
|
|
|
|
|
|
|
|
DrawText("SCREEN AREA", 640, 10, 20, RED);
|
|
|
|
|
|
|
|
DrawRectangle(0, 0, screenWidth, 5, RED);
|
|
|
|
DrawRectangle(0, 5, 5, screenHeight - 10, RED);
|
|
|
|
DrawRectangle(screenWidth - 5, 5, 5, screenHeight - 10, RED);
|
|
|
|
DrawRectangle(0, screenHeight - 5, screenWidth, 5, RED);
|
2016-03-05 17:40:08 +03:00
|
|
|
|
2016-06-03 14:22:24 +03:00
|
|
|
DrawRectangle( 10, 10, 250, 113, Fade(SKYBLUE, 0.5f));
|
|
|
|
DrawRectangleLines( 10, 10, 250, 113, BLUE);
|
2016-03-27 19:33:54 +03:00
|
|
|
|
2016-06-03 14:22:24 +03:00
|
|
|
DrawText("Free 2d camera controls:", 20, 20, 10, BLACK);
|
|
|
|
DrawText("- Right/Left to move Offset", 40, 40, 10, DARKGRAY);
|
|
|
|
DrawText("- Mouse Wheel to Zoom in-out", 40, 60, 10, DARKGRAY);
|
|
|
|
DrawText("- A / S to Rotate", 40, 80, 10, DARKGRAY);
|
|
|
|
DrawText("- R to reset Zoom and Rotation", 40, 100, 10, DARKGRAY);
|
2016-03-05 17:40:08 +03:00
|
|
|
|
|
|
|
EndDrawing();
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
}
|
|
|
|
|
|
|
|
// De-Initialization
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
CloseWindow(); // Close window and OpenGL context
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|