11#define RETROGXC_PRESENT 1
13#ifndef RETROGXC_INITIAL_SZ
14# define RETROGXC_INITIAL_SZ 16
17#ifndef RETROGXC_TRACE_LVL
18# define RETROGXC_TRACE_LVL 0
21#define RETROGXC_ERROR_CACHE_MISS (-1)
23#define RETROGXC_ASSET_TYPE_NONE 0
24#define RETROGXC_ASSET_TYPE_BITMAP 1
25#define RETROGXC_ASSET_TYPE_FONT 2
27#define retrogxc_load_bitmap( res_p, flags ) \
28 retrogxc_load_asset( res_p, retrogxc_loader_bitmap, NULL, flags )
30#define retrogxc_load_xpm( res_p, flags ) \
31 retrogxc_load_asset( res_p, retrogxc_loader_xpm, NULL, flags )
33typedef int8_t RETROGXC_ASSET_TYPE;
35typedef RETROGXC_ASSET_TYPE (*retrogxc_loader)(
37 void* data, uint8_t flags );
48 uint16_t glyphs_count;
53void retrogxc_clear_cache();
55void retrogxc_shutdown();
57RETROGXC_ASSET_TYPE retrogxc_loader_bitmap(
59 void* data, uint8_t flags );
61RETROGXC_ASSET_TYPE retrogxc_loader_xpm(
63 void* data, uint8_t flags );
65RETROGXC_ASSET_TYPE retrogxc_loader_font(
67 void* data, uint8_t flags );
69int16_t retrogxc_load_font(
71 uint8_t glyph_h, uint16_t first_glyph, uint16_t glyphs_count );
73int16_t retrogxc_load_asset(
79 size_t s_x,
size_t s_y,
size_t d_x,
size_t d_y,
80 size_t w,
size_t h, int16_t instance );
84 const char* str,
size_t str_sz,
85 size_t font_idx,
size_t x,
size_t y,
86 size_t max_w,
size_t max_h, uint8_t flags );
90 size_t font_idx,
size_t max_w,
size_t max_h,
91 size_t* out_w_p,
size_t* out_h_p, uint8_t flags );
97static struct MDATA_VECTOR gs_retrogxc_bitmaps;
116void retrogxc_clear_cache() {
117 size_t dropped_count = 0;
122 mdata_vector_lock( &gs_retrogxc_bitmaps );
124 while( 0 < mdata_vector_ct( &gs_retrogxc_bitmaps ) ) {
125 asset = mdata_vector_get(
127 assert( NULL != asset );
130 switch( asset->type ) {
131 case RETROGXC_ASSET_TYPE_BITMAP:
132 maug_mlock( asset->handle, bitmap );
133 if( NULL != bitmap ) {
136 maug_munlock( asset->handle, bitmap );
137 maug_mfree( asset->handle );
140 case RETROGXC_ASSET_TYPE_FONT:
142 maug_mfree( asset->handle );
146 mdata_vector_unlock( &gs_retrogxc_bitmaps );
147 mdata_vector_remove( &gs_retrogxc_bitmaps, 0 );
148 mdata_vector_lock( &gs_retrogxc_bitmaps );
151 debug_printf( RETROGXC_TRACE_LVL,
152 "graphics cache cleared (" SIZE_T_FMT
" assets)", dropped_count );
156 if( MERROR_OK == retval ) {
157 mdata_vector_unlock( &gs_retrogxc_bitmaps );
165void retrogxc_shutdown() {
166 retrogxc_clear_cache();
167 mdata_vector_free( &gs_retrogxc_bitmaps );
172RETROGXC_ASSET_TYPE retrogxc_loader_bitmap(
179 assert( (MAUG_MHANDLE)NULL == *handle_p );
182 maug_cleanup_if_null_alloc( MAUG_MHANDLE, *handle_p );
184 maug_mlock( *handle_p, bitmap );
189 maug_cleanup_if_not_ok();
193 if( NULL != bitmap ) {
194 maug_munlock( *handle_p, bitmap );
197 if( MERROR_OK == retval ) {
198 return RETROGXC_ASSET_TYPE_BITMAP;
200 if( NULL != *handle_p ) {
201 maug_mfree( *handle_p );
203 return RETROGXC_ASSET_TYPE_NONE;
211RETROGXC_ASSET_TYPE retrogxc_loader_xpm(
218 assert( (MAUG_MHANDLE)NULL == *handle_p );
221 maug_cleanup_if_null_alloc( MAUG_MHANDLE, *handle_p );
223 maug_mlock( *handle_p, bitmap );
227 retval = retroflat_load_xpm( res_p, bitmap,
flags );
228 maug_cleanup_if_not_ok();
232 if( NULL != bitmap ) {
233 maug_munlock( *handle_p, bitmap );
236 if( MERROR_OK == retval ) {
237 return RETROGXC_ASSET_TYPE_BITMAP;
239 if( NULL != *handle_p ) {
240 maug_mfree( *handle_p );
242 return RETROGXC_ASSET_TYPE_NONE;
250#ifdef RETROFONT_PRESENT
252RETROGXC_ASSET_TYPE retrogxc_loader_font(
259 assert( (MAUG_MHANDLE)NULL == *handle_p );
261 debug_printf( 1,
"loading font into cache: %s (%d, %d, %d)",
262 res_p, parms->glyph_h, parms->first_glyph, parms->glyphs_count );
264 retval = retrofont_load( res_p, handle_p,
265 parms->glyph_h, parms->first_glyph, parms->glyphs_count );
266 maug_cleanup_if_not_ok();
270 if( MERROR_OK == retval ) {
271 return RETROGXC_ASSET_TYPE_FONT;
273 return RETROGXC_ASSET_TYPE_NONE;
281int16_t retrogxc_load_asset(
285 int16_t idx = RETROGXC_ERROR_CACHE_MISS,
289 RETROGXC_ASSET_TYPE asset_type = RETROGXC_ASSET_TYPE_NONE;
294 if( 0 == mdata_vector_ct( &gs_retrogxc_bitmaps ) ) {
295 goto just_load_asset;
299 mdata_vector_lock( &gs_retrogxc_bitmaps );
300 for( i = 0 ; mdata_vector_ct( &gs_retrogxc_bitmaps ) > (size_t)i ; i++ ) {
301 asset_iter = mdata_vector_get(
303 assert( NULL != asset_iter );
304 debug_printf( RETROGXC_TRACE_LVL,
"\"%s\" vs \"%s\"",
305 asset_iter->id, res_p );
307 debug_printf( RETROGXC_TRACE_LVL,
308 "found asset \"%s\" at index %d with type %d!",
309 res_p, i, asset_iter->type );
311 mdata_vector_unlock( &gs_retrogxc_bitmaps );
315 mdata_vector_unlock( &gs_retrogxc_bitmaps );
320 debug_printf( RETROGXC_TRACE_LVL,
321 "asset %s not found in cache; loading...", res_p );
324 asset_type = l( res_p, &asset_new.handle, data, flags );
325 if( RETROGXC_ASSET_TYPE_NONE != asset_type ) {
326 asset_new.type = asset_type;
328 idx = mdata_vector_append(
329 &gs_retrogxc_bitmaps, &asset_new,
334 debug_printf( RETROGXC_TRACE_LVL,
335 "asset type %d, \"%s\" assigned cache ID: %d",
336 asset_type, res_p, idx );
341 error_printf(
"unable to load asset; cache full or not initialized?" );
345 if( MERROR_OK != retval ) {
356 size_t s_x,
size_t s_y,
size_t d_x,
size_t d_y,
357 size_t w,
size_t h, int16_t instance
363 mdata_vector_lock( &gs_retrogxc_bitmaps );
365 if( mdata_vector_ct( &gs_retrogxc_bitmaps ) <= bitmap_idx ) {
366 error_printf(
"invalid bitmap index: " SIZE_T_FMT, bitmap_idx );
367 retval = MERROR_OVERFLOW;
371 asset = mdata_vector_get(
374 if( RETROGXC_ASSET_TYPE_BITMAP != asset->type ) {
376 "index " SIZE_T_FMT
" not present in cache or not bitmap (%d)!",
377 bitmap_idx, asset->type );
378 retval = MERROR_FILE;
382 maug_mlock( asset->handle, bitmap );
385 target, bitmap, s_x, s_y, d_x, d_y, w, h, instance );
389 if( NULL != bitmap ) {
390 maug_munlock( asset->handle, bitmap );
393 mdata_vector_unlock( &gs_retrogxc_bitmaps );
406 mdata_vector_lock( &gs_retrogxc_bitmaps );
408 if( mdata_vector_ct( &gs_retrogxc_bitmaps ) <= bitmap_idx ) {
409 error_printf(
"invalid bitmap index: " SIZE_T_FMT, bitmap_idx );
410 retval = MERROR_OVERFLOW;
414 asset = mdata_vector_get(
417 if( RETROGXC_ASSET_TYPE_BITMAP != asset->type ) {
419 "index " SIZE_T_FMT
" not present in cache or not bitmap (%d)!",
420 bitmap_idx, asset->type );
421 retval = MERROR_FILE;
425 maug_mlock( asset->handle, bitmap );
427 w_out = retroflat_bitmap_w( bitmap );
431 if( NULL != bitmap ) {
432 maug_munlock( asset->handle, bitmap );
435 if( MERROR_OK != retval ) {
439 mdata_vector_unlock( &gs_retrogxc_bitmaps );
446#ifdef RETROFONT_PRESENT
448int16_t retrogxc_load_font(
450 uint8_t glyph_h, uint16_t first_glyph, uint16_t glyphs_count
455 parms.glyph_h = glyph_h;
456 parms.first_glyph = first_glyph;
457 parms.glyphs_count = glyphs_count;
459 idx = retrogxc_load_asset( font_name, retrogxc_loader_font, &parms, 0 );
468 const char* str,
size_t str_sz,
469 size_t font_idx,
size_t x,
size_t y,
470 size_t max_w,
size_t max_h, uint8_t flags
475 mdata_vector_lock( &gs_retrogxc_bitmaps );
477 if( mdata_vector_ct( &gs_retrogxc_bitmaps ) <= font_idx ) {
478 error_printf(
"invalid font index: " SIZE_T_FMT, font_idx );
479 retval = MERROR_OVERFLOW;
483 asset = mdata_vector_get(
486 if( RETROGXC_ASSET_TYPE_FONT != asset->type ) {
488 "index " SIZE_T_FMT
" not present in cache or not font (%d)!",
489 font_idx, asset->type );
490 retval = MERROR_FILE;
495 target, color, str, str_sz, asset->handle, x, y, max_w, max_h, flags );
499 mdata_vector_unlock( &gs_retrogxc_bitmaps );
508 size_t font_idx,
size_t max_w,
size_t max_h,
509 size_t* out_w_p,
size_t* out_h_p, uint8_t flags
514 mdata_vector_lock( &gs_retrogxc_bitmaps );
516 if( mdata_vector_ct( &gs_retrogxc_bitmaps ) <= font_idx ) {
517 error_printf(
"invalid font index: " SIZE_T_FMT, font_idx );
518 retval = MERROR_OVERFLOW;
522 asset = mdata_vector_get(
525 if( RETROGXC_ASSET_TYPE_FONT != asset->type ) {
527 "index " SIZE_T_FMT
" not present in cache or not font (%d)!",
528 font_idx, asset->type );
529 retval = MERROR_FILE;
534 target, str, str_sz, asset->handle,
535 max_w, max_h, out_w_p, out_h_p, flags );
539 mdata_vector_unlock( &gs_retrogxc_bitmaps );
int MERROR_RETVAL
Return type indicating function returns a value from this list.
Definition merror.h:19
#define maug_mzero(ptr, sz)
Zero the block of memory pointed to by ptr.
Definition mmem.h:62
#define retroflat_cmp_asset_path(a, b)
Compare two asset paths. Return 0 if they're the same.
Definition retroflt.h:779
char retroflat_asset_path[RETROFLAT_PATH_MAX]
Path/name used to load an asset from disk.
Definition retroflt.h:774
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.
void retroflat_destroy_bitmap(struct RETROFLAT_BITMAP *bitmap)
Unload a bitmap from a RETROFLAT_BITMAP struct. The struct, itself, is not freed (in case it is on th...
MERROR_RETVAL retroflat_load_bitmap(const char *filename, struct RETROFLAT_BITMAP *bmp_out, uint8_t flags)
Load a bitmap into the given RETROFLAT_BITMAP structure if it is available. Bitmaps are subject to th...
int8_t RETROFLAT_COLOR
Defines an index in the platform-specific color-table.
Definition retroflt.h:307
#define RETROFLAT_ASSETS_PATH_MAX
Maximum size of the assets path, to allow room for appending.
Definition retroflt.h:749
Platform-specific bitmap structure. retroflat_bitmap_ok() can be used on a pointer to it to determine...
Definition retpltd.h:21
uint8_t flags
Platform-specific bitmap flags.
Definition retpltd.h:25