#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; }