2007-01-15 12:09:25 -08:00
|
|
|
/*
|
|
|
|
This file is part of Warzone 2100.
|
|
|
|
Copyright (C) 1999-2004 Eidos Interactive
|
|
|
|
Copyright (C) 2005-2007 Warzone Resurrection Project
|
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
2007-06-28 10:47:08 -07:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2008-01-01 13:33:47 -08:00
|
|
|
#include "lib/framework/frame.h"
|
|
|
|
#include "lib/framework/frameresource.h"
|
|
|
|
|
2007-06-28 10:47:08 -07:00
|
|
|
#include "piepalette.h"
|
|
|
|
#include "tex.h"
|
|
|
|
#include "ivispatch.h"
|
|
|
|
#include "bitimage.h"
|
2007-05-27 08:50:56 -07:00
|
|
|
#include <physfs.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-01-01 13:33:47 -08:00
|
|
|
iTexture *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-01-01 13:33:47 -08:00
|
|
|
pSprite = (iTexture*)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;
|
|
|
|
UDWORD 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-01 14:13:27 -08:00
|
|
|
ImageFile = malloc(sizeof(IMAGEFILE));
|
|
|
|
ImageFile->ImageDefs = malloc(sizeof(IMAGEDEF) * numImages);
|
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-01-01 13:33:47 -08:00
|
|
|
retval = sscanf(ptr, "%d,%d,%d,%d,%d,%d,%d%n", &ImageDef->TPageID, &ImageDef->Tu, &ImageDef->Tv, &ImageDef->Width,
|
|
|
|
&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-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];
|
|
|
|
|
|
|
|
snprintf(path, PATH_MAX, "%s%d.png", texFileName, i);
|
|
|
|
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
|
|
|
}
|