2014-02-16 14:32:28 +00:00
|
|
|
#include "texture.h"
|
|
|
|
#include "graphics.h"
|
|
|
|
|
2021-06-08 23:10:09 +00:00
|
|
|
SDL_Surface *SDL_DisplayFormatAlpha(SDL_Surface *surf) {
|
|
|
|
SDL_Surface *alpha_surf = SDL_ConvertSurfaceFormat(surf, SDL_PIXELFORMAT_ARGB8888, 0);
|
|
|
|
SDL_FreeSurface(surf);
|
|
|
|
return alpha_surf;
|
|
|
|
}
|
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
Texture::Texture(void) {
|
2014-02-16 14:32:28 +00:00
|
|
|
this->textureId = -1;
|
|
|
|
this->enabled = false;
|
|
|
|
}
|
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
Texture::~Texture(void) {
|
|
|
|
if (textureId != -1) {
|
2014-02-16 14:32:28 +00:00
|
|
|
DRUID.freeTexture(textureId);
|
|
|
|
textureId = -1;
|
|
|
|
enabled = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
Texture::Texture(int id) {
|
|
|
|
if (id > -1) {
|
2014-02-16 14:32:28 +00:00
|
|
|
this->textureId = id;
|
|
|
|
this->enabled = false;
|
2015-06-03 12:29:34 +00:00
|
|
|
} else {
|
2014-02-16 14:32:28 +00:00
|
|
|
this->textureId = -1;
|
|
|
|
this->enabled = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
bool Texture::loadImage(char *path) {
|
|
|
|
format = GL_RGB;
|
2014-02-16 14:32:28 +00:00
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
texture = IMG_Load(path);
|
2014-02-16 14:32:28 +00:00
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
// texture = SDL_DisplayFormatAlpha(texture);
|
2014-02-16 14:32:28 +00:00
|
|
|
|
|
|
|
this->textureId = DRUID.loadTexture(texture, -1);
|
2015-06-03 12:29:34 +00:00
|
|
|
// this->textureId = DRUID.loadTexture(path, -1);
|
2014-02-16 14:32:28 +00:00
|
|
|
this->enable();
|
|
|
|
return this->isValidId();
|
|
|
|
}
|
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
bool Texture::loadImage(char *path, float *trans) {
|
|
|
|
format = GL_RGBA;
|
2014-02-16 14:32:28 +00:00
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
texture = IMG_Load(path);
|
2014-02-16 14:32:28 +00:00
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
Uint32 colorKey =
|
|
|
|
SDL_MapRGB(texture->format, (Uint8)(trans[0] * 255),
|
|
|
|
(Uint8)(trans[1] * 255), (Uint8)(trans[2] * 255));
|
|
|
|
// SDL_SetAlpha(texture, 0, SDL_ALPHA_OPAQUE);
|
2014-02-16 14:32:28 +00:00
|
|
|
|
2021-06-08 23:10:09 +00:00
|
|
|
SDL_SetColorKey(texture, SDL_TRUE, colorKey);
|
2014-02-16 14:32:28 +00:00
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
// SDL_Surface* alphaSurface = SDL_DisplayFormatAlpha(texture);
|
2014-02-16 14:32:28 +00:00
|
|
|
texture = SDL_DisplayFormatAlpha(texture);
|
|
|
|
|
|
|
|
this->textureId = DRUID.loadTexture(texture, -1, format);
|
|
|
|
this->enable();
|
|
|
|
return this->isValidId();
|
|
|
|
}
|
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
void Texture::reload(void) {
|
|
|
|
this->textureId = DRUID.loadTexture(texture, -1, format);
|
|
|
|
this->enable();
|
2014-02-16 14:32:28 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
void Texture::disable(void) {
|
2014-02-16 14:32:28 +00:00
|
|
|
this->enabled = false;
|
|
|
|
glDisable(GL_TEXTURE_2D);
|
|
|
|
}
|
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
void Texture::enable(void) {
|
|
|
|
if (textureId > -1) {
|
2014-02-16 14:32:28 +00:00
|
|
|
this->enabled = true;
|
|
|
|
glEnable(GL_TEXTURE_2D);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, textureId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
bool Texture::isEnabled(void) { return this->enabled; }
|
2014-02-16 14:32:28 +00:00
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
int Texture::getId(void) { return this->textureId; }
|
2014-02-16 14:32:28 +00:00
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
void Texture::setId(int id) {
|
|
|
|
if (id > -1) {
|
2014-02-16 14:32:28 +00:00
|
|
|
this->textureId = id;
|
|
|
|
this->enable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-03 12:29:34 +00:00
|
|
|
bool Texture::isValidId(void) { return (this->textureId > -1); }
|