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)(
 
   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(
 
   73   void* data, uint8_t flags );
 
   75retrogxc_asset_type_t retrogxc_loader_xpm(
 
   77   void* data, uint8_t flags );
 
   79retrogxc_asset_type_t retrogxc_loader_font(
 
   81   void* data, uint8_t flags );
 
   83int16_t retrogxc_load_font(
 
   85   uint8_t glyph_h, uint16_t first_glyph, uint16_t glyphs_count );
 
   87int16_t retrogxc_load_asset(
 
   92   retroflat_blit_t* target, 
size_t bitmap_idx,
 
   93   size_t s_x, 
size_t s_y, 
size_t d_x, 
size_t d_y,
 
   94   size_t w, 
size_t h, int16_t instance );
 
   98   const char* str, 
size_t str_sz,
 
   99   size_t font_idx, 
size_t x, 
size_t y,
 
  100   size_t max_w, 
size_t max_h, uint8_t flags );
 
  104   const char* str, 
size_t str_sz,
 
  105   size_t font_idx, 
size_t x, 
size_t y,
 
  106   size_t max_w, 
size_t max_h, 
size_t indent, uint8_t flags );
 
  109   retroflat_blit_t* target, 
const char* str, 
size_t str_sz,
 
  110   size_t font_idx, 
size_t max_w, 
size_t max_h,
 
  111   size_t* out_w_p, 
size_t* out_h_p, uint8_t flags );
 
  118static struct MDATA_VECTOR SEG_MGLOBAL gs_retrogxc_bitmaps;
 
  137void retrogxc_clear_cache() {
 
  138   size_t dropped_count = 0;
 
  140   retroflat_blit_t* bitmap = NULL;
 
  151      asset = mdata_vector_get(
 
  153      assert( NULL != asset );
 
  156      switch( asset->type ) {
 
  157      case RETROGXC_ASSET_TYPE_BITMAP:
 
  158         maug_mlock( asset->handle, bitmap );
 
  159         if( NULL != bitmap ) {
 
  160            retroflat_2d_destroy_bitmap( bitmap );
 
  162         maug_munlock( asset->handle, bitmap );
 
  163         maug_mfree( asset->handle );
 
  166      case RETROGXC_ASSET_TYPE_FONT:
 
  168         retrofont_free( &(asset->handle) );
 
  177   debug_printf( RETROGXC_TRACE_LVL,
 
  178      "graphics cache cleared (" SIZE_T_FMT 
" assets)", dropped_count );
 
  182   if( MERROR_OK == retval ) {
 
  191void retrogxc_shutdown() {
 
  192   retrogxc_clear_cache();
 
  193   mdata_vector_free( &gs_retrogxc_bitmaps );
 
  198retrogxc_asset_type_t retrogxc_loader_bitmap(
 
  203   retroflat_blit_t* bitmap = NULL;
 
  205   assert( (MAUG_MHANDLE)NULL == *handle_p );
 
  207   *handle_p = maug_malloc( 1, 
sizeof( retroflat_blit_t ) );
 
  208   maug_cleanup_if_null_alloc( MAUG_MHANDLE, *handle_p );
 
  210   maug_mlock( *handle_p, bitmap );
 
  211   maug_cleanup_if_null_alloc( retroflat_blit_t*, bitmap );
 
  215   retval = retroflat_load_xpm( res_p, bitmap, flags );
 
  217   retval = retroflat_2d_load_bitmap( res_p, bitmap, flags );
 
  219   maug_cleanup_if_not_ok();
 
  223   if( NULL != bitmap ) {
 
  224      maug_munlock( *handle_p, bitmap );
 
  227   if( MERROR_OK == retval ) {
 
  228      return RETROGXC_ASSET_TYPE_BITMAP;
 
  230      if( (MAUG_MHANDLE)NULL != *handle_p ) {
 
  231         maug_mfree( *handle_p );
 
  233      return RETROGXC_ASSET_TYPE_NONE;
 
  239#ifdef RETROFONT_PRESENT 
  241retrogxc_asset_type_t retrogxc_loader_font(
 
  248   assert( (MAUG_MHANDLE)NULL == *handle_p );
 
  250   debug_printf( 1, 
"loading font into cache: %s (%d, %d, %d)",
 
  251      res_p, parms->glyph_h, parms->first_glyph, parms->glyphs_count );
 
  254      parms->glyph_h, parms->first_glyph, parms->glyphs_count );
 
  255   maug_cleanup_if_not_ok();
 
  259   if( MERROR_OK == retval ) {
 
  260      return RETROGXC_ASSET_TYPE_FONT;
 
  262      return RETROGXC_ASSET_TYPE_NONE;
 
  270int16_t retrogxc_load_asset(
 
  274   int16_t idx = RETROGXC_ERROR_CACHE_MISS,
 
  278   retrogxc_asset_type_t asset_type = RETROGXC_ASSET_TYPE_NONE;
 
  284      goto just_load_asset;
 
  289   for( i = 0 ; 
mdata_vector_ct( &gs_retrogxc_bitmaps ) > (size_t)i ; i++ ) {
 
  290      asset_iter = mdata_vector_get(
 
  292      assert( NULL != asset_iter );
 
  293      debug_printf( RETROGXC_TRACE_LVL, 
"\"%s\" vs \"%s\"",
 
  294        asset_iter->id, res_p );
 
  296         debug_printf( RETROGXC_TRACE_LVL,
 
  297            "found asset \"%s\" at index %d with type %d!",
 
  298            res_p, i, asset_iter->type );
 
  309   debug_printf( RETROGXC_TRACE_LVL,
 
  310      "asset %s not found in cache; loading...", res_p );
 
  313   asset_type = l( res_p, &asset_new.handle, data, flags );
 
  314   if( RETROGXC_ASSET_TYPE_NONE != asset_type ) {
 
  315      asset_new.type = asset_type;
 
  316      mfile_assign_path( asset_new.id, res_p, 0 );
 
  318         &gs_retrogxc_bitmaps, &asset_new,
 
  323      debug_printf( RETROGXC_TRACE_LVL,
 
  324         "asset type %d, \"%s\" assigned cache ID: %d",
 
  325         asset_type, res_p, idx );
 
  330   error_printf( 
"unable to load asset; cache full or not initialized?" );
 
  334   if( MERROR_OK != retval ) {
 
  344   retroflat_blit_t* target, 
size_t bitmap_idx,
 
  345   size_t s_x, 
size_t s_y, 
size_t d_x, 
size_t d_y,
 
  346   size_t w, 
size_t h, int16_t instance
 
  350   retroflat_blit_t* bitmap = NULL;
 
  352   assert( (MAUG_MHANDLE)NULL != gs_retrogxc_bitmaps.data_h );
 
  357      error_printf( 
"invalid bitmap index: " SIZE_T_FMT, bitmap_idx );
 
  358      retval = MERROR_OVERFLOW;
 
  362   asset = mdata_vector_get(
 
  365   if( RETROGXC_ASSET_TYPE_BITMAP != asset->type ) {
 
  367         "index " SIZE_T_FMT 
" not present in cache or not bitmap (%d)!",
 
  368         bitmap_idx, asset->type );
 
  369      retval = MERROR_FILE;
 
  373   maug_mlock( asset->handle, bitmap );
 
  375   retval = retroflat_2d_blit_bitmap(
 
  376      target, bitmap, s_x, s_y, d_x, d_y, w, h, instance );
 
  380   if( NULL != bitmap ) {
 
  381      maug_munlock( asset->handle, bitmap );
 
  396   retroflat_blit_t* bitmap = NULL;
 
  401      error_printf( 
"invalid bitmap index: " SIZE_T_FMT, bitmap_idx );
 
  402      retval = MERROR_OVERFLOW;
 
  406   asset = mdata_vector_get(
 
  409   if( RETROGXC_ASSET_TYPE_BITMAP != asset->type ) {
 
  411         "index " SIZE_T_FMT 
" not present in cache or not bitmap (%d)!",
 
  412         bitmap_idx, asset->type );
 
  413      retval = MERROR_FILE;
 
  417   maug_mlock( asset->handle, bitmap );
 
  420      *p_w = retroflat_2d_bitmap_w( bitmap );
 
  424      *p_h = retroflat_2d_bitmap_h( bitmap );
 
  429   if( NULL != bitmap ) {
 
  430      maug_munlock( asset->handle, bitmap );
 
  440#ifdef RETROFONT_PRESENT 
  442int16_t retrogxc_load_font(
 
  444   uint8_t glyph_h, uint16_t first_glyph, uint16_t glyphs_count 
 
  449   parms.glyph_h = glyph_h;
 
  450   parms.first_glyph = first_glyph;
 
  451   parms.glyphs_count = glyphs_count;
 
  453   idx = retrogxc_load_asset( font_name, retrogxc_loader_font, &parms, 0 );
 
  462   const char* str, 
size_t str_sz,
 
  463   size_t font_idx, 
size_t x, 
size_t y,
 
  464   size_t max_w, 
size_t max_h, uint8_t flags
 
  467   return retrogxc_string_indent(
 
  468      target, color, str, str_sz, font_idx, x, y, max_w, max_h, 0, flags );
 
  475   const char* str, 
size_t str_sz,
 
  476   size_t font_idx, 
size_t x, 
size_t y,
 
  477   size_t max_w, 
size_t max_h, 
size_t indent, uint8_t flags
 
  485      error_printf( 
"invalid font index: " SIZE_T_FMT, font_idx );
 
  486      retval = MERROR_OVERFLOW;
 
  490   asset = mdata_vector_get(
 
  493   if( RETROGXC_ASSET_TYPE_FONT != asset->type ) {
 
  495         "index " SIZE_T_FMT 
" not present in cache or not font (%d)!",
 
  496         font_idx, asset->type );
 
  497      retval = MERROR_FILE;
 
  501   retrofont_string_indent(
 
  502      target, color, str, str_sz, asset->handle, x, y, max_w, max_h,
 
  515   retroflat_blit_t* target, 
const char* str, 
size_t str_sz,
 
  516   size_t font_idx, 
size_t max_w, 
size_t max_h,
 
  517   size_t* out_w_p, 
size_t* out_h_p, uint8_t flags
 
  525      error_printf( 
"invalid font index: " SIZE_T_FMT, font_idx );
 
  526      retval = MERROR_OVERFLOW;
 
  530   asset = mdata_vector_get(
 
  533   if( RETROGXC_ASSET_TYPE_FONT != asset->type ) {
 
  535         "index " SIZE_T_FMT 
" not present in cache or not font (%d)!",
 
  536         font_idx, asset->type );
 
  537      retval = MERROR_FILE;
 
  542      target, str, str_sz, asset->handle,
 
  543      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:19
char retroflat_asset_path[MAUG_PATH_SZ_MAX+1]
Path/name used to load an asset from disk.
Definition: mfile.h:129
#define mfile_cmp_path(a, b)
Compare two asset paths. Return 0 if they're the same.
Definition: mfile.h:134
int8_t RETROFLAT_COLOR
Defines an index in the platform-specific color-table.
Definition: retroflt.h:325
size_t retroflat_pxxy_t
Type used for surface pixel coordinates.
Definition: retroflt.h:870
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:93
#define mdata_vector_lock(v)
Lock the vector. This should be done when items from the vector are actively being referenced,...
Definition: mdata.h:320
#define mdata_vector_unlock(v)
Unlock the vector so items may be added and removed.
Definition: mdata.h:353
#define mdata_vector_ct(v)
Number of items of MDATA_VECTOR::item_sz bytes actively stored in this vector.
Definition: mdata.h:396
Definition: retrogxc.h:53
Definition: retrogxc.h:59