#include "../stdafx.h" #include "SDL.h" #include "CDefHandler.h" #include #include "CLodHandler.h" #include "../lib/VCMI_Lib.h" /* * 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 * */ long long pow(long long a, int b) { 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; for (size_t i=0; i 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; width = readNormalNr(i,4,FDef); i+=4; height = readNormalNr(i,4,FDef); i+=4; 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=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=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;iformat->palette->colors+i))=pr; } if (defType2==0) { if (TopMargin>0) { for (int i=0;ipixels))[ftcp++]='\0'; } } for (int i=0;i0) { for (int j=0;jpixels))[ftcp++]='\0'; } for (int j=0; jpixels))[ftcp++]=FDef[BaseOffset++]; if (RightMargin>0) { for (int j=0;jpixels))[ftcp++]='\0'; } } if (BottomMargin>0) { for (int i=0;ipixels))[ftcp++]='\0'; } } } if (defType2==1) { if (TopMargin>0) { for (int i=0;ipixels))[ftcp++]='\0'; } } memcpy(RWEntries, FDef+BaseOffset, SpriteHeight*sizeof(int)); BaseOffset += sizeof(int) * SpriteHeight; for (int i=0;i0) { for (int j=0;jpixels))[ftcp++]='\0'; } TotalRowLength=0; do { SegmentType=FDef[BaseOffset++]; SegmentLength=FDef[BaseOffset++]; if (SegmentType==0xFF) { for (int k=0;k<=SegmentLength;k++) { ((char*)(ret->pixels))[ftcp++]=FDef[BaseOffset+k]; if ((TotalRowLength+k+1)>=SpriteWidth) break; } BaseOffset+=SegmentLength+1;//// TotalRowLength+=SegmentLength+1; } else { for (int k=0;kpixels))[ftcp++]=SegmentType;// //((char*)(ret->pixels))+='\0'; } TotalRowLength+=SegmentLength+1; } }while(TotalRowLength0) { for (int j=0;jpixels))[ftcp++]='\0'; } if (add>0) { for (int j=0;jpixels))[ftcp++]='\0'; } } if (BottomMargin>0) { for (int i=0;ipixels))[ftcp++]='\0'; } } } if (defType2==2) { if (TopMargin>0) { for (int i=0;ipixels))[ftcp++]='\0'; } } for (int i=0;i0) { for (int j=0;jpixels))[ftcp++]='\0'; } TotalRowLength=0; do { SegmentType=FDef[BaseOffset++]; unsigned char code = SegmentType / 32; unsigned char value = (SegmentType & 31) + 1; if(code==7) { for(int h=0; hpixels))[ftcp++]=FDef[BaseOffset++]; } } else { for(int h=0; hpixels))[ftcp++]=code; } } TotalRowLength+=value; } while(TotalRowLength0) { for (int j=0;jpixels))[ftcp++]='\0'; } if (add>0) { for (int j=0;jpixels))[ftcp++]='\0'; } } if (BottomMargin>0) { for (int i=0;ipixels))[ftcp++]='\0'; } } } if (defType2==3) { if (add==4) add=0; if (TopMargin>0) { for (int i=0;ipixels))[ftcp++]='\0'; } } for (int i=0;i0) { for (int j=0;jpixels))[ftcp++]='\0'; } TotalRowLength=0; do { SegmentType=FDef[BaseOffset++]; unsigned char code = SegmentType / 32; unsigned char value = (SegmentType & 31) + 1; if(code==7) { for(int h=0; h=SpriteWidth) break; ((char*)(ret->pixels))[ftcp++]=FDef[BaseOffset++]; } } else { for(int h=0; h=SpriteWidth) break; ((char*)(ret->pixels))[ftcp++]=code; } } TotalRowLength+=( LeftMargin>=0 ? value : value+LeftMargin ); }while(TotalRowLength0) { for (int j=0;jpixels))[ftcp++]='\0'; } if (add>0) { for (int j=0;jpixels))[ftcp++]='\0'; } } if (BottomMargin>0) { for (int i=0;ipixels))[ftcp++]='\0'; } } } 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; } CDefHandler * CDefHandler::giveDef(std::string defName) { unsigned char * data = spriteh->giveFile(defName); if(!data) throw "bad def name!"; CDefHandler * nh = new CDefHandler(); nh->openFromMemory(data, defName); nh->alphaTransformed = false; delete [] data; return nh; } CDefEssential * CDefHandler::giveDefEss(std::string defName) { CDefEssential * ret; CDefHandler * temp = giveDef(defName); ret = temp->essentialize(); delete temp; return ret; }