-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
286 lines (240 loc) · 11.3 KB
/
Program.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#undef DEBUG
using System.Runtime.InteropServices;
using static SDL2.SDL;
using static SDL2.SDL_image;
namespace SpriteAtlas;
internal class Program
{
internal const string DIRECTORY_PATH = "Images";
private const int WINDOW_W = 1280;
private const int WINDOW_H = 720;
private static IntPtr _window;
internal static IntPtr renderer;
static void Main(string[] args)
{
setup();
Atlas atlas = new();
const string search_pattern = "*.png";
var file_names =
Directory.GetFiles(DIRECTORY_PATH, search_pattern);
foreach (var file_name in file_names)
{
var file_path = Path.GetRelativePath(".", file_name);
#if DEBUG
Console.WriteLine(file_path);
#endif
atlas.surface_data[atlas.entries.Count] =
IMG_Load(file_path);
atlas.entries.Add(new AtlasEntry(file_path,
new SDL_Rect
{ x = 0, y = 0, w = 0, h = 0 }, false));
}
Array.Resize(ref atlas.surface_data, atlas.entries.Count);
Array.Sort(atlas.surface_data, atlas);
for (var i = 0; i < atlas.surface_data.Length; i++)
{
SDL_QueryTexture(
SDL_CreateTextureFromSurface(renderer,
atlas.surface_data[i]), out _, out _, out int w,
out int h);
var rotated = false;
var found_node = Atlas.find_node(atlas.first, w, h);
if (found_node == null)
{
rotated = true;
found_node = Atlas.find_node(atlas.first, h, w);
}
if (found_node != null)
{
#if DEBUG
Console.WriteLine($"Node found for image #{i}");
#endif
//int rotations = 0;
if (rotated)
{
found_node.height = w;
found_node.width = h;
//rotations++;
}
var dest = new SDL_Rect
{
x = found_node.x,
y = found_node.y,
w = w,
h = h
};
atlas.entries[i] =
new AtlasEntry(atlas.entries[i].filename,
dest, rotated);
if (rotated == false)
{
SDL_BlitSurface(atlas.surface_data[i],
IntPtr.Zero,
atlas.master_surface,
ref dest);
}
else
{
IntPtr result =
blit_rotated(atlas.surface_data[i]);
if (result != IntPtr.Zero)
{
SDL_BlitSurface(result,
IntPtr.Zero,
atlas.master_surface,
ref dest);
}
}
}
SDL_FreeSurface(atlas.surface_data[i]);
}
// DEMO BEGINS
// Create the master texture
var master_texture =
SDL_CreateTextureFromSurface(renderer,
atlas.master_surface);
// Extract a single image from the atlas (test)
var test_extract =
atlas.get_atlas_image("1");
// Query the texture for its width and height
SDL_QueryTexture(test_extract, out _, out _,
out var extract_w,
out var extract_h);
// Create the rectangles for the blit
var dst_rect = new SDL_Rect
{ x = 50, y = 50, w = extract_w, h = extract_h };
var dst_rect2 = new SDL_Rect
{
x = 0, y = 0, w = Atlas.ATLAS_SIZE, h = Atlas.ATLAS_SIZE
};
#if DEBUG
foreach (var ae in atlas.entries) {
Console.WriteLine(ae.filename);
}
#endif
while (true)
{
while ((SDL_PollEvent(out var e)) != 0)
{
switch (e.type)
{
case SDL_EventType.SDL_QUIT:
Environment.Exit(0);
break;
default:
break;
}
}
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer,
master_texture,
0, ref dst_rect2);
SDL_RenderPresent(renderer);
}
}
private static void setup()
{
const uint sdl_flags = SDL_INIT_VIDEO;
const SDL_WindowFlags window_flags =
SDL_WindowFlags.SDL_WINDOW_SHOWN;
const SDL_RendererFlags renderer_flags =
SDL_RendererFlags.SDL_RENDERER_ACCELERATED |
SDL_RendererFlags.SDL_RENDERER_PRESENTVSYNC;
const IMG_InitFlags img_flags = IMG_InitFlags.IMG_INIT_PNG;
if (SDL_Init(sdl_flags) < 0)
{
SDL_LogError((int)SDL_LogCategory.SDL_LOG_CATEGORY_APPLICATION,
$"There was an issue starting SDL:\n{SDL_GetError()}!");
}
_window = SDL_CreateWindow("SpriteAtlas Test",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
WINDOW_W, WINDOW_H, window_flags);
if (_window == IntPtr.Zero)
{
SDL_LogError((int)SDL_LogCategory.SDL_LOG_CATEGORY_APPLICATION,
$"There was an issue creating the window:\n{SDL_GetError()}");
}
renderer = SDL_CreateRenderer(_window, -1, renderer_flags);
if (renderer == IntPtr.Zero)
{
SDL_LogError((int)SDL_LogCategory.SDL_LOG_CATEGORY_APPLICATION,
$"There was an issue creating the renderer:\n{SDL_GetError()}");
}
if (IMG_Init(img_flags) != (int)img_flags)
{
SDL_LogError((int)SDL_LogCategory.SDL_LOG_CATEGORY_APPLICATION,
$"There was an issue starting SDL_image:\n{SDL_GetError()}!");
}
}
private static (byte r, byte g, byte b, byte a) get_pixel_color_values(
IntPtr surface_ptr, int x, int y)
{
var pixel = get_pixel(surface_ptr, x, y);
var r = (byte)(pixel & 0xFF);
var g = (byte)((pixel >> 8) & 0xFF);
var b = (byte)((pixel >> 16) & 0xFF);
var a = (byte)((pixel >> 24) & 0xFF);
return (r, g, b, a);
}
private static IntPtr blit_rotated(IntPtr source_surface_ptr)
{
var source_surface =
Marshal.PtrToStructure<SDL_Surface>(source_surface_ptr);
#if DEBUG
Console.WriteLine(
$"Source surface dimensions: {source_surface.w}x{source_surface.h}");
#endif
var dest_surface_ptr = SDL_CreateRGBSurfaceWithFormat(0,
source_surface.h, source_surface.w, 32,
SDL_PIXELFORMAT_ARGB8888);
SDL_LockSurface(source_surface_ptr);
SDL_LockSurface(dest_surface_ptr);
for (var y = 0; y < source_surface.h; y++)
{
for (var x = 0; x < source_surface.w; x++)
{
var pixel = get_pixel(source_surface_ptr, x, y);
var (r, g, b, a) =
get_pixel_color_values(source_surface_ptr,
x, y);
#if DEBUG
Console.WriteLine(
$"Source Pixel ({x}, {y}): R: {r} G: {g} B: {b} A: {a}");
#endif
var dest_x = source_surface.h - y - 1;
set_pixel(dest_surface_ptr, dest_x, x, pixel);
var (dest_r, dest_g, dest_b, dest_a) =
get_pixel_color_values(dest_surface_ptr,
dest_x, x);
#if DEBUG
Console.WriteLine(
$"Dest Pixel ({dest_x}, {x}): R: {dest_r} G: {dest_g} B: {dest_b} A: {dest_a}");
#endif
set_pixel(dest_surface_ptr, dest_x, x, pixel);
}
}
SDL_UnlockSurface(source_surface_ptr);
SDL_UnlockSurface(dest_surface_ptr);
return dest_surface_ptr;
}
private static uint get_pixel(IntPtr surface_ptr, int x, int y)
{
var surface =
Marshal.PtrToStructure<SDL_Surface>(surface_ptr);
const int bytes_per_pixel = 4; //RGBA8888
var pixel = surface.pixels + y * surface.pitch +
x * bytes_per_pixel;
return (uint)Marshal.ReadInt32(pixel);
}
private static void set_pixel(IntPtr surface_ptr, int x, int y,
uint new_pixel)
{
var surface =
Marshal.PtrToStructure<SDL_Surface>(surface_ptr);
const int bytes_per_pixel = 4; //RGBA8888
var pixel = surface.pixels + y * surface.pitch +
x * bytes_per_pixel;
Marshal.WriteInt32(pixel, (int)new_pixel);
}
}