Skip to content

Commit

Permalink
devil implemented, deleted sdl_image now can read .dds
Browse files Browse the repository at this point in the history
  • Loading branch information
joanmarquesbesses committed Oct 31, 2024
1 parent 62935ee commit 0ae8b87
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 31 deletions.
12 changes: 8 additions & 4 deletions MyGameMaker/MyGameEditor/main.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#include <iostream>

//assimp
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
//devil
#include <IL/il.h>
#include <IL/ilu.h>

//imgui and panels
#include "MyGUI.h"
Expand All @@ -30,6 +29,11 @@
using namespace std;

int main(int argc, char* argv[]) {

//init DevIL // we have to create a texture manager to initialize it and load textures
ilInit();
iluInit();

//start engine
Engine& engine = Engine::Instance();
engine.Awake();
Expand Down
2 changes: 1 addition & 1 deletion MyGameMaker/MyGameEngine/Input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ bool Input::PreUpdate()

case SDL_DROPFILE:
std::string filename = event.drop.file;
if (filename.ends_with(".png")) {
if (filename.ends_with(".png") || filename.ends_with(".dds")) {
Engine::Instance().scene->loadTextureByPath(event.drop.file);
}
else if (filename.ends_with(".fbx")) {
Expand Down
3 changes: 2 additions & 1 deletion MyGameMaker/MyGameEngine/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ void Scene::Start()
go->GetComponent<Mesh>()->setModel(models[i]);
go->GetComponent<Mesh>()->setFilePath("Assets/FBX/BakerHouse.fbx");
go->CreateComponent(ComponentType::Material, go.get());
go->GetComponent<Material>()->m_Texture = std::make_unique<Texture>("Assets/Textures/Baker_house.png");
std::string path = "Assets/Textures/Baker_house.png";
go->GetComponent<Material>()->m_Texture = std::make_unique<Texture>(path);
go->GetComponent<Material>()->m_Shader = std::make_unique<Shader>("Assets/Shaders/Basic.shader");
go->GetComponent<Mesh>()->loadToOpenGL();
addChild(go);
Expand Down
31 changes: 7 additions & 24 deletions MyGameMaker/MyGameEngine/Texture.cpp
Original file line number Diff line number Diff line change
@@ -1,32 +1,15 @@
#include "Texture.h"

#include <SDL2/SDL_image.h>
#include <IL/il.h>
#include <IL/ilu.h>

#include "Renderer.h"

Texture::Texture(const string& path, unsigned int* valor) : m_RendererID(0), m_FilePath(path), m_LocalBuffer(nullptr),
m_Width(0), m_Height(0), m_BPP(0)
{
// Carga la imagen usando SDL_image
SDL_Surface* imageSurface = IMG_Load(path.c_str());
if (!imageSurface) {
printf("Error al cargar imagen: %s\n", IMG_GetError());
return;
}

// Determina el formato de la imagen para OpenGL
GLenum format;
if (imageSurface->format->BytesPerPixel == 4) { // RGBA
format = (imageSurface->format->Rmask == 0x000000ff) ? GL_RGBA : GL_BGRA;
}
else if (imageSurface->format->BytesPerPixel == 3) { // RGB
format = (imageSurface->format->Rmask == 0x000000ff) ? GL_RGB : GL_BGR;
}
else {
printf("Formato de imagen no compatible\n");
SDL_FreeSurface(imageSurface);
return;
}
auto il_img_id = ilGenImage();
ilLoadImage((const wchar_t*)path.c_str());

GLCall(glGenTextures(1, &m_RendererID));
GLCall(glBindTexture(GL_TEXTURE_2D, m_RendererID));
Expand All @@ -40,11 +23,11 @@ m_Width(0), m_Height(0), m_BPP(0)
GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));

glTexImage2D(GL_TEXTURE_2D, 0, format, imageSurface->w, imageSurface->h, 0, format, GL_UNSIGNED_BYTE, imageSurface->pixels);
glTexImage2D(GL_TEXTURE_2D, 0, ilGetInteger(IL_IMAGE_BPP), ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT), 0, ilGetInteger(IL_IMAGE_FORMAT), GL_UNSIGNED_BYTE, ilGetData());
GLCall(glBindTexture(GL_TEXTURE_2D, 0));

// Liberar la superficie de SDL
SDL_FreeSurface(imageSurface);
// Free image
ilDeleteImage(il_img_id);

if (valor != nullptr)
{
Expand Down
2 changes: 1 addition & 1 deletion vcpkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
"features": [ "sdl2-binding", "opengl3-binding" ]
},
"glm",
"sdl2-image"
"devil"
]
}

0 comments on commit 0ae8b87

Please sign in to comment.