1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
bool Level::save(std::string filepath, std::string mapname, LevelData* level)
{
std::string path;
if(m_Data.m_Name != "")
{
path = filepath;
mapname = m_Data.m_Name;
mapname.append(".sld");
path.append(mapname);
}
else
{
path = filepath;
mapname.append(".sld");
path.append(mapname);
}
std::ofstream save(path.c_str(), std::ios_base::binary);
if(save.is_open())
{
// Save Render Layer
std::pair<std::multimap<LevelFlags, GameObject*>::iterator, std::multimap<LevelFlags, GameObject*>::iterator> ii;
std::multimap<LevelFlags, GameObject*>::iterator it;
ii = m_Data.m_RenderMap.equal_range(LEVEL_LAYER_0);
int layer = LEVEL_LAYER_0;
save.write((char*)&layer, sizeof(int));
int layerSize = std::distance(ii.first, ii.second);
save.write((char*)&layerSize, sizeof(int));
for(it = ii.first; it != ii.second; ++it)//REsave it
{
it->second->serialize(save);
}
ii = m_Data.m_RenderMap.equal_range(LEVEL_LAYER_1);
layer = LEVEL_LAYER_1;
save.write((char*)&layer, sizeof(int));
layerSize = std::distance(ii.first, ii.second);
save.write((char*)&layerSize, sizeof(int));
for(it = ii.first; it != ii.second; ++it)
{
it->second->serialize(save);
}
ii = m_Data.m_RenderMap.equal_range(LEVEL_LAYER_2);
layer = LEVEL_LAYER_2;
save.write((char*)&layer, sizeof(int));
layerSize = std::distance(ii.first, ii.second);
save.write((char*)&layerSize, sizeof(int));
for(it = ii.first; it != ii.second; ++it)
{
it->second->serialize(save);
}
ii = m_Data.m_RenderMap.equal_range(LEVEL_LAYER_3);
layer = LEVEL_LAYER_3;
save.write((char*)&layer, sizeof(int));
layerSize = std::distance(ii.first, ii.second);
save.write((char*)&layerSize, sizeof(int));
for(it = ii.first; it != ii.second; ++it)
{
it->second->serialize(save);
}
// Save Misc Layer
std::pair<std::multimap<LevelFlags, MiscObject*>::iterator, std::multimap<LevelFlags, MiscObject*>::iterator> iimisc;
std::multimap<LevelFlags, MiscObject*>::iterator itmisc;
iimisc = m_Data.m_MiscMap.equal_range(LEVEL_LAYER_M);
layer = LEVEL_LAYER_M;
save.write((char*)&layer, sizeof(int));
layerSize = std::distance(ii.first, ii.second);
save.write((char*)&layerSize, sizeof(int));
for(itmisc = iimisc.first; itmisc != iimisc.second; ++itmisc)
{
itmisc->second->serialize(save);
}
layer = LEVEL_LAYER_E;
save.write((char*)&layer, sizeof(int));
// Save tile spacing
save.write((char*)&m_Data.m_TileSpace, sizeof(int));
// Save files name
//writeString(save, m_Data.m_Name);
save.close();
return true;
}
return false;
}
bool Level::load(std::string filepath, std::string mapname, LevelData* level)
{
std::string path;
std::cout << "load called.\n";
if(m_Data.m_Name != "")
{
path = filepath;
mapname = m_Data.m_Name;
mapname.append(".sld");
path.append(mapname);
}
else
{
path = filepath;
mapname.append(".sld");
path.append(mapname);
}
std::ifstream load(path.c_str(), std::ios_base::binary);
if(load.is_open())
{
// Read Render Layer
int currentLayer = 0;
int currentLayerSize = 0;
GameObject gameobj;
while(currentLayer != LEVEL_LAYER_M)
{
load.read((char*)¤tLayer, sizeof(int));
load.read((char*)¤tLayerSize, sizeof(int));
if(currentLayerSize > 0)
{
for(unsigned i = 0; i < currentLayerSize; i++)
{
gameobj.unserialize(load);
insertObject(static_cast<LevelFlags>(currentLayer), new GameObject(gameobj));
}
}
}
// Read misc layer
MiscObject miscobj;
while(currentLayer != LEVEL_LAYER_E)
{
load.read((char*)¤tLayer, sizeof(int));
load.read((char*)¤tLayerSize, sizeof(int));
if(currentLayerSize > 0)
{
for(unsigned i = 0; i < currentLayerSize; i++)
{
miscobj.unserialize(load);
insertMObject(static_cast<LevelFlags>(currentLayer), &miscobj);
}
}
}
// Load tile spacing
load.read((char*)&m_Data.m_TileSpace, sizeof(int));
// Load files name
//readString(load, m_Data.l_Name);
load.close();
std::cout << "loading successful\n";
return true;
}
else
{
std::cout << "Unable to load level data\n";
return false;
}
return false;
}
|