Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update master #1

Merged
merged 2 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/vuelto/core/renderer.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "renderer.hpp"

#include <iostream>
#include <vector>

#include "../tools/definitions.hpp"
#define STB_IMAGE_IMPLEMENTATION
Expand All @@ -18,7 +17,8 @@ Vuelto::Renderer2D CreateRenderer2D(Window win) {

} // namespace Application

std::vector<Renderer2D::Image> 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);
Expand All @@ -28,6 +28,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) {
Expand All @@ -46,33 +48,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;
}
Expand All @@ -95,7 +95,7 @@ void Renderer2D::Image::DrawImage() {
}

void Renderer2D::CleanUp() {
for (auto& image : v) {
for (auto& image : image_array) {
glDeleteTextures(1, &image.texture);
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/vuelto/tools/definitions.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#ifdef __APPLE__
// #define GL_SILENCE_DEPRECATION
#define GL_SILENCE_DEPRECATION
#include <OpenGL/gl.h>
#elif __linux__
#define GL_SILENCE_DEPRECATION
#include <GL/gl.h>
#elif _WIN32
#define GL_CLAMP_TO_EDGE 0x812f
#elif _WIN64
#define GL_CLAMP_TO_EDGE 0x812f
#else
#include <GL/gl.h>
#endif
Expand Down
8 changes: 4 additions & 4 deletions test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down