#include "engine.hpp" #include "config.hpp" #include #include "dbc.hpp" #include #include #include #include "image.hpp" #include #include void framebuffer_size_callback(GLFWwindow *, int width, int height) { glViewport(0, 0, width, height); } void mouse_callback(GLFWwindow *window, double xpos, double ypos) { Engine* engine = static_cast(glfwGetWindowUserPointer(window)); engine->mouse(xpos, ypos); engine->ui.mouse(xpos, ypos, guecs::NO_MODS); } void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) { Engine* engine = static_cast(glfwGetWindowUserPointer(window)); if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) { glfwSetWindowShouldClose(window, true); } if (key == GLFW_KEY_W && action == GLFW_PRESS) { engine->change_slide(-1); } if (key == GLFW_KEY_S && action == GLFW_PRESS) { engine->change_slide(1); } } void init_glfw() { glfwInit(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); #ifdef __APPLE__ glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); #endif } GLFWwindow* create_window() { GLFWwindow* window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Bezos2", NULL, NULL); dbc::check(window != NULL, "failed to open window"); glfwMakeContextCurrent(window); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); auto good = gladLoadGL(glfwGetProcAddress); dbc::check(good, "failed to load GLAD"); glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL); glfwSetCursorPosCallback(window, mouse_callback); glfwSetKeyCallback(window, key_callback); return window; } void setup() { glEnable(GL_BLEND); glEnable(GL_CULL_FACE); glEnable(GL_MULTISAMPLE); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glDepthFunc(GL_LESS); } void Engine::init_ui() { ui.position(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); ui.layout( "[=title|=*%(200,100)description|_|_|_]" "[*%(100,700)slide]" "[_]" "[_]" "[_]" "[_]" "[_]" "[_]"); ui.init(); auto click = ui.entity("slide"); ui.set(click, { [](auto mods){ fmt::println("HOVER on slide!"); } }); auto title = ui.entity("title"); ui.set(title, {L"The Text Test!" }); ui.set(title, {"assets/bone.png"}); ui.set(title, { [&](auto mods){ auto& at_title = ui.get(ui.entity("title")); fmt::println("HOVER on title at x={}, y={}", at_title.position.x, at_title.position.y); } }); ui.init(); } void Engine::init() { init_glfw(); window = create_window(); setup(); text = std::make_shared(); text->load_font(); screen = std::make_shared(); framebuffer = std::make_shared(); deck.analyze(); screen->init(); framebuffer->init(); glfwSetWindowUserPointer(window, this); init_ui(); } void Engine::render() { framebuffer->begin(); glClearColor(0.1f, 0.1f, 0.1f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); auto& slide = deck.slides.at(at_slide); float line = 0; for(auto& node : slide.content) { line++; float y = (text->font_size + 20) * line; if(node.type == NodeType::TEXT || node.type == NodeType::H1 || node.type == NodeType::UL) { text->render(node.content, 25.0f, y, 1.0f, glm::vec3(1.0f, 1.0f, 1.0f)); } else if(node.type == NodeType::IMAGE) { dbc::check(node.image != nullptr, "Node's image not configured."); node.image->render(); } else { dbc::sentinel("Unknown NodeType"); } } ui.render(); framebuffer->commit(); framebuffer->draw(*screen); glfwSwapBuffers(window); } void Engine::change_slide(int dir) { if(deck.slides.size() == 0) at_slide = 0; at_slide = std::clamp(at_slide + dir, 0, int(deck.slides.size() - 1)); } void Engine::update() { ui.update(); glfwPollEvents(); } void Engine::quit() { glfwTerminate(); } bool Engine::active() { return !glfwWindowShouldClose(window); }