1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-26 08:41:13 +02:00
vcmi/CDefHandler.cpp

567 lines
25 KiB
C++
Raw Normal View History

#include "stdafx.h"
#include "CDefHandler.h"
#include "SDL_image.h"
#include <sstream>
long long pow(long long a, int b)
{
if (!b) return 1;
long c = a;
while (--b)
a*=c;
return a;
}
void BMPHeader::print(std::ostream & out)
{
CDefHandler::print(out,fullSize,4);
CDefHandler::print(out,_h1,4);
CDefHandler::print(out,_c1,4);
CDefHandler::print(out,_c2,4);
CDefHandler::print(out,x,4);
CDefHandler::print(out,y,4);
CDefHandler::print(out,_c3,2);
CDefHandler::print(out,_c4,2);
CDefHandler::print(out,_h2,4);
CDefHandler::print(out,_h3,4);
CDefHandler::print(out,dataSize1,4);
CDefHandler::print(out,dataSize2,4);
for (int i=0;i<8;i++)
out << _c5[i];
out.flush();
}
void CDefHandler::openDef(std::string name)
{
int i,j, totalInBlock;
char Buffer[13];
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
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); i+=4;
fullWidth = readNormalNr(i,4); i+=4;
fullHeight = readNormalNr(i,4); i+=4;
i=0xc;
totalBlocks = readNormalNr(i,4); 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); 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);
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);
}
2007-07-07 14:09:25 +03:00
for(int i=0; i<SEntries.size(); ++i)
{
Cimage nimg;
nimg.bitmap = getSprite(i);
nimg.imName = SEntries[i].name;
nimg.groupNumber = SEntries[i].group;
2007-07-07 14:09:25 +03:00
ourImages.push_back(nimg);
}
}
void CDefHandler::openFromMemory(unsigned char *table, int size, std::string name)
{
int i,j, totalInBlock;
char Buffer[13];
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
FDef = new unsigned char[andame]; // allocate memory
is->read((char*)FDef, andame); // read map file to buffer
is->close();
delete is;*/
//FDef = new unsigned char[size]; // allocate memory
while (!(FDef = (unsigned char *)malloc((sizeof(unsigned char))*size)));
for (int i=0;i<size;i++)
FDef[i]=table[i];
int andame = size;
i = 0;
DEFType = readNormalNr(i,4); i+=4;
fullWidth = readNormalNr(i,4); i+=4;
fullHeight = readNormalNr(i,4); i+=4;
i=0xc;
totalBlocks = readNormalNr(i,4); i+=4;
i=0x10;
for (int it=0;it<256;it++)
{
2007-07-07 14:09:25 +03:00
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); i+=4;
for (j=SEntries.size(); j<totalEntries+totalInBlock; j++)
SEntries.push_back(SEntry());
i+=8;
for (j=0; j<totalInBlock; j++)
{
2007-07-07 14:09:25 +03:00
for (int k=0;k<13;k++) Buffer[k]=FDef[i+k];
i+=13;
SEntries[totalEntries+j].name=Buffer;
}
2007-07-07 14:09:25 +03:00
for (j=0; j<totalInBlock; j++)
{
SEntries[totalEntries+j].offset = readNormalNr(i,4);
i+=4;
}
//totalEntries+=totalInBlock;
for(int hh=0; hh<totalInBlock; ++hh)
{
SEntries[totalEntries].group = z;
++totalEntries;
}
2007-07-07 14:09:25 +03:00
}
for(j=0; j<SEntries.size(); ++j)
{
SEntries[j].name = SEntries[j].name.substr(0, SEntries[j].name.find('.')+4);
}
for(int i=0; i<SEntries.size(); ++i)
{
Cimage nimg;
nimg.bitmap = getSprite(i);
nimg.imName = SEntries[i].name;
nimg.groupNumber = SEntries[i].group;
ourImages.push_back(nimg);
}
}
2007-07-07 14:09:25 +03:00
unsigned char * CDefHandler::writeNormalNr (int nr, int bytCon)
{
2007-07-07 14:09:25 +03:00
//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;
}
long long amp = pow((float)256,bytCon-1);
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;
}
int CDefHandler::readNormalNr (int pos, int bytCon, unsigned char * str, bool cyclic)
{
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]);
2007-07-07 14:09:25 +03:00
free(temp);
}
SDL_Surface * CDefHandler::getSprite (int SIndex) //procedure GetSprite(SIndex: LongInt; var SOut: PStream);
{
//std::ostringstream BMP;
//std::ofstream BMP("testtt.bmp");
std::ofstream BMP;
BMP.open("testtt.bmp", std::ios::binary);
2007-07-07 14:09:25 +03:00
if(!BMP.is_open())
{
return NULL;
}
long BaseOffset,
SpriteWidth, SpriteHeight, //format sprite'a
LeftMargin, RightMargin, TopMargin,BottomMargin, //BABC?K >B :@0O ?>;=>3> 87>1@065=8O
i, add, FullHeight,FullWidth,
TotalRowLength, // dlugosc przeczytanego segmentu
NextSpriteOffset, RowAdd;
std::ifstream Fdef;
unsigned char SegmentType, SegmentLength, BL, BR;
unsigned char * TempDef; //memory
std::string FTemp;
BaseOffset=SEntries[SIndex].offset;//0?><8=05< =0G0;L=>5 A<5I5=85 A?@09B0
i=BaseOffset+4;
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;
BMPHeader tb;
tb.x = FullWidth;
tb.y = FullHeight;
tb.dataSize2 = tb.dataSize1 = tb.x*tb.y;
tb.fullSize = tb.dataSize1+436;
tb._h3=tb.fullSize-36;
add = (int)(4*(((float)1) - ((int)(((int)((float)FullWidth/(float)4))-((float)FullWidth/(float)4)))));
2007-07-01 17:58:44 +03:00
/*if (add==4)
add=0;*/ //moved to defcompression dependent block
BMP << "BM";
//int tempee2 = readNormalNr(0,4,((unsigned char *)tempee.c_str()));
print(BMP,tb.fullSize,4);
BMP << '\0' << '\0' << '\0' << '\0';
print(BMP,0x436,4);
print(BMP,0x28,4);
print(BMP,tb.x,4); //w
print(BMP,tb.y,4); //h
print(BMP,1,2); //layers
print(BMP,8,2);
print(BMP,0,4);
print(BMP,0,4);
print(BMP,tb.dataSize1,4);
print(BMP,tb.dataSize2,4);
print(BMP,0,4);
print(BMP,0,4);
int BaseOffsetor= BaseOffset = i;
for (int i=0;i<256;i++)
{
print(BMP,palette[i].B,1);
print(BMP,palette[i].G,1);
print(BMP,palette[i].R,1);
print(BMP,palette[i].F,1);
}
for (int i=0;i<800;i++)
fbuffer[i]=0;
BMP << std::flush;
if (defType2==0)
{
2007-07-01 17:58:44 +03:00
if (add==4)
add=0;
if (TopMargin>0)
{
for (int i=0;i<TopMargin;i++)
{
for (int j=0;j<FullWidth+add;j++)
FTemp+=fbuffer[j];
}
}
for (int i=0;i<SpriteHeight;i++)
{
if (LeftMargin>0)
{
for (int j=0;j<LeftMargin;j++)
FTemp+=fbuffer[j];
}
for (int j=0; j<SpriteWidth;j++)
FTemp+=FDef[BaseOffset++];
if (RightMargin>0)
{
for (int j=0;j<add;j++)
FTemp+=fbuffer[j];
}
}
if (BottomMargin>0)
{
for (int i=0;i<BottomMargin;i++)
{
for (int j=0;j<FullWidth+add;j++)
FTemp+=fbuffer[j];
}
}
}
if (defType2==1)
{
2007-07-01 17:58:44 +03:00
if (add==4)
add=2;
if (TopMargin>0)
{
for (int i=0;i<TopMargin;i++)
{
for (int j=0;j<FullWidth+add;j++)
FTemp+=fbuffer[j];
}
}
RLEntries = new int[SpriteHeight];
for (int i=0;i<SpriteHeight;i++)
{
RLEntries[i]=readNormalNr(BaseOffset,4,FDef);BaseOffset+=4;
}
for (int i=0;i<SpriteHeight;i++)
{
BaseOffset=BaseOffsetor+RLEntries[i];
if (LeftMargin>0)
{
for (int j=0;j<LeftMargin;j++)
FTemp+=fbuffer[j];
}
TotalRowLength=0;
do
{
SegmentType=FDef[BaseOffset++];
SegmentLength=FDef[BaseOffset++];
if (SegmentType==0xFF)
{
for (int k=0;k<=SegmentLength;k++)
{
FTemp+=FDef[BaseOffset+k];
if ((TotalRowLength+k+1)>=SpriteWidth)
break;
}
TotalRowLength+=SegmentLength+1;
}
else
{
for (int k=0;k<SegmentLength+1;k++)
{
//FTemp+=FDef[BaseOffset+k];//
FTemp+='\0';
}
TotalRowLength+=SegmentLength+1;
}
}while(TotalRowLength<SpriteWidth);
RowAdd=SpriteWidth-TotalRowLength;
if (RightMargin>0)
{
for (int j=0;j<RightMargin;j++)
FTemp+=fbuffer[j];
}
if (add>0)
{
for (int j=0;j<add+RowAdd;j++)
FTemp+=fbuffer[j];
}
}
2007-07-07 14:09:25 +03:00
delete RLEntries;
if (BottomMargin>0)
{
for (int i=0;i<BottomMargin;i++)
{
for (int j=0;j<FullWidth+add;j++)
FTemp+=fbuffer[j];
}
}
}
if (defType2==3)
{
2007-07-01 17:58:44 +03:00
if (add==4)
add=0;
if (TopMargin>0)
{
for (int i=0;i<TopMargin;i++)
{
for (int j=0;j<FullWidth+add;j++)
FTemp+=fbuffer[j];
}
}
RWEntries = new unsigned int[SpriteHeight];
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)
{
for (int j=0;j<LeftMargin;j++)
FTemp+=fbuffer[j];
}
TotalRowLength=0;
do
{
SegmentType=FDef[BaseOffset++];
expand(SegmentType,BL,BR);
2007-07-01 17:58:44 +03:00
if (BL==0) //transparent
{
for (int k=0;k<100;k++)
fbuffer[k]='\0';
for (int k=0;k<BR+1;k++)
{
FTemp+=fbuffer[k];
}
TotalRowLength=TotalRowLength+BR+1;
}
2007-07-01 17:58:44 +03:00
if (BL==1) //shadow
{
for (int k=0;k<100;k++)
2007-07-01 17:58:44 +03:00
fbuffer[k]='\1';
for (int k=0;k<BR+1;k++)
{
FTemp+=fbuffer[k];
}
TotalRowLength=TotalRowLength+BR+1;
}
2007-07-01 17:58:44 +03:00
if (BL==2) //shadow
{
for (int k=0;k<100;k++)
2007-07-01 17:58:44 +03:00
fbuffer[k]='\2';
for (int k=0;k<BR+1;k++)
{
FTemp+=fbuffer[k];
}
TotalRowLength=TotalRowLength+BR+1;
}
2007-07-01 17:58:44 +03:00
if (BL==3) //shadow
{
for (int k=0;k<100;k++)
2007-07-01 17:58:44 +03:00
fbuffer[k]='\3';
for (int k=0;k<BR+1;k++)
{
FTemp+=fbuffer[k];
}
TotalRowLength=TotalRowLength+BR+1;
}
if (BL==4) //shadow
{
for (int k=0;k<100;k++)
fbuffer[k]='\4';
for (int k=0;k<BR+1;k++)
{
FTemp+=fbuffer[k];
}
TotalRowLength=TotalRowLength+BR+1;
}
if (BL==5) //team colour?
{
for (int k=0;k<100;k++)
fbuffer[k]='\5';
for (int k=0;k<BR+1;k++)
{
FTemp+=fbuffer[k];
}
TotalRowLength=TotalRowLength+BR+1;
}
2007-07-01 17:58:44 +03:00
if (BL==7) //opaque
{
for (int k=0;k<BR+1;k++)
{
FTemp+=FDef[BaseOffset++];
}
TotalRowLength=TotalRowLength+BR+1;
}
}while(TotalRowLength<SpriteWidth);
if (RightMargin>0)
{
for (int j=0;j<RightMargin;j++)
FTemp+=fbuffer[j];
}
if (add>0)
{
for (int j=0;j<add+RowAdd;j++)
FTemp+=fbuffer[j];
}
}
2007-07-07 14:09:25 +03:00
delete RWEntries;
if (BottomMargin>0)
{
for (int i=0;i<BottomMargin;i++)
{
for (int j=0;j<FullWidth+add;j++)
FTemp+=fbuffer[j];
}
}
}
for (int i=1;i<=FullHeight;i++)
{
int at = (FullHeight-i)*(FullWidth+add);
for (int j=0;j<FullWidth+add;j++)
{
BMP << FTemp[at+j];
}
}
BMP.close();
return IMG_Load("testtt.bmp");
};
CDefEssential * CDefHandler::essentialize()
{
CDefEssential * ret;
ret->ourImages = ourImages;
return ret;
}