From d3477eedc113a2c126f36f41384b8921d610906a Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Fri, 8 Feb 2008 19:24:12 +0000 Subject: updated filesystem, removed inifile, updated game and tga loader minor cleanups --- src/render/tga.cc | 145 +++++++++++++++++++++--------------------------------- 1 file changed, 57 insertions(+), 88 deletions(-) (limited to 'src/render/tga.cc') diff --git a/src/render/tga.cc b/src/render/tga.cc index b863d9b..5aa3665 100644 --- a/src/render/tga.cc +++ b/src/render/tga.cc @@ -1,4 +1,4 @@ -/* +/* Ronny Andr�Reierstad http://www.morrowland.com http://www.morrowland.com/apron/tut_gl.php @@ -9,57 +9,48 @@ #include "render/tga.h" #include "sys/sys.h" #include "GL/gl.h" -#include +#include -namespace render +namespace render { ///////////////////////////////////////////////////////////////////////////////////////////////// // TGA TEXTURE LOADER ///////////////////////////////////////////////////////////////////////////////////////////////// -void TGA::texture(GLuint textureArray[], const char *filename, int ID) +bool TGA::texture(GLuint textureArray[], const char *filename, int ID) { - if(!filename) - return; - - filesystem::File f; - - f.open(filename); - - if (!f.is_open()) - return; - f.close(); - - image *pBitMap = load(f.path().c_str()); - - if(pBitMap == 0) { - con_warn << "could not load " << f.path().c_str() << std::endl; - return; + if (!filename) + return false; + + image *pBitMap = load(filename); + if (!pBitMap) { + con_warn << "Could not load " << filename << std::endl; + return false; } - + glGenTextures(1, &textureArray[ID]); glBindTexture(GL_TEXTURE_2D, textureArray[ID]); int textureType = GL_RGB; - if(pBitMap->channels == 4) textureType = GL_RGBA; + if (pBitMap->channels == 4) textureType = GL_RGBA; gluBuild2DMipmaps(GL_TEXTURE_2D, pBitMap->channels, pBitMap->size_x, pBitMap->size_y, textureType, GL_UNSIGNED_BYTE, pBitMap->data); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST); - glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR); + glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR); - if (pBitMap) - { - if (pBitMap->data) - { - free(pBitMap->data); + if (pBitMap) { + if (pBitMap->data) { + free(pBitMap->data); } - free(pBitMap); + free(pBitMap); } + + return true; } TGA::image *TGA::load(const char *filename) { - image *pImgData = NULL; - FILE *pFile = NULL; + image *pImgData = NULL; + //FILE *pFile = NULL; GLushort width = 0; GLushort height = 0; GLubyte length = 0; @@ -68,56 +59,46 @@ TGA::image *TGA::load(const char *filename) GLushort channels = 0; GLushort stride = 0; - pFile = fopen(filename, "rb"); - if (!pFile) + filesystem::File *f = filesystem::open(filename); + if (!f) return 0; - + pImgData = (image*)malloc(sizeof(image)); - fread(&length, sizeof(GLubyte), 1, pFile); - - fseek(pFile,1,SEEK_CUR); - - fread(&imgType, sizeof(GLubyte), 1, pFile); + f->read((void *)&length, sizeof(GLubyte)); + f->skip(1); + f->read((void *)&imgType, sizeof(GLubyte)); + f->skip(9); + f->read((void *)&width, sizeof(GLushort)); + f->read((void *)&height, sizeof(GLushort)); + f->read((void *)&bits, sizeof(GLubyte)); + f->skip(length +1); - fseek(pFile, 9, SEEK_CUR); - - fread(&width, sizeof(GLushort), 1, pFile); - fread(&height, sizeof(GLushort), 1, pFile); - fread(&bits, sizeof(GLubyte), 1, pFile); - con_debug << "TGA loading " << width << "x" << height << " " << (int) bits << "bpp" << std::endl; - fseek(pFile, length + 1, SEEK_CUR); - - if(imgType != TGA_RLE) - { + if (imgType != TGA_RLE) { // Check for 24 or 32 Bit - if(bits == 24 || bits == 32) - { - + if (bits == 24 || bits == 32) { + channels = bits / 8; stride = channels * width; pImgData->data = new unsigned char[stride * height]; - for(GLushort y = 0; y < height; y++) - { + for (GLushort y = 0; y < height; y++) { unsigned char *pLine = &(pImgData->data[stride * y]); - fread(pLine, stride, 1, pFile); + f->read((void *)pLine, stride); - for(GLushort i = 0; i < stride; i += channels) - { + for (GLushort i = 0; i < stride; i += channels) { int temp = pLine[i]; pLine[i] = pLine[i + 2]; pLine[i + 2] = temp; } } } - + // Check for 16 Bit - else if(bits == 16) - { + else if (bits == 16) { unsigned short pixels = 0; int r=0, g=0, b=0; @@ -125,9 +106,8 @@ TGA::image *TGA::load(const char *filename) stride = channels * width; pImgData->data = new unsigned char[stride * height]; - for(int i = 0; i < width*height; i++) - { - fread(&pixels, sizeof(GLushort), 1, pFile); + for (int i = 0; i < width*height; i++) { + f->read((void *)&pixels, sizeof(GLushort)); b = (pixels & 0x1f) << 3; g = ((pixels >> 5) & 0x1f) << 3; @@ -137,14 +117,9 @@ TGA::image *TGA::load(const char *filename) pImgData->data[i * 3 + 1] = g; pImgData->data[i * 3 + 2] = b; } - } - - else + } else return NULL; - } - - else - { + } else { GLubyte rleID = 0; int colorsRead = 0; channels = bits / 8; @@ -154,44 +129,38 @@ TGA::image *TGA::load(const char *filename) GLubyte *pColors = new GLubyte [channels]; int i = 0; - while(i < width*height) - { - - fread(&rleID, sizeof(GLubyte), 1, pFile); + while (i < width*height) { + + f->read((void *)&rleID, sizeof(GLubyte)); - if(rleID < 128) { + if (rleID < 128) { rleID++; - while(rleID) - { - fread(pColors, sizeof(GLubyte) * channels, 1, pFile); + while (rleID) { + f->read((void *)pColors, sizeof(GLubyte) * channels); pImgData->data[colorsRead + 0] = pColors[2]; pImgData->data[colorsRead + 1] = pColors[1]; pImgData->data[colorsRead + 2] = pColors[0]; - if(bits == 32) pImgData->data[colorsRead + 3] = pColors[3]; + if (bits == 32) pImgData->data[colorsRead + 3] = pColors[3]; i++; rleID--; colorsRead += channels; } - } - - else - { + } else { rleID -= 127; - fread(pColors, sizeof(GLubyte) * channels, 1, pFile); + f->read((void *)pColors, sizeof(GLubyte) * channels); - while(rleID) - { + while (rleID) { pImgData->data[colorsRead + 0] = pColors[2]; pImgData->data[colorsRead + 1] = pColors[1]; pImgData->data[colorsRead + 2] = pColors[0]; - if(bits == 32) pImgData->data[colorsRead + 3] = pColors[3]; + if (bits == 32) pImgData->data[colorsRead + 3] = pColors[3]; i++; rleID--; @@ -202,7 +171,7 @@ TGA::image *TGA::load(const char *filename) delete[] pColors; } - fclose(pFile); + filesystem::close(f); pImgData->channels = channels; -- cgit v1.2.3