From 2c7acb5efa36b774cb7d84881e26b7a2a3bc490f Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Fri, 13 Nov 2026 01:22:04 -0500 Subject: [PATCH] Basic little rectangle showing. --- meson.build | 2 +- shaders/rect.frag.glsl | 8 ++++++ shaders/rect.vert.glsl | 9 +++++++ src/engine.cpp | 9 +++++-- src/guecs/opengl/components.cpp | 11 +++++++-- src/guecs/opengl/components.hpp | 3 ++- src/meson.build | 1 + src/rect.cpp | 43 +++++++++++++++++++++++++++++++++ src/rect.hpp | 28 +++++++++++++++++++++ 9 files changed, 108 insertions(+), 6 deletions(-) create mode 100644 shaders/rect.frag.glsl create mode 100644 shaders/rect.vert.glsl create mode 100644 src/rect.cpp create mode 100644 src/rect.hpp diff --git a/meson.build b/meson.build index 5c18762..6420be6 100644 --- a/meson.build +++ b/meson.build @@ -17,7 +17,7 @@ cpp_args=[ ] link_args=[] # these are passed as override_defaults -exe_defaults = [ 'warning_level=2', 'werror=false'] +exe_defaults = [ 'warning_level=3', 'werror=false'] fmt = subproject('fmt').get_variable('fmt_dep') fuc2 = subproject('fuc2').get_variable('fuc2_dep') diff --git a/shaders/rect.frag.glsl b/shaders/rect.frag.glsl new file mode 100644 index 0000000..5131efd --- /dev/null +++ b/shaders/rect.frag.glsl @@ -0,0 +1,8 @@ +#version 460 core +out vec4 FragColor; +uniform vec4 color; + +void main() +{ + FragColor = vec4(1.0, 0.0, 0.0, 1.0); +} diff --git a/shaders/rect.vert.glsl b/shaders/rect.vert.glsl new file mode 100644 index 0000000..97817a1 --- /dev/null +++ b/shaders/rect.vert.glsl @@ -0,0 +1,9 @@ +#version 460 core +layout (location = 0) in vec2 aPos; // the position variable has attribute position 0 + +uniform mat4 projection; + +void main() +{ + gl_Position = projection * vec4(aPos, 0.0, 1.0); // see how we directly give a vec3 to vec4's constructor +} diff --git a/src/engine.cpp b/src/engine.cpp index 3dc6e30..e4c3afe 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -81,14 +81,14 @@ void setup() { void Engine::init_ui() { ui.position(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); ui.layout( - "[=title|=*%(200,100)description|_|_|_]" "[*%(100,700)slide]" "[_]" "[_]" "[_]" "[_]" "[_]" - "[_]"); + "[_]" + "[=title|=*%(200,100)description|_|_|_]"); ui.init(); auto click = ui.entity("slide"); @@ -107,6 +107,11 @@ void Engine::init_ui() { fmt::println("HOVER on title at x={}, y={}", at_title.position.x, at_title.position.y); } }); + ui.set(title, {}); + + auto desc = ui.entity("description"); + ui.set(desc, {"assets/bone.png"}); + ui.set(desc, {}); ui.init(); } diff --git a/src/guecs/opengl/components.cpp b/src/guecs/opengl/components.cpp index 00ec6ee..75dbd86 100644 --- a/src/guecs/opengl/components.cpp +++ b/src/guecs/opengl/components.cpp @@ -58,6 +58,7 @@ namespace guecs { if(stretch) { // handle stretch + // glm::vec2 bounds{cell.w, cell.h}; // get this from the image } else { float box_width = float(cell.w - padding * 2); float box_height = float(cell.h - padding * 2); @@ -84,12 +85,18 @@ namespace guecs { } void Rectangle::init(lel::Cell& cell) { - glm::vec2 size{float(cell.w) - padding * 2.0f, float(cell.h) - padding * 2.0f}; + shape = make_shared( + float(cell.x), float(cell.y), + float(cell.w) - padding * 2.0f, float(cell.h) - padding * 2.0f); + shape->init(); + update(); - // sfml_center_helper(shape, cell, padding); } void Rectangle::render() { + if(shape != nullptr) { + shape->render(); + } } void Rectangle::update() { diff --git a/src/guecs/opengl/components.hpp b/src/guecs/opengl/components.hpp index 5d3e30b..f2a8f30 100644 --- a/src/guecs/opengl/components.hpp +++ b/src/guecs/opengl/components.hpp @@ -8,6 +8,7 @@ #include #include "text.hpp" #include "shader.hpp" +#include "rect.hpp" namespace guecs { using std::shared_ptr, std::wstring, std::string; @@ -58,7 +59,7 @@ namespace guecs { Color color = THEME.FILL_COLOR; Color border_color = THEME.BORDER_COLOR; int border_px = THEME.BORDER_PX; - void* shape = nullptr; // CHANGE ME BRO + std::shared_ptr shape = nullptr; void init(lel::Cell& cell); void render(); diff --git a/src/meson.build b/src/meson.build index e5f2e24..dfcc1c5 100644 --- a/src/meson.build +++ b/src/meson.build @@ -18,4 +18,5 @@ sources = [ 'src/guecs/theme.cpp', 'src/guecs/opengl/components.cpp', 'src/guecs/opengl/backend.cpp', + 'src/rect.cpp', ] diff --git a/src/rect.cpp b/src/rect.cpp new file mode 100644 index 0000000..3e53d40 --- /dev/null +++ b/src/rect.cpp @@ -0,0 +1,43 @@ +#include "rect.hpp" +#include +#include +#include +#include +#include +#include "config.hpp" + +void Rect::init() { + // glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); + + shader.use(); + + glm::mat4 projection = glm::ortho(0.0f, static_cast(WINDOW_WIDTH), 0.0f, static_cast(WINDOW_HEIGHT)); + shader.setMat4("projection", projection); + shader.setVec4("color", color); + + glGenVertexArrays(1, &rect_vao); + glGenBuffers(1, &rect_vbo); + + glBindVertexArray(rect_vao); + glBindBuffer(GL_ARRAY_BUFFER, rect_vbo); + glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 6 * 2, NULL, GL_DYNAMIC_DRAW); + + // position attrib location=0 + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0); + glBindVertexArray(0); +} + +void Rect::render() { + shader.use(); + glBindVertexArray(rect_vao); + + glBindBuffer(GL_ARRAY_BUFFER, rect_vbo); + glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices); + + glBindBuffer(GL_ARRAY_BUFFER, 0); + // render quad + glDrawArrays(GL_TRIANGLES, 0, 6); + + glBindVertexArray(0); +} diff --git a/src/rect.hpp b/src/rect.hpp new file mode 100644 index 0000000..24d4106 --- /dev/null +++ b/src/rect.hpp @@ -0,0 +1,28 @@ +#pragma once +#include "shader.hpp" +#include "config.hpp" + +struct Rect { + float x = 0.0f; + float y = 0.0f; + float w = 0.0f; + float h = 0.0f; + glm::vec4 color{1.0f, 1.0f, 1.0f, 1.0f}; + + float vertices[6][2] = { + { x, WINDOW_HEIGHT - y + h}, + { x, WINDOW_HEIGHT - y}, + { x + w, WINDOW_HEIGHT - y}, + { x, WINDOW_HEIGHT - y + h}, + { x + w, WINDOW_HEIGHT - y}, + { x + w, WINDOW_HEIGHT - y + h} + }; + + unsigned int rect_vao = UINT_MAX; + unsigned int rect_vbo = UINT_MAX; + + Shader shader{{"shaders/rect.vert.glsl", "shaders/rect.frag.glsl", ""}}; + + void init(); + void render(); +};