mirror of
https://github.com/vcmi/vcmi.git
synced 2025-08-13 19:54:17 +02:00
Load a map by block instead of byte per byte.
This commit is contained in:
48
lib/map.cpp
48
lib/map.cpp
@@ -539,25 +539,55 @@ void Mapa::addBlockVisTiles(CGObjectInstance * obj)
|
|||||||
}
|
}
|
||||||
Mapa::Mapa(std::string filename)
|
Mapa::Mapa(std::string filename)
|
||||||
{
|
{
|
||||||
|
const int bufsize = 65536;
|
||||||
|
int mapsize = 0;
|
||||||
|
|
||||||
tlog0<<"Opening map file: "<<filename<<"\t "<<std::flush;
|
tlog0<<"Opening map file: "<<filename<<"\t "<<std::flush;
|
||||||
gzFile map = gzopen(filename.c_str(),"rb");
|
gzFile map = gzopen(filename.c_str(),"rb");
|
||||||
std::vector<unsigned char> mapstr; int pom;
|
std::vector<unsigned char *> mapstr;
|
||||||
while((pom=gzgetc(map))>=0)
|
|
||||||
{
|
// Read a map by chunks
|
||||||
mapstr.push_back(pom);
|
// We could try to read the map size directly (cf RFC 1952) and then read
|
||||||
|
// directly the whole map, but that would create more problems.
|
||||||
|
do {
|
||||||
|
unsigned char *buf = new unsigned char[bufsize];
|
||||||
|
|
||||||
|
int ret = gzread(map, buf, bufsize);
|
||||||
|
if (ret == 0 || ret == -1) {
|
||||||
|
delete [] buf;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mapstr.push_back(buf);
|
||||||
|
mapsize += ret;
|
||||||
|
} while(1);
|
||||||
|
|
||||||
gzclose(map);
|
gzclose(map);
|
||||||
unsigned char *initTable = new unsigned char[mapstr.size()];
|
|
||||||
for(int ss=0; ss<mapstr.size(); ++ss)
|
// Now that we know the uncompressed size, reassemble the chunks
|
||||||
{
|
unsigned char *initTable = new unsigned char[mapsize];
|
||||||
initTable[ss] = mapstr[ss];
|
|
||||||
|
std::vector<unsigned char *>::iterator it;
|
||||||
|
int offset;
|
||||||
|
int tocopy = mapsize;
|
||||||
|
for (it = mapstr.begin(), offset = 0;
|
||||||
|
it != mapstr.end();
|
||||||
|
it++, offset+=bufsize ) {
|
||||||
|
memcpy(&initTable[offset], *it, tocopy > bufsize ? bufsize : tocopy);
|
||||||
|
tocopy -= bufsize;
|
||||||
|
delete [] *it;
|
||||||
}
|
}
|
||||||
|
|
||||||
tlog0<<"done."<<std::endl;
|
tlog0<<"done."<<std::endl;
|
||||||
|
|
||||||
|
// Compute checksum
|
||||||
boost::crc_32_type result;
|
boost::crc_32_type result;
|
||||||
result.process_bytes(initTable,mapstr.size());
|
result.process_bytes(initTable,mapsize);
|
||||||
checksum = result.checksum();
|
checksum = result.checksum();
|
||||||
tlog0 << "\tOur map checksum: "<<result.checksum() << std::endl;
|
tlog0 << "\tOur map checksum: "<<result.checksum() << std::endl;
|
||||||
|
|
||||||
initFromBytes(initTable);
|
initFromBytes(initTable);
|
||||||
|
|
||||||
delete [] initTable;
|
delete [] initTable;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user