forked from JavidPack/CheatSheet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModUtils.cs
141 lines (119 loc) · 3.8 KB
/
ModUtils.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Terraria;
namespace CheatSheet
{
internal static class ModUtils
{
internal const int TextureMaxTile = 128;
/*
internal static Texture2D Resize(this Texture2D texture, int size)
{
Texture2D result = texture;
float max = Math.Max(texture.Width, texture.Height);
float scale = size / max;
int width = (int)(texture.Width * scale);
int height = (int)(texture.Height * scale);
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
texture.SaveAsPng(ms, width, height);
// Crashes Mac (maybe Linux?)
result = Texture2D.FromStream(texture.GraphicsDevice, ms);
}
return result;
}
*/
internal static Texture2D Resize(this Texture2D[,] textures, int size)
{
Texture2D result = new Texture2D(Main.graphics.GraphicsDevice, size, size);
Color[] data1 = new Color[size * size];
int maxX = textures.GetLength(0);
int maxY = textures.GetLength(1);
int maxWidth = 0;
int maxHeight = 0;
for (int x = 0; x < maxX; x++)
{
maxWidth += textures[x, 0].Width;
}
for (int y = 0; y < maxY; y++)
{
maxHeight += textures[0, y].Height;
}
float max = Math.Max(maxWidth, maxHeight);
float scale = size / max;
int[] widths = new int[maxX + 1];
int[] heights = new int[maxY + 1];
// TODO: Wait, shouldn't I just do this all in 1 draw call?
for (int x = 0; x < maxX; x++)
{
for (int y = 0; y < maxY; y++)
{
Texture2D texture = textures[x, y];
int width = (int)(texture.Width * scale);
int height = (int)(texture.Height * scale);
widths[x + 1] = widths[x] + width;
heights[y + 1] = heights[y] + height;
Texture2D texture2;
if (Terraria.ModLoader.ModLoader.windows)
{
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
// This doesn't work on Mac/Linux but doesn't cause a screen flash.
// TODO: Do this in an Update method so screen flash doesn't matter.
texture.SaveAsPng(ms, width, height);
texture2 = Texture2D.FromStream(texture.GraphicsDevice, ms);
}
}
else
{
// Alt method for Mac/Linux, causes screen flash
texture2 = ResizeTexture(texture, scale);
}
Color[] data2 = new Color[width * height];
texture2.GetData<Color>(data2);
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
data1[heights[y] * size + widths[x] + i * size + j] = data2[i * width + j];
}
}
}
}
result.SetData(data1);
return result;
}
private static Texture2D ResizeTexture(Texture2D texture, float scale)
{
int width = (int)(texture.Width * scale);
int height = (int)(texture.Height * scale);
Main.spriteBatch.End();
RenderTarget2D renderTarget = new RenderTarget2D(Main.graphics.GraphicsDevice, width, height);
Main.instance.GraphicsDevice.SetRenderTarget(renderTarget);
Main.instance.GraphicsDevice.Clear(Color.Transparent);
Main.spriteBatch.Begin();
Rectangle destinationRectangle = new Rectangle(
0, 0, width, height);
Main.spriteBatch.Draw(texture, destinationRectangle, Color.White);
Main.spriteBatch.End();
Main.instance.GraphicsDevice.SetRenderTarget(null);
Main.spriteBatch.Begin();
return renderTarget;
}
public static Texture2D Offset(this Texture2D texture, int x, int y, int width, int height)
{
Texture2D result = new Texture2D(Main.graphics.GraphicsDevice, width, height);
Color[] data = new Color[height * width];
texture.GetData(0, new Rectangle?(new Rectangle(x, y, width, height)), data, 0, data.Length);
result.SetData(data);
return result;
}
internal static Vector2 Offset(this Vector2 position, float x, float y)
{
position.X += x;
position.Y += y;
return position;
}
}
}