Text, Rect, and Image now display within cells. Rect had problems because of face culling.

master
Zed A. Shaw 2 days ago
parent 2c7acb5efa
commit c4e0844e0a
  1. 2
      shaders/rect.frag.glsl
  2. 4
      src/document.cpp
  3. 3
      src/document.hpp
  4. 5
      src/engine.cpp
  5. 41
      src/guecs/opengl/components.cpp
  6. 6
      src/guecs/theme.hpp
  7. 7
      src/rect.cpp
  8. 14
      src/rect.hpp

@ -4,5 +4,5 @@ uniform vec4 color;
void main()
{
FragColor = vec4(1.0, 0.0, 0.0, 1.0);
FragColor = color;
}

@ -23,3 +23,7 @@ void Deck::analyze() {
analyzed = true;
}
glm::vec4 Color::to_vec4() {
return {float(r) / 255.0f, float(g) / 255.0f, float(b) / 255.0f, float(a) / 255.0f};
}

@ -6,6 +6,7 @@
#include <cstdint>
#include <memory>
#include "image.hpp"
#include <glm/glm.hpp>
enum class NodeType {
TEXT,
@ -20,6 +21,8 @@ struct Color {
uint8_t g=0;
uint8_t b=0;
uint8_t a=255;
glm::vec4 to_vec4();
};
struct Node {

@ -71,6 +71,7 @@ GLFWwindow* create_window() {
void setup() {
glEnable(GL_BLEND);
glEnable(GL_CULL_FACE);
glEnable(GL_MULTISAMPLE);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
@ -99,7 +100,7 @@ void Engine::init_ui() {
});
auto title = ui.entity("title");
ui.set<guecs::Text>(title, {L"The Text Test!" });
ui.set<guecs::Text>(title, {L"The Text Test!", 24});
ui.set<guecs::Sprite>(title, {"assets/bone.png"});
ui.set<guecs::Clickable>(title, {
[&](auto mods){
@ -110,7 +111,7 @@ void Engine::init_ui() {
ui.set<guecs::Rectangle>(title, {});
auto desc = ui.entity("description");
ui.set<guecs::Sprite>(desc, {"assets/bone.png"});
// ui.set<guecs::Sprite>(desc, {"assets/bone.png"});
ui.set<guecs::Rectangle>(desc, {});
ui.init();

@ -16,17 +16,14 @@ namespace guecs {
text = make_shared<::Text>();
text->font_size = size;
text->load_font();
if(centered || cell.center) {
// WRONG! Need the scale here...
auto& ch = text->characters.at('H');
auto text_cell = lel::center(ch.size.x, ch.size.y, cell);
// this stupid / 2 is because SFML renders from baseline rather than from the claimed bounding box
position = {float(text_cell.x), float(text_cell.y) - text_cell.h / 2};
if(centered || cell.center) {
// BUG: this doesn't center, but need to render the whole text then center it
// BUG: lel::center doesn't do what I think it does
position = {float(cell.x + padding * 2), cell.mid_y + ch.size.y / 2};
} else {
position = {float(cell.x + padding * 2), float(cell.y + padding * 2)};
position = {float(cell.x + padding * 2), float(cell.y + ch.size.y + padding * 2)};
}
}
@ -55,27 +52,24 @@ namespace guecs {
// setup the image size and such
glm::vec2 bounds{image->width, image->height}; // get this from the image
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);
float scale = std::min(box_width / float(bounds.x), box_height / float(bounds.y));
// no stretch so scale it
image->scale.x = (bounds.x / float(WINDOW_WIDTH)) * scale;
image->scale.y = (bounds.y / float(WINDOW_HEIGHT)) * scale;
// find center point of cell since origin of image is center (for now)
// BUG: lel::center doesn't work here, why?
image->position.x = (cell.x + box_width / 2);
image->position.y = (cell.y + box_height / 2);
fmt::println("pos: {},{} | bounds: {},{} | box: {},{} | scale: {}",
image->position.x, image->position.y, bounds.x, bounds.y, box_width, box_height, scale);
if(stretch) {
// handle stretch, but should use OpenGL image texture stretch options
image->scale.x = (box_width / float(WINDOW_WIDTH));
image->scale.y = (box_height / float(WINDOW_HEIGHT));
} else {
// no stretch so scale it
float scale = std::min(box_width / float(bounds.x), box_height / float(bounds.y));
image->scale.x = (bounds.x / float(WINDOW_WIDTH)) * scale;
image->scale.y = (bounds.y / float(WINDOW_HEIGHT)) * scale;
}
// used the center helper here
}
@ -87,7 +81,10 @@ namespace guecs {
void Rectangle::init(lel::Cell& cell) {
shape = make_shared<Rect>(
float(cell.x), float(cell.y),
float(cell.w) - padding * 2.0f, float(cell.h) - padding * 2.0f);
float(cell.w) - padding * 2.0f,
float(cell.h) - padding * 2.0f,
color.to_vec4());
shape->init();
update();

@ -23,9 +23,9 @@ namespace guecs {
int BORDER_PX = 1;
unsigned int TEXT_SIZE = 30;
int LABEL_SIZE = 20;
Color FILL_COLOR{255,0,0,255};
Color TEXT_COLOR{255,0,0,255};
Color BG_COLOR{255,0,0,255};
Color FILL_COLOR{20,20,20,255};
Color TEXT_COLOR{255,255,255,255};
Color BG_COLOR{50,50,50,255};
Color BORDER_COLOR{255,0,0,255};
Color BG_COLOR_DARK{0,0,0,255};
std::string FONT_FILE_NAME{"you_forgot_to_init.otf"};

@ -6,6 +6,12 @@
#include <GLFW/glfw3.h>
#include "config.hpp"
void Rect::dump() {
for(size_t i = 0; i < 6; i++) {
fmt::println("{}: x={}, y={}", i, vertices[i][0], vertices[i][1]);
}
}
void Rect::init() {
// glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
@ -26,6 +32,7 @@ void Rect::init() {
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0);
glBindVertexArray(0);
dump();
}
void Rect::render() {

@ -10,12 +10,13 @@ struct Rect {
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}
{ x, WINDOW_HEIGHT - y - h}, // left, bottom
{ x + w, WINDOW_HEIGHT - y}, // right, top
{ x, WINDOW_HEIGHT - y}, // left, top
{ x, WINDOW_HEIGHT - y - h}, // left, bottom
{ x + w, WINDOW_HEIGHT - y - h}, // right, bottom
{ x + w, WINDOW_HEIGHT - y}, // right, top
};
unsigned int rect_vao = UINT_MAX;
@ -25,4 +26,5 @@ struct Rect {
void init();
void render();
void dump();
};

Loading…
Cancel
Save