maug
Quick and dirty C mini-augmentation library.
Loading...
Searching...
No Matches
retrotil.h
Go to the documentation of this file.
1
2#ifndef RETROTIL_H
3#define RETROTIL_H
4
5#include <mjson.h>
6#include <mfile.h>
7
16
17#define RETROTILE_PRESENT
18
19typedef int16_t retrotile_coord_t;
20
21#ifndef RETROTILE_TRACE_CHARS
22# define RETROTILE_TRACE_CHARS 0
23#endif /* !RETROTILE_TRACE_TOKENS */
24
25#ifndef RETROTILE_NAME_SZ_MAX
27# define RETROTILE_NAME_SZ_MAX 10
28#endif /* !RETROTILE_NAME_SZ_MAX */
29
30#ifndef RETROTILE_PROP_NAME_SZ_MAX
32# define RETROTILE_PROP_NAME_SZ_MAX 10
33#endif /* !RETROTILE_PROP_NAME_SZ_MAX */
34
35#ifndef RETROTILE_TILE_SCALE_DEFAULT
37# define RETROTILE_TILE_SCALE_DEFAULT 1.0f
38#endif /* !RETROTILE_TILE_SCALE_DEFAULT */
39
40#ifndef RETROTILE_TRACE_LVL
42# define RETROTILE_TRACE_LVL 0
43#endif /* !RETROTILE_TRACE_LVL */
44
45#ifndef RETROTILE_VORONOI_DEFAULT_SPB
46# define RETROTILE_VORONOI_DEFAULT_SPB 8
47#endif /* !RETROTILE_VORONOI_DEFAULT_SPB */
48
49#ifndef RETROTILE_VORONOI_DEFAULT_DRIFT
50# define RETROTILE_VORONOI_DEFAULT_DRIFT 4
51#endif /* !RETROTILE_VORONOI_DEFAULT_DRIFT */
52
53#ifdef MPARSER_TRACE_NAMES
54# define retrotile_mstate_name( state ) gc_retrotile_mstate_names[state]
55#else
56# define retrotile_mstate_name( state ) state
57#endif /* MPARSER_TRACE_NAMES */
58
59#ifndef RETROTILE_PARSER_FLAG_LITERAL_PATHS
65# define RETROTILE_PARSER_FLAG_LITERAL_PATHS 0x02
66#endif /* !RETROTILE_PARSER_FLAG_LITERAL_PATHS */
67
73#define RETROTILE_PARSER_MODE_MAP 0
74
80#define RETROTILE_PARSER_MODE_DEFS 1
81
82#define RETROTILE_CLASS_TABLE( f ) \
83 f( TILE, tile, 0 ) \
84 f( MOBILE, mobile, 1 ) \
85 f( WARP, warp, 2 ) \
86 f( ITEM, item, 3 ) \
87 f( CROP, crop, 4 )
88
93
98
103#define RETROTILE_TILE_FLAG_BLOCK 0x01
104
109#define RETROTILE_TILE_FLAG_RESERVED1 0x02
110
115#define RETROTILE_TILE_FLAG_RESERVED2 0x04
116
121#define RETROTILE_TILE_FLAG_RESERVED3 0x08
122 /* retrotile_defs_types */
124
129#define RETROTILE_PROP_TYPE_OTHER 0
130
135#define RETROTILE_PROP_TYPE_STRING 1
136
141#define RETROTILE_PROP_TYPE_FILE 2
142
147#define RETROTILE_PROP_TYPE_INT 3
148
159#define RETROTILE_DS_FLAG_INIT_DATA 0x02
160
164#define RETROTILE_IDX_FMT "%u"
165
168 size_t sz;
169 uint8_t flags;
170 retroflat_asset_path image_path;
171 size_t x;
172 size_t y;
173 uint16_t tile_class;
174 char warp_dest[RETROTILE_NAME_SZ_MAX + 1];
181 retrotile_coord_t warp_x;
182 retrotile_coord_t warp_y;
184 int8_t no_serial;
185 RETROFLAT_COLOR color;
186#ifdef RETROFLAT_3D
187 /* TODO: Work this into retrogxc? */
188 struct RETRO3DP_MODEL model;
189#endif /* RETROFLAT_3D */
190#ifdef RETROGXC_PRESENT
191 ssize_t image_cache_id;
192#else
193 /* TODO: Don't exclude this if the cache is disabled; see retrogui image ctl. */
194 struct RETROFLAT_BITMAP image;
195#endif /* RETROGXC_PRESENT */
196};
197 /* retrotile_defs */
199
202 size_t sz;
204 size_t total_sz;
205 uint16_t layer_class;
206};
207
213 /* \brief X position in tilemap tiles. */
214 retrotile_coord_t x;
215 /* \brief Y position in tilemap tiles. */
216 retrotile_coord_t y;
217};
218
224struct RETROTILE {
226 size_t sz;
227 char name[RETROTILE_NAME_SZ_MAX + 1];
228 char tileset[RETROTILE_NAME_SZ_MAX + 1];
230 uint32_t total_sz;
232 uint32_t layers_count;
236 size_t tiles_h;
238 size_t tiles_w;
244};
245
252 int16_t sect_x;
254 int16_t sect_y;
256 int16_t sect_w;
258 int16_t sect_h;
260 int16_t sect_w_half;
262 int16_t sect_h_half;
263 retroflat_tile_t highest_generated;
264 retroflat_tile_t lowest_generated;
265};
266
268 int16_t tiles_changed;
269 retroflat_tile_t center;
270 retroflat_tile_t outside;
273};
274
275#define retrotile_get_tile( tilemap, layer, x, y ) \
276 (retrotile_get_tiles_p( layer )[((y) * (tilemap)->tiles_w) + (x)])
277
278#define retrotile_set_tile( tilemap, layer, x, y, new_val ) \
279 (retrotile_get_tiles_p( layer )[((y) * (tilemap)->tiles_w) + (x)])
280
281#define retrotile_get_tiles_p( layer ) \
282 ((retroflat_tile_t*)(((uint8_t*)(layer)) + \
283 sizeof( struct RETROTILE_LAYER )))
284
285#ifdef MAUG_NO_STDLIB
286/* Loop is slower, but doesn't use memset. */
287/* TODO: This kinda assumes retroflat_tile_t is 8 bits... Fix that! */
288# define retrotile_clear_tiles( t, layer, i ) \
289 assert( 1 == sizeof( retroflat_tile_t ) ); \
290 for( \
291 i = 0 ; \
292 (t)->tiles_w * (t)->tiles_h * sizeof( retroflat_tile_t ) > i ; \
293 i++ \
294 ) { \
295 retrotile_get_tiles_p( layer )[i] = -1; \
296 }
297#else
298# define retrotile_clear_tiles( t, layer, i ) \
299 memset( retrotile_get_tiles_p( layer ), -1, \
300 (t)->tiles_w * (t)->tiles_h * sizeof( retroflat_tile_t ) )
301#endif /* MAUG_NO_STDLIB */
302
307
308typedef MERROR_RETVAL (*retrotile_tj_parse_cb)(
309 const char* dirname, const char* filename, MAUG_MHANDLE* p_tm_h,
310 struct MDATA_VECTOR* p_td, mparser_wait_cb_t wait_cb, void* wait_data,
311 mparser_parse_token_cb token_cb, void* token_cb_data, uint8_t passes,
312 uint8_t flags );
313
315 uint8_t mstate;
316 uint16_t flags;
317 /* TODO: Use flags and combine these. */
318 uint8_t pass;
319 uint8_t passes_max;
321 uint8_t mode;
322 mparser_wait_cb_t wait_cb;
323 void* wait_data;
324 retroflat_ms_t wait_last;
325 size_t layer_tile_iter;
328 int last_prop_type;
329 size_t last_prop_name_sz;
332 char tileset_name[RETROTILE_NAME_SZ_MAX + 1];
333 size_t pass_layer_iter;
339 size_t tiles_w;
340 size_t tiles_h;
341 struct RETROTILE* t;
342 retrotile_tj_parse_cb tj_parse_cb;
348 mparser_parse_token_cb custom_token_cb;
349 void* custom_token_cb_data;
350 struct MJSON_PARSER jparser;
351 struct MDATA_VECTOR* p_tile_defs;
352 char dirname[MAUG_PATH_SZ_MAX + 1];
353 uint16_t layer_class;
354};
355
356/* State Idx JSONKeyWord Parent ParseMode */
357#define RETROTILE_PARSER_MSTATE_TABLE( f ) \
358 f( MTILESTATE_NONE, 0, "", 0, 0 ) \
359 f( MTILESTATE_HEIGHT, 1, "height", 0 , 0 ) \
360 f( MTILESTATE_WIDTH, 2, "width", 0 , 0 ) \
361 f( MTILESTATE_LAYERS, 3, "layers", 0 , 0 ) \
362 f( MTILESTATE_LAYER_DATA, 4, "data", 15 /* LAYER */ , 0 ) \
363 f( MTILESTATE_LAYER_NAME, 5, "name", 15 /* LAYER */ , 0 ) \
364 f( MTILESTATE_TILES, 6, "tiles", 0 , 1 ) \
365 f( MTILESTATE_TILES_ID, 7, "id", 6 /* TILES */ , 1 ) \
366 f( MTILESTATE_TILES_IMAGE, 8, "image", 6 /* TILES */ , 1 ) \
367 f( MTILESTATE_TILESETS, 9, "tilesets", 0 , 0 ) \
368 f( MTILESTATE_TILESETS_SRC, 10, "source", 9 /* TILESETS */ , 0 ) \
369 f( MTILESTATE_TILESETS_FGID, 11, "firstgid", 9 /* TILESETS */ , 0 ) \
370 f( MTILESTATE_TILESETS_PROP, 12, "firstgid", 9 /* TILESETS */ , 0 ) \
371 f( MTILESTATE_GRID, 13, "grid", 0 , 1 ) \
372 f( MTILESTATE_TILES_PROP, 14, "properties", 6 /* TILES */ , 1 ) \
373 f( MTILESTATE_LAYER, 15, "layers", /* [sic] */ 3 , 0 ) \
374 f( MTILESTATE_TILES_PROP_NAME, 16, "name", 14 /* TIL_PROP */ , 1 ) \
375 f( MTILESTATE_TILES_PROP_TYPE, 17, "type", 14 /* TIL_PROP */ , 1 ) \
376 f( MTILESTATE_TILES_PROP_VAL, 18, "value", 14 /* TIL_PROP */ , 1 ) \
377 f( MTILESTATE_PROP, 19, "properties", 0 /* NONE */ , 0 ) \
378 f( MTILESTATE_PROP_NAME, 20, "name", 19 /* PROP */ , 0 ) \
379 f( MTILESTATE_PROP_TYPE, 21, "type", 19 /* PROP */ , 0 ) \
380 f( MTILESTATE_PROP_VAL, 22, "value", 19 /* PROP */ , 0 ) \
381 f( MTILESTATE_LAYER_CLASS, 23, "class", 15 /* LAYER */ , 0 ) \
382 f( MTILESTATE_TILES_CLASS, 24, "type", 6 /* TILES */ , 1 ) \
383 f( MTILESTATE_NAME, 25, "name", 0 , 1 ) \
384 f( MTILESTATE_WANGSETS, 26, "wangsets", 0 , 1 ) \
385 f( MTILESTATE_TPROP, 27, "properties", 0 /* NONE */ , 1 ) \
386 f( MTILESTATE_TPROP_NAME, 28, "name", 27 /* PROP */ , 1 ) \
387 f( MTILESTATE_TPROP_TYPE, 29, "type", 27 /* PROP */ , 1 ) \
388 f( MTILESTATE_TPROP_VAL, 30, "value", 27 /* PROP */ , 1 )
389
390/* TODO: Mine wangsets for slowdown values, etc. */
391
393retrotile_parse_json_c( struct RETROTILE_PARSER* parser, char c );
394
399int retrotile_parse_prop_type( const char* token, size_t token_sz );
400
408mfix_t retrotile_static_rotation_from_dir( const char* dir );
409
435 const char* dirname, const char* filename, MAUG_MHANDLE* p_tilemap_h,
436 struct MDATA_VECTOR* p_tile_defs,
437 mparser_wait_cb_t wait_cb, void* wait_data,
438 mparser_parse_token_cb token_cb, void* token_cb_data, uint8_t passes,
439 uint8_t flags );
440 /* retrotile_parser */
442
448
449typedef MERROR_RETVAL (*retrotile_ani_cb)(
450 void* animation_cb_data, int16_t iter );
451
452typedef MERROR_RETVAL (*retrotile_gen_cb)(
453 struct RETROTILE* t, retroflat_tile_t min_z, retroflat_tile_t max_z,
454 uint32_t tuning, size_t layer_idx, uint8_t flags, void* data,
455 retrotile_ani_cb animation_cb, void* animation_cb_data );
456
467 struct RETROTILE* t, retroflat_tile_t min_z, retroflat_tile_t max_z,
468 uint32_t tuning, size_t layer_idx, uint8_t flags, void* data,
469 retrotile_ani_cb animation_cb, void* animation_cb_data );
470
482 struct RETROTILE* t, retroflat_tile_t min_z, retroflat_tile_t max_z,
483 uint32_t tuning, size_t layer_idx, uint8_t flags, void* data,
484 retrotile_ani_cb animation_cb, void* animation_cb_data );
485
494 struct RETROTILE* t, retroflat_tile_t min_z, retroflat_tile_t max_z,
495 uint32_t tuning, size_t layer_idx, uint8_t flags, void* data,
496 retrotile_ani_cb animation_cb, void* animation_cb_data );
497
505 struct RETROTILE* t, retroflat_tile_t min_z, retroflat_tile_t max_z,
506 uint32_t tuning, size_t layer_idx, uint8_t flags, void* data,
507 retrotile_ani_cb animation_cb, void* animation_cb_data );
508 /* retrotile_gen */
510
511struct RETROTILE_LAYER* retrotile_get_layer_p(
512 struct RETROTILE* tilemap, uint32_t layer_idx );
513
514MERROR_RETVAL retrotile_alloc(
515 MAUG_MHANDLE* p_tilemap_h, size_t w, size_t h, size_t layers_count,
516 const char* tilemap_name, const char* tileset_name );
517
518void retrotile_format_asset_path(
519 retroflat_asset_path path_out, const char* afile,
520 struct RETROTILE_PARSER* parser );
521
522MERROR_RETVAL retrotile_clear_refresh( retroflat_pxxy_t y_max );
523
524MERROR_RETVAL retrotile_topdown_draw(
525 struct RETROFLAT_BITMAP* target,
526 struct RETROTILE* t, struct MDATA_VECTOR* t_defs );
527
528#ifdef RETROTIL_C
529
530# include <mparser.h>
531
532/* TODO: Function names should be verb_noun! */
533
534#if RETROTILE_TRACE_LVL > 0
535# define retrotile_parser_mstate( parser, new_mstate ) \
536 parser->mstate = new_mstate; \
537 debug_printf( \
538 RETROTILE_TRACE_LVL, "parser mstate: %d", parser->mstate );
539#else
540# define retrotile_parser_mstate( parser, new_mstate ) \
541 parser->mstate = new_mstate;
542#endif /* RETROTILE_TRACE_LVL */
543
544# define RETROTILE_PARSER_MSTATE_TABLE_CONST( name, idx, tokn, parent, m ) \
545 MAUG_CONST uint8_t SEG_MCONST name = idx;
546
547RETROTILE_PARSER_MSTATE_TABLE( RETROTILE_PARSER_MSTATE_TABLE_CONST )
548
549#ifdef MPARSER_TRACE_NAMES
550# define RETROTILE_PARSER_MSTATE_TABLE_NAME( name, idx, tokn, parent, m ) \
551 #name,
552
553static MAUG_CONST char* SEG_MCONST gc_retrotile_mstate_names[] = {
554 RETROTILE_PARSER_MSTATE_TABLE( RETROTILE_PARSER_MSTATE_TABLE_NAME )
555 ""
556};
557#endif /* MPARSER_TRACE_NAMES */
558
559# define RETROTILE_PARSER_MSTATE_TABLE_TOKEN( name, idx, tokn, parent, m ) \
560 tokn,
561
562static MAUG_CONST char* SEG_MCONST gc_retrotile_mstate_tokens[] = {
563 RETROTILE_PARSER_MSTATE_TABLE( RETROTILE_PARSER_MSTATE_TABLE_TOKEN )
564 ""
565};
566
567# define RETROTILE_PARSER_MSTATE_TABLE_PARNT( name, idx, tokn, parent, m ) \
568 parent,
569
570static MAUG_CONST uint8_t SEG_MCONST gc_retrotile_mstate_parents[] = {
571 RETROTILE_PARSER_MSTATE_TABLE( RETROTILE_PARSER_MSTATE_TABLE_PARNT )
572 0
573};
574
575# define RETROTILE_PARSER_MSTATE_TABLE_MODE( name, idx, tokn, parent, m ) \
576 m,
577
578static MAUG_CONST uint8_t SEG_MCONST gc_retrotile_mstate_modes[] = {
579 RETROTILE_PARSER_MSTATE_TABLE( RETROTILE_PARSER_MSTATE_TABLE_MODE )
580 0
581};
582
583# define RETROTILE_CLASS_TABLE_CONSTS( A, a, i ) \
584 MAUG_CONST uint8_t SEG_MCONST RETROTILE_CLASS_ ## A = i;
585
586RETROTILE_CLASS_TABLE( RETROTILE_CLASS_TABLE_CONSTS )
587
588/* === */
589
590static void retrotile_parser_match_token(
591 const char* token, size_t token_sz, struct RETROTILE_PARSER* parser
592) {
593 size_t j = 1;
594
595 /* Figure out what the key is for. */
596 while( '\0' != gc_retrotile_mstate_tokens[j][0] ) {
597 if(
598 /* Make sure tokens match. */
599 maug_strlen( gc_retrotile_mstate_tokens[j] ) != token_sz ||
600 0 != maug_strncmp(
601 token, gc_retrotile_mstate_tokens[j], token_sz + 1
602 )
603 ) {
604 j++;
605 continue;
606
607 } else if(
608 /* This state can only be
609 * reached THROUGH that parent state. This allows us to have
610 * keys with the same name but different parents!
611 */
612 parser->mstate != gc_retrotile_mstate_parents[j]
613 ) {
614#ifdef RETROTILE_TRACE_TOKENS
615 debug_printf(
617 "found token \"%s\" "
618#ifdef MPARSER_TRACE_NAMES
619 "but incorrect parent %s (%d) (needs %s (%d))!",
620#else
621 "but incorrect parent %d (needs %d)!",
622#endif /* MPARSER_TRACE_NAMES */
623 token,
624#ifdef MPARSER_TRACE_NAMES
625 retrotile_mstate_name( parser->mstate ),
626 parser->mstate,
627 retrotile_mstate_name( gc_retrotile_mstate_parents[j] ),
628 gc_retrotile_mstate_parents[j]
629#else
630 parser->mstate,
631 gc_retrotile_mstate_parents[j]
632#endif /* MPARSER_TRACE_NAMES */
633 );
634#endif /* !RETROTILE_TRACE_TOKENS */
635 j++;
636 continue;
637
638 } else if( parser->mode != gc_retrotile_mstate_modes[j] ) {
639#ifdef RETROTILE_TRACE_TOKENS
640 debug_printf(
641 RETROTILE_TRACE_LVL, "found token %s but incorrect mode %u!",
642 token,
643 gc_retrotile_mstate_modes[j] );
644#endif /* !RETROTILE_TRACE_TOKENS */
645 j++;
646 continue;
647
648 } else {
649 /* Found it! */
650#ifdef RETROTILE_TRACE_TOKENS
651 debug_printf(
653 "found token \"%s\" "
654#ifdef MPARSER_TRACE_NAMES
655 "under correct parent %s (%d)!",
656#else
657 "under correct parent %d!",
658#endif /* MPARSER_TRACE_NAMES */
659 token,
660#ifdef MPARSER_TRACE_NAMES
661 retrotile_mstate_name( parser->mstate ),
662 parser->mstate
663#else
664 parser->mstate
665#endif /* MPARSER_TRACE_NAMES */
666 );
667#endif /* !RETROTILE_TRACE_TOKENS */
668
669 retrotile_parser_mstate( parser, j );
670 return;
671 }
672 }
673}
674
675/* === */
676
677MERROR_RETVAL retrotile_parser_parse_tiledef_token(
678 void* jparser_void, const char* token, size_t token_sz, void* parser_arg
679) {
680 MERROR_RETVAL retval = MERROR_OK;
681 struct RETROTILE_TILE_DEF* tile_def = NULL;
682 struct RETROTILE_PARSER* parser = (struct RETROTILE_PARSER*)parser_arg;
683 size_t tileset_id_parsed = 0;
684
685 /* We don't lock the vector right away, since we might reallocate tile defs
686 * later on, below.
687 */
688
689 /* Try the custom parser. */
690 if(
691 NULL != parser->custom_token_cb &&
692 /* MERROR_PREEMPT is dealt with in cleanup. */
693 MERROR_OK != (retval = parser->custom_token_cb(
694 parser, token, token_sz, parser_arg ))
695 ) {
696 goto cleanup;
697 }
698
699 if( MJSON_PSTATE_OBJECT_VAL != mjson_parser_pstate( &(parser->jparser) ) ) {
700 /* Not a token we have to worry about! */
701 retrotile_parser_match_token( token, token_sz, parser );
702 goto cleanup;
703 }
704
705 if( MTILESTATE_TILES_ID == parser->mstate ) {
706 retrotile_parser_mstate( parser, MTILESTATE_TILES );
707 if( 0 == parser->pass ) {
708 /* Parse tile ID. */
709 tileset_id_parsed = maug_atou32( token, token_sz, 10 );
710 if( tileset_id_parsed > parser->tileset_id_cur ) {
711 parser->tileset_id_cur = tileset_id_parsed;
712#if RETROTILE_TRACE_LVL > 0
713 debug_printf(
715 "new highest tile ID: " SIZE_T_FMT, parser->tileset_id_cur );
716#endif /* RETROTILE_TRACE_LVL */
717 }
718 } else {
719 assert( 0 < mdata_vector_ct( parser->p_tile_defs ) );
720 parser->tileset_id_cur = maug_atou32( token, token_sz, 10 );
721#if RETROTILE_TRACE_LVL > 0
722 debug_printf(
724 "next tile ID: " SIZE_T_FMT, parser->tileset_id_cur );
725#endif /* RETROTILE_TRACE_LVL */
726 }
727 retrotile_parser_mstate( parser, MTILESTATE_TILES );
728
729 } else if( MTILESTATE_TILES_IMAGE == parser->mstate ) {
730 if( 1 == parser->pass ) {
731 /* Need this pass 1 so tile_defs vector is allocated. */
732 mdata_vector_lock( parser->p_tile_defs );
733 tile_def = mdata_vector_get(
734 parser->p_tile_defs, parser->tileset_id_cur,
735 struct RETROTILE_TILE_DEF );
736 assert( NULL != tile_def );
737
738 /* Parse tile image. */
739 if(
742 ) {
743 mfile_assign_path( tile_def->image_path, token, 0 );
744 } else {
745 mfile_assign_path(
746 tile_def->image_path, token, MFILE_ASSIGN_FLAG_TRIM_EXT );
747 }
748
749 /* Setup tile_def sz for serializing. */
750 tile_def->sz = sizeof( struct RETROTILE_TILE_DEF );
751
752#if RETROTILE_TRACE_LVL > 0
753 debug_printf(
754 RETROTILE_TRACE_LVL, "set tile ID " SIZE_T_FMT " to: %s",
755 parser->tileset_id_cur, tile_def->image_path );
756#endif /* RETROTILE_TRACE_LVL */
757 }
758 retrotile_parser_mstate( parser, MTILESTATE_TILES );
759
760 } else if( MTILESTATE_TILES_CLASS == parser->mstate ) {
761 if( 1 == parser->pass ) {
762 /* Set the class from the pass 1 so the custom function can use
763 * it on pass 2 even if it's alphabetically out of order.
764 * (Tiled calls it "type" and puts it near the bottom!)
765 *
766 * We can't do this on pass 0 since tiles aren't allocated yet!
767 */
768 mdata_vector_lock( parser->p_tile_defs );
769 tile_def = mdata_vector_get(
770 parser->p_tile_defs, parser->tileset_id_cur,
771 struct RETROTILE_TILE_DEF );
772 assert( NULL != tile_def );
773 assert( 0 == tile_def->tile_class );
774
775#if RETROTILE_TRACE_LVL > 0
776 #define RETROTILE_CLASS_TABLE_SET( A, a, i ) \
777 } else if( 0 == maug_strncmp( #a, token, maug_strlen( #a ) + 1 ) ) { \
778 tile_def->tile_class = RETROTILE_CLASS_ ## A; \
779 debug_printf( RETROTILE_TRACE_LVL, \
780 "set tile " SIZE_T_FMT " type: " #a " (%u)", \
781 parser->tileset_id_cur, tile_def->tile_class );
782#else
783 #define RETROTILE_CLASS_TABLE_SET( A, a, i ) \
784 } else if( 0 == maug_strncmp( #a, token, maug_strlen( #a ) + 1 ) ) { \
785 tile_def->tile_class = RETROTILE_CLASS_ ## A;
786#endif /* RETROTILE_TRACE_LVL */
787
788 if( 0 ) {
789 RETROTILE_CLASS_TABLE( RETROTILE_CLASS_TABLE_SET )
790 } else {
791 tile_def->tile_class = RETROTILE_CLASS_TILE;
792#if RETROTILE_TRACE_LVL > 0
793 debug_printf( RETROTILE_TRACE_LVL,
794 "set tile " SIZE_T_FMT " type: tile (%u)",
795 parser->tileset_id_cur, tile_def->tile_class );
796#endif /* RETROTILE_TRACE_LVL */
797 }
798 }
799 retrotile_parser_mstate( parser, MTILESTATE_TILES );
800
801 } else if( MTILESTATE_TILES_PROP_NAME == parser->mstate ) {
802#if RETROTILE_TRACE_LVL > 0
803 debug_printf( RETROTILE_TRACE_LVL, "parsing property: %s", token );
804#endif /* RETROTILE_TRACE_LVL */
805 maug_mzero( parser->last_prop_name, RETROTILE_PROP_NAME_SZ_MAX + 1 );
806 maug_strncpy( parser->last_prop_name, token, RETROTILE_PROP_NAME_SZ_MAX );
807 retrotile_parser_mstate( parser, MTILESTATE_TILES_PROP );
808
809 } else if( MTILESTATE_TILES_PROP_TYPE == parser->mstate ) {
810#if RETROTILE_TRACE_LVL > 0
811 debug_printf( RETROTILE_TRACE_LVL, "property %s is type: %s",
812 parser->last_prop_name, token );
813#endif /* RETROTILE_TRACE_LVL */
814 parser->last_prop_type = retrotile_parse_prop_type( token, token_sz );
815 retrotile_parser_mstate( parser, MTILESTATE_TILES_PROP );
816
817 } else if( MTILESTATE_TILES_PROP_VAL == parser->mstate ) {
818 /* This would be ideal to be handled in the custom token parser callback.
819 */
820
821 if( 1 == parser->pass ) {
822 mdata_vector_lock( parser->p_tile_defs );
823 tile_def = mdata_vector_get(
824 parser->p_tile_defs, parser->tileset_id_cur,
825 struct RETROTILE_TILE_DEF );
826 assert( NULL != tile_def );
827
828 if( 0 == maug_strncmp( "warp_dest", parser->last_prop_name, 10 ) ) {
829 maug_mzero( tile_def->warp_dest, RETROTILE_NAME_SZ_MAX );
830 maug_strncpy( tile_def->warp_dest, token, RETROTILE_NAME_SZ_MAX );
831#if RETROTILE_TRACE_LVL > 0
832 debug_printf(
833 RETROTILE_TRACE_LVL, "set tile " SIZE_T_FMT " warp_dest: %s",
834 parser->tileset_id_cur, tile_def->warp_dest );
835#endif /* RETROTILE_TRACE_LVL */
836
837 } else if( 0 == maug_strncmp( "warp_x", parser->last_prop_name, 7 ) ) {
838 tile_def->warp_x = maug_atos32( token, token_sz );
839#if RETROTILE_TRACE_LVL > 0
840 debug_printf(
841 RETROTILE_TRACE_LVL, "set tile " SIZE_T_FMT " warp_x: %d",
842 parser->tileset_id_cur, tile_def->warp_x );
843#endif /* RETROTILE_TRACE_LVL */
844
845 } else if( 0 == maug_strncmp( "warp_y", parser->last_prop_name, 7 ) ) {
846 tile_def->warp_y = maug_atos32( token, token_sz );
847#if RETROTILE_TRACE_LVL > 0
848 debug_printf(
849 RETROTILE_TRACE_LVL, "set tile " SIZE_T_FMT " warp_y: %d",
850 parser->tileset_id_cur, tile_def->warp_y );
851#endif /* RETROTILE_TRACE_LVL */
852
853 } else if( 0 == maug_strncmp( "color", parser->last_prop_name, 7 ) ) {
854
855#define RETROFLAT_COLOR_TABLE_TILPRP( idx, name_l, name_u, r, g, b, cgc, cgd ) \
856 } else if( 0 == maug_strncmp( token, #name_l, token_sz ) ) { \
857 tile_def->color = RETROFLAT_COLOR_ ## name_u;
858
859 if( 0 ) {
860 RETROFLAT_COLOR_TABLE( RETROFLAT_COLOR_TABLE_TILPRP );
861 }
862
863#if RETROTILE_TRACE_LVL > 0
864 debug_printf(
865 RETROTILE_TRACE_LVL, "set tile " SIZE_T_FMT " color: %d",
866 parser->tileset_id_cur, tile_def->color );
867#endif /* RETROTILE_TRACE_LVL */
868
869 }
870 }
871
872 maug_mzero( parser->last_prop_name, RETROTILE_PROP_NAME_SZ_MAX + 1 );
873 retrotile_parser_mstate( parser, MTILESTATE_TILES_PROP );
874
875 } else if( MTILESTATE_NAME == parser->mstate ) {
876 maug_strncpy( parser->tileset_name, token, RETROTILE_NAME_SZ_MAX );
877#if RETROTILE_TRACE_LVL > 0
878 debug_printf(
879 RETROTILE_TRACE_LVL, "tileset name: %s", parser->tileset_name );
880#endif /* RETROTILE_TRACE_LVL */
881
882 retrotile_parser_mstate( parser, 0 );
883
884 } else if( MTILESTATE_TPROP_NAME == parser->mstate ) {
885 maug_mzero( parser->last_prop_name, RETROTILE_PROP_NAME_SZ_MAX + 1 );
886 maug_strncpy(
888 parser->last_prop_name_sz = token_sz;
889 retrotile_parser_mstate( parser, MTILESTATE_TPROP );
890
891 } else if( MTILESTATE_TPROP_TYPE == parser->mstate ) {
892#if RETROTILE_TRACE_LVL > 0
893 debug_printf( RETROTILE_TRACE_LVL, "property %s is type: %s",
894 parser->last_prop_name, token );
895#endif /* RETROTILE_TRACE_LVL */
896 parser->last_prop_type = retrotile_parse_prop_type( token, token_sz );
897 retrotile_parser_mstate( parser, MTILESTATE_TPROP );
898
899 } else if( MTILESTATE_TPROP_VAL == parser->mstate ) {
900 /* This should be handled in the custom_cb above! */
901 maug_mzero( parser->last_prop_name, RETROTILE_PROP_NAME_SZ_MAX + 1 );
902 retrotile_parser_mstate( parser, MTILESTATE_TPROP );
903 }
904
905cleanup:
906
907 mdata_vector_unlock( parser->p_tile_defs );
908
909 if( MERROR_PREEMPT == retval ) {
910 /* Reset custom callback retval. */
911 retval = MERROR_OK;
912 }
913
914 return retval;
915}
916
917/* === */
918
919MERROR_RETVAL retrotile_parser_parse_token(
920 void* jparser_void, const char* token, size_t token_sz, void* parser_arg
921) {
922 MERROR_RETVAL retval = MERROR_OK;
923 struct RETROTILE_LAYER* tiles_layer = NULL;
924 struct RETROTILE_PARSER* parser = (struct RETROTILE_PARSER*)parser_arg;
925 retroflat_tile_t* tiles = NULL;
926
927 /* Try the custom parser. */
928 if(
929 NULL != parser->custom_token_cb &&
930 /* MERROR_PREEMPT is dealt with in cleanup. */
931 MERROR_OK != (retval = parser->custom_token_cb(
932 parser, token, token_sz, parser_arg ))
933 ) {
934 goto cleanup;
935 }
936
937 if( MJSON_PSTATE_LIST == mjson_parser_pstate( &(parser->jparser) ) ) {
938 /* We're parsing a list. What lists do we care about? */
939
940 /* Please note that this is for new tokens encountered inside of a list.
941 * For dealing with the closing of a list, see
942 * retrotile_json_close_list()!
943 */
944
945 if(
946 1 == parser->pass &&
947 MTILESTATE_LAYER_DATA == parser->mstate
948 ) {
949 /* This is a list of layer data... AKA tiles! */
950
951 /*
952 assert( NULL != *(parser->p_tilemap_h) );
953 maug_mlock( *(parser->p_tilemap_h), tilemap );
954 maug_cleanup_if_null_alloc( struct RETROTILE*, tilemap );
955 */
956
957 /*
958 debug_printf( RETROTILE_TRACE_LVL,
959 "selecting layer " SIZE_T_FMT "...",
960 parser->pass_layer_iter );
961 */
962 assert( NULL != parser->t );
963 tiles_layer = retrotile_get_layer_p(
964 parser->t, parser->pass_layer_iter );
965 assert( NULL != tiles_layer );
966
967 /* Apply the class parsed previously. */
968 tiles_layer->layer_class = parser->layer_class;
969
970 tiles = retrotile_get_tiles_p( tiles_layer );
971
972 /* Parse tilemap tile. */
973 /*
974 debug_printf( RETROTILE_TRACE_LVL,
975 "layer " SIZE_T_FMT " tile: " SIZE_T_FMT " (tiles: %p)",
976 parser->pass_layer_iter, parser->layer_tile_iter,
977 tiles );
978 */
979 assert( NULL != token );
980 assert( NULL != parser );
981 assert( NULL != tiles );
982
983 if(
984 parser->layer_tile_iter >=
985 parser->t->tiles_w * parser->t->tiles_h
986 ) {
987 /* Tile is outside of layer! */
988 error_printf(
989 "tile " SIZE_T_FMT " outside of layer tile buffer size "
990 SIZE_T_FMT "!",
991 parser->layer_tile_iter,
992 parser->t->tiles_w * parser->t->tiles_h );
993 retval = MERROR_OVERFLOW;
994 goto cleanup;
995 }
996
997 assert( 0 == tiles[parser->layer_tile_iter] );
998
999 tiles[parser->layer_tile_iter] = maug_atou32(
1000 token, token_sz, 10 );
1001
1002 }
1003 parser->layer_tile_iter++;
1004 goto cleanup;
1005
1006 } else if(
1007 MJSON_PSTATE_OBJECT_VAL ==
1008 mjson_parser_pstate( &(parser->jparser) )
1009 ) {
1010
1011 /* Pay attention to the retrotile_parser_mstate() calls here. We don't
1012 * have a stack of states, and we really only track a few we're
1013 * interested in. Even if we don't process their data here, we still
1014 * use retrotile_parser_mstate() to move the parser mstate back up
1015 * to the parent mstate when we're done with it. This kind of "fakes" a
1016 * stack (in a well-formed file, of course).
1017 */
1018
1019 if( MTILESTATE_TILESETS_FGID == parser->mstate ) {
1020 if( 1 == parser->pass ) {
1021 parser->t->tileset_fgid = maug_atou32( token, token_sz, 10 );
1022#if RETROTILE_TRACE_LVL > 0
1023 debug_printf(
1024 RETROTILE_TRACE_LVL, "tileset FGID set to: " SIZE_T_FMT,
1025 parser->t->tileset_fgid );
1026#endif /* RETROTILE_TRACE_LVL */
1027 }
1028 retrotile_parser_mstate( parser, MTILESTATE_TILESETS );
1029
1030 } else if( MTILESTATE_TILESETS_SRC == parser->mstate ) {
1031 if( 1 == parser->pass ) {
1032#if RETROTILE_TRACE_LVL > 0
1033 debug_printf( RETROTILE_TRACE_LVL, "parsing %s...", token );
1034#endif /* RETROTILE_TRACE_LVL */
1035 parser->tj_parse_cb(
1036 parser->dirname, token, NULL, parser->p_tile_defs,
1037 parser->wait_cb, parser->wait_data,
1038 parser->custom_token_cb, parser->custom_token_cb_data,
1039 parser->passes_max, parser->flags );
1040 }
1041 retrotile_parser_mstate( parser, MTILESTATE_TILESETS );
1042
1043 } else if( MTILESTATE_HEIGHT == parser->mstate ) {
1044 if( 0 == parser->pass ) {
1045 /* Need this to allocate on pass 1. */
1046 parser->tiles_h = maug_atou32( token, token_sz, 10 );
1047#if RETROTILE_TRACE_LVL > 0
1048 debug_printf(
1049 RETROTILE_TRACE_LVL, "tilemap height: " SIZE_T_FMT,
1050 parser->tiles_h );
1051#endif /* RETROTILE_TRACE_LVL */
1052 }
1053 retrotile_parser_mstate( parser, MTILESTATE_NONE );
1054
1055 } else if( MTILESTATE_WIDTH == parser->mstate ) {
1056 if( 0 == parser->pass ) {
1057 /* Need this to allocate on pass 1. */
1058 parser->tiles_w = maug_atou32( token, token_sz, 10 );
1059#if RETROTILE_TRACE_LVL > 0
1060 debug_printf(
1061 RETROTILE_TRACE_LVL, "tilemap width: " SIZE_T_FMT,
1062 parser->tiles_w );
1063#endif /* RETROTILE_TRACE_LVL */
1064 }
1065 retrotile_parser_mstate( parser, MTILESTATE_NONE );
1066
1067 } else if( MTILESTATE_LAYER_NAME == parser->mstate ) {
1068 /* TODO: Store */
1069 retrotile_parser_mstate( parser, MTILESTATE_LAYER );
1070
1071 } else if( MTILESTATE_LAYER_CLASS == parser->mstate ) {
1072 /* TODO: Use the class table to create layers for e.g. crops, items. */
1073 if( 0 == maug_strncmp( "mobile", token, 7 ) ) {
1074#if RETROTILE_TRACE_LVL > 0
1075 debug_printf( RETROTILE_TRACE_LVL,
1076 "layer " SIZE_T_FMT " type: mobile",
1077 parser->pass_layer_iter );
1078#endif /* RETROTILE_TRACE_LVL */
1079 parser->layer_class = RETROTILE_CLASS_MOBILE;
1080 } else {
1081#if RETROTILE_TRACE_LVL > 0
1082 debug_printf( RETROTILE_TRACE_LVL,
1083 "layer " SIZE_T_FMT " type: tile",
1084 parser->pass_layer_iter );
1085#endif /* RETROTILE_TRACE_LVL */
1086 parser->layer_class = RETROTILE_CLASS_TILE;
1087 }
1088 retrotile_parser_mstate( parser, MTILESTATE_LAYER );
1089
1090 } else if( MTILESTATE_PROP_NAME == parser->mstate ) {
1091 maug_mzero( parser->last_prop_name, RETROTILE_PROP_NAME_SZ_MAX + 1 );
1092 maug_strncpy(
1094 parser->last_prop_name_sz = token_sz;
1095 retrotile_parser_mstate( parser, MTILESTATE_PROP );
1096
1097 } else if( MTILESTATE_PROP_TYPE == parser->mstate ) {
1098#if RETROTILE_TRACE_LVL > 0
1099 debug_printf( RETROTILE_TRACE_LVL, "property %s is type: %s",
1100 parser->last_prop_name, token );
1101#endif /* RETROTILE_TRACE_LVL */
1102 parser->last_prop_type = retrotile_parse_prop_type( token, token_sz );
1103 retrotile_parser_mstate( parser, MTILESTATE_PROP );
1104
1105 } else if( MTILESTATE_PROP_VAL == parser->mstate ) {
1106 /* We're dealing with properties of the tilemap. */
1107 if( 0 == maug_strncmp( parser->last_prop_name, "name", 5 ) ) {
1108#if RETROTILE_TRACE_LVL > 0
1109 debug_printf( RETROTILE_TRACE_LVL, "tilemap name: %s", token );
1110#endif /* RETROTILE_TRACE_LVL */
1111 maug_strncpy( parser->tilemap_name, token, RETROTILE_NAME_SZ_MAX );
1112 }
1113
1114 maug_mzero( parser->last_prop_name, RETROTILE_PROP_NAME_SZ_MAX + 1 );
1115 retrotile_parser_mstate( parser, MTILESTATE_PROP );
1116 }
1117 goto cleanup;
1118 }
1119
1120 retrotile_parser_match_token( token, token_sz, parser );
1121
1122cleanup:
1123
1124 return retval;
1125}
1126
1127/* === */
1128
1129MERROR_RETVAL retrotile_json_close_list(
1130 struct MJSON_PARSER* jparser, void* parg
1131) {
1132 struct RETROTILE_PARSER* parser = (struct RETROTILE_PARSER*)parg;
1133
1134 if( MTILESTATE_LAYER_DATA == parser->mstate ) {
1135 assert( RETROTILE_PARSER_MODE_MAP == parser->mode );
1136#if RETROTILE_TRACE_LVL > 0
1137 debug_printf( RETROTILE_TRACE_LVL, "parsed " SIZE_T_FMT " tiles!",
1138 parser->layer_tile_iter );
1139#endif /* RETROTILE_TRACE_LVL */
1140 assert( parser->layer_tile_iter > 0 );
1141 retrotile_parser_mstate( parser, MTILESTATE_LAYER );
1142
1143 } else if( MTILESTATE_LAYERS == parser->mstate ) {
1144 assert( RETROTILE_PARSER_MODE_MAP == parser->mode );
1145 retrotile_parser_mstate( parser, MTILESTATE_NONE );
1146
1147 } else if( MTILESTATE_TILESETS == parser->mstate ) {
1148 assert( RETROTILE_PARSER_MODE_MAP == parser->mode );
1149 retrotile_parser_mstate( parser, MTILESTATE_NONE );
1150
1151 } else if( MTILESTATE_TILES_PROP == parser->mstate ) {
1152 assert( RETROTILE_PARSER_MODE_DEFS == parser->mode );
1153 retrotile_parser_mstate( parser, MTILESTATE_TILES );
1154
1155 } else if( MTILESTATE_TILES == parser->mstate ) {
1156 assert( RETROTILE_PARSER_MODE_DEFS == parser->mode );
1157 retrotile_parser_mstate( parser, MTILESTATE_NONE );
1158
1159 } else if( MTILESTATE_PROP == parser->mstate ) {
1160 retrotile_parser_mstate( parser, MTILESTATE_NONE );
1161
1162 } else if( MTILESTATE_TPROP == parser->mstate ) {
1163 retrotile_parser_mstate( parser, MTILESTATE_NONE );
1164 }
1165
1166 return MERROR_OK;
1167}
1168
1169/* === */
1170
1171MERROR_RETVAL retrotile_json_open_obj(
1172 struct MJSON_PARSER* jparser, void* parg
1173) {
1174 struct RETROTILE_PARSER* parser = (struct RETROTILE_PARSER*)parg;
1175
1176 if( MTILESTATE_LAYERS == parser->mstate ) {
1177 assert( RETROTILE_PARSER_MODE_MAP == parser->mode );
1178 /* Reset on open so count is retained for allocating after first
1179 * pass. */
1180 parser->layer_tile_iter = 0;
1181 retrotile_parser_mstate( parser, MTILESTATE_LAYER );
1182 }
1183
1184 return MERROR_OK;
1185}
1186
1187/* === */
1188
1189MERROR_RETVAL retrotile_json_close_obj(
1190 struct MJSON_PARSER* jparser, void* parg
1191) {
1192 struct RETROTILE_PARSER* parser = (struct RETROTILE_PARSER*)parg;
1193
1194 if( MTILESTATE_LAYER == parser->mstate ) {
1195 assert( RETROTILE_PARSER_MODE_MAP == parser->mode );
1196#if RETROTILE_TRACE_LVL > 0
1197 debug_printf( RETROTILE_TRACE_LVL,
1198 "incrementing pass layer to " SIZE_T_FMT " after " SIZE_T_FMT
1199 " tiles...",
1200 parser->pass_layer_iter + 1, parser->layer_tile_iter );
1201#endif /* RETROTILE_TRACE_LVL */
1202 parser->pass_layer_iter++;
1203 retrotile_parser_mstate( parser, MTILESTATE_LAYERS );
1204
1205 } else if( MTILESTATE_GRID == parser->mstate ) {
1206 retrotile_parser_mstate( parser, MTILESTATE_NONE );
1207 }
1208
1209 return MERROR_OK;
1210}
1211
1212/* === */
1213
1214int retrotile_parse_prop_type( const char* token, size_t token_sz ) {
1215 int out = RETROTILE_PROP_TYPE_OTHER;
1216
1217 if( 0 == maug_strncmp( "string", token, 7 ) ) {
1219 } else if( 0 == maug_strncmp( "file", token, 5 ) ) {
1221 } else if( 0 == maug_strncmp( "int", token, 4 ) ) {
1223 }
1224
1225 return out;
1226}
1227
1228/* === */
1229
1230mfix_t retrotile_static_rotation_from_dir( const char* dir ) {
1231 mfix_t static_rotate_out = 0;
1232
1233 if( NULL == dir ) {
1234 return 0;
1235 }
1236
1237 /* Translate dir into rotation value. */
1238 if( 0 == maug_strncmp( dir, "NW", 3 ) ) {
1239 static_rotate_out = mfix_from_f( 90.0f );
1240 } else if( 0 == maug_strncmp( dir, "SW", 3 ) ) {
1241 static_rotate_out = mfix_from_f( 180.0f );
1242 } else if( 0 == maug_strncmp( dir, "SE", 3 ) ) {
1243 static_rotate_out = mfix_from_f( 270.0f );
1244
1245 } else if( 0 == maug_strncmp( dir, "W", 2 ) ) {
1246 static_rotate_out = mfix_from_f( 90.0f );
1247 } else if( 0 == maug_strncmp( dir, "S", 2 ) ) {
1248 static_rotate_out = mfix_from_f( 180.0f );
1249 } else if( 0 == maug_strncmp( dir, "E", 2 ) ) {
1250 static_rotate_out = mfix_from_f( 270.0f );
1251
1252 } else {
1253 static_rotate_out = 0;
1254 }
1255
1256 return static_rotate_out;
1257}
1258
1259/* === */
1260
1262 const char* dirname, const char* filename, MAUG_MHANDLE* p_tilemap_h,
1263 struct MDATA_VECTOR* p_tile_defs, mparser_wait_cb_t wait_cb, void* wait_data,
1264 mparser_parse_token_cb token_cb, void* token_cb_data, uint8_t passes,
1265 uint8_t flags
1266) {
1267 MERROR_RETVAL retval = MERROR_OK;
1268 MAUG_MHANDLE parser_h = (MAUG_MHANDLE)NULL;
1269 struct RETROTILE_PARSER* parser = NULL;
1270 char filename_path[MAUG_PATH_SZ_MAX];
1271 mfile_t tile_file;
1272 char c;
1273 char* filename_ext = NULL;
1274
1275 /* Initialize parser. */
1276 parser_h = maug_malloc( 1, sizeof( struct RETROTILE_PARSER ) );
1277 maug_cleanup_if_null_alloc( MAUG_MHANDLE, parser_h );
1278
1279 maug_mlock( parser_h, parser );
1280 maug_cleanup_if_null_alloc( struct RETROTILE_PARSER*, parser );
1281 maug_mzero( parser, sizeof( struct RETROTILE_PARSER ) );
1282
1283 parser->flags = flags;
1284 parser->tj_parse_cb = retrotile_parse_json_file;
1285 parser->custom_token_cb = token_cb;
1286 parser->custom_token_cb_data = token_cb_data;
1287
1288 maug_strncpy( parser->dirname, dirname, MAUG_PATH_SZ_MAX );
1289
1290 if( 2 > passes ) {
1291#if RETROTILE_TRACE_LVL > 0
1292 debug_printf( RETROTILE_TRACE_LVL,
1293 "increasing parse passes to minimum, 2!" );
1294#endif /* RETROTILE_TRACE_LVL */
1295 passes = 2;
1296 }
1297
1298 parser->passes_max = passes;
1299
1300 /* Setup filename path. */
1301 maug_mzero( filename_path, MAUG_PATH_SZ_MAX );
1302 /* TODO: Configurable path. */
1303 maug_snprintf(
1304 filename_path, MAUG_PATH_SZ_MAX, "%s/%s", dirname, filename );
1305
1306#if RETROTILE_TRACE_LVL > 0
1307 debug_printf( RETROTILE_TRACE_LVL, "opening %s...", filename_path );
1308#endif /* RETROTILE_TRACE_LVL */
1309
1310 retval = mfile_open_read( filename_path, &tile_file );
1311 maug_cleanup_if_not_ok();
1312
1313 /* Parse JSON and react to state. */
1314 for( parser->pass = 0 ; passes > parser->pass ; parser->pass++ ) {
1315#if RETROTILE_TRACE_LVL > 0
1316 debug_printf( RETROTILE_TRACE_LVL, "beginning pass #%u...",
1317 parser->pass );
1318#endif /* RETROTILE_TRACE_LVL */
1319
1320 /* Reset tilemap parser. */
1321 parser->mstate = 0;
1322
1323 /* Reset JSON parser. */
1324 maug_mzero( &(parser->jparser.base), sizeof( struct MJSON_PARSER ) );
1325
1326 parser->wait_cb = wait_cb;
1327 parser->wait_data = wait_data;
1328 parser->jparser.base.wait_cb = wait_cb;
1329 parser->jparser.base.wait_data = wait_data;
1330
1331 /* Figure out if we're parsing a .tmj or .tsj. */
1332 filename_ext = maug_strrchr( filename, '.' );
1333 if( NULL == filename_ext ) {
1334 error_printf( "could not parse filename extension!" );
1335 retval = MERROR_FILE;
1336 goto cleanup;
1337 }
1338 if( 's' == filename_ext[2] ) {
1339#if RETROTILE_TRACE_LVL > 0
1340 debug_printf( RETROTILE_TRACE_LVL,
1341 "(tile_defs pass %u)", parser->pass );
1342#endif /* RETROTILE_TRACE_LVL */
1344 parser->jparser.token_parser = retrotile_parser_parse_tiledef_token;
1345 parser->jparser.token_parser_arg = parser;
1346 parser->jparser.close_list = retrotile_json_close_list;
1347 parser->jparser.close_list_arg = parser;
1348 parser->jparser.close_obj = retrotile_json_close_obj;
1349 parser->jparser.close_obj_arg = parser;
1350 /*
1351 parser->jparser.base.close_val = retrotile_json_close_val;
1352 parser->jparser.base.close_val_arg = parser;
1353 */
1354 parser->p_tile_defs = p_tile_defs;
1355
1356 assert( NULL != p_tile_defs );
1357 if( 1 == parser->pass ) {
1358 /* Allocate tile defs based on highest tile ID found on
1359 * first pass.
1360 */
1361 assert( 0 < parser->tileset_id_cur );
1362#if RETROTILE_TRACE_LVL > 0
1363 debug_printf(
1364 RETROTILE_TRACE_LVL, "allocating " SIZE_T_FMT " tile defs...",
1365 parser->tileset_id_cur + 1 );
1366#endif /* RETROTILE_TRACE_LVL */
1367 mdata_vector_fill(
1368 parser->p_tile_defs, parser->tileset_id_cur + 1,
1369 sizeof( struct RETROTILE_TILE_DEF ) );
1370 }
1371 } else {
1372#if RETROTILE_TRACE_LVL > 0
1373 debug_printf( RETROTILE_TRACE_LVL, "(tilemap pass %u)", parser->pass );
1374#endif /* RETROTILE_TRACE_LVL */
1376
1377 parser->jparser.close_list = retrotile_json_close_list;
1378 parser->jparser.close_list_arg = parser;
1379 parser->jparser.open_obj = retrotile_json_open_obj;
1380 parser->jparser.open_obj_arg = parser;
1381 parser->jparser.close_obj = retrotile_json_close_obj;
1382 parser->jparser.close_obj_arg = parser;
1383 parser->jparser.token_parser = retrotile_parser_parse_token;
1384 parser->jparser.token_parser_arg = parser;
1385 parser->p_tile_defs = p_tile_defs;
1386
1387 assert( NULL != p_tilemap_h );
1388 assert( NULL != p_tile_defs );
1389 if( 1 == parser->pass ) {
1390 /* Allocate tiles for the new layers. */
1391 retval = retrotile_alloc(
1392 p_tilemap_h, parser->tiles_w, parser->tiles_h,
1393 parser->pass_layer_iter, parser->tilemap_name,
1394 parser->tileset_name );
1395 maug_cleanup_if_not_ok();
1396 maug_mlock( *p_tilemap_h, parser->t );
1397 }
1398 parser->pass_layer_iter = 0;
1399 }
1400
1401 while( tile_file.has_bytes( &tile_file ) ) {
1402 tile_file.read_int( &tile_file, (uint8_t*)&c, 1, 0 );
1403#if RETROTILE_TRACE_CHARS > 0
1404 debug_printf( RETROTILE_TRACE_CHARS, "%c", c );
1405#endif /* RETROTILE_TRACE_CHARS */
1406 retval = mjson_parse_c( &(parser->jparser), c );
1407 if( MERROR_OK != retval ) {
1408 error_printf( "error parsing JSON!" );
1409 goto cleanup;
1410 }
1411 }
1412
1413 tile_file.seek( &tile_file, 0 );
1414
1415 filename_ext = maug_strrchr( filename, '.' );
1416 if( NULL == filename_ext ) {
1417 error_printf( "could not parse filename extension!" );
1418 retval = MERROR_FILE;
1419 goto cleanup;
1420 }
1421 if( 's' != filename_ext[2] ) {
1422#if RETROTILE_TRACE_LVL > 0
1423 debug_printf( RETROTILE_TRACE_LVL,
1424 "pass %u found " SIZE_T_FMT " layers",
1425 parser->pass, parser->pass_layer_iter );
1426#endif /* RETROTILE_TRACE_LVL */
1427 }
1428 }
1429
1430#if RETROTILE_TRACE_LVL > 0
1431 debug_printf(
1432 RETROTILE_TRACE_LVL, "finished parsing %s, retval: %d",
1433 filename_path, retval );
1434#endif /* RETROTILE_TRACE_LVL */
1435
1436cleanup:
1437
1438 if( NULL != parser ) {
1439 if( NULL != parser->t ) {
1440 maug_munlock( *p_tilemap_h, parser->t );
1441 }
1442 maug_munlock( parser_h, parser );
1443 }
1444
1445 if( (MAUG_MHANDLE)NULL != parser_h ) {
1446 maug_mfree( parser_h );
1447 }
1448
1449 return retval;
1450}
1451
1452/* === */
1453
1454static retroflat_tile_t retrotile_gen_diamond_square_rand(
1455 retroflat_tile_t min_z, retroflat_tile_t max_z, uint32_t tuning,
1456 retroflat_tile_t top_left_z
1457) {
1458 retroflat_tile_t avg = top_left_z;
1459
1460 if( 8 > rand() % 10 ) {
1461 /* avg = min_z + (rand() % (max_z - min_z)); */
1462 avg -= (min_z / tuning) + (rand() % (max_z / tuning));
1463 /* } else {
1464 avg += (min_z / 10) + (rand() % (max_z / 10)); */
1465 }
1466
1467 /* Clamp the result. */
1468
1469 if( min_z > avg ) {
1470 avg = min_z;
1471 }
1472
1473 if( max_z < avg ) {
1474 avg = max_z;
1475 }
1476
1477 return avg;
1478}
1479
1480/* === */
1481
1482static void retrotile_gen_diamond_square_corners(
1483 int16_t corners_x[2][2], int16_t corners_y[2][2],
1485 uint32_t tuning, struct RETROTILE_DATA_DS* data_ds,
1486 struct RETROTILE_LAYER* layer, struct RETROTILE* t
1487) {
1488 int16_t iter_x = 0,
1489 iter_y = 0;
1490 retroflat_tile_t* tile_iter = NULL;
1491 retroflat_tile_t top_left_z = 0;
1492
1493 /* Generate missing corner data. Loop through X/Y coords stored in
1494 * corners_x/corners_y convenience arrays.
1495 */
1496 for( iter_y = 0 ; iter_y < 2 ; iter_y++ ) {
1497 for( iter_x = 0 ; iter_x < 2 ; iter_x++ ) {
1498
1499 /* Make sure corner X is in bounds. */
1500 corners_x[iter_x][iter_y] =
1501 (data_ds->sect_x - 1) + (iter_x * data_ds->sect_w);
1502 if( 0 > corners_x[iter_x][iter_y] ) {
1503 corners_x[iter_x][iter_y] += 1;
1504 }
1505
1506 /* Make sure corner Y is in bounds. */
1507 corners_y[iter_x][iter_y] =
1508 (data_ds->sect_y - 1) + (iter_y * data_ds->sect_h);
1509 if( 0 > corners_y[iter_x][iter_y] ) {
1510 corners_y[iter_x][iter_y] += 1;
1511 }
1512 }
1513 }
1514
1515 /* Should be handled by the check above. */
1516 assert( 0 <= corners_x[0][0] );
1517 assert( 0 <= corners_y[0][0] );
1518 assert( t->tiles_w > corners_x[0][0] );
1519 assert( t->tiles_h > corners_y[0][0] );
1520
1521 /* Grab the top-left Z-value to anchor generated corners to. */
1522 top_left_z = retrotile_get_tile(
1523 t, layer,
1524 corners_x[0][0],
1525 corners_y[0][0] );
1526
1527 if( 0 > top_left_z ) {
1528 retrotile_get_tile(
1529 t, layer,
1530 corners_x[0][0] >= 0 ? corners_x[0][0] : 0,
1531 corners_y[0][0] >= 0 ? corners_y[0][0] : 0 ) = max_z;
1532 top_left_z = max_z;
1533 }
1534
1535 /* Fill in missing corners. */
1536 for( iter_y = 0 ; iter_y < 2 ; iter_y++ ) {
1537 for( iter_x = 0 ; iter_x < 2 ; iter_x++ ) {
1538 /* Grab a pointer to the corner so we can modify it easily. */
1539 tile_iter = &(retrotile_get_tile(
1540 t, layer,
1541 corners_x[iter_x][iter_y],
1542 corners_y[iter_x][iter_y] ));
1543
1544 /* Check if corner is already filled in. */
1545 if( -1 != *tile_iter ) {
1546#if RETROTILE_TRACE_LVL > 0
1547 debug_printf(
1548 RETROTILE_TRACE_LVL, "corner coord %d x %d present: %d",
1549 corners_x[iter_x][iter_y], corners_y[iter_x][iter_y],
1550 retrotile_get_tile(
1551 t, layer,
1552 corners_x[iter_x][iter_y],
1553 corners_y[iter_x][iter_y] ) );
1554#endif /* RETROTILE_TRACE_LVL */
1555 continue;
1556 }
1557
1558 /* Generate a new value for this corner. */
1559 *tile_iter = retrotile_gen_diamond_square_rand(
1560 min_z, max_z, tuning, top_left_z );
1561
1562#if RETROTILE_TRACE_LVL > 0
1563 debug_printf( RETROTILE_TRACE_LVL,
1564 "missing corner coord %d x %d: %d",
1565 corners_x[iter_x][iter_y], corners_y[iter_x][iter_y],
1566 *tile_iter );
1567#endif /* RETROTILE_TRACE_LVL */
1568 }
1569 }
1570}
1571
1572/* === */
1573
1574static retroflat_tile_t retrotile_gen_diamond_square_avg(
1575 int16_t corners_x[2][2], int16_t corners_y[2][2],
1576 struct RETROTILE* t, struct RETROTILE_LAYER* layer
1577) {
1578 retroflat_tile_t* tile_iter = NULL;
1579 int16_t iter_x = 0,
1580 iter_y = 0;
1581 retroflat_tile_t avg = 0;
1582
1583 /* Average corner data. */
1584 for( iter_y = 0 ; 2 > iter_y ; iter_y++ ) {
1585 for( iter_x = 0 ; 2 > iter_x ; iter_x++ ) {
1586 tile_iter = &(retrotile_get_tile(
1587 t, layer,
1588 corners_x[iter_x][iter_y],
1589 corners_y[iter_x][iter_y] ));
1590 assert( -1 != *tile_iter );
1591 /*
1592 debug_printf(
1593 RETROTILE_TRACE_LVL, "%d: adding from coords %d x %d: %d",
1594 iter_depth,
1595 corners_x[iter_x][iter_y], corners_y[iter_x][iter_y],
1596 *tile_iter ); */
1597 avg += *tile_iter;
1598 }
1599 }
1600
1601 /* TODO: Use right shift? */
1602 avg /= 4;
1603
1604 return avg;
1605}
1606
1607/* === */
1608
1610 struct RETROTILE* t, retroflat_tile_t min_z, retroflat_tile_t max_z,
1611 uint32_t tuning, size_t layer_idx, uint8_t flags, void* data,
1612 retrotile_ani_cb animation_cb, void* animation_cb_data
1613) {
1614 int16_t iter_x = 0,
1615 iter_y = 0
1616#if RETROTILE_TRACE_LVL > 0
1617 , iter_depth = 0;
1618#else
1619 ;
1620#endif /* RETROTILE_TRACE_LVL */
1621 int16_t corners_x[2][2];
1622 int16_t corners_y[2][2];
1623 int32_t avg = 0;
1624 /* size_t tile_idx = 0; */
1625 struct RETROTILE_DATA_DS data_ds_sub;
1626 MAUG_MHANDLE data_ds_h = (MAUG_MHANDLE)NULL;
1627 struct RETROTILE_DATA_DS* data_ds = NULL;
1628 /* retroflat_tile_t* tiles = NULL; */
1629 MERROR_RETVAL retval = MERROR_OK;
1630 struct RETROTILE_LAYER* layer = NULL;
1631 retroflat_tile_t* tile_iter = NULL;
1632 uint8_t free_ds_data = 0;
1633#ifdef MAUG_NO_STDLIB
1634 size_t i = 0;
1635#endif /* MAUG_NO_STDLIB */
1636
1637 /*
1638 maug_mlock( t->tiles, tiles );
1639 maug_cleanup_if_null_alloc( struct GRIDCITY_TILE*, tiles );
1640 */
1641
1642 #define _retrotile_ds_update_statistics( data, tile ) \
1643 /* Gather statistics. */ \
1644 if( (data)->highest_generated < (tile) && 32767 > (tile) ) { \
1645 (data)->highest_generated = (tile); \
1646 } \
1647 if( (data)->lowest_generated > (tile) && 0 < (tile) ) { \
1648 (data)->lowest_generated = (tile); \
1649 }
1650
1651 layer = retrotile_get_layer_p( t, layer_idx );
1652
1653 if(
1654 NULL == data ||
1656 ) {
1657 /* This must be the first call, so initialize or allocate a new struct.
1658 */
1659 if( NULL == data ) {
1660 /* An internal struct needs to be allocated before initialization. */
1661 data_ds_h = maug_malloc( 1, sizeof( struct RETROTILE_DATA_DS ) );
1662 maug_cleanup_if_null_alloc( MAUG_MHANDLE, data_ds_h );
1663 free_ds_data = 1;
1664 maug_mlock( data_ds_h, data_ds );
1665 maug_cleanup_if_null_alloc( struct RETROTILE_DATA_DS*, data_ds );
1666 } else {
1667 data_ds = (struct RETROTILE_DATA_DS*)data;
1668 }
1669
1670 /* Initialize passed tilemap while we're handling first call stuff. */
1671 retrotile_clear_tiles( t, layer, i );
1672
1673 /* Initialize DS struct from tilemap properties. */
1674 maug_mzero( data_ds, sizeof( struct RETROTILE_DATA_DS ) );
1675 data_ds->sect_w = t->tiles_w;
1676 data_ds->sect_h = t->tiles_h;
1677 data_ds->sect_w_half = data_ds->sect_w >> 1;
1678 data_ds->sect_h_half = data_ds->sect_h >> 1;
1679 data_ds->lowest_generated = 32767;
1680
1681 /* Disable this flag for subsequent calls. */
1683 } else {
1684 data_ds = (struct RETROTILE_DATA_DS*)data;
1685 }
1686 assert( NULL != data_ds );
1687
1688 /* Trivial case; end recursion. */
1689 if( 0 == data_ds->sect_w ) {
1690#if RETROTILE_TRACE_LVL > 0
1691 debug_printf(
1692 RETROTILE_TRACE_LVL, "%d return: null sector", iter_depth );
1693#endif /* RETROTILE_TRACE_LVL */
1694 goto cleanup;
1695 }
1696
1697 if(
1698 data_ds->sect_x + data_ds->sect_w > t->tiles_w ||
1699 data_ds->sect_y + data_ds->sect_h > t->tiles_h
1700 ) {
1701#if RETROTILE_TRACE_LVL > 0
1702 debug_printf(
1703 RETROTILE_TRACE_LVL, "%d return: overflow sector", iter_depth );
1704#endif /* RETROTILE_TRACE_LVL */
1705 goto cleanup;
1706 }
1707
1708#if RETROTILE_TRACE_LVL > 0
1709 iter_depth = t->tiles_w / data_ds->sect_w;
1710#endif /* RETROTILE_TRACE_LVL */
1711
1712 /* Generate/grab corners before averaging them! */
1713 retrotile_gen_diamond_square_corners(
1714 corners_x, corners_y, min_z, max_z, tuning, data_ds, layer, t );
1715
1716 if( 2 == data_ds->sect_w || 2 == data_ds->sect_h ) {
1717 /* Nothing to average, this sector is just corners! */
1718#if RETROTILE_TRACE_LVL > 0
1719 debug_printf(
1721 "%d return: reached innermost point", iter_depth );
1722#endif /* RETROTILE_TRACE_LVL */
1723 goto cleanup; /* Skip further descent regardless. */
1724 }
1725
1726 avg =
1727 retrotile_gen_diamond_square_avg( corners_x, corners_y, t, layer );
1728
1729#if RETROTILE_TRACE_LVL > 0
1730 debug_printf( RETROTILE_TRACE_LVL, "avg: " S32_FMT, avg );
1731#endif /* RETROTILE_TRACE_LVL */
1732
1733 tile_iter = &(retrotile_get_tile(
1734 t, layer,
1735 data_ds->sect_x + data_ds->sect_w_half,
1736 data_ds->sect_y + data_ds->sect_h_half ));
1737 if( -1 != *tile_iter ) {
1738#if RETROTILE_TRACE_LVL > 0
1739 debug_printf( RETROTILE_TRACE_LVL, "avg already present at %d x %d!",
1740 data_ds->sect_x + data_ds->sect_w_half,
1741 data_ds->sect_y + data_ds->sect_h_half );
1742#endif /* RETROTILE_TRACE_LVL */
1743 goto cleanup;
1744 }
1745 *tile_iter = avg;
1746 _retrotile_ds_update_statistics( data_ds, avg );
1747
1748 /* assert( 0 <= tiles[tile_idx].terrain );
1749
1750 maug_munlock( city->tiles, tiles );
1751 tiles = NULL; */
1752
1753 /* Recurse into subsectors. */
1754 for(
1755 iter_y = data_ds->sect_y ;
1756 iter_y < (data_ds->sect_y + data_ds->sect_h) ;
1757 iter_y++
1758 ) {
1759 for(
1760 iter_x = data_ds->sect_x ;
1761 iter_x < (data_ds->sect_x + data_ds->sect_w) ;
1762 iter_x++
1763 ) {
1764 data_ds_sub.sect_x = data_ds->sect_x + iter_x;
1765
1766 data_ds_sub.sect_y = data_ds->sect_y + iter_y;
1767
1768 data_ds_sub.sect_w = data_ds->sect_w_half;
1769 data_ds_sub.sect_h = data_ds->sect_h_half;
1770 data_ds_sub.sect_w_half = data_ds_sub.sect_w >> 1;
1771 data_ds_sub.sect_h_half = data_ds_sub.sect_h >> 1;
1772 data_ds_sub.lowest_generated = 32767;
1773 data_ds_sub.highest_generated = 0;
1774
1775#if RETROTILE_TRACE_LVL > 0
1776 debug_printf(
1777 RETROTILE_TRACE_LVL, "%d: child sector at %d x %d, %d wide",
1778 iter_depth,
1779 data_ds_sub.sect_x, data_ds_sub.sect_y, data_ds_sub.sect_w );
1780#endif /* RETROTILE_TRACE_LVL */
1781
1783 t, min_z, max_z, tuning, layer_idx, flags, &data_ds_sub,
1784 animation_cb, animation_cb_data );
1785 maug_cleanup_if_not_ok();
1786
1787 _retrotile_ds_update_statistics(
1788 data_ds, data_ds_sub.highest_generated );
1789 _retrotile_ds_update_statistics(
1790 data_ds, data_ds_sub.lowest_generated );
1791 }
1792 }
1793
1794 if(
1795 data_ds->sect_w == (t->tiles_w >> 1) &&
1796 NULL != animation_cb
1797 ) {
1798 retval = animation_cb( animation_cb_data, iter_y );
1799 maug_cleanup_if_not_ok();
1800 }
1801
1802#if RETROTILE_TRACE_LVL > 0
1803 debug_printf(
1804 RETROTILE_TRACE_LVL, "%d return: all sectors complete", iter_depth );
1805#endif /* RETROTILE_TRACE_LVL */
1806
1807cleanup:
1808
1809 if( free_ds_data && NULL != data_ds ) {
1810 maug_munlock( data_ds_h, data_ds );
1811 }
1812
1813 if( free_ds_data && (MAUG_MHANDLE)NULL != data_ds_h ) {
1814 maug_mfree( data_ds_h );
1815 }
1816
1817 return retval;
1818}
1819
1820/* === */
1821
1823 struct RETROTILE* t, retroflat_tile_t min_z, retroflat_tile_t max_z,
1824 uint32_t tuning, size_t layer_idx, uint8_t flags, void* data,
1825 retrotile_ani_cb animation_cb, void* animation_cb_data
1826) {
1827 size_t x = 0,
1828 y = 0;
1829 int16_t offset_x = 0,
1830 offset_y = 0,
1831 finished = 0;
1832 MERROR_RETVAL retval = MERROR_OK;
1833 struct RETROTILE_LAYER* layer = NULL;
1834 int16_t spb = RETROTILE_VORONOI_DEFAULT_SPB;
1835 int16_t drift = RETROTILE_VORONOI_DEFAULT_DRIFT;
1836 MAUG_MHANDLE temp_grid_h = (MAUG_MHANDLE)NULL;
1837 retroflat_tile_t* temp_grid = NULL;
1838 retroflat_tile_t* tiles = NULL;
1839 /* Only use 4 cardinal directions. */
1840 int8_t side_iter = 0;
1841#ifdef MAUG_NO_STDLIB
1842 size_t i = 0;
1843#endif /* MAUG_NO_STDLIB */
1844
1845 layer = retrotile_get_layer_p( t, 0 );
1846
1847 tiles = retrotile_get_tiles_p( layer );
1848
1849 /* Initialize grid to empty. */
1850 retrotile_clear_tiles( t, layer, i );
1851
1852 /* Generate the initial sector starting points. */
1853 for( y = 0 ; t->tiles_w > y ; y += spb ) {
1854 for( x = 0 ; t->tiles_w > x ; x += spb ) {
1855 offset_x = x + ((drift * -1) + (rand() % drift));
1856 offset_y = y + ((drift * -1) + (rand() % drift));
1857
1858 /* Clamp sector offsets onto map borders. */
1859 if( 0 > offset_x ) {
1860 offset_x = 0;
1861 }
1862 if( offset_x >= t->tiles_w ) {
1863 offset_x = t->tiles_w - 1;
1864 }
1865 if( 0 > offset_y ) {
1866 offset_y = 0;
1867 }
1868 if( offset_y >= t->tiles_h ) {
1869 offset_y = t->tiles_h - 1;
1870 }
1871
1872 retrotile_get_tile( t, layer, offset_x, offset_y ) =
1873 min_z + (rand() % max_z);
1874 }
1875 }
1876
1877 temp_grid_h = maug_malloc(
1878 sizeof( retroflat_tile_t ), t->tiles_w * t->tiles_h );
1879 maug_cleanup_if_null_alloc( MAUG_MHANDLE, temp_grid_h );
1880
1881 maug_mlock( temp_grid_h, temp_grid );
1882 maug_cleanup_if_null_alloc( retroflat_tile_t*, temp_grid );
1883
1884 /* Grow the sector starting points. */
1885 while( !finished ) {
1886 if( NULL != animation_cb ) {
1887 retval = animation_cb( animation_cb_data, -1 );
1888 maug_cleanup_if_not_ok();
1889 }
1890
1891 /* Prepare sampling grid so we're working from unexpanded sections
1892 * below.
1893 */
1894 memcpy(
1895 temp_grid, tiles,
1896 sizeof( retroflat_tile_t ) * t->tiles_w * t->tiles_h );
1897
1898 /* Starting another pass, assume finished until proven otherwise. */
1899 finished = 1;
1900 for( y = 0 ; t->tiles_h > y ; y++ ) {
1901 for( x = 0 ; t->tiles_w > x ; x++ ) {
1902 if( -1 == retrotile_get_tile( t, layer, x, y ) ) {
1903 /* If there are still unfilled tiles, we're not finished
1904 * yet!
1905 */
1906 finished = 0;
1907
1908 /* Skip filled tile. */
1909 continue;
1910 }
1911
1912
1913 for( side_iter = 0 ; 4 > side_iter ; side_iter++ ) {
1914#if RETROTILE_TRACE_LVL > 0
1915 debug_printf( RETROTILE_TRACE_LVL,
1916 SIZE_T_FMT " (%d), " SIZE_T_FMT " (%d) ("
1917 SIZE_T_FMT ", " SIZE_T_FMT ")",
1918 x,
1919 gc_retroflat_offsets4_x[side_iter],
1920 y,
1921 gc_retroflat_offsets4_y[side_iter],
1922 t->tiles_w, t->tiles_h );
1923#endif /* RETROTILE_TRACE_LVL */
1924
1925 /* Iterate through directions to expand. */
1926 /* TODO: Add tuning to select directional probability. */
1927 if(
1928 t->tiles_w > x + gc_retroflat_offsets4_x[side_iter] &&
1929 t->tiles_h > y + gc_retroflat_offsets4_y[side_iter] &&
1930 -1 == temp_grid[
1931 ((y + gc_retroflat_offsets4_y[side_iter]) *
1932 t->tiles_w) +
1933 (x + gc_retroflat_offsets4_x[side_iter])]
1934 ) {
1935 /* Copy center tile to this direction. */
1936 retrotile_get_tile( t, layer,
1937 x + gc_retroflat_offsets4_x[side_iter],
1938 y + gc_retroflat_offsets4_y[side_iter] ) =
1939 retrotile_get_tile( t, layer, x, y );
1940 break;
1941 }
1942 }
1943 }
1944 }
1945 }
1946
1947cleanup:
1948
1949 if( NULL != temp_grid ) {
1950 maug_munlock( temp_grid_h, temp_grid );
1951 }
1952
1953 if( (MAUG_MHANDLE)NULL != temp_grid_h ) {
1954 maug_mfree( temp_grid_h );
1955 }
1956
1957 return retval;
1958}
1959
1960/* === */
1961
1963 struct RETROTILE* t, retroflat_tile_t min_z, retroflat_tile_t max_z,
1964 uint32_t tuning, size_t layer_idx, uint8_t flags, void* data,
1965 retrotile_ani_cb animation_cb, void* animation_cb_data
1966) {
1967 MERROR_RETVAL retval = MERROR_OK;
1968 size_t x = 0,
1969 y = 0;
1970 int16_t side_iter = 0,
1971 sides_avail = 0,
1972 sides_sum = 0;
1973 /* Sides start from 12 on the clock (up). */
1974 struct RETROTILE_LAYER* layer = NULL;
1975
1976 assert( NULL != t );
1977 layer = retrotile_get_layer_p( t, layer_idx );
1978 assert( NULL != layer );
1979
1980 for( y = 0 ; t->tiles_h > y ; y++ ) {
1981 if( NULL != animation_cb ) {
1982 retval = animation_cb( animation_cb_data, y );
1983 maug_cleanup_if_not_ok();
1984 }
1985 for( x = 0 ; t->tiles_w > x ; x++ ) {
1986 /* Reset average. */
1987 sides_avail = 0;
1988 sides_sum = 0;
1989
1990 /* Grab values for available sides. */
1991 for( side_iter = 0 ; 8 > side_iter ; side_iter++ ) {
1992 if(
1993 t->tiles_w <= x + gc_retroflat_offsets8_x[side_iter] ||
1994 t->tiles_h <= y + gc_retroflat_offsets8_y[side_iter]
1995 ) {
1996 continue;
1997 }
1998
1999 sides_avail++;
2000#if RETROTILE_TRACE_LVL > 0
2001 debug_printf(
2003 "si %d: x, y: " SIZE_T_FMT " (+%d), " SIZE_T_FMT
2004 " (+%d) idx: " SIZE_T_FMT,
2005 side_iter,
2006 x + gc_retroflat_offsets8_x[side_iter],
2007 gc_retroflat_offsets8_x[side_iter],
2008 y + gc_retroflat_offsets8_y[side_iter],
2009 gc_retroflat_offsets8_y[side_iter],
2010 ((y + gc_retroflat_offsets8_y[side_iter]) * t->tiles_w) +
2011 x + gc_retroflat_offsets8_x[side_iter] );
2012#endif /* RETROTILE_TRACE_LVL */
2013 sides_sum += retrotile_get_tile(
2014 t, layer,
2015 x + gc_retroflat_offsets8_x[side_iter],
2016 y + gc_retroflat_offsets8_y[side_iter] );
2017 }
2018
2019 retrotile_get_tile( t, layer, x, y ) = sides_sum / sides_avail;
2020 }
2021 }
2022
2023cleanup:
2024
2025 return retval;
2026}
2027
2028/* === */
2029
2031 struct RETROTILE* t, retroflat_tile_t min_z, retroflat_tile_t max_z,
2032 uint32_t tuning, size_t layer_idx, uint8_t flags, void* data,
2033 retrotile_ani_cb animation_cb, void* animation_cb_data
2034) {
2035 MERROR_RETVAL retval = MERROR_OK;
2036 struct RETROTILE_DATA_BORDER* borders =
2037 (struct RETROTILE_DATA_BORDER*)data;
2038 size_t i = 0,
2039 x = 0,
2040 y = 0,
2041 x_plus_1 = 0,
2042 y_plus_1 = 0,
2043 side = 0;
2044 int16_t ctr_iter = 0,
2045 outside_iter = 0;
2046 struct RETROTILE_LAYER* layer = NULL;
2047
2048 assert( NULL != t );
2049 layer = retrotile_get_layer_p( t, layer_idx );
2050 assert( NULL != layer );
2051
2052 /* Reset tile counter for all defined borders. */
2053 for( i = 0 ; 0 <= borders[i].center ; i++ ) {
2054 borders[i].tiles_changed = 0;
2055 }
2056
2057#if RETROTILE_TRACE_LVL > 0
2058 debug_printf( RETROTILE_TRACE_LVL, "adding borders..." );
2059#endif /* RETROTILE_TRACE_LVL */
2060
2061 for( y = 0 ; t->tiles_h > y ; y++ ) {
2062 for( x = 0 ; t->tiles_w > x ; x++ ) {
2063 i = 0;
2064 while( 0 <= borders[i].center ) {
2065 /* Compare/grab current center tile. */
2066 ctr_iter = retrotile_get_tile( t, layer, x, y );
2067#if RETROTILE_TRACE_LVL > 0
2068 debug_printf( RETROTILE_TRACE_LVL,
2069 "x: " SIZE_T_FMT ", y: " SIZE_T_FMT ", 0x%04x vs 0x%04x",
2070 x, y, ctr_iter, borders[i].center );
2071#endif /* RETROTILE_TRACE_LVL */
2072 if( ctr_iter != borders[i].center ) {
2073 i++;
2074 continue;
2075 }
2076
2077#if RETROTILE_TRACE_LVL > 0
2078 debug_printf( RETROTILE_TRACE_LVL, "comparing sides..." );
2079#endif /* RETROTILE_TRACE_LVL */
2080
2081 /* Zeroth pass: look for stick-outs. */
2082 for( side = 0 ; 8 > side ; side += 2 ) {
2083 if(
2084 x + gc_retroflat_offsets8_x[side] > t->tiles_w ||
2085 y + gc_retroflat_offsets8_y[side] > t->tiles_h
2086 ) {
2087 /* Skip out-of-bounds. */
2088 continue;
2089 }
2090 /* Get the outside tile on this side. */
2091 outside_iter = retrotile_get_tile( t, layer,
2092 x + gc_retroflat_offsets8_x[side],
2093 y + gc_retroflat_offsets8_y[side] );
2094
2095 /* Get the outside tile next two clock-steps from this one.
2096 */
2097 if( side + 4 < 8 ) {
2098 x_plus_1 = x + gc_retroflat_offsets8_x[side + 4];
2099 y_plus_1 = y + gc_retroflat_offsets8_y[side + 4];
2100 } else {
2101 x_plus_1 = x + gc_retroflat_offsets8_x[side - 4];
2102 y_plus_1 = y + gc_retroflat_offsets8_y[side - 4];
2103 }
2104
2105 if(
2106 x_plus_1 < t->tiles_w && y_plus_1 < t->tiles_h &&
2107 outside_iter == borders[i].outside &&
2108 outside_iter == retrotile_get_tile( t, layer,
2109 x_plus_1, y_plus_1 )
2110 ) {
2111 /* This has the outside on two opposing sides, so just
2112 * erase it and use the outside. */
2113 retrotile_get_tile( t, layer, x, y ) =
2114 borders[i].outside;
2115 borders[i].tiles_changed++;
2116 goto tile_done;
2117 }
2118 }
2119
2120
2121 /* First pass: look for corners. */
2122 for( side = 0 ; 8 > side ; side += 2 ) {
2123 if(
2124 x + gc_retroflat_offsets8_x[side] > t->tiles_w ||
2125 y + gc_retroflat_offsets8_y[side] > t->tiles_h
2126 ) {
2127 /* Skip out-of-bounds. */
2128 continue;
2129 }
2130 /* Get the outside tile on this side. */
2131 outside_iter = retrotile_get_tile( t, layer,
2132 x + gc_retroflat_offsets8_x[side],
2133 y + gc_retroflat_offsets8_y[side] );
2134
2135 /* Get the outside tile next two clock-steps from this one.
2136 */
2137 if( side + 2 < 8 ) {
2138 x_plus_1 = x + gc_retroflat_offsets8_x[side + 2];
2139 y_plus_1 = y + gc_retroflat_offsets8_y[side + 2];
2140 } else {
2141 x_plus_1 = x + gc_retroflat_offsets8_x[0];
2142 y_plus_1 = y + gc_retroflat_offsets8_y[0];
2143 }
2144
2145 if(
2146 x_plus_1 < t->tiles_w && y_plus_1 < t->tiles_h &&
2147 outside_iter == borders[i].outside &&
2148 outside_iter == retrotile_get_tile( t, layer,
2149 x_plus_1, y_plus_1 )
2150 ) {
2151 /* This has the outside on two sides, so use a corner. */
2152 retrotile_get_tile( t, layer, x, y ) =
2153 borders[i].mod_to[side + 1 < 8 ? side + 1 : 0];
2154 borders[i].tiles_changed++;
2155 goto tile_done;
2156 }
2157 }
2158
2159 /* Second pass (if first pass fails): look for edges. */
2160 for( side = 0 ; 8 > side ; side += 2 ) {
2161 if(
2162 x + gc_retroflat_offsets8_x[side] > t->tiles_w ||
2163 y + gc_retroflat_offsets8_y[side] > t->tiles_h
2164 ) {
2165 /* Skip out-of-bounds. */
2166 continue;
2167 }
2168 /* Get the outside tile on this side. */
2169 outside_iter = retrotile_get_tile( t, layer,
2170 x + gc_retroflat_offsets8_x[side],
2171 y + gc_retroflat_offsets8_y[side] );
2172
2173 if( outside_iter == borders[i].outside ) {
2174 /* It only matches on this side. */
2175#if RETROTILE_TRACE_LVL > 0
2176 debug_printf( RETROTILE_TRACE_LVL, "replacing..." );
2177#endif /* RETROTILE_TRACE_LVL */
2178 retrotile_get_tile( t, layer, x, y ) =
2179 borders[i].mod_to[side];
2180 borders[i].tiles_changed++;
2181 goto tile_done;
2182 }
2183 }
2184
2185tile_done:
2186 /* Tile replaced or not replaceable. */
2187 break;
2188 }
2189 }
2190 }
2191
2192 return retval;
2193}
2194
2195/* === */
2196
2197struct RETROTILE_LAYER* retrotile_get_layer_p(
2198 struct RETROTILE* tilemap, uint32_t layer_idx
2199) {
2200 struct RETROTILE_LAYER* layer_iter = NULL;
2201 uint8_t* tilemap_buf = (uint8_t*)tilemap;
2202
2203 if( 0 == tilemap->layers_count || layer_idx >= tilemap->layers_count ) {
2204 error_printf( "invalid layer " U32_FMT
2205 " requested (of " U32_FMT ")!",
2206 layer_idx, tilemap->layers_count );
2207 return NULL;
2208 }
2209
2210 /* Advance to first grid. */
2211 tilemap_buf += sizeof( struct RETROTILE );
2212 layer_iter = (struct RETROTILE_LAYER*)tilemap_buf;
2213 while( layer_idx > 0 ) {
2214 tilemap_buf += layer_iter->total_sz;
2215 layer_iter = (struct RETROTILE_LAYER*)tilemap_buf;
2216 layer_idx--;
2217 }
2218
2219 return layer_iter;
2220}
2221
2222/* === */
2223
2224MERROR_RETVAL retrotile_alloc(
2225 MAUG_MHANDLE* p_tilemap_h, size_t w, size_t h, size_t layers_count,
2226 const char* tilemap_name, const char* tileset_name
2227) {
2228 struct RETROTILE_LAYER* layer_iter = NULL;
2229 MERROR_RETVAL retval = MERROR_OK;
2230 size_t tilemap_sz = 0;
2231 struct RETROTILE* tilemap = NULL;
2232 size_t i = 0;
2233
2234 tilemap_sz = sizeof( struct RETROTILE ) +
2235 (layers_count * sizeof( struct RETROTILE_LAYER )) +
2236 (layers_count * (w * h * sizeof( retroflat_tile_t ) ));
2237
2238#if RETROTILE_TRACE_LVL > 0
2239 debug_printf(
2240 RETROTILE_TRACE_LVL, "allocating new tilemap " SIZE_T_FMT "x" SIZE_T_FMT
2241 " tiles, " SIZE_T_FMT " layers (" SIZE_T_FMT " bytes)...",
2242 w, h, layers_count, tilemap_sz );
2243#endif /* RETROTILE_TRACE_LVL */
2244
2245 *p_tilemap_h = maug_malloc( 1, tilemap_sz );
2246 maug_cleanup_if_null_alloc( MAUG_MHANDLE, *p_tilemap_h );
2247
2248 maug_mlock( *p_tilemap_h, tilemap );
2249 maug_cleanup_if_null_alloc( struct RETROTILE*, tilemap );
2250
2251 maug_mzero( tilemap, tilemap_sz );
2252 tilemap->total_sz = tilemap_sz;
2253 tilemap->layers_count = layers_count;
2254 tilemap->tiles_w = w;
2255 tilemap->tiles_h = h;
2257 tilemap->sz = sizeof( struct RETROTILE );
2258
2259 maug_strncpy( tilemap->name, tilemap_name, RETROTILE_NAME_SZ_MAX );
2260
2261 maug_strncpy( tilemap->tileset, tileset_name, RETROTILE_NAME_SZ_MAX );
2262
2263 for( i = 0 ; layers_count > i ; i++ ) {
2264 layer_iter = retrotile_get_layer_p( tilemap, i );
2265 assert( NULL != layer_iter );
2266 layer_iter->total_sz = sizeof( struct RETROTILE_LAYER ) +
2267 (w * h * sizeof( retroflat_tile_t ));
2268 maug_cleanup_if_not_ok();
2269 layer_iter->sz = sizeof( struct RETROTILE_LAYER );
2270 }
2271
2272cleanup:
2273
2274 if( NULL != tilemap ) {
2275 maug_munlock( *p_tilemap_h, tilemap );
2276 }
2277
2278 return retval;
2279}
2280
2281/* === */
2282
2283void retrotile_format_asset_path(
2284 retroflat_asset_path path_out, const char* afile,
2285 struct RETROTILE_PARSER* parser
2286) {
2287 /* Load the portrait. */
2288 maug_mzero( path_out, MAUG_PATH_SZ_MAX + 1 );
2289 maug_snprintf( path_out, MAUG_PATH_SZ_MAX, "%s/%s",
2290 parser->dirname, afile );
2291}
2292
2293/* === */
2294
2295MERROR_RETVAL retrotile_clear_refresh( retroflat_pxxy_t y_max ) {
2296 MERROR_RETVAL retval = MERROR_OK;
2297#ifndef RETROFLAT_NO_VIEWPORT_REFRESH
2298 int16_t x = 0,
2299 y = 0;
2300
2301#if RETROTILE_TRACE_LVL > 0
2302 debug_printf( RETROTILE_TRACE_LVL,
2303 "clearing " SIZE_T_FMT " vertical viewport pixels (" SIZE_T_FMT
2304 " rows)...",
2305 y_max, y_max / RETROFLAT_TILE_H );
2306#endif /* RETROTILE_TRACE_LVL */
2307
2308 retroflat_viewport_lock_refresh();
2309 for( y = 0 ; y_max > y ; y += RETROFLAT_TILE_H ) {
2310 for( x = 0 ; retroflat_viewport_screen_w() > x ; x += RETROFLAT_TILE_W ) {
2311 retroflat_viewport_set_refresh( x, y, -1 );
2312 }
2313 }
2314
2315cleanup:
2316
2317 retroflat_viewport_unlock_refresh();
2318
2319#endif /* !RETROFLAT_NO_VIEWPORT_REFRESH */
2320
2321 return retval;
2322}
2323
2324/* === */
2325
2326MERROR_RETVAL retrotile_topdown_draw(
2327 struct RETROFLAT_BITMAP* target,
2328 struct RETROTILE* t, struct MDATA_VECTOR* t_defs
2329) {
2330 int16_t x = 0,
2331 y = 0,
2332 x_tile = 0,
2333 y_tile = 0;
2334 retroflat_tile_t tile_id = 0;
2335 struct RETROTILE_LAYER* layer = NULL;
2336 struct RETROTILE_TILE_DEF* t_def = NULL;
2337 MERROR_RETVAL retval = MERROR_OK;
2338
2339 layer = retrotile_get_layer_p( t, 0 );
2340
2341 mdata_vector_lock( t_defs );
2342 /* TODO: Rework this so it uses viewport tile indexes and then calculates
2343 * screen pixel X/Y from those? For performance?
2344 */
2345 for(
2346 y = ((retroflat_viewport_world_y() >>
2347 RETROFLAT_TILE_H_BITS) << RETROFLAT_TILE_H_BITS) ;
2348 y < (int)retroflat_viewport_world_y() +
2349 (int)retroflat_viewport_screen_h() ;
2350 y += RETROFLAT_TILE_H
2351 ) {
2352 for(
2353 x = ((retroflat_viewport_world_x() >>
2354 RETROFLAT_TILE_W_BITS) << RETROFLAT_TILE_W_BITS) ;
2355 x < (int)retroflat_viewport_world_x() +
2356 (int)retroflat_viewport_screen_w() ;
2357 x += RETROFLAT_TILE_W
2358 ) {
2359 /* Limit to tiles that exist in the world. */
2360 if(
2361 -1 > x || -1 > y ||
2362 (int)retroflat_viewport_world_w() <= x ||
2363 (int)retroflat_viewport_world_h() <= y
2364 ) {
2365 continue;
2366 }
2367
2368 /* Divide by tile width (16), or shift by 2^4. */
2369 x_tile = x >> RETROFLAT_TILE_W_BITS;
2370 y_tile = y >> RETROFLAT_TILE_H_BITS;
2371
2372 tile_id = retrotile_get_tile( t, layer, x_tile, y_tile );
2373 t_def = mdata_vector_get(
2374 t_defs, tile_id - t->tileset_fgid, struct RETROTILE_TILE_DEF );
2375 if( NULL == t_def ) {
2376 error_printf(
2377 "invalid tile ID: %d (- " SIZE_T_FMT " = " SIZE_T_FMT ")",
2378 tile_id, t->tileset_fgid, tile_id - t->tileset_fgid );
2379 continue;
2380 }
2381 assert( NULL != t_def );
2382
2383#ifndef RETROFLAT_NO_VIEWPORT_REFRESH
2384 /* Check tile refresh buffer. */
2385 retroflat_viewport_lock_refresh();
2386 if( !retroflat_viewport_tile_is_stale(
2387 x - retroflat_viewport_world_x(),
2388 y - retroflat_viewport_world_y(), tile_id
2389 ) ) {
2390 retroflat_viewport_unlock_refresh();
2391 continue;
2392 }
2393 /* Noisy! */
2394 /*
2395 debug_printf( RETROTILE_TRACE_LVL, "redrawing tile: %u, %u",
2396 x - retroflat_viewport_world_x(),
2397 y - retroflat_viewport_world_y() );
2398 */
2399 retroflat_viewport_set_refresh(
2400 x - retroflat_viewport_world_x(),
2401 y - retroflat_viewport_world_y(), tile_id );
2402 retroflat_viewport_unlock_refresh();
2403#endif /* !RETROFLAT_NO_VIEWPORT_REFRESH */
2404
2405#ifdef RETROGXC_PRESENT
2406 retrogxc_blit_bitmap( target, t_def->image_cache_id,
2407 t_def->x, t_def->y,
2410 RETROFLAT_TILE_W, RETROFLAT_TILE_H,
2411 retroflat_instance_tile( tile_id ) );
2412#else
2413 retroflat_blit_bitmap( target, &(t_def->image),
2414 t_def->x, t_def->y,
2417 RETROFLAT_TILE_W, RETROFLAT_TILE_H,
2418 retroflat_instance_tile( tile_id ) );
2419#endif /* RETROGXC_PRESENT */
2420 }
2421 }
2422
2423cleanup:
2424
2425 mdata_vector_unlock( t_defs );
2426
2427 return retval;
2428}
2429
2430#else
2431
2432/* This is defined externally so custom token callbacks can reference it. */
2433
2434# define RETROTILE_PARSER_MSTATE_TABLE_CONST( name, idx, tokn, parent, m ) \
2435 extern MAUG_CONST uint8_t SEG_MCONST name;
2436
2437RETROTILE_PARSER_MSTATE_TABLE( RETROTILE_PARSER_MSTATE_TABLE_CONST )
2438
2439# define RETROTILE_CLASS_TABLE_CONSTS( A, a, i ) \
2440 extern MAUG_CONST uint8_t SEG_MCONST RETROTILE_CLASS_ ## A;
2441
2442RETROTILE_CLASS_TABLE( RETROTILE_CLASS_TABLE_CONSTS )
2443
2444
2445#endif /* RETROTIL_C */
2446 /* retrotile */
2448 /* maug_retroflt */
2450
2451#endif /* !RETROTIL_H */
2452
uint16_t MERROR_RETVAL
Return type indicating function returns a value from this list.
Definition merror.h:28
#define MAUG_PATH_SZ_MAX
Maximum size allocated for asset paths.
Definition mfile.h:36
MERROR_RETVAL mfile_open_read(const char *filename, mfile_t *p_file)
Open a file and read it into memory or memory-map it.
#define MERROR_PREEMPT
Indicates MLISP_AST_NODE can be executed again on next step iter pass.
Definition merror.h:67
char retroflat_asset_path[MAUG_PATH_SZ_MAX+1]
Path/name used to load an asset from disk.
Definition mfile.h:130
MERROR_RETVAL retroflat_blit_bitmap(struct RETROFLAT_BITMAP *target, struct RETROFLAT_BITMAP *src, size_t s_x, size_t s_y, int16_t d_x, int16_t d_y, size_t w, size_t h, int16_t instance)
Blit the contents of a RETROFLAT_BITMAP onto another RETROFLAT_BITMAP.
#define retroflat_instance_tile(instance)
Declare that a given instance ID is for a tile, rather than a sprite.
Definition retroflt.h:575
int8_t RETROFLAT_COLOR
Defines an index in the platform-specific color-table.
Definition retroflt.h:325
#define RETROFLAT_COLOR_TABLE(f)
This macro defines all colors supported by RetroFlat for primative operations, particularly using ret...
Definition retroflt.h:306
#define retroflat_viewport_screen_y(world_y)
Return the screenspace Y coordinate at which something at the given world coordinate should be drawn.
Definition retroflt.h:1544
#define retroflat_viewport_screen_x(world_x)
Return the screenspace X coordinate at which something at the given world coordinate should be drawn.
Definition retroflt.h:1537
size_t retroflat_pxxy_t
Type used for surface pixel coordinates.
Definition retroflt.h:870
#define RETROTILE_DS_FLAG_INIT_DATA
Flag for retrotile_gen_diamond_square_iter() indicating that passed RETROTILE_DATA_DS object should b...
Definition retrotil.h:159
#define RETROTILE_PROP_TYPE_FILE
Value for RETROTILE_PARSER::last_prop_type indicating file path.
Definition retrotil.h:141
#define RETROTILE_PROP_TYPE_STRING
Value for RETROTILE_PARSER::last_prop_type indicating string.
Definition retrotil.h:135
#define RETROTILE_PROP_TYPE_INT
Value for RETROTILE_PARSER::last_prop_type indicating integer.
Definition retrotil.h:147
#define RETROTILE_PROP_TYPE_OTHER
Value for RETROTILE_PARSER::last_prop_type indicating other type.
Definition retrotil.h:129
MERROR_RETVAL retrotile_gen_smooth_iter(struct RETROTILE *t, retroflat_tile_t min_z, retroflat_tile_t max_z, uint32_t tuning, size_t layer_idx, uint8_t flags, void *data, retrotile_ani_cb animation_cb, void *animation_cb_data)
Average the values in adjacent tiles over an already-generated tilemap.
MERROR_RETVAL retrotile_gen_borders_iter(struct RETROTILE *t, retroflat_tile_t min_z, retroflat_tile_t max_z, uint32_t tuning, size_t layer_idx, uint8_t flags, void *data, retrotile_ani_cb animation_cb, void *animation_cb_data)
Given a list of RETROTILE_DATA_BORDER structs, this will search for occurrences of RETROTILE_DATA_BOR...
MERROR_RETVAL retrotile_gen_diamond_square_iter(struct RETROTILE *t, retroflat_tile_t min_z, retroflat_tile_t max_z, uint32_t tuning, size_t layer_idx, uint8_t flags, void *data, retrotile_ani_cb animation_cb, void *animation_cb_data)
Generate tilemap terrain using diamond square algorithm.
MERROR_RETVAL retrotile_gen_voronoi_iter(struct RETROTILE *t, retroflat_tile_t min_z, retroflat_tile_t max_z, uint32_t tuning, size_t layer_idx, uint8_t flags, void *data, retrotile_ani_cb animation_cb, void *animation_cb_data)
Generate tilemap terrain using voronoi graph.
int retrotile_parse_prop_type(const char *token, size_t token_sz)
Convert a Tiled "type" field to an integer suitable for use with RETROTILE_PARSER::last_prop_type.
MERROR_RETVAL retrotile_parse_json_file(const char *dirname, const char *filename, MAUG_MHANDLE *p_tilemap_h, struct MDATA_VECTOR *p_tile_defs, mparser_wait_cb_t wait_cb, void *wait_data, mparser_parse_token_cb token_cb, void *token_cb_data, uint8_t passes, uint8_t flags)
Parse the JSON file at the given path into a heap-allocated tilemap with a RETROTILE struct header.
mfix_t retrotile_static_rotation_from_dir(const char *dir)
Convert a less-or-equal-to-two-character string to a direction in degrees.
#define RETROTILE_TILE_SCALE_DEFAULT
Default value for RETROTILE::tile_scale.
Definition retrotil.h:37
#define RETROTILE_PROP_NAME_SZ_MAX
Maximum number of chars in a parsed property name.
Definition retrotil.h:32
#define RETROTILE_PARSER_MODE_DEFS
Value for RETROTILE_PARSER::mode indicating the parser is currently parsing tile definitions.
Definition retrotil.h:80
#define RETROTILE_TRACE_LVL
If defined, bring debug printf statements up to this level.
Definition retrotil.h:42
#define RETROTILE_PARSER_MODE_MAP
Value for RETROTILE_PARSER::mode indicating the parser is currently parsing a tilemap.
Definition retrotil.h:73
int16_t retroflat_tile_t
Value for an individual tile in a RETROTILE_LAYER.
Definition retroflt.h:19
#define RETROTILE_NAME_SZ_MAX
Maximum number of chars in a RETROTILE::name.
Definition retrotil.h:27
#define RETROTILE_PARSER_FLAG_LITERAL_PATHS
Flag for RETROTILE_PARSER::flags indicating to use literal image asset paths.
Definition retrotil.h:65
A vector of uniformly-sized objects, stored contiguously.
Definition mdata.h:105
This is not currently used for anything, but is provided as a a convenience for game logic.
Definition retrotil.h:212
Definition retrotil.h:267
retroflat_tile_t mod_to[8]
If the center and outside match, use this mod-to.
Definition retrotil.h:272
Internal data structure used by retrotile_gen_diamond_square_iter().
Definition retrotil.h:250
int16_t sect_y
Starting Y of subsector in a given iteration.
Definition retrotil.h:254
int16_t sect_w
Width of subsector in a given iteration.
Definition retrotil.h:256
int16_t sect_w_half
Half of the width of subsector in a given iteration.
Definition retrotil.h:260
int16_t sect_x
Starting X of subsector in a given iteration.
Definition retrotil.h:252
int16_t sect_h
Height of subsector in a given iteration.
Definition retrotil.h:258
int16_t sect_h_half
Half of the height of subsector in a given iteration.
Definition retrotil.h:262
Definition retrotil.h:200
size_t total_sz
Size of the layer in bytes (including this struct header).
Definition retrotil.h:204
size_t sz
Size of this struct (useful for serializing).
Definition retrotil.h:202
Definition retrotil.h:314
mparser_parse_token_cb custom_token_cb
Callback to parse engine-specific custom tokens from the tilemap JSON. Should return MERROR_PREEMPT i...
Definition retrotil.h:348
char tilemap_name[RETROTILE_NAME_SZ_MAX+1]
The name to give to the new tilemap.
Definition retrotil.h:331
char last_prop_name[RETROTILE_PROP_NAME_SZ_MAX+1]
The name of the last property key/value pair parsed.
Definition retrotil.h:327
uint8_t mode
Value indicating the current type of object being parsed into.
Definition retrotil.h:321
size_t tileset_id_cur
Highest tileset ID on first pass and next ID to be assigned on second.
Definition retrotil.h:338
Definition retrotil.h:166
mfix_t static_rotation
Field indicating how many degrees the tile should always be rotated before drawin on-screen....
Definition retrotil.h:180
size_t sz
Size of this struct (useful for serializing).
Definition retrotil.h:168
int8_t no_serial
Dummy field; do not serialize fields after this!
Definition retrotil.h:184
A struct representing a tilemap.
Definition retrotil.h:224
size_t tiles_h
Height of all layers of the tilemap in tiles.
Definition retrotil.h:236
uint32_t total_sz
Size of the tilemap in bytes (including this struct header).
Definition retrotil.h:230
size_t tileset_fgid
First GID in the accompanying tileset.
Definition retrotil.h:234
float tile_scale
Amount by which to scale tiles (convenience property).
Definition retrotil.h:243
size_t tiles_w
Width of all layers of the tilemap in tiles.
Definition retrotil.h:238
uint32_t layers_count
Number of tile layers in this tilemap.
Definition retrotil.h:232
size_t sz
Size of this struct (useful for serializing).
Definition retrotil.h:226