add initial loadTexture() source

This commit is contained in:
Josh Holtrop 2011-05-16 14:52:59 -04:00
commit 87b5ff8691
4 changed files with 97 additions and 0 deletions

10
Makefile Normal file
View File

@ -0,0 +1,10 @@
export SCONSFLAGS := -Q
all:
@scons
install:
@scons $@
clean:
@scons -c

6
SConstruct Normal file
View File

@ -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')

59
loadTexture.c Normal file
View File

@ -0,0 +1,59 @@
#include <stdio.h>
#include <stdlib.h>
#include <SDL.h>
#include <SDL_image.h>
#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;
}

22
loadTexture.h Normal file
View File

@ -0,0 +1,22 @@
#ifndef LOADTEXTURE_H
#define LOADTEXTURE_H
#ifdef GL_INCLUDE_FILE
#include GL_INCLUDE_FILE
#else
#include <GL/gl.h>
#endif
#ifdef __cplusplus
extern "C" {
#endif
GLuint loadTexture(const char *filename);
#ifdef __cplusplus
}
#endif
#endif