summaryrefslogtreecommitdiffstats
path: root/src/Texture.cpp
diff options
context:
space:
mode:
authorLaG1924 <12997935+LaG1924@users.noreply.github.com>2017-08-29 17:25:30 +0200
committerLaG1924 <12997935+LaG1924@users.noreply.github.com>2018-01-13 03:39:31 +0100
commita0d5862d99186f317a0f7364e8b6cd3cfeccfdf1 (patch)
tree7bf54f8141c513650130e8d3cc6bd8ce3f71b1fe /src/Texture.cpp
parent2017-08-27 (diff)
downloadAltCraft-a0d5862d99186f317a0f7364e8b6cd3cfeccfdf1.tar
AltCraft-a0d5862d99186f317a0f7364e8b6cd3cfeccfdf1.tar.gz
AltCraft-a0d5862d99186f317a0f7364e8b6cd3cfeccfdf1.tar.bz2
AltCraft-a0d5862d99186f317a0f7364e8b6cd3cfeccfdf1.tar.lz
AltCraft-a0d5862d99186f317a0f7364e8b6cd3cfeccfdf1.tar.xz
AltCraft-a0d5862d99186f317a0f7364e8b6cd3cfeccfdf1.tar.zst
AltCraft-a0d5862d99186f317a0f7364e8b6cd3cfeccfdf1.zip
Diffstat (limited to '')
-rw-r--r--src/Texture.cpp76
1 files changed, 64 insertions, 12 deletions
diff --git a/src/Texture.cpp b/src/Texture.cpp
index 5c7ebf7..d39585f 100644
--- a/src/Texture.cpp
+++ b/src/Texture.cpp
@@ -1,5 +1,11 @@
#include "Texture.hpp"
+#include <SDL.h>
+#include <SDL_image.h>
+
+
+bool IsImgInitialized = false;
+
Texture::Texture(std::string filename, GLenum textureWrapping, GLenum textureFiltering) {
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
@@ -12,25 +18,71 @@ Texture::Texture(std::string filename, GLenum textureWrapping, GLenum textureFil
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- //Image load
- sf::Image image;
- if (!image.loadFromFile(filename)) {
- LOG(ERROR) << "Can't open image " << filename;
- throw 201;
+ //Initialize SDL2_image
+ if (!IsImgInitialized) {
+ IsImgInitialized = true;
+ int imgFlags = IMG_INIT_PNG;
+ if (!(IMG_Init(imgFlags) & imgFlags))
+ {
+ throw std::runtime_error("IMG Not initialized: " + std::string(IMG_GetError()));
+ }
}
- if (image.getPixelsPtr() == nullptr) {
- LOG(ERROR) << "Image data is corrupted!";
- throw 202;
- }
- image.flipVertically();
+ //Load image
+ SDL_Surface *image = IMG_Load(filename.c_str());
+ if (!image)
+ throw std::runtime_error("Image not loaded: " + std::string(IMG_GetError()));
+
+ if (image->format->BytesPerPixel != 4)
+ throw std::runtime_error("PNG format is not RGBA");
+
+ //Flip surface
+ SDL_LockSurface(image);
+
+ {
+ int pitch = image->pitch;
+ int height = image->h;
+ void* image_pixels = image->pixels;
+
+ int index;
+ void* temp_row;
+ int height_div_2;
+
+ temp_row = (void *)malloc(pitch);
+ if (!temp_row)
+ throw std::runtime_error("Not enough memory for texture flipping");
+
+ height_div_2 = (int)(height * .5);
+ for (index = 0; index < height_div_2; index++) {
+ memcpy((Uint8 *)temp_row,
+ (Uint8 *)(image_pixels)+
+ pitch * index,
+ pitch);
+
+ memcpy(
+ (Uint8 *)(image_pixels)+
+ pitch * index,
+ (Uint8 *)(image_pixels)+
+ pitch * (height - index - 1),
+ pitch);
+ memcpy(
+ (Uint8 *)(image_pixels)+
+ pitch * (height - index - 1),
+ temp_row,
+ pitch);
+ }
+ free(temp_row);
+ }
//Creating texture
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.getSize().x, image.getSize().y, 0, GL_RGBA, GL_UNSIGNED_BYTE,
- (GLvoid *) image.getPixelsPtr());
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image->w,image->h, 0, GL_RGBA, GL_UNSIGNED_BYTE,
+ (GLvoid *) image->pixels);
+ SDL_UnlockSurface(image);
+
//glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
+ SDL_FreeSurface(image);
}
Texture::~Texture() {