finished skip tile based map loading

This commit is contained in:
oy 2017-12-11 09:50:15 +01:00
parent 787035a27a
commit 2503dfdaec
7 changed files with 73 additions and 2 deletions

View file

@ -305,6 +305,15 @@ void *CDataFileReader::GetDataSwapped(int Index)
return GetDataImpl(Index, 1);
}
void CDataFileReader::ReplaceData(int Index, char *pData)
{
// make sure the data has been loaded
GetDataImpl(Index, 0);
UnloadData(Index);
m_pDataFile->m_ppDataPtrs[Index] = pData;
}
void CDataFileReader::UnloadData(int Index)
{
if(Index < 0)

View file

@ -20,6 +20,7 @@ public:
void *GetData(int Index);
void *GetDataSwapped(int Index); // makes sure that the data is 32bit LE ints when saved
int GetDataSize(int Index) const;
void ReplaceData(int Index, char *pData);
void UnloadData(int Index);
void *GetItem(int Index, int *pType, int *pID);
int GetItemSize(int Index) const;

View file

@ -37,6 +37,46 @@ public:
CMapItemVersion *pItem = (CMapItemVersion *)m_DataFile.FindItem(MAPITEMTYPE_VERSION, 0);
if(!pItem || pItem->m_Version != CMapItemVersion::CURRENT_VERSION)
return false;
// replace compressed tile layers with uncompressed ones
int GroupsStart, GroupsNum, LayersStart, LayersNum;
m_DataFile.GetType(MAPITEMTYPE_GROUP, &GroupsStart, &GroupsNum);
m_DataFile.GetType(MAPITEMTYPE_LAYER, &LayersStart, &LayersNum);
for(int g = 0; g < GroupsNum; g++)
{
CMapItemGroup *pGroup = static_cast<CMapItemGroup *>(m_DataFile.GetItem(GroupsStart + g, 0, 0));
for(int l = 0; l < pGroup->m_NumLayers; l++)
{
CMapItemLayer *pLayer = static_cast<CMapItemLayer *>(m_DataFile.GetItem(LayersStart + pGroup->m_StartLayer + l, 0, 0));
if(pLayer->m_Type == LAYERTYPE_TILES)
{
CMapItemLayerTilemap *pTilemap = reinterpret_cast<CMapItemLayerTilemap *>(pLayer);
if(pTilemap->m_Version > 3)
{
CTile *pTiles = static_cast<CTile *>(mem_alloc(pTilemap->m_Width * pTilemap->m_Height * sizeof(CTile), 1));
// extract original tile data
int i = 0;
CTile *pSavedTiles = static_cast<CTile *>(m_DataFile.GetData(pTilemap->m_Data));
while(i < pTilemap->m_Width * pTilemap->m_Height)
{
for(unsigned Counter = 0; Counter <= pSavedTiles->m_Skip && i < pTilemap->m_Width * pTilemap->m_Height; Counter++)
{
pTiles[i] = *pSavedTiles;
pTiles[i++].m_Skip = 0;
}
pSavedTiles++;
}
m_DataFile.ReplaceData(pTilemap->m_Data, reinterpret_cast<char *>(pTiles));
}
}
}
}
return true;
}

View file

@ -447,6 +447,7 @@ public:
virtual void ModifyEnvelopeIndex(INDEX_MODIFY_FUNC pfnFunc);
void PrepareForSave();
void ExtractTiles(CTile *pSavedTiles);
void GetSize(float *w, float *h) const { *w = m_Width*32.0f; *h = m_Height*32.0f; }

View file

@ -395,7 +395,12 @@ int CEditorMap::Load(class IStorage *pStorage, const char *pFileName, int Storag
if(pTilemapItem->m_Version >= 3)
IntsToStr(pTilemapItem->m_aName, sizeof(pTiles->m_aName)/sizeof(int), pTiles->m_aName);
mem_copy(pTiles->m_pTiles, pData, pTiles->m_Width*pTiles->m_Height*sizeof(CTile));
// get tile data
if(pTilemapItem->m_Version > 3)
pTiles->ExtractTiles((CTile *)pData);
else
mem_copy(pTiles->m_pTiles, pData, pTiles->m_Width*pTiles->m_Height*sizeof(CTile));
if(pTiles->m_Game && pTilemapItem->m_Version == MakeVersion(1, *pTilemapItem))
{

View file

@ -129,6 +129,21 @@ void CLayerTiles::PrepareForSave()
}
}
void CLayerTiles::ExtractTiles(CTile *pSavedTiles)
{
int i = 0;
while(i < m_Width * m_Height)
{
for(unsigned Counter = 0; Counter <= pSavedTiles->m_Skip && i < m_Width * m_Height; Counter++)
{
m_pTiles[i] = *pSavedTiles;
m_pTiles[i++].m_Skip = 0;
}
pSavedTiles++;
}
}
void CLayerTiles::MakePalette()
{
for(int y = 0; y < m_Height; y++)

View file

@ -152,7 +152,7 @@ struct CMapItemLayer
struct CMapItemLayerTilemap
{
enum { CURRENT_VERSION=3 };
enum { CURRENT_VERSION=4 };
CMapItemLayer m_Layer;
int m_Version;