Skip to content

Commit 610a515

Browse files
committed
Add H&V tile flip reduction
1 parent cc08e6d commit 610a515

File tree

3 files changed

+71
-30
lines changed

3 files changed

+71
-30
lines changed

src/b2g_builder.c

Lines changed: 61 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,35 @@
55
#include <stdio.h>
66
#include <stb_image.c>
77

8+
static inline ColoredTile builder_internal_h_flip_tile(const ColoredTile* tile) {
9+
ColoredTile h_flipped_tile = *tile;
10+
11+
for (int y = 0; y < 8; ++y) {
12+
size_t end = y * 8 + (8-1);
13+
for (int x = 0; x < 8 / 2; x++) {
14+
COLOR tmp = h_flipped_tile.color[y * 8 + x];
15+
h_flipped_tile.color[y * 8 + x] = h_flipped_tile.color[end];
16+
h_flipped_tile.color[end] = tmp;
17+
end--;
18+
}
19+
}
20+
21+
return h_flipped_tile;
22+
}
23+
24+
static inline ColoredTile builder_internal_v_flip_tile(const ColoredTile* tile) {
25+
ColoredTile v_flipped_tile = *tile;
26+
27+
COLOR tmp[8];
28+
for (int y = 0; y < 8 / 2; ++y) {
29+
memcpy(tmp, &v_flipped_tile.color[y * 8], sizeof(COLOR) * 8);
30+
memcpy(&v_flipped_tile.color[y * 8], &v_flipped_tile.color[(7-y) * 8], sizeof(COLOR) * 8);
31+
memcpy(&v_flipped_tile.color[(7-y) * 8], tmp, sizeof(COLOR) * 8);
32+
}
33+
34+
return v_flipped_tile;
35+
}
36+
837
BackgroundData* builder_create_background_data_from_image_paths(const char** paths, size_t paths_count, COLOR transparent_color, unsigned int brute_force_shuffle_count)
938
{
1039
BackgroundData* background_data = c_calloc(1, sizeof(BackgroundData));
@@ -60,22 +89,22 @@ BackgroundData* builder_create_background_data_from_image_paths(const char** pat
6089

6190
background_data->maps[map_count] = c_calloc(sizeof(Map) + (sizeof(MapTileIndex) * map_width * map_height), 1);
6291
Map* map = background_data->maps[map_count];
63-
map->map_width = map_width;
64-
map->map_height = map_height;
92+
map->width = map_width;
93+
map->height = map_height;
6594

6695
const char* map_name = strrchr(file_name, '/');
6796
if (map_name != NULL) {
6897
map_name++;
6998
} else {
7099
map_name = file_name;
71100
}
72-
snprintf(map->map_name, sizeof(map->map_name), "%s", map_name);
73-
for (int j = 0; j < strlen(map->map_name); ++j) {
74-
if (map->map_name[j] == ' ' || map->map_name[j] == '/' || map->map_name[j] == '\\') {
75-
map->map_name[j] = '_';
101+
snprintf(map->name, sizeof(map->name), "%s", map_name);
102+
for (int j = 0; j < strlen(map->name); ++j) {
103+
if (map->name[j] == ' ' || map->name[j] == '/' || map->name[j] == '\\') {
104+
map->name[j] = '_';
76105
}
77-
if (map->map_name[j] == '.') {
78-
map->map_name[j] = '\0';
106+
if (map->name[j] == '.') {
107+
map->name[j] = '\0';
79108
}
80109
}
81110

@@ -94,15 +123,25 @@ BackgroundData* builder_create_background_data_from_image_paths(const char** pat
94123
}
95124
}
96125

97-
uint32_t current_tile_index;
126+
ColoredTile h_flipped_tile = builder_internal_h_flip_tile(&tile);
127+
ColoredTile v_flipped_tile = builder_internal_v_flip_tile(&tile);
128+
ColoredTile hv_flipped_tile = builder_internal_v_flip_tile(&h_flipped_tile);
129+
130+
MapTileIndex current_tile_index;
98131
if (cmap_ct_contains(&tiles, tile)) {
99-
current_tile_index = cmap_ct_get(&tiles, tile)->second;
100-
} else {
132+
current_tile_index = (cmap_ct_get(&tiles, tile)->second & 0x03FF);
133+
} else if (cmap_ct_contains(&tiles, h_flipped_tile)) {
134+
current_tile_index = 0x0400 | (cmap_ct_get(&tiles, h_flipped_tile)->second & 0x03FF);
135+
} else if (cmap_ct_contains(&tiles, v_flipped_tile)) {
136+
current_tile_index = 0x0800 | (cmap_ct_get(&tiles, v_flipped_tile)->second & 0x03FF);
137+
} else if (cmap_ct_contains(&tiles, hv_flipped_tile)) {
138+
current_tile_index = 0x0800 | 0x0400 | (cmap_ct_get(&tiles, hv_flipped_tile)->second & 0x03FF);
139+
}else {
101140
current_tile_index = total_unique_tiles_count;
102141
cmap_ct_insert(&tiles, tile, total_unique_tiles_count++);
103142
}
104143

105-
map_tiles[tile_y * map_width + tile_x] = current_tile_index & 0x03FF;
144+
map_tiles[tile_y * map_width + tile_x] = current_tile_index;
106145
}
107146
}
108147

@@ -176,8 +215,6 @@ BackgroundData* builder_create_background_data_from_image_paths(const char** pat
176215
}
177216
background_data->tile_color_sets_reduction_count = bank_color_sets_used;
178217

179-
// TODO: Add tile reduction using H and V flipping.
180-
181218
// TODO: Make tile palette reduction possible, by adjusting the palette banks so
182219
// that colors of two similar tiles with different colors appear in the same indices in a bank.
183220
// meaning that we can have less tiles which only have a different palette bank.
@@ -222,8 +259,7 @@ BackgroundData* builder_create_background_data_from_image_paths(const char** pat
222259
}
223260

224261
if ((bank_space_left - ((int)set->size - overlap_count)) >= 0) {
225-
c_foreach(k, cset_color, *set)
226-
{
262+
c_foreach(k, cset_color, *set) {
227263
cset_color_insert(bank, *k.ref);
228264
}
229265
used_sets_count++;
@@ -338,20 +374,20 @@ BackgroundData* builder_create_background_data_from_image_paths(const char** pat
338374
// Update Map Arrays
339375
for (int idx = 0; idx < background_data->map_count; ++idx) {
340376
Map* map = background_data->maps[idx];
341-
int height = map->map_height;
342-
int width = map->map_width;
377+
int height = map->height;
378+
int width = map->width;
343379

344380
for (int y = 0; y < height; ++y) {
345381
for (int x = 0; x < width; ++x) {
346-
int tile_idx = map->tiles[y * width + x];
382+
uint16_t tile_idx = map->tiles[y * width + x] & 0x03FF;
347383

348384
if (tile_idx != tile_color_sets[tile_idx].tile_idx) {
349-
fprintf(stderr, "FATAL ERROR: tile id's do not match! file: %s line: %i\n", __FILE__, __LINE__);
385+
fprintf(stderr, "FATAL ERROR: tile idx's do not match! file: %s line: %i\n", __FILE__, __LINE__);
350386
abort();
351387
}
352388

353389
uint16_t palette_bits = tile_color_sets[tile_idx].bank_idx << 12;
354-
map->tiles[y * width + x] = palette_bits | tile_idx;
390+
map->tiles[y * width + x] |= palette_bits;
355391
}
356392
}
357393
}
@@ -463,14 +499,13 @@ void builder_internal_print_tile_data(const BackgroundData* background_data)
463499

464500
void builder_internal_print_map_data(const Map* map)
465501
{
466-
printf("const unsigned short %s_map[%i] __attribute__((aligned(4))) __attribute__((visibility(\"hidden\"))) = { \n", map->map_name, map->map_width * map->map_height);
502+
printf("const unsigned short %s_map[%i] __attribute__((aligned(4))) __attribute__((visibility(\"hidden\"))) = { \n", map->name, map->width * map->height);
467503

468-
for (int y = 0; y < map->map_height; ++y) {
504+
for (int y = 0; y < map->height; ++y) {
469505
printf("\t");
470506

471-
for (int x = 0; x < map->map_width; ++x) {
472-
int tile_id = map->tiles[y * map->map_width + x];
473-
printf("0x%.04X, ", map->tiles[y * map->map_width + x]);
507+
for (int x = 0; x < map->width; ++x) {
508+
printf("0x%.04X, ", map->tiles[y * map->width + x]);
474509
}
475510

476511
printf("\n");

src/b2g_types.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ typedef struct {
2323
typedef uint16_t MapTileIndex;
2424

2525
typedef struct {
26-
char map_name[128];
26+
char name[128];
2727

28-
int map_width;
29-
int map_height;
28+
int width;
29+
int height;
3030

3131
MapTileIndex tiles[];
3232
} Map;
@@ -55,6 +55,12 @@ typedef struct {
5555
#define i_tag ct
5656
#include <stc/cmap.h>
5757

58+
#define i_key ColoredTile
59+
#define i_hash(ct) (ct->color[0] ^ ct->color[1] ^ ct->color[2] ^ ct->color[3] ^ ct->color[4] ^ ct->color[5] ^ ct->color[6] ^ ct->color[7] ^ ct->color[8] ^ ct->color[9] ^ ct->color[10] ^ ct->color[11] ^ ct->color[12] ^ ct->color[13] ^ ct->color[14] ^ ct->color[15] ^ ct->color[16] ^ ct->color[17] ^ ct->color[18] ^ ct->color[19] ^ ct->color[20] ^ ct->color[21] ^ ct->color[22] ^ ct->color[23] ^ ct->color[24] ^ ct->color[25] ^ ct->color[26] ^ ct->color[27] ^ ct->color[28] ^ ct->color[29] ^ ct->color[30] ^ ct->color[31] ^ ct->color[32] ^ ct->color[33] ^ ct->color[34] ^ ct->color[35] ^ ct->color[36] ^ ct->color[37] ^ ct->color[38] ^ ct->color[39] ^ ct->color[40] ^ ct->color[41] ^ ct->color[42] ^ ct->color[43] ^ ct->color[44] ^ ct->color[45] ^ ct->color[46] ^ ct->color[47] ^ ct->color[48] ^ ct->color[49] ^ ct->color[50] ^ ct->color[51] ^ ct->color[52] ^ ct->color[53] ^ ct->color[54] ^ ct->color[55] ^ ct->color[56] ^ ct->color[57] ^ ct->color[58] ^ ct->color[59] ^ ct->color[60] ^ ct->color[61] ^ ct->color[62] ^ ct->color[63])
60+
#define i_eq c_memcmp_eq
61+
#define i_tag ct
62+
#include <stc/cset.h>
63+
5864
static int sort_tile_color_set_using_unique_color_count(const void* a, const void* b)
5965
{
6066
TileColorSet* set_a = (TileColorSet*)a;

src/b2g_version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
#define B2G_VERSION_H
33

44
#define B2G_VERSION_MAJOR 0
5-
#define B2G_VERSION_MINOR 2
5+
#define B2G_VERSION_MINOR 3
66

77
#endif //B2G_VERSION_H

0 commit comments

Comments
 (0)