Basic little rectangle showing.

master
Zed A. Shaw 5 days ago
parent 43bfba0d71
commit 2c7acb5efa
  1. 2
      meson.build
  2. 8
      shaders/rect.frag.glsl
  3. 9
      shaders/rect.vert.glsl
  4. 9
      src/engine.cpp
  5. 11
      src/guecs/opengl/components.cpp
  6. 3
      src/guecs/opengl/components.hpp
  7. 1
      src/meson.build
  8. 43
      src/rect.cpp
  9. 28
      src/rect.hpp

@ -17,7 +17,7 @@ cpp_args=[
] ]
link_args=[] link_args=[]
# these are passed as override_defaults # 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') fmt = subproject('fmt').get_variable('fmt_dep')
fuc2 = subproject('fuc2').get_variable('fuc2_dep') fuc2 = subproject('fuc2').get_variable('fuc2_dep')

@ -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);
}

@ -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
}

@ -81,14 +81,14 @@ void setup() {
void Engine::init_ui() { void Engine::init_ui() {
ui.position(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); ui.position(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
ui.layout( ui.layout(
"[=title|=*%(200,100)description|_|_|_]"
"[*%(100,700)slide]" "[*%(100,700)slide]"
"[_]" "[_]"
"[_]" "[_]"
"[_]" "[_]"
"[_]" "[_]"
"[_]" "[_]"
"[_]"); "[_]"
"[=title|=*%(200,100)description|_|_|_]");
ui.init(); ui.init();
auto click = ui.entity("slide"); 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); fmt::println("HOVER on title at x={}, y={}", at_title.position.x, at_title.position.y);
} }
}); });
ui.set<guecs::Rectangle>(title, {});
auto desc = ui.entity("description");
ui.set<guecs::Sprite>(desc, {"assets/bone.png"});
ui.set<guecs::Rectangle>(desc, {});
ui.init(); ui.init();
} }

@ -58,6 +58,7 @@ namespace guecs {
if(stretch) { if(stretch) {
// handle stretch // handle stretch
// glm::vec2 bounds{cell.w, cell.h}; // get this from the image
} else { } else {
float box_width = float(cell.w - padding * 2); float box_width = float(cell.w - padding * 2);
float box_height = float(cell.h - padding * 2); float box_height = float(cell.h - padding * 2);
@ -84,12 +85,18 @@ namespace guecs {
} }
void Rectangle::init(lel::Cell& cell) { void Rectangle::init(lel::Cell& cell) {
glm::vec2 size{float(cell.w) - padding * 2.0f, float(cell.h) - padding * 2.0f}; shape = make_shared<Rect>(
float(cell.x), float(cell.y),
float(cell.w) - padding * 2.0f, float(cell.h) - padding * 2.0f);
shape->init();
update(); update();
// sfml_center_helper(shape, cell, padding);
} }
void Rectangle::render() { void Rectangle::render() {
if(shape != nullptr) {
shape->render();
}
} }
void Rectangle::update() { void Rectangle::update() {

@ -8,6 +8,7 @@
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include "text.hpp" #include "text.hpp"
#include "shader.hpp" #include "shader.hpp"
#include "rect.hpp"
namespace guecs { namespace guecs {
using std::shared_ptr, std::wstring, std::string; using std::shared_ptr, std::wstring, std::string;
@ -58,7 +59,7 @@ namespace guecs {
Color color = THEME.FILL_COLOR; Color color = THEME.FILL_COLOR;
Color border_color = THEME.BORDER_COLOR; Color border_color = THEME.BORDER_COLOR;
int border_px = THEME.BORDER_PX; int border_px = THEME.BORDER_PX;
void* shape = nullptr; // CHANGE ME BRO std::shared_ptr<Rect> shape = nullptr;
void init(lel::Cell& cell); void init(lel::Cell& cell);
void render(); void render();

@ -18,4 +18,5 @@ sources = [
'src/guecs/theme.cpp', 'src/guecs/theme.cpp',
'src/guecs/opengl/components.cpp', 'src/guecs/opengl/components.cpp',
'src/guecs/opengl/backend.cpp', 'src/guecs/opengl/backend.cpp',
'src/rect.cpp',
] ]

@ -0,0 +1,43 @@
#include "rect.hpp"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glad/gl.h>
#include <GLFW/glfw3.h>
#include "config.hpp"
void Rect::init() {
// glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
shader.use();
glm::mat4 projection = glm::ortho(0.0f, static_cast<float>(WINDOW_WIDTH), 0.0f, static_cast<float>(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);
}

@ -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();
};
Loading…
Cancel
Save