2007-01-15 12:09:25 -08:00
|
|
|
/*
|
|
|
|
This file is part of Warzone 2100.
|
|
|
|
Copyright (C) 1999-2004 Eidos Interactive
|
2009-02-10 10:01:48 -08:00
|
|
|
Copyright (C) 2005-2009 Warzone Resurrection Project
|
2007-01-15 12:09:25 -08:00
|
|
|
|
|
|
|
Warzone 2100 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 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Warzone 2100 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 Warzone 2100; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2008-01-01 13:33:47 -08:00
|
|
|
#include "lib/framework/frame.h"
|
2008-03-16 05:39:08 -07:00
|
|
|
|
2008-01-01 13:33:47 -08:00
|
|
|
#include "lib/framework/frameresource.h"
|
2008-03-16 05:39:08 -07:00
|
|
|
#include "lib/framework/file.h"
|
2008-01-01 13:33:47 -08:00
|
|
|
|
2007-06-28 10:47:08 -07:00
|
|
|
#include "bitimage.h"
|
2008-03-16 05:39:08 -07:00
|
|
|
#include "tex.h"
|
|
|
|
|
2006-09-23 11:38:12 -07:00
|
|
|
|
2008-01-01 13:33:47 -08:00
|
|
|
static unsigned short LoadTextureFile(const char *FileName)
|
2007-05-25 10:21:13 -07:00
|
|
|
{
|
2008-02-07 09:02:25 -08:00
|
|
|
iV_Image *pSprite;
|
2007-05-27 09:41:21 -07:00
|
|
|
unsigned int i;
|
2007-05-25 10:21:13 -07:00
|
|
|
|
2007-05-27 09:41:21 -07:00
|
|
|
ASSERT(resPresent("IMGPAGE", FileName), "Texture file \"%s\" not preloaded.", FileName);
|
|
|
|
|
2008-02-07 09:02:25 -08:00
|
|
|
pSprite = (iV_Image*)resGetData("IMGPAGE", FileName);
|
2007-05-27 09:41:21 -07:00
|
|
|
debug(LOG_TEXTURE, "Load texture from resource cache: %s (%d, %d)",
|
|
|
|
FileName, pSprite->width, pSprite->height);
|
2007-05-25 10:21:13 -07:00
|
|
|
|
2008-01-01 13:33:47 -08:00
|
|
|
/* Have we already uploaded this one? */
|
2007-05-27 09:41:21 -07:00
|
|
|
for (i = 0; i < _TEX_INDEX; ++i)
|
|
|
|
{
|
|
|
|
if (strcasecmp(FileName, _TEX_PAGE[i].name) == 0)
|
|
|
|
{
|
2008-01-01 13:33:47 -08:00
|
|
|
debug(LOG_TEXTURE, "LoadTextureFile: already uploaded");
|
2007-05-27 09:41:21 -07:00
|
|
|
return _TEX_PAGE[i].id;
|
2007-05-25 10:21:13 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-01 13:33:47 -08:00
|
|
|
debug(LOG_TEXTURE, "LoadTextureFile: had to upload texture!");
|
2007-08-01 08:06:23 -07:00
|
|
|
return pie_AddTexPage(pSprite, FileName, 0);
|
2007-05-25 10:21:13 -07:00
|
|
|
}
|
|
|
|
|
2007-05-27 08:50:56 -07:00
|
|
|
IMAGEFILE *iV_LoadImageFile(const char *fileName)
|
|
|
|
{
|
2008-01-01 13:33:47 -08:00
|
|
|
char *pFileData, *ptr, *dot;
|
2008-01-06 13:21:31 -08:00
|
|
|
unsigned int pFileSize, numImages = 0, i, tPages = 0;
|
2007-05-27 08:50:56 -07:00
|
|
|
IMAGEFILE *ImageFile;
|
2008-01-01 13:33:47 -08:00
|
|
|
char texFileName[PATH_MAX];
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2008-01-01 13:33:47 -08:00
|
|
|
if (!loadFile(fileName, &pFileData, &pFileSize))
|
2007-05-27 08:50:56 -07:00
|
|
|
{
|
2008-01-01 13:33:47 -08:00
|
|
|
debug(LOG_ERROR, "iV_LoadImageFile: failed to open %s", fileName);
|
2007-06-28 10:47:08 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
2008-01-01 13:33:47 -08:00
|
|
|
ptr = pFileData;
|
|
|
|
// count lines, which is identical to number of images
|
|
|
|
while (ptr < pFileData + pFileSize && *ptr != '\0')
|
2007-05-27 08:50:56 -07:00
|
|
|
{
|
2008-01-01 13:33:47 -08:00
|
|
|
numImages += (*ptr == '\n') ? 1 : 0;
|
|
|
|
ptr++;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
2008-01-06 13:21:31 -08:00
|
|
|
ImageFile = malloc(sizeof(IMAGEFILE) + sizeof(IMAGEDEF) * numImages);
|
|
|
|
ImageFile->ImageDefs = (IMAGEDEF*)(ImageFile + 1); // we allocated extra space for it
|
2008-01-01 13:33:47 -08:00
|
|
|
ptr = pFileData;
|
|
|
|
numImages = 0;
|
|
|
|
while (ptr < pFileData + pFileSize)
|
2007-05-27 09:41:21 -07:00
|
|
|
{
|
2008-01-01 13:33:47 -08:00
|
|
|
int temp, retval;
|
|
|
|
IMAGEDEF *ImageDef = &ImageFile->ImageDefs[numImages];
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2008-03-16 05:39:08 -07:00
|
|
|
retval = sscanf(ptr, "%u,%u,%u,%u,%u,%d,%d%n", &ImageDef->TPageID, &ImageDef->Tu, &ImageDef->Tv, &ImageDef->Width,
|
2008-01-01 13:33:47 -08:00
|
|
|
&ImageDef->Height, &ImageDef->XOffset, &ImageDef->YOffset, &temp);
|
|
|
|
if (retval != 7)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ptr += temp;
|
|
|
|
numImages++;
|
|
|
|
// Find number of texture pages to load (no gaps allowed in number series, eg use intfac0, intfac1, etc.!)
|
|
|
|
if (ImageDef->TPageID > tPages)
|
2007-06-19 09:51:40 -07:00
|
|
|
{
|
2008-01-01 13:33:47 -08:00
|
|
|
tPages = ImageDef->TPageID;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
2008-01-02 11:15:45 -08:00
|
|
|
while (ptr < pFileData + pFileSize && *ptr++ != '\n'); // skip rest of line
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
2008-01-01 13:33:47 -08:00
|
|
|
dot = strrchr(fileName, '/'); // go to last path character
|
|
|
|
dot++; // skip it
|
|
|
|
strcpy(texFileName, dot); // make a copy
|
|
|
|
dot = strchr(texFileName, '.'); // find extension
|
|
|
|
*dot = '\0'; // then discard it
|
|
|
|
// Load the texture pages.
|
|
|
|
for (i = 0; i <= tPages; i++)
|
|
|
|
{
|
|
|
|
char path[PATH_MAX];
|
|
|
|
|
2008-01-06 13:21:31 -08:00
|
|
|
snprintf(path, PATH_MAX, "%s%u.png", texFileName, i);
|
2008-01-01 13:33:47 -08:00
|
|
|
ImageFile->TPageIDs[i] = LoadTextureFile(path);
|
|
|
|
}
|
|
|
|
ImageFile->NumImages = numImages;
|
|
|
|
free(pFileData);
|
2007-05-27 12:28:54 -07:00
|
|
|
|
2007-06-28 10:47:08 -07:00
|
|
|
return ImageFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
void iV_FreeImageFile(IMAGEFILE *ImageFile)
|
|
|
|
{
|
2007-04-15 03:43:05 -07:00
|
|
|
free(ImageFile);
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|