From ff50d2ebce620062b6988247293af3a7e3b7ab90 Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Sat, 6 Aug 2005 16:55:53 +0000 Subject: [PATCH] Started removing DevIL dependency. PNG read support works now. --- Source/Game.h | 11 +-- Source/OpenGL_Windows.cpp | 148 +++++++++++++++++++++++++++++++++++++- makefile | 87 ++++++++++++++++++---- 3 files changed, 226 insertions(+), 20 deletions(-) diff --git a/Source/Game.h b/Source/Game.h index c4f033e..d5978de 100644 --- a/Source/Game.h +++ b/Source/Game.h @@ -10,14 +10,17 @@ //#include #include "TGALoader.h" -#ifdef WIN32 -#include "WinInput.h" -#elif USE_SDL + +#if USE_SDL #include "SDL.h" -#include "SDLInput.h" +#endif + +#if !PLATFORM_MACOSX +#include "WinInput.h" #else #include "Macinput.h" #endif + #include "Terrain.h" #include "Skybox.h" #include "Skeleton.h" diff --git a/Source/OpenGL_Windows.cpp b/Source/OpenGL_Windows.cpp index 6535b95..302ec1d 100644 --- a/Source/OpenGL_Windows.cpp +++ b/Source/OpenGL_Windows.cpp @@ -4,9 +4,25 @@ #endif #include "Game.h" -#include "IL/il.h" -#include "IL/ilu.h" -#include "IL/ilut.h" + +#ifndef USE_DEVIL +# ifdef WIN32 +# define USE_DEVIL +# endif +#endif + +#if USE_DEVIL + #include "IL/il.h" + #include "IL/ilu.h" + #include "IL/ilut.h" +#else + // just use libpng and libjpg directly; it's lighter-weight and easier + // to manage the dependencies on Linux... + #include "png.h" + static bool load_image(const char * fname, TGAImageRec & tex); + static bool load_png(const char * fname, TGAImageRec & tex); + static bool load_jpg(const char * fname, TGAImageRec & tex); +#endif // ADDED GWC #ifdef _MSC_VER @@ -940,6 +956,7 @@ Boolean SetUp (Game & game) glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glAlphaFunc( GL_GREATER, 0.5f); +#if USE_DEVIL if (ilGetInteger(IL_VERSION_NUM) < IL_VERSION || iluGetInteger(ILU_VERSION_NUM) < ILU_VERSION || ilutGetInteger(ILUT_VERSION_NUM) < ILUT_VERSION) @@ -956,6 +973,7 @@ Boolean SetUp (Game & game) ilEnable(IL_ORIGIN_SET); ilOriginFunc(IL_ORIGIN_LOWER_LEFT); +#endif GLint width = kContextWidth; GLint height = kContextHeight; @@ -1154,7 +1172,9 @@ void CleanUp (void) // game.Dispose(); +#if USE_DEVIL ilShutDown(); +#endif #if USE_SDL SDL_Quit(); @@ -2096,6 +2116,7 @@ int main (void) return false; } + #if USE_DEVIL ILstring f = strdup(ConvertFileName(fname)); if (!f) { @@ -2152,12 +2173,17 @@ int main (void) } */ free(f); + #else + res = load_image(fname, tex); + if (!res) STUBBED("load_image() failed!"); + #endif return res; } void ScreenShot(const char * fname) { + #if USE_DEVIL ILstring f = strdup(fname); if (!f) { @@ -2174,6 +2200,122 @@ int main (void) ilDeleteImages(1, &iid); free(f); + #else + STUBBED("Non-DevIL screenshot"); + #endif } +#if !USE_DEVIL +static bool load_image(const char *file_name, TGAImageRec &tex) +{ + char *ptr = strrchr(file_name, '.'); + if (ptr) + { + if (stricmp(ptr+1, "png") == 0) + return load_png(file_name, tex); + else if (stricmp(ptr+1, "jpg") == 0) + return load_jpg(file_name, tex); + } + + STUBBED("Unsupported image type"); + return false; +} + +static bool load_jpg(const char *file_name, TGAImageRec &tex) +{ + // !!! FIXME: write this. + STUBBED("load_jpg"); + return false; +} + +/* stolen from public domain example.c code in libpng distribution. */ +static bool load_png(const char *file_name, TGAImageRec &tex) +{ + bool hasalpha = false; + png_structp png_ptr = NULL; + png_infop info_ptr = NULL; + png_uint_32 width, height; + int bit_depth, color_type, interlace_type; + png_byte **rows = NULL; + bool retval = false; + png_byte **row_pointers = NULL; + FILE *fp = fopen(file_name, "rb"); + + if (fp == NULL) + return(NULL); + + png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); + if (png_ptr == NULL) + goto png_done; + + info_ptr = png_create_info_struct(png_ptr); + if (info_ptr == NULL) + goto png_done; + + if (setjmp(png_jmpbuf(png_ptr))) + goto png_done; + + png_init_io(png_ptr, fp); + png_read_png(png_ptr, info_ptr, + PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_PACKING, + png_voidp_NULL); + png_get_IHDR(png_ptr, info_ptr, &width, &height, + &bit_depth, &color_type, &interlace_type, NULL, NULL); + + if (bit_depth != 8) // transform SHOULD handle this... + goto png_done; + + if (color_type & PNG_COLOR_MASK_PALETTE) // !!! FIXME? + goto png_done; + + if ((color_type & PNG_COLOR_MASK_COLOR) == 0) // !!! FIXME? + goto png_done; + + hasalpha = ((color_type & PNG_COLOR_MASK_ALPHA) != 0); + row_pointers = png_get_rows(png_ptr, info_ptr); + if (!row_pointers) + goto png_done; + + retval = malloc(width * height * 4); + if (!retval) + goto png_done; + + if (!hasalpha) + { + png_byte *dst = tex.data; + for (int i = height-1; i >= 0; i--) + { + png_byte *src = row_pointers[i]; + for (int j = 0; j < width; j++) + { + dst[0] = src[0]; + dst[1] = src[1]; + dst[2] = src[2]; + dst[3] = 0xFF; + src += 3; + dst += 4; + } + } + } + + else + { + png_byte *dst = tex.data; + int pitch = width * 4; + for (int i = height-1; i >= 0; i--, dst += pitch) + memcpy(dst, row_pointers[i], pitch); + } + + tex.sizeX = width; + tex.sizeY = height; + tex.bpp = 32; + retval = true; + +png_done: + png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL); + fclose(fp); + return (retval); +} +#endif + diff --git a/makefile b/makefile index e838ed8..a85cc9b 100644 --- a/makefile +++ b/makefile @@ -1,10 +1,13 @@ macosx := false +use_devil := false BINDIR := bin RUNDIR := run SRCDIR := Source SDLDIR := SDL12 +LIBPNGDIR := libpng-1.2.8 +ZLIBDIR := zlib-1.2.3 EXE := $(RUNDIR)/lugaru-bin @@ -33,11 +36,18 @@ DEFINES := \ -Dstricmp=strcasecmp \ INCLUDES := \ - -I./SDL12/include \ + -I$(SRCDIR) \ + -I$(SDLDIR)/include \ -I./OpenGL/ \ -I./OpenGL/GL \ - -I$(SRCDIR) \ - -I$(SRCDIR)/devil/include \ + +ifeq ($(strip $(use_devil)),true) + DEFINES += -DUSE_DEVIL=1 + INCLUDES += -I$(SRCDIR)/devil/include +else + DEFINES += -DZ_PREFIX=1 + INCLUDES += -I$(ZLIBDIR) -I$(LIBPNGDIR) +endif CFLAGS := -g -c $(OPT) $(INCLUDES) $(DEFINES) -fsigned-char CFLAGS += -w @@ -50,7 +60,11 @@ ifeq ($(strip $(macosx)),true) else CFLAGS += -DPLATFORM_LINUX=1 #CFLAGS += -msse -mmmx - LDFLAGS := ./libSDL-1.2.so.0 -lGL -lGLU ./libfmod.so ./libIL.so.1 ./libILU.so.1 ./libILUT.so.1 + LDFLAGS := ./libSDL-1.2.so.0 -lGL -lGLU ./libfmod.so + + ifeq ($(strip $(use_devil)),true) + LDFLAGS += ./libIL.so.1 ./libILU.so.1 ./libILUT.so.1 + endif endif CXXFLAGS := $(CFLAGS) @@ -86,6 +100,51 @@ SRCS := \ WinInput.cpp \ OpenGL_Windows.cpp \ +SRCS := $(foreach f,$(SRCS),$(SRCDIR)/$(f)) + + +IMGSRCS := \ + png.c \ + pngerror.c \ + pnggccrd.c \ + pngget.c \ + pngmem.c \ + pngpread.c \ + pngread.c \ + pngrio.c \ + pngrtran.c \ + pngrutil.c \ + pngset.c \ + pngtrans.c \ + pngvcrd.c \ + pngwio.c \ + pngwrite.c \ + pngwtran.c \ + pngwutil.c \ + +IMGSRCS := $(foreach f,$(IMGSRCS),$(LIBPNGDIR)/$(f)) + + +ZLIBSRCS = \ + adler32.c \ + compress.c \ + crc32.c \ + deflate.c \ + gzio.c \ + infback.c \ + inffast.c \ + inflate.c \ + inftrees.c \ + trees.c \ + uncompr.c \ + zutil.c \ + +ZLIBSRCS := $(foreach f,$(ZLIBSRCS),$(ZLIBDIR)/$(f)) + + +ifneq ($(strip $(use_devil)),true) + SRCS += $(IMGSRCS) $(ZLIBSRCS) +endif OBJS := $(SRCS:.CC=.o) OBJS := $(OBJS:.cc=.o) @@ -93,26 +152,25 @@ OBJS := $(OBJS:.cpp=.o) OBJS := $(OBJS:.c=.o) OBJS := $(OBJS:.m=.o) OBJS := $(foreach f,$(OBJS),$(BINDIR)/$(f)) -SRCS := $(foreach f,$(SRCS),$(SRCDIR)/$(f)) .PHONY: clean all all : $(EXE) -$(BINDIR)/%.o : $(SRCDIR)/%.cpp +$(BINDIR)/%.o : %.cpp $(CXX) -o $@ $(CXXFLAGS) $< -$(BINDIR)/%.o : $(SRCDIR)/%.CC +$(BINDIR)/%.o : %.CC $(CXX) -o $@ $(CXXFLAGS) $< -$(BINDIR)/%.o : $(SRCDIR)/%.cc +$(BINDIR)/%.o : %.cc $(CXX) -o $@ $(CXXFLAGS) $< -$(BINDIR)/%.o : $(SRCDIR)/%.m +$(BINDIR)/%.o : %.m $(CC) -o $@ $(CFLAGS) $< -$(BINDIR)/%.o : $(SRCDIR)/%.c +$(BINDIR)/%.o : %.c $(CC) -o $@ $(CFLAGS) $< $(EXE) : $(OBJS) $(APPOBJS) @@ -123,9 +181,12 @@ endif $(LD) -o $@ $(APPLDFLAGS) $(LDFLAGS) $(OBJS) $(APPOBJS) clean: - rm -rf $(BINDIR)/*.o - rm -rf $(BINDIR)/logger/*.o - rm -rf $(EXE) + rm -f $(BINDIR)/*.o + rm -f $(BINDIR)/$(SRCDIR)/*.o + rm -f $(BINDIR)/$(SRCDIR)/logger/*.o + rm -f $(BINDIR)/$(LIBPNGDIR)/*.o + rm -f $(BINDIR)/$(ZLIBDIR)/*.o + rm -f $(EXE) # end of makefile ... -- 2.39.5