From 5ea18b9426823e92f1fc16e686702f2caadf83c9 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sat, 5 Mar 2016 15:40:08 +0100 Subject: [PATCH] Support 2d camera system -IN PROGRESS- --- examples/core_2d_camera.c | 71 +++++++++++++++++++++++++++++++++++++++ src/core.c | 21 ++++++++++-- src/raylib.h | 11 +++++- 3 files changed, 100 insertions(+), 3 deletions(-) create mode 100644 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 diff --git a/src/core.c b/src/core.c index a8cc593a..870ca3b4 100644 --- a/src/core.c +++ b/src/core.c @@ -560,8 +560,25 @@ void BeginDrawing(void) // NOTE: Not required with OpenGL 3.3+ } -// Setup drawing canvas with extended parameters -void BeginDrawingEx(int blendMode, Shader shader, Matrix transform) +// Setup drawing canvas with 2d camera +void BeginDrawingEx(Camera2D camera) +{ + BeginDrawing(); + + // TODO: Consider origin offset on position, rotation, scaling + + 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 matTransform = MatrixMultiply(MatrixMultiply(matScale, matRotation), matTranslation); + + rlMultMatrixf(MatrixToFloat(matTransform)); +} + +// Setup drawing canvas with pro parameters +void BeginDrawingPro(int blendMode, Shader shader, Matrix transform) { BeginDrawing(); diff --git a/src/raylib.h b/src/raylib.h index b6cf98fe..7173a556 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -315,6 +315,14 @@ typedef struct Camera { float fovy; // Field-Of-View apperture in Y (degrees) } Camera; +// Camera2D type, defines a 2d camera +typedef struct Camera2D { + Vector2 position; // Camera position + Vector2 origin; // Camera origin (for rotation and zoom) + float rotation; // Camera rotation in degrees + float zoom; // Camera zoom (scaling), should be 1.0f by default +} Camera2D; + // Bounding box type typedef struct BoundingBox { Vector3 min; @@ -528,7 +536,8 @@ int GetScreenHeight(void); // Get current scree void ClearBackground(Color color); // Sets Background Color void BeginDrawing(void); // Setup drawing canvas to start drawing -void BeginDrawingEx(int blendMode, Shader shader, Matrix transform); // Setup drawing canvas with extended parameters +void BeginDrawingEx(Camera2D camera); // Setup drawing canvas with 2d camera +void BeginDrawingPro(int blendMode, Shader shader, Matrix transform); // Setup drawing canvas with pro parameters void EndDrawing(void); // End canvas drawing and Swap Buffers (Double Buffering) void Begin3dMode(Camera camera); // Initializes 3D mode for drawing (Camera setup)