| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | #include <stdlib.h> |
|---|
| 17 | #include <stdio.h> |
|---|
| 18 | #include <string.h> |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | #include <trpage_geom.h> |
|---|
| 28 | #include <trpage_read.h> |
|---|
| 29 | #include <trpage_compat.h> |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | class trpgShortMaterial { |
|---|
| 35 | public: |
|---|
| 36 | |
|---|
| 37 | int baseMat; |
|---|
| 38 | |
|---|
| 39 | std::vector<int> texids; |
|---|
| 40 | }; |
|---|
| 41 | |
|---|
| 42 | trpgMatTable1_0::trpgMatTable1_0(const trpgMatTable &inTbl) |
|---|
| 43 | { |
|---|
| 44 | *((trpgMatTable *)this) = inTbl; |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | bool trpgMatTable1_0::Read(trpgReadBuffer &buf) |
|---|
| 48 | { |
|---|
| 49 | trpgMaterial mat; |
|---|
| 50 | trpgToken matTok; |
|---|
| 51 | int32 len; |
|---|
| 52 | bool status; |
|---|
| 53 | unsigned int i,j,k; |
|---|
| 54 | |
|---|
| 55 | std::vector<trpgShortMaterial> shortTable; |
|---|
| 56 | std::vector<trpgMaterial> baseMats; |
|---|
| 57 | |
|---|
| 58 | try { |
|---|
| 59 | buf.Get(numTable); |
|---|
| 60 | buf.Get(numMat); |
|---|
| 61 | if (numTable <= 0 || numMat < 0) throw 1; |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | shortTable.resize(numTable*numMat); |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | buf.GetToken(matTok,len); |
|---|
| 68 | if (matTok == TRPGSHORTMATTABLE) { |
|---|
| 69 | int32 numTex,texId; |
|---|
| 70 | buf.PushLimit(len); |
|---|
| 71 | for (i=0;i<(unsigned int)numTable;i++) { |
|---|
| 72 | for (j=0;j<(unsigned int)numMat;j++) { |
|---|
| 73 | trpgShortMaterial &smat = shortTable[i*numMat+j]; |
|---|
| 74 | buf.Get(smat.baseMat); |
|---|
| 75 | buf.Get(numTex); |
|---|
| 76 | for (k=0;k<(unsigned int)numTex;k++) { |
|---|
| 77 | buf.Get(texId); |
|---|
| 78 | smat.texids.push_back(texId); |
|---|
| 79 | } |
|---|
| 80 | } |
|---|
| 81 | } |
|---|
| 82 | buf.PopLimit(); |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | int32 numBaseMat; |
|---|
| 86 | buf.Get(numBaseMat); |
|---|
| 87 | if (numBaseMat < 0) throw 1; |
|---|
| 88 | baseMats.resize(numBaseMat); |
|---|
| 89 | for (i=0;i<(unsigned int)numBaseMat;i++) { |
|---|
| 90 | buf.GetToken(matTok,len); |
|---|
| 91 | if (matTok != TRPGMATERIAL) throw 1; |
|---|
| 92 | buf.PushLimit(len); |
|---|
| 93 | mat.Reset(); |
|---|
| 94 | status = mat.Read(buf); |
|---|
| 95 | buf.PopLimit(); |
|---|
| 96 | if (!status) throw 1; |
|---|
| 97 | baseMats[i] = mat; |
|---|
| 98 | } |
|---|
| 99 | } |
|---|
| 100 | } |
|---|
| 101 | catch (...) { |
|---|
| 102 | return false; |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | for (i=0;i<shortTable.size();i++) { |
|---|
| 107 | trpgShortMaterial &shortMat = shortTable[i]; |
|---|
| 108 | trpgMaterial &baseMat = baseMats[shortMat.baseMat]; |
|---|
| 109 | AddMaterial(baseMat,false); |
|---|
| 110 | |
|---|
| 111 | trpgMaterial newMat = baseMat; |
|---|
| 112 | newMat.SetNumTexture(shortMat.texids.size()); |
|---|
| 113 | for (j=0;j<shortMat.texids.size();j++) { |
|---|
| 114 | int texId; |
|---|
| 115 | trpgTextureEnv texEnv; |
|---|
| 116 | baseMat.GetTexture(j,texId,texEnv); |
|---|
| 117 | newMat.SetTexture(j,shortMat.texids[j],texEnv); |
|---|
| 118 | } |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | valid = true; |
|---|
| 122 | return true; |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | bool trpgMatTable1_0::Write(trpgWriteBuffer &buf) |
|---|
| 126 | { |
|---|
| 127 | if (!isValid()) |
|---|
| 128 | return false; |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | std::vector<trpgShortMaterial> shortMats; |
|---|
| 132 | shortMats.resize(numTable*numMat); |
|---|
| 133 | |
|---|
| 134 | int i=0; |
|---|
| 135 | |
|---|
| 136 | MaterialMapType::iterator itr = materialMap.begin(); |
|---|
| 137 | for ( ; itr != materialMap.end( ); itr++) { |
|---|
| 138 | |
|---|
| 139 | trpgMaterial &mat = itr->second; |
|---|
| 140 | |
|---|
| 141 | trpgShortMaterial &sMat = shortMats[i]; |
|---|
| 142 | sMat.baseMat = 0; |
|---|
| 143 | int numTex; |
|---|
| 144 | mat.GetNumTexture(numTex); |
|---|
| 145 | for (int j=0;j<numTex;j++) { |
|---|
| 146 | int texId; |
|---|
| 147 | trpgTextureEnv texEnv; |
|---|
| 148 | mat.GetTexture(j,texId,texEnv); |
|---|
| 149 | sMat.texids.push_back(texId); |
|---|
| 150 | sMat.baseMat = i; |
|---|
| 151 | } |
|---|
| 152 | i++; |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | buf.Begin(TRPGMATTABLE2); |
|---|
| 157 | buf.Add(numTable); |
|---|
| 158 | buf.Add(numMat); |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | buf.Begin(TRPGSHORTMATTABLE); |
|---|
| 162 | for (i=0;i<static_cast<int>(shortMats.size());i++) { |
|---|
| 163 | trpgShortMaterial &sMat = shortMats[i]; |
|---|
| 164 | buf.Add(sMat.baseMat); |
|---|
| 165 | buf.Add((int)(sMat.texids.size())); |
|---|
| 166 | unsigned int j; |
|---|
| 167 | for (j=0;j<sMat.texids.size();j++) |
|---|
| 168 | buf.Add(sMat.texids[j]); |
|---|
| 169 | } |
|---|
| 170 | buf.End(); |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | buf.Add((int)materialMap.size()); |
|---|
| 174 | |
|---|
| 175 | itr = materialMap.begin(); |
|---|
| 176 | for ( ; itr != materialMap.end( ); itr++) { |
|---|
| 177 | |
|---|
| 178 | trpgMaterial &mat = itr->second; |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | mat.Write(buf); |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | buf.End(); |
|---|
| 187 | |
|---|
| 188 | return true; |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | trpgTexture1_0 trpgTexture1_0::operator = (const trpgTexture &inTex) |
|---|
| 192 | { |
|---|
| 193 | *((trpgTexture *)this) = inTex; |
|---|
| 194 | |
|---|
| 195 | return *this; |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | bool trpgTexture1_0::Read(trpgReadBuffer &buf) |
|---|
| 199 | { |
|---|
| 200 | mode = External; |
|---|
| 201 | |
|---|
| 202 | try { |
|---|
| 203 | char texName[1024]; |
|---|
| 204 | buf.Get(texName,1023); |
|---|
| 205 | SetName(texName); |
|---|
| 206 | buf.Get(useCount); |
|---|
| 207 | } |
|---|
| 208 | catch (...) { |
|---|
| 209 | return false; |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | return true; |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | bool trpgTexture1_0::Write(trpgWriteBuffer &buf) |
|---|
| 216 | { |
|---|
| 217 | |
|---|
| 218 | if (mode != External) |
|---|
| 219 | return false; |
|---|
| 220 | |
|---|
| 221 | |
|---|
| 222 | buf.Add(name); |
|---|
| 223 | buf.Add(useCount); |
|---|
| 224 | |
|---|
| 225 | return true; |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | trpgTexTable1_0::trpgTexTable1_0(const trpgTexTable &inTbl) |
|---|
| 229 | { |
|---|
| 230 | *((trpgTexTable *)this) = inTbl; |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | bool trpgTexTable1_0::Read(trpgReadBuffer &buf) |
|---|
| 234 | { |
|---|
| 235 | int32 numTex; |
|---|
| 236 | |
|---|
| 237 | try { |
|---|
| 238 | buf.Get(numTex); |
|---|
| 239 | |
|---|
| 240 | for (int i=0;i<numTex;i++) { |
|---|
| 241 | trpgTexture1_0 tex1_0; |
|---|
| 242 | tex1_0.Read(buf); |
|---|
| 243 | AddTexture(tex1_0); |
|---|
| 244 | |
|---|
| 245 | } |
|---|
| 246 | } |
|---|
| 247 | catch (...) { |
|---|
| 248 | return false; |
|---|
| 249 | } |
|---|
| 250 | |
|---|
| 251 | valid = true; |
|---|
| 252 | return true; |
|---|
| 253 | } |
|---|
| 254 | |
|---|
| 255 | bool trpgTexTable1_0::Write(trpgWriteBuffer &buf) |
|---|
| 256 | { |
|---|
| 257 | int32 numTex; |
|---|
| 258 | |
|---|
| 259 | if (!isValid()) |
|---|
| 260 | return false; |
|---|
| 261 | |
|---|
| 262 | buf.Begin(TRPGTEXTABLE); |
|---|
| 263 | |
|---|
| 264 | numTex = textureMap.size(); |
|---|
| 265 | buf.Add(numTex); |
|---|
| 266 | TextureMapType::const_iterator itr = textureMap.begin(); |
|---|
| 267 | for ( ; itr != textureMap.end( ); itr++) { |
|---|
| 268 | trpgTexture1_0 tex1_0; |
|---|
| 269 | tex1_0 = itr->second; |
|---|
| 270 | if (!tex1_0.Write(buf)) |
|---|
| 271 | return false; |
|---|
| 272 | } |
|---|
| 273 | buf.End(); |
|---|
| 274 | |
|---|
| 275 | return true; |
|---|
| 276 | |
|---|
| 277 | } |
|---|
| 278 | |
|---|
| 279 | trpgTileTable1_0::trpgTileTable1_0(const trpgTileTable& ) |
|---|
| 280 | { |
|---|
| 281 | |
|---|
| 282 | } |
|---|
| 283 | |
|---|
| 284 | bool trpgTileTable1_0::Write(trpgWriteBuffer &buf) |
|---|
| 285 | { |
|---|
| 286 | try { |
|---|
| 287 | buf.Begin(TRPGTILETABLE); |
|---|
| 288 | buf.Add(""); |
|---|
| 289 | buf.End(); |
|---|
| 290 | } |
|---|
| 291 | catch (...) { |
|---|
| 292 | return false; |
|---|
| 293 | } |
|---|
| 294 | |
|---|
| 295 | return true; |
|---|
| 296 | } |
|---|