-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopyForTemporaryStructInLoop.cs
More file actions
90 lines (76 loc) · 2.16 KB
/
Copy pathCopyForTemporaryStructInLoop.cs
File metadata and controls
90 lines (76 loc) · 2.16 KB
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
using System;
using System.Collections.Generic;
public static class Program {
public static STex[] list = null;
public static void Main (string[] args) {
SplitGrid(null, 4, 4, new Rectangle(0, 0, 256, 256));
Console.WriteLine(list[0]);
Console.WriteLine(list[1]);
Console.WriteLine(list[2]);
}
public static void SplitGrid (object tex, int gridx, int gridy, Rectangle sheetrect) {
bool gappy = false;
if (gridx < 0) {
gridx = -gridx;
gridy = -gridy;
gappy = true;
}
list = new STex[gridx * gridy];
int cellwidth = sheetrect.Width / gridx;
int cellheight = sheetrect.Height / gridy;
for (int y = 0; y < gridy; y++) {
for (int x = 0; x < gridx; x++) {
int sheetx = sheetrect.X + x * cellwidth;
int sheety = sheetrect.Y + y * cellheight;
Rectangle rect = new Rectangle(sheetx, sheety, cellwidth, cellheight);
if (gappy) {
rect.X += 1;
rect.Y += 1;
rect.Width -= 2;
rect.Height -= 2;
}
list[y * gridx + x] = new STex(tex, rect);
}
}
}
}
public struct Rectangle {
public int X, Y, Width, Height;
public Rectangle (int x, int y, int width, int height) {
X = x;
Y = y;
Width = width;
Height = height;
}
public override string ToString () {
return String.Format("{0}, {1} w={2} h={3}", X, Y, Width, Height);
}
}
public struct STex {
private object tex;
private Rectangle? srcrect;
public object Tex
{
get { return tex; }
}
public Rectangle? Rect
{
get { return srcrect; }
}
public STex(object texture)
{
tex = texture;
srcrect = null;
}
public STex(object texture, Rectangle srcrect)
{
tex = texture;
this.srcrect = srcrect;
}
public override string ToString () {
if (srcrect == null)
return "null";
else
return srcrect.Value.ToString();
}
}