summaryrefslogtreecommitdiff
path: root/src/engine.cc
blob: d4424a7f81ea142ef8f8a0d4a0e675fd51291aca (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
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "engine.h"
#include "context.h"

#include "objects/scene.h"

using namespace Mauri;

auto View::center_scale_viewport() -> void
{
    Render::set_viewport(this->camera_pos[0], this->camera_pos[1],
        this->ortho_width / this->scale, this->ortho_height / this->scale);
}

auto View::center_viewport(f32 buffer_width, f32 buffer_height) -> void
{
    Render::set_viewport((this->width - buffer_width) / -2,
        (this->height - buffer_height) / -2, buffer_width, buffer_height);
}

auto View::default_viewport(f32 buffer_width, f32 buffer_height) -> void
{
    Render::set_viewport(0, 0, buffer_width, buffer_height);
}

auto View::center(f32 buffer_width, f32 buffer_height) -> void
{
    f32 w_scale = this->width / buffer_width;
    f32 h_scale = this->height / buffer_height;

    this->scale = (w_scale <= h_scale) ? w_scale : h_scale;

    f32 scaled_width = this->width / this->scale;
    f32 scaled_height = this->height / this->scale;

    this->camera_pos[0] = ((scaled_width - buffer_width) / -2.f);
    this->camera_pos[1] = ((scaled_height - buffer_height) / -2.f);
}

Engine::Engine(Scene *scene, f32 rate, bool audio)
    : scene(scene), rate(rate), audio_enabled(audio)
{
    this->view.width = scene->width;
    this->view.height = scene->height;
    this->view.ortho_width = scene->width;
    this->view.ortho_height = scene->height;

    this->view.texel_size[0] = 1.f / this->view.width;
    this->view.texel_size[1] = 1.f / this->view.height;

    this->view.texel_half_size[0] = .5f / this->view.width;
    this->view.texel_half_size[1] = .5f / this->view.height;

    this->view.center(context()->width(), context()->height());
    this->view.camera_pos[0] += scene->cam.offset[0];
    this->view.camera_pos[1] += scene->cam.offset[1];

    // No audio for now.
    this->audio_enabled = false;

    this->shader = get_shader("hdr_downsample");
    this->render_shader = this->shader->compile({});

    this->create_framebuffer(COMBINE_BUFFER, this->view.width, this->view.height, 1.f);
    this->combine_buffer = this->get_framebuffer(COMBINE_BUFFER);
    this->create_framebuffer(MIPMAPPED_BUFFER, this->view.width, this->view.height, 1.f);

    this->default_mat = glm::ortho(-1.f, 1.f, -1.f, 1.f, -3.f, 1.f);
    this->default_mat = glm::scale(this->default_mat, vec3{1.f, 1.f, 0.001f});
    this->default_mat_inverse = glm::inverse(this->default_mat);

    this->default_object = new RenderObject(DEFAULT);
    this->flipped_object = new RenderObject(DEFAULT_FLIPPED);
    this->fullscreen_object = new RenderObject(FULLSCREEN);
}

auto Engine::create_framebuffer(const std::string &name, f32 width, f32 height, f32 scale) -> void
{
    this->framebuffers.emplace_back(new Framebuffer(name, width, height, scale));
}

auto Engine::get_framebuffer(const std::string &name) -> Framebuffer *
{
    for (Framebuffer *buffer : this->framebuffers)
    {
        if (buffer->name == name) return buffer;
    }
    return nullptr;
}

auto Engine::get_framebuffer_texture(const std::string &name) -> Texture *
{
    Framebuffer *buffer = this->get_framebuffer(name);
    return ((buffer) ? buffer->texture : nullptr);
}

auto Engine::update() -> void
{
    this->view.cursor_pos[0] = (f32)context()->pointer_x;
    this->view.cursor_pos[1] = (f32)context()->pointer_y;

    this->view.cursor_pos[0] -= this->view.camera_pos[0];
    this->view.cursor_pos[1] -= this->view.camera_pos[1];

    this->view.cursor_pos[0] *= this->view.scale;
    this->view.cursor_pos[1] *= this->view.scale;

    this->view.cursor_pos[1] = this->view.height - this->view.cursor_pos[1];
}

auto Engine::draw() -> void
{
    if (this->scene->clear_enabled)
    {
        this->combine_buffer->buffer->clear(this->scene->clear_color);
    }

    this->scene->draw(this);

    Render::bind_framebuffer(0);
    this->shader->bind(this->render_shader, this, nullptr, &this->default_mat, nullptr);

    this->combine_buffer->texture->bind(0);

    this->view.center_scale_viewport();

    this->flipped_object->bind();
    this->flipped_object->draw();
}

auto Engine::run() -> void
{
    while (!context()->should_close_window())
    {
        this->time = context()->current_time();
        context()->poll();
        this->update();
        this->draw();
        context()->swap_buffers();
    }
}

Engine::~Engine()
{
    for (Framebuffer *buffer : this->framebuffers)
    {
        delete buffer;
    }

    delete this->shader;

    delete this->default_object;
    delete this->flipped_object;
    delete this->fullscreen_object;
}