-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathats_texture_table.c
285 lines (219 loc) · 7.33 KB
/
ats_texture_table.c
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
#pragma once
#define STB_IMAGE_RESIZE_IMPLEMENTATION
#include "ext/stb_image_resize.h"
#ifndef TEXTURE_BOARDER_SIZE
#define TEXTURE_BOARDER_SIZE (2)
#endif
#define TEXTURE_TABLE_SIZE (1024)
typedef struct {
b32 in_use;
u32 hash;
tex_rect rect;
char name[64];
} tex_entry;
typedef struct {
u16 width;
u16 height;
u32* pixels;
tex_entry array[TEXTURE_TABLE_SIZE];
} tex_table;
typedef struct {
b32 user_provided;
u16 width;
u16 height;
const u32* pixels;
char name[256];
} tex_image;
static tex_table texture_table;
static usize tex_image_count;
static tex_image tex_image_array[2048];
ATS_API u32* tex_get_pixels(void) {
return texture_table.pixels;
}
ATS_API u16 tex_get_width(void) {
return texture_table.width;
}
ATS_API u16 tex_get_height(void) {
return texture_table.height;
}
ATS_API tex_rect tex_get_rect(tex_id id) {
return texture_table.array[id.index].rect;
}
ATS_API tex_id tex_get_id(const char* name) {
u32 hash = hash_str(name);
u16 index = hash % TEXTURE_TABLE_SIZE;
while (texture_table.array[index].in_use) {
if ((texture_table.array[index].hash == hash) && (strcmp(texture_table.array[index].name, name) == 0)) {
tex_id id = { index };
return id;
}
index = (index + 1) % TEXTURE_TABLE_SIZE;
}
puts(name);
assert(0);
return tex_id(0);
}
ATS_API tex_rect tex_get(const char* name) {
return tex_get_rect(tex_get_id(name));
}
static int tex_cmp_image(const void* va, const void* vb) {
const tex_image* a = (const tex_image*)va;
const tex_image* b = (const tex_image*)vb;
return b->width - a->width;
}
ATS_API void tex_add_image(const char* name, void* pixels, u16 width, u16 height) {
tex_image image = {0};
image.user_provided = 1;
image.width = width;
image.height = height;
image.pixels = pixels;
strcpy_s(image.name, countof(image.name), name);
tex_image_array[tex_image_count++] = image;
}
ATS_API void tex_load_dir(const char* path) {
const char* ext[] = { "*.png", "*.jpg" };
for (i32 i = 0; i < countof(ext); ++i) {
dir_iter(path, ext[i]) {
tex_image image = {0};
image.pixels = file_load_image(dir_path(), &image.width, &image.height);
strcpy_s(image.name, countof(image.name), dir_name());
tex_image_array[tex_image_count++] = image;
}
}
}
ATS_API void tex_load_and_scale_dir(const char* path, u16 denominator) {
assert(denominator);
const char* ext[] = { "*.png", "*.jpg" };
for (i32 i = 0; i < countof(ext); ++i) {
dir_iter(path, ext[i]) {
u16 width = 0;
u16 height = 0;
u32* pixels = file_load_image(dir_path(), &width, &height);
tex_image image = {0};
image.user_provided = 1;
image.width = width / denominator;
image.height = height / denominator;
image.pixels = mem_array(u32, image.width * image.height);
stbir_resize_uint8(
(unsigned char*)pixels, width, height, 0,
(unsigned char*)image.pixels, image.width, image.height, 0,
4);
strcpy_s(image.name, countof(image.name), dir_name());
tex_image_array[tex_image_count++] = image;
free(pixels);
}
}
}
ATS_API void tex_begin(u16 width, u16 height) {
tex_image_count = 0;
texture_table = (tex_table) {
width,
height,
mem_array(u32, (usize)(width * height)),
};
texture_table.array[0].in_use = 1;
}
static usize tex_stack_top;
static tex_rect tex_stack_buf[4096];
static b32 _rect_contains_image(tex_rect rect, u16 width, u16 height) {
u16 rect_width = rect.max_x - rect.min_x;
u16 rect_height = rect.max_y - rect.min_y;
return width <= rect_width && height <= rect_height;
}
static tex_rect _tex_get_fit(u16 width, u16 height) {
u32 j = 0;
for (j = 0; j < tex_stack_top; ++j) {
if (_rect_contains_image(tex_stack_buf[j], width, height)) {
break;
}
}
tex_rect rect = tex_stack_buf[j];
tex_stack_buf[j] = tex_stack_buf[--tex_stack_top];
return rect;
}
static void _tex_add_entry(const char* name, tex_rect rect) {
u32 hash = hash_str(name);
u16 index = hash % TEXTURE_TABLE_SIZE;
while (texture_table.array[index].in_use) {
if ((texture_table.array[index].hash == hash) && (strcmp(texture_table.array[index].name, name) == 0)) {
assert(0);
}
index = (index + 1) % TEXTURE_TABLE_SIZE;
}
tex_entry* entry = &texture_table.array[index];
entry->in_use = 1;
entry->rect = rect;
entry->hash = hash;
strcpy_s(entry->name, 64, name);
}
static u32 _tex_get_image_pixel(const tex_image* image, u16 x, u16 y) {
return image->pixels[y * image->width + x];
}
static void _tex_set_pixel(u16 x, u16 y, u32 color) {
texture_table.pixels[y * texture_table.width + x] = color;
}
static u32 _tex_get_pixel(u16 x, u16 y) {
return texture_table.pixels[y * texture_table.width + x];
}
ATS_API void tex_end(void) {
tex_stack_top = 0;
tex_stack_buf[tex_stack_top++] = (tex_rect) {
0,
0,
texture_table.width,
texture_table.height,
};
qsort(tex_image_array, tex_image_count, sizeof (tex_image), tex_cmp_image);
for (usize i = 0; i < tex_image_count; ++i) {
tex_image* image = &tex_image_array[i];
tex_rect rect = _tex_get_fit(image->width + 2, image->height + 2);
u16 offset_x = rect.min_x + TEXTURE_BOARDER_SIZE;
u16 offset_y = rect.min_y + TEXTURE_BOARDER_SIZE;
u16 size_x = image->width + 2 * TEXTURE_BOARDER_SIZE;
u16 size_y = image->height + 2 * TEXTURE_BOARDER_SIZE;
tex_rect tex = {
offset_x,
offset_y,
(u16)(offset_x + image->width),
(u16)(offset_y + image->height),
};
_tex_add_entry(image->name, tex);
for (u16 y = 0; y < image->height; ++y) {
for (u16 x = 0; x < image->width; ++x) {
u16 tx = offset_x + x;
u16 ty = offset_y + y;
_tex_set_pixel(tx, ty, _tex_get_image_pixel(image, x, y));
}
}
for (u16 y = (tex.min_y - TEXTURE_BOARDER_SIZE); y < (tex.max_y + TEXTURE_BOARDER_SIZE); ++y) {
for (u16 x = (tex.min_x - TEXTURE_BOARDER_SIZE); x < (tex.max_x + TEXTURE_BOARDER_SIZE); ++x) {
_tex_set_pixel(x, y, _tex_get_pixel(
clamp(x, tex.min_x, tex.max_x - 1),
clamp(y, tex.min_y, tex.max_y - 1)));
}
}
tex_rect a = {
(u16)(rect.min_x + size_x),
rect.min_y,
rect.max_x,
rect.max_y,
};
tex_rect b = {
rect.min_x,
(u16)(rect.min_y + size_y),
(u16)(rect.min_x + size_x),
rect.max_y,
};
if (a.min_x + size_x <= rect.max_x && a.min_y + size_y <= rect.max_y) {
tex_stack_buf[tex_stack_top++] = a;
}
if (b.min_x + size_x <= rect.max_x && b.min_y + size_y <= rect.max_y) {
tex_stack_buf[tex_stack_top++] = b;
}
if (!image->user_provided) {
file_free_image(image->pixels);
}
}
tex_image_count = 0;
tex_stack_top = 0;
}