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

Last change on this file since 198 was 189, checked in by alloc, 10 years ago

fixes

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