void CTileHandler::ProcessTiles2(void) { unsigned char* data=new unsigned char[1024*1024*4]; bigTex=CBitmap(data,1,1); /// /free big tex memory int tilex=xsize/4; int tiley=ysize/4; for(int a=0;a<(tilex*tiley)/1024;++a){ int startTile=a*1024; char name[100]; DDSURFACEDESC2 ddsheader; int ddssignature; CFileHandler file; if (prog == "nvdxt.exe " || prog == "nvcompress.exe " || prog == "texconv.exe " || prog == "old_nvdxt.exe "){ sprintf(name,"Temp%03i.dds",a); file.Open(name); file.Read(&ddssignature, sizeof(int)); file.Read(&ddsheader, sizeof(DDSURFACEDESC2)); } if (prog == "texcompress.exe "){ sprintf(name, "temp\\Temp%03i.png.raw", a); file.Open(name); } char bigtile[696320]; // /1024x1024 and 4 mipmaps file.Read(bigtile, 696320); for(int b=0;b<1024;++b){ int x=b%32; int y=b/32; int xb=(startTile+b)%tilex; int yb=(startTile+b)/tilex; char* ctile=new char[SMALL_TILE_SIZE]; ReadTile(x*32,y*32,ctile,bigtile); CBitmap* bm=new CBitmap(); bm->CreateFromDXT1((unsigned char*)ctile,32,32); int t1=tileUse[max(0,(yb-1)*tilex+xb)]; int t2=tileUse[max(0,yb*tilex+xb-1)]; int ct=FindCloseTile(bm,t1==t2?t1:-1); if(ct==-1){ tileUse[yb*tilex+xb]=usedTiles; tiles[usedTiles++]=bm; newTiles.push_back(ctile); } else { tileUse[yb*tilex+xb]=ct; delete bm; delete[] ctile; } } printf("Creating tiles %i/%i %i%%\n", usedTiles-numExternalTile,(a+1)*1024,(((a+1)*1024)*100)/(tilex*tiley)); } delete[] data; }
static std::string GetLine(CFileHandler& fh) { std::string s = ""; char a; fh.Read(&a, 1); while (a!='\n' && a!='\r') { s += a; fh.Read(&a, 1); if (fh.Eof()) break; } return s; }
string CUnit3DLoader::GetLine(CFileHandler& fh) { string s=""; char a; fh.Read(&a,1); while(a!='\xd' && a!='\xa'){ s+=a; fh.Read(&a,1); if(fh.Eof()) break; } return s; }
/// Read TileFileHeader head from file src void CSMFMapFile::ReadMapTileFileHeader(TileFileHeader& head, CFileHandler& file) { file.Read(&head.magic,sizeof(head.magic)); head.version = ReadInt(file); head.numTiles = ReadInt(file); head.tileSize = ReadInt(file); head.compressionType = ReadInt(file); }
void CTAPalette::Init(CFileHandler& paletteFile) { for (unsigned c = 0; c < NUM_PALETTE_ENTRIES; ++c) { for (unsigned c2 = 0; c2 < 4; ++c2) { paletteFile.Read(&p[c][c2], 1); } p[c][3] = 255; } }
static void GetWord(CFileHandler& fh, std::string &s) { char a = fh.Peek(); while (a == ' ' || a == '\n' || a == '\r') { fh.Read(&a, 1); a = fh.Peek(); if (fh.Eof()) break; } s = ""; fh.Read(&a, 1); while (a != ',' && a != ' ' && a != '\n' && a != '\r') { s += a; fh.Read(&a, 1); if (fh.Eof()) break; } }
string CUnit3DLoader::GetWord(CFileHandler& fh) { char a=fh.Peek(); while(a==' ' || a=='\xd' || a=='\xa'){ fh.Read(&a,1); a=fh.Peek(); if(fh.Eof()) break; } string s=""; fh.Read(&a,1); while(a!=',' && a!=' ' && a!='\xd' && a!='\xa'){ s+=a; fh.Read(&a,1); if(fh.Eof()) break; } return s; }
std::string CSpawnScript::LoadToken(CFileHandler& file) { std::string s; char c; while (!file.Eof()) { file.Read(&c,1); if(c>='0' && c<='z') break; } s += c; while (!file.Eof()) { file.Read(&c,1); if(c<'0' || c>'z') return s; s+=c; } return s; }
bool CAICallback::ReadFile (const char *name, void *buffer, int bufferLength) { CFileHandler fh (name); int fs; if (!fh.FileExists() || bufferLength < (fs = fh.FileSize())) return false; fh.Read (buffer, fs); return true; }
// returns next line (without newlines) string SimpleParser::GetLine(CFileHandler& fh) { lineNumber++; char a; string s = ""; while (!fh.Eof()) { fh.Read(&a, 1); if (a == '\n') { break; } if (a != '\r') { s += a; } } return s; }
// returns next line (without newlines) static string GetLine(CFileHandler& fh) { lineNumber++; char a; string s = ""; while (true) { a = fh.Peek(); if (a == EOF) { break; } fh.Read(&a, 1); if (a == '\n') { break; } if (a != '\r') { s += a; } } return s; }
/// Read SMFHeader head from file void CSMFMapFile::ReadMapHeader(SMFHeader& head, CFileHandler& file) { file.Read(head.magic,sizeof(head.magic)); head.version = ReadInt(file); head.mapid = ReadInt(file); head.mapx = ReadInt(file); head.mapy = ReadInt(file); head.squareSize = ReadInt(file); head.texelPerSquare = ReadInt(file); head.tilesize = ReadInt(file); head.minHeight = ReadFloat(file); head.maxHeight = ReadFloat(file); head.heightmapPtr = ReadInt(file); head.typeMapPtr = ReadInt(file); head.tilesPtr = ReadInt(file); head.minimapPtr = ReadInt(file); head.metalmapPtr = ReadInt(file); head.featurePtr = ReadInt(file); head.numExtraHeaders = ReadInt(file); }
int CArchiveDir::ReadFile(int handle, void* buffer, int numBytes) { CFileHandler* f = GetFileHandler(handle); f->Read(buffer, numBytes); return 0; }
CBFGroundTextures::CBFGroundTextures(CSmfReadMap *rm) { CFileHandler *ifs = rm->ifs; map = rm; numBigTexX=gs->mapx/128; numBigTexY=gs->mapy/128; MapHeader* header=&map->header; ifs->Seek(header->tilesPtr); tileSize = header->tilesize; MapTileHeader tileHeader; READPTR_MAPTILEHEADER(tileHeader,ifs); tileMap = SAFE_NEW int[(header->mapx*header->mapy)/16]; tiles = SAFE_NEW char[tileHeader.numTiles*SMALL_TILE_SIZE]; int curTile=0; for(int a=0;a<tileHeader.numTileFiles;++a){ PrintLoadMsg("Loading tile file"); int size; ifs->Read(&size,4); size = swabdword(size); string name; while(true){ char ch; ifs->Read(&ch,1); /* char, no swab */ if(ch==0) break; name+=ch; } name=string("maps/")+name; CFileHandler tileFile(name); if(!tileFile.FileExists()){ logOutput.Print("Couldnt find tile file %s",name.c_str()); memset(&tiles[curTile*SMALL_TILE_SIZE],0xaa,size*SMALL_TILE_SIZE); curTile+=size; continue; } PrintLoadMsg("Reading tiles"); TileFileHeader tfh; READ_TILEFILEHEADER(tfh,tileFile); if(strcmp(tfh.magic,"spring tilefile")!=0 || tfh.version!=1 || tfh.tileSize!=32 || tfh.compressionType!=1){ char t[500]; sprintf(t,"Error couldnt open tile file %s",name.c_str()); handleerror(0,t,"Error when reading tile file",0); exit(0); } for(int b=0;b<size;++b){ tileFile.Read(&tiles[curTile*SMALL_TILE_SIZE],SMALL_TILE_SIZE); curTile++; } } PrintLoadMsg("Reading tile map"); int count = (header->mapx*header->mapy)/16; ifs->Read(tileMap, count*sizeof(int)); for (int i = 0; i < count; i++) { tileMap[i] = swabdword(tileMap[i]); } tileMapXSize = header->mapx/4; tileMapYSize = header->mapy/4; squares=SAFE_NEW GroundSquare[numBigTexX*numBigTexY]; for(int y=0;y<numBigTexY;++y){ for(int x=0;x<numBigTexX;++x){ GroundSquare* square=&squares[y*numBigTexX+x]; square->texLevel=1; square->lastUsed=-100; LoadSquare(x,y,2); } } inRead=false; }
CglFont::CglFont(const std::string& fontfile, int size, int _outlinewidth, float _outlineweight): fontSize(size), fontPath(fontfile), outlineWidth(_outlinewidth), outlineWeight(_outlineweight), inBeginEnd(false) { if (size<=0) size = 14; const float invSize = 1.0f / size; const float normScale = invSize / 64.0f; FT_Library library; FT_Face face; //! initialize Freetype2 library FT_Error error = FT_Init_FreeType(&library); if (error) { string msg = "FT_Init_FreeType failed:"; msg += GetFTError(error); throw std::runtime_error(msg); } //! load font via VFS CFileHandler* f = new CFileHandler(fontPath); if (!f->FileExists()) { //! check in 'fonts/', too if (fontPath.substr(0, 6) != "fonts/") { delete f; fontPath = "fonts/" + fontPath; f = new CFileHandler(fontPath); } if (!f->FileExists()) { delete f; FT_Done_FreeType(library); throw content_error("Couldn't find font '" + fontfile + "'."); } } int filesize = f->FileSize(); FT_Byte* buf = new FT_Byte[filesize]; f->Read(buf,filesize); delete f; //! create face error = FT_New_Memory_Face(library, buf, filesize, 0, &face); if (error) { FT_Done_FreeType(library); delete[] buf; string msg = fontfile + ": FT_New_Face failed: "; msg += GetFTError(error); throw content_error(msg); } //! set render size error = FT_Set_Pixel_Sizes(face, 0, size); if (error) { FT_Done_Face(face); FT_Done_FreeType(library); delete[] buf; string msg = fontfile + ": FT_Set_Pixel_Sizes failed: "; msg += GetFTError(error); throw content_error(msg); } //! setup character range charstart = 32; charend = 254; //! char 255 = colorcode chars = (charend - charstart) + 1; //! get font information fontFamily = face->family_name; fontStyle = face->style_name; //! font's descender & height (in pixels) fontDescender = normScale * FT_MulFix(face->descender, face->size->metrics.y_scale); //lineHeight = invSize * (FT_MulFix(face->height, face->size->metrics.y_scale) / 64.0f); //lineHeight = invSize * math::ceil(FT_MulFix(face->height, face->size->metrics.y_scale) / 64.0f); lineHeight = face->height / face->units_per_EM; //lineHeight = invSize * face->size->metrics.height / 64.0f; if (lineHeight<=0.0f) { lineHeight = 1.25 * invSize * (face->bbox.yMax - face->bbox.yMin); } //! used to create the glyph textureatlas CFontTextureRenderer texRenderer(outlineWidth, outlineWeight); for (unsigned int i = charstart; i <= charend; i++) { GlyphInfo* g = &glyphs[i]; //! translate WinLatin (codepage-1252) to Unicode (used by freetype) int unicode = WinLatinToUnicode(i); //! convert to an anti-aliased bitmap error = FT_Load_Char(face, unicode, FT_LOAD_RENDER); if ( error ) { continue; } FT_GlyphSlot slot = face->glyph; //! Keep sign! const float ybearing = slot->metrics.horiBearingY * normScale; const float xbearing = slot->metrics.horiBearingX * normScale; g->advance = slot->advance.x * normScale; g->height = slot->metrics.height * normScale; g->descender = ybearing - g->height; g->x0 = xbearing; g->y0 = ybearing - fontDescender; g->x1 = (xbearing + slot->metrics.width * normScale); g->y1 = g->y0 - g->height; texRenderer.AddGlyph(i, slot->bitmap.width, slot->bitmap.rows, slot->bitmap.buffer, slot->bitmap.pitch); } //! create font atlas texture fontTexture = texRenderer.CreateTexture(); texWidth = texRenderer.texWidth; texHeight = texRenderer.texHeight; //! get glyph's uv coords in the atlas for (unsigned int i = charstart; i <= charend; i++) { texRenderer.GetGlyph(i,&glyphs[i]); } //! generate kerning tables for (unsigned int i = charstart; i <= charend; i++) { GlyphInfo& g = glyphs[i]; int unicode = WinLatinToUnicode(i); FT_UInt left_glyph = FT_Get_Char_Index(face, unicode); for (unsigned int j = 0; j <= 255; j++) { unicode = WinLatinToUnicode(j); FT_UInt right_glyph = FT_Get_Char_Index(face, unicode); FT_Vector kerning; kerning.x = kerning.y = 0.0f; FT_Get_Kerning(face, left_glyph, right_glyph, FT_KERNING_DEFAULT, &kerning); g.kerning[j] = g.advance + normScale * static_cast<float>(kerning.x); } } //! initialize null char GlyphInfo& g = glyphs[0]; g.height = g.descender = g.advance = 0.0f; for (unsigned int j = 0; j <= 255; j++) { g.kerning[j] = 0.0f; } FT_Done_Face(face); FT_Done_FreeType(library); delete[] buf; textColor = white; outlineColor = darkOutline; }
/// read an int from file (endian aware) static int ReadInt(CFileHandler& file) { unsigned int __tmpdw = 0; file.Read(&__tmpdw,sizeof(unsigned int)); return (int)swabDWord(__tmpdw); }
/// read a float from file (endian aware) static float ReadFloat(CFileHandler& file) { float __tmpfloat = 0.0f; file.Read(&__tmpfloat,sizeof(float)); return swabFloat(__tmpfloat); }
size_t VorbisStreamRead(void* ptr, size_t size, size_t nmemb, void* datasource) { CFileHandler* buffer = static_cast<CFileHandler*>(datasource); return buffer->Read(ptr, size * nmemb); }
CCobFile::CCobFile(CFileHandler &in, string name) { char *cobdata = NULL; this->name = name; //Figure out size needed and allocate it int size = in.FileSize(); // Handle errors (this is fairly fatal..) if (size < 0) { logOutput.Print("Could not find script for unit %s", name.c_str()); exit(0); } cobdata = new char[size]; //Read the entire thing, we will need it in.Read(cobdata, size); //Time to parse COBHeader ch; READ_COBHEADER(ch,cobdata); for (int i = 0; i < ch.NumberOfScripts; ++i) { int ofs; ofs = *(int *)&cobdata[ch.OffsetToScriptNameOffsetArray + i * 4]; ofs = swabdword(ofs); string s = &cobdata[ofs]; scriptNames.push_back(s); ofs = *(int *)&cobdata[ch.OffsetToScriptCodeIndexArray + i * 4]; ofs = swabdword(ofs); scriptOffsets.push_back(ofs); } //Check for zero-length scripts for (int i = 0; i < ch.NumberOfScripts - 1; ++i) { scriptLengths.push_back(scriptOffsets[i + 1] - scriptOffsets[i]); } scriptLengths.push_back(ch.TotalScriptLen - scriptOffsets[ch.NumberOfScripts - 1]); for (int i = 0; i < ch.NumberOfPieces; ++i) { int ofs; ofs = *(int *)&cobdata[ch.OffsetToPieceNameOffsetArray + i * 4]; ofs = swabdword(ofs); string s = StringToLower(&cobdata[ofs]); pieceNames.push_back(s); } int code_octets = size - ch.OffsetToScriptCode; int code_ints = (code_octets) / 4 + 4; code = new int[code_ints]; memcpy(code, &cobdata[ch.OffsetToScriptCode], code_octets); for (int i = 0; i < code_ints; i++) { code[i] = swabdword(code[i]); } numStaticVars = ch.NumberOfStaticVars; // If this is a TA:K script, read the sound names if (ch.VersionSignature == 6) { for (int i = 0; i < ch.NumberOfSounds; ++i) { int ofs; ofs = *(int *)&cobdata[ch.OffsetToSoundNameArray + i * 4]; /* TODO This probably isn't correct. */ ofs = swabdword(ofs); string s = &cobdata[ofs]; // Load the wave file and store the ID for future use s = "sounds/" + s + ".wav"; sounds.push_back(sound->GetWaveId(s)); } } delete[] cobdata; //Create a reverse mapping (name->int) for (unsigned int i = 0; i < scriptNames.size(); ++i) { scriptMap[scriptNames[i]] = i; } //Map common function names to indicies scriptIndex.resize(COBFN_Last + COB_MaxWeapons * 5); scriptIndex[COBFN_Create] = getFunctionId("Create"); scriptIndex[COBFN_StartMoving] = getFunctionId("StartMoving"); scriptIndex[COBFN_StopMoving] = getFunctionId("StopMoving"); scriptIndex[COBFN_Activate] = getFunctionId("Activate"); scriptIndex[COBFN_Killed] = getFunctionId("Killed"); scriptIndex[COBFN_Deactivate] = getFunctionId("Deactivate"); scriptIndex[COBFN_SetDirection] = getFunctionId("SetDirection"); scriptIndex[COBFN_SetSpeed] = getFunctionId("SetSpeed"); scriptIndex[COBFN_RockUnit] = getFunctionId("RockUnit"); scriptIndex[COBFN_HitByWeapon] = getFunctionId("HitByWeapon"); scriptIndex[COBFN_MoveRate0] = getFunctionId("MoveRate0"); scriptIndex[COBFN_MoveRate1] = getFunctionId("MoveRate1"); scriptIndex[COBFN_MoveRate2] = getFunctionId("MoveRate2"); scriptIndex[COBFN_MoveRate3] = getFunctionId("MoveRate3"); scriptIndex[COBFN_SetSFXOccupy] = getFunctionId("setSFXoccupy"); // Also add the weapon aiming stuff for (int i = 0; i < COB_MaxWeapons; ++i) { char buf[15]; sprintf(buf, "Weapon%d", i + 1); string weapon(buf); sprintf(buf, "%d", i + 1); string weap(buf); scriptIndex[COBFN_QueryPrimary + i] = getFunctionId("Query" + weapon); scriptIndex[COBFN_AimPrimary + i] = getFunctionId("Aim" + weapon); scriptIndex[COBFN_AimFromPrimary + i] = getFunctionId("AimFrom" + weapon); scriptIndex[COBFN_FirePrimary + i] = getFunctionId("Fire" + weapon); scriptIndex[COBFN_EndBurst + i] = getFunctionId("EndBurst" + weap); // If new-naming functions are not found, we need to support the old naming scheme if (i > 2) continue; switch (i) { case 0: weapon = "Primary"; break; case 1: weapon = "Secondary"; break; case 2: weapon = "Tertiary"; break; } if (scriptIndex[COBFN_QueryPrimary + i] == -1) scriptIndex[COBFN_QueryPrimary + i] = getFunctionId("Query" + weapon); if (scriptIndex[COBFN_AimPrimary + i] == -1) scriptIndex[COBFN_AimPrimary + i] = getFunctionId("Aim" + weapon); if (scriptIndex[COBFN_AimFromPrimary + i] == -1) scriptIndex[COBFN_AimFromPrimary + i] = getFunctionId("AimFrom" + weapon); if (scriptIndex[COBFN_FirePrimary + i] == -1) scriptIndex[COBFN_FirePrimary + i] = getFunctionId("Fire" + weapon); } }
size_t VorbisStreamRead(void* ptr, size_t size, size_t nmemb, void* datasource) { CFileHandler* buffer = (CFileHandler*)datasource; return buffer->Read(ptr, size * nmemb); }