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+
837BackgroundData * 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
464500void 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" );
0 commit comments