aboutsummaryrefslogtreecommitdiff
path: root/examples/core_2d_camera.c
blob: 5e6b7c6c813ac706f608b3cd5d4ff98e62a90c84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*******************************************************************************************
*
*   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.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);
    //--------------------------------------------------------------------------------------

    // Main game loop
    while (!WindowShouldClose())    // Detect window close button or ESC key
    {
        // 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;
        
        // 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 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
        //----------------------------------------------------------------------------------
        BeginDrawingEx(camera);

            ClearBackground(RAYWHITE);

            DrawText("2D CAMERA TEST", 20, 20, 20, GRAY);
            
            DrawRectangle(0, 300, screenWidth, 50, GRAY);
            DrawRectangleRec(player, RED);
            
            DrawRectangle(camera.origin.x, 0, 1, screenHeight, GREEN);
            DrawRectangle(0, camera.origin.y, screenWidth, 1, GREEN);

        EndDrawing();
        //----------------------------------------------------------------------------------
    }

    // De-Initialization
    //--------------------------------------------------------------------------------------   
    CloseWindow();        // Close window and OpenGL context
    //--------------------------------------------------------------------------------------

    return 0;
}