#include "../stdafx.h" #include "CLodHandler.h" #include "../SDL_Extensions.h" #include "CDefHandler.h" #include #include #include #include #include "boost/filesystem.hpp" // includes all needed Boost.Filesystem declarations int readNormalNr (int pos, int bytCon, unsigned char * str) { int ret=0; int amp=1; if (str) { for (int i=0; i open(path.c_str(),std::ios::binary); is->seekg(0,std::ios::end); // to the end pcxs = is->tellg(); // read length is->seekg(0,std::ios::beg); // wracamy na poczatek pcx = new unsigned char[pcxs]; // allocate memory is->read((char*)pcx, pcxs); // read map file to buffer is->close(); delete is; } void CPCXConv::saveBMP(std::string path) { std::ofstream os; os.open(path.c_str(), std::ios::binary); os.write((char*)bmp,bmps); os.close(); } void CPCXConv::convert() { BMPHeader bh; BMPPalette pal[256]; Epcxformat format; int fSize,i,y; bool check1, check2; unsigned char add; int it=0; std::stringstream out; fSize = readNormalNr(it,4,pcx);it+=4; bh.x = readNormalNr(it,4,pcx);it+=4; bh.y = readNormalNr(it,4,pcx);it+=4; if (fSize==bh.x*bh.y*3) check1=true; else check1=false; if (fSize==bh.x*bh.y) check2=true; else check2=false; if (check1) format=PCX24B; else if (check2) format=PCX8B; else return; add = 4 - bh.x%4; if (add==4) add=0; bh._h3=bh.x*bh.y; if (format==PCX8B) { bh._c1=0x436; bh._c2=0x28; bh._c3=1; bh._c4=8; //bh.dataSize2=bh.dataSize1=maxx*maxy; bh.dataSize1=bh.x; bh.dataSize2=bh.y; bh.fullSize = bh.dataSize1+436; } else { bh._c1=0x36; bh._c2=0x28; bh._c3=1; bh._c4=0x18; //bh.dataSize2=bh.dataSize1=0xB12; bh.dataSize1=bh.x; bh.dataSize2=bh.y; bh.fullSize=(bh.x+add)*bh.y*3+36+18; bh._h3*=3; } if (format==PCX8B) { it = pcxs-256*3; for (int i=0;i<256;i++) { pal[i].R=pcx[it++]; pal[i].G=pcx[it++]; pal[i].B=pcx[it++]; pal[i].F='\0'; } } out<<"BM"; bh.print(out); if (format==PCX8B) { for (int i=0;i<256;i++) { out<0;y--) { it=0xC+(y-1)*bh.x; for (int j=0;j0) { for (int j=0;j0; y--) { it=0xC+(y-1)*bh.x*3; for (int j=0;j0) { for (int j=0;jformat->palette->colors+i) = tp; } for (y=bh.y;y>0;y--) { it=0xC+(y-1)*bh.x; for (int j=0;jpixels + ret->pitch * (y-1) + ret->format->BytesPerPixel * j) = pcx[it+j]; } if (add>0) { for (int j=0;jpixels + ret->pitch * (y-1) + ret->format->BytesPerPixel * (j+bh.x)) = 0; } } } } else { for (y=bh.y; y>0; y--) { it=0xC+(y-1)*bh.x*3; for (int j=0;jpixels + ret->pitch * (y-1) + j) = pcx[it+j]; } if (add>0) { for (int j=0;jpixels + ret->pitch * (y-1) + (j+bh.x*3)) = 0; } } } } return ret; } SDL_Surface * CLodHandler::loadBitmap(std::string fname) { if(!fname.size()) return NULL; unsigned char * pcx; std::transform(fname.begin(),fname.end(),fname.begin(),toupper); fname.replace(fname.find_first_of('.'),fname.find_first_of('.')+4,".PCX"); Entry *e = entries.znajdz(fname); if(!e) { std::cout<<"File "<offset<0) { fname.replace(fname.find_first_of('.'),fname.find_first_of('.')+4,".BMP"); fname = "Data/"+fname; return SDL_LoadBMP(fname.c_str()); } fseek(FLOD, e->offset, 0); if (e->size==0) //file is not compressed { pcx = new unsigned char[e->realSize]; fread((char*)pcx, 1, e->realSize, FLOD); } else { unsigned char * pcd = new unsigned char[e->size]; fread((char*)pcd, 1, e->size, FLOD); int res=infs2(pcd,e->size,e->realSize,pcx); if(res!=0) { std::cout<<"an error "<realSize); return cp.getSurface(); } int CLodHandler::decompress (unsigned char * source, int size, int realSize, std::string & dest) { std::ofstream lb; lb.open("lodbuf\\buf.gz", std::ios::out|std::ios::binary); for(int i=0; ioffset, 0); unsigned char * outp; if (ourEntry->offset<0) //file in the sprites/ folder { char * outp = new char[ourEntry->realSize]; char name[120];for(int i=0;i<120;i++) name[i]='\0'; strcat(name,"Sprites/"); strcat(name,(char*)ourEntry->name); FILE * f = fopen(name,"rb"); int result = fread(outp,1,ourEntry->realSize,f); if(result<0) {std::cout<<"Error in file reading: "<openFromMemory((unsigned char*)outp, ourEntry->realSize, std::string((char*)ourEntry->name)); nh->alphaTransformed = false; ret = nh; delete[] outp; } else if (ourEntry->size==0) //file is not compressed { outp = new unsigned char[ourEntry->realSize]; fread((char*)outp, 1, ourEntry->realSize, FLOD); CDefHandler * nh = new CDefHandler(); nh->openFromMemory(outp, ourEntry->realSize, std::string((char*)ourEntry->name)); nh->alphaTransformed = false; ret = nh; delete[] outp; } else //we will decompress file { outp = new unsigned char[ourEntry->size]; fread((char*)outp, 1, ourEntry->size, FLOD); fseek(FLOD, 0, 0); unsigned char * decomp = NULL; int decRes = infs2(outp, ourEntry->size, ourEntry->realSize, decomp); CDefHandler * nh = new CDefHandler(); nh->openFromMemory(decomp, ourEntry->realSize, std::string((char*)ourEntry->name)); nh->alphaTransformed = false; ret = nh; delete[] decomp; delete[] outp; } return ret; } CDefEssential * CLodHandler::giveDefEss(std::string defName) { CDefEssential * ret; CDefHandler * temp = giveDef(defName); ret = temp->essentialize(); delete temp; return ret; } std::vector CLodHandler::extractManyFiles(std::vector defNamesIn) { std::vector ret(defNamesIn.size()); for(int hh=0; hhoffset, 0); unsigned char * outp; if (e->size==0) //file is not compressed { outp = new unsigned char[e->realSize]; fread((char*)outp, 1, e->realSize, FLOD); CDefHandler * nh = new CDefHandler; nh->openFromMemory(outp, e->realSize, std::string((char*)e->name)); nh->alphaTransformed = false; ret[hh] = nh; } else //we will decompressing file { outp = new unsigned char[e->size]; fread((char*)outp, 1, e->size, FLOD); fseek(FLOD, 0, 0); unsigned char * decomp = NULL; int decRes = infs2(outp, e->size, e->realSize, decomp); CDefHandler * nh = new CDefHandler; nh->openFromMemory(decomp, e->realSize, std::string((char*)e->name)); nh->alphaTransformed = false; delete [] decomp; ret[hh] = nh; } delete[] outp; } return ret; } 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=amp/2) { ret = ret-amp; } return ret; } void CLodHandler::init(std::string lodFile) { std::string Ts; FLOD = fopen(lodFile.c_str(), "rb"); fseek(FLOD, 8, 0); unsigned char temp[4]; fread((char*)temp, 1, 4, FLOD); totalFiles = readNormalNr(temp,4); fseek(FLOD, 0x5c, 0); for (int i=0; ioffset<0) { char * outp = new char[e->realSize]; char name[120];for(int i=0;i<120;i++) name[i]='\0'; strcat(name,"Data/"); strcat(name,(char*)e->name); FILE * f = fopen(name,"rb"); int result = fread(outp,1,e->realSize,f); if(result<0) {std::cout<<"Error in file reading: "<realSize;itr++) ret0+= *(outp+itr); delete[] outp; return ret0; } fseek(FLOD, e->offset, 0); unsigned char * outp; if (e->size==0) //file is not compressed { outp = new unsigned char[e->realSize]; fread((char*)outp, 1, e->realSize, FLOD); std::string ret = std::string((char*)outp); delete[] outp; return ret; } else //we will decompressing file { outp = new unsigned char[e->size]; fread((char*)outp, 1, e->size, FLOD); fseek(FLOD, 0, 0); unsigned char * decomp = NULL; int decRes = infs2(outp, e->size, e->realSize, decomp); std::string ret; for (int itr=0;itrrealSize;itr++) ret+= *((char*)decomp+itr); delete[] outp; delete[] decomp; return ret; } return ret0; }