Implementado ventana de log ImGUI

parent f1733fdc
build/ build/
pr01 pr01
imgui.ini
cmake_minimum_required(VERSION 3.27) cmake_minimum_required(VERSION 3.25)
project(pr01) project(pr01)
set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD 17)
......
...@@ -27,5 +27,27 @@ namespace PAG { ...@@ -27,5 +27,27 @@ namespace PAG {
ImGui::DestroyContext(); ImGui::DestroyContext();
} }
void GUI::addVentana(std::shared_ptr<Ventana> ventana) {
ventanas.push_back(ventana);
}
void GUI::render() {
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
for (auto &ventana : ventanas) {
ventana->render();
}
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
}
void GUI::addMouseButtonEvent(int button, bool click) {
ImGui::GetIO().AddMouseButtonEvent(button, click);
}
GUI::GUI() {}; GUI::GUI() {};
} }
...@@ -3,6 +3,11 @@ ...@@ -3,6 +3,11 @@
#include "glad/glad.h" #include "glad/glad.h"
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <memory>
#include <vector>
#include "ventana.h"
namespace PAG { namespace PAG {
class GUI { class GUI {
public: public:
...@@ -14,7 +19,14 @@ namespace PAG { ...@@ -14,7 +19,14 @@ namespace PAG {
void init(GLFWwindow *); void init(GLFWwindow *);
void shutdown(); void shutdown();
void addVentana(std::shared_ptr<Ventana>);
void render();
void addMouseButtonEvent(int button, bool click);
private: private:
GUI(); GUI();
std::vector<std::shared_ptr<Ventana>> ventanas;
}; };
}; };
#include "log.h"
namespace PAG {
Logger log = Logger();
}
#pragma once
#include <sstream>
#include <iostream>
namespace PAG {
// Clase que imprime a stderr y a un std::string
class Logger : public std::ostream {
public:
Logger() : buffer(), std::ostream(&buffer) {};
Logger(const Logger &) = delete;
void operator=(const Logger &) = delete;
std::string str() {
return buffer.full_log();
}
private:
class Buffer : public std::stringbuf {
std::stringstream inner_full_log;
public:
int sync() override {
std::cerr << str();
std::cerr.flush();
inner_full_log << str();
str("");
return 0;
}
std::string full_log() {
return inner_full_log.str();
}
};
Buffer buffer;
};
extern Logger log;
};
...@@ -7,17 +7,19 @@ ...@@ -7,17 +7,19 @@
#include "renderer.h" #include "renderer.h"
#include "gui.h" #include "gui.h"
#include "log.h"
#include "ventana_log.h"
// Helper que muestra un mensaje y cierra el programa // Helper que muestra un mensaje y cierra el programa
#define panic(message) { \ #define panic(message) { \
std::cout << __FILE__ << ":" << __LINE__ << " : panic!" << std::endl;\ PAG::log << __FILE__ << ":" << __LINE__ << " : panic!" << std::endl;\
std::cout << __FILE__ << ":" << __LINE__ << " : " << message << std::endl;\ PAG::log << __FILE__ << ":" << __LINE__ << " : " << message << std::endl;\
exit(-1);\ exit(-1);\
} }
// Callback cuando ocurre un error en GLFW // Callback cuando ocurre un error en GLFW
void error_callback(int errno, const char *desc) { void error_callback(int errno, const char *desc) {
std::cout << "GLFW error " << errno << ": " << desc << std::endl; PAG::log << "GLFW error " << errno << ": " << desc << std::endl;
} }
void window_refresh_callback(GLFWwindow *window) { void window_refresh_callback(GLFWwindow *window) {
...@@ -25,27 +27,29 @@ void window_refresh_callback(GLFWwindow *window) { ...@@ -25,27 +27,29 @@ void window_refresh_callback(GLFWwindow *window) {
glfwSwapBuffers(window); glfwSwapBuffers(window);
std::cout << "Refresh callback called" << std::endl; PAG::log << "Refresh callback called" << std::endl;
} }
void framebuffer_size_callback(GLFWwindow *window, int width, int height) { void framebuffer_size_callback(GLFWwindow *window, int width, int height) {
PAG::Renderer::getInstance().setViewport(width, height); PAG::Renderer::getInstance().setViewport(width, height);
std::cout << "Resize callback called. New size: " << width << ", " << height << std::endl; PAG::log << "Resize callback called. New size: " << width << ", " << height << std::endl;
} }
void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods) { void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods) {
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) { if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
glfwSetWindowShouldClose(window, GLFW_TRUE); glfwSetWindowShouldClose(window, GLFW_TRUE);
} }
std::cout << "Key callback called" << std::endl; PAG::log << "Key callback called" << std::endl;
} }
void mouse_button_callback(GLFWwindow *window, int button, int action, int mods) { void mouse_button_callback(GLFWwindow *window, int button, int action, int mods) {
if (action == GLFW_PRESS) { if (action == GLFW_PRESS) {
std::cout << "Pulsado el botón: mouse " << button << std::endl; PAG::log << "Pulsado el botón: mouse " << button << std::endl;
PAG::GUI::getInstance().addMouseButtonEvent(button, true);
} else if (action == GLFW_RELEASE) { } else if (action == GLFW_RELEASE) {
std::cout << "Soltado el botón: mouse " << button << std::endl; PAG::log << "Soltado el botón: mouse " << button << std::endl;
PAG::GUI::getInstance().addMouseButtonEvent(button, false);
} }
} }
...@@ -55,7 +59,7 @@ const double gradient_colors[][3] = {{0.6, 0.6, 0.6}, {1, 0.65, 0}}; ...@@ -55,7 +59,7 @@ const double gradient_colors[][3] = {{0.6, 0.6, 0.6}, {1, 0.65, 0}};
void scroll_callback(GLFWwindow *window, double xoffset, double yoffset) { void scroll_callback(GLFWwindow *window, double xoffset, double yoffset) {
static double position_in_gradient = 0; static double position_in_gradient = 0;
std::cout << "Movida la rueda del raton " PAG::log << "Movida la rueda del raton "
<< xoffset << " unidades en horizontal y " << xoffset << " unidades en horizontal y "
<< yoffset << " unidades en vertical" << std::endl; << yoffset << " unidades en vertical" << std::endl;
...@@ -79,7 +83,7 @@ void scroll_callback(GLFWwindow *window, double xoffset, double yoffset) { ...@@ -79,7 +83,7 @@ void scroll_callback(GLFWwindow *window, double xoffset, double yoffset) {
} }
int main() { int main() {
std::cout << "Starting Application PAG - Prueba 01" << std::endl; PAG::log << "Starting Application PAG - Prueba 01" << std::endl;
glfwSetErrorCallback((GLFWerrorfun) error_callback); glfwSetErrorCallback((GLFWerrorfun) error_callback);
...@@ -109,13 +113,14 @@ int main() { ...@@ -109,13 +113,14 @@ int main() {
} }
PAG::GUI::getInstance().init(window); PAG::GUI::getInstance().init(window);
PAG::GUI::getInstance().addVentana(std::make_shared<PAG::VentanaLog>(PAG::VentanaLog()));
auto &renderer = PAG::Renderer::getInstance(); auto &renderer = PAG::Renderer::getInstance();
std::cout << renderer.getRendererName() << std::endl; PAG::log << renderer.getRendererName() << std::endl;
std::cout << renderer.getVendor() << std::endl; PAG::log << renderer.getVendor() << std::endl;
std::cout << renderer.getVersion() << std::endl; PAG::log << renderer.getVersion() << std::endl;
std::cout << renderer.getShadingLanguageVersion() << std::endl; PAG::log << renderer.getShadingLanguageVersion() << std::endl;
glfwSetWindowRefreshCallback(window, window_refresh_callback); glfwSetWindowRefreshCallback(window, window_refresh_callback);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
...@@ -131,12 +136,14 @@ int main() { ...@@ -131,12 +136,14 @@ int main() {
while (!glfwWindowShouldClose(window)) { while (!glfwWindowShouldClose(window)) {
renderer.clear(); renderer.clear();
PAG::GUI::getInstance().render();
glfwSwapBuffers(window); glfwSwapBuffers(window);
glfwPollEvents(); glfwPollEvents();
} }
std::cout << "Finishing application pag prueba" << std::endl; PAG::log << "Finishing application pag prueba" << std::endl;
PAG::GUI::getInstance().shutdown(); PAG::GUI::getInstance().shutdown();
glfwDestroyWindow(window); glfwDestroyWindow(window);
......
#pragma once
namespace PAG {
class Ventana {
public:
virtual void render() = 0;
virtual ~Ventana() = default;
};
}
#include "ventana_log.h"
#include <imgui.h>
#include "log.h"
namespace PAG {
void VentanaLog::render() {
ImGui::SetNextWindowPos(ImVec2(10, 10), ImGuiCond_FirstUseEver);
ImGui::SetNextWindowSize(ImVec2(600, 300), ImGuiCond_FirstUseEver);
if (ImGui::Begin("Mensajes")) {
ImGui::SetWindowFontScale(1.0);
ImGui::Text(PAG::log.str().c_str());
}
ImGui::End();
}
}
#pragma once
#include "ventana.h"
namespace PAG {
class VentanaLog : public Ventana {
public:
void render() override;
};
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment