28#define RETROGXC_PRESENT 1
30#ifndef RETROGXC_INITIAL_SZ
31# define RETROGXC_INITIAL_SZ 16
34#ifndef RETROGXC_TRACE_LVL
35# define RETROGXC_TRACE_LVL 0
38#define RETROGXC_ERROR_CACHE_MISS (-1)
40#define RETROGXC_ASSET_TYPE_NONE 0
41#define RETROGXC_ASSET_TYPE_BITMAP 1
42#define RETROGXC_ASSET_TYPE_FONT 2
44#define retrogxc_load_bitmap( res_p, flags ) \
45 retrogxc_load_asset( res_p, retrogxc_loader_bitmap, NULL, flags )
47typedef int8_t retrogxc_asset_type_t;
49typedef retrogxc_asset_type_t (*retrogxc_loader)(
50 const maug_path res_p, MAUG_MHANDLE* handle_p,
51 void* data, uint8_t flags );
62 uint16_t glyphs_count;
67void retrogxc_clear_cache();
69void retrogxc_shutdown();
71retrogxc_asset_type_t retrogxc_loader_bitmap(
72 const maug_path res_p, MAUG_MHANDLE* handle_p,
73 void* data, uint8_t flags );
75retrogxc_asset_type_t retrogxc_loader_xpm(
76 const maug_path res_p, MAUG_MHANDLE* handle_p,
77 void* data, uint8_t flags );
79retrogxc_asset_type_t retrogxc_loader_font(
80 const maug_path res_p, MAUG_MHANDLE* handle_p,
81 void* data, uint8_t flags );
83int16_t retrogxc_load_font(
85 uint8_t glyph_h, uint16_t first_glyph, uint16_t glyphs_count );
92 const maug_path res_p, retrogxc_loader l,
void* data,
102 size_t asset_idx, retrogxc_asset_type_t asset_type );
105 retroflat_blit_t* target,
size_t bitmap_idx,
112 const char* str,
size_t str_sz,
118 const char* str,
size_t str_sz,
124 retroflat_blit_t* target,
const char* str,
size_t str_sz,
133static struct MDATA_VECTOR SEG_MGLOBAL gs_retrogxc_bitmaps;
152void retrogxc_clear_cache() {
153 size_t dropped_count = 0;
155 retroflat_blit_t* bitmap = NULL;
166 asset = mdata_vector_get(
168 assert( NULL != asset );
171 switch( asset->type ) {
172 case RETROGXC_ASSET_TYPE_BITMAP:
173 maug_mlock( asset->handle, bitmap );
174 if( NULL != bitmap ) {
175 retroflat_2d_destroy_bitmap( bitmap );
177 maug_munlock( asset->handle, bitmap );
178 maug_mfree( asset->handle );
181 case RETROGXC_ASSET_TYPE_FONT:
183 retrofont_free( &(asset->handle) );
192 debug_printf( RETROGXC_TRACE_LVL,
193 "graphics cache cleared (" SIZE_T_FMT
" assets)", dropped_count );
197 if( MERROR_OK == retval ) {
206void retrogxc_shutdown() {
207 retrogxc_clear_cache();
208 mdata_vector_free( &gs_retrogxc_bitmaps );
213retrogxc_asset_type_t retrogxc_loader_bitmap(
214 const maug_path res_p, MAUG_MHANDLE* handle_p,
void* data,
218 retroflat_blit_t* bitmap = NULL;
220 assert( (MAUG_MHANDLE)NULL == *handle_p );
222 maug_malloc_test( *handle_p, 1,
sizeof( retroflat_blit_t ) );
224 maug_mlock( *handle_p, bitmap );
225 maug_cleanup_if_null_alloc( retroflat_blit_t*, bitmap );
229 retval = retroflat_load_xpm( res_p, bitmap, flags );
231 retval = retroflat_2d_load_bitmap( res_p, bitmap, flags );
233 maug_cleanup_if_not_ok();
237 if( NULL != bitmap ) {
238 maug_munlock( *handle_p, bitmap );
241 if( MERROR_OK == retval ) {
242 return RETROGXC_ASSET_TYPE_BITMAP;
244 if( (MAUG_MHANDLE)NULL != *handle_p ) {
245 maug_mfree( *handle_p );
247 return RETROGXC_ASSET_TYPE_NONE;
253#ifdef RETROFONT_PRESENT
255retrogxc_asset_type_t retrogxc_loader_font(
256 const maug_path res_p, MAUG_MHANDLE* handle_p,
void* data,
262 assert( (MAUG_MHANDLE)NULL == *handle_p );
264 debug_printf( 1,
"loading font into cache: %s (%d, %d, %d)",
265 res_p, parms->glyph_h, parms->first_glyph, parms->glyphs_count );
268 parms->glyph_h, parms->first_glyph, parms->glyphs_count );
269 maug_cleanup_if_not_ok();
273 if( MERROR_OK == retval ) {
274 return RETROGXC_ASSET_TYPE_FONT;
276 return RETROGXC_ASSET_TYPE_NONE;
285 const maug_path res_p, retrogxc_loader l,
void* data,
288 int16_t idx = RETROGXC_ERROR_CACHE_MISS,
292 retrogxc_asset_type_t asset_type = RETROGXC_ASSET_TYPE_NONE;
298 goto just_load_asset;
303 for( i = 0 ;
mdata_vector_ct( &gs_retrogxc_bitmaps ) > (size_t)i ; i++ ) {
304 asset_iter = mdata_vector_get(
306 assert( NULL != asset_iter );
307 debug_printf( RETROGXC_TRACE_LVL,
"\"%s\" vs \"%s\"",
308 asset_iter->id, res_p );
310 debug_printf( RETROGXC_TRACE_LVL,
311 "found asset \"%s\" at index %d with type %d!",
312 res_p, i, asset_iter->type );
323 debug_printf( RETROGXC_TRACE_LVL,
324 "asset %s not found in cache; loading...", res_p );
327 asset_type = l( res_p, &asset_new.handle, data, flags );
328 if( RETROGXC_ASSET_TYPE_NONE != asset_type ) {
329 asset_new.type = asset_type;
332 &gs_retrogxc_bitmaps, &asset_new,
337 debug_printf( RETROGXC_TRACE_LVL,
338 "asset type %d, \"%s\" assigned cache ID: %d",
339 asset_type, res_p, idx );
344 error_printf(
"unable to load asset; cache full or not initialized?" );
348 if( MERROR_OK != retval ) {
358 size_t asset_idx, retrogxc_asset_type_t asset_type
360 MAUG_MHANDLE handle_out = (MAUG_MHANDLE)NULL;
364 assert( (MAUG_MHANDLE)NULL != gs_retrogxc_bitmaps.data_h );
369 error_printf(
"invalid asset index: " SIZE_T_FMT, asset_idx );
373 asset = mdata_vector_get(
376 if( asset_type != asset->type ) {
379 " not present in cache or not requested type %d (%d)!",
380 asset_idx, asset_type, asset->type );
384 handle_out = asset->handle;
388 if( MERROR_OK != retval ) {
400 retroflat_blit_t* target,
size_t bitmap_idx,
407 retroflat_blit_t* bitmap = NULL;
409 assert( (MAUG_MHANDLE)NULL != gs_retrogxc_bitmaps.data_h );
414 error_printf(
"invalid bitmap index: " SIZE_T_FMT, bitmap_idx );
415 retval = MERROR_OVERFLOW;
419 asset = mdata_vector_get(
422 if( RETROGXC_ASSET_TYPE_BITMAP != asset->type ) {
424 "index " SIZE_T_FMT
" not present in cache or not bitmap (%d)!",
425 bitmap_idx, asset->type );
426 retval = MERROR_FILE;
430 maug_mlock( asset->handle, bitmap );
432 retval = retroflat_2d_blit_bitmap(
433 target, bitmap, s_x, s_y, d_x, d_y, w, h, instance );
437 if( NULL != bitmap ) {
438 maug_munlock( asset->handle, bitmap );
453 retroflat_blit_t* bitmap = NULL;
458 error_printf(
"invalid bitmap index: " SIZE_T_FMT, bitmap_idx );
459 retval = MERROR_OVERFLOW;
463 asset = mdata_vector_get(
466 if( RETROGXC_ASSET_TYPE_BITMAP != asset->type ) {
468 "index " SIZE_T_FMT
" not present in cache or not bitmap (%d)!",
469 bitmap_idx, asset->type );
470 retval = MERROR_FILE;
474 maug_mlock( asset->handle, bitmap );
477 *p_w = retroflat_2d_bitmap_w( bitmap );
481 *p_h = retroflat_2d_bitmap_h( bitmap );
486 if( NULL != bitmap ) {
487 maug_munlock( asset->handle, bitmap );
497#ifdef RETROFONT_PRESENT
499int16_t retrogxc_load_font(
501 uint8_t glyph_h, uint16_t first_glyph, uint16_t glyphs_count
506 parms.glyph_h = glyph_h;
507 parms.first_glyph = first_glyph;
508 parms.glyphs_count = glyphs_count;
521 const char* str,
size_t str_sz,
526 return retrogxc_string_indent(
527 target, color, str, str_sz, font_idx, x, y, max_w, max_h, 0, flags );
534 const char* str,
size_t str_sz,
545 error_printf(
"invalid font index: " SIZE_T_FMT, font_idx );
546 retval = MERROR_OVERFLOW;
550 asset = mdata_vector_get(
553 if( RETROGXC_ASSET_TYPE_FONT != asset->type ) {
555 "index " SIZE_T_FMT
" not present in cache or not font (%d)!",
556 font_idx, asset->type );
557 retval = MERROR_FILE;
561 retrofont_string_indent(
562 target, color, str, str_sz, asset->handle, x, y, max_w, max_h,
575 retroflat_blit_t* target,
const char* str,
size_t str_sz,
585 error_printf(
"invalid font index: " SIZE_T_FMT, font_idx );
586 retval = MERROR_OVERFLOW;
590 asset = mdata_vector_get(
593 if( RETROGXC_ASSET_TYPE_FONT != asset->type ) {
595 "index " SIZE_T_FMT
" not present in cache or not font (%d)!",
596 font_idx, asset->type );
597 retval = MERROR_FILE;
602 target, str, str_sz, asset->handle,
603 max_w, max_h, out_w_p, out_h_p, flags );
uint16_t MERROR_RETVAL
Return type indicating function returns a value from this list.
Definition: merror.h:28
#define mfile_cmp_path(a, b)
Compare two asset paths. Return 0 if they're the same.
Definition: mfile.h:146
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
int8_t RETROFLAT_COLOR
Defines an index in the platform-specific color-table.
Definition: retroflt.h:326
int16_t retroflat_pxxy_t
Type used for surface pixel coordinates.
Definition: retroflt.h:879
int16_t retrogxc_load_asset(const maug_path res_p, retrogxc_loader l, void *data, uint8_t flags)
Try to load an asset by file path. Return a cached copy if it's already been loaded.
MAUG_MHANDLE retrogxc_get_asset(size_t asset_idx, retrogxc_asset_type_t asset_type)
Retrive an asset for which we have a prior cached index.
ssize_t mdata_vector_append(struct MDATA_VECTOR *v, const void *item, size_t item_sz)
Append an item to the specified vector.
MERROR_RETVAL mdata_vector_remove(struct MDATA_VECTOR *v, size_t idx)
Remove item at the given index, shifting subsequent items up by 1.
MERROR_RETVAL retrofont_load(const char *font_name, MAUG_MHANDLE *p_font_h, uint8_t glyph_h, uint16_t first_glyph, uint16_t glyphs_count)
Load a font for drawing.
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_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: retrogxc.h:53
Definition: retrogxc.h:59