Graphics Animations & Renaming

This commit is contained in:
IlushaShurupov 2023-07-23 22:11:14 +03:00
parent 3a68e70f03
commit 5f186164a0
15 changed files with 718 additions and 318 deletions

View file

@ -0,0 +1,59 @@
#include "Window.hpp"
// -------- OpenGL -------- //
#include <GL/glew.h>
#include "WindowContext.hpp"
// -------- Canvas -------- //
#define NANOVG_GL3_IMPLEMENTATION
#include <nanovg.h>
#include <nanovg_gl.h>
namespace tp {
class Graphics::Canvas::Context {
public:
NVGcontext* vg;
};
}
using namespace tp;
Graphics::Canvas::Canvas() {
mContext = new Context();
}
Graphics::Canvas::~Canvas() {
delete mContext;
}
void Graphics::Canvas::init() {
mContext->vg = nvgCreateGL3(NVG_ANTIALIAS | NVG_STENCIL_STROKES);
}
void Graphics::Canvas::deinit() {
// Cleanup
nvgDeleteGL3(mContext->vg);
}
void Graphics::Canvas::proc() {
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 Graphics::Canvas::draw() {
// End NanoVG rendering
nvgEndFrame(mContext->vg);
}

View file

@ -0,0 +1,57 @@
#include "Window.hpp"
#include "WindowContext.hpp"
// -------- Debug UI -------- //
#include <imgui.h>
#include <imgui_impl_glfw.h>
#include <imgui_impl_opengl3.h>
namespace tp {
class Graphics::GUI::Context {
public:
ImGuiIO* io{};
ImGuiContext* ctx{};
};
}
using namespace tp;
Graphics::GUI::GUI() {
mContext = new Context();
}
Graphics::GUI::~GUI() {
delete mContext;
}
void Graphics::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 Graphics::GUI::deinit() {
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
}
void Graphics::GUI::proc() {
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
// ImGui code goes here
ImGui::Begin("Window");
ImGui::End();
}
void Graphics::GUI::draw() {
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
}

View file

@ -1,262 +0,0 @@
#include "Graphics.hpp"
#include "Allocators.hpp"
// -------- OpenGL -------- //
#include <GL/glew.h>
// -------- Window Context -------- //
#include <GLFW/glfw3.h>
// -------- Debug UI -------- //
#include <imgui.h>
#include <imgui_impl_glfw.h>
#include <imgui_impl_opengl3.h>
// -------- Canvas -------- //
#define NANOVG_GL3_IMPLEMENTATION
#include <nanovg.h>
#include <nanovg_gl.h>
#include <cstdio>
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; }

View file

@ -0,0 +1,150 @@
#include "Window.hpp"
// -------- OpenGL -------- //
#include <GL/glew.h>
#include "WindowContext.hpp"
#include <cstdio>
namespace tp {
class Graphics::GL::Context {
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
};
};
}
using namespace tp;
Graphics::GL::GL() {
mContext = new Context();
}
Graphics::GL::~GL() {
delete mContext;
}
void Graphics::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 Graphics::GL::deinit() {
glDeleteBuffers(1, &mContext->vbo);
glDeleteVertexArrays(1, &mContext->vao);
}
void Graphics::GL::proc() {
glClear(GL_COLOR_BUFFER_BIT);
}
void Graphics::GL::draw() {
glDrawArrays(GL_TRIANGLES, 0, 3);
}
void Graphics::init(Window* window) {
mGl.init();
mGui.init(window);
mCanvas.init();
}
void Graphics::deinit() {
mCanvas.deinit();
mGui.deinit();
mGl.deinit();
}
void Graphics::draw() {
mGl.proc();
mCanvas.proc();
mGui.proc();
mGl.draw();
mCanvas.draw();
mGui.draw();
}
Window::Window(int width, int height, const char* title) {
mContext = new Context();
// 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;
}
mGraphics.init(this);
}
Window::~Window() {
mGraphics.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)) {
mGraphics.draw();
// Swap buffers and poll events
glfwSwapBuffers(mContext->window);
glfwPollEvents();
}
}
auto Window::getContext() -> Context* { return mContext; }

View file

@ -0,0 +1,16 @@
#pragma once
// -------- Window Context -------- //
#include <GLFW/glfw3.h>
namespace tp {
class Window;
class Window::Context {
friend Graphics::GL;
friend Graphics::GUI;
friend Graphics::Canvas;
public:
GLFWwindow* window;
};
}