From 5ea18b9426823e92f1fc16e686702f2caadf83c9 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sat, 5 Mar 2016 15:40:08 +0100 Subject: Support 2d camera system -IN PROGRESS- --- examples/core_2d_camera.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 examples/core_2d_camera.c (limited to 'examples/core_2d_camera.c') diff --git a/examples/core_2d_camera.c b/examples/core_2d_camera.c new file mode 100644 index 00000000..71c474f0 --- /dev/null +++ b/examples/core_2d_camera.c @@ -0,0 +1,71 @@ +/******************************************************************************************* +* +* 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" + +int main() +{ + // Initialization + //-------------------------------------------------------------------------------------- + int screenWidth = 800; + int screenHeight = 450; + + InitWindow(screenWidth, screenHeight, "raylib [core] example - 2d camera"); + + Camera2D camera; + + camera.position = (Vector2){ 0, 0 }; + camera.origin = (Vector2){ 100, 100 }; + camera.rotation = 0.0f; + camera.zoom = 1.0f; + + SetTargetFPS(60); + //-------------------------------------------------------------------------------------- + + // Main game loop + while (!WindowShouldClose()) // Detect window close button or ESC key + { + // Update + //---------------------------------------------------------------------------------- + if (IsKeyDown(KEY_RIGHT)) camera.position.x--; + else if (IsKeyDown(KEY_LEFT)) camera.position.x++; + else if (IsKeyDown(KEY_UP)) camera.position.y++; + else if (IsKeyDown(KEY_DOWN)) camera.position.y--; + + if (IsKeyDown(KEY_R)) camera.rotation--; + else if (IsKeyDown(KEY_F)) camera.rotation++; + + if (IsKeyDown(KEY_W)) camera.zoom += 0.005f; + if (IsKeyDown(KEY_S)) camera.zoom -= 0.005f; + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawingEx(camera); + + ClearBackground(RAYWHITE); + + DrawText("2D CAMERA TEST", 20, 20, 20, GRAY); + + DrawRectangle(0, 300, screenWidth, 50, GRAY); + DrawRectangle(400, 250, 40, 40, RED); + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- + + return 0; +} \ No newline at end of file -- cgit v1.2.3 From a3f16c84594a4811219892e444350d7d3441a6ae Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sun, 27 Mar 2016 18:33:54 +0200 Subject: Improved 2d camera system -IN PROGRESS- --- examples/core_2d_camera.c | 36 +++++++++++++++++++++++++++--------- src/core.c | 10 +++++----- 2 files changed, 32 insertions(+), 14 deletions(-) (limited to 'examples/core_2d_camera.c') diff --git a/examples/core_2d_camera.c b/examples/core_2d_camera.c index 71c474f0..5e6b7c6c 100644 --- a/examples/core_2d_camera.c +++ b/examples/core_2d_camera.c @@ -22,11 +22,14 @@ int main() Camera2D camera; - camera.position = (Vector2){ 0, 0 }; - camera.origin = (Vector2){ 100, 100 }; + camera.offset = (Vector2){ 0, 0 }; + camera.target = (Vector2){ 400, 200 }; camera.rotation = 0.0f; camera.zoom = 1.0f; + Rectangle player = { 400, 200, 40, 40 }; + camera.target = (Vector2){ player.x + 20, player.y + 20 }; + SetTargetFPS(60); //-------------------------------------------------------------------------------------- @@ -35,16 +38,28 @@ int main() { // Update //---------------------------------------------------------------------------------- - if (IsKeyDown(KEY_RIGHT)) camera.position.x--; - else if (IsKeyDown(KEY_LEFT)) camera.position.x++; - else if (IsKeyDown(KEY_UP)) camera.position.y++; - else if (IsKeyDown(KEY_DOWN)) camera.position.y--; + if (IsKeyDown(KEY_RIGHT)) player.x -= 2; + else if (IsKeyDown(KEY_LEFT)) player.x += 2; + else if (IsKeyDown(KEY_UP)) player.y -= 2; + else if (IsKeyDown(KEY_DOWN)) player.y += 2; + + // Camera target follows player + camera.target = (Vector2){ player.x + 20, player.y + 20 }; if (IsKeyDown(KEY_R)) camera.rotation--; else if (IsKeyDown(KEY_F)) camera.rotation++; - if (IsKeyDown(KEY_W)) camera.zoom += 0.005f; - if (IsKeyDown(KEY_S)) camera.zoom -= 0.005f; + // Camera controls + if (IsKeyDown(KEY_R)) camera.rotation--; + else if (IsKeyDown(KEY_F)) camera.rotation++; + + camera.zoom += ((float)GetMouseWheelMove()*0.05f); + + if (IsKeyPressed(KEY_Z)) + { + camera.zoom = 1.0f; + camera.rotation = 0.0f; + } //---------------------------------------------------------------------------------- // Draw @@ -56,7 +71,10 @@ int main() DrawText("2D CAMERA TEST", 20, 20, 20, GRAY); DrawRectangle(0, 300, screenWidth, 50, GRAY); - DrawRectangle(400, 250, 40, 40, RED); + DrawRectangleRec(player, RED); + + DrawRectangle(camera.origin.x, 0, 1, screenHeight, GREEN); + DrawRectangle(0, camera.origin.y, screenWidth, 1, GREEN); EndDrawing(); //---------------------------------------------------------------------------------- diff --git a/src/core.c b/src/core.c index c05de93b..2ab39ab0 100644 --- a/src/core.c +++ b/src/core.c @@ -560,14 +560,14 @@ void BeginDrawingEx(Camera2D camera) { BeginDrawing(); - // TODO: Consider origin offset on position, rotation, scaling - + // Camera rotation and scaling is always relative to target + Matrix matOrigin = MatrixTranslate(-camera.target.x, -camera.target.y, 0.0f); Matrix matRotation = MatrixRotate((Vector3){ 0.0f, 0.0f, 1.0f }, camera.rotation*DEG2RAD); Matrix matScale = MatrixScale(camera.zoom, camera.zoom, 1.0f); - Matrix matTranslation = MatrixTranslate(camera.position.x, camera.position.y, 0.0f); - Matrix matOrigin = MatrixTranslate(-camera.origin.x, -camera.origin.y, 0.0f); + + Matrix matTranslation = MatrixTranslate(camera.offset.x + camera.target.x, camera.offset.y + camera.target.y, 0.0f); - Matrix matTransform = MatrixMultiply(MatrixMultiply(matScale, matRotation), matTranslation); + Matrix matTransform = MatrixMultiply(MatrixMultiply(matOrigin, MatrixMultiply(matScale, matRotation)), matTranslation); rlMultMatrixf(MatrixToFloat(matTransform)); } -- cgit v1.2.3 From 501ef80bed15aff369b39712214169325fd5419b Mon Sep 17 00:00:00 2001 From: raysan5 Date: Fri, 3 Jun 2016 13:22:24 +0200 Subject: Updated camera 2d example --- examples/core_2d_camera.c | 92 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 71 insertions(+), 21 deletions(-) (limited to 'examples/core_2d_camera.c') diff --git a/examples/core_2d_camera.c b/examples/core_2d_camera.c index 5e6b7c6c..73e1d65f 100644 --- a/examples/core_2d_camera.c +++ b/examples/core_2d_camera.c @@ -11,6 +11,8 @@ #include "raylib.h" +#define MAX_BUILDINGS 100 + int main() { // Initialization @@ -20,16 +22,31 @@ int main() InitWindow(screenWidth, screenHeight, "raylib [core] example - 2d camera"); + Rectangle player = { 400, 280, 40, 40 }; + Rectangle buildings[MAX_BUILDINGS] = { 0, 0, 0, 0 }; + Color buildColors[MAX_BUILDINGS] = { 80, 80, 80, 255 }; + + 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 }; + } + Camera2D camera; + camera.target = (Vector2){ player.x + 20, player.y + 20 }; camera.offset = (Vector2){ 0, 0 }; - camera.target = (Vector2){ 400, 200 }; camera.rotation = 0.0f; camera.zoom = 1.0f; - Rectangle player = { 400, 200, 40, 40 }; - camera.target = (Vector2){ player.x + 20, player.y + 20 }; - SetTargetFPS(60); //-------------------------------------------------------------------------------------- @@ -38,24 +55,36 @@ int main() { // Update //---------------------------------------------------------------------------------- - if (IsKeyDown(KEY_RIGHT)) player.x -= 2; - else if (IsKeyDown(KEY_LEFT)) player.x += 2; - else if (IsKeyDown(KEY_UP)) player.y -= 2; - else if (IsKeyDown(KEY_DOWN)) player.y += 2; + 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 + } // Camera target follows player camera.target = (Vector2){ player.x + 20, player.y + 20 }; - if (IsKeyDown(KEY_R)) camera.rotation--; - else if (IsKeyDown(KEY_F)) camera.rotation++; + // Camera rotation controls + if (IsKeyDown(KEY_A)) camera.rotation--; + else if (IsKeyDown(KEY_S)) camera.rotation++; - // Camera controls - if (IsKeyDown(KEY_R)) camera.rotation--; - else if (IsKeyDown(KEY_F)) camera.rotation++; + // Limit camera rotation to 80 degrees (-40 to 40) + if (camera.rotation > 40) camera.rotation = 40; + else if (camera.rotation < -40) camera.rotation = -40; + // Camera zoom controls camera.zoom += ((float)GetMouseWheelMove()*0.05f); - if (IsKeyPressed(KEY_Z)) + 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)) { camera.zoom = 1.0f; camera.rotation = 0.0f; @@ -64,17 +93,38 @@ int main() // Draw //---------------------------------------------------------------------------------- - BeginDrawingEx(camera); - + BeginDrawing(); + ClearBackground(RAYWHITE); + + Begin2dMode(camera); - DrawText("2D CAMERA TEST", 20, 20, 20, GRAY); + 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); - DrawRectangle(0, 300, screenWidth, 50, GRAY); - DrawRectangleRec(player, RED); + DrawRectangle( 10, 10, 250, 113, Fade(SKYBLUE, 0.5f)); + DrawRectangleLines( 10, 10, 250, 113, BLUE); - DrawRectangle(camera.origin.x, 0, 1, screenHeight, GREEN); - DrawRectangle(0, camera.origin.y, screenWidth, 1, GREEN); + 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); EndDrawing(); //---------------------------------------------------------------------------------- -- cgit v1.2.3 From f88c95ce2d48730a7c4025a65d7b65402ffb8467 Mon Sep 17 00:00:00 2001 From: Ray Date: Thu, 30 Jun 2016 00:26:56 +0200 Subject: Updated examples and makefile --- examples/Makefile | 5 +++++ examples/core_2d_camera.c | 4 ++-- examples/core_oculus_rift.c | 2 +- examples/core_world_screen.c | 1 - 4 files changed, 8 insertions(+), 4 deletions(-) (limited to 'examples/core_2d_camera.c') diff --git a/examples/Makefile b/examples/Makefile index 15df3ec9..711f03a7 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -173,6 +173,7 @@ EXAMPLES = \ core_3d_camera_free \ core_3d_camera_first_person \ core_2d_camera \ + core_world_screen \ core_oculus_rift \ shapes_logo_raylib \ shapes_basic_shapes \ @@ -288,6 +289,10 @@ core_3d_camera_first_person: core_3d_camera_first_person.c # compile [core] example - 2d camera core_2d_camera: core_2d_camera.c $(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDES) $(LFLAGS) $(LIBS) -D$(PLATFORM) $(WINFLAGS) + +# compile [core] example - world screen +core_world_screen: core_world_screen.c + $(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDES) $(LFLAGS) $(LIBS) -D$(PLATFORM) $(WINFLAGS) # compile [core] example - oculus rift core_oculus_rift: core_oculus_rift.c diff --git a/examples/core_2d_camera.c b/examples/core_2d_camera.c index 73e1d65f..f2f219ef 100644 --- a/examples/core_2d_camera.c +++ b/examples/core_2d_camera.c @@ -23,8 +23,8 @@ int main() InitWindow(screenWidth, screenHeight, "raylib [core] example - 2d camera"); Rectangle player = { 400, 280, 40, 40 }; - Rectangle buildings[MAX_BUILDINGS] = { 0, 0, 0, 0 }; - Color buildColors[MAX_BUILDINGS] = { 80, 80, 80, 255 }; + Rectangle buildings[MAX_BUILDINGS]; + Color buildColors[MAX_BUILDINGS]; int spacing = 0; diff --git a/examples/core_oculus_rift.c b/examples/core_oculus_rift.c index 95b89106..c073d3d6 100644 --- a/examples/core_oculus_rift.c +++ b/examples/core_oculus_rift.c @@ -34,7 +34,7 @@ int main() Vector3 cubePosition = { 0.0f, 0.0f, 0.0f }; - //SetTargetFPS(90); // Set our game to run at 90 frames-per-second + SetTargetFPS(90); // Set our game to run at 90 frames-per-second //-------------------------------------------------------------------------------------- // Main game loop diff --git a/examples/core_world_screen.c b/examples/core_world_screen.c index f3798830..aa9505e8 100644 --- a/examples/core_world_screen.c +++ b/examples/core_world_screen.c @@ -63,7 +63,6 @@ int main() DrawText("Enemy: 100 / 100", cubeScreenPosition.x - MeasureText("Enemy: 100 / 100", 20) / 2, cubeScreenPosition.y, 20, BLACK); DrawText("Text is always on top of the cube", (screenWidth - MeasureText("Text is always on top of the cube", 20)) / 2, 25, 20, GRAY); - EndDrawing(); //---------------------------------------------------------------------------------- -- cgit v1.2.3