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

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