Skip to content

Commit

Permalink
Add H&V tile flip reduction
Browse files Browse the repository at this point in the history
  • Loading branch information
LutzenH committed Sep 5, 2022
1 parent cc08e6d commit 610a515
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 30 deletions.
87 changes: 61 additions & 26 deletions src/b2g_builder.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,35 @@
#include <stdio.h>
#include <stb_image.c>

static inline ColoredTile builder_internal_h_flip_tile(const ColoredTile* tile) {
ColoredTile h_flipped_tile = *tile;

for (int y = 0; y < 8; ++y) {
size_t end = y * 8 + (8-1);
for (int x = 0; x < 8 / 2; x++) {
COLOR tmp = h_flipped_tile.color[y * 8 + x];
h_flipped_tile.color[y * 8 + x] = h_flipped_tile.color[end];
h_flipped_tile.color[end] = tmp;
end--;
}
}

return h_flipped_tile;
}

static inline ColoredTile builder_internal_v_flip_tile(const ColoredTile* tile) {
ColoredTile v_flipped_tile = *tile;

COLOR tmp[8];
for (int y = 0; y < 8 / 2; ++y) {
memcpy(tmp, &v_flipped_tile.color[y * 8], sizeof(COLOR) * 8);
memcpy(&v_flipped_tile.color[y * 8], &v_flipped_tile.color[(7-y) * 8], sizeof(COLOR) * 8);
memcpy(&v_flipped_tile.color[(7-y) * 8], tmp, sizeof(COLOR) * 8);
}

return v_flipped_tile;
}

BackgroundData* builder_create_background_data_from_image_paths(const char** paths, size_t paths_count, COLOR transparent_color, unsigned int brute_force_shuffle_count)
{
BackgroundData* background_data = c_calloc(1, sizeof(BackgroundData));
Expand Down Expand Up @@ -60,22 +89,22 @@ BackgroundData* builder_create_background_data_from_image_paths(const char** pat

background_data->maps[map_count] = c_calloc(sizeof(Map) + (sizeof(MapTileIndex) * map_width * map_height), 1);
Map* map = background_data->maps[map_count];
map->map_width = map_width;
map->map_height = map_height;
map->width = map_width;
map->height = map_height;

const char* map_name = strrchr(file_name, '/');
if (map_name != NULL) {
map_name++;
} else {
map_name = file_name;
}
snprintf(map->map_name, sizeof(map->map_name), "%s", map_name);
for (int j = 0; j < strlen(map->map_name); ++j) {
if (map->map_name[j] == ' ' || map->map_name[j] == '/' || map->map_name[j] == '\\') {
map->map_name[j] = '_';
snprintf(map->name, sizeof(map->name), "%s", map_name);
for (int j = 0; j < strlen(map->name); ++j) {
if (map->name[j] == ' ' || map->name[j] == '/' || map->name[j] == '\\') {
map->name[j] = '_';
}
if (map->map_name[j] == '.') {
map->map_name[j] = '\0';
if (map->name[j] == '.') {
map->name[j] = '\0';
}
}

Expand All @@ -94,15 +123,25 @@ BackgroundData* builder_create_background_data_from_image_paths(const char** pat
}
}

uint32_t current_tile_index;
ColoredTile h_flipped_tile = builder_internal_h_flip_tile(&tile);
ColoredTile v_flipped_tile = builder_internal_v_flip_tile(&tile);
ColoredTile hv_flipped_tile = builder_internal_v_flip_tile(&h_flipped_tile);

MapTileIndex current_tile_index;
if (cmap_ct_contains(&tiles, tile)) {
current_tile_index = cmap_ct_get(&tiles, tile)->second;
} else {
current_tile_index = (cmap_ct_get(&tiles, tile)->second & 0x03FF);
} else if (cmap_ct_contains(&tiles, h_flipped_tile)) {
current_tile_index = 0x0400 | (cmap_ct_get(&tiles, h_flipped_tile)->second & 0x03FF);
} else if (cmap_ct_contains(&tiles, v_flipped_tile)) {
current_tile_index = 0x0800 | (cmap_ct_get(&tiles, v_flipped_tile)->second & 0x03FF);
} else if (cmap_ct_contains(&tiles, hv_flipped_tile)) {
current_tile_index = 0x0800 | 0x0400 | (cmap_ct_get(&tiles, hv_flipped_tile)->second & 0x03FF);
}else {
current_tile_index = total_unique_tiles_count;
cmap_ct_insert(&tiles, tile, total_unique_tiles_count++);
}

map_tiles[tile_y * map_width + tile_x] = current_tile_index & 0x03FF;
map_tiles[tile_y * map_width + tile_x] = current_tile_index;
}
}

Expand Down Expand Up @@ -176,8 +215,6 @@ BackgroundData* builder_create_background_data_from_image_paths(const char** pat
}
background_data->tile_color_sets_reduction_count = bank_color_sets_used;

// TODO: Add tile reduction using H and V flipping.

// TODO: Make tile palette reduction possible, by adjusting the palette banks so
// that colors of two similar tiles with different colors appear in the same indices in a bank.
// meaning that we can have less tiles which only have a different palette bank.
Expand Down Expand Up @@ -222,8 +259,7 @@ BackgroundData* builder_create_background_data_from_image_paths(const char** pat
}

if ((bank_space_left - ((int)set->size - overlap_count)) >= 0) {
c_foreach(k, cset_color, *set)
{
c_foreach(k, cset_color, *set) {
cset_color_insert(bank, *k.ref);
}
used_sets_count++;
Expand Down Expand Up @@ -338,20 +374,20 @@ BackgroundData* builder_create_background_data_from_image_paths(const char** pat
// Update Map Arrays
for (int idx = 0; idx < background_data->map_count; ++idx) {
Map* map = background_data->maps[idx];
int height = map->map_height;
int width = map->map_width;
int height = map->height;
int width = map->width;

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

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

uint16_t palette_bits = tile_color_sets[tile_idx].bank_idx << 12;
map->tiles[y * width + x] = palette_bits | tile_idx;
map->tiles[y * width + x] |= palette_bits;
}
}
}
Expand Down Expand Up @@ -463,14 +499,13 @@ void builder_internal_print_tile_data(const BackgroundData* background_data)

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

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

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

printf("\n");
Expand Down
12 changes: 9 additions & 3 deletions src/b2g_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ typedef struct {
typedef uint16_t MapTileIndex;

typedef struct {
char map_name[128];
char name[128];

int map_width;
int map_height;
int width;
int height;

MapTileIndex tiles[];
} Map;
Expand Down Expand Up @@ -55,6 +55,12 @@ typedef struct {
#define i_tag ct
#include <stc/cmap.h>

#define i_key ColoredTile
#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])
#define i_eq c_memcmp_eq
#define i_tag ct
#include <stc/cset.h>

static int sort_tile_color_set_using_unique_color_count(const void* a, const void* b)
{
TileColorSet* set_a = (TileColorSet*)a;
Expand Down
2 changes: 1 addition & 1 deletion src/b2g_version.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
#define B2G_VERSION_H

#define B2G_VERSION_MAJOR 0
#define B2G_VERSION_MINOR 2
#define B2G_VERSION_MINOR 3

#endif //B2G_VERSION_H

0 comments on commit 610a515

Please sign in to comment.