Skip to content

Commit

Permalink
IMAGES WORK!!!!!!
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkauzh committed Dec 4, 2023
1 parent 34acb2b commit 9ead69d
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 20 deletions.
12 changes: 1 addition & 11 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,12 @@ set_property(TARGET vuelto PROPERTY CXX_STANDARD 11)
# Set the include directories for the library
target_include_directories(vuelto PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/vuelto/vendor/stb_image>
$<INSTALL_INTERFACE:include>
)

# Copy header files with folder structure under src
foreach(HEADER ${LIBRARY_HEADERS})
file(RELATIVE_PATH HEADER_RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}/src ${HEADER})
get_filename_component(HEADER_NAME ${HEADER} NAME)
get_filename_component(HEADER_PATH ${HEADER_RELATIVE} DIRECTORY)

file(MAKE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/vuelto/${HEADER_PATH})
file(COPY ${HEADER} DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/include/vuelto/${HEADER_PATH})
endforeach()

# Link GLFW to the library
target_link_libraries(vuelto PUBLIC glfw)
target_sources(vuelto PRIVATE src/vuelto/vendor/stb_image/stb_image.h)

# On macOS, link against the OpenGL framework
if(APPLE)
Expand Down
7 changes: 6 additions & 1 deletion src/vuelto/core/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ Window CreateWindow(int width, int height, const char *title, bool resizable) {

if (!MultipleWindowsEnabled) glfwMakeContextCurrent(glfw_window);

glEnable(GL_BLEND);
glEnable(GL_TEXTURE_2D);

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

Window window;
window.width = width;
window.height = height;
Expand Down Expand Up @@ -65,7 +70,7 @@ void Window::MakeContextCurrent() { glfwMakeContextCurrent(window); }
void Window::Refresh() {
glfwSwapBuffers(window);
if (!Application::MultipleWindowsEnabled) glfwPollEvents();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClear(GL_COLOR_BUFFER_BIT);
}

} // namespace Vuelto
2 changes: 2 additions & 0 deletions src/vuelto/core/app.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <iostream>

#include "../tools/definitions.hpp"

namespace Vuelto {
Expand Down
8 changes: 3 additions & 5 deletions src/vuelto/core/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <vector>

#include "../tools/definitions.hpp"
#define STB_IMAGE_IMPLEMENTATION
#include "../vendor/stb_image/stb_image.h"
#include "app.hpp"

Expand Down Expand Up @@ -46,8 +47,8 @@ Renderer2D::Image Renderer2D::LoadImage(const char* imagePath, float x, float y,
Image image;

// Load the image using stb_image
int imgWidth, imgHeight, numChannels;
unsigned char* imageData = stbi_load(imagePath, &imgWidth, &imgHeight, &numChannels, 0);
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;
Expand Down Expand Up @@ -77,10 +78,8 @@ Renderer2D::Image Renderer2D::LoadImage(const char* imagePath, float x, float y,
}

void Renderer2D::Image::DrawImage() {
// Bind the texture
glBindTexture(GL_TEXTURE_2D, texture);

// Draw a quad with the specified dimensions
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex2f(x, y);
Expand All @@ -92,7 +91,6 @@ void Renderer2D::Image::DrawImage() {
glVertex2f(x, y + height);
glEnd();

// Unbind the texture
glBindTexture(GL_TEXTURE_2D, 0);
}

Expand Down
Binary file added test/coin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 5 additions & 3 deletions test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ int main() {

Vuelto::Renderer2D renderer = Vuelto::Application::CreateRenderer2D(win);

Vuelto::Renderer2D::Image img = renderer.LoadImage("test/image.png", 100.0f, 100.0f, 200.0f, 150.0f);
// Vuelto::Renderer2D::Image img = renderer.LoadImage("test/coin.png", -0.2, -0.2, 0.7, 0.7);

while (!win.WindowShouldClose()) {
renderer.DrawRect(0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1);
img.DrawImage();
renderer.DrawRect(0, 0, 0.7, 0.7, 0.5, 0.1, 0.3);
// img.DrawImage();
win.Refresh();
}

renderer.CleanUp();

Vuelto::Application::Terminate();
return 0;
}

0 comments on commit 9ead69d

Please sign in to comment.