Refactored and setup for better analyzer.

master
Zed A. Shaw 2 weeks ago
parent 6013283b00
commit 13e072853f
  1. 2
      Makefile
  2. 2
      TODO.md
  3. 5
      lua/builder_test.lua
  4. 2
      src/bezos.cpp
  5. 24
      src/document.cpp
  6. 3
      src/document.hpp
  7. 4
      src/image.cpp
  8. 108
      src/main.cpp
  9. 123
      src/render.cpp
  10. 16
      src/render.hpp
  11. 2
      src/text.hpp

@ -33,7 +33,7 @@ debug: build
gdb --nx -x .gdbinit --ex run --args builddir/bezos_tests
debug_run: build
gdb --nx -x .gdbinit --batch --ex run --ex bt --ex q --args builddir/bezos_tests
gdb --nx -x .gdbinit --batch --ex run --ex bt --ex q --args builddir/bezos
clean:
meson compile --clean -C builddir

@ -0,0 +1,2 @@
* Make the Deck::analyze do a more thorough analysis/check.
* Get the code online.

@ -31,7 +31,10 @@ slide{
t [[
Makes programming content
more interesting.
]]
]],
image{
src="assets/test.jpg"
}
}
slide{

@ -40,7 +40,9 @@ namespace Bezos {
lua.script_file(file);
// BUG: don't do this, instead set the builder on the lua script
Builder builder = lua["DECK"];
return builder.deck;
}

@ -1,2 +1,26 @@
#include "document.hpp"
#include <fmt/core.h>
void Deck::analyze() {
// Needed work:
// * Character mapping for the fonts
// * Checks for errors
// * Resource prep: images, fonts, font glyphs
for(auto& slide : slides) {
for(auto& node : slide.content) {
switch(node.type) {
case NodeType::IMAGE: {
dbc::check(node.image == nullptr, "Got an image node that's already setup.");
node.image = std::make_shared<Image>();
std::string src = std::any_cast<std::string>(node.config.at("src"));
node.image->load(src);
break;
}
default:
fmt::println("Skipping unknown node");
}
}
}
analyzed = true;
}

@ -41,4 +41,7 @@ struct Slide {
struct Deck {
std::vector<Slide> slides{};
std::unordered_map<std::string, std::any> config{};
bool analyzed = false;
void analyze();
};

@ -59,6 +59,9 @@ void Image::render() {
glBindVertexArray(image_vao);
glBindTexture(GL_TEXTURE_2D, texture_id);
glDrawArrays(GL_TRIANGLES, 0, 6);
glBindTexture(GL_TEXTURE_2D, 0);
glBindVertexArray(0);
}
Image::Image() {
@ -76,4 +79,5 @@ Image::Image() {
// color attribute
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)(2 * sizeof(float)));
glEnableVertexAttribArray(1);
glBindVertexArray(0);
}

@ -1,116 +1,20 @@
#define _USE_MATH_DEFINES
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <stb_image.h>
#include <fmt/core.h>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include "config.hpp"
#include "render.hpp"
#include "bezos.hpp"
void framebuffer_size_callback(GLFWwindow *window, int width, int height);
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
}
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);
}
}
GLFWwindow* create_window() {
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_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_DEPTH_TEST);
glEnable(GL_BLEND);
glEnable(GL_CULL_FACE);
glEnable(GL_MULTISAMPLE);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDepthFunc(GL_LESS);
}
void update(GLFWwindow* window) {
glfwPollEvents();
}
void render(GLFWwindow* window, Engine &state) {
state.render();
glfwSwapBuffers(window);
}
void quit(GLFWwindow* window) {
glfwTerminate();
}
int main() {
init_glfw();
auto window = create_window();
setup();
Deck deck = Bezos::load("lua/builder_test.lua");
Engine state{.deck=deck};
state.init();
Engine state(deck);
glfwSetWindowUserPointer(window, &state);
while(!glfwWindowShouldClose(window)) {
update(window);
render(window, state);
while(state.active()) {
state.update();
state.render();
}
quit(window);
state.quit();
return 0;
}

@ -6,17 +6,97 @@
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "image.hpp"
#include <glad/glad.h>
Engine::Engine(Deck &deck) :
deck(deck)
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)
{
text.load_font();
screen.init();
framebuffer.init();
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(SCR_WIDTH, SCR_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_DEPTH_TEST);
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>();
screen = std::make_shared<Screen>();
framebuffer = std::make_shared<FrameBuffer>();
text->load_font();
// BUG: this will fail if it's before text.load_font
deck.analyze();
screen->init();
framebuffer->init();
glfwSetWindowUserPointer(window, this);
}
void Engine::render() {
framebuffer.begin();
framebuffer->begin();
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
@ -26,25 +106,22 @@ void Engine::render() {
for(auto& node : slide.content) {
line++;
float y = (text.font_size + 20) * 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, SCR_HEIGHT - y, 1.0f, glm::vec3(1.0f, 1.0f, 1.0f));
} else if(node.type == NodeType::IMAGE) {
if(node.image == nullptr) {
node.image = std::make_shared<Image>();
std::string src = std::any_cast<std::string>(node.config.at("src"));
node.image->load(src);
}
text->render(node.content, 25.0f, SCR_HEIGHT - y, 1.0f, glm::vec3(1.0f, 1.0f, 1.0f));
} else if(node.type == NodeType::IMAGE && node.image != nullptr) {
dbc::check(node.image != nullptr, "Node's image not configured.");
node.image->render();
} else {
dbc::sentinel("Unknown NodeType");
}
}
framebuffer.commit();
framebuffer.draw(screen);
framebuffer->commit();
framebuffer->draw(*screen);
glfwSwapBuffers(window);
}
void Engine::change_slide(int dir) {
@ -52,3 +129,15 @@ void Engine::change_slide(int dir) {
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);
}

@ -3,17 +3,18 @@
#include "framebuffer.hpp"
#include "text.hpp"
#include "document.hpp"
#include <GLFW/glfw3.h>
#include <memory>
struct Engine {
Deck& deck;
FrameBuffer framebuffer;
Screen screen;
Text text{.font_size=100};
std::shared_ptr<FrameBuffer> framebuffer = nullptr;
std::shared_ptr<Screen> screen = nullptr;
std::shared_ptr<Text> text = nullptr;
int at_slide = 0;
float mouse_x = 0.0f;
float mouse_y = 0.0f;
Engine(Deck& deck);
GLFWwindow* window = nullptr;
void render();
void change_slide(int dir);
@ -22,4 +23,9 @@ struct Engine {
mouse_x = x;
mouse_y = y;
}
void update();
void quit();
void init();
bool active();
};

@ -12,7 +12,7 @@ struct Character {
};
struct Text {
int font_size = 48;
int font_size = 108;
std::string font{"assets/fonts/text.ttf"};
Shader text_shader{{"shaders/text.vert.glsl", "shaders/text.frag.glsl", ""}};
unsigned int text_vao = UINT_MAX;

Loading…
Cancel
Save