diff --git a/Graphics/examples/Example.cpp b/Graphics/examples/Example.cpp index c151f86..9113156 100644 --- a/Graphics/examples/Example.cpp +++ b/Graphics/examples/Example.cpp @@ -1,254 +1,5 @@ -#include "Allocators.hpp" - -// -------- OpenGL -------- // -#include - -// -------- Window Context -------- // -#include - -// -------- Debug UI -------- // -#include -#include -#include - -// -------- Canvas -------- // -#define NANOVG_GL3_IMPLEMENTATION -#include -#include - -#include - -class Showoff { - class GL { - // Showoff data - GLuint vao{}; - GLuint vbo{}; - GLfloat vertices[9] = { - -0.5f, -0.5f, 0.0f, // Left vertex - 0.5f, -0.5f, 0.0f, // Right vertex - 0.0f, 0.5f, 0.0f // Top vertex - }; - - public: - GL() = default; - - void init() { - // Create a Vertex Array Object (VAO) - glGenVertexArrays(1, &vao); - glBindVertexArray(vao); - - // Create a Vertex Buffer Object (VBO) - glGenBuffers(1, &vbo); - glBindBuffer(GL_ARRAY_BUFFER, vbo); - glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); - - // Set up vertex attributes - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (void*) nullptr); - glEnableVertexAttribArray(0); - } - - void deinit() { - glDeleteBuffers(1, &vbo); - glDeleteVertexArrays(1, &vao); - } - - static void beginDraw() { - glClear(GL_COLOR_BUFFER_BIT); - glDrawArrays(GL_TRIANGLES, 0, 3); - } - - static void endDraw() { - } - }; - - class GUI { - ImGuiIO* io{}; - ImGuiContext* ctx{}; - public: - GUI() = default; - - void init(GLFWwindow* window) { - IMGUI_CHECKVERSION(); - ctx = ImGui::CreateContext(); - io = &ImGui::GetIO(); - - // Initialize ImGui with GLFW and OpenGL - ImGui_ImplGlfw_InitForOpenGL(window, true); - ImGui_ImplOpenGL3_Init("#version 330"); - } - - void deinit() { - ImGui_ImplOpenGL3_Shutdown(); - ImGui_ImplGlfw_Shutdown(); - ImGui::DestroyContext(); - } - - void beginDraw() { - ImGui_ImplOpenGL3_NewFrame(); - ImGui_ImplGlfw_NewFrame(); - ImGui::NewFrame(); - - // ImGui code goes here - ImGui::Begin("Window"); - ImGui::End(); - } - - void endDraw() { - ImGui::Render(); - ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); - } - }; - - - class Canvas { - - NVGcontext* vg; - float width; - float height; - - public: - Canvas() = default; - - void init(int aWidth, int aHeight) { - width = aWidth; - height = aHeight; - - vg = nvgCreateGL3(NVG_ANTIALIAS | NVG_STENCIL_STROKES); - } - - void deinit() { - // Cleanup - nvgDeleteGL3(vg); - } - - void beginDraw() { - // Start NanoVG rendering - nvgBeginFrame(vg, width, height, 1.0); - - // Clear the screen - nvgBeginPath(vg); - nvgRect(vg, 0, 0, width, height); - nvgFillColor(vg, nvgRGBf(0.2f, 0.2f, 0.2f)); - nvgFill(vg); - - // Draw a rectangle - nvgBeginPath(vg); - nvgRect(vg, 200, 200, 300, 200); - nvgFillColor(vg, nvgRGBf(0.8f, 0.4f, 0.1f)); - nvgFill(vg); - - // End NanoVG rendering - nvgEndFrame(vg); - } - - void endDraw() { - // End NanoVG rendering - nvgEndFrame(vg); - } - }; - - -public: - Showoff() = default; - - void init(GLFWwindow* window, int aWidth, int aHeight) { - gui.init(window); - gl.init(); - canvas.init(aWidth, aHeight); - } - - void deinit() { - gui.deinit(); - gl.deinit(); - canvas.deinit(); - } - - void draw() { - gl.beginDraw(); - canvas.beginDraw(); - gui.beginDraw(); - gui.endDraw(); - canvas.endDraw(); - gl.endDraw(); - } - -private: - GUI gui{}; - GL gl{}; - Canvas canvas{}; -}; - -class Window { - Window(int width, int height, const char* title) { - // Create a window and OpenGL context - window = glfwCreateWindow(width, height, title, nullptr, nullptr); - if (!window) { - printf("Failed to create GLFW window\n"); - return; - } - - glfwMakeContextCurrent(window); - - // Initialize GLEW - if (glewInit() != GLEW_OK) { - printf("Failed to initialize GLEW\n"); - return; - } - - showoff.init(window, width, height); - } - - ~Window() { - showoff.deinit(); - glfwDestroyWindow(window); - } - -public: - - static Window* createWindow(int width, int height, const char* title) { - static int count = 1; - if (!count) { - printf("Window class is a singleton\n"); - return nullptr; - } - count--; - - // Initialize GLFW - if (!glfwInit()) { - printf("Failed to initialize GLFW\n"); - return nullptr; - } - - // Set the GLFW error callback - glfwSetErrorCallback([] (int error, const char* description) { - printf("GLFW Error: %i %s\n", error, description); - }); - - return new Window(width, height, title); - } - - static void destroyWindow(Window* window) { - delete window; - glfwTerminate(); - } - -public: - void renderLoop() { - while (!glfwWindowShouldClose(window)) { - - showoff.draw(); - - // Swap buffers and poll events - glfwSwapBuffers(window); - glfwPollEvents(); - } - } - -private: - GLFWwindow* window; - Showoff showoff; -}; +#include "Graphics.hpp" int main() { tp::ModuleManifest* deps[] = { &tp::gModuleAllocators, nullptr }; @@ -260,14 +11,14 @@ int main() { { tp::HeapAllocGlobal::startIgnore(); - auto window = Window::createWindow(800, 600, "Window 1"); + auto window = tp::Window::createWindow(800, 600, "Window 1"); tp::HeapAllocGlobal::stopIgnore(); if (window) { window->renderLoop(); } - Window::destroyWindow(window); + tp::Window::destroyWindow(window); } testModule.deinitialize(); diff --git a/Graphics/private/bindings/Graphics.cpp b/Graphics/private/bindings/Graphics.cpp new file mode 100644 index 0000000..43dad69 --- /dev/null +++ b/Graphics/private/bindings/Graphics.cpp @@ -0,0 +1,262 @@ +#include "Graphics.hpp" + +#include "Allocators.hpp" + +// -------- OpenGL -------- // +#include + +// -------- Window Context -------- // +#include + +// -------- Debug UI -------- // +#include +#include +#include + +// -------- Canvas -------- // +#define NANOVG_GL3_IMPLEMENTATION +#include +#include + +#include + +namespace tp { + + class ShowoffGlContext { + public: + // Showoff data + GLuint vao{}; + GLuint vbo{}; + GLfloat vertices[9] = { + -0.0f, -0.0f, 0.0f, // Left vertex + 1.0f, 0.0f, 0.0f, // Right vertex + 0.5f, 1.0f, 0.0f // Top vertex + }; + }; + + class ShowoffGUIContext { + public: + ImGuiIO* io{}; + ImGuiContext* ctx{}; + }; + + class ShowoffCanvasContext { + public: + NVGcontext* vg; + }; + + class WindowContext { + friend Showoff::GL; + friend Showoff::GUI; + friend Showoff::Canvas; + public: + GLFWwindow* window; + }; +} + +using namespace tp; + +Showoff::GL::GL() { + mContext = new ShowoffGlContext(); +} + +Showoff::GL::~GL() { + delete mContext; +} + +void Showoff::GL::init() { + // Create a Vertex Array Object (VAO) + glGenVertexArrays(1, &mContext->vao); + glBindVertexArray(mContext->vao); + + // Create a Vertex Buffer Object (VBO) + glGenBuffers(1, &mContext->vbo); + glBindBuffer(GL_ARRAY_BUFFER, mContext->vbo); + glBufferData(GL_ARRAY_BUFFER, sizeof(mContext->vertices), mContext->vertices, GL_STATIC_DRAW); + + // Set up vertex attributes + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (void*) nullptr); + glEnableVertexAttribArray(0); +} + +void Showoff::GL::deinit() { + glDeleteBuffers(1, &mContext->vbo); + glDeleteVertexArrays(1, &mContext->vao); +} + +void Showoff::GL::beginDraw() { + glClear(GL_COLOR_BUFFER_BIT); +} + +void Showoff::GL::endDraw() { + glDrawArrays(GL_TRIANGLES, 0, 3); +} + +Showoff::GUI::GUI() { + mContext = new ShowoffGUIContext(); +} + +Showoff::GUI::~GUI() { + delete mContext; +} + +void Showoff::GUI::init(Window* window) { + IMGUI_CHECKVERSION(); + mContext->ctx = ImGui::CreateContext(); + mContext->io = &ImGui::GetIO(); + + // Initialize ImGui with GLFW and OpenGL + ImGui_ImplGlfw_InitForOpenGL(window->getContext()->window, true); + ImGui_ImplOpenGL3_Init("#version 330"); +} + +void Showoff::GUI::deinit() { + ImGui_ImplOpenGL3_Shutdown(); + ImGui_ImplGlfw_Shutdown(); + ImGui::DestroyContext(); +} + +void Showoff::GUI::beginDraw() { + ImGui_ImplOpenGL3_NewFrame(); + ImGui_ImplGlfw_NewFrame(); + ImGui::NewFrame(); + + // ImGui code goes here + ImGui::Begin("Window"); + ImGui::End(); +} + +void Showoff::GUI::endDraw() { + ImGui::Render(); + ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); +} + +Showoff::Canvas::Canvas() { + mContext = new ShowoffCanvasContext(); +} + +Showoff::Canvas::~Canvas() { + delete mContext; +} + +void Showoff::Canvas::init() { + mContext->vg = nvgCreateGL3(NVG_ANTIALIAS | NVG_STENCIL_STROKES); +} + +void Showoff::Canvas::deinit() { + // Cleanup + nvgDeleteGL3(mContext->vg); +} + +void Showoff::Canvas::beginDraw() { + auto width = 600; + auto height = 600; + + // Start NanoVG rendering + nvgBeginFrame(mContext->vg, width, height, 1.0); + + // Draw a rectangle + nvgBeginPath(mContext->vg); + nvgRect(mContext->vg, 50, 50, 50, 50); + nvgFillColor(mContext->vg, nvgRGBf(0.8f, 0.4f, 0.1f)); + nvgFill(mContext->vg); + + // End NanoVG rendering + nvgEndFrame(mContext->vg); +} + +void Showoff::Canvas::endDraw() { + // End NanoVG rendering + nvgEndFrame(mContext->vg); +} + + +void Showoff::init(Window* window) { + gl.init(); + gui.init(window); + canvas.init(); +} + +void Showoff::deinit() { + canvas.deinit(); + gui.deinit(); + gl.deinit(); +} + +void Showoff::draw() { + gl.beginDraw(); + canvas.beginDraw(); + gui.beginDraw(); + + gl.endDraw(); + canvas.endDraw(); + gui.endDraw(); +} + +Window::Window(int width, int height, const char* title) { + mContext = new WindowContext(); + + // Create a window and OpenGL context + mContext->window = glfwCreateWindow(width, height, title, nullptr, nullptr); + if (!mContext->window) { + printf("Failed to create GLFW window\n"); + return; + } + + glfwMakeContextCurrent(mContext->window); + + // Initialize GLEW + if (glewInit() != GLEW_OK) { + printf("Failed to initialize GLEW\n"); + return; + } + + showoff.init(this); +} + +Window::~Window() { + showoff.deinit(); + glfwDestroyWindow(mContext->window); + delete mContext; +} + + +Window* Window::createWindow(int width, int height, const char* title) { + static int count = 1; + if (!count) { + printf("Window class is a singleton\n"); + return nullptr; + } + count--; + + // Initialize GLFW + if (!glfwInit()) { + printf("Failed to initialize GLFW\n"); + return nullptr; + } + + // Set the GLFW error callback + glfwSetErrorCallback([] (int error, const char* description) { + printf("GLFW Error: %i %s\n", error, description); + }); + + return new Window(width, height, title); +} + +void Window::destroyWindow(Window* window) { + delete window; + glfwTerminate(); +} + +void Window::renderLoop() { + while (!glfwWindowShouldClose(mContext->window)) { + + showoff.draw(); + + // Swap buffers and poll events + glfwSwapBuffers(mContext->window); + glfwPollEvents(); + } +} + +WindowContext* Window::getContext() { return mContext; } \ No newline at end of file diff --git a/Graphics/private/bindings/Window.cpp b/Graphics/private/bindings/Window.cpp deleted file mode 100644 index e69de29..0000000 diff --git a/Graphics/public/Canvas.hpp b/Graphics/public/Canvas.hpp deleted file mode 100644 index e69de29..0000000 diff --git a/Graphics/public/DebugGui.hpp b/Graphics/public/DebugGui.hpp deleted file mode 100644 index e69de29..0000000 diff --git a/Graphics/public/Graphics.hpp b/Graphics/public/Graphics.hpp new file mode 100644 index 0000000..8a4b294 --- /dev/null +++ b/Graphics/public/Graphics.hpp @@ -0,0 +1,77 @@ +#include "Strings.hpp" + +namespace tp { + + class ShowoffGlContext; + class ShowoffGUIContext; + class ShowoffCanvasContext; + + class Window; + class WindowContext; + + class Showoff { + public: + class GL { + ShowoffGlContext* mContext; + public: + GL(); + ~GL(); + + void init(); + void deinit(); + static void beginDraw(); + static void endDraw(); + }; + + class GUI { + ShowoffGUIContext* mContext; + public: + GUI(); + ~GUI(); + + void init(Window* window); + void deinit(); + void beginDraw(); + void endDraw(); + }; + + class Canvas { + ShowoffCanvasContext* mContext; + public: + Canvas(); + ~Canvas(); + + void init(); + void deinit(); + void beginDraw(); + void endDraw(); + }; + + public: + Showoff() = default; + void init(Window* window); + void deinit(); + void draw(); + + private: + GUI gui; + GL gl; + Canvas canvas; + }; + + class Window { + Window(int width, int height, const char* title); + ~Window(); + + public: + static Window* createWindow(int width, int height, const char* title); + static void destroyWindow(Window* window); + + public: + void renderLoop(); + WindowContext* getContext(); + private: + WindowContext* mContext; + Showoff showoff; + }; +} \ No newline at end of file diff --git a/Graphics/public/Window.hpp b/Graphics/public/Window.hpp deleted file mode 100644 index 2645460..0000000 --- a/Graphics/public/Window.hpp +++ /dev/null @@ -1,8 +0,0 @@ -// -// Created by ilusha on 22.07.23. -// - -#ifndef TYPES_WINDOW_HPP -#define TYPES_WINDOW_HPP - -#endif//TYPES_WINDOW_HPP