- Timestamp:
- Sep 4, 2018, 2:33:52 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements/MapRendering/MapRendering/MapRendering.cs
r325 r326 104 104 Texture2D fullMapTexture = null; 105 105 106 Vector2i minChunk = default (Vector2i), maxChunk = default (Vector2i);107 Vector2i minPos = default (Vector2i), maxPos = default (Vector2i);106 Vector2i minChunk, maxChunk; 107 Vector2i minPos, maxPos; 108 108 int widthChunks, heightChunks, widthPix, heightPix; 109 109 getWorldExtent (rfm, out minChunk, out maxChunk, out minPos, out maxPos, out widthChunks, out heightChunks, … … 181 181 File.WriteAllBytes (Constants.MAP_DIRECTORY + "/map.png", array); 182 182 Object.Destroy (fullMapTexture); 183 fullMapTexture = null;184 183 } 185 184 … … 190 189 } 191 190 192 private void SaveAllBlockMaps ( object source, ElapsedEventArgs e) {191 private void SaveAllBlockMaps () { 193 192 for (int i = 0; i < Constants.ZOOMLEVELS; i++) { 194 193 zoomLevelBuffers [i].SaveBlock (); … … 215 214 msw.ResetAndRestart (); 216 215 217 if (dirtyChunks.Count > 0) { 218 List<Vector2i> keys = new List<Vector2i> (dirtyChunks.Keys); 219 List<Vector2i> chunksDone = new List<Vector2i> (); 220 221 Vector2i chunkPos = keys [0]; 222 chunksDone.Add (chunkPos); 223 224 //Log.Out ("Start Dirty: " + chunkPos); 225 226 Vector2i block = default (Vector2i), blockOffset = default (Vector2i); 227 getBlockNumber (chunkPos, out block, out blockOffset, Constants.MAP_BLOCK_TO_CHUNK_DIV, 216 if (dirtyChunks.Count <= 0) { 217 return; 218 } 219 220 List<Vector2i> keys = new List<Vector2i> (dirtyChunks.Keys); 221 List<Vector2i> chunksDone = new List<Vector2i> (); 222 223 Vector2i chunkPos = keys [0]; 224 chunksDone.Add (chunkPos); 225 226 //Log.Out ("Start Dirty: " + chunkPos); 227 228 Vector2i block, blockOffset; 229 getBlockNumber (chunkPos, out block, out blockOffset, Constants.MAP_BLOCK_TO_CHUNK_DIV, 230 Constants.MAP_CHUNK_SIZE); 231 232 zoomLevelBuffers [Constants.ZOOMLEVELS - 1].LoadBlock (block); 233 234 Vector2i v_block, v_blockOffset; 235 foreach (Vector2i v in keys) { 236 getBlockNumber (v, out v_block, out v_blockOffset, Constants.MAP_BLOCK_TO_CHUNK_DIV, 228 237 Constants.MAP_CHUNK_SIZE); 229 230 zoomLevelBuffers [Constants.ZOOMLEVELS - 1].LoadBlock (block); 231 232 Vector2i v_block = default (Vector2i), v_blockOffset = default (Vector2i); 233 foreach (Vector2i v in keys) { 234 getBlockNumber (v, out v_block, out v_blockOffset, Constants.MAP_BLOCK_TO_CHUNK_DIV, 235 Constants.MAP_CHUNK_SIZE); 236 if (v_block.Equals (block)) { 237 //Log.Out ("Dirty: " + v + " render: true"); 238 chunksDone.Add (v); 239 if (dirtyChunks [v].Length != Constants.MAP_CHUNK_SIZE * Constants.MAP_CHUNK_SIZE) { 240 Log.Error (string.Format ("Rendering chunk has incorrect data size of {0} instead of {1}", 241 dirtyChunks [v].Length, Constants.MAP_CHUNK_SIZE * Constants.MAP_CHUNK_SIZE)); 242 } 243 244 zoomLevelBuffers [Constants.ZOOMLEVELS - 1] 245 .SetPart (v_blockOffset, Constants.MAP_CHUNK_SIZE, dirtyChunks [v]); 246 } 247 } 248 249 foreach (Vector2i v in chunksDone) { 250 dirtyChunks.Remove (v); 251 } 252 253 RenderZoomLevel (Constants.ZOOMLEVELS - 1, block); 254 255 SaveAllBlockMaps (null, null); 256 } 257 } 258 259 private void RenderZoomLevel (int level, Vector2i innerBlock) { 260 if (level > 0) { 261 Vector2i block = default (Vector2i), blockOffset = default (Vector2i); 238 if (v_block.Equals (block)) { 239 //Log.Out ("Dirty: " + v + " render: true"); 240 chunksDone.Add (v); 241 if (dirtyChunks [v].Length != Constants.MAP_CHUNK_SIZE * Constants.MAP_CHUNK_SIZE) { 242 Log.Error (string.Format ("Rendering chunk has incorrect data size of {0} instead of {1}", 243 dirtyChunks [v].Length, Constants.MAP_CHUNK_SIZE * Constants.MAP_CHUNK_SIZE)); 244 } 245 246 zoomLevelBuffers [Constants.ZOOMLEVELS - 1] 247 .SetPart (v_blockOffset, Constants.MAP_CHUNK_SIZE, dirtyChunks [v]); 248 } 249 } 250 251 foreach (Vector2i v in chunksDone) { 252 dirtyChunks.Remove (v); 253 } 254 255 RenderZoomLevel (block); 256 257 SaveAllBlockMaps (); 258 } 259 260 private void RenderZoomLevel (Vector2i innerBlock) { 261 int level = Constants.ZOOMLEVELS - 1; 262 while (level > 0) { 263 Vector2i block, blockOffset; 262 264 getBlockNumber (innerBlock, out block, out blockOffset, 2, Constants.MAP_BLOCK_SIZE / 2); 263 265 264 266 zoomLevelBuffers [level - 1].LoadBlock (block); 265 zoomLevelBuffers [level - 1].SetPart (blockOffset, Constants.MAP_BLOCK_SIZE / 2, 266 zoomLevelBuffers [level].GetHalfScaled ()); 267 268 RenderZoomLevel (level - 1, block);267 zoomLevelBuffers [level - 1].SetPart (blockOffset, Constants.MAP_BLOCK_SIZE / 2, zoomLevelBuffers [level].GetHalfScaled ()); 268 269 level = level - 1; 270 innerBlock = block; 269 271 } 270 272 } … … 290 292 291 293 private bool LoadMapInfo () { 292 if (File.Exists (Constants.MAP_DIRECTORY + "/mapinfo.json")) { 293 string json = File.ReadAllText (Constants.MAP_DIRECTORY + "/mapinfo.json", Encoding.UTF8); 294 try { 295 JSONNode node = Parser.Parse (json); 296 if (node is JSONObject) { 297 JSONObject jo = (JSONObject) node; 298 if (jo.ContainsKey ("blockSize")) { 299 Constants.MAP_BLOCK_SIZE = ((JSONNumber) jo ["blockSize"]).GetInt (); 300 } 301 302 if (jo.ContainsKey ("maxZoom")) { 303 Constants.ZOOMLEVELS = ((JSONNumber) jo ["maxZoom"]).GetInt () + 1; 304 } 305 306 return true; 307 } 308 } catch (MalformedJSONException e) { 309 Log.Out ("Exception in LoadMapInfo: " + e); 310 } catch (InvalidCastException e) { 311 Log.Out ("Exception in LoadMapInfo: " + e); 312 } 294 if (!File.Exists (Constants.MAP_DIRECTORY + "/mapinfo.json")) { 295 return false; 296 } 297 298 string json = File.ReadAllText (Constants.MAP_DIRECTORY + "/mapinfo.json", Encoding.UTF8); 299 try { 300 JSONNode node = Parser.Parse (json); 301 if (node is JSONObject) { 302 JSONObject jo = (JSONObject) node; 303 if (jo.ContainsKey ("blockSize")) { 304 Constants.MAP_BLOCK_SIZE = ((JSONNumber) jo ["blockSize"]).GetInt (); 305 } 306 307 if (jo.ContainsKey ("maxZoom")) { 308 Constants.ZOOMLEVELS = ((JSONNumber) jo ["maxZoom"]).GetInt () + 1; 309 } 310 311 return true; 312 } 313 } catch (MalformedJSONException e) { 314 Log.Out ("Exception in LoadMapInfo: " + e); 315 } catch (InvalidCastException e) { 316 Log.Out ("Exception in LoadMapInfo: " + e); 313 317 } 314 318
Note:
See TracChangeset
for help on using the changeset viewer.