Reformat code to have non GL interface

This commit is contained in:
Corentin 2022-10-03 16:06:32 +09:00
commit 9ec93964ba
34 changed files with 644 additions and 362 deletions

View file

@ -1,6 +1,6 @@
#include <iostream>
#include "uui/opengl/uui.hpp"
#include "uui/uui.hpp"
using namespace std;
@ -8,12 +8,12 @@ int main()
{
try
{
uui::GlApplication app;
// uui::GlApplication app2; // exception : application already created
app.create_window(800, 600, "OpenGl!", [](uui::GlWindow& window) {
auto app = uui::create_application(uui::Application::OPENGL);
// auto app2 = uui::create_application(uui::Application::OPENGL); // exception : application already created
app->create_window(800, 600, "OpenGl!", [](uui::Window& window) {
for(int i = 0; i < 100; i += 1) window.create_label(4 * i, 4 * i, "Hello World!", {1.0f, 1.0f, 1.0f, 0.5f}, {});
});
app.run();
app->run();
}
catch(const std::exception& error)
{

View file

@ -1,6 +1,6 @@
#include <iostream>
#include "uui/opengl/uui.hpp"
#include "uui/uui.hpp"
using namespace std;
@ -10,92 +10,92 @@ int main()
{
cout << "Starting example 1" << endl;
{
uui::GlApplication app;
// uui::GlApplication app2; // exception : application already created
app.create_window(800, 600, "OpenGl!", [](uui::GlWindow& window) {
window.create_component(0, 100, 0.5f, 50, [](uui::GlComponent& component) {
auto app = uui::create_application(uui::Application::OPENGL);
// auto app2 = uui::create_application(uui::Application::OPENGL); // exception : application already created
app->create_window(800, 600, "OpenGl!", [](uui::Window& window) {
window.create_component(0, 100, 0.5f, 50, [](uui::Component& component) {
component.set_background_color(0.8f, 0.1f, 0.5f);
});
window.create_component(0.25f, 50, 0.4f, 150, [](uui::GlComponent& component) {
window.create_component(0.25f, 50, 0.4f, 150, [](uui::Component& component) {
component.set_background_color(0.3f, 0.8f, 0.4f, 0.5f);
});
window.create_label(50, 50, "Hello World!", {0.5f, 1.0f, 0.5f, 1.0f}, [](uui::GlLabel& label) {
window.create_label(50, 50, "Hello World!", {0.5f, 1.0f, 0.5f, 1.0f}, [](uui::Label& label) {
label.set_background_color(0.1f, 0.2f, 0.5f, 0.8f);
});
window.create_label(0.5f, 0.5f, "Hello World!", {1.0f, 1.0f, 1.0f, 0.5f}, [](uui::GlLabel& label) {
window.create_label(0.5f, 0.5f, "Hello World!", {1.0f, 1.0f, 1.0f, 0.5f}, [](uui::Label& label) {
label.set_background_color(0.5f, 0.5f, 0.1f);
});
window.create_label(0.75f, 0.75f, "Hello World!", {1.0f, 1.0f, 1.0f, 0.1f}, {});
window.create_flex(200, 0.6f, uui::Direction::HORIZONTAL, [](uui::GlFlex& flex) {
flex.create_component(100, 50, [](uui::GlComponent& component) {
window.create_flex(200, 0.6f, uui::Direction::HORIZONTAL, [](uui::Flex& flex) {
flex.create_component(100, 50, [](uui::Component& component) {
component.set_background_color(0.2f, 0.75f, 0.25f);
});
flex.create_label("test", {0.8f, 0.2f, 0.2f, 1.0f}, {});
flex.create_component(100, 20, [](uui::GlComponent& component) {
flex.create_component(100, 20, [](uui::Component& component) {
component.set_background_color(0.2f, 0.25f, 0.75f);
});
});
window.create_flex(50, 400, uui::Direction::VERTICAL, [](uui::GlFlex& flex) {
flex.create_component(50, 100, [](uui::GlComponent& component) {
window.create_flex(50, 400, uui::Direction::VERTICAL, [](uui::Flex& flex) {
flex.create_component(50, 100, [](uui::Component& component) {
component.set_background_color(0.2f, 0.75f, 0.25f);
});
flex.create_label("test", {0.8f, 0.2f, 0.2f, 1.0f}, {});
flex.create_component(100, 20, [](uui::GlComponent& component) {
flex.create_component(100, 20, [](uui::Component& component) {
component.set_background_color(0.2f, 0.25f, 0.75f);
});
});
});
app.create_window(800, 600, "OpenGl 2", [](uui::GlWindow& window) {
window.create_stack(200, 0.6f, uui::Direction::HORIZONTAL, [](uui::GlStack& stack) {
stack.create_component(100, 50, [](uui::GlComponent& component) {
app->create_window(800, 600, "OpenGl 2", [](uui::Window& window) {
window.create_stack(200, 0.6f, uui::Direction::HORIZONTAL, [](uui::Stack& stack) {
stack.create_component(100, 50, [](uui::Component& component) {
component.set_background_color(0.2f, 0.75f, 0.25f);
});
stack.create_label("test", {0.8f, 0.2f, 0.2f, 1.0f}, {});
stack.create_component(100, 20, [](uui::GlComponent& component) {
stack.create_component(100, 20, [](uui::Component& component) {
component.set_background_color(0.2f, 0.25f, 0.75f);
});
});
window.create_stack(50, 400, uui::Direction::VERTICAL, [](uui::GlStack& stack) {
stack.create_component(100, 50, [](uui::GlComponent& component) {
window.create_stack(50, 400, uui::Direction::VERTICAL, [](uui::Stack& stack) {
stack.create_component(100, 50, [](uui::Component& component) {
component.set_background_color(0.2f, 0.75f, 0.25f);
});
stack.create_label("test", {0.8f, 0.2f, 0.2f, 1.0f}, {});
stack.create_component(100, 20, [](uui::GlComponent& component) {
stack.create_component(100, 20, [](uui::Component& component) {
component.set_background_color(0.2f, 0.25f, 0.75f);
});
});
});
app.run();
app->run();
}
cout << "\nStarting example 2" << endl;
{
uui::GlApplication app;
app.create_window(800, 600, "OpenGl!", [](uui::GlWindow& window) {
window.create_component(0.25f, 0.25f, 0.5f, 0.5f, [](uui::GlComponent& component) {
auto app = uui::create_application(uui::Application::OPENGL);
app->create_window(800, 600, "OpenGl!", [](uui::Window& window) {
window.create_component(0.25f, 0.25f, 0.5f, 0.5f, [](uui::Component& component) {
component.set_background_color(0.8f, 0.1f, 0.5f);
});
window.create_component(0.75f, 0.75f, 0.25f, 0.25f, [](uui::GlComponent& component) {
window.create_component(0.75f, 0.75f, 0.25f, 0.25f, [](uui::Component& component) {
component.set_background_color(0.2f, 0.1f, 0.5f);
});
});
app.create_window(600, 600, "OpenGL 2!", [](uui::GlWindow& window) {
window.create_component(0, 0.5f, 100, 0.5f, [](uui::GlComponent& component) {
app->create_window(600, 600, "OpenGL 2!", [](uui::Window& window) {
window.create_component(0, 0.5f, 100, 0.5f, [](uui::Component& component) {
component.set_background_color(0.2f, 0.5f, 0.3f);
});
window.create_label(50, 50, "Hello World!", {0.5f, 1.0f, 0.5f, 1.0f}, [](uui::GlLabel& label) {
window.create_label(50, 50, "Hello World!", {0.5f, 1.0f, 0.5f, 1.0f}, [](uui::Label& label) {
label.set_background_color(0.1f, 0.2f, 0.5f);
});
});
app.run();
app->run();
}
}
catch(const std::exception& error)

View file

@ -1,14 +1,26 @@
#include "uui/opengl/uui.hpp"
#include "uui/uui.hpp"
int main()
{
uui::GlApplication app;
app.create_window(800, 600, "UUI sample", [](uui::GlWindow& window) {
auto app = uui::create_application(uui::Application::OPENGL);
app->create_window(800, 600, "UUI sample", [](uui::Window& window) {
window.create_component(
0, 0, 200, 100, [](uui::GlComponent& component) { component.set_background_color(1.0f, 0.f, 0.f, 0.5f); });
0, 0, 200, 100, [](uui::Component& component) { component.set_background_color(1.0f, 0.f, 0.f, 0.5f); });
window.create_label(0.5f, 0.5f, "Hello world!", {0.8f, 0.8f, 0.8f, 1.0f}, nullptr);
});
app.run();
app->run();
// app->run();
// app = uui::create_application(uui::Application::OPENGL);
// app->create_window(800, 600, "test", [](uui::Window& window) {
// window.create_flex(10, 10, uui::HORIZONTAL, [](uui::Flex& flex) {
// flex.create_label("Hello!", {0.5f, 0.f, 0.f, 1.0f}, [](uui::Label& label) {
// label.set_background_color(0.2f, 0.2f, 0.6f, 1.0f);
// });
// flex.create_label("World!", {1.0, 1.0, 1.0, 1.0}, {});
// });
// });
// app->run();
return 0;
}

View file

@ -1,3 +1,20 @@
#include "uui/application.hpp"
bool uui::Application::application_created = false;
#include <stdexcept>
#include "uui/opengl/application.hpp"
std::shared_ptr<uui::Application> uui::Application::current_application;
std::shared_ptr<uui::Application> uui::create_application(uui::Application::Implementaion implementation)
{
switch(implementation)
{
case uui::Application::Implementaion::OPENGL:
if(uui::Application::current_application != nullptr)
throw std::runtime_error("Application error: the application already created");
std::shared_ptr<uui::Application> new_application(new uui::GlApplication());
uui::Application::current_application = new_application;
return new_application;
}
}

View file

@ -3,16 +3,32 @@
#include <chrono>
#include <iostream>
uui::GlApplication* uui::GlApplication::application_pointer = nullptr;
#include "uui/opengl/window.hpp"
uui::GlApplication::GlApplication()
{
if(application_pointer != nullptr)
throw std::runtime_error("GlApplication error: the application already created");
if constexpr(debug_mode)
std::cout << "Creating the GlApplication in debug mode" << std::endl;
else
std::cout << "Creating the GlApplication in release mode" << std::endl;
initialized = false;
init();
}
uui::GlApplication::~GlApplication()
{
if constexpr(debug_mode)
std::cout << "Destroying the GlApplication" << std::endl;
if(initialized)
deinit();
}
void uui::GlApplication::init()
{
if constexpr(debug_mode)
std::cout << "Initialize the GlApplication" << std::endl;
// Init GLFW
glfwInit();
// Set all the required options for GLFW
@ -23,22 +39,24 @@ uui::GlApplication::GlApplication()
if constexpr(uui::GlApplication::debug_mode)
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, true);
application_pointer = this;
initialized = true;
}
uui::GlApplication::~GlApplication()
void uui::GlApplication::deinit()
{
if constexpr(debug_mode)
std::cout << "Destroying the GlApplication" << std::endl;
std::cout << "Deinit the GlApplication" << std::endl;
windows.clear();
glfwTerminate();
application_pointer = nullptr;
initialized = false;
}
void uui::GlApplication::run()
{
if constexpr(debug_mode)
std::cout << "Starting the GlApplication" << std::endl;
if(!initialized)
init();
auto start_time = std::chrono::high_resolution_clock::now();
auto end_time = start_time;
@ -75,6 +93,8 @@ void uui::GlApplication::run()
glfwMakeContextCurrent((*(windows.end() - 2))->glfw_window);
glfwSwapInterval(1);
}
if constexpr(debug_mode)
std::cout << "\nGlApplication closing GlWindow : " << window->title << std::endl;
window->deinit();
windows.erase(it);
it -= 1;
@ -85,7 +105,7 @@ void uui::GlApplication::run()
window->render();
else
{
if(window->render_needed && !window->iconified)
if(window->_render_needed && !window->_iconified)
window->render();
}
}
@ -95,12 +115,12 @@ void uui::GlApplication::run()
}
if constexpr(debug_mode)
std::cout << "Ending the GlApplication" << std::endl;
current_application = nullptr;
}
void uui::GlApplication::create_window(
int width, int height, const std::string& title, uui::InitFunction<GlWindow> init)
void uui::GlApplication::create_window(int width, int height, const std::string& title, uui::InitFunction<Window> init)
{
std::shared_ptr<uui::GlWindow> window(new uui::GlWindow(this, width, height, title));
auto window = std::shared_ptr<uui::GlWindow>(new uui::GlWindow(current_application, width, height, title));
window->init();
windows.push_back(window);
glfwSwapInterval(1);
@ -114,7 +134,7 @@ void uui::GlApplication::create_window(
init(*window);
}
std::shared_ptr<uui::GlWindow> uui::GlApplication::get_window(std::size_t index) const
std::shared_ptr<uui::Window> uui::GlApplication::get_window(std::size_t index) const
{
if(index > windows.size())
throw std::runtime_error("Application doesn't have window at given index");

View file

@ -3,17 +3,30 @@
#include <iostream>
#include "uui/opengl/application.hpp"
#include "uui/opengl/container.hpp"
#include "uui/opengl/gl_utils.hpp"
uui::GlComponent::GlComponent(
std::shared_ptr<GlWindow> window, std::size_t window_index, std::shared_ptr<GlContainer> parent,
std::size_t parent_index, const position_t x, const position_t y, const position_t width, const position_t height):
window(window),
window_index(window_index), parent(parent), parent_index(parent_index), position_x(x), position_y(y),
size_width(width), size_height(height), initialized(false)
std::shared_ptr<uui::GlWindow> window, std::size_t window_index, std::shared_ptr<uui::GlContainer> parent,
std::size_t parent_index, const position_t x, const position_t y, const position_t width, const position_t height)
{
if constexpr(uui::GlApplication::debug_mode)
std::cout << "Creating a GlBox" << std::endl;
if constexpr(debug_mode)
std::cout << "Creating a GlComponent " << parent_index << std::endl;
gl_window = window;
this->window = std::static_pointer_cast<uui::Window>(window);
this->window_index = window_index;
this->parent = parent;
this->parent_index = parent_index;
this->parent_index = parent_index;
initialized = false;
position_x = x;
position_y = y;
size_width = width;
size_height = height;
coord_all_relative = true;
auto parse_coords = [&](position_t& value) {
@ -36,7 +49,7 @@ uui::GlComponent::GlComponent(
uui::GlComponent::~GlComponent()
{
if constexpr(uui::GlApplication::debug_mode)
std::cout << "Closing a GlComponent" << std::endl;
std::cout << "Destructor GlComponent " << parent_index << std::endl;
if(initialized)
deinit();
// if(parent != window)
@ -54,7 +67,7 @@ void uui::GlComponent::set_background_color(float r, float g, float b, float a /
void uui::GlComponent::init()
{
if(initialized)
throw std::runtime_error("GlBox error : calling init while already initialized");
throw std::runtime_error("GlComponent error : calling init while already initialized");
glGenVertexArrays(1, &gl_vao);
glGenBuffers(1, &gl_vbo);
@ -88,16 +101,16 @@ void uui::GlComponent::set_vbo_data()
{
float vert_x = position_x.index() == 1
? std::get<1>(position_x)
: static_cast<float>(std::get<0>(position_x)) / static_cast<float>(window->width);
: static_cast<float>(std::get<0>(position_x)) / static_cast<float>(window->width());
float vert_y = position_y.index() == 1
? std::get<1>(position_y)
: static_cast<float>(std::get<0>(position_y)) / static_cast<float>(window->height);
: static_cast<float>(std::get<0>(position_y)) / static_cast<float>(window->height());
float vert_w = size_width.index() == 1
? std::get<1>(size_width)
: static_cast<float>(std::get<0>(size_width)) / static_cast<float>(window->width);
: static_cast<float>(std::get<0>(size_width)) / static_cast<float>(window->width());
float vert_h = size_height.index() == 1
? std::get<1>(size_height)
: static_cast<float>(std::get<0>(size_height)) / static_cast<float>(window->height);
: static_cast<float>(std::get<0>(size_height)) / static_cast<float>(window->height());
float x_min = (2.0f * vert_x) - 1.0f;
float x_max = x_min + (2.0f * vert_w);
float y_min = (-2.0f * vert_y) + 1.0f;
@ -136,12 +149,18 @@ void uui::GlComponent::set_vbo_data()
void uui::GlComponent::deinit()
{
if constexpr(uui::GlApplication::debug_mode)
std::cout << "Deinit GlComponent " << parent_index << std::endl;
if(!initialized)
throw std::runtime_error("GlBox error : calling deinit while not initialized");
throw std::runtime_error("GlComponent error : calling deinit while not initialized");
glDeleteVertexArrays(1, &gl_vao);
glDeleteBuffers(1, &gl_vbo);
glDeleteBuffers(1, &gl_ebo);
window = nullptr;
parent = nullptr;
initialized = false;
}

View file

@ -0,0 +1,37 @@
#include "uui/opengl/container.hpp"
#include "uui/opengl/component.hpp"
std::size_t uui::GlContainer::push_child(std::shared_ptr<uui::Component> child [[maybe_unused]])
{
throw std::runtime_error("GlContainer error : should only push GlComponent child, not Component");
}
std::size_t uui::GlContainer::push_child(std::shared_ptr<uui::GlComponent> child)
{
components.push_back(child);
// components.push_back(std::static_pointer_cast<uui::Component>(child));
return components.size() - 1;
}
std::shared_ptr<uui::Component> uui::GlContainer::get_child(std::size_t index) const
{
return components.at(index);
}
void uui::GlContainer::remove_child(std::size_t index)
{
components.at(index) = nullptr;
};
std::tuple<int, int> uui::GlContainer::get_child_position(const std::size_t child_index [[maybe_unused]]) const
{
return {0, 0};
}
uui::GlContainer::~GlContainer()
{
if constexpr(debug_mode)
std::cout << "Destroying GlContainer" << std::endl;
components.clear();
}

View file

@ -7,10 +7,11 @@
uui::GlFlex::GlFlex(
std::shared_ptr<uui::GlWindow> window, std::size_t window_index, std::shared_ptr<uui::GlContainer> parent,
std::size_t parent_index, const position_t x, const position_t y, const Direction direction):
GlComponent(window, window_index, parent, parent_index, x, y, 0.0f, 0.0f),
GlStack(window, window_index, parent, parent_index, x, y, direction)
{
if constexpr(uui::GlApplication::debug_mode)
std::cout << "Creating a GlStack" << std::endl;
std::cout << "Creating a GlFlex " << parent_index << std::endl;
type = Type::FLEX;
coord_all_relative = false;
@ -20,10 +21,10 @@ std::tuple<int, int> uui::GlFlex::get_child_position(const std::size_t child_ind
{
const int initial_x = position_x.index() == 0
? std::get<0>(position_x)
: static_cast<int>(std::get<1>(position_x) * static_cast<float>(window->width));
: static_cast<int>(std::get<1>(position_x) * static_cast<float>(window->width()));
const int initial_y = position_y.index() == 0
? std::get<0>(position_y)
: static_cast<int>(std::get<1>(position_y) * static_cast<float>(window->height));
: static_cast<int>(std::get<1>(position_y) * static_cast<float>(window->height()));
if(child_index == 0)
return {initial_x, initial_y};
@ -37,11 +38,11 @@ std::tuple<int, int> uui::GlFlex::get_child_position(const std::size_t child_ind
{
auto& component = components[index];
if(component->size_width.index() == 1)
width = static_cast<int>(std::get<1>(component->size_width) * static_cast<float>(window->width));
width = static_cast<int>(std::get<1>(component->size_width) * static_cast<float>(window->width()));
else
width = std::get<0>(component->size_width);
if(component->size_height.index() == 1)
height = static_cast<int>(std::get<1>(component->size_height) * static_cast<float>(window->height));
height = static_cast<int>(std::get<1>(component->size_height) * static_cast<float>(window->height()));
else
height = std::get<0>(component->size_height);
}
@ -64,7 +65,7 @@ std::tuple<int, int> uui::GlFlex::get_child_position(const std::size_t child_ind
std::tie(child_width, child_height) = get_child_size(index);
if(child_height > max_height)
max_height = child_height;
if(x != initial_x && x + child_width > window->width)
if(x != initial_x && x + child_width > window->width())
{
x = initial_x + child_width;
y += max_height;
@ -76,7 +77,7 @@ std::tuple<int, int> uui::GlFlex::get_child_position(const std::size_t child_ind
if(components[child_index] != nullptr)
{
std::tie(child_width, child_height) = get_child_size(child_index);
if(x != initial_x && x + child_width > window->width)
if(x != initial_x && x + child_width > window->width())
{
x = initial_x;
y += max_height;
@ -98,7 +99,7 @@ std::tuple<int, int> uui::GlFlex::get_child_position(const std::size_t child_ind
std::tie(child_width, child_height) = get_child_size(index);
if(child_width > max_width)
max_width = child_width;
if(y != initial_y && y + child_height > window->height)
if(y != initial_y && y + child_height > window->height())
{
y = initial_y + child_height;
x += max_width;
@ -110,7 +111,7 @@ std::tuple<int, int> uui::GlFlex::get_child_position(const std::size_t child_ind
if(components[child_index] != nullptr)
{
std::tie(child_width, child_height) = get_child_size(child_index);
if(y != initial_y && y + child_height > window->height)
if(y != initial_y && y + child_height > window->height())
{
y = initial_y;
x += max_width;

View file

@ -11,7 +11,10 @@
#include "uui/opengl/gl_utils.hpp"
#include "uui/shader.hpp"
uui::GlFontManager::GlFontManager(): initialized(false) {}
uui::GlFontManager::GlFontManager()
{
initialized = false;
}
uui::GlFontManager::~GlFontManager()
{
@ -25,7 +28,7 @@ void uui::GlFontManager::init()
throw std::runtime_error("GlFontManager error : calling init while initialized");
if constexpr(uui::GlApplication::debug_mode)
std::cout << "Init glFontManager" << std::endl;
std::cout << "Init GlFontManager" << std::endl;
if(FT_Init_FreeType(&ft))
throw std::runtime_error("ERROR::FREETYPE: Could not init FreeType Library");
@ -117,6 +120,9 @@ void uui::GlFontManager::deinit()
if(!initialized)
throw std::runtime_error("GlFontManager error : calling deinit while not initialized");
if constexpr(uui::GlApplication::debug_mode)
std::cout << "Deinit GlFontManager" << std::endl;
for(const auto& font: fonts) glDeleteTextures(1, &font.atlas_texture);
glDeleteBuffers(1, &gl_vbo);
glDeleteBuffers(1, &gl_vao);

View file

@ -2,6 +2,7 @@
#include <iostream>
#include "uui/opengl/font_manager.hpp"
#include "uui/opengl/gl_utils.hpp"
uui::GlLabel::GlLabel(
@ -11,12 +12,12 @@ uui::GlLabel::GlLabel(
GlComponent(window, window_index, parent, parent_index, x, y, 0.0f, 0.0f),
text(text), text_color(text_color)
{
if constexpr(uui::GlApplication::debug_mode)
std::cout << "Creating a GlLabel" << std::endl;
if constexpr(debug_mode)
std::cout << "Creating GlLabel " << parent_index << std::endl;
font_index = 0;
std::tie(text_width, text_height) =
window->font_manager.get_text_size(text, window->font_manager.fonts[font_index], 1.0f, 1.0f);
window->font_manager->get_text_size(text, window->font_manager->fonts[font_index], 1.0f, 1.0f);
size_width = text_width;
size_height = text_height;
coord_all_relative = false;
@ -24,15 +25,15 @@ uui::GlLabel::GlLabel(
void uui::GlLabel::render()
{
float text_x = position_x.index() == 1 ? std::get<1>(position_x) * static_cast<float>(window->width)
float text_x = position_x.index() == 1 ? std::get<1>(position_x) * static_cast<float>(window->width())
: static_cast<float>(std::get<0>(position_x));
float text_y = position_y.index() == 1 ? std::get<1>(position_y) * static_cast<float>(window->height)
float text_y = position_y.index() == 1 ? std::get<1>(position_y) * static_cast<float>(window->height())
: static_cast<float>(std::get<0>(position_y));
text_y += static_cast<float>(text_height) - 1;
glBindVertexArray(gl_vao);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, (void*)0);
window->font_manager.render_text(
text.c_str(), window->font_manager.fonts.front(), text_x, text_y, 1.0f, 1.0f, text_color);
window->font_manager->render_text(
text.c_str(), window->font_manager->fonts.front(), text_x, text_y, 1.0f, 1.0f, text_color);
glBindVertexArray(0);
}

View file

@ -3,34 +3,36 @@
#include <iostream>
#include "uui/opengl/gl_utils.hpp"
#include "uui/opengl/label.hpp"
uui::GlStack::GlStack(
std::shared_ptr<uui::GlWindow> window, std::size_t window_index, std::shared_ptr<uui::GlContainer> parent,
std::size_t parent_index, const position_t x, const position_t y, const Direction direction):
GlComponent(window, window_index, parent, parent_index, x, y, 0.0f, 0.0f),
direction(direction)
GlComponent(window, window_index, parent, parent_index, x, y, 0.0f, 0.0f)
{
if constexpr(uui::GlApplication::debug_mode)
std::cout << "Creating a GlStack" << std::endl;
std::cout << "Creating GlStack " << parent_index << std::endl;
this->direction = direction;
type = Type::STACK;
coord_all_relative = false;
}
void uui::GlStack::create_component(
const uui::position_t width, const uui::position_t height, uui::InitFunction<uui::GlComponent> init)
const uui::position_t width, const uui::position_t height, uui::InitFunction<uui::Component> init)
{
if(!initialized)
throw std::runtime_error("GlWindow error : cannot create component while not initialized");
glfwMakeContextCurrent(window->glfw_window);
glfwMakeContextCurrent(gl_window->glfw_window);
std::shared_ptr<uui::GlComponent> component(new uui::GlComponent(
window, window->components.size(), std::dynamic_pointer_cast<uui::GlContainer>(shared_from_this()),
gl_window, gl_window->components.size(), std::dynamic_pointer_cast<uui::GlContainer>(shared_from_this()),
components.size(), 0, 0, width, height));
push_child(component);
component->init();
std::tie(component->position_x, component->position_y) = get_child_position(components.size() - 1);
window->components.push_back(component);
gl_window->components.push_back(component);
if(component != nullptr)
init(*component);
@ -38,19 +40,19 @@ void uui::GlStack::create_component(
void uui::GlStack::create_label(
const std::string& text, const std::tuple<float, float, float, float>& text_color,
uui::InitFunction<uui::GlLabel> init)
uui::InitFunction<uui::Label> init)
{
if(!initialized)
throw std::runtime_error("GlWindow error : cannot create component while not initialized");
glfwMakeContextCurrent(window->glfw_window);
glfwMakeContextCurrent(gl_window->glfw_window);
std::shared_ptr<uui::GlLabel> label(new uui::GlLabel(
window, window->components.size(), std::dynamic_pointer_cast<uui::GlContainer>(shared_from_this()),
gl_window, gl_window->components.size(), std::dynamic_pointer_cast<uui::GlContainer>(shared_from_this()),
components.size(), 0, 0, text, text_color));
push_child(label);
push_child(std::static_pointer_cast<uui::GlComponent>(label));
label->init();
std::tie(label->position_x, label->position_y) = get_child_position(components.size() - 1);
window->components.push_back(label);
gl_window->components.push_back(label);
if(label != nullptr && init != nullptr)
init(*label);
@ -59,9 +61,9 @@ void uui::GlStack::create_label(
std::tuple<int, int> uui::GlStack::get_child_position(const std::size_t child_index) const
{
int x = position_x.index() == 0 ? std::get<0>(position_x)
: static_cast<int>(std::get<1>(position_x) * static_cast<float>(window->width));
: static_cast<int>(std::get<1>(position_x) * static_cast<float>(window->width()));
int y = position_y.index() == 0 ? std::get<0>(position_y)
: static_cast<int>(std::get<1>(position_y) * static_cast<float>(window->height));
: static_cast<int>(std::get<1>(position_y) * static_cast<float>(window->height()));
if(child_index == 0)
return {x, y};
@ -75,7 +77,7 @@ std::tuple<int, int> uui::GlStack::get_child_position(const std::size_t child_in
{
auto& component = components[index];
if(component->size_width.index() == 1)
x += static_cast<int>(std::get<1>(component->size_width) * static_cast<float>(window->width));
x += static_cast<int>(std::get<1>(component->size_width) * static_cast<float>(window->width()));
else
x += std::get<0>(component->size_width);
}
@ -88,7 +90,7 @@ std::tuple<int, int> uui::GlStack::get_child_position(const std::size_t child_in
{
auto& component = components[index];
if(component->size_height.index() == 1)
y += static_cast<int>(std::get<1>(component->size_height) * static_cast<float>(window->height));
y += static_cast<int>(std::get<1>(component->size_height) * static_cast<float>(window->height()));
else
y += std::get<0>(component->size_height);
}

View file

@ -6,11 +6,13 @@
#include "uui/opengl/flex.hpp"
#include "uui/opengl/gl_utils.hpp"
#include "uui/opengl/label.hpp"
#include "uui/opengl/flex.hpp"
#include "uui/opengl/stack.hpp"
#include "uui/shader.hpp"
static void APIENTRY glDebugOutput(
GLenum source, GLenum type, unsigned int id, GLenum severity, GLsizei length, const char* message,
const void* userParam)
GLenum source, GLenum type, unsigned int id, GLenum severity, GLsizei length [[maybe_unused]], const char* message,
const void* userParam [[maybe_unused]])
{
// ignore non-significant error/warning codes
if(id == 131169 || id == 131185 || id == 131218 || id == 131204)
@ -59,59 +61,68 @@ void uui::GlWindow::glfw_resize_callback(GLFWwindow* window, int width, int heig
glfwMakeContextCurrent(window);
auto gl_window = reinterpret_cast<uui::GlWindow*>(glfwGetWindowUserPointer(window));
glViewport(0, 0, width, height);
gl_window->width = width;
gl_window->height = height;
gl_window->render_needed = true;
gl_window->resize_needed = true;
gl_window->_width = width;
gl_window->_height = height;
gl_window->_render_needed = true;
gl_window->_resize_needed = true;
glm::mat4 projection = glm::ortho(0.0f, static_cast<float>(width), static_cast<float>(height), 0.0f);
glfwMakeContextCurrent(window);
glUseProgram(gl_window->font_manager.gl_program);
glUseProgram(gl_window->gl_font_manager->gl_program);
glUniformMatrix4fv(
glGetUniformLocation(gl_window->font_manager.gl_program, "projection"), 1, GL_FALSE,
glGetUniformLocation(gl_window->gl_font_manager->gl_program, "projection"), 1, GL_FALSE,
glm::value_ptr(projection));
}
void uui::GlWindow::glfw_iconify_callback(GLFWwindow* window, int iconified)
{
auto gl_window = reinterpret_cast<uui::GlWindow*>(glfwGetWindowUserPointer(window));
gl_window->iconified = iconified;
gl_window->_iconified = iconified;
}
void uui::GlWindow::glfw_key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
void uui::GlWindow::glfw_key_callback(GLFWwindow* window, int key, int scancode [[maybe_unused]], int action, int mods [[maybe_unused]])
{
if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
uui::GlWindow::GlWindow(uui::GlApplication* app, int width, int height, const std::string& title):
app(app), initialized(false), width(width), height(height), render_needed(true), resize_needed(false),
iconified(false)
uui::GlWindow::GlWindow(std::shared_ptr<uui::Application> app, int width, int height, const std::string& title)
{
if constexpr(uui::GlApplication::debug_mode)
std::cout << "Creating a GlWindow" << std::endl;
_app = app;
_initialized = false;
_width = width;
_height = height;
_render_needed = true;
_resize_needed = false;
_iconified = false;
if constexpr(debug_mode)
std::cout << "Creating GlWindow : " << title << std::endl;
this->title = title;
type = Type::WINDOW;
}
uui::GlWindow::~GlWindow()
{
if constexpr(uui::GlApplication::debug_mode)
std::cout << "Closing a GlWindow" << std::endl;
if(initialized)
if constexpr(debug_mode)
std::cout << "Destructor GlWindow : " << title << std::endl;
if(_initialized)
{
if constexpr(uui::GlApplication::debug_mode)
std::cout << "Destructor-deinit a GlWindow" << std::endl;
if constexpr(debug_mode)
std::cout << "Destructor-deinit GlWindow : " << title << std::endl;
deinit();
}
}
void uui::GlWindow::init()
{
if(initialized)
if(_initialized)
throw std::runtime_error("GlWindow error : calling init while already initialized");
glfw_window = glfwCreateWindow(width, height, title.c_str(), NULL, NULL);
gl_font_manager = std::make_shared<uui::GlFontManager>();
font_manager = std::static_pointer_cast<uui::FontManager>(gl_font_manager);
glfw_window = glfwCreateWindow(_width, _height, title.c_str(), NULL, NULL);
if(glfw_window == NULL)
throw std::runtime_error("GLWindow : failed to create GLFW window");
glfwMakeContextCurrent(glfw_window);
@ -207,32 +218,35 @@ void uui::GlWindow::init()
glDeleteShader(vertex_shader);
glDeleteShader(fragment_shader);
font_manager.init();
glUseProgram(font_manager.gl_program);
glm::mat4 projection = glm::ortho(0.0f, static_cast<float>(width), static_cast<float>(height), 0.0f);
gl_font_manager->init();
glUseProgram(gl_font_manager->gl_program);
glm::mat4 projection = glm::ortho(0.0f, static_cast<float>(_width), static_cast<float>(_height), 0.0f);
glUniformMatrix4fv(
glGetUniformLocation(font_manager.gl_program, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
glGetUniformLocation(gl_font_manager->gl_program, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
glUseProgram(gl_program);
initialized = true;
_initialized = true;
}
void uui::GlWindow::deinit()
{
if(!initialized)
if(!_initialized)
throw std::runtime_error("GlWindow error : calling deinit while not initialized");
if constexpr(uui::GlApplication::debug_mode)
std::cout << "Deinit a GlWindow" << std::endl;
std::cout << "Deinit GlWindow : " << title << std::endl;
glfwMakeContextCurrent(glfw_window);
glDeleteProgram(gl_program);
for(auto& component: components) component->deinit();
for(auto& gl_component: components) gl_component->deinit();
components.clear();
font_manager.deinit();
gl_font_manager->deinit();
glfwDestroyWindow(glfw_window);
initialized = false;
_app = nullptr;
font_manager = nullptr;
gl_font_manager = nullptr;
_initialized = false;
}
void uui::GlWindow::render()
@ -242,22 +256,22 @@ void uui::GlWindow::render()
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(gl_program);
if(resize_needed)
if(_resize_needed)
{
for(auto& component: components) component->on_resize();
resize_needed = false;
_resize_needed = false;
}
for(auto& component: components) component->render();
glfwSwapBuffers(glfw_window);
render_needed = false;
_render_needed = false;
}
void uui::GlWindow::create_component(
const uui::position_t x, const uui::position_t y, const uui::position_t width, const uui::position_t height,
uui::InitFunction<GlComponent> init)
uui::InitFunction<Component> init)
{
if(!initialized)
if(!_initialized)
throw std::runtime_error("GlWindow error : cannot create component while not initialized");
glfwMakeContextCurrent(glfw_window);
@ -272,15 +286,15 @@ void uui::GlWindow::create_component(
void uui::GlWindow::create_label(
const uui::position_t x, const uui::position_t y, const std::string& text,
const std::tuple<float, float, float, float>& text_color, uui::InitFunction<GlLabel> init)
const std::tuple<float, float, float, float>& text_color, uui::InitFunction<Label> init)
{
if(!initialized)
if(!_initialized)
throw std::runtime_error("GlWindow error : cannot create label while not initialized");
glfwMakeContextCurrent(glfw_window);
std::shared_ptr<uui::GlLabel> label(
new uui::GlLabel(shared_from_this(), components.size(), x, y, text, text_color));
push_child(label);
push_child(std::static_pointer_cast<uui::GlComponent>(label));
label->init();
if(label != nullptr && init != nullptr)
@ -288,14 +302,14 @@ void uui::GlWindow::create_label(
}
void uui::GlWindow::create_flex(
const uui::position_t x, const uui::position_t y, const uui::Direction direction, uui::InitFunction<GlFlex> init)
const uui::position_t x, const uui::position_t y, const uui::Direction direction, uui::InitFunction<Flex> init)
{
if(!initialized)
if(!_initialized)
throw std::runtime_error("GlWindow error : cannot create flex while not initialized");
glfwMakeContextCurrent(glfw_window);
std::shared_ptr<uui::GlFlex> flex(new uui::GlFlex(shared_from_this(), components.size(), x, y, direction));
push_child(flex);
push_child(std::static_pointer_cast<uui::GlComponent>(flex));
flex->init();
if(flex != nullptr && init != nullptr)
@ -303,16 +317,17 @@ void uui::GlWindow::create_flex(
}
void uui::GlWindow::create_stack(
const uui::position_t x, const uui::position_t y, const uui::Direction direction, uui::InitFunction<GlStack> init)
const uui::position_t x, const uui::position_t y, const uui::Direction direction, uui::InitFunction<Stack> init)
{
if(!initialized)
if(!_initialized)
throw std::runtime_error("GlWindow error : cannot create stack while not initialized");
glfwMakeContextCurrent(glfw_window);
std::shared_ptr<uui::GlStack> stack(new uui::GlStack(shared_from_this(), components.size(), x, y, direction));
push_child(stack);
push_child(std::static_pointer_cast<uui::GlComponent>(stack));
stack->init();
if(stack != nullptr && init != nullptr)
init(*stack);
}