aboutsummaryrefslogtreecommitdiff
path: root/src/Textures.cpp
diff options
context:
space:
mode:
authorNguyễn Gia Phong <mcsinyx@disroot.org>2021-06-22 11:28:22 +0700
committerNguyễn Gia Phong <mcsinyx@disroot.org>2021-06-22 11:28:22 +0700
commitf8d2b2ce4c4827059089ec7cd0a5414b167e673e (patch)
treefc4886b9e7aa38201180576c337832bbde26270b /src/Textures.cpp
parentf842bf6f2544457ecd488157a74dba8f8a2ea1c4 (diff)
downloadblackshades-f8d2b2ce4c4827059089ec7cd0a5414b167e673e.tar.gz
Replace SDL_image with stb for texture loading
This fixes the legal issues with the non-free Textures source files and allow Zig to compile the game. However, the texts are now loaded incorrectly.
Diffstat (limited to 'src/Textures.cpp')
-rw-r--r--src/Textures.cpp105
1 files changed, 54 insertions, 51 deletions
diff --git a/src/Textures.cpp b/src/Textures.cpp
index dbd3674..3c1bcca 100644
--- a/src/Textures.cpp
+++ b/src/Textures.cpp
@@ -1,21 +1,51 @@
-/*
-(c) 2008 Victor "ErV" Eremin, Voronezh, Russia.
-mailto: ErV2005@rambler.ru, erv@box.vsi.ru
-for non-ncommercial use only
-*/
-#include "Textures.h"
-#include <GL/glu.h>
-#include <SDL/SDL_image.h>
+// Texture loader implementation
+// Copyright (C) 2021 Nguyễn Gia Phong
+//
+// This file is part of Black Shades.
+//
+// Black Shades is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published
+// by the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Black Shades is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Black Shades. If not, see <https://www.gnu.org/licenses/>.
+
#include <stdio.h>
-GLuint loadTexture(const char* filename_, GLenum minFilter, GLenum magFilter, bool mipmaps){
+#define STB_IMAGE_IMPLEMENTATION
+#include <stb/stb_image.h>
+#include <GL/glu.h>
+
+#include "Textures.h"
+
+GLuint loadTexture(const char* filename_, GLenum minFilter,
+ GLenum magFilter, bool mipmaps)
+{
+ // TODO: get rid of the :Data: thing
char filename[1024];
strcpy(filename, filename_+1);
- while(true){
+ while (true) {
char *c = strchr(filename, ':');
if (!c) break;
*c = '/';
}
+
+ int req_format = STBI_rgb_alpha;
+ int width, height, orig_format;
+ unsigned char* data = stbi_load(filename, &width, &height,
+ &orig_format, req_format);
+ if (data == nullptr) {
+ fprintf(stderr, "Loading image failed: %s\n",
+ stbi_failure_reason());
+ return 0;
+ }
+
GLuint tex = 0;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
@@ -23,50 +53,23 @@ GLuint loadTexture(const char* filename_, GLenum minFilter, GLenum magFilter, bo
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
- SDL_Surface *surface = IMG_Load(filename);
- if (!surface){
- fprintf(stderr, "couldn't load file %s!\n", filename);
- return 0;
+ GLint internal_format;
+ GLenum pixel_format;
+ if (req_format == STBI_rgb) {
+ internal_format = 3;
+ pixel_format = GL_RGB;
+ } else { // STBI_rgb_alpha (RGBA)
+ internal_format = 4;
+ pixel_format = GL_RGBA;
}
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
- GLenum format = GL_RGBA;
- int numColors = surface->format->BytesPerPixel;
- if (surface){
- switch (numColors){
- case (4):{
- if (surface->format->Rmask == 0x000000ff)
- format = GL_RGBA;
- else
- format = GL_BGRA;
- break;
- };
- case (3):{
- if (surface->format->Rmask == 0x000000ff)
- format = GL_RGB;
- else
- format = GL_BGR;
- };
- };
-
- //well, our textures are upside down. Fixing it here.
- Uint32 bytesPerRow = surface->format->BytesPerPixel*surface->w;
- char * buf = new char[bytesPerRow];
- char* p = (char*)surface->pixels;
- for (Uint32 i = 0; i < surface->h/2; i++){
- Uint32 offset1 = i*bytesPerRow;
- Uint32 offset2 = (surface->h - i - 1)*bytesPerRow;
- memcpy(buf, &p[offset1], bytesPerRow);
- memcpy(&p[offset1], &p[offset2], bytesPerRow);
- memcpy(&p[offset2], buf, bytesPerRow);
- }
- delete[] buf;
-
- glTexImage2D(GL_TEXTURE_2D, 0, numColors, surface->w, surface->h, 0, format, GL_UNSIGNED_BYTE, surface->pixels);
- if (mipmaps)
- gluBuild2DMipmaps(GL_TEXTURE_2D, format, surface->w, surface->h, format, GL_UNSIGNED_BYTE, surface->pixels);
- }
+ glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0,
+ pixel_format, GL_UNSIGNED_BYTE, data);
+ if (mipmaps)
+ gluBuild2DMipmaps(GL_TEXTURE_2D, internal_format, width, height,
+ pixel_format, GL_UNSIGNED_BYTE, data);
- delete surface;
+ // stbi_image_free(data);
return tex;
}