summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--Makefile5
-rw-r--r--build.zig3
-rw-r--r--src/Textures.cpp105
-rw-r--r--src/Textures.h28
4 files changed, 78 insertions, 63 deletions
diff --git a/Makefile b/Makefile
index 31607f7..7e14cd9 100644
--- a/Makefile
+++ b/Makefile
@@ -4,9 +4,8 @@ SRCDIR := ./src
 BINDIR := ./build
 EXE := $(BINDIR)/blackshades
 
-CFLAGS := -O2 -Wall -g $(shell sdl-config --cflags) -I$(SRCDIR)
-CXXFLAGS := $(CFLAGS)
-LDFLAGS := $(shell sdl-config --libs) -lSDL_image -lGL -lGLU -lopenal -lalut
+CXXFLAGS := -O2 -Wall -g -I$(SRCDIR)
+LDFLAGS := -lSDL -lGL -lGLU -lopenal -lalut
 
 # Don't want ogg?
 #CFLAGS += -DNOOGG
diff --git a/build.zig b/build.zig
index 3803827..7b29a00 100644
--- a/build.zig
+++ b/build.zig
@@ -14,7 +14,6 @@ pub fn build(b: *std.build.Builder) void {
     exe.addCSourceFile("src/GameTick.cpp", &cxxflags);
     exe.addCSourceFile("src/Globals.cpp", &cxxflags);
     exe.addCSourceFile("src/MacInput.cpp", &cxxflags);
-    exe.addCSourceFile("src/Main.cpp", &cxxflags);
     exe.addCSourceFile("src/Models.cpp", &cxxflags);
     exe.addCSourceFile("src/Person.cpp", &cxxflags);
     exe.addCSourceFile("src/Quaternions.cpp", &cxxflags);
@@ -30,10 +29,10 @@ pub fn build(b: *std.build.Builder) void {
     exe.linkSystemLibrary("GL");
     exe.linkSystemLibrary("GLU");
     exe.linkSystemLibrary("SDL");
-    exe.linkSystemLibrary("SDL_image");
     exe.linkSystemLibrary("alut");
     exe.linkSystemLibrary("c++");
     exe.linkSystemLibrary("openal");
+    exe.linkSystemLibrary("vorbisfile");
 
     // Standard target options allows the person running `zig build` to choose
     // what target to build for. Here we do not override the defaults, which
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;
 }
diff --git a/src/Textures.h b/src/Textures.h
index 6deeb1e..4332d26 100644
--- a/src/Textures.h
+++ b/src/Textures.h
@@ -1,14 +1,28 @@
-/*
-(c) 2008 Victor "ErV" Eremin (Voronezh, Russia)
-mailto:ErV2005@rambler.ru, erv@box.vsi.ru
-for non-commercial use only
-*/
+// Texture loader header
+// 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/>.
+
 #ifndef TEXTURES_H
 #define TEXTURES_H
 
 #include <SDL/SDL.h>
 #include <GL/gl.h>
 
-GLuint loadTexture(const char* filename, GLenum minFilter = GL_LINEAR, GLenum magFilter = GL_LINEAR, bool mipmaps = true);
+GLuint loadTexture(const char* filename, GLenum minFilter = GL_LINEAR,
+	GLenum magFilter = GL_LINEAR, bool mipmaps = true);
 
-#endif
+#endif // TEXTURES_H