From be283410b27fda012459e2e3a89f90965ef5da47 Mon Sep 17 00:00:00 2001 From: Dima Marhitych Date: Tue, 5 Dec 2023 09:51:15 +0100 Subject: [PATCH 1/2] Rewrite to C: Part 1 --- src/vuelto/core/renderer.cpp | 15 ++++++++------- src/vuelto/tools/definitions.hpp | 4 +++- test/test.cpp | 8 ++++---- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/vuelto/core/renderer.cpp b/src/vuelto/core/renderer.cpp index beb7354..dbf3e97 100644 --- a/src/vuelto/core/renderer.cpp +++ b/src/vuelto/core/renderer.cpp @@ -18,7 +18,8 @@ Vuelto::Renderer2D CreateRenderer2D(Window win) { } // namespace Application -std::vector v; +Renderer2D::Image image_array[5]; +int size = 0; void Renderer2D::DrawRect(float x, float y, float height, float width, float color1, float color2, float color3) { glBegin(GL_QUADS); @@ -28,6 +29,8 @@ void Renderer2D::DrawRect(float x, float y, float height, float width, float col glVertex2f(x + width, y + height); glVertex2f(x, y + height); glEnd(); + + glColor3f(1.0f, 1.0f, 1.0f); } void Renderer2D::SetBackgroundColor(float color1, float color2, float color3) { @@ -46,33 +49,31 @@ void Renderer2D::DrawLine(int x1, int x2, int y1, int y2, float color1, float co Renderer2D::Image Renderer2D::LoadImage(const char* imagePath, float x, float y, float width, float height) { Image image; - // Load the image using stb_image int imgWidth, imgHeight, channels; unsigned char* imageData = stbi_load(imagePath, &imgWidth, &imgHeight, &channels, 4); // Force 4 channels (RGBA) if (!imageData) { std::cerr << "Failed to load image: " << imagePath << std::endl; - return image; // Return an empty image on failure + return image; } glGenTextures(1, &image.texture); glBindTexture(GL_TEXTURE_2D, image.texture); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, imgWidth, imgHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData); - // Set texture parameters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - stbi_image_free(imageData); // Free the image data after loading + stbi_image_free(imageData); image.x = x; image.y = y; image.width = width; image.height = height; - v.push_back(image); + image_array[size++] = image; return image; } @@ -95,7 +96,7 @@ void Renderer2D::Image::DrawImage() { } void Renderer2D::CleanUp() { - for (auto& image : v) { + for (auto& image : image_array) { glDeleteTextures(1, &image.texture); } } diff --git a/src/vuelto/tools/definitions.hpp b/src/vuelto/tools/definitions.hpp index c569103..cfc896c 100644 --- a/src/vuelto/tools/definitions.hpp +++ b/src/vuelto/tools/definitions.hpp @@ -1,11 +1,13 @@ #ifdef __APPLE__ -// #define GL_SILENCE_DEPRECATION +#define GL_SILENCE_DEPRECATION #include #elif __linux__ #define GL_SILENCE_DEPRECATION #include #elif _WIN32 +#define GL_CLAMP_TO_EDGE 0x812f #elif _WIN64 +#define GL_CLAMP_TO_EDGE 0x812f #else #include #endif diff --git a/test/test.cpp b/test/test.cpp index 1915726..33153c9 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -3,15 +3,15 @@ int main() { Vuelto::Application::Init(); - Vuelto::Window win = Vuelto::Application::CreateWindow(800, 600, "test", true); + Vuelto::Window win = Vuelto::Application::CreateWindow(800, 600, "test", false); Vuelto::Renderer2D renderer = Vuelto::Application::CreateRenderer2D(win); - // Vuelto::Renderer2D::Image img = renderer.LoadImage("test/coin.png", -0.2, -0.2, 0.7, 0.7); + Vuelto::Renderer2D::Image img = renderer.LoadImage("test/image.png", -0.2, -0.2, 0.7, 0.7); while (!win.WindowShouldClose()) { - renderer.DrawRect(0, 0, 0.7, 0.7, 0.5, 0.1, 0.3); - // img.DrawImage(); + renderer.DrawRect(-1, -1, 0.5, 0.5, 0.5, 0.1, 0.8); + img.DrawImage(); win.Refresh(); } From cdf432e9277ddc082774691ab1e37bd012fd5a47 Mon Sep 17 00:00:00 2001 From: Dima Marhitych Date: Tue, 5 Dec 2023 10:03:28 +0100 Subject: [PATCH 2/2] Update renderer.cpp --- src/vuelto/core/renderer.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/vuelto/core/renderer.cpp b/src/vuelto/core/renderer.cpp index dbf3e97..0177bfb 100644 --- a/src/vuelto/core/renderer.cpp +++ b/src/vuelto/core/renderer.cpp @@ -1,7 +1,6 @@ #include "renderer.hpp" #include -#include #include "../tools/definitions.hpp" #define STB_IMAGE_IMPLEMENTATION