1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-03-25 21:38:59 +02:00

* various improvements and code cleanups

This commit is contained in:
mateuszb 2009-10-25 14:36:11 +00:00
parent 60a401384f
commit 68c5bb63a4
8 changed files with 53 additions and 235 deletions

View File

@ -22,24 +22,6 @@ int readNormalNr (int pos, int bytCon, const unsigned char * str);
extern DLL_EXPORT CLodHandler *bitmaph;
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 CPCXConv::openPCX(char * PCX, int len)
{
pcxs=len;
@ -57,11 +39,11 @@ void CPCXConv::fromFile(std::string path)
is.close();
}
void CPCXConv::saveBMP(std::string path)
void CPCXConv::saveBMP(std::string path) const
{
std::ofstream os;
os.open(path.c_str(), std::ios::binary);
os.write((char*)bmp,bmps);
os.write(reinterpret_cast<const char*>(bmp), bmps);
os.close();
}
@ -131,19 +113,14 @@ SDL_Surface * CPCXConv::getSurface() const
tp.unused = 0;
*(ret->format->palette->colors+i) = tp;
}
for (y=height;y>0;y--)
for (y=height; y>0; --y)
{
it=0xC+(y-1)*width;
for (int j=0;j<width;j++)
{
*((char*)ret->pixels + ret->pitch * (y-1) + ret->format->BytesPerPixel * j) = pcx[it+j];
}
it = 0xC + (y-1)*width;
memcpy((char*)ret->pixels + ret->pitch * (y-1), pcx + it, width);
if (add>0)
{
for (int j=0;j<add;j++)
{
*((char*)ret->pixels + ret->pitch * (y-1) + ret->format->BytesPerPixel * (j+width)) = 0;
}
memset((char*)ret->pixels + ret->pitch * (y-1) + width, 0, add);
}
}
}
@ -151,17 +128,13 @@ SDL_Surface * CPCXConv::getSurface() const
{
for (y=height; y>0; y--)
{
it=0xC+(y-1)*width*3;
for (int j=0;j<width*3;j++)
{
*((char*)ret->pixels + ret->pitch * (y-1) + j) = pcx[it+j];
}
it = 0xC + (y-1)*width*3;
memcpy((char*)ret->pixels + ret->pitch * (y-1), pcx + it, width*3);
if (add>0)
{
for (int j=0;j<add*3;j++)
{
*((char*)ret->pixels + ret->pitch * (y-1) + (j+width*3)) = 0;
}
memset((char*)ret->pixels + ret->pitch * (y-1) + width*3, 0, add*3);
}
}
}

View File

@ -24,27 +24,13 @@ struct BMPPalette
unsigned char R,G,B,F;
};
struct BMPHeader
{
int fullSize, _h1, _h2, _h3, _c1, _c2, _c3, _c4, x, y,
dataSize1, dataSize2; //DataSize=X*Y+2*Y
unsigned char _c5[8];
void print(std::ostream & out);
BMPHeader()
{
_h1=_h2=0;
for(int i=0;i<8;i++)
_c5[i]=0;
}
};
class CPCXConv
{
public:
unsigned char * pcx, *bmp;
int pcxs, bmps;
void fromFile(std::string path);
void saveBMP(std::string path);
void saveBMP(std::string path) const;
void openPCX(char * PCX, int len);
SDL_Surface * getSurface() const; //for standard H3 PCX
//SDL_Surface * getSurfaceZ(); //for ZSoft PCX

View File

@ -155,4 +155,13 @@ void CCursorHandler::shiftPos( int &x, int &y )
y -= 20;
}
}
}
}
CCursorHandler::~CCursorHandler()
{
if(help)
SDL_FreeSurface(help);
for(int g=0; g<cursors.size(); ++g)
delete cursors[g];
}

View File

@ -25,7 +25,7 @@ public:
std::vector<CDefHandler*> cursors;
int xpos, ypos; //position of cursor
void initCursor(); //inits cursorHandler
void initCursor(); //inits cursorHandler - run only once, it's not memleak-proof (rev 1333)
void cursorMove(const int & x, const int & y); //change cursor's positions to (x, y)
void changeGraphic(const int & type, const int & no); //changes cursor graphic for type type (0 - adventure, 1 - combat, 2 - default, 3 - spellbook) and frame no (not used for type 3)
void draw1();
@ -34,6 +34,7 @@ public:
void draw2();
void hide(){Show=0;};
void show(){Show=1;};
~CCursorHandler();
};

View File

@ -77,7 +77,7 @@ void CDefHandler::openFromMemory(unsigned char *table, std::string name)
DEFType = SDL_SwapLE32(de.DEFType);
width = SDL_SwapLE32(de.width);
height = SDL_SwapLE32(de.height);
totalBlocks = SDL_SwapLE32(de.totalBlocks);
unsigned int totalBlocks = SDL_SwapLE32(de.totalBlocks);
for (unsigned int it=0;it<256;it++)
{
@ -91,7 +91,7 @@ void CDefHandler::openFromMemory(unsigned char *table, std::string name)
p = reinterpret_cast<unsigned char *>(&de);
p += sizeof(de);
totalEntries=0;
int totalEntries=0;
for (unsigned int z=0; z<totalBlocks; z++)
{
SDefEntryBlock &block = * reinterpret_cast<SDefEntryBlock *>(p);
@ -139,27 +139,6 @@ void CDefHandler::openFromMemory(unsigned char *table, std::string name)
}
}
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;
}
long long amp = pow((long long int)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;
@ -191,15 +170,8 @@ int CDefHandler::readNormalNr (int pos, int bytCon, const unsigned char * str, b
}
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);
}
SDL_Surface * CDefHandler::getSprite (int SIndex, unsigned char * FDef, BMPPalette * palette)
SDL_Surface * CDefHandler::getSprite (int SIndex, const unsigned char * FDef, const BMPPalette * palette) const
{
SDL_Surface * ret=NULL;
@ -216,7 +188,7 @@ SDL_Surface * CDefHandler::getSprite (int SIndex, unsigned char * FDef, BMPPalet
unsigned char SegmentType;//, BL, BR; //TODO use me
BaseOffset = SEntries[SIndex].offset;
SSpriteDef sd = * reinterpret_cast<SSpriteDef *>(FDef + BaseOffset);
SSpriteDef sd = * reinterpret_cast<const SSpriteDef *>(FDef + BaseOffset);
prSize = SDL_SwapLE32(sd.prSize); //TODO use me
defType2 = SDL_SwapLE32(sd.defType2);
@ -261,7 +233,7 @@ SDL_Surface * CDefHandler::getSprite (int SIndex, unsigned char * FDef, BMPPalet
// 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);
memset( reinterpret_cast<char*>(ret->pixels), 0, FullHeight*FullWidth);
}
// Skip top margin
@ -277,7 +249,7 @@ SDL_Surface * CDefHandler::getSprite (int SIndex, unsigned char * FDef, BMPPalet
if (LeftMargin>0)
ftcp += LeftMargin;
memcpy((char*)(ret->pixels)+ftcp, &FDef[BaseOffset], SpriteWidth);
memcpy(reinterpret_cast<char*>(ret->pixels)+ftcp, &FDef[BaseOffset], SpriteWidth);
ftcp += SpriteWidth;
BaseOffset += SpriteWidth;
@ -289,7 +261,7 @@ SDL_Surface * CDefHandler::getSprite (int SIndex, unsigned char * FDef, BMPPalet
case 1:
{
unsigned int * RWEntriesLoc = reinterpret_cast<unsigned int *>(FDef+BaseOffset);
const unsigned int * RWEntriesLoc = reinterpret_cast<const unsigned int *>(FDef+BaseOffset);
BaseOffset += sizeof(int) * SpriteHeight;
for (unsigned int i=0;i<SpriteHeight;i++)
{
@ -309,7 +281,7 @@ SDL_Surface * CDefHandler::getSprite (int SIndex, unsigned char * FDef, BMPPalet
{
for (unsigned int k=0; k<SegmentLength;k++)
{
((char*)(ret->pixels))[ftcp++]=FDef[BaseOffset+k];
(reinterpret_cast<char*>(ret->pixels))[ftcp++]=FDef[BaseOffset+k];
if ((TotalRowLength+k)>=SpriteWidth)
break;
}
@ -318,7 +290,7 @@ SDL_Surface * CDefHandler::getSprite (int SIndex, unsigned char * FDef, BMPPalet
}
else
{
memset((char*)(ret->pixels)+ftcp, SegmentType, SegmentLength);
memset(reinterpret_cast<char*>(ret->pixels)+ftcp, SegmentType, SegmentLength);
ftcp += SegmentLength;
TotalRowLength += SegmentLength;
}
@ -337,11 +309,7 @@ SDL_Surface * CDefHandler::getSprite (int SIndex, unsigned char * FDef, BMPPalet
case 2:
{
/*for (int i=0;i<SpriteHeight;i++)
{
RWEntries[i] = readNormalNr(BaseOffsetor+i*2*(SpriteWidth/32), 2, FDef);
}*/
BaseOffset = BaseOffsetor + *(unsigned short*)( FDef + BaseOffsetor ); //was + RWEntries[0];
BaseOffset = BaseOffsetor + *reinterpret_cast<const unsigned short*>( FDef + BaseOffsetor ); //was + RWEntries[0];
for (unsigned int i=0;i<SpriteHeight;i++)
{
//BaseOffset = BaseOffsetor+RWEntries[i];
@ -357,13 +325,13 @@ SDL_Surface * CDefHandler::getSprite (int SIndex, unsigned char * FDef, BMPPalet
unsigned char value = (SegmentType & 31) + 1;
if(code==7)
{
memcpy((char*)(ret->pixels)+ftcp, &FDef[BaseOffset], value);
memcpy(reinterpret_cast<char*>(ret->pixels)+ftcp, &FDef[BaseOffset], value);
ftcp += value;
BaseOffset += value;
}
else
{
memset((char*)(ret->pixels)+ftcp, code, value);
memset(reinterpret_cast<char*>(ret->pixels)+ftcp, code, value);
ftcp += value;
}
TotalRowLength+=value;
@ -382,10 +350,6 @@ SDL_Surface * CDefHandler::getSprite (int SIndex, unsigned char * FDef, BMPPalet
case 3:
{
/*for (int i=0;i<SpriteHeight;i++)
{
RWEntries[i] = readNormalNr(BaseOffsetor+i*2*(SpriteWidth/32), 2, FDef);
}*/
for (unsigned int i=0;i<SpriteHeight;i++)
{
BaseOffset = BaseOffsetor + *(unsigned short*)( FDef + BaseOffsetor+i*2*(SpriteWidth/32) ); //was + RWEntries[i] before speedup

View File

@ -68,8 +68,7 @@ struct SSpriteDef {
class CDefHandler
{
private:
unsigned int totalEntries, DEFType, totalBlocks;
bool allowRepaint;
unsigned int DEFType;
int length;
//unsigned int * RWEntries;
struct SEntry
@ -82,19 +81,17 @@ private:
public:
int width, height; //width and height
std::string defName, curDir;
std::string defName;
std::vector<Cimage> ourImages;
bool alphaTransformed;
bool notFreeImgs;
CDefHandler(); //c-tor
~CDefHandler(); //d-tor
static void print (std::ostream & stream, int nr, int bytcon);
static int readNormalNr (int pos, int bytCon, const unsigned char * str=NULL, bool cyclic=false);
static unsigned char *writeNormalNr (int nr, int bytCon);
SDL_Surface * getSprite (int SIndex, unsigned char * FDef, BMPPalette * palette); //zapisuje klatke o zadanym numerze do "testtt.bmp"
SDL_Surface * getSprite (int SIndex, const unsigned char * FDef, const BMPPalette * palette) const; //zapisuje klatke o zadanym numerze do "testtt.bmp"
void openDef(std::string name);
void expand(unsigned char N,unsigned char & BL, unsigned char & BR);
static void expand(unsigned char N,unsigned char & BL, unsigned char & BR);
void openFromMemory(unsigned char * table, std::string name);
CDefEssential * essentialize();

View File

@ -98,82 +98,6 @@ unsigned char * CLodHandler::giveFile(std::string defName, int * length)
return NULL;
}
int CLodHandler::infs(unsigned char * in, int size, int realSize, std::ofstream & out, int wBits)
{
int ret;
unsigned have;
z_stream strm;
unsigned char inx[NLoadHandlerHelp::fCHUNK];
unsigned char outx[NLoadHandlerHelp::fCHUNK];
/* allocate inflate state */
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.avail_in = 0;
strm.next_in = Z_NULL;
ret = inflateInit2(&strm, wBits);
if (ret != Z_OK)
return ret;
int chunkNumber = 0;
do
{
int readBytes = 0;
for(int i=0; i<NLoadHandlerHelp::fCHUNK && (chunkNumber * NLoadHandlerHelp::fCHUNK + i)<size; ++i)
{
inx[i] = in[chunkNumber * NLoadHandlerHelp::fCHUNK + i];
++readBytes;
}
++chunkNumber;
strm.avail_in = readBytes;
//strm.avail_in = fread(inx, 1, NLoadHandlerHelp::fCHUNK, source);
/*if (in.bad())
{
(void)inflateEnd(&strm);
return Z_ERRNO;
}*/
if (strm.avail_in == 0)
break;
strm.next_in = inx;
/* run inflate() on input until output buffer not full */
do
{
strm.avail_out = NLoadHandlerHelp::fCHUNK;
strm.next_out = outx;
ret = inflate(&strm, Z_NO_FLUSH);
//assert(ret != Z_STREAM_ERROR); /* state not clobbered */
switch (ret)
{
case Z_NEED_DICT:
ret = Z_DATA_ERROR; /* and fall through */
case Z_DATA_ERROR:
case Z_MEM_ERROR:
(void)inflateEnd(&strm);
return ret;
}
have = NLoadHandlerHelp::fCHUNK - strm.avail_out;
/*if (fwrite(out, 1, have, dest) != have || ferror(dest))
{
(void)inflateEnd(&strm);
return Z_ERRNO;
}*/
out.write((char*)outx, have);
if(out.bad())
{
(void)inflateEnd(&strm);
return Z_ERRNO;
}
} while (strm.avail_out == 0);
/* done when inflate() says it's done */
} while (ret != Z_STREAM_END);
/* clean up and return */
(void)inflateEnd(&strm);
return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
}
DLL_EXPORT int CLodHandler::infs2(unsigned char * in, int size, int realSize, unsigned char *& out, int wBits)
{
int ret;
@ -240,50 +164,18 @@ DLL_EXPORT int CLodHandler::infs2(unsigned char * in, int size, int realSize, un
void CLodHandler::extractFile(std::string FName, std::string name)
{
std::transform(name.begin(), name.end(), name.begin(), (int(*)(int))toupper);
for (int i=0;i<totalFiles;i++)
int len; //length of file to write
unsigned char * outp = giveFile(name, &len);
std::ofstream out;
out.open(FName.c_str(), std::ios::binary);
if(!out.is_open())
{
std::string buf1 = entries[i].nameStr;
std::transform(buf1.begin(), buf1.end(), buf1.begin(), (int(*)(int))toupper);
if(buf1!=name)
continue;
LOD.seekg(entries[i].offset, std::ios::beg);
std::string bufff = (FName);
unsigned char * outp;
if (entries[i].size==0) //file is not compressed
{
outp = new unsigned char[entries[i].realSize];
LOD.read((char*)outp, entries[i].realSize);
std::ofstream out;
out.open(bufff.c_str(), std::ios::binary);
if(!out.is_open())
{
tlog1<<"Unable to create "<<bufff;
}
else
{
for(int hh=0; hh<entries[i].realSize; ++hh)
{
out<<*(outp+hh);
}
out.close();
}
}
else //we will decompressing file
{
outp = new unsigned char[entries[i].size];
LOD.read((char*)outp, entries[i].size);
std::ofstream destin;
destin.open(bufff.c_str(), std::ios::binary);
//int decRes = decompress(outp, entries[i].size, entries[i].realSize, bufff);
int decRes = infs(outp, entries[i].size, entries[i].realSize, destin);
destin.close();
if(decRes!=0)
{
tlog1<<"LOD Extraction error"<<" "<<decRes<<" while extracting to "<<bufff<<std::endl;
}
}
delete[] outp;
tlog1<<"Unable to create "<<FName<<std::endl;
}
else
{
out.write(reinterpret_cast<char*>(outp), len);
out.close();
}
}
@ -368,10 +260,7 @@ std::string CLodHandler::getTextFile(std::string name)
exit(1);
}
std::string ret;
ret.reserve(length);
for(int i=0;i<length;i++)
ret+=data[i];
std::string ret(data, data+length);
delete [] data;
return ret;
}

View File

@ -70,7 +70,6 @@ public:
CLodHandler();
~CLodHandler();
int infs(unsigned char * in, int size, int realSize, std::ofstream & out, int wBits=15); //zlib fast handler
int infs2(unsigned char * in, int size, int realSize, unsigned char*& out, int wBits=15); //zlib fast handler
unsigned char * giveFile(std::string defName, int * length=NULL); //returns pointer to the decompressed data - it must be deleted when no longer needed!
std::string getTextFile(std::string name); //extracts one file