2008-08-04 12:05:52 +03:00
|
|
|
#include "../stdafx.h"
|
|
|
|
#include "SDL.h"
|
|
|
|
#include "CDefHandler.h"
|
|
|
|
#include <sstream>
|
|
|
|
#include "CLodHandler.h"
|
2009-05-09 19:18:27 +03:00
|
|
|
#include "../lib/VCMI_Lib.h"
|
2009-04-15 17:03:31 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* CDefHandler.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2009-05-24 01:57:39 +03:00
|
|
|
static long long pow(long long a, int b)
|
2008-08-04 12:05:52 +03:00
|
|
|
{
|
|
|
|
if (!b) return 1;
|
|
|
|
long c = a;
|
|
|
|
while (--b)
|
|
|
|
a*=c;
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
CDefHandler::CDefHandler()
|
|
|
|
{
|
|
|
|
//FDef = NULL;
|
|
|
|
RWEntries = NULL;
|
|
|
|
notFreeImgs = false;
|
|
|
|
}
|
|
|
|
CDefHandler::~CDefHandler()
|
|
|
|
{
|
|
|
|
//if (FDef)
|
|
|
|
//delete [] FDef;
|
|
|
|
if (RWEntries)
|
|
|
|
delete [] RWEntries;
|
|
|
|
if (notFreeImgs)
|
|
|
|
return;
|
2008-12-21 21:17:35 +02:00
|
|
|
for (size_t i=0; i<ourImages.size(); ++i)
|
2008-08-04 12:05:52 +03:00
|
|
|
{
|
|
|
|
if (ourImages[i].bitmap)
|
|
|
|
{
|
|
|
|
SDL_FreeSurface(ourImages[i].bitmap);
|
|
|
|
ourImages[i].bitmap=NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CDefEssential::~CDefEssential()
|
|
|
|
{
|
2008-12-21 21:17:35 +02:00
|
|
|
for(size_t i=0; i < ourImages.size(); ++i)
|
2008-08-04 12:05:52 +03:00
|
|
|
SDL_FreeSurface(ourImages[i].bitmap);
|
|
|
|
}
|
|
|
|
void CDefHandler::openDef(std::string name)
|
|
|
|
{
|
|
|
|
int i,j, totalInBlock;
|
|
|
|
char Buffer[13];
|
2008-11-11 17:17:58 +02:00
|
|
|
BMPPalette palette[256];
|
2008-08-04 12:05:52 +03:00
|
|
|
defName=name;
|
|
|
|
|
|
|
|
int andame;
|
|
|
|
std::ifstream * is = new std::ifstream();
|
|
|
|
is -> open(name.c_str(),std::ios::binary);
|
|
|
|
is->seekg(0,std::ios::end); // na koniec
|
|
|
|
andame = is->tellg(); // read length
|
|
|
|
is->seekg(0,std::ios::beg); // wracamy na poczatek
|
|
|
|
unsigned char * FDef = new unsigned char[andame]; // allocate memory
|
|
|
|
is->read((char*)FDef, andame); // read map file to buffer
|
|
|
|
is->close();
|
|
|
|
delete is;
|
|
|
|
i = 0;
|
|
|
|
DEFType = readNormalNr(i,4,FDef); i+=4;
|
2008-11-12 20:59:33 +02:00
|
|
|
width = readNormalNr(i,4,FDef); i+=4;
|
|
|
|
height = readNormalNr(i,4,FDef); i+=4;
|
2008-08-04 12:05:52 +03:00
|
|
|
i=0xc;
|
|
|
|
totalBlocks = readNormalNr(i,4,FDef); i+=4;
|
|
|
|
|
|
|
|
i=0x10;
|
|
|
|
for (int it=0;it<256;it++)
|
|
|
|
{
|
|
|
|
palette[it].R = FDef[i++];
|
|
|
|
palette[it].G = FDef[i++];
|
|
|
|
palette[it].B = FDef[i++];
|
|
|
|
palette[it].F = 0;
|
|
|
|
}
|
|
|
|
i=0x310;
|
|
|
|
totalEntries=0;
|
|
|
|
for (int z=0; z<totalBlocks; z++)
|
|
|
|
{
|
|
|
|
i+=4;
|
|
|
|
totalInBlock = readNormalNr(i,4,FDef); i+=4;
|
|
|
|
for (j=SEntries.size(); j<totalEntries+totalInBlock; j++)
|
|
|
|
SEntries.push_back(SEntry());
|
|
|
|
i+=8;
|
|
|
|
for (j=0; j<totalInBlock; j++)
|
|
|
|
{
|
|
|
|
for (int k=0;k<13;k++) Buffer[k]=FDef[i+k];
|
|
|
|
i+=13;
|
|
|
|
SEntries[totalEntries+j].name=Buffer;
|
|
|
|
}
|
|
|
|
for (j=0; j<totalInBlock; j++)
|
|
|
|
{
|
|
|
|
SEntries[totalEntries+j].offset = readNormalNr(i,4,FDef);
|
|
|
|
i+=4;
|
|
|
|
}
|
|
|
|
//totalEntries+=totalInBlock;
|
|
|
|
for(int hh=0; hh<totalInBlock; ++hh)
|
|
|
|
{
|
|
|
|
SEntries[totalEntries].group = z;
|
|
|
|
++totalEntries;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for(j=0; j<SEntries.size(); ++j)
|
|
|
|
{
|
|
|
|
SEntries[j].name = SEntries[j].name.substr(0, SEntries[j].name.find('.')+4);
|
|
|
|
}
|
2008-12-21 21:17:35 +02:00
|
|
|
for(size_t i=0; i < SEntries.size(); ++i)
|
2008-08-04 12:05:52 +03:00
|
|
|
{
|
|
|
|
Cimage nimg;
|
2008-11-11 17:17:58 +02:00
|
|
|
nimg.bitmap = getSprite(i, FDef, palette);
|
2008-08-04 12:05:52 +03:00
|
|
|
nimg.imName = SEntries[i].name;
|
|
|
|
nimg.groupNumber = SEntries[i].group;
|
|
|
|
ourImages.push_back(nimg);
|
|
|
|
}
|
|
|
|
delete [] FDef;
|
|
|
|
FDef = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CDefHandler::openFromMemory(unsigned char *table, std::string name)
|
|
|
|
{
|
|
|
|
int i,j, totalInBlock;
|
2008-11-11 17:17:58 +02:00
|
|
|
BMPPalette palette[256];
|
2008-08-04 12:05:52 +03:00
|
|
|
defName=name;
|
|
|
|
i = 0;
|
|
|
|
DEFType = readNormalNr(i,4,table); i+=4;
|
2008-11-12 20:59:33 +02:00
|
|
|
width = readNormalNr(i,4,table); i+=4;
|
|
|
|
height = readNormalNr(i,4,table); i+=4;
|
2008-08-04 12:05:52 +03:00
|
|
|
i=0xc;
|
|
|
|
totalBlocks = readNormalNr(i,4,table); i+=4;
|
|
|
|
|
|
|
|
i=0x10;
|
|
|
|
for (int it=0;it<256;it++)
|
|
|
|
{
|
|
|
|
palette[it].R = table[i++];
|
|
|
|
palette[it].G = table[i++];
|
|
|
|
palette[it].B = table[i++];
|
|
|
|
palette[it].F = 0;
|
|
|
|
}
|
|
|
|
i=0x310;
|
|
|
|
totalEntries=0;
|
|
|
|
for (int z=0; z<totalBlocks; z++)
|
|
|
|
{
|
2008-12-21 21:17:35 +02:00
|
|
|
int unknown1 = readNormalNr(i,4,table); i+=4; //TODO use me
|
2008-08-04 12:05:52 +03:00
|
|
|
totalInBlock = readNormalNr(i,4,table); i+=4;
|
|
|
|
for (j=SEntries.size(); j<totalEntries+totalInBlock; j++)
|
|
|
|
SEntries.push_back(SEntry());
|
2008-12-21 21:17:35 +02:00
|
|
|
int unknown2 = readNormalNr(i,4,table); //TODO use me
|
|
|
|
i+=4;
|
|
|
|
int unknown3 = readNormalNr(i,4,table); //TODO use me
|
|
|
|
i+=4;
|
2008-08-04 12:05:52 +03:00
|
|
|
for (j=0; j<totalInBlock; j++)
|
|
|
|
{
|
2009-08-19 08:00:59 +03:00
|
|
|
char Buffer[13];
|
|
|
|
memcpy(Buffer, &table[i], 12);
|
|
|
|
Buffer[12] = 0;
|
2008-08-04 12:05:52 +03:00
|
|
|
SEntries[totalEntries+j].name=Buffer;
|
2009-08-19 08:00:59 +03:00
|
|
|
|
|
|
|
i+=13;
|
2008-08-04 12:05:52 +03:00
|
|
|
}
|
|
|
|
for (j=0; j<totalInBlock; j++)
|
|
|
|
{
|
|
|
|
SEntries[totalEntries+j].offset = readNormalNr(i,4,table);
|
2008-12-21 21:17:35 +02:00
|
|
|
int unknown4 = readNormalNr(i,4,table); //TODO use me
|
|
|
|
i+=4;
|
2008-08-04 12:05:52 +03:00
|
|
|
}
|
|
|
|
//totalEntries+=totalInBlock;
|
|
|
|
for(int hh=0; hh<totalInBlock; ++hh)
|
|
|
|
{
|
|
|
|
SEntries[totalEntries].group = z;
|
|
|
|
++totalEntries;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for(j=0; j<SEntries.size(); ++j)
|
|
|
|
{
|
|
|
|
SEntries[j].name = SEntries[j].name.substr(0, SEntries[j].name.find('.')+4);
|
|
|
|
}
|
2008-11-12 20:59:33 +02:00
|
|
|
RWEntries = new unsigned int[height];
|
2008-12-21 21:17:35 +02:00
|
|
|
for(size_t i=0; i < SEntries.size(); ++i)
|
2008-08-04 12:05:52 +03:00
|
|
|
{
|
|
|
|
Cimage nimg;
|
2008-11-11 17:17:58 +02:00
|
|
|
nimg.bitmap = getSprite(i, table, palette);
|
2008-08-04 12:05:52 +03:00
|
|
|
nimg.imName = SEntries[i].name;
|
|
|
|
nimg.groupNumber = SEntries[i].group;
|
|
|
|
ourImages.push_back(nimg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char * CDefHandler::writeNormalNr (int nr, int bytCon)
|
|
|
|
{
|
|
|
|
//int tralalalatoniedziala = 2*9+100-4*bytCon;
|
|
|
|
//unsigned char * ret = new unsigned char[bytCon];
|
|
|
|
unsigned char * ret = NULL;
|
|
|
|
for(int jj=0; jj<100; ++jj)
|
|
|
|
{
|
|
|
|
ret = (unsigned char*)calloc(1, bytCon);
|
|
|
|
if(ret!=NULL)
|
|
|
|
break;
|
|
|
|
}
|
2008-09-24 19:25:14 +03:00
|
|
|
long long amp = pow((long long int)256,bytCon-1);
|
2008-08-04 12:05:52 +03:00
|
|
|
for (int i=bytCon-1; i>=0;i--)
|
|
|
|
{
|
|
|
|
int test2 = nr/(amp);
|
|
|
|
ret[i]=test2;
|
|
|
|
nr -= (nr/(amp))*amp;
|
|
|
|
amp/=256;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
void CDefHandler::expand(unsigned char N,unsigned char & BL, unsigned char & BR)
|
|
|
|
{
|
|
|
|
BL = (N & 0xE0) >> 5;
|
|
|
|
BR = N & 0x1F;
|
|
|
|
}
|
2009-08-17 11:50:31 +03:00
|
|
|
int CDefHandler::readNormalNr (int pos, int bytCon, const unsigned char * str, bool cyclic)
|
2008-08-04 12:05:52 +03:00
|
|
|
{
|
|
|
|
int ret=0;
|
|
|
|
int amp=1;
|
|
|
|
if (str)
|
|
|
|
{
|
|
|
|
for (int i=0; i<bytCon; i++)
|
|
|
|
{
|
|
|
|
ret+=str[pos+i]*amp;
|
|
|
|
amp*=256;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//else
|
|
|
|
//{
|
|
|
|
// for (int i=0; i<bytCon; i++)
|
|
|
|
// {
|
|
|
|
// ret+=FDef[pos+i]*amp;
|
|
|
|
// amp*=256;
|
|
|
|
// }
|
|
|
|
//}
|
|
|
|
if(cyclic && bytCon<4 && ret>=amp/2)
|
|
|
|
{
|
|
|
|
ret = ret-amp;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
void CDefHandler::print (std::ostream & stream, int nr, int bytcon)
|
|
|
|
{
|
|
|
|
unsigned char * temp = writeNormalNr(nr,bytcon);
|
|
|
|
for (int i=0;i<bytcon;i++)
|
|
|
|
stream << char(temp[i]);
|
|
|
|
free(temp);
|
|
|
|
}
|
|
|
|
|
2008-11-11 17:17:58 +02:00
|
|
|
SDL_Surface * CDefHandler::getSprite (int SIndex, unsigned char * FDef, BMPPalette * palette)
|
2008-08-04 12:05:52 +03:00
|
|
|
{
|
|
|
|
SDL_Surface * ret=NULL;
|
|
|
|
|
|
|
|
long BaseOffset,
|
|
|
|
SpriteWidth, SpriteHeight, //format sprite'a
|
|
|
|
LeftMargin, RightMargin, TopMargin,BottomMargin,
|
|
|
|
i, add, FullHeight,FullWidth,
|
|
|
|
TotalRowLength, // dlugosc przeczytanego segmentu
|
2008-12-21 21:17:35 +02:00
|
|
|
RowAdd;//, NextSpriteOffset; //TODO use me
|
2008-08-04 12:05:52 +03:00
|
|
|
|
2009-05-23 07:22:21 +03:00
|
|
|
unsigned char SegmentType;//, BL, BR; //TODO use me
|
2008-08-04 12:05:52 +03:00
|
|
|
|
|
|
|
i=BaseOffset=SEntries[SIndex].offset;
|
2008-12-22 19:48:41 +02:00
|
|
|
int prSize=readNormalNr(i,4,FDef);i+=4; //TODO use me
|
2008-08-04 12:05:52 +03:00
|
|
|
int defType2 = readNormalNr(i,4,FDef);i+=4;
|
|
|
|
FullWidth = readNormalNr(i,4,FDef);i+=4;
|
|
|
|
FullHeight = readNormalNr(i,4,FDef);i+=4;
|
|
|
|
SpriteWidth = readNormalNr(i,4,FDef);i+=4;
|
|
|
|
SpriteHeight = readNormalNr(i,4,FDef);i+=4;
|
|
|
|
LeftMargin = readNormalNr(i,4,FDef);i+=4;
|
|
|
|
TopMargin = readNormalNr(i,4,FDef);i+=4;
|
|
|
|
RightMargin = FullWidth - SpriteWidth - LeftMargin;
|
|
|
|
BottomMargin = FullHeight - SpriteHeight - TopMargin;
|
|
|
|
|
|
|
|
//if(LeftMargin + RightMargin < 0)
|
|
|
|
// SpriteWidth += LeftMargin + RightMargin; //ugly construction... TODO: check how to do it nicer
|
|
|
|
if(LeftMargin<0)
|
|
|
|
SpriteWidth+=LeftMargin;
|
|
|
|
if(RightMargin<0)
|
|
|
|
SpriteWidth+=RightMargin;
|
|
|
|
|
2009-05-23 07:22:21 +03:00
|
|
|
// Note: this looks bogus because we allocate only FullWidth, not FullWidth+add
|
2008-08-04 12:05:52 +03:00
|
|
|
add = 4 - FullWidth%4;
|
|
|
|
if (add==4)
|
|
|
|
add=0;
|
|
|
|
|
|
|
|
ret = SDL_CreateRGBSurface(SDL_SWSURFACE, FullWidth, FullHeight, 8, 0, 0, 0, 0);
|
|
|
|
//int tempee2 = readNormalNr(0,4,((unsigned char *)tempee.c_str()));
|
|
|
|
|
|
|
|
int BaseOffsetor = BaseOffset = i;
|
|
|
|
|
|
|
|
for(int i=0; i<256; ++i)
|
|
|
|
{
|
|
|
|
SDL_Color pr;
|
|
|
|
pr.r = palette[i].R;
|
|
|
|
pr.g = palette[i].G;
|
|
|
|
pr.b = palette[i].B;
|
|
|
|
pr.unused = palette[i].F;
|
|
|
|
(*(ret->format->palette->colors+i))=pr;
|
|
|
|
}
|
|
|
|
|
2009-05-23 07:22:21 +03:00
|
|
|
int ftcp=0;
|
|
|
|
|
|
|
|
// If there's a margin anywhere, just blank out the whole surface.
|
|
|
|
if (TopMargin > 0 || BottomMargin > 0 || LeftMargin > 0 || RightMargin > 0) {
|
|
|
|
memset(((char *)ret->pixels), 0, FullHeight*FullWidth);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip top margin
|
|
|
|
if (TopMargin > 0)
|
|
|
|
ftcp += TopMargin*(FullWidth+add);
|
|
|
|
|
2009-05-24 17:32:36 +03:00
|
|
|
switch(defType2)
|
|
|
|
{
|
2009-05-23 07:22:21 +03:00
|
|
|
case 0:
|
2008-08-04 12:05:52 +03:00
|
|
|
{
|
|
|
|
for (int i=0;i<SpriteHeight;i++)
|
|
|
|
{
|
|
|
|
if (LeftMargin>0)
|
2009-05-23 07:22:21 +03:00
|
|
|
ftcp += LeftMargin;
|
|
|
|
|
|
|
|
memcpy((char*)(ret->pixels)+ftcp, &FDef[BaseOffset], SpriteWidth);
|
|
|
|
ftcp += SpriteWidth;
|
|
|
|
BaseOffset += SpriteWidth;
|
|
|
|
|
2008-08-04 12:05:52 +03:00
|
|
|
if (RightMargin>0)
|
2009-05-23 07:22:21 +03:00
|
|
|
ftcp += RightMargin;
|
2008-08-04 12:05:52 +03:00
|
|
|
}
|
|
|
|
}
|
2009-05-23 07:22:21 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
2008-08-04 12:05:52 +03:00
|
|
|
{
|
2008-11-12 20:59:33 +02:00
|
|
|
memcpy(RWEntries, FDef+BaseOffset, SpriteHeight*sizeof(int));
|
|
|
|
BaseOffset += sizeof(int) * SpriteHeight;
|
2008-08-04 12:05:52 +03:00
|
|
|
for (int i=0;i<SpriteHeight;i++)
|
|
|
|
{
|
2008-11-11 17:17:58 +02:00
|
|
|
BaseOffset=BaseOffsetor+RWEntries[i];
|
2008-08-04 12:05:52 +03:00
|
|
|
if (LeftMargin>0)
|
2009-05-23 07:22:21 +03:00
|
|
|
ftcp += LeftMargin;
|
|
|
|
|
2008-08-04 12:05:52 +03:00
|
|
|
TotalRowLength=0;
|
|
|
|
do
|
|
|
|
{
|
2009-05-23 07:22:21 +03:00
|
|
|
unsigned int SegmentLength;
|
|
|
|
|
2008-08-04 12:05:52 +03:00
|
|
|
SegmentType=FDef[BaseOffset++];
|
2009-05-23 07:22:21 +03:00
|
|
|
SegmentLength=FDef[BaseOffset++] + 1;
|
|
|
|
|
2008-08-04 12:05:52 +03:00
|
|
|
if (SegmentType==0xFF)
|
|
|
|
{
|
2009-05-23 07:22:21 +03:00
|
|
|
for (int k=0;k<SegmentLength;k++)
|
2008-08-04 12:05:52 +03:00
|
|
|
{
|
|
|
|
((char*)(ret->pixels))[ftcp++]=FDef[BaseOffset+k];
|
2009-05-23 07:22:21 +03:00
|
|
|
if ((TotalRowLength+k)>=SpriteWidth)
|
2008-08-04 12:05:52 +03:00
|
|
|
break;
|
|
|
|
}
|
2009-05-23 07:22:21 +03:00
|
|
|
BaseOffset+=SegmentLength;
|
|
|
|
TotalRowLength+=SegmentLength;
|
2008-08-04 12:05:52 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-05-23 07:22:21 +03:00
|
|
|
memset((char*)(ret->pixels)+ftcp, SegmentType, SegmentLength);
|
|
|
|
ftcp += SegmentLength;
|
|
|
|
TotalRowLength += SegmentLength;
|
2008-08-04 12:05:52 +03:00
|
|
|
}
|
|
|
|
}while(TotalRowLength<SpriteWidth);
|
2009-05-23 07:22:21 +03:00
|
|
|
|
2008-08-04 12:05:52 +03:00
|
|
|
RowAdd=SpriteWidth-TotalRowLength;
|
2009-05-23 07:22:21 +03:00
|
|
|
|
2008-08-04 12:05:52 +03:00
|
|
|
if (RightMargin>0)
|
2009-05-23 07:22:21 +03:00
|
|
|
ftcp += RightMargin;
|
|
|
|
|
2008-08-04 12:05:52 +03:00
|
|
|
if (add>0)
|
2009-05-23 07:22:21 +03:00
|
|
|
ftcp += add+RowAdd;
|
2008-08-04 12:05:52 +03:00
|
|
|
}
|
|
|
|
}
|
2009-05-23 07:22:21 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
2008-08-04 12:05:52 +03:00
|
|
|
{
|
|
|
|
for (int i=0;i<SpriteHeight;i++)
|
|
|
|
{
|
|
|
|
BaseOffset=BaseOffsetor+i*2*(SpriteWidth/32);
|
|
|
|
RWEntries[i] = readNormalNr(BaseOffset,2,FDef);
|
|
|
|
}
|
|
|
|
BaseOffset = BaseOffsetor+RWEntries[0];
|
|
|
|
for (int i=0;i<SpriteHeight;i++)
|
|
|
|
{
|
|
|
|
//BaseOffset = BaseOffsetor+RWEntries[i];
|
|
|
|
if (LeftMargin>0)
|
2009-05-23 07:22:21 +03:00
|
|
|
ftcp += LeftMargin;
|
|
|
|
|
2008-08-04 12:05:52 +03:00
|
|
|
TotalRowLength=0;
|
2009-05-23 07:22:21 +03:00
|
|
|
|
2008-08-04 12:05:52 +03:00
|
|
|
do
|
|
|
|
{
|
|
|
|
SegmentType=FDef[BaseOffset++];
|
|
|
|
unsigned char code = SegmentType / 32;
|
|
|
|
unsigned char value = (SegmentType & 31) + 1;
|
|
|
|
if(code==7)
|
|
|
|
{
|
2009-05-23 07:22:21 +03:00
|
|
|
memcpy((char*)(ret->pixels)+ftcp, &FDef[BaseOffset], value);
|
|
|
|
ftcp += value;
|
|
|
|
BaseOffset += value;
|
2008-08-04 12:05:52 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-05-23 07:22:21 +03:00
|
|
|
memset((char*)(ret->pixels)+ftcp, code, value);
|
|
|
|
ftcp += value;
|
2008-08-04 12:05:52 +03:00
|
|
|
}
|
|
|
|
TotalRowLength+=value;
|
|
|
|
} while(TotalRowLength<SpriteWidth);
|
2009-05-23 07:22:21 +03:00
|
|
|
|
2008-08-04 12:05:52 +03:00
|
|
|
if (RightMargin>0)
|
2009-05-23 07:22:21 +03:00
|
|
|
ftcp += RightMargin;
|
|
|
|
|
2009-05-24 17:32:36 +03:00
|
|
|
RowAdd=SpriteWidth-TotalRowLength;
|
|
|
|
|
2008-08-04 12:05:52 +03:00
|
|
|
if (add>0)
|
2009-05-23 07:22:21 +03:00
|
|
|
ftcp += add+RowAdd;
|
2008-08-04 12:05:52 +03:00
|
|
|
}
|
|
|
|
}
|
2009-05-23 07:22:21 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
2008-08-04 12:05:52 +03:00
|
|
|
{
|
|
|
|
for (int i=0;i<SpriteHeight;i++)
|
|
|
|
{
|
|
|
|
BaseOffset=BaseOffsetor+i*2*(SpriteWidth/32);
|
|
|
|
RWEntries[i] = readNormalNr(BaseOffset,2,FDef);
|
|
|
|
}
|
|
|
|
for (int i=0;i<SpriteHeight;i++)
|
|
|
|
{
|
|
|
|
BaseOffset = BaseOffsetor+RWEntries[i];
|
|
|
|
if (LeftMargin>0)
|
2009-05-23 07:22:21 +03:00
|
|
|
ftcp += LeftMargin;
|
|
|
|
|
2008-08-04 12:05:52 +03:00
|
|
|
TotalRowLength=0;
|
2009-05-23 07:22:21 +03:00
|
|
|
|
2008-08-04 12:05:52 +03:00
|
|
|
do
|
|
|
|
{
|
|
|
|
SegmentType=FDef[BaseOffset++];
|
|
|
|
unsigned char code = SegmentType / 32;
|
|
|
|
unsigned char value = (SegmentType & 31) + 1;
|
|
|
|
if(code==7)
|
|
|
|
{
|
|
|
|
for(int h=0; h<value; ++h)
|
|
|
|
{
|
|
|
|
if(h<-LeftMargin)
|
|
|
|
continue;
|
|
|
|
if(h+TotalRowLength>=SpriteWidth)
|
|
|
|
break;
|
|
|
|
((char*)(ret->pixels))[ftcp++]=FDef[BaseOffset++];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for(int h=0; h<value; ++h)
|
|
|
|
{
|
|
|
|
if(h<-LeftMargin)
|
|
|
|
continue;
|
|
|
|
if(h+TotalRowLength>=SpriteWidth)
|
|
|
|
break;
|
|
|
|
((char*)(ret->pixels))[ftcp++]=code;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TotalRowLength+=( LeftMargin>=0 ? value : value+LeftMargin );
|
|
|
|
}while(TotalRowLength<SpriteWidth);
|
2009-05-23 07:22:21 +03:00
|
|
|
|
2008-08-04 12:05:52 +03:00
|
|
|
if (RightMargin>0)
|
2009-05-23 07:22:21 +03:00
|
|
|
ftcp += RightMargin;
|
|
|
|
|
2009-05-24 17:32:36 +03:00
|
|
|
RowAdd=SpriteWidth-TotalRowLength;
|
|
|
|
|
2008-08-04 12:05:52 +03:00
|
|
|
if (add>0)
|
2009-05-23 07:22:21 +03:00
|
|
|
ftcp += add+RowAdd;
|
2008-08-04 12:05:52 +03:00
|
|
|
}
|
|
|
|
}
|
2009-05-23 07:22:21 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw std::string("Unknown sprite format.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-08-04 12:05:52 +03:00
|
|
|
SDL_Color ttcol = ret->format->palette->colors[0];
|
|
|
|
Uint32 keycol = SDL_MapRGBA(ret->format, ttcol.r, ttcol.b, ttcol.g, ttcol.unused);
|
|
|
|
SDL_SetColorKey(ret, SDL_SRCCOLORKEY, keycol);
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
|
|
|
CDefEssential * CDefHandler::essentialize()
|
|
|
|
{
|
|
|
|
CDefEssential * ret = new CDefEssential;
|
|
|
|
ret->ourImages = ourImages;
|
|
|
|
notFreeImgs = true;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-05-09 19:18:27 +03:00
|
|
|
CDefHandler * CDefHandler::giveDef(std::string defName)
|
2008-08-04 12:05:52 +03:00
|
|
|
{
|
|
|
|
unsigned char * data = spriteh->giveFile(defName);
|
2008-09-24 19:25:14 +03:00
|
|
|
if(!data)
|
|
|
|
throw "bad def name!";
|
2008-08-04 12:05:52 +03:00
|
|
|
CDefHandler * nh = new CDefHandler();
|
|
|
|
nh->openFromMemory(data, defName);
|
|
|
|
nh->alphaTransformed = false;
|
|
|
|
delete [] data;
|
|
|
|
return nh;
|
|
|
|
}
|
2009-05-09 19:18:27 +03:00
|
|
|
CDefEssential * CDefHandler::giveDefEss(std::string defName)
|
2008-08-04 12:05:52 +03:00
|
|
|
{
|
|
|
|
CDefEssential * ret;
|
2009-05-09 19:18:27 +03:00
|
|
|
CDefHandler * temp = giveDef(defName);
|
2008-08-04 12:05:52 +03:00
|
|
|
ret = temp->essentialize();
|
|
|
|
delete temp;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|