source: binary-improvements/7dtd-server-fixes/src/MapRendering/MapRenderBlockBuffer.cs@ 218

Last change on this file since 218 was 199, checked in by alloc, 10 years ago

fixes

File size: 2.6 KB
Line 
1using System;
2using System.IO;
3using System.Threading;
4using UnityEngine;
5
6namespace AllocsFixes.MapRendering
7{
8 public class MapRenderBlockBuffer
9 {
10 private int zoomLevel;
11 private string currentBlockMap = string.Empty;
12 private Texture2D blockMap = new Texture2D (Constants.MAP_BLOCK_SIZE, Constants.MAP_BLOCK_SIZE);
13 private Texture2D zoomBuffer = new Texture2D (1, 1);
14 private Color nullColor = new Color (0, 0, 0, 0);
15 private AllocsFixes.FileCache.MapTileCache cache;
16
17 public MapRenderBlockBuffer (int level, AllocsFixes.FileCache.MapTileCache cache)
18 {
19 zoomLevel = level;
20 this.cache = cache;
21 }
22
23 public void ResetBlock ()
24 {
25 currentBlockMap = string.Empty;
26 }
27
28 public void SaveBlock ()
29 {
30 try {
31 if (currentBlockMap.Length > 0)
32 saveTextureToFile (currentBlockMap);
33 } catch (Exception e) {
34 Log.Out ("Exception in MapRenderBlockBuffer.SaveBlock(): " + e);
35 }
36 }
37
38 public bool LoadBlock (Vector2i block)
39 {
40 bool res = false;
41 lock (blockMap) {
42 string folder = Constants.MAP_DIRECTORY + "/" + (zoomLevel) + "/" + block.x;
43 string fileName = folder + "/" + block.y + ".png";
44 Directory.CreateDirectory (folder);
45 if (!fileName.Equals (currentBlockMap)) {
46 res = true;
47 SaveBlock ();
48 loadTextureFromFile (fileName);
49 }
50 currentBlockMap = fileName;
51 }
52 return res;
53 }
54
55 public void SetPart (Vector2i offset, int partSize, Color[] pixels)
56 {
57 blockMap.SetPixels (offset.x, offset.y, partSize, partSize, pixels);
58 }
59
60 public Color[] GetHalfScaled ()
61 {
62 zoomBuffer.Resize (Constants.MAP_BLOCK_SIZE, Constants.MAP_BLOCK_SIZE);
63 zoomBuffer.SetPixels (blockMap.GetPixels ());
64
65 TextureScale.Point (zoomBuffer, Constants.MAP_BLOCK_SIZE / 2, Constants.MAP_BLOCK_SIZE / 2);
66
67 return zoomBuffer.GetPixels ();
68 }
69
70 private void loadTextureFromFile (string _fileName)
71 {
72 byte[] array = cache.LoadTile (zoomLevel, _fileName);
73 if (array != null) {
74 blockMap.LoadImage (array);
75 } else {
76 //try {
77 //byte[] array = File.ReadAllBytes (_fileName);
78 //blockMap.LoadImage (array);
79 //} catch (Exception) {
80 for (int x = 0; x < Constants.MAP_BLOCK_SIZE; x++) {
81 for (int y = 0; y < Constants.MAP_BLOCK_SIZE; y++) {
82 blockMap.SetPixel (x, y, nullColor);
83 }
84 }
85 }
86 }
87
88 private void saveTextureToFile (string _fileName)
89 {
90 byte[] array = blockMap.EncodeToPNG ();
91 cache.SaveTile (zoomLevel, array);
92// try {
93// byte[] array = blockMap.EncodeToPNG ();
94// File.WriteAllBytes (_fileName, array);
95// } catch (Exception e) {
96// Log.Out ("Exception in MapRenderBlockBuffer.saveTextureToFile(): " + e);
97// }
98 }
99
100 }
101}
Note: See TracBrowser for help on using the repository browser.