51 lines
No EOL
1.1 KiB
C++
51 lines
No EOL
1.1 KiB
C++
#include "graphics/texture.h"
|
|
#include "util.h"
|
|
|
|
#include <SDL_image.h>
|
|
#include <iostream>
|
|
#include <glad/glad.h>
|
|
|
|
bool Texture::loadTexture(const char* imagePath)
|
|
{
|
|
SDL_Surface* buffer = IMG_Load(imagePath);
|
|
if (!buffer)
|
|
{
|
|
std::cout << "Failed to load image file: " << imagePath << std::endl;
|
|
return false;
|
|
}
|
|
//UTIL::flip_surface(buffer);
|
|
|
|
glGenTextures(1, &ID);
|
|
GLenum error = glGetError();
|
|
if(error != GL_NO_ERROR) {
|
|
std::cout << "OpenGL error: " << error << std::endl;
|
|
}
|
|
|
|
glBindTexture(GL_TEXTURE_2D, ID);
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, buffer->w, buffer->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer->pixels);
|
|
glGenerateMipmap(ID);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
|
|
textureWidth = buffer->w;
|
|
textureHeight = buffer->h;
|
|
|
|
SDL_FreeSurface(buffer);
|
|
|
|
return true;
|
|
}
|
|
|
|
void Texture::bind()
|
|
{
|
|
glActiveTexture(GL_TEXTURE0);
|
|
glBindTexture(GL_TEXTURE_2D, ID);
|
|
}
|
|
|
|
Texture::~Texture()
|
|
{
|
|
glDeleteTextures(1, &ID);
|
|
} |