Bezos2 is my latest version of the Bezos learning project that uses raw OpenGL and C++ with Lua.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Bezos2/src/engine.cpp

144 lines
3.3 KiB

#include "engine.hpp"
#include "config.hpp"
#include <algorithm>
#include "dbc.hpp"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "image.hpp"
#include <glad/glad.h>
#include <magic_enum/magic_enum.hpp>
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<Engine*>(glfwGetWindowUserPointer(window));
engine->mouse(xpos, ypos);
}
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
Engine* engine = static_cast<Engine*>(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, "LearnOpenGL", NULL, NULL);
dbc::check(window != NULL, "failed to open window");
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
auto good = gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
dbc::check(good, "failed to load GLAD");
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
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() {
init_glfw();
window = create_window();
setup();
text = std::make_shared<Text>();
text->load_font();
screen = std::make_shared<Screen>();
framebuffer = std::make_shared<FrameBuffer>();
deck.analyze();
screen->init();
framebuffer->init();
glfwSetWindowUserPointer(window, this);
}
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, WINDOW_HEIGHT - 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");
}
}
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() {
glfwPollEvents();
}
void Engine::quit() {
glfwTerminate();
}
bool Engine::active() {
return !glfwWindowShouldClose(window);
}