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

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