summary refs log tree commit diff
path: root/src/Game.cpp
blob: dd04b2f5317afff2aed5e9943b95c766f3db82ed (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
#include "Game.hpp"

#include <GLFW/glfw3.h>
#include "Time.hpp"
#include "Common/Sizes.hpp"
#include "Entities/Player.hpp"
#include "GFX/Camera.hpp"
#include "GFX/Window.hpp"
#include "World/Clouds.hpp"
#include "World/World.hpp"

namespace MC {

void Game::run() const {
    ASSERT_MAIN_THREAD();

    World::World world{};

    GFX::Camera camera{};

    World::Clouds clouds{};
    Entities::Player player{{0, World::Chunk::Height / 2.0, 0}};

    Input::register_callbacks(m_window);

    Input input;
    Time time;

    while (!m_window.should_close()) {
        m_window.poll_events();

#ifdef __APPLE__
        // Needs to happen on the main thread
        fix_macos_render();
#endif

        time.start_frame();

        input.update(time.frame_start());

        if (input.pressed(Key::Escape)) {
            m_window.close();
        }

        player.update(time, input, camera, world);
        clouds.update(time);

        GFX::Actions actions;

        for (auto chunk : world.get_visible_chunks(time, camera.position())) {
            auto position = chunk->chunk.value().position();
            actions.add({
                .program = GFX::Resources::Program::Terrain,
                .mesh = &chunk->land_mesh.value(),
                .transform = Transform(position),
            });

            actions.add({
                .program = GFX::Resources::Program::Terrain,
                .mesh = &chunk->water_mesh.value(),
                .transform = Transform(position - Vec3{0, 0.2, 0}),
                .alpha = 0.4,
            });
        }

        player.render(actions);
        clouds.render(actions, player.position());

        m_render_control->send_render_data({actions, camera});
        m_render_control->wait_for_render_finish();

        time.end_frame();
    }
}

void Game::fix_macos_render() const {
    static Bool moved = false;

    if(!moved) {
        I32 x, y;
        glfwGetWindowPos(m_window.get(), &x, &y);
        glfwSetWindowPos(m_window.get(), ++x, y);

        moved = true;
    }
}

}