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

Last change on this file since 171 was 168, checked in by alloc, 10 years ago

fixes

File size: 2.3 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
16 public MapRenderBlockBuffer (int level)
17 {
18 zoomLevel = level;
19 }
20
21 public void ResetBlock ()
22 {
23 currentBlockMap = string.Empty;
24 }
25
26 public void SaveBlock ()
27 {
28 try {
29 if (currentBlockMap.Length > 0)
30 saveTextureToFile (currentBlockMap);
31 } catch (Exception e) {
32 Log.Out ("Exception in MapRenderBlockBuffer.SaveBlock(): " + e);
33 }
34 }
35
36 public bool LoadBlock (Vector2i block)
37 {
38 bool res = false;
39 Monitor.Enter (blockMap);
40 try {
41 string folder = Constants.MAP_DIRECTORY + "/" + (zoomLevel) + "/" + block.x;
42 string fileName = folder + "/" + block.y + ".png";
43 Directory.CreateDirectory (folder);
44 if (!fileName.Equals (currentBlockMap)) {
45 res = true;
46 SaveBlock ();
47 loadTextureFromFile (fileName);
48 }
49 currentBlockMap = fileName;
50 } finally {
51 Monitor.Exit (blockMap);
52 }
53 return res;
54 }
55
56 public void SetPart (Vector2i offset, int partSize, Color[] pixels)
57 {
58 blockMap.SetPixels (offset.x, offset.y, partSize, partSize, pixels);
59 }
60
61 public Color[] GetHalfScaled ()
62 {
63 zoomBuffer.Resize (Constants.MAP_BLOCK_SIZE, Constants.MAP_BLOCK_SIZE);
64 zoomBuffer.SetPixels (blockMap.GetPixels ());
65
66 TextureScale.Point (zoomBuffer, Constants.MAP_BLOCK_SIZE / 2, Constants.MAP_BLOCK_SIZE / 2);
67
68 return zoomBuffer.GetPixels ();
69 }
70
71 private void loadTextureFromFile (string _fileName)
72 {
73 try {
74 byte[] array = File.ReadAllBytes (_fileName);
75 blockMap.LoadImage (array);
76 } catch (Exception) {
77 for (int x = 0; x < Constants.MAP_BLOCK_SIZE; x++) {
78 for (int y = 0; y < Constants.MAP_BLOCK_SIZE; y++) {
79 blockMap.SetPixel (x, y, nullColor);
80 }
81 }
82 }
83 }
84
85 private void saveTextureToFile (string _fileName)
86 {
87 try {
88 byte[] array = blockMap.EncodeToPNG ();
89 File.WriteAllBytes (_fileName, array);
90 } catch (Exception e) {
91 Log.Out ("Exception in MapRenderBlockBuffer.saveTextureToFile(): " + e);
92 }
93 }
94
95 }
96}
Note: See TracBrowser for help on using the repository browser.