maug
Quick and dirty C mini-augmentation library.
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
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
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 maug_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
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 maug_path dirname;
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 maug_path 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
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 maug_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 {
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 maug_path 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 maug_path filename_path;
1271 mfile_t tile_file;
1272 char c;
1273 char* filename_ext = NULL;
1274
1275 /* Initialize parser. */
1276 maug_malloc_test( parser_h, 1, sizeof( struct RETROTILE_PARSER ) );
1277
1278 maug_mlock( parser_h, parser );
1279 maug_cleanup_if_null_alloc( struct RETROTILE_PARSER*, parser );
1280 maug_mzero( parser, sizeof( struct RETROTILE_PARSER ) );
1281
1282 parser->flags = flags;
1283 parser->tj_parse_cb = retrotile_parse_json_file;
1284 parser->custom_token_cb = token_cb;
1285 parser->custom_token_cb_data = token_cb_data;
1286
1287 retval = mfile_assign_path( parser->dirname, dirname, 0 );
1288 maug_cleanup_if_not_ok();
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 retrotile_format_asset_path( filename_path, filename, parser );
1302
1303#if RETROTILE_TRACE_LVL > 0
1304 debug_printf( RETROTILE_TRACE_LVL, "opening %s...", filename_path );
1305#endif /* RETROTILE_TRACE_LVL */
1306
1307 retval = mfile_open_read( filename_path, &tile_file );
1308 maug_cleanup_if_not_ok();
1309
1310 /* Parse JSON and react to state. */
1311 for( parser->pass = 0 ; passes > parser->pass ; parser->pass++ ) {
1312#if RETROTILE_TRACE_LVL > 0
1313 debug_printf( RETROTILE_TRACE_LVL, "beginning pass #%u...",
1314 parser->pass );
1315#endif /* RETROTILE_TRACE_LVL */
1316
1317 /* Reset tilemap parser. */
1318 parser->mstate = 0;
1319
1320 /* Reset JSON parser. */
1321 maug_mzero( &(parser->jparser.base), sizeof( struct MJSON_PARSER ) );
1322
1323 parser->wait_cb = wait_cb;
1324 parser->wait_data = wait_data;
1325 parser->jparser.base.wait_cb = wait_cb;
1326 parser->jparser.base.wait_data = wait_data;
1327
1328 /* Figure out if we're parsing a .tmj or .tsj. */
1329 filename_ext = maug_strrchr( filename, '.' );
1330 if( NULL == filename_ext ) {
1331 error_printf( "could not parse filename extension!" );
1332 retval = MERROR_FILE;
1333 goto cleanup;
1334 }
1335 if( 's' == filename_ext[2] ) {
1336#if RETROTILE_TRACE_LVL > 0
1337 debug_printf( RETROTILE_TRACE_LVL,
1338 "(tile_defs pass %u)", parser->pass );
1339#endif /* RETROTILE_TRACE_LVL */
1341 parser->jparser.token_parser = retrotile_parser_parse_tiledef_token;
1342 parser->jparser.token_parser_arg = parser;
1343 parser->jparser.close_list = retrotile_json_close_list;
1344 parser->jparser.close_list_arg = parser;
1345 parser->jparser.close_obj = retrotile_json_close_obj;
1346 parser->jparser.close_obj_arg = parser;
1347 /*
1348 parser->jparser.base.close_val = retrotile_json_close_val;
1349 parser->jparser.base.close_val_arg = parser;
1350 */
1351 parser->p_tile_defs = p_tile_defs;
1352
1353 assert( NULL != p_tile_defs );
1354 if( 1 == parser->pass ) {
1355 /* Allocate tile defs based on highest tile ID found on
1356 * first pass.
1357 */
1358 assert( 0 < parser->tileset_id_cur );
1359#if RETROTILE_TRACE_LVL > 0
1360 debug_printf(
1361 RETROTILE_TRACE_LVL, "allocating " SIZE_T_FMT " tile defs...",
1362 parser->tileset_id_cur + 1 );
1363#endif /* RETROTILE_TRACE_LVL */
1365 parser->p_tile_defs, parser->tileset_id_cur + 1,
1366 sizeof( struct RETROTILE_TILE_DEF ) );
1367 }
1368 } else {
1369#if RETROTILE_TRACE_LVL > 0
1370 debug_printf( RETROTILE_TRACE_LVL, "(tilemap pass %u)", parser->pass );
1371#endif /* RETROTILE_TRACE_LVL */
1373
1374 parser->jparser.close_list = retrotile_json_close_list;
1375 parser->jparser.close_list_arg = parser;
1376 parser->jparser.open_obj = retrotile_json_open_obj;
1377 parser->jparser.open_obj_arg = parser;
1378 parser->jparser.close_obj = retrotile_json_close_obj;
1379 parser->jparser.close_obj_arg = parser;
1380 parser->jparser.token_parser = retrotile_parser_parse_token;
1381 parser->jparser.token_parser_arg = parser;
1382 parser->p_tile_defs = p_tile_defs;
1383
1384 assert( NULL != p_tilemap_h );
1385 assert( NULL != p_tile_defs );
1386 if( 1 == parser->pass ) {
1387 /* Allocate tiles for the new layers. */
1388 retval = retrotile_alloc(
1389 p_tilemap_h, parser->tiles_w, parser->tiles_h,
1390 parser->pass_layer_iter, parser->tilemap_name,
1391 parser->tileset_name );
1392 maug_cleanup_if_not_ok();
1393 maug_mlock( *p_tilemap_h, parser->t );
1394 }
1395 parser->pass_layer_iter = 0;
1396 }
1397
1398 while( tile_file.has_bytes( &tile_file ) ) {
1399 tile_file.read_int( &tile_file, (uint8_t*)&c, 1, 0 );
1400#if RETROTILE_TRACE_CHARS > 0
1401 debug_printf( RETROTILE_TRACE_CHARS, "%c", c );
1402#endif /* RETROTILE_TRACE_CHARS */
1403 retval = mjson_parse_c( &(parser->jparser), c );
1404 if( MERROR_OK != retval ) {
1405 error_printf( "error parsing JSON!" );
1406 goto cleanup;
1407 }
1408 }
1409
1410 tile_file.seek( &tile_file, 0 );
1411
1412 filename_ext = maug_strrchr( filename, '.' );
1413 if( NULL == filename_ext ) {
1414 error_printf( "could not parse filename extension!" );
1415 retval = MERROR_FILE;
1416 goto cleanup;
1417 }
1418 if( 's' != filename_ext[2] ) {
1419#if RETROTILE_TRACE_LVL > 0
1420 debug_printf( RETROTILE_TRACE_LVL,
1421 "pass %u found " SIZE_T_FMT " layers",
1422 parser->pass, parser->pass_layer_iter );
1423#endif /* RETROTILE_TRACE_LVL */
1424 }
1425 }
1426
1427#if RETROTILE_TRACE_LVL > 0
1428 debug_printf(
1429 RETROTILE_TRACE_LVL, "finished parsing %s, retval: %d",
1430 filename_path, retval );
1431#endif /* RETROTILE_TRACE_LVL */
1432
1433cleanup:
1434
1435 if( NULL != parser ) {
1436 if( NULL != parser->t ) {
1437 maug_munlock( *p_tilemap_h, parser->t );
1438 }
1439 maug_munlock( parser_h, parser );
1440 }
1441
1442 if( (MAUG_MHANDLE)NULL != parser_h ) {
1443 maug_mfree( parser_h );
1444 }
1445
1446 return retval;
1447}
1448
1449/* === */
1450
1451static retroflat_tile_t retrotile_gen_diamond_square_rand(
1452 retroflat_tile_t min_z, retroflat_tile_t max_z, uint32_t tuning,
1453 retroflat_tile_t top_left_z
1454) {
1455 retroflat_tile_t avg = top_left_z;
1456
1457 if( 8 > rand() % 10 ) {
1458 /* avg = min_z + (rand() % (max_z - min_z)); */
1459 avg -= (min_z / tuning) + (rand() % (max_z / tuning));
1460 /* } else {
1461 avg += (min_z / 10) + (rand() % (max_z / 10)); */
1462 }
1463
1464 /* Clamp the result. */
1465
1466 if( min_z > avg ) {
1467 avg = min_z;
1468 }
1469
1470 if( max_z < avg ) {
1471 avg = max_z;
1472 }
1473
1474 return avg;
1475}
1476
1477/* === */
1478
1479static void retrotile_gen_diamond_square_corners(
1480 int16_t corners_x[2][2], int16_t corners_y[2][2],
1482 uint32_t tuning, struct RETROTILE_DATA_DS* data_ds,
1483 struct RETROTILE_LAYER* layer, struct RETROTILE* t
1484) {
1485 int16_t iter_x = 0,
1486 iter_y = 0;
1487 retroflat_tile_t* tile_iter = NULL;
1488 retroflat_tile_t top_left_z = 0;
1489
1490 /* Generate missing corner data. Loop through X/Y coords stored in
1491 * corners_x/corners_y convenience arrays.
1492 */
1493 for( iter_y = 0 ; iter_y < 2 ; iter_y++ ) {
1494 for( iter_x = 0 ; iter_x < 2 ; iter_x++ ) {
1495
1496 /* Make sure corner X is in bounds. */
1497 corners_x[iter_x][iter_y] =
1498 (data_ds->sect_x - 1) + (iter_x * data_ds->sect_w);
1499 if( 0 > corners_x[iter_x][iter_y] ) {
1500 corners_x[iter_x][iter_y] += 1;
1501 }
1502
1503 /* Make sure corner Y is in bounds. */
1504 corners_y[iter_x][iter_y] =
1505 (data_ds->sect_y - 1) + (iter_y * data_ds->sect_h);
1506 if( 0 > corners_y[iter_x][iter_y] ) {
1507 corners_y[iter_x][iter_y] += 1;
1508 }
1509 }
1510 }
1511
1512 /* Should be handled by the check above. */
1513 assert( 0 <= corners_x[0][0] );
1514 assert( 0 <= corners_y[0][0] );
1515 assert( t->tiles_w > corners_x[0][0] );
1516 assert( t->tiles_h > corners_y[0][0] );
1517
1518 /* Grab the top-left Z-value to anchor generated corners to. */
1519 top_left_z = retrotile_get_tile(
1520 t, layer,
1521 corners_x[0][0],
1522 corners_y[0][0] );
1523
1524 if( 0 > top_left_z ) {
1525 retrotile_get_tile(
1526 t, layer,
1527 corners_x[0][0] >= 0 ? corners_x[0][0] : 0,
1528 corners_y[0][0] >= 0 ? corners_y[0][0] : 0 ) = max_z;
1529 top_left_z = max_z;
1530 }
1531
1532 /* Fill in missing corners. */
1533 for( iter_y = 0 ; iter_y < 2 ; iter_y++ ) {
1534 for( iter_x = 0 ; iter_x < 2 ; iter_x++ ) {
1535 /* Grab a pointer to the corner so we can modify it easily. */
1536 tile_iter = &(retrotile_get_tile(
1537 t, layer,
1538 corners_x[iter_x][iter_y],
1539 corners_y[iter_x][iter_y] ));
1540
1541 /* Check if corner is already filled in. */
1542 if( -1 != *tile_iter ) {
1543#if RETROTILE_TRACE_LVL > 0
1544 debug_printf(
1545 RETROTILE_TRACE_LVL, "corner coord %d x %d present: %d",
1546 corners_x[iter_x][iter_y], corners_y[iter_x][iter_y],
1547 retrotile_get_tile(
1548 t, layer,
1549 corners_x[iter_x][iter_y],
1550 corners_y[iter_x][iter_y] ) );
1551#endif /* RETROTILE_TRACE_LVL */
1552 continue;
1553 }
1554
1555 /* Generate a new value for this corner. */
1556 *tile_iter = retrotile_gen_diamond_square_rand(
1557 min_z, max_z, tuning, top_left_z );
1558
1559#if RETROTILE_TRACE_LVL > 0
1560 debug_printf( RETROTILE_TRACE_LVL,
1561 "missing corner coord %d x %d: %d",
1562 corners_x[iter_x][iter_y], corners_y[iter_x][iter_y],
1563 *tile_iter );
1564#endif /* RETROTILE_TRACE_LVL */
1565 }
1566 }
1567}
1568
1569/* === */
1570
1571static retroflat_tile_t retrotile_gen_diamond_square_avg(
1572 int16_t corners_x[2][2], int16_t corners_y[2][2],
1573 struct RETROTILE* t, struct RETROTILE_LAYER* layer
1574) {
1575 retroflat_tile_t* tile_iter = NULL;
1576 int16_t iter_x = 0,
1577 iter_y = 0;
1578 retroflat_tile_t avg = 0;
1579
1580 /* Average corner data. */
1581 for( iter_y = 0 ; 2 > iter_y ; iter_y++ ) {
1582 for( iter_x = 0 ; 2 > iter_x ; iter_x++ ) {
1583 tile_iter = &(retrotile_get_tile(
1584 t, layer,
1585 corners_x[iter_x][iter_y],
1586 corners_y[iter_x][iter_y] ));
1587 assert( -1 != *tile_iter );
1588 /*
1589 debug_printf(
1590 RETROTILE_TRACE_LVL, "%d: adding from coords %d x %d: %d",
1591 iter_depth,
1592 corners_x[iter_x][iter_y], corners_y[iter_x][iter_y],
1593 *tile_iter ); */
1594 avg += *tile_iter;
1595 }
1596 }
1597
1598 /* TODO: Use right shift? */
1599 avg /= 4;
1600
1601 return avg;
1602}
1603
1604/* === */
1605
1607 struct RETROTILE* t, retroflat_tile_t min_z, retroflat_tile_t max_z,
1608 uint32_t tuning, size_t layer_idx, uint8_t flags, void* data,
1609 retrotile_ani_cb animation_cb, void* animation_cb_data
1610) {
1611 int16_t iter_x = 0,
1612 iter_y = 0
1613#if RETROTILE_TRACE_LVL > 0
1614 , iter_depth = 0;
1615#else
1616 ;
1617#endif /* RETROTILE_TRACE_LVL */
1618 int16_t corners_x[2][2];
1619 int16_t corners_y[2][2];
1620 int32_t avg = 0;
1621 /* size_t tile_idx = 0; */
1622 struct RETROTILE_DATA_DS data_ds_sub;
1623 MAUG_MHANDLE data_ds_h = (MAUG_MHANDLE)NULL;
1624 struct RETROTILE_DATA_DS* data_ds = NULL;
1625 /* retroflat_tile_t* tiles = NULL; */
1626 MERROR_RETVAL retval = MERROR_OK;
1627 struct RETROTILE_LAYER* layer = NULL;
1628 retroflat_tile_t* tile_iter = NULL;
1629 uint8_t free_ds_data = 0;
1630#ifdef MAUG_NO_STDLIB
1631 size_t i = 0;
1632#endif /* MAUG_NO_STDLIB */
1633
1634 /*
1635 maug_mlock( t->tiles, tiles );
1636 maug_cleanup_if_null_alloc( struct GRIDCITY_TILE*, tiles );
1637 */
1638
1639 #define _retrotile_ds_update_statistics( data, tile ) \
1640 /* Gather statistics. */ \
1641 if( (data)->highest_generated < (tile) && 32767 > (tile) ) { \
1642 (data)->highest_generated = (tile); \
1643 } \
1644 if( (data)->lowest_generated > (tile) && 0 < (tile) ) { \
1645 (data)->lowest_generated = (tile); \
1646 }
1647
1648 layer = retrotile_get_layer_p( t, layer_idx );
1649
1650 if(
1651 NULL == data ||
1653 ) {
1654 /* This must be the first call, so initialize or allocate a new struct.
1655 */
1656 if( NULL == data ) {
1657 /* An internal struct needs to be allocated before initialization. */
1658 maug_malloc_test( data_ds_h, 1, sizeof( struct RETROTILE_DATA_DS ) );
1659 free_ds_data = 1;
1660 maug_mlock( data_ds_h, data_ds );
1661 maug_cleanup_if_null_alloc( struct RETROTILE_DATA_DS*, data_ds );
1662 } else {
1663 data_ds = (struct RETROTILE_DATA_DS*)data;
1664 }
1665
1666 /* Initialize passed tilemap while we're handling first call stuff. */
1667 retrotile_clear_tiles( t, layer, i );
1668
1669 /* Initialize DS struct from tilemap properties. */
1670 maug_mzero( data_ds, sizeof( struct RETROTILE_DATA_DS ) );
1671 data_ds->sect_w = t->tiles_w;
1672 data_ds->sect_h = t->tiles_h;
1673 data_ds->sect_w_half = data_ds->sect_w >> 1;
1674 data_ds->sect_h_half = data_ds->sect_h >> 1;
1675 data_ds->lowest_generated = 32767;
1676
1677 /* Disable this flag for subsequent calls. */
1678 flags &= ~RETROTILE_DS_FLAG_INIT_DATA;
1679 } else {
1680 data_ds = (struct RETROTILE_DATA_DS*)data;
1681 }
1682 assert( NULL != data_ds );
1683
1684 /* Trivial case; end recursion. */
1685 if( 0 == data_ds->sect_w ) {
1686#if RETROTILE_TRACE_LVL > 0
1687 debug_printf(
1688 RETROTILE_TRACE_LVL, "%d return: null sector", iter_depth );
1689#endif /* RETROTILE_TRACE_LVL */
1690 goto cleanup;
1691 }
1692
1693 if(
1694 data_ds->sect_x + data_ds->sect_w > t->tiles_w ||
1695 data_ds->sect_y + data_ds->sect_h > t->tiles_h
1696 ) {
1697#if RETROTILE_TRACE_LVL > 0
1698 debug_printf(
1699 RETROTILE_TRACE_LVL, "%d return: overflow sector", iter_depth );
1700#endif /* RETROTILE_TRACE_LVL */
1701 goto cleanup;
1702 }
1703
1704#if RETROTILE_TRACE_LVL > 0
1705 iter_depth = t->tiles_w / data_ds->sect_w;
1706#endif /* RETROTILE_TRACE_LVL */
1707
1708 /* Generate/grab corners before averaging them! */
1709 retrotile_gen_diamond_square_corners(
1710 corners_x, corners_y, min_z, max_z, tuning, data_ds, layer, t );
1711
1712 if( 2 == data_ds->sect_w || 2 == data_ds->sect_h ) {
1713 /* Nothing to average, this sector is just corners! */
1714#if RETROTILE_TRACE_LVL > 0
1715 debug_printf(
1717 "%d return: reached innermost point", iter_depth );
1718#endif /* RETROTILE_TRACE_LVL */
1719 goto cleanup; /* Skip further descent regardless. */
1720 }
1721
1722 avg =
1723 retrotile_gen_diamond_square_avg( corners_x, corners_y, t, layer );
1724
1725#if RETROTILE_TRACE_LVL > 0
1726 debug_printf( RETROTILE_TRACE_LVL, "avg: " S32_FMT, avg );
1727#endif /* RETROTILE_TRACE_LVL */
1728
1729 tile_iter = &(retrotile_get_tile(
1730 t, layer,
1731 data_ds->sect_x + data_ds->sect_w_half,
1732 data_ds->sect_y + data_ds->sect_h_half ));
1733 if( -1 != *tile_iter ) {
1734#if RETROTILE_TRACE_LVL > 0
1735 debug_printf( RETROTILE_TRACE_LVL, "avg already present at %d x %d!",
1736 data_ds->sect_x + data_ds->sect_w_half,
1737 data_ds->sect_y + data_ds->sect_h_half );
1738#endif /* RETROTILE_TRACE_LVL */
1739 goto cleanup;
1740 }
1741 *tile_iter = avg;
1742 _retrotile_ds_update_statistics( data_ds, avg );
1743
1744 /* assert( 0 <= tiles[tile_idx].terrain );
1745
1746 maug_munlock( city->tiles, tiles );
1747 tiles = NULL; */
1748
1749 /* Recurse into subsectors. */
1750 for(
1751 iter_y = data_ds->sect_y ;
1752 iter_y < (data_ds->sect_y + data_ds->sect_h) ;
1753 iter_y++
1754 ) {
1755 for(
1756 iter_x = data_ds->sect_x ;
1757 iter_x < (data_ds->sect_x + data_ds->sect_w) ;
1758 iter_x++
1759 ) {
1760 data_ds_sub.sect_x = data_ds->sect_x + iter_x;
1761
1762 data_ds_sub.sect_y = data_ds->sect_y + iter_y;
1763
1764 data_ds_sub.sect_w = data_ds->sect_w_half;
1765 data_ds_sub.sect_h = data_ds->sect_h_half;
1766 data_ds_sub.sect_w_half = data_ds_sub.sect_w >> 1;
1767 data_ds_sub.sect_h_half = data_ds_sub.sect_h >> 1;
1768 data_ds_sub.lowest_generated = 32767;
1769 data_ds_sub.highest_generated = 0;
1770
1771#if RETROTILE_TRACE_LVL > 0
1772 debug_printf(
1773 RETROTILE_TRACE_LVL, "%d: child sector at %d x %d, %d wide",
1774 iter_depth,
1775 data_ds_sub.sect_x, data_ds_sub.sect_y, data_ds_sub.sect_w );
1776#endif /* RETROTILE_TRACE_LVL */
1777
1779 t, min_z, max_z, tuning, layer_idx, flags, &data_ds_sub,
1780 animation_cb, animation_cb_data );
1781 maug_cleanup_if_not_ok();
1782
1783 _retrotile_ds_update_statistics(
1784 data_ds, data_ds_sub.highest_generated );
1785 _retrotile_ds_update_statistics(
1786 data_ds, data_ds_sub.lowest_generated );
1787 }
1788 }
1789
1790 if(
1791 data_ds->sect_w == (t->tiles_w >> 1) &&
1792 NULL != animation_cb
1793 ) {
1794 retval = animation_cb( animation_cb_data, iter_y );
1795 maug_cleanup_if_not_ok();
1796 }
1797
1798#if RETROTILE_TRACE_LVL > 0
1799 debug_printf(
1800 RETROTILE_TRACE_LVL, "%d return: all sectors complete", iter_depth );
1801#endif /* RETROTILE_TRACE_LVL */
1802
1803cleanup:
1804
1805 if( free_ds_data && NULL != data_ds ) {
1806 maug_munlock( data_ds_h, data_ds );
1807 }
1808
1809 if( free_ds_data && (MAUG_MHANDLE)NULL != data_ds_h ) {
1810 maug_mfree( data_ds_h );
1811 }
1812
1813 return retval;
1814}
1815
1816/* === */
1817
1819 struct RETROTILE* t, retroflat_tile_t min_z, retroflat_tile_t max_z,
1820 uint32_t tuning, size_t layer_idx, uint8_t flags, void* data,
1821 retrotile_ani_cb animation_cb, void* animation_cb_data
1822) {
1823 size_t x = 0,
1824 y = 0;
1825 int16_t offset_x = 0,
1826 offset_y = 0,
1827 finished = 0;
1828 MERROR_RETVAL retval = MERROR_OK;
1829 struct RETROTILE_LAYER* layer = NULL;
1830 int16_t spb = RETROTILE_VORONOI_DEFAULT_SPB;
1831 int16_t drift = RETROTILE_VORONOI_DEFAULT_DRIFT;
1832 MAUG_MHANDLE temp_grid_h = (MAUG_MHANDLE)NULL;
1833 retroflat_tile_t* temp_grid = NULL;
1834 retroflat_tile_t* tiles = NULL;
1835 /* Only use 4 cardinal directions. */
1836 int8_t side_iter = 0;
1837#ifdef MAUG_NO_STDLIB
1838 size_t i = 0;
1839#endif /* MAUG_NO_STDLIB */
1840
1841 layer = retrotile_get_layer_p( t, 0 );
1842
1843 tiles = retrotile_get_tiles_p( layer );
1844
1845 /* Initialize grid to empty. */
1846 retrotile_clear_tiles( t, layer, i );
1847
1848 /* Generate the initial sector starting points. */
1849 for( y = 0 ; t->tiles_w > y ; y += spb ) {
1850 for( x = 0 ; t->tiles_w > x ; x += spb ) {
1851 offset_x = x + ((drift * -1) + (rand() % drift));
1852 offset_y = y + ((drift * -1) + (rand() % drift));
1853
1854 /* Clamp sector offsets onto map borders. */
1855 if( 0 > offset_x ) {
1856 offset_x = 0;
1857 }
1858 if( offset_x >= t->tiles_w ) {
1859 offset_x = t->tiles_w - 1;
1860 }
1861 if( 0 > offset_y ) {
1862 offset_y = 0;
1863 }
1864 if( offset_y >= t->tiles_h ) {
1865 offset_y = t->tiles_h - 1;
1866 }
1867
1868 retrotile_get_tile( t, layer, offset_x, offset_y ) =
1869 min_z + (rand() % max_z);
1870 }
1871 }
1872
1873 maug_malloc_test(
1874 temp_grid_h, sizeof( retroflat_tile_t ), t->tiles_w * t->tiles_h );
1875
1876 maug_mlock( temp_grid_h, temp_grid );
1877 maug_cleanup_if_null_alloc( retroflat_tile_t*, temp_grid );
1878
1879 /* Grow the sector starting points. */
1880 while( !finished ) {
1881 if( NULL != animation_cb ) {
1882 retval = animation_cb( animation_cb_data, -1 );
1883 maug_cleanup_if_not_ok();
1884 }
1885
1886 /* Prepare sampling grid so we're working from unexpanded sections
1887 * below.
1888 */
1889 memcpy(
1890 temp_grid, tiles,
1891 sizeof( retroflat_tile_t ) * t->tiles_w * t->tiles_h );
1892
1893 /* Starting another pass, assume finished until proven otherwise. */
1894 finished = 1;
1895 for( y = 0 ; t->tiles_h > y ; y++ ) {
1896 for( x = 0 ; t->tiles_w > x ; x++ ) {
1897 if( -1 == retrotile_get_tile( t, layer, x, y ) ) {
1898 /* If there are still unfilled tiles, we're not finished
1899 * yet!
1900 */
1901 finished = 0;
1902
1903 /* Skip filled tile. */
1904 continue;
1905 }
1906
1907
1908 for( side_iter = 0 ; 4 > side_iter ; side_iter++ ) {
1909#if RETROTILE_TRACE_LVL > 0
1910 debug_printf( RETROTILE_TRACE_LVL,
1911 SIZE_T_FMT " (%d), " SIZE_T_FMT " (%d) ("
1912 SIZE_T_FMT ", " SIZE_T_FMT ")",
1913 x,
1914 gc_retroflat_offsets4_x[side_iter],
1915 y,
1916 gc_retroflat_offsets4_y[side_iter],
1917 t->tiles_w, t->tiles_h );
1918#endif /* RETROTILE_TRACE_LVL */
1919
1920 /* Iterate through directions to expand. */
1921 /* TODO: Add tuning to select directional probability. */
1922 if(
1923 t->tiles_w > x + gc_retroflat_offsets4_x[side_iter] &&
1924 t->tiles_h > y + gc_retroflat_offsets4_y[side_iter] &&
1925 -1 == temp_grid[
1926 ((y + gc_retroflat_offsets4_y[side_iter]) *
1927 t->tiles_w) +
1928 (x + gc_retroflat_offsets4_x[side_iter])]
1929 ) {
1930 /* Copy center tile to this direction. */
1931 retrotile_get_tile( t, layer,
1932 x + gc_retroflat_offsets4_x[side_iter],
1933 y + gc_retroflat_offsets4_y[side_iter] ) =
1934 retrotile_get_tile( t, layer, x, y );
1935 break;
1936 }
1937 }
1938 }
1939 }
1940 }
1941
1942cleanup:
1943
1944 if( NULL != temp_grid ) {
1945 maug_munlock( temp_grid_h, temp_grid );
1946 }
1947
1948 if( (MAUG_MHANDLE)NULL != temp_grid_h ) {
1949 maug_mfree( temp_grid_h );
1950 }
1951
1952 return retval;
1953}
1954
1955/* === */
1956
1958 struct RETROTILE* t, retroflat_tile_t min_z, retroflat_tile_t max_z,
1959 uint32_t tuning, size_t layer_idx, uint8_t flags, void* data,
1960 retrotile_ani_cb animation_cb, void* animation_cb_data
1961) {
1962 MERROR_RETVAL retval = MERROR_OK;
1963 size_t x = 0,
1964 y = 0;
1965 int16_t side_iter = 0,
1966 sides_avail = 0,
1967 sides_sum = 0;
1968 /* Sides start from 12 on the clock (up). */
1969 struct RETROTILE_LAYER* layer = NULL;
1970
1971 assert( NULL != t );
1972 layer = retrotile_get_layer_p( t, layer_idx );
1973 assert( NULL != layer );
1974
1975 for( y = 0 ; t->tiles_h > y ; y++ ) {
1976 if( NULL != animation_cb ) {
1977 retval = animation_cb( animation_cb_data, y );
1978 maug_cleanup_if_not_ok();
1979 }
1980 for( x = 0 ; t->tiles_w > x ; x++ ) {
1981 /* Reset average. */
1982 sides_avail = 0;
1983 sides_sum = 0;
1984
1985 /* Grab values for available sides. */
1986 for( side_iter = 0 ; 8 > side_iter ; side_iter++ ) {
1987 if(
1988 t->tiles_w <= x + gc_retroflat_offsets8_x[side_iter] ||
1989 t->tiles_h <= y + gc_retroflat_offsets8_y[side_iter]
1990 ) {
1991 continue;
1992 }
1993
1994 sides_avail++;
1995#if RETROTILE_TRACE_LVL > 0
1996 debug_printf(
1998 "si %d: x, y: " SIZE_T_FMT " (+%d), " SIZE_T_FMT
1999 " (+%d) idx: " SIZE_T_FMT,
2000 side_iter,
2001 x + gc_retroflat_offsets8_x[side_iter],
2002 gc_retroflat_offsets8_x[side_iter],
2003 y + gc_retroflat_offsets8_y[side_iter],
2004 gc_retroflat_offsets8_y[side_iter],
2005 ((y + gc_retroflat_offsets8_y[side_iter]) * t->tiles_w) +
2006 x + gc_retroflat_offsets8_x[side_iter] );
2007#endif /* RETROTILE_TRACE_LVL */
2008 sides_sum += retrotile_get_tile(
2009 t, layer,
2010 x + gc_retroflat_offsets8_x[side_iter],
2011 y + gc_retroflat_offsets8_y[side_iter] );
2012 }
2013
2014 retrotile_get_tile( t, layer, x, y ) = sides_sum / sides_avail;
2015 }
2016 }
2017
2018cleanup:
2019
2020 return retval;
2021}
2022
2023/* === */
2024
2026 struct RETROTILE* t, retroflat_tile_t min_z, retroflat_tile_t max_z,
2027 uint32_t tuning, size_t layer_idx, uint8_t flags, void* data,
2028 retrotile_ani_cb animation_cb, void* animation_cb_data
2029) {
2030 MERROR_RETVAL retval = MERROR_OK;
2031 struct RETROTILE_DATA_BORDER* borders =
2032 (struct RETROTILE_DATA_BORDER*)data;
2033 size_t i = 0,
2034 x = 0,
2035 y = 0,
2036 x_plus_1 = 0,
2037 y_plus_1 = 0,
2038 side = 0;
2039 int16_t ctr_iter = 0,
2040 outside_iter = 0;
2041 struct RETROTILE_LAYER* layer = NULL;
2042
2043 assert( NULL != t );
2044 layer = retrotile_get_layer_p( t, layer_idx );
2045 assert( NULL != layer );
2046
2047 /* Reset tile counter for all defined borders. */
2048 for( i = 0 ; 0 <= borders[i].center ; i++ ) {
2049 borders[i].tiles_changed = 0;
2050 }
2051
2052#if RETROTILE_TRACE_LVL > 0
2053 debug_printf( RETROTILE_TRACE_LVL, "adding borders..." );
2054#endif /* RETROTILE_TRACE_LVL */
2055
2056 for( y = 0 ; t->tiles_h > y ; y++ ) {
2057 for( x = 0 ; t->tiles_w > x ; x++ ) {
2058 i = 0;
2059 while( 0 <= borders[i].center ) {
2060 /* Compare/grab current center tile. */
2061 ctr_iter = retrotile_get_tile( t, layer, x, y );
2062#if RETROTILE_TRACE_LVL > 0
2063 debug_printf( RETROTILE_TRACE_LVL,
2064 "x: " SIZE_T_FMT ", y: " SIZE_T_FMT ", 0x%04x vs 0x%04x",
2065 x, y, ctr_iter, borders[i].center );
2066#endif /* RETROTILE_TRACE_LVL */
2067 if( ctr_iter != borders[i].center ) {
2068 i++;
2069 continue;
2070 }
2071
2072#if RETROTILE_TRACE_LVL > 0
2073 debug_printf( RETROTILE_TRACE_LVL, "comparing sides..." );
2074#endif /* RETROTILE_TRACE_LVL */
2075
2076 /* Zeroth pass: look for stick-outs. */
2077 for( side = 0 ; 8 > side ; side += 2 ) {
2078 if(
2079 x + gc_retroflat_offsets8_x[side] > t->tiles_w ||
2080 y + gc_retroflat_offsets8_y[side] > t->tiles_h
2081 ) {
2082 /* Skip out-of-bounds. */
2083 continue;
2084 }
2085 /* Get the outside tile on this side. */
2086 outside_iter = retrotile_get_tile( t, layer,
2087 x + gc_retroflat_offsets8_x[side],
2088 y + gc_retroflat_offsets8_y[side] );
2089
2090 /* Get the outside tile next two clock-steps from this one.
2091 */
2092 if( side + 4 < 8 ) {
2093 x_plus_1 = x + gc_retroflat_offsets8_x[side + 4];
2094 y_plus_1 = y + gc_retroflat_offsets8_y[side + 4];
2095 } else {
2096 x_plus_1 = x + gc_retroflat_offsets8_x[side - 4];
2097 y_plus_1 = y + gc_retroflat_offsets8_y[side - 4];
2098 }
2099
2100 if(
2101 x_plus_1 < t->tiles_w && y_plus_1 < t->tiles_h &&
2102 outside_iter == borders[i].outside &&
2103 outside_iter == retrotile_get_tile( t, layer,
2104 x_plus_1, y_plus_1 )
2105 ) {
2106 /* This has the outside on two opposing sides, so just
2107 * erase it and use the outside. */
2108 retrotile_get_tile( t, layer, x, y ) =
2109 borders[i].outside;
2110 borders[i].tiles_changed++;
2111 goto tile_done;
2112 }
2113 }
2114
2115
2116 /* First pass: look for corners. */
2117 for( side = 0 ; 8 > side ; side += 2 ) {
2118 if(
2119 x + gc_retroflat_offsets8_x[side] > t->tiles_w ||
2120 y + gc_retroflat_offsets8_y[side] > t->tiles_h
2121 ) {
2122 /* Skip out-of-bounds. */
2123 continue;
2124 }
2125 /* Get the outside tile on this side. */
2126 outside_iter = retrotile_get_tile( t, layer,
2127 x + gc_retroflat_offsets8_x[side],
2128 y + gc_retroflat_offsets8_y[side] );
2129
2130 /* Get the outside tile next two clock-steps from this one.
2131 */
2132 if( side + 2 < 8 ) {
2133 x_plus_1 = x + gc_retroflat_offsets8_x[side + 2];
2134 y_plus_1 = y + gc_retroflat_offsets8_y[side + 2];
2135 } else {
2136 x_plus_1 = x + gc_retroflat_offsets8_x[0];
2137 y_plus_1 = y + gc_retroflat_offsets8_y[0];
2138 }
2139
2140 if(
2141 x_plus_1 < t->tiles_w && y_plus_1 < t->tiles_h &&
2142 outside_iter == borders[i].outside &&
2143 outside_iter == retrotile_get_tile( t, layer,
2144 x_plus_1, y_plus_1 )
2145 ) {
2146 /* This has the outside on two sides, so use a corner. */
2147 retrotile_get_tile( t, layer, x, y ) =
2148 borders[i].mod_to[side + 1 < 8 ? side + 1 : 0];
2149 borders[i].tiles_changed++;
2150 goto tile_done;
2151 }
2152 }
2153
2154 /* Second pass (if first pass fails): look for edges. */
2155 for( side = 0 ; 8 > side ; side += 2 ) {
2156 if(
2157 x + gc_retroflat_offsets8_x[side] > t->tiles_w ||
2158 y + gc_retroflat_offsets8_y[side] > t->tiles_h
2159 ) {
2160 /* Skip out-of-bounds. */
2161 continue;
2162 }
2163 /* Get the outside tile on this side. */
2164 outside_iter = retrotile_get_tile( t, layer,
2165 x + gc_retroflat_offsets8_x[side],
2166 y + gc_retroflat_offsets8_y[side] );
2167
2168 if( outside_iter == borders[i].outside ) {
2169 /* It only matches on this side. */
2170#if RETROTILE_TRACE_LVL > 0
2171 debug_printf( RETROTILE_TRACE_LVL, "replacing..." );
2172#endif /* RETROTILE_TRACE_LVL */
2173 retrotile_get_tile( t, layer, x, y ) =
2174 borders[i].mod_to[side];
2175 borders[i].tiles_changed++;
2176 goto tile_done;
2177 }
2178 }
2179
2180tile_done:
2181 /* Tile replaced or not replaceable. */
2182 break;
2183 }
2184 }
2185 }
2186
2187 return retval;
2188}
2189
2190/* === */
2191
2192struct RETROTILE_LAYER* retrotile_get_layer_p(
2193 struct RETROTILE* tilemap, uint32_t layer_idx
2194) {
2195 struct RETROTILE_LAYER* layer_iter = NULL;
2196 uint8_t* tilemap_buf = (uint8_t*)tilemap;
2197
2198 if( 0 == tilemap->layers_count || layer_idx >= tilemap->layers_count ) {
2199 error_printf( "invalid layer " U32_FMT
2200 " requested (of " U32_FMT ")!",
2201 layer_idx, tilemap->layers_count );
2202 return NULL;
2203 }
2204
2205 /* Advance to first grid. */
2206 tilemap_buf += sizeof( struct RETROTILE );
2207 layer_iter = (struct RETROTILE_LAYER*)tilemap_buf;
2208 while( layer_idx > 0 ) {
2209 tilemap_buf += layer_iter->total_sz;
2210 layer_iter = (struct RETROTILE_LAYER*)tilemap_buf;
2211 layer_idx--;
2212 }
2213
2214 return layer_iter;
2215}
2216
2217/* === */
2218
2219MERROR_RETVAL retrotile_alloc(
2220 MAUG_MHANDLE* p_tilemap_h, size_t w, size_t h, size_t layers_count,
2221 const char* tilemap_name, const char* tileset_name
2222) {
2223 struct RETROTILE_LAYER* layer_iter = NULL;
2224 MERROR_RETVAL retval = MERROR_OK;
2225 size_t tilemap_sz = 0;
2226 struct RETROTILE* tilemap = NULL;
2227 size_t i = 0;
2228
2229 tilemap_sz = sizeof( struct RETROTILE ) +
2230 (layers_count * sizeof( struct RETROTILE_LAYER )) +
2231 (layers_count * (w * h * sizeof( retroflat_tile_t ) ));
2232
2233#if RETROTILE_TRACE_LVL > 0
2234 debug_printf(
2235 RETROTILE_TRACE_LVL, "allocating new tilemap " SIZE_T_FMT "x" SIZE_T_FMT
2236 " tiles, " SIZE_T_FMT " layers (" SIZE_T_FMT " bytes)...",
2237 w, h, layers_count, tilemap_sz );
2238#endif /* RETROTILE_TRACE_LVL */
2239
2240 maug_malloc_test( *p_tilemap_h, 1, tilemap_sz );
2241
2242 maug_mlock( *p_tilemap_h, tilemap );
2243 maug_cleanup_if_null_alloc( struct RETROTILE*, tilemap );
2244
2245 maug_mzero( tilemap, tilemap_sz );
2246 tilemap->total_sz = tilemap_sz;
2247 tilemap->layers_count = layers_count;
2248 tilemap->tiles_w = w;
2249 tilemap->tiles_h = h;
2251 tilemap->sz = sizeof( struct RETROTILE );
2252
2253 maug_strncpy( tilemap->name, tilemap_name, RETROTILE_NAME_SZ_MAX );
2254
2255 maug_strncpy( tilemap->tileset, tileset_name, RETROTILE_NAME_SZ_MAX );
2256
2257 for( i = 0 ; layers_count > i ; i++ ) {
2258 layer_iter = retrotile_get_layer_p( tilemap, i );
2259 assert( NULL != layer_iter );
2260 layer_iter->total_sz = sizeof( struct RETROTILE_LAYER ) +
2261 (w * h * sizeof( retroflat_tile_t ));
2262 maug_cleanup_if_not_ok();
2263 layer_iter->sz = sizeof( struct RETROTILE_LAYER );
2264 }
2265
2266cleanup:
2267
2268 if( NULL != tilemap ) {
2269 maug_munlock( *p_tilemap_h, tilemap );
2270 }
2271
2272 return retval;
2273}
2274
2275/* === */
2276
2277void retrotile_format_asset_path(
2278 maug_path path_out, const char* afile,
2279 struct RETROTILE_PARSER* parser
2280) {
2281 /* Load the portrait. */
2282 maug_mzero( path_out, MAUG_PATH_SZ_MAX );
2283 maug_snprintf( path_out, MAUG_PATH_SZ_MAX - 1, "%s/%s",
2284 parser->dirname, afile );
2285}
2286
2287/* === */
2288
2289MERROR_RETVAL retrotile_clear_refresh( retroflat_pxxy_t y_max ) {
2290 MERROR_RETVAL retval = MERROR_OK;
2291#ifndef RETROFLAT_NO_VIEWPORT_REFRESH
2292 int16_t x = 0,
2293 y = 0;
2294
2295#if RETROTILE_TRACE_LVL > 0
2296 debug_printf( RETROTILE_TRACE_LVL,
2297 "clearing " SIZE_T_FMT " vertical viewport pixels (" SIZE_T_FMT
2298 " rows)...",
2299 y_max, y_max / RETROFLAT_TILE_H );
2300#endif /* RETROTILE_TRACE_LVL */
2301
2303 for( y = 0 ; y_max > y ; y += RETROFLAT_TILE_H ) {
2304 for( x = 0 ; retroflat_viewport_screen_w() > x ; x += RETROFLAT_TILE_W ) {
2306 }
2307 }
2308
2309cleanup:
2310
2312
2313#endif /* !RETROFLAT_NO_VIEWPORT_REFRESH */
2314
2315 return retval;
2316}
2317
2318/* === */
2319
2320MERROR_RETVAL retrotile_topdown_draw(
2321 struct RETROFLAT_BITMAP* target,
2322 struct RETROTILE* t, struct MDATA_VECTOR* t_defs
2323) {
2324 int16_t x = 0,
2325 y = 0,
2326 x_tile = 0,
2327 y_tile = 0;
2328 retroflat_tile_t tile_id = 0;
2329 struct RETROTILE_LAYER* layer = NULL;
2330 struct RETROTILE_TILE_DEF* t_def = NULL;
2331 MERROR_RETVAL retval = MERROR_OK;
2332
2333 layer = retrotile_get_layer_p( t, 0 );
2334
2335 mdata_vector_lock( t_defs );
2336 /* TODO: Rework this so it uses viewport tile indexes and then calculates
2337 * screen pixel X/Y from those? For performance?
2338 */
2339 for(
2341 RETROFLAT_TILE_H_BITS) << RETROFLAT_TILE_H_BITS) ;
2342 y < (int)retroflat_viewport_world_y() +
2344 y += RETROFLAT_TILE_H
2345 ) {
2346 for(
2348 RETROFLAT_TILE_W_BITS) << RETROFLAT_TILE_W_BITS) ;
2349 x < (int)retroflat_viewport_world_x() +
2351 x += RETROFLAT_TILE_W
2352 ) {
2353 /* Limit to tiles that exist in the world. */
2354 if(
2355 -1 > x || -1 > y ||
2356 (int)retroflat_viewport_world_w() <= x ||
2357 (int)retroflat_viewport_world_h() <= y
2358 ) {
2359 continue;
2360 }
2361
2362 /* Divide by tile width (16), or shift by 2^4. */
2363 x_tile = x >> RETROFLAT_TILE_W_BITS;
2364 y_tile = y >> RETROFLAT_TILE_H_BITS;
2365
2366 tile_id = retrotile_get_tile( t, layer, x_tile, y_tile );
2367 t_def = mdata_vector_get(
2368 t_defs, tile_id - t->tileset_fgid, struct RETROTILE_TILE_DEF );
2369 if( NULL == t_def ) {
2370 error_printf(
2371 "invalid tile ID: %d (- " SIZE_T_FMT " = " SIZE_T_FMT ")",
2372 tile_id, t->tileset_fgid, tile_id - t->tileset_fgid );
2373 continue;
2374 }
2375 assert( NULL != t_def );
2376
2377#ifndef RETROFLAT_NO_VIEWPORT_REFRESH
2378 /* Check tile refresh buffer. */
2380 if( !retroflat_viewport_tile_is_stale(
2382 y - retroflat_viewport_world_y(), tile_id
2383 ) ) {
2385 continue;
2386 }
2387 /* Noisy! */
2388 /*
2389 debug_printf( RETROTILE_TRACE_LVL, "redrawing tile: %u, %u",
2390 x - retroflat_viewport_world_x(),
2391 y - retroflat_viewport_world_y() );
2392 */
2395 y - retroflat_viewport_world_y(), tile_id );
2397#endif /* !RETROFLAT_NO_VIEWPORT_REFRESH */
2398
2399#ifdef RETROGXC_PRESENT
2400 retrogxc_blit_bitmap( target, t_def->image_cache_id,
2401 t_def->x, t_def->y,
2404 RETROFLAT_TILE_W, RETROFLAT_TILE_H,
2405 retroflat_instance_tile( tile_id ) );
2406#else
2407 retroflat_blit_bitmap( target, &(t_def->image),
2408 t_def->x, t_def->y,
2411 RETROFLAT_TILE_W, RETROFLAT_TILE_H,
2412 retroflat_instance_tile( tile_id ) );
2413#endif /* RETROGXC_PRESENT */
2414 }
2415 }
2416
2417cleanup:
2418
2419 mdata_vector_unlock( t_defs );
2420
2421 return retval;
2422}
2423
2424#else
2425
2426/* This is defined externally so custom token callbacks can reference it. */
2427
2428# define RETROTILE_PARSER_MSTATE_TABLE_CONST( name, idx, tokn, parent, m ) \
2429 extern MAUG_CONST uint8_t SEG_MCONST name;
2430
2431RETROTILE_PARSER_MSTATE_TABLE( RETROTILE_PARSER_MSTATE_TABLE_CONST )
2432
2433# define RETROTILE_CLASS_TABLE_CONSTS( A, a, i ) \
2434 extern MAUG_CONST uint8_t SEG_MCONST RETROTILE_CLASS_ ## A;
2435
2436RETROTILE_CLASS_TABLE( RETROTILE_CLASS_TABLE_CONSTS )
2437
2438
2439#endif /* RETROTIL_C */
2440 /* retrotile */
2442 /* maug_retroflt */
2444
2445#endif /* !RETROTIL_H */
2446
uint16_t MERROR_RETVAL
Return type indicating function returns a value from this list.
Definition: merror.h:28
MERROR_RETVAL mfile_open_read(const maug_path filename, mfile_t *p_file)
Open a file and read it into memory or memory-map it.
#define MAUG_PATH_SZ_MAX
Maximum size allocated for asset paths.
Definition: mfile.h:41
#define MERROR_PREEMPT
Indicates MLISP_AST_NODE can be executed again on next step iter pass.
Definition: merror.h:67
MERROR_RETVAL mfile_assign_path(maug_path tgt, const maug_path src, uint8_t flags)
Copy a maug_path from one place to another, safely observing character limits, etc.
char maug_path[MAUG_PATH_SZ_MAX]
Path/name used to load an asset from disk or access other files.
Definition: mfile.h:141
#define retroflat_instance_tile(instance)
Declare that a given instance ID is for a tile, rather than a sprite.
Definition: retroflt.h:576
MERROR_RETVAL retroflat_blit_bitmap(struct RETROFLAT_BITMAP *target, struct RETROFLAT_BITMAP *src, retroflat_pxxy_t s_x, retroflat_pxxy_t s_y, retroflat_pxxy_t d_x, retroflat_pxxy_t d_y, retroflat_pxxy_t w, retroflat_pxxy_t h, int16_t instance)
Blit the contents of a ::RETROFLAT_BITMAP onto another ::RETROFLAT_BITMAP.
int8_t RETROFLAT_COLOR
Defines an index in the platform-specific color-table.
Definition: retroflt.h:326
#define RETROFLAT_COLOR_TABLE(f)
This macro defines all colors supported by RetroFlat for primative operations, particularly using ret...
Definition: retroflt.h:307
#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:1570
#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:1563
int16_t retroflat_pxxy_t
Type used for surface pixel coordinates.
Definition: retroflt.h:879
#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
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 maug_path 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_TRACE_LVL
If defined, bring debug printf statements up to this level.
Definition: retrotil.h:42
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
A vector of uniformly-sized objects, stored contiguously.
Definition: mdata.h:108
#define mdata_vector_lock(v)
Lock the vector. This should be done when items from the vector are actively being referenced,...
Definition: mdata.h:372
#define mdata_vector_fill(v, ct_new, sz)
Allocate and mark the new slots as active.
Definition: mdata.h:460
#define mdata_vector_unlock(v)
Unlock the vector so items may be added and removed.
Definition: mdata.h:405
#define mdata_vector_ct(v)
Number of items of MDATA_VECTOR::item_sz bytes actively stored in this vector.
Definition: mdata.h:448
Definition: mfile.h:206
#define retroflat_viewport_screen_h()
Return the height of the viewport in pixels.
Definition: retroflt.h:1462
#define retroflat_viewport_world_y()
Return the current viewport Y position in the world in pixels.
Definition: retroflt.h:1410
#define retroflat_viewport_world_x()
Return the current viewport X position in the world in pixels.
Definition: retroflt.h:1402
#define retroflat_viewport_world_w()
Return the current width of the world in pixels.
Definition: retroflt.h:1427
#define retroflat_viewport_screen_w()
Return the width of the viewport in pixels.
Definition: retroflt.h:1455
#define retroflat_viewport_world_h()
Return the current height of the world in pixels.
Definition: retroflt.h:1434
#define retroflat_viewport_unlock_refresh()
Unlock access to RETROFLAT_VIEWPORT::refresh_grid in memory.
Definition: retroflt.h:1525
#define retroflat_viewport_lock_refresh()
Lock access to RETROFLAT_VIEWPORT::refresh_grid in memory.
Definition: retroflt.h:1515
#define retroflat_viewport_set_refresh(x, y, tid)
Set the tile at the given screen pixel coordinates to the given tile ID.
Definition: retroflt.h:1539
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
#define RETROTILE_PROP_TYPE_FILE
Value for RETROTILE_PARSER::last_prop_type indicating file path.
Definition: retrotil.h:141
#define RETROTILE_PARSER_MODE_DEFS
Value for RETROTILE_PARSER::mode indicating the parser is currently parsing tile definitions.
Definition: retrotil.h:80
#define RETROTILE_PROP_TYPE_STRING
Value for RETROTILE_PARSER::last_prop_type indicating string.
Definition: retrotil.h:135
char tilemap_name[RETROTILE_NAME_SZ_MAX+1]
The name to give to the new tilemap.
Definition: retrotil.h:331
#define RETROTILE_PROP_TYPE_INT
Value for RETROTILE_PARSER::last_prop_type indicating integer.
Definition: retrotil.h:147
char last_prop_name[RETROTILE_PROP_NAME_SZ_MAX+1]
The name of the last property key/value pair parsed.
Definition: retrotil.h:327
#define RETROTILE_PARSER_MODE_MAP
Value for RETROTILE_PARSER::mode indicating the parser is currently parsing a tilemap.
Definition: retrotil.h:73
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
#define RETROTILE_PROP_TYPE_OTHER
Value for RETROTILE_PARSER::last_prop_type indicating other type.
Definition: retrotil.h:129
#define RETROTILE_PARSER_FLAG_LITERAL_PATHS
Flag for RETROTILE_PARSER::flags indicating to use literal image asset paths.
Definition: retrotil.h:65
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