commit 87b5ff86910cc947fda91b271fa62b5b9276311d Author: Josh Holtrop Date: Mon May 16 14:52:59 2011 -0400 add initial loadTexture() source diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..c5213ff --- /dev/null +++ b/Makefile @@ -0,0 +1,10 @@ +export SCONSFLAGS := -Q + +all: + @scons + +install: + @scons $@ + +clean: + @scons -c diff --git a/SConstruct b/SConstruct new file mode 100644 index 0000000..3d72df4 --- /dev/null +++ b/SConstruct @@ -0,0 +1,6 @@ +# vim:filetype=python + +env = Environment(LIBS = ['GL'], CFLAGS = ['-Wall', '-O2']) +env.ParseConfig('sdl-config --cflags --libs') + +env.Object('loadTexture.o', 'loadTexture.c') diff --git a/loadTexture.c b/loadTexture.c new file mode 100644 index 0000000..acfd8f2 --- /dev/null +++ b/loadTexture.c @@ -0,0 +1,59 @@ + +#include +#include +#include +#include +#include "loadTexture.h" + +GLuint loadTexture(const char *filename) +{ + GLuint texture; + SDL_Surface * temp = IMG_Load(filename); + if (!temp) + { + fprintf(stderr, "Failed to load image '%s'!\n", filename); + return 0; + } + + SDL_PixelFormat fmt; + fmt.palette = NULL; + fmt.BitsPerPixel = 32; + fmt.BytesPerPixel = 4; + fmt.Rmask = 0x000000FF; + fmt.Gmask = 0x0000FF00; + fmt.Bmask = 0x00FF0000; + fmt.Amask = 0xFF000000; + fmt.Rshift = 0; + fmt.Gshift = 8; + fmt.Bshift = 16; + fmt.Ashift = 24; + + SDL_Surface * texsurf = SDL_ConvertSurface(temp, &fmt, SDL_SWSURFACE); + SDL_FreeSurface(temp); + if (!texsurf) + { + fprintf(stderr, "'%s' was not converted properly!\n", filename); + return 0; + } + unsigned int * pixels = + malloc(sizeof(unsigned int) * texsurf->w * texsurf->h); + int y; + unsigned int dstOffset = texsurf->w * (texsurf->h - 1); + unsigned int srcOffset = 0; + for (y = 0; y < texsurf->h; y++) + { + memcpy(pixels + dstOffset, + ((unsigned int *)texsurf->pixels) + srcOffset, + texsurf->w << 2); + dstOffset -= texsurf->w; + srcOffset += texsurf->w; + } + glGenTextures(1, &texture); + glBindTexture(GL_TEXTURE_2D, texture); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texsurf->w, texsurf->h, 0, + GL_RGBA, GL_UNSIGNED_BYTE, pixels); + + SDL_FreeSurface(texsurf); + free(pixels); + return texture; +} diff --git a/loadTexture.h b/loadTexture.h new file mode 100644 index 0000000..8774258 --- /dev/null +++ b/loadTexture.h @@ -0,0 +1,22 @@ + +#ifndef LOADTEXTURE_H +#define LOADTEXTURE_H + +#ifdef GL_INCLUDE_FILE +#include GL_INCLUDE_FILE +#else +#include +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +GLuint loadTexture(const char *filename); + +#ifdef __cplusplus +} +#endif + +#endif +