aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.cpp
blob: d3481adf3b6d702bfcb8e4c1577a099098b687bb (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <stdlib.h>
#include "raylib.h"
#include "raymath.h"

#include "yabs_defines.h"
#include "yabs_utils.h"
#include "yabs_gameplay.h"

static yabs::gameState game_state;
static yabs::Scene3D sc3d {
// camera
{{5.0f, 5.0f, 5.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 1.0f, 0.0f}, 45.0f, 0},
// movement
{0.0f, 0.0f},
// rotation
{0.0f, -0.2f, 0.0f},
// mouse
{0.0f, 0.0f}
};

int main(void) {
    // create window
    InitWindow(G_SCREENWIDTH, G_SCREENHEIGHT, G_TITLE);

    // create image
    Image checked = GenImageChecked(2, 2, 1, 1, BLACK, GRAY);
    Texture2D texture = LoadTextureFromImage(checked);
    UnloadImage(checked);

    // create model with image as texture
    Model model = {0};
    model = LoadModelFromMesh(GenMeshCube(1.0f, 1.0f, 1.0f));
    model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture;

    // setup camera
    SetCameraMode(sc3d.camera, CAMERA_FIRST_PERSON);  // Set camera mode
    sc3d.camera.position = (Vector3){50.0f, 1.0f, 50.0f};
    sc3d.camera.target = (Vector3){50.0f, 1.0f, 50.0f};
    sc3d.camera.up = (Vector3){0.0f, 1.0f, 0.0f};
    sc3d.camera.fovy = 90.0f;
    sc3d.camera.projection = CAMERA_PERSPECTIVE;

    sc3d.rotation = (Vector3){0.0f, 0.0f, 0.0f};
    sc3d.lastMousePos = GetMousePosition();
    SetTargetFPS(60);
    // game loop
    while (!WindowShouldClose()) {
        switch (game_state) {
            case (yabs::TITLE):
            {
                break;
            }
            case (yabs::PICK):
            {
                break;
            }
            case (yabs::DEATH):
            {
                break;
            }
            case (yabs::GAMEPLAY):
            {
                break;
            }
            case (yabs::NEXT):
            {
                break;
            }
            case (yabs::ENDING):
            {
                break;
            }
        }
        // mouse look
        Vector2 mouseMovement =
            Vector2Subtract(GetMousePosition(), sc3d.lastMousePos);
        sc3d.lastMousePos = GetMousePosition();
        sc3d.rotation.x += (mouseMovement.x * -G_CAMERA_MOUSE_MOVE_SENSITIVITY);
        sc3d.rotation.y += (mouseMovement.y * -G_CAMERA_MOUSE_MOVE_SENSITIVITY);

        // direction array
        bool direction[4] = {IsKeyDown(KEY_W), IsKeyDown(KEY_S),
                             IsKeyDown(KEY_D), IsKeyDown(KEY_A)};

        // compute movement
        sc3d.movement = (Vector3){0, 0, 0};
        sc3d.movement.x =
            (sinf(sc3d.rotation.x) * direction[1] - sinf(sc3d.rotation.x) * direction[0] -
             cosf(sc3d.rotation.x) * direction[3] +
             cosf(sc3d.rotation.x) * direction[2]) /
            G_PLAYER_MOVEMENT_SENSITIVITY;
        sc3d.movement.z =
            (cosf(sc3d.rotation.x) * direction[1] - cosf(sc3d.rotation.x) * direction[0] +
             sinf(sc3d.rotation.x) * direction[3] -
             sinf(sc3d.rotation.x) * direction[2]) /
            G_PLAYER_MOVEMENT_SENSITIVITY;
        // elevation
        if (IsKeyDown(KEY_SPACE))
            sc3d.movement.y += 0.12f;
        if (IsKeyDown(KEY_LEFT_CONTROL))
            sc3d.movement.y -= 0.12f;

        // apply movement
        sc3d.camera.position.x += sc3d.movement.x / G_PLAYER_MOVEMENT_SENSITIVITY;
        sc3d.camera.position.y += sc3d.movement.y;
        sc3d.camera.position.z += sc3d.movement.z / G_PLAYER_MOVEMENT_SENSITIVITY;

        Matrix translation = MatrixTranslate(0, 0, (10));
        Matrix rotation2 = MatrixRotateXYZ(
            (Vector3){DOUBLEPI - sc3d.rotation.y, DOUBLEPI - sc3d.rotation.x, 0});
        Matrix transform = MatrixMultiply(translation, rotation2);

        // apply camera rotation
        sc3d.camera.target.x = sc3d.camera.position.x - transform.m12;
        sc3d.camera.target.y = sc3d.camera.position.y - transform.m13;
        sc3d.camera.target.z = sc3d.camera.position.z - transform.m14;
        BeginDrawing();
        ClearBackground(GRAY);
        BeginMode3D(sc3d.camera);

        // draw checkerboard model
        for (int x = 0; x < 50; x++) {
            for (int z = 0; z < 50; z++) {
                Vector3 position;
                position.x = x;
                position.y = 0;
                position.z = z;
                DrawModel(model, position, 1.0f, WHITE);
            }
        }

        EndMode3D();
        DrawFPS(0, 0);
        DrawText("Controls - Mouse / WASD / R/F.", 400, 0, 20, DARKGRAY);
        EndDrawing();
    }
    UnloadTexture(texture);
    UnloadModel(model);
    CloseWindow();
    return 0;
}