maug
Quick and dirty C mini-augmentation library.
Loading...
Searching...
No Matches
retroflt.h
Go to the documentation of this file.
1
2#ifndef RETROFLT_H
3#define RETROFLT_H
4
5#ifdef RETROFLAT_OPENGL
6# define RETROFLAT_BMP_TEX
7#endif /* RETROFLAT_OPENGL */
8
13
19typedef int16_t retroflat_tile_t;
20
21typedef int16_t retrotile_coord_t;
22
24
44
165
279
280/* === Generic Includes and Defines === */
281
282#ifndef RETROFLAT_BITMAP_TRACE_LVL
283# define RETROFLAT_BITMAP_TRACE_LVL 0
284#endif /* !RETROFLAT_BITMAP_TRACE_LVL */
285
286#ifndef RETROINPUT_TRACE_LVL
287# define RETROINPUT_TRACE_LVL 0
288#endif /* !RETROINPUT_TRACE_LVL */
289
290#include <stdarg.h>
291
292#include <marge.h>
293
298
309#define RETROFLAT_COLOR_TABLE( f ) \
310 f( 0, black, BLACK, 0, 0, 0, BLACK, BLACK ) \
311 f( 1, darkblue, DARKBLUE, 0, 0, 170, CYAN, BLACK ) \
312 f( 2, darkgreen, DARKGREEN, 0, 170, 0, CYAN, BLACK ) \
313 f( 3, teal, TEAL, 0, 170, 170, CYAN, CYAN ) \
314 f( 4, darkred, DARKRED, 170, 0, 0, MAGENTA, BLACK ) \
315 f( 5, violet, VIOLET, 170, 0, 170, MAGENTA, BLACK ) \
316 f( 6, brown, BROWN, 170, 85, 0, CYAN, MAGENTA ) \
317 f( 7, gray, GRAY, 170, 170, 170, WHITE, BLACK ) \
318 f( 8, darkgray, DARKGRAY, 85, 85, 85, WHITE, BLACK ) \
319 f( 9, blue, BLUE, 85, 85, 255, CYAN, WHITE ) \
320 f( 10, green, GREEN, 85, 255, 85, CYAN, CYAN ) \
321 f( 11, cyan, CYAN, 85, 255, 255, CYAN, CYAN ) \
322 f( 12, red, RED, 255, 85, 85, MAGENTA, WHITE ) \
323 f( 13, magenta, MAGENTA, 255, 85, 255, MAGENTA, MAGENTA ) \
324 f( 14, yellow, YELLOW, 255, 255, 85, CYAN, MAGENTA ) \
325 f( 15, white, WHITE, 255, 255, 255, WHITE, WHITE )
326
328typedef int8_t RETROFLAT_COLOR;
329
330# define RETROFLAT_COLOR_NULL (-1)
331
332# define RETROFLAT_COLORS_CT 16
333
335
336/* TODO: Mouse is broken under DOS/Allegro. */
337#if defined( RETROFLAT_OS_UNIX ) || defined( RETROFLAT_OS_WIN )
338#define RETROFLAT_MOUSE
339#endif /* RETROFLAT_OS_WIN || RETROFLAT_OS_WIN */
340
346
348#define RETROFLAT_OK 0x00
349#define RETROFLAT_ERROR_ENGINE 0x01
350#define RETROFLAT_ERROR_GRAPHICS 0x02
351#define RETROFLAT_ERROR_MOUSE 0x04
352
357#define RETROFLAT_ERROR_BITMAP 0x08
358#define RETROFLAT_ERROR_TIMER 0x0f
359 /* maug_retroflt_retval */
361
371
376#define RETROFLAT_DRAW_FLAG_FILL 0x01
377
384#define RETROFLAT_FONT_FLAG_ALL_CAPS 0x02
385 /* maug_retroflt_drawing */
387
394#define RETROFLAT_STATE_FLAG_RUNNING 0x01
395
401#define RETROFLAT_STATE_FLAG_UNLOCK_FPS 0x02
402
408#define RETROFLAT_STATE_FLAG_KEY_REPEAT 0x04
409
415#define RETROFLAT_STATE_FLAG_SCREENSAVER 0x08
416
420#define RETROFLAT_STATE_FLAG_HWSCROLLING 0x10
421
426#define RETROFLAT_STATE_FLAG_WAIT_FOR_FPS 0x20
427
432#define RETROFLAT_STATE_FLAG_USE_GXC 0x40
433
438#define RETROFLAT_STATE_FLAG_QUIT_REQUESTED 0x80
439
446
450#define RETROFLAT_MSG_FLAG_TYPE_MASK 0x07
451
456#define RETROFLAT_MSG_FLAG_ERROR 0x01
457
463#define RETROFLAT_MSG_FLAG_INFO 0x02
464
470#define RETROFLAT_MSG_FLAG_WARNING 0x04
471 /* maug_retroflt_msg_api */
473
474struct RETROFLAT_STATE;
475
487
495#define RETROFLAT_VDP_FLAG_PXLOCK 0x01
496
509
510#define retroflat_vdp_available() (NULL != g_retroflat_state->vdp_exe)
511 /* maug_retroflt_vdp */
513
518
519typedef MERROR_RETVAL (*retroflat_proc_resize_t)(
520 uint16_t new_w, uint16_t new_h, void* data );
521
522#define RETROFLAT_FOCUS_FLAG_ACTIVE 0x01
523
524#define RETROFLAT_FOCUS_FLAG_VISIBLE 0x02
525
526typedef MERROR_RETVAL (*retroflat_proc_focus_t)( uint8_t flags, void* data );
527
532typedef MERROR_RETVAL (*retroflat_proc_quit_t)( void* data );
533 /* maug_retroflt_platform */
535
540
541#define RETROSND_ARGS_FLAG_LIST_DEVS 0x01
542
544
570
575#define RETROFLAT_BITMAP_FLAG_OPAQUE 0x01
576
580#define RETROFLAT_BITMAP_FLAG_LITERAL_PATH 0x02
581
585#define RETROFLAT_BITMAP_FLAG_RO 0x04
586
593#define RETROFLAT_BITMAP_FLAG_SCREEN_BUFFER 0x08
594
598#define RETROFLAT_BITMAP_FLAG_SCREEN_LOCK 0x20
599
603#define RETROFLAT_BITMAP_FLAG_LOCK 0x40
604
610#define RETROFLAT_INSTANCE_NULL (0)
611
616#define retroflat_instance_tile( instance ) \
617 (0 - (instance))
618
623#ifndef RETROFLAT_BITMAP_EXT
624# define RETROFLAT_BITMAP_EXT "bmp"
625#endif /* !RETROFLAT_BITMAP_EXT */
626
627#ifndef RETROFLAT_OPENGL_BPP
628# define RETROFLAT_OPENGL_BPP 32
629#endif /* !RETROFLAT_OPENGL_BPP */
630
631#ifndef RETROFLAT_TILE_W
632# define RETROFLAT_TILE_W 16
633#endif /* !RETROFLAT_TILE_W */
634
635#ifndef RETROFLAT_TILE_W_BITS
636# define RETROFLAT_TILE_W_BITS 4
637#endif /* !RETROFLAT_TILE_W_BITS */
638
639#ifndef RETROFLAT_TILE_H
640# define RETROFLAT_TILE_H 16
641#endif /* !RETROFLAT_TILE_H */
642
643#ifndef RETROFLAT_TILE_H_BITS
644# define RETROFLAT_TILE_H_BITS 4
645#endif /* !RETROFLAT_TILE_H_BITS */
646
647/* Transparency background color: black by default, to match Allegro. */
648#ifndef RETROFLAT_TXP_R
654# define RETROFLAT_TXP_R 0x00
655#endif /* !RETROFLAT_TXP_R */
656
657#ifndef RETROFLAT_TXP_G
663# define RETROFLAT_TXP_G 0x00
664#endif /* !RETROFLAT_TXP_G */
665
666#ifndef RETROFLAT_TXP_B
672# define RETROFLAT_TXP_B 0x00
673#endif /* !RETROFLAT_TXP_B */
674
675#ifndef RETROFLAT_TXP_PAL_IDX
676# define RETROFLAT_TXP_PAL_IDX 0
677#endif /* !RETROFLAT_TXP_PAL_IDX */
678 /* maug_retroflt_bitmap */
680
681#ifndef RETROFLAT_DEFAULT_SCREEN_W
682# define RETROFLAT_DEFAULT_SCREEN_W 320
683#endif /* !RETROFLAT_DEFAULT_SCREEN_W */
684
685#ifndef RETROFLAT_DEFAULT_SCREEN_H
686# define RETROFLAT_DEFAULT_SCREEN_H 200
687#endif /* !RETROFLAT_DEFAULT_SCREEN_H */
688
689#ifndef RETROFLAT_DEFAULT_SCREEN_SCALE
690# define RETROFLAT_DEFAULT_SCREEN_SCALE 1
691#endif /* !RETROFLAT_DEFAULT_SCREEN_SCALE */
692
693#define retroflat_on_resize( w, h ) \
694 g_retroflat_state->screen_w = (w); \
695 g_retroflat_state->screen_h = (h);
696
701
702#ifndef RETROFLAT_LINE_THICKNESS
707# define RETROFLAT_LINE_THICKNESS 1
708#endif /* !RETROFLAT_LINE_THICKNESS */
709
710#define RETROFLAT_PI 3.14159
711 /* maug_retroflt_drawing */
713
718
719#ifndef RETROFLAT_FPS
724# define RETROFLAT_FPS 30
725#endif /* !RETROFLAT_FPS */
726
727#define retroflat_fps_next() (1000 / RETROFLAT_FPS)
728
729#ifndef RETROFLAT_WINDOW_CLASS
734# define RETROFLAT_WINDOW_CLASS "RetroFlatWindowClass"
735#endif /* !RETROFLAT_WINDOW_CLASS */
736
737#ifndef RETROFLAT_WIN_FRAME_TIMER_ID
742# define RETROFLAT_WIN_FRAME_TIMER_ID 6001
743#endif /* !RETROFLAT_WIN_FRAME_TIMER_ID */
744
745#ifndef RETROFLAT_WIN_LOOP_TIMER_ID
750# define RETROFLAT_WIN_LOOP_TIMER_ID 6002
751#endif /* !RETROFLAT_WIN_LOOP_TIMER_ID */
752
753#ifndef RETROFLAT_MSG_MAX
758# define RETROFLAT_MSG_MAX 4096
759#endif /* !RETROFLAT_MSG_MAX */
760
761#ifndef RETROFLAT_TITLE_MAX
762# define RETROFLAT_TITLE_MAX 255
763#endif /* !RETROFLAT_TITLE_MAX */
764
765#ifndef RETROFLAT_VDP_ARGS_SZ_MAX
769# define RETROFLAT_VDP_ARGS_SZ_MAX 255
770#endif /* !RETROFLAT_VDP_ARGS_SZ_MAX */
771
772#if defined( RETROFLAT_API_SDL2 )
773# if !defined( NO_RETROFLAT_RESIZABLE )
774# define RETROFLAT_WIN_FLAGS SDL_WINDOW_RESIZABLE
775# else
776# define RETROFLAT_WIN_FLAGS 0
777# endif /* !NO_RETROFLAT_RESIZABLE */
778#endif /* RETROFLAT_API_SDL2 */
779
780#if defined( RETROFLAT_API_SDL1 )
781# define RETROFLAT_SDL_CC_FLAGS (SDL_RLEACCEL | SDL_SRCCOLORKEY)
782#elif defined( RETROFLAT_API_SDL2 )
783# define RETROFLAT_SDL_CC_FLAGS (SDL_TRUE)
784#endif /* RETROFLAT_API_SDL1 || RETROFLAT_API_SDL2 */
785
786#ifdef RETROFLAT_OS_DOS
787# define RETROFLAT_PATH_SEP '\\'
788#else
790# define RETROFLAT_PATH_SEP '/'
791#endif /* RETROFLAT_OS_DOS */
792
793#ifndef RETROFLAT_COLORS_CT_MAX
794# define RETROFLAT_COLORS_CT_MAX 256
795#endif /* !RETROFLAT_COLORS_CT_MAX */
796
797#ifndef RETROFLAT_TIMER_CT_MAX
798# define RETROFLAT_TIMER_CT_MAX 10
799#endif /* !RETROFLAT_TIMER_CT_MAX */
800 /* maug_retroflt_compiling */
802
803#define retroflat_wait_for_frame() \
804 (g_retroflat_state->retroflat_flags |= RETROFLAT_STATE_FLAG_WAIT_FOR_FPS)
805
806#define retroflat_is_waiting_for_frame() \
807 (RETROFLAT_STATE_FLAG_WAIT_FOR_FPS == \
808 (g_retroflat_state->retroflat_flags & RETROFLAT_STATE_FLAG_WAIT_FOR_FPS))
809
813typedef void (*retroflat_loop_iter)(void* data);
814
815uint32_t retroflat_get_rand( void );
816
822
826#define retroflat_buffer_bksp( buffer, buffer_cur, buffer_sz ) \
827 if( 0 < buffer_cur ) { \
828 if( buffer_cur < buffer_sz ) { \
829 memmove( \
830 &(buffer[(buffer_cur) - 1]), \
831 &(buffer[buffer_cur]), \
832 (buffer_sz) - (buffer_cur) ); \
833 } \
834 buffer_cur--; \
835 buffer_sz--; \
836 buffer[buffer_sz] = '\0'; \
837 }
838
842#define retroflat_buffer_insert( c, buffer, buffer_cur, buffer_sz, buffer_mx ) \
843 if( buffer_sz + 1 < buffer_mx ) { \
844 if( buffer_cur < buffer_sz ) { \
845 memmove( \
846 &(buffer[(buffer_cur) + 1]), \
847 &(buffer[buffer_cur]), \
848 (buffer_sz) - (buffer_cur) ); \
849 } \
850 buffer[buffer_cur] = c; \
851 buffer_cur++; \
852 buffer_sz++; \
853 buffer[buffer_sz] = '\0'; \
854 }
855
856#define RETROFLAT_INPUT_MOD_SHIFT 0x01
857
858#define RETROFLAT_INPUT_MOD_ALT 0x02
859
860#define RETROFLAT_INPUT_MOD_CTRL 0x04
861
862#define RETROFLAT_INPUT_FORCE_UPPER 0x08
863
876 uint8_t key_flags;
877};
878 /* maug_retroflt_input */
880
887
888typedef int8_t retroflat_dir4_t;
889
890typedef int8_t retroflat_dir8_t;
891
892#define RETROFLAT_DIR4_NONE (-1)
893#define RETROFLAT_DIR4_NORTH 0
894#define RETROFLAT_DIR4_EAST 1
895#define RETROFLAT_DIR4_SOUTH 2
896#define RETROFLAT_DIR4_WEST 3
897
898#define RETROFLAT_DIR8_NONE (-1)
899#define RETROFLAT_DIR8_NORTH 0
900#define RETROFLAT_DIR8_EAST 2
901#define RETROFLAT_DIR8_SOUTH 4
902#define RETROFLAT_DIR8_WEST 6
903
904#define retroflat_dir4_rotate_cw( dir ) \
905 ((dir + 1) % 4)
906
907#define retroflat_dir8_reverse( dir ) \
908 ((dir + 4) % 8)
909
910#define retroflat_dir8_bounce( dir ) \
911 ((dir + 2) % 8)
912 /* maug_retroflt_dir */
914
915#define retroflat_outside_rect( x, y, rx, ry, rw, rh ) \
916 ((unsigned int)(x - rx) >= (unsigned int)rw || \
917 (unsigned int)(y - ry) >= (unsigned int)rh)
918
925typedef int16_t retroflat_pxxy_t;
926
927#define PXXY_FMT "%d"
928
929#define PXXY_MAX INT16_MAX
930
931struct RETROFLAT_ARGS;
932
933#ifndef RETRO2D_TRACE_LVL
934# define RETRO2D_TRACE_LVL 0
935#endif /* !RETRO2D_TRACE_LVL */
936
937#ifndef RETRO2D_DRAW_TRACE_LVL
938# define RETRO2D_DRAW_TRACE_LVL 0
939#endif /* !RETRO2D_DRAW_TRACE_LVL */
940
941#ifndef RETRO2D_LOCK_TRACE_LVL
942# define RETRO2D_LOCK_TRACE_LVL 0
943#endif /* !RETRO2D_LOCK_TRACE_LVL */
944
945typedef maug_ms_t retroflat_ms_t;
946
947retroflat_ms_t retroflat_get_ms( void );
948
949#ifndef RETROFLAT_NO_SOUND
950# include "retrosnd.h"
951#endif /* !RETROFLAT_NO_SOUND */
952
953/* === Platform-specific APIs === */
954
955/* The first call to these headers should just establish definitions (macros, defines, prototypes,
956 * typedefs, etc). The later call below should then define function bodies.
957 */
958#ifndef RETROFLAT_NO_SOUND
959# include <retapis.h>
960#endif /* !RETROFLAT_NO_SOUND */
961#include <retapii.h>
962
963/* === End platform-specific APIs === */
964
969
970# if defined( RETROFLAT_NO_KEYBOARD )
971# define retroflat_case_key( key, pad ) case pad:
972# define retroflat_or_key( input, key, pad ) ((input) == (pad))
973# elif defined( RETROFLAT_NO_PAD )
974# define retroflat_case_key( key, pad ) case key:
975# define retroflat_or_key( input, key, pad ) ((input) == (key))
976# else
984# define retroflat_case_key( key, pad ) case pad: case key:
985# define retroflat_or_key( input, key, pad ) \
986 (((input) == (pad)) || ((input) == (key)))
987# endif
988 /* maug_retroflt_input */
990
991/* === OS-Specific Includes and Defines === */
992
993#if defined( RETROFLAT_OS_WIN ) && !defined( MAUG_WINDOWS_H )
994# include <windows.h>
995# define MAUG_WINDOWS_H
996#endif /* !MAUG_WINDOWS_H */
997
998#if defined( RETROFLAT_BMP_TEX ) || defined( DOCUMENTATION )
999
1001 uint8_t flags;
1002 MAUG_MHANDLE bytes_h;
1003 uint8_t* bytes;
1004 uint32_t bpp;
1005 uint32_t sz;
1006 uint8_t* px;
1007 uint32_t id;
1008 size_t w;
1009 size_t h;
1010};
1011
1012#endif /* RETROFLAT_BMP_TEX */
1013
1014/* TODO: Migrate all platform-specific parts below to retapid.h. */
1015#include <retapid.h>
1016
1017#ifndef retroflat_system_task
1023# define retroflat_system_task()
1024#endif /* !retroflat_system_task */
1025
1026typedef void (*retroflat_timer_cb_t)( retroflat_ms_t time, void* data );
1027
1028#include "retrom2d.h"
1029
1030/* === Structures === */
1031
1032/* TODO: Break the args into API-specific headers. */
1033
1036 uint8_t flags;
1041 char* title;
1064 int screen_scale;
1072 int joystick_id;
1073 struct RETROFLAT_PLATFORM_ARGS platform;
1074# ifndef RETROFLAT_NO_SOUND
1075 struct RETROFLAT_SOUND_ARGS sound;
1076# endif /* !RETROFLAT_NO_SOUND */
1077};
1078
1083# define retroflat_screen_colors() (g_retroflat_state->screen_colors)
1084
1091#define retroflat_heartbeat_set( len, max ) \
1092 g_retroflat_state->heartbeat_max = max; \
1093 g_retroflat_state->heartbeat_len = len;
1094
1099#define retroflat_heartbeat() (g_retroflat_state->heartbeat_frame)
1100
1108#define retroflat_heartbeat_update() \
1109 /* Update the heartbeat animation frame. */ \
1110 if( g_retroflat_state->heartbeat_next <= retroflat_get_ms() ) { \
1111 g_retroflat_state->heartbeat_frame++; \
1112 if( \
1113 g_retroflat_state->heartbeat_frame >= \
1114 g_retroflat_state->heartbeat_max \
1115 ) { \
1116 g_retroflat_state->heartbeat_frame = 0; \
1117 } \
1118 g_retroflat_state->heartbeat_next = \
1119 retroflat_get_ms() + g_retroflat_state->heartbeat_len; \
1120 }
1121
1122#include <retrovi2.h>
1123
1131 /* TODO: Set this up in the initialization function! */
1132 /* TODO: We probably need more of these. */
1133 size_t sz;
1134 size_t offset_pal;
1135 size_t offset_tex_pal;
1136
1137 void* loop_data;
1138 MERROR_RETVAL retval;
1141 maug_path config_path;
1142 maug_path assets_path;
1143 maug_path saves_path;
1144
1146 /* struct RETROFLAT_BITMAP buffer; */
1148
1149# if defined( RETROFLAT_VDP ) || defined( DOCUMENTATION ) || \
1150defined( RETROVDP_C )
1158 struct RETROFLAT_BITMAP* vdp_buffer_in;
1159 struct RETROFLAT_BITMAP* vdp_buffer_out;
1160# ifdef RETROFLAT_OS_WIN
1161 HMODULE vdp_exe;
1162# else
1164 void* vdp_exe;
1165# endif /* RETROFLAT_OS_WIN */
1174 uint8_t vdp_flags;
1175# endif /* RETROFLAT_VDP || DOCUMENTATION || RETROVDP_C */
1176
1177 /* These are used by VDP so should be standardized/not put in plat-spec! */
1178
1179 struct RETROFLAT_VIEWPORT viewport;
1180
1196 size_t screen_w;
1198 size_t screen_h;
1201
1202 /* WARNING: The VDP requires the state specifier to be the same size
1203 * as the one it was compiled for! Do not modify above here!
1204 */
1205
1206 /* TODO: Put these in a platform-specific struct of some kind to maintain
1207 * consistent state struct size for VDP?
1208 */
1209
1210 retroflat_ms_t heartbeat_next;
1228
1230 retroflat_proc_resize_t on_resize;
1231 void* on_resize_data;
1232
1234 retroflat_proc_focus_t on_focus;
1235 void* on_focus_data;
1238
1241 void* on_quit_data;
1242
1243#ifndef RETROFLAT_BMP_TEX
1245 RETROFLAT_COLOR_DEF palette[RETROFLAT_COLORS_CT_MAX];
1246#endif /* !RETROFLAT_BMP_TEX */
1247
1248 retroflat_loop_iter loop_iter;
1249 retroflat_loop_iter frame_iter;
1250
1251 struct RETROFLAT_PLATFORM platform;
1252
1253# if defined( RETROFLAT_BMP_TEX )
1254 /* This allows native colors to be used for things like glColor3fv while
1255 * these colors are used to manipulate textures passed through
1256 * retroflat_bitmap_*().
1257 */
1258 RETROFLAT_TEX_COLOR_DEF tex_palette[RETROFLAT_COLORS_CT_MAX];
1259# endif /* RETROFLAT_BMP_TEX */
1260
1261 struct RETROFLAT_INPUT_STATE input;
1262
1267 retroflat_timer_cb_t timers_cb[RETROFLAT_TIMER_CT_MAX];
1268 retroflat_ms_t timers_at[RETROFLAT_TIMER_CT_MAX];
1269 void* timers_data[RETROFLAT_TIMER_CT_MAX];
1270 size_t timers_ct;
1271
1272# ifndef RETROFLAT_NO_SOUND
1273 struct RETROFLAT_SOUND_STATE sound;
1274# endif /* !RETROFLAT_NO_SOUND */
1275};
1276
1277/* === Translation Module === */
1278
1279/* Declare the prototypes so that internal functions can call each other. */
1280
1281# ifdef retroflat_loop
1282MERROR_RETVAL retroflat_loop_generic(
1283 retroflat_loop_iter frame_iter, retroflat_loop_iter loop_iter, void* data );
1284# else
1291 retroflat_loop_iter frame_iter, retroflat_loop_iter loop_iter, void* data );
1292# endif /* retroflat_loop */
1293
1294#define retroflat_get_assets_path() (g_retroflat_state->assets_path)
1295
1296MERROR_RETVAL retroflat_build_filename_path(
1297 const maug_path filename_in, const char* filename_ext,
1298 char* buffer_out, size_t buffer_out_sz, uint8_t flags );
1299
1304
1312 uint8_t flags, const char* title, const char* format, ... );
1313 /* maug_retroflt_msg_api */
1315
1323MERROR_RETVAL retroflat_init( int argc, char* argv[], struct RETROFLAT_ARGS* args );
1324
1331void retroflat_shutdown( int retval );
1332
1333# if defined( RETROFLAT_VDP ) || defined( DOCUMENTATION )
1338
1342MERROR_RETVAL retroflat_vdp_call( const char* proc_name );
1343
1344uint8_t* retroflat_vdp_get_vdp_in( void );
1345
1346uint8_t* retroflat_vdp_get_vdp_out( void );
1347 /* maug_retroflt_vdp */
1349# endif /* RETROFLAT_VDP || DOCUMENTATION */
1350
1351RETROFLAT_IN_KEY retroflat_repeat_input(
1352 RETROFLAT_IN_KEY key_out, struct RETROFLAT_INPUT* input,
1353 RETROFLAT_IN_KEY* prev_input, int* prev_delay );
1354
1355void retroflat_set_title( const char* format, ... );
1356
1357# if !defined( RETROFLAT_NO_KEYBOARD )
1358char retroflat_vk_to_ascii( RETROFLAT_IN_KEY k, uint8_t flags );
1359# endif /* !RETROFLAT_NO_KEYBOARD */
1360
1365
1377 const char* filename, struct RETROFLAT_BITMAP* bmp_out, uint8_t flags );
1378
1379MERROR_RETVAL retroflat_create_bitmap(
1381 struct RETROFLAT_BITMAP* bmp_out, uint8_t flags );
1382
1388void retroflat_destroy_bitmap( struct RETROFLAT_BITMAP* bitmap );
1389
1406 struct RETROFLAT_BITMAP* target, struct RETROFLAT_BITMAP* src,
1410 int16_t instance );
1411 /* maug_retroflt_bitmap */
1413
1418
1428MERROR_RETVAL retroflat_draw_lock( struct RETROFLAT_BITMAP* bmp );
1429
1430MERROR_RETVAL retroflat_draw_release( struct RETROFLAT_BITMAP* bmp );
1431
1432void retroflat_px(
1433 struct RETROFLAT_BITMAP* target, const RETROFLAT_COLOR color,
1434 retroflat_pxxy_t x, retroflat_pxxy_t y, uint8_t flags );
1435
1436#ifdef RETROFLAT_SOFT_SHAPES
1437# ifdef RETROFLAT_OPENGL
1438/* Make sure we're not passing NULL to openGL texture drawers... they can't
1439 * handle that!
1440 */
1441# define retroflat_rect( t, c, x, y, w, h, f ) \
1442 assert( NULL != t ); \
1443 retrosoft_rect( t, c, x, y, w, h, f );
1444# define retroflat_ellipse( t, c, x, y, w, h, f ) \
1445 assert( NULL != t ); \
1446 retrosoft_ellipse( t, c, x, y, w, h, f )
1447# else
1448# define retroflat_rect( t, c, x, y, w, h, f ) \
1449 retrosoft_rect( t, c, x, y, w, h, f )
1450# define retroflat_ellipse( t, c, x, y, w, h, f ) \
1451 retrosoft_ellipse( t, c, x, y, w, h, f )
1452# endif /* RETROFLAT_3D */
1453#else
1454
1466 struct RETROFLAT_BITMAP* target, const RETROFLAT_COLOR color,
1468 retroflat_pxxy_t w, retroflat_pxxy_t h, uint8_t flags );
1469
1481 struct RETROFLAT_BITMAP* target, const RETROFLAT_COLOR color,
1483 retroflat_pxxy_t w, retroflat_pxxy_t h, uint8_t flags );
1484
1485#endif /* RETROFLAT_SOFT_SHAPES */
1486
1487#ifdef RETROFLAT_SOFT_LINES
1488# define retroflat_line( t, c, x1, y1, x2, y2, f ) \
1489 retrosoft_line( t, c, x1, y1, x2, y2, f )
1490#else
1491
1504 struct RETROFLAT_BITMAP* target, const RETROFLAT_COLOR color,
1506 retroflat_pxxy_t x2, retroflat_pxxy_t y2, uint8_t flags );
1507
1508#endif /* RETROFLAT_SOFT_LINES */
1509
1510void retroflat_cursor( struct RETROFLAT_BITMAP* target, uint8_t flags );
1511
1512#define retroflat_blank( target, color ) \
1513 retroflat_rect( \
1514 target, color, 0, 0, \
1515 retroflat_bitmap_w( NULL == target ? retroflat_screen_buffer() : target )\
1516 , \
1517 retroflat_bitmap_h( NULL == target ? retroflat_screen_buffer() : target )\
1518 , RETROFLAT_DRAW_FLAG_FILL );
1519
1520/* TODO: Documentation! */
1521void retroflat_get_palette( uint8_t idx, uint32_t* rgb );
1522
1523MERROR_RETVAL retroflat_set_palette( uint8_t idx, uint32_t rgb );
1524 /* maug_retroflt_bitmap */
1526
1532
1543 retroflat_proc_resize_t on_resize_in, void* data_in );
1544
1550 retroflat_ms_t time, retroflat_timer_cb_t cb, void* data );
1551
1561 retroflat_proc_focus_t on_focus_in, void* data_in );
1562
1572 retroflat_proc_quit_t on_quit_in, void* data_in );
1573
1582
1587#define retroflat_soft_quit( retval ) \
1588 if( \
1589 NULL == g_retroflat_state->on_quit || \
1590 MERROR_PREEMPT != g_retroflat_state->on_quit( \
1591 g_retroflat_state->on_quit_data \
1592 ) ) { \
1593 retroflat_quit( retval ); \
1594 }
1595
1601
1607
1613 /* maug_retroflt_platform_handler */
1615 /* maug_retroflt_platform */
1617
1622
1628RETROFLAT_IN_KEY retroflat_poll_input( struct RETROFLAT_INPUT* input );
1629 /* maug_retroflt_input */
1631
1632#ifdef RETROFLT_C
1633
1634MAUG_CONST int16_t SEG_MCONST gc_retroflat_offsets8_x[8] =
1635 { 0, 1, 1, 1, 0, -1, -1, -1 };
1636MAUG_CONST int16_t SEG_MCONST gc_retroflat_offsets8_y[8] =
1637 { -1, -1, 0, 1, 1, 1, 0, -1 };
1638
1639MAUG_CONST int16_t SEG_MCONST gc_retroflat_offsets4_x[4] =
1640 { 0, 1, 0, -1 };
1641MAUG_CONST int16_t SEG_MCONST gc_retroflat_offsets4_y[4] =
1642 { -1, 0, 1, 0 };
1643
1644MAUG_CONST int16_t SEG_MCONST gc_retroflat_offsets4_x_tile[4] =
1645 { 0, RETROFLAT_TILE_W, 0, -RETROFLAT_TILE_W };
1646MAUG_CONST int16_t SEG_MCONST gc_retroflat_offsets4_y_tile[4] =
1647 { -RETROFLAT_TILE_H, 0, RETROFLAT_TILE_H, 0 };
1648
1649# ifdef RETROFLAT_STATE_ON_STACK
1650struct RETROFLAT_STATE SEG_MGLOBAL g_retroflat_state_stack;
1651# else
1652MAUG_MHANDLE SEG_MGLOBAL g_retroflat_state_h = (MAUG_MHANDLE)NULL;
1653# endif /* RETROFLAT_STATE_ON_STACK */
1654struct RETROFLAT_STATE* SEG_MGLOBAL g_retroflat_state = NULL;
1655
1656# define RETROFLAT_COLOR_TABLE_CONSTS( idx, name_l, name_u, r, g, b, cgac, cgad ) \
1657 MAUG_CONST RETROFLAT_COLOR SEG_MCONST RETROFLAT_COLOR_ ## name_u = idx;
1658
1659RETROFLAT_COLOR_TABLE( RETROFLAT_COLOR_TABLE_CONSTS )
1660
1661# define RETROFLAT_COLOR_TABLE_NAMES( idx, name_l, name_u, r, g, b, cgac, cgad ) \
1662 #name_u,
1663
1664MAUG_CONST char* SEG_MCONST gc_retroflat_color_names[] = {
1665 RETROFLAT_COLOR_TABLE( RETROFLAT_COLOR_TABLE_NAMES )
1666};
1667
1668/* Call a second time, to add function bodies. */
1669#include <retrovi2.h>
1670
1671/* Callback table is down below, after the statically-defined callbacks. */
1672
1673/* === Function Definitions === */
1674
1675MERROR_RETVAL retroflat_build_filename_path(
1676 const maug_path filename_in, const char* filename_ext,
1677 char* buffer_out, size_t buffer_out_sz, uint8_t flags
1678) {
1679 MERROR_RETVAL retval = MERROR_OK;
1680
1681 assert( 1 < buffer_out_sz );
1682
1683 /* Build the path to the bitmap. */
1684 maug_mzero( buffer_out, buffer_out_sz );
1685 if(
1688 ) {
1689 /* TODO: Error checking. */
1690 maug_snprintf( buffer_out, buffer_out_sz - 1, "%s", filename_in );
1691 } else if( NULL != filename_ext ) {
1692 /* TODO: Error checking. */
1693 maug_snprintf( buffer_out, buffer_out_sz - 1, "%s%c%s.%s",
1694 g_retroflat_state->assets_path, RETROFLAT_PATH_SEP,
1695 filename_in, filename_ext );
1696 } else {
1697 maug_snprintf( buffer_out, buffer_out_sz - 1, "%s%c%s",
1698 g_retroflat_state->assets_path, RETROFLAT_PATH_SEP,
1699 filename_in );
1700 }
1701
1702 return retval;
1703}
1704
1705/* === */
1706
1707# if (defined( RETROFLAT_SOFT_SHAPES ) || defined( RETROFLAT_SOFT_LINES ) || \
1708 defined( RETROFLAT_3D ))
1709/* RETROSOFT_PRESENT is different from RETROFLAT_SOFT_SHAPES in that it only
1710 * indicates that the retrosoft library is loaded, not that it is the default
1711 * for drawing primatives!
1712 */
1713# define RETROSOFT_PRESENT
1714# endif
1715
1716# if defined( RETROFLAT_3D )
1717# if !defined( MAUG_NO_AUTO_C )
1718# define RETRO3D_C
1719# define RETRO3DP_C
1720# define RETROFP_C
1721# endif /* MAUG_NO_AUTO_C */
1722# include <retro3dp.h>
1723# include <retro3d.h>
1724# include <retro3du.h>
1725# include <retapi3.h>
1726# endif /* RETROFLAT_3D */
1727
1728# ifdef RETROSOFT_PRESENT
1729# if !defined( MAUG_NO_AUTO_C )
1730# define RETROSFT_C
1731# endif /* !MAUG_NO_AUTO_C */
1732# define RETROSOFT_PRESENT
1733# include <retrosft.h>
1734# endif /* RETROFLAT_SOFT_SHAPES */
1735
1736# ifndef RETROFLAT_NO_SOUND
1737# include <retapis.h>
1738# endif /* !RETROFLAT_NO_SOUND */
1739
1740# include <retapii.h>
1741
1742# if defined( RETROFLAT_VDP ) && defined( RETROFLAT_OS_UNIX )
1743# include <dlfcn.h>
1744# endif
1745
1746/* Still inside RETROFLT_C! */
1747
1748/* === */
1749
1750#ifndef RETROFLAT_NO_GENERIC_LOOP
1751
1752MERROR_RETVAL retroflat_loop_generic(
1753 retroflat_loop_iter frame_iter, retroflat_loop_iter loop_iter, void* data
1754) {
1755 MERROR_RETVAL retval = MERROR_OK;
1756 retroflat_ms_t next = 0,
1757 now = 0;
1758
1759 g_retroflat_state->loop_iter = (retroflat_loop_iter)loop_iter;
1760 g_retroflat_state->loop_data = (void*)data;
1761 g_retroflat_state->frame_iter = (retroflat_loop_iter)frame_iter;
1762
1763 if(
1765 (g_retroflat_state->retroflat_flags & RETROFLAT_STATE_FLAG_RUNNING)
1766 ) {
1767 /* Main loop is already running, so we're just changing the iter call
1768 * and leaving!
1769 */
1770 debug_printf( 1, "main loop already running!" );
1771 goto cleanup;
1772 }
1773
1774 g_retroflat_state->retroflat_flags |= RETROFLAT_STATE_FLAG_RUNNING;
1775 do {
1777
1778 if(
1779 /* Not waiting for the next frame? */
1781 (RETROFLAT_STATE_FLAG_WAIT_FOR_FPS & g_retroflat_state->retroflat_flags) &&
1782 /* Inter-frame loop present? */
1783 NULL != g_retroflat_state->loop_iter
1784 ) {
1785 /* Run the loop iter as many times as possible. */
1786 g_retroflat_state->loop_iter( g_retroflat_state->loop_data );
1787 }
1788 if(
1790 (RETROFLAT_STATE_FLAG_UNLOCK_FPS & g_retroflat_state->retroflat_flags) &&
1791 retroflat_get_ms() < next
1792 ) {
1793 /* Sleep/low power for a bit. */
1794 continue;
1795 }
1796
1798
1800
1801 if(
1802 NULL != g_retroflat_state->on_focus &&
1803 g_retroflat_state->last_focus_flags != retroflat_focus_platform()
1804 ) {
1805 g_retroflat_state->last_focus_flags = retroflat_focus_platform();
1806 retval = g_retroflat_state->on_focus(
1807 g_retroflat_state->last_focus_flags,
1808 g_retroflat_state->on_focus_data );
1809 maug_cleanup_if_not_ok();
1810 }
1811
1812 if( NULL != g_retroflat_state->frame_iter ) {
1813 /* Run the frame iterator once per FPS tick. */
1814 g_retroflat_state->frame_iter( g_retroflat_state->loop_data );
1815 }
1816 /* Reset wait-for-frame flag AFTER frame callback. */
1817 g_retroflat_state->retroflat_flags &= ~RETROFLAT_STATE_FLAG_WAIT_FOR_FPS;
1818 now = retroflat_get_ms();
1819 if( now + retroflat_fps_next() > now ) {
1820 next = now + retroflat_fps_next();
1821 } else {
1822 /* Rollover protection. */
1823 /* TODO: Add difference from now/next to 0 here. */
1824 next = 0;
1825 }
1826 } while(
1828 (RETROFLAT_STATE_FLAG_RUNNING & g_retroflat_state->retroflat_flags)
1829 );
1830 retval = g_retroflat_state->retval;
1831
1832cleanup:
1833
1834 /* This should be set by retroflat_quit(). */
1835 return retval;
1836}
1837
1838#endif /* !RETROFLAT_NO_GENERIC_LOOP */
1839
1840/* === */
1841
1842# if !defined( RETROFLAT_NO_KEYBOARD )
1843
1844char retroflat_vk_to_ascii( RETROFLAT_IN_KEY k, uint8_t flags ) {
1845 char c = 0;
1846 char offset_lower = 0;
1847
1848 if( RETROFLAT_INPUT_MOD_SHIFT != (RETROFLAT_INPUT_MOD_SHIFT & flags) ) {
1849 /* Shift is *not* being held down. */
1850
1851 if( RETROFLAT_KEY_A <= k && RETROFLAT_KEY_Z >= k ) {
1852 if(
1853 RETROFLAT_INPUT_FORCE_UPPER !=
1854 (RETROFLAT_INPUT_FORCE_UPPER & flags)
1855 ) {
1856 /* Key is alphabetical and we're not forcing uppercase. */
1857 offset_lower = 0x20;
1858 }
1859 } else {
1860 offset_lower = 1;
1861 }
1862 }
1863
1864 switch( k ) {
1865 case RETROFLAT_KEY_A: c = 0x41 + offset_lower; break;
1866 case RETROFLAT_KEY_B: c = 0x42 + offset_lower; break;
1867 case RETROFLAT_KEY_C: c = 0x43 + offset_lower; break;
1868 case RETROFLAT_KEY_D: c = 0x44 + offset_lower; break;
1869 case RETROFLAT_KEY_E: c = 0x45 + offset_lower; break;
1870 case RETROFLAT_KEY_F: c = 0x46 + offset_lower; break;
1871 case RETROFLAT_KEY_G: c = 0x47 + offset_lower; break;
1872 case RETROFLAT_KEY_H: c = 0x48 + offset_lower; break;
1873 case RETROFLAT_KEY_I: c = 0x49 + offset_lower; break;
1874 case RETROFLAT_KEY_J: c = 0x4a + offset_lower; break;
1875 case RETROFLAT_KEY_K: c = 0x4b + offset_lower; break;
1876 case RETROFLAT_KEY_L: c = 0x4c + offset_lower; break;
1877 case RETROFLAT_KEY_M: c = 0x4d + offset_lower; break;
1878 case RETROFLAT_KEY_N: c = 0x4e + offset_lower; break;
1879 case RETROFLAT_KEY_O: c = 0x4f + offset_lower; break;
1880 case RETROFLAT_KEY_P: c = 0x50 + offset_lower; break;
1881 case RETROFLAT_KEY_Q: c = 0x51 + offset_lower; break;
1882 case RETROFLAT_KEY_R: c = 0x52 + offset_lower; break;
1883 case RETROFLAT_KEY_S: c = 0x53 + offset_lower; break;
1884 case RETROFLAT_KEY_T: c = 0x54 + offset_lower; break;
1885 case RETROFLAT_KEY_U: c = 0x55 + offset_lower; break;
1886 case RETROFLAT_KEY_V: c = 0x56 + offset_lower; break;
1887 case RETROFLAT_KEY_W: c = 0x57 + offset_lower; break;
1888 case RETROFLAT_KEY_X: c = 0x58 + offset_lower; break;
1889 case RETROFLAT_KEY_Y: c = 0x59 + offset_lower; break;
1890 case RETROFLAT_KEY_Z: c = 0x60 + offset_lower; break;
1891 case RETROFLAT_KEY_0: c = offset_lower ? 0x30 : ')'; break;
1892 case RETROFLAT_KEY_1: c = offset_lower ? 0x31 : '!'; break;
1893 case RETROFLAT_KEY_2: c = offset_lower ? 0x32 : '@'; break;
1894 case RETROFLAT_KEY_3: c = offset_lower ? 0x33 : '#'; break;
1895 case RETROFLAT_KEY_4: c = offset_lower ? 0x34 : '$'; break;
1896 case RETROFLAT_KEY_5: c = offset_lower ? 0x35 : '%'; break;
1897 case RETROFLAT_KEY_6: c = offset_lower ? 0x36 : '^'; break;
1898 case RETROFLAT_KEY_7: c = offset_lower ? 0x37 : '&'; break;
1899 case RETROFLAT_KEY_8: c = offset_lower ? 0x38 : '*'; break;
1900 case RETROFLAT_KEY_9: c = offset_lower ? 0x39 : '('; break;
1901 case RETROFLAT_KEY_SPACE: c = ' '; break;
1902 case RETROFLAT_KEY_BKSP: c = 0x08; break;
1903 case RETROFLAT_KEY_ENTER: c = '\n'; break;
1904 case RETROFLAT_KEY_SEMICOLON: c = offset_lower ? ';' : ':'; break;
1905 case RETROFLAT_KEY_DASH: c = offset_lower ? '-' : '_'; break;
1906 case RETROFLAT_KEY_SLASH: c = offset_lower ? '/' : '?'; break;
1907 case RETROFLAT_KEY_PERIOD: c = offset_lower ? '.' : '>'; break;
1908 case RETROFLAT_KEY_COMMA: c = offset_lower ? ',' : '<'; break;
1909 case RETROFLAT_KEY_QUOTE: c = offset_lower ? '\'' : '"'; break;
1910 case RETROFLAT_KEY_EQUALS: c = offset_lower ? '=' : '+'; break;
1911 case RETROFLAT_KEY_BACKSLASH: c = offset_lower ? '\\' : '|'; break;
1912 case RETROFLAT_KEY_BRACKETL: c = offset_lower ? '[' : '{'; break;
1913 case RETROFLAT_KEY_BRACKETR: c = offset_lower ? ']' : '}'; break;
1914#ifndef RETROFLAT_API_PC_BIOS
1915 /* TODO: FIXME in DOS! */
1916 case RETROFLAT_KEY_GRAVE: c = offset_lower ? '`' : '~'; break;
1917#endif /* !RETROFLAT_API_PC_BIOS */
1918 }
1919
1920 debug_printf( RETROINPUT_TRACE_LVL, "0x%02x", c );
1921
1922 return c;
1923}
1924
1925#endif /* !RETROFLAT_NO_KEYBOARD */
1926
1927/* === */
1928
1929/* TODO: Migrate all platform-specific parts below to retapif.h. */
1930#include <retapif.h>
1931
1932/* === */
1933
1934# ifndef RETROFLAT_NO_CLI
1935
1936# if !defined( RETROFLAT_NO_SOUND ) && defined( RETROSND_ARGS )
1937
1938static MERROR_RETVAL retrosnd_cli_rsl(
1939 const char* arg, ssize_t arg_c, struct RETROFLAT_ARGS* args
1940) {
1941 if(
1942 0 <= arg_c &&
1943 0 == maug_strncmp( MAUG_CLI_SIGIL "rsl", arg, MAUG_CLI_SIGIL_SZ + 4 )
1944 ) {
1945 args->sound.flags |= RETROSND_ARGS_FLAG_LIST_DEVS;
1946 }
1947 return MERROR_OK;
1948}
1949
1950# endif /* !RETROFLAT_NO_SOUND && RETROSND_ARGS */
1951
1952# if !defined( RETROFLAT_API_PC_BIOS ) && !defined( RETROFLAT_NO_CLI_SZ )
1953
1954static MERROR_RETVAL retroflat_cli_rfs(
1955 const char* arg, ssize_t arg_c, struct RETROFLAT_ARGS* args
1956) {
1957 if( 1 < arg_c ) {
1958 args->screen_scale = atoi( arg );
1959 debug_printf( 3, "screen scale arg set to: %d", args->screen_scale );
1960 }
1961 return MERROR_OK;
1962}
1963
1964static MERROR_RETVAL retroflat_cli_rfx(
1965 const char* arg, ssize_t arg_c, struct RETROFLAT_ARGS* args
1966) {
1967 if( 0 > arg_c ) {
1968 if( 0 == args->screen_w ) {
1969 args->screen_x = 0;
1970 }
1971 } else if(
1972 0 == maug_strncmp( MAUG_CLI_SIGIL "rfx", arg, MAUG_CLI_SIGIL_SZ + 4 )
1973 ) {
1974 /* The next arg must be the new var. */
1975 } else {
1976 args->screen_x = atoi( arg );
1977 }
1978 return MERROR_OK;
1979}
1980
1981static MERROR_RETVAL retroflat_cli_rfy(
1982 const char* arg, ssize_t arg_c, struct RETROFLAT_ARGS* args
1983) {
1984 if( 0 > arg_c ) {
1985 if( 0 == args->screen_h ) {
1986 args->screen_y = 0;
1987 }
1988 } else if(
1989 0 == maug_strncmp( MAUG_CLI_SIGIL "rfy", arg, MAUG_CLI_SIGIL_SZ + 4 )
1990 ) {
1991 /* The next arg must be the new var. */
1992 } else {
1993 args->screen_y = atoi( arg );
1994 }
1995 return MERROR_OK;
1996}
1997
1998static MERROR_RETVAL retroflat_cli_rfw(
1999 const char* arg, ssize_t arg_c, struct RETROFLAT_ARGS* args
2000) {
2001 if( 0 > arg_c ) {
2002 if( 0 == args->screen_w ) {
2003 args->screen_w = RETROFLAT_DEFAULT_SCREEN_W;
2004 debug_printf( 1, "setting arg screen_w to default %d:",
2005 args->screen_w );
2006 }
2007 } else if(
2008 0 == maug_strncmp( MAUG_CLI_SIGIL "rfw", arg, MAUG_CLI_SIGIL_SZ + 4 )
2009 ) {
2010 /* The next arg must be the new var. */
2011 } else {
2012 args->screen_w = atoi( arg );
2013 debug_printf( 1, "setting arg screen_w to: %d",
2014 args->screen_w );
2015 }
2016 return MERROR_OK;
2017}
2018
2019static MERROR_RETVAL retroflat_cli_rfh(
2020 const char* arg, ssize_t arg_c, struct RETROFLAT_ARGS* args
2021) {
2022 if( 0 > arg_c ) {
2023 if( 0 == args->screen_h ) {
2024 args->screen_h = RETROFLAT_DEFAULT_SCREEN_H;
2025 debug_printf( 1, "setting arg screen_h to default: %d",
2026 args->screen_h );
2027 }
2028 } else if(
2029 0 == maug_strncmp( MAUG_CLI_SIGIL "rfh", arg, MAUG_CLI_SIGIL_SZ + 4 )
2030 ) {
2031 /* The next arg must be the new var. */
2032 } else {
2033 args->screen_h = atoi( arg );
2034 debug_printf( 1, "setting arg screen_h to: %d",
2035 args->screen_h );
2036 }
2037 return MERROR_OK;
2038}
2039
2040# endif /* !RETROFLAT_API_PC_BIOS && !RETROFLAT_NO_CLI_SZ */
2041
2042# ifdef RETROFLAT_VDP
2043static MERROR_RETVAL retroflat_cli_vdp(
2044 const char* arg, ssize_t arg_c, struct RETROFLAT_ARGS* args
2045) {
2046 if( 0 == maug_strncmp( MAUG_CLI_SIGIL "vdp", arg, MAUG_CLI_SIGIL_SZ + 4 ) ) {
2047 /* Next arg is VDP args str. */
2048 } else {
2049 maug_mzero( g_retroflat_state->vdp_args, RETROFLAT_VDP_ARGS_SZ_MAX + 1 );
2050 maug_strncpy(
2051 g_retroflat_state->vdp_args, arg, RETROFLAT_VDP_ARGS_SZ_MAX );
2052 debug_printf( 1, "VDP args: %s", g_retroflat_state->vdp_args );
2053 }
2054 return MERROR_OK;
2055}
2056# endif /* RETROFLAT_VDP */
2057
2058static MERROR_RETVAL retroflat_cli_u(
2059 const char* arg, ssize_t arg_c, struct RETROFLAT_ARGS* args
2060) {
2061 if( 0 > arg_c ) {
2062 args->flags &= ~RETROFLAT_STATE_FLAG_UNLOCK_FPS;
2063 } else if(
2064 0 == maug_strncmp( MAUG_CLI_SIGIL "rfu", arg, MAUG_CLI_SIGIL_SZ + 4 )
2065 ) {
2066 debug_printf( 1, "unlocking FPS..." );
2067 args->flags |= RETROFLAT_STATE_FLAG_UNLOCK_FPS;
2068 }
2069 return MERROR_OK;
2070}
2071
2072# ifndef RETROFLAT_NO_PAD
2073
2074static MERROR_RETVAL retroflat_cli_rfj(
2075 const char* arg, ssize_t arg_c, struct RETROFLAT_ARGS* args
2076) {
2077 if( 0 > arg_c ) {
2078 args->joystick_id = -1;
2079 debug_printf( 1, "setting arg joystick_id to default: %d",
2080 args->joystick_id );
2081 } else if(
2082 0 == maug_strncmp( MAUG_CLI_SIGIL "rfj", arg, MAUG_CLI_SIGIL_SZ + 4 )
2083 ) {
2084 /* The next arg must be the new var. */
2085 } else {
2086 args->joystick_id = atoi( arg );
2087 debug_printf( 1, "setting arg joystick_id to: %d",
2088 args->joystick_id );
2089 }
2090 return MERROR_OK;
2091}
2092
2093# endif /* !RETROFLAT_NO_PAD */
2094
2095#endif /* !RETROFLAT_NO_CLI */
2096
2097/* === */
2098
2099/* Still inside RETROFLT_C! */
2100
2102 int argc, char* argv[], struct RETROFLAT_ARGS* args
2103) {
2104
2105 /* = Declare Init Vars = */
2106
2107 MERROR_RETVAL retval = 0;
2108
2109 /* = Begin Init Procedure = */
2110
2111# ifdef RETROFLAT_COMMIT_HASH
2112 debug_printf( 1, "retroflat commit: " RETROFLAT_COMMIT_HASH );
2113# endif /* RETROFLAT_COMMIT_HASH */
2114
2115 debug_printf( 1, "retroflat: initializing..." );
2116
2117 /* System sanity checks. */
2118 assert( 2 <= sizeof( MERROR_RETVAL ) );
2119 assert( 4 == sizeof( uint32_t ) );
2120 assert( 4 == sizeof( int32_t ) );
2121 assert( 2 == sizeof( uint16_t ) );
2122 assert( 2 == sizeof( int16_t ) );
2123 assert( 1 == sizeof( uint8_t ) );
2124 assert( 1 == sizeof( int8_t ) );
2125 assert( NULL != args );
2126 assert( 1 << RETROFLAT_TILE_W_BITS == RETROFLAT_TILE_W );
2127 assert( 1 << RETROFLAT_TILE_H_BITS == RETROFLAT_TILE_H );
2128
2129 debug_printf( 1, "initializing drawing routines..." );
2130
2131 /* Initialize 2D callbacks depending on if we're in 2D or 3D mode.
2132 * Please see retrom2d.h for more information.
2133 */
2134# if defined( RETROFLAT_BMP_TEX )
2135 retroflat_2d_px = (retroflat_px_cb)retro3d_texture_px;
2136 retroflat_2d_line = (retroflat_line_cb)retrosoft_line;
2137 retroflat_2d_rect = (retroflat_rect_cb)retrosoft_rect;
2138 retroflat_2d_ellipse = (retroflat_ellipse_cb)retrosoft_ellipse;
2139 retroflat_2d_blit_bitmap = (retroflat_blit_bitmap_cb)retro3d_texture_blit;
2140 retroflat_2d_load_bitmap =
2141 (retroflat_load_bitmap_cb)retro3d_texture_load_bitmap;
2142 retroflat_2d_create_bitmap =
2143 (retroflat_create_bitmap_cb)retro3d_texture_create;
2144# else
2145 retroflat_2d_px = (retroflat_px_cb)retroflat_px;
2146# ifdef RETROFLAT_SOFT_SHAPES
2147 /* TODO: Work retrosoft routines to use retroflat_blit_t */
2148 retroflat_2d_line = (retroflat_line_cb)retrosoft_line;
2149 retroflat_2d_rect = (retroflat_rect_cb)retrosoft_rect;
2150 retroflat_2d_ellipse = (retroflat_ellipse_cb)retrosoft_ellipse;
2151# else
2152 retroflat_2d_line = (retroflat_line_cb)retroflat_line;
2153 retroflat_2d_rect = (retroflat_rect_cb)retroflat_rect;
2154 retroflat_2d_ellipse = (retroflat_ellipse_cb)retroflat_ellipse;
2155# endif /* RETROFLAT_SOFT_SHAPES */
2156 retroflat_2d_blit_bitmap = (retroflat_blit_bitmap_cb)retroflat_blit_bitmap;
2157 retroflat_2d_load_bitmap = (retroflat_load_bitmap_cb)retroflat_load_bitmap;
2158 retroflat_2d_create_bitmap =
2159 (retroflat_create_bitmap_cb)retroflat_create_bitmap;
2160# endif /* RETROFLAT_BMP_TEX */
2161
2162 debug_printf( 1, "retroflat: MFIX_PRECISION: %f", MFIX_PRECISION );
2163
2164 debug_printf( 1, "retroflat: allocating state (" SIZE_T_FMT " bytes)...",
2165 sizeof( struct RETROFLAT_STATE ) );
2166
2167 debug_printf( 1, "retroflat: size_t is (" SIZE_T_FMT " bytes)...",
2168 sizeof( size_t ) );
2169
2170 debug_printf( 1, "retroflat: ssize_t is (" SIZE_T_FMT " bytes)...",
2171 sizeof( ssize_t ) );
2172
2173 debug_printf( 1, "retroflat: off_t is (" SIZE_T_FMT " bytes)...",
2174 sizeof( off_t ) );
2175
2176 debug_printf( 1, "initializing global state..." );
2177
2178# ifdef RETROFLAT_STATE_ON_STACK
2179 g_retroflat_state = &g_retroflat_state_stack;
2180# else
2181 maug_malloc_test( g_retroflat_state_h, 1, sizeof( struct RETROFLAT_STATE ) );
2182
2183 maug_mlock( g_retroflat_state_h, g_retroflat_state );
2184 if( NULL == g_retroflat_state ) {
2186 "Error", "Could not lock global state!" );
2187 retval = MERROR_ALLOC;
2188 goto cleanup;
2189 }
2190# endif /* RETROFLAT_STATE_ON_STACK */
2191
2192 maug_mzero( g_retroflat_state, sizeof( struct RETROFLAT_STATE ) );
2193
2194 retroflat_heartbeat_set( 1000, 2 );
2195
2196 debug_printf( 1, "initializing platform filesystem..." );
2197 retval = mfile_plt_init();
2198 maug_cleanup_if_not_ok();
2199
2200# ifndef RETROFLAT_NO_CLI
2201
2202 debug_printf( 1, "retroflat: parsing args..." );
2203
2204 /* All platforms: add command-line args based on compile definitons. */
2205
2206 retval = maug_add_arg(
2207 MAUG_CLI_SIGIL "h", MAUG_CLI_SIGIL_SZ + 2, "Display command-line help",
2208 26, maug_cli_h, NULL );
2209
2210# if !defined( RETROFLAT_NO_SOUND ) && defined( RETROSND_ARGS )
2211 retval = maug_add_arg( MAUG_CLI_SIGIL "rsd", MAUG_CLI_SIGIL_SZ + 4,
2212 "Select MIDI device", 0, (maug_cli_cb)retrosnd_cli_rsd, args );
2213 maug_cleanup_if_not_ok();
2214 retval = maug_add_arg( MAUG_CLI_SIGIL "rsl", MAUG_CLI_SIGIL_SZ + 4,
2215 "List MIDI devices", 0, (maug_cli_cb)retrosnd_cli_rsl, args );
2216 maug_cleanup_if_not_ok();
2217# endif /* !RETROFLAT_NO_SOUND && RETROSND_ARGS */
2218
2219# ifdef RETROFLAT_SCREENSAVER
2220 retval = maug_add_arg( MAUG_CLI_SIGIL "p", MAUG_CLI_SIGIL_SZ + 2,
2221 "Preview screensaver", 0, (maug_cli_cb)retroflat_cli_p, args );
2222 maug_cleanup_if_not_ok();
2223 retval = maug_add_arg( MAUG_CLI_SIGIL "s", MAUG_CLI_SIGIL_SZ + 2,
2224 "Launch screensaver", 0, (maug_cli_cb)retroflat_cli_s, args );
2225 maug_cleanup_if_not_ok();
2226# endif /* RETROFLAT_SCREENSAVER */
2227
2228# ifdef RETROFLAT_API_PC_BIOS
2229 retval = maug_add_arg( MAUG_CLI_SIGIL "rfm", MAUG_CLI_SIGIL_SZ + 4,
2230 "Set the screen mode.", 0,
2231 (maug_cli_cb)retroflat_cli_rfm, args );
2232 maug_cleanup_if_not_ok();
2233# elif !defined( RETROFLAT_NO_CLI_SZ )
2234 retval = maug_add_arg( MAUG_CLI_SIGIL "rfs", MAUG_CLI_SIGIL_SZ + 4,
2235 "Set screen scale factor.", 0,
2236 (maug_cli_cb)retroflat_cli_rfs, args );
2237 maug_cleanup_if_not_ok();
2238 retval = maug_add_arg( MAUG_CLI_SIGIL "rfx", MAUG_CLI_SIGIL_SZ + 4,
2239 "Set the screen X position.", 0,
2240 (maug_cli_cb)retroflat_cli_rfx, args );
2241 maug_cleanup_if_not_ok();
2242 retval = maug_add_arg( MAUG_CLI_SIGIL "rfy", MAUG_CLI_SIGIL_SZ + 4,
2243 "Set the screen Y position.", 0,
2244 (maug_cli_cb)retroflat_cli_rfy, args );
2245 maug_cleanup_if_not_ok();
2246 retval = maug_add_arg( MAUG_CLI_SIGIL "rfw", MAUG_CLI_SIGIL_SZ + 4,
2247 "Set the screen width.", 0,
2248 (maug_cli_cb)retroflat_cli_rfw, args );
2249 maug_cleanup_if_not_ok();
2250 retval = maug_add_arg( MAUG_CLI_SIGIL "rfh", MAUG_CLI_SIGIL_SZ + 4,
2251 "Set the screen height.", 0,
2252 (maug_cli_cb)retroflat_cli_rfh, args );
2253 maug_cleanup_if_not_ok();
2254# endif /* !RETROFLAT_NO_CLI_SZ */
2255
2256# ifdef RETROFLAT_VDP
2257 retval = maug_add_arg( MAUG_CLI_SIGIL "vdp", MAUG_CLI_SIGIL_SZ + 4,
2258 "Pass a string of args to the VDP.", 0,
2259 (maug_cli_cb)retroflat_cli_vdp, args );
2260 maug_cleanup_if_not_ok();
2261# endif /* RETROFLAT_VDP */
2262
2263 retval = maug_add_arg( MAUG_CLI_SIGIL "rfu", MAUG_CLI_SIGIL_SZ + 4,
2264 "Unlock FPS.", 0,
2265 (maug_cli_cb)retroflat_cli_u, args );
2266 maug_cleanup_if_not_ok();
2267
2268# ifndef RETROFLAT_NO_PAD
2269 retval = maug_add_arg( MAUG_CLI_SIGIL "rfj", MAUG_CLI_SIGIL_SZ + 4,
2270 "Specify joystick ID to use.", 0,
2271 (maug_cli_cb)retroflat_cli_rfj, args );
2272 maug_cleanup_if_not_ok();
2273# endif /* !RETROFLAT_NO_PAD */
2274
2275 /* Parse command line args. */
2276 retval = maug_parse_args( argc, argv );
2277 maug_cleanup_if_not_ok();
2278
2279# endif /* !RETROFLAT_NO_CLI */
2280
2281 if( 0 == args->screen_w ) {
2282 args->screen_w = RETROFLAT_DEFAULT_SCREEN_W;
2283 debug_printf( 1, "setting arg screen_w to default: %d",
2284 args->screen_w );
2285 }
2286 if( 0 == args->screen_h ) {
2287 args->screen_h = RETROFLAT_DEFAULT_SCREEN_H;
2288 debug_printf( 1, "setting arg screen_h to default: %d",
2289 args->screen_h );
2290 }
2291 if( 0 == args->screen_scale ) {
2292 args->screen_scale = RETROFLAT_DEFAULT_SCREEN_SCALE;
2293 debug_printf( 1, "setting arg screen_scale to default: %d",
2294 args->screen_scale );
2295 }
2296 if( 0 == args->screen_colors ) {
2297 args->screen_colors = 16;
2298 debug_printf( 1, "setting arg screen_colors to default: %d",
2299 args->screen_colors );
2300 }
2301 args->joystick_id = -1;
2302
2303 if(
2305 ) {
2306 g_retroflat_state->retroflat_flags |= RETROFLAT_STATE_FLAG_UNLOCK_FPS;
2307 }
2308
2309 debug_printf( 1, "retroflat: setting config..." );
2310
2311 /* Set the assets path. */
2312 if( NULL != args->assets_path ) {
2313 maug_mzero( g_retroflat_state->assets_path, MAUG_PATH_SZ_MAX );
2314 maug_strncpy( g_retroflat_state->assets_path,
2315 args->assets_path, MAUG_PATH_SZ_MAX - 1 );
2316 }
2317
2318# if defined( RETROFLAT_SCREENSAVER )
2319 if(
2321 (RETROFLAT_STATE_FLAG_SCREENSAVER & args->flags)
2322 ) {
2323 g_retroflat_state->retroflat_flags |= RETROFLAT_STATE_FLAG_SCREENSAVER;
2324 }
2325# endif /* RETROFLAT_SCREENSAVER */
2326
2327# if !defined( RETROFLAT_NO_CLI_SZ )
2328 /* Setup intended screen size. */
2329 g_retroflat_state->screen_scale = args->screen_scale;
2330 g_retroflat_state->screen_v_w = args->screen_w;
2331 g_retroflat_state->screen_v_h = args->screen_h;
2332 g_retroflat_state->screen_w = args->screen_w *
2333 g_retroflat_state->screen_scale;
2334 g_retroflat_state->screen_h = args->screen_h *
2335 g_retroflat_state->screen_scale;
2336 g_retroflat_state->screen_colors = args->screen_colors;
2337
2338 debug_printf( 3, "attempting to initialize platform with: "
2339 SIZE_T_FMT "x" SIZE_T_FMT " pixels (scaled to " SIZE_T_FMT "x" SIZE_T_FMT
2340 ") and " SIZE_T_FMT " colors",
2341 g_retroflat_state->screen_v_w, g_retroflat_state->screen_v_h,
2342 g_retroflat_state->screen_w, g_retroflat_state->screen_h,
2343 g_retroflat_state->screen_colors );
2344# endif /* !RETROFLAT_NO_CLI_SZ */
2345
2346 /* == Platform-Specific Init == */
2347
2348 retval = retroflat_init_platform( argc, argv, args );
2349 maug_cleanup_if_not_ok();
2350
2351# if defined( RETROFLAT_NO_CLI_SZ )
2352 g_retroflat_state->screen_scale = args->screen_scale;
2353 g_retroflat_state->screen_v_w = args->screen_w;
2354 g_retroflat_state->screen_v_h = args->screen_h;
2355 g_retroflat_state->screen_w = args->screen_w *
2356 g_retroflat_state->screen_scale;
2357 g_retroflat_state->screen_h = args->screen_h *
2358 g_retroflat_state->screen_scale;
2359 g_retroflat_state->screen_colors = args->screen_colors;
2360
2361 debug_printf( 3, "initialized platform with: "
2362 SIZE_T_FMT "x" SIZE_T_FMT " pixels (scaled to " SIZE_T_FMT "x" SIZE_T_FMT
2363 ") and " SIZE_T_FMT " colors",
2364 g_retroflat_state->screen_v_w, g_retroflat_state->screen_v_h,
2365 g_retroflat_state->screen_w, g_retroflat_state->screen_h,
2366 g_retroflat_state->screen_colors );
2367# endif /* RETROFLAT_NO_CLI_SZ */
2368
2369 retval = retroflat_init_input( args );
2370 maug_cleanup_if_not_ok();
2371
2372 debug_printf( 3, "screen initialized with: " SIZE_T_FMT "x" SIZE_T_FMT
2373 " pixels (scaled to " SIZE_T_FMT "x" SIZE_T_FMT
2374 ") with " SIZE_T_FMT " colors",
2375 g_retroflat_state->screen_v_w, g_retroflat_state->screen_v_h,
2376 g_retroflat_state->screen_w, g_retroflat_state->screen_h,
2378
2379 /* Setup the refresh grid, if requested, only after screen space has been
2380 * determined by the platform!
2381 */
2382 maug_cleanup_if_eq(
2383 (size_t)0, retroflat_screen_w(), SIZE_T_FMT, MERROR_GUI );
2384 maug_cleanup_if_eq(
2385 (size_t)0, retroflat_screen_h(), SIZE_T_FMT, MERROR_GUI );
2386 maug_cleanup_if_eq(
2387 (size_t)0, retroflat_screen_colors(), SIZE_T_FMT, MERROR_GUI );
2388
2389 /* Init the viewport with no world size, just to get the refresh grid
2390 * allocated.
2391 */
2392 retval = retroview_init( 0, 0 );
2393 maug_cleanup_if_not_ok();
2394
2395# ifdef RETROFLAT_VDP
2396# if defined( RETROFLAT_OS_UNIX )
2397 g_retroflat_state->vdp_exe = dlopen(
2398 "./" RETROFLAT_VDP_LIB_NAME ".so", RTLD_LAZY );
2399# elif defined( RETROFLAT_OS_WIN )
2400 g_retroflat_state->vdp_exe = LoadLibrary(
2401 "./" RETROFLAT_VDP_LIB_NAME ".dll" );
2402# else
2403# error "dlopen undefined!"
2404# endif /* RETROFLAT_OS_UNIX */
2405
2406 if( !(g_retroflat_state->vdp_exe) ) {
2407 error_printf( "not loading VDP" );
2408 /* Skip creating the buffer or trying to run the init proc. */
2409 goto skip_vdp;
2410 }
2411
2412 debug_printf( 1, "initializing VDP..." );
2413 retval = retroflat_vdp_call( "retroflat_vdp_init" );
2414 maug_cleanup_if_not_ok();
2415
2416skip_vdp:
2417
2418# endif /* RETROFLAT_VDP */
2419
2420# ifdef RETROFLAT_3D
2421 retro3d_platform_init();
2422# endif /* RETROFLAT_3D */
2423
2424# if !defined( RETROFLAT_NO_BLANK_INIT ) && !defined( RETROFLAT_3D )
2425 retroflat_draw_lock( NULL );
2427 NULL, RETROFLAT_COLOR_BLACK, 0, 0,
2428 retroflat_screen_w(), retroflat_screen_h(),
2430 retroflat_draw_release( NULL );
2431# endif /* !RETROFLAT_NO_BLANK_INIT */
2432
2433cleanup:
2434
2435 return retval;
2436}
2437
2438/* === */
2439
2440void retroflat_shutdown( int retval ) {
2441
2442 debug_printf( 1, "retroflat shutdown called..." );
2443
2444 retroview_shutdown();
2445
2446# if defined( RETROFLAT_VDP )
2447 if( NULL != g_retroflat_state->vdp_exe ) {
2448 retroflat_vdp_call( "retroflat_vdp_shutdown" );
2449# ifdef RETROFLAT_OS_UNIX
2450 dlclose( g_retroflat_state->vdp_exe );
2451# elif defined( RETROFLAT_OS_WIN )
2452 FreeLibrary( g_retroflat_state->vdp_exe );
2453# else
2454# error "dlclose undefined!"
2455# endif /* RETROFLAT_OS_UNIX || RETROFLAT_OS_WIN */
2456 }
2457# endif /* RETROFLAT_VDP */
2458
2459 /* === Platform-Specific Shutdown === */
2460
2461#ifdef RETROFLAT_3D
2462 retro3d_platform_shutdown();
2463#endif /* RETROFLAT_3D */
2464
2465 retroflat_shutdown_platform( retval );
2466
2467#ifndef RETROFLAT_STATE_ON_STACK
2468 maug_munlock( g_retroflat_state_h, g_retroflat_state );
2469 maug_mfree( g_retroflat_state_h );
2470#endif /* !RETROFLAT_STATE_ON_STACK */
2471
2472}
2473
2474/* === */
2475
2476RETROFLAT_IN_KEY retroflat_repeat_input(
2477 RETROFLAT_IN_KEY key_out, struct RETROFLAT_INPUT* input,
2478 RETROFLAT_IN_KEY* prev_input, int* prev_delay
2479) {
2480
2481 /* Add a slight debounce for gamepad button repeat. */
2482 if( 0 < (*prev_delay) ) {
2483 debug_printf(
2484 RETROINPUT_TRACE_LVL,
2485 "repeat delay: %d", (*prev_delay) );
2486 (*prev_delay)--;
2487 }
2488
2489 /* If nothing else happened and repeat is enabled and a joypad button is
2490 * down, then emulate repeat for it.
2491 */
2492 if(
2493 0 == key_out &&
2495 (RETROFLAT_STATE_FLAG_KEY_REPEAT & g_retroflat_state->retroflat_flags) &&
2496 /* There is an input to repeat. */
2497 0 != *prev_input &&
2498 /* Delay countdown reached. */
2499 0 == *prev_delay
2500 ) {
2501 key_out = *prev_input;
2502 *prev_delay = 1;
2503 debug_printf( RETROINPUT_TRACE_LVL, "repeat: %d", key_out );
2504 }
2505
2506 return key_out;
2507}
2508
2509/* === */
2510
2511ssize_t retroflat_timer_add(
2512 retroflat_ms_t at_time, retroflat_timer_cb_t cb, void* data
2513) {
2514 if( retroflat_get_ms() > at_time ) {
2515 error_printf( "timer time is in the past!" );
2516 return merror_retval_to_sz( MERROR_EXEC );
2517 }
2518
2519 if( g_retroflat_state->timers_ct + 1 < RETROFLAT_TIMER_CT_MAX ) {
2520 g_retroflat_state->timers_cb[g_retroflat_state->timers_ct] = cb;
2521 g_retroflat_state->timers_at[g_retroflat_state->timers_ct] = at_time;
2522 g_retroflat_state->timers_ct++;
2523 return g_retroflat_state->timers_ct - 1;
2524 }
2525
2526 error_printf( "too many timers!" );
2527 return merror_retval_to_sz( MERROR_OVERFLOW );
2528}
2529
2530/* === */
2531
2532void retroflat_timer_handle( void ) {
2533 size_t i = 0;
2534 retroflat_ms_t time_now = 0;
2535
2536 time_now = retroflat_get_ms();
2537
2538 for( i = 0 ; g_retroflat_state->timers_ct > i ; i++ ) {
2539 if( g_retroflat_state->timers_at[i] <= time_now ) {
2540 g_retroflat_state->timers_cb[i](
2541 time_now, g_retroflat_state->timers_data[i] );
2542 memmove(
2543 &(g_retroflat_state->timers_cb[i]),
2544 &(g_retroflat_state->timers_cb[i + 1]),
2545 sizeof( retroflat_timer_cb_t ) *
2546 ((g_retroflat_state->timers_ct - i) - 1)
2547 );
2548 memmove(
2549 &(g_retroflat_state->timers_at[i]),
2550 &(g_retroflat_state->timers_at[i + 1]),
2551 sizeof( retroflat_ms_t ) * ((g_retroflat_state->timers_ct - i) - 1)
2552 );
2553 memmove(
2554 &(g_retroflat_state->timers_data[i]),
2555 &(g_retroflat_state->timers_data[i + 1]),
2556 sizeof( void* ) * ((g_retroflat_state->timers_ct - i) - 1)
2557 );
2558 g_retroflat_state->timers_ct--;
2559 i--;
2560 }
2561 }
2562}
2563
2564/* === */
2565
2566# ifdef RETROFLAT_VDP
2567
2568MERROR_RETVAL retroflat_vdp_call( const char* proc_name ) {
2569 MERROR_RETVAL retval = MERROR_OK;
2571# ifdef RETROFLAT_OS_WIN
2572 char proc_name_ex[256];
2573# endif /* RETROFLAT_OS_WIN */
2574
2575 if( NULL == g_retroflat_state->vdp_exe ) {
2576 goto cleanup;
2577 }
2578
2579# ifdef RETROFLAT_OS_UNIX
2580 vdp_proc = dlsym( g_retroflat_state->vdp_exe, proc_name );
2581# elif defined( RETROFLAT_OS_WIN )
2582 /* Append a _ to the proc_name because Watcom? Windows? */
2583 maug_snprintf( proc_name_ex, 255, "%s_", proc_name );
2584 vdp_proc = (retroflat_vdp_proc_t)GetProcAddress(
2585 g_retroflat_state->vdp_exe, proc_name_ex );
2586# else
2587# error "dlsym undefined!"
2588# endif
2589 if( (retroflat_vdp_proc_t)NULL == vdp_proc ) {
2590 goto cleanup;
2591 }
2592
2593# ifdef RETROFLAT_OS_WIN
2594 retroflat_draw_lock( g_retroflat_state->vdp_buffer_in );
2595# endif /* RETROFLAT_OS_WIN */
2596
2597 if(
2598 /* Don't pxlock before init can set the flag! */
2599 0 == strcmp( "retroflat_vdp_flip", proc_name ) &&
2601 (RETROFLAT_VDP_FLAG_PXLOCK & g_retroflat_state->vdp_flags)
2602 ) {
2603 retroflat_vdp_lock( g_retroflat_state->vdp_buffer_in );
2604 retroflat_vdp_lock( g_retroflat_state->vdp_buffer_out );
2605 }
2606
2607 retval = vdp_proc( g_retroflat_state );
2608
2609 if(
2610 0 == strcmp( "retroflat_vdp_flip", proc_name ) &&
2612 (RETROFLAT_VDP_FLAG_PXLOCK & g_retroflat_state->vdp_flags)
2613 ) {
2614 retroflat_vdp_release( g_retroflat_state->vdp_buffer_out );
2615 retroflat_vdp_release( g_retroflat_state->vdp_buffer_in );
2616 }
2617
2618# ifdef RETROFLAT_OS_WIN
2619 retroflat_draw_release( g_retroflat_state->vdp_buffer_in );
2620# endif /* RETROFLAT_OS_WIN */
2621
2622cleanup:
2623 return retval;
2624}
2625
2626# endif /* RETROFLAT_VDP */
2627
2628/* === */
2629
2630#if 0
2631
2632void retroflat_cursor( struct RETROFLAT_BITMAP* target, uint8_t flags ) {
2633#if 0
2634 char mouse_str[11] = "";
2635
2636 maug_snprintf(
2637 mouse_str, 10, "%02d, %02d", g_retroflat_state->last_mouse_x, g_retroflat_state->last_mouse_y );
2638
2639 retroflat_string(
2640 target, RETROFLAT_COLOR_BLACK,
2641 mouse_str, 10, NULL, 0, 0, 0 );
2643 target, RETROFLAT_COLOR_BLACK,
2644 g_retroflat_state->last_mouse_x - 5, g_retroflat_state->last_mouse_y - 5, 10, 10, 0 );
2645#endif
2646}
2647
2648#endif
2649
2650/* === */
2651
2652void maug_critical_error( const char* msg ) {
2654}
2655
2656/* === */
2657
2659 retroflat_proc_resize_t on_resize_in, void* data_in
2660) {
2661 g_retroflat_state->on_resize = on_resize_in;
2662 g_retroflat_state->on_resize_data = data_in;
2663}
2664
2665/* === */
2666
2668 retroflat_proc_focus_t on_focus_in, void* data_in
2669) {
2670 g_retroflat_state->on_focus = on_focus_in;
2671 g_retroflat_state->on_focus_data = data_in;
2672}
2673
2674/* === */
2675
2677 retroflat_proc_quit_t on_quit_in, void* data_in
2678) {
2679 g_retroflat_state->on_quit = on_quit_in;
2680 g_retroflat_state->on_quit_data = data_in;
2681}
2682
2683/* === */
2684
2685#ifdef RETROFLAT_LOAD_BITMAP_GENERIC
2686
2687/* This is a generic function that uses the callback
2688 * retroflat_load_bitmap_px_cb(), which should be defined by the
2689 * platform-specific API to draw the loaded bitmap onto a native canvas.
2690 */
2691
2693 const char* filename, struct RETROFLAT_BITMAP* bmp_out, uint8_t flags
2694) {
2695 maug_path filename_path;
2696 MERROR_RETVAL retval = MERROR_OK;
2697 mfile_t bmp_file;
2698 struct MFMT_STRUCT_BMPFILE header_bmp;
2699 uint8_t bmp_flags = 0;
2700
2701 assert( NULL != bmp_out );
2702 maug_mzero( bmp_out, sizeof( struct RETROFLAT_BITMAP ) );
2703 retval = retroflat_build_filename_path(
2704 filename, RETROFLAT_BITMAP_EXT, filename_path,
2705 MAUG_PATH_SZ_MAX, flags );
2706 maug_cleanup_if_not_ok();
2707 debug_printf( 1, "retroflat: loading bitmap: %s", filename_path );
2708
2709 bmp_out->flags = flags;
2710
2711 /* Open the bitmap file. */
2712 retval = mfile_open_read( filename_path, &bmp_file );
2713 maug_cleanup_if_not_ok();
2714
2715 /* mfmt file detection system. */
2716 maug_mzero( &header_bmp, sizeof( struct MFMT_STRUCT_BMPFILE ) );
2717 header_bmp.magic[0] = 'B';
2718 header_bmp.magic[1] = 'M';
2719 header_bmp.info.sz = 40;
2720
2721 retval = mfmt_read_bmp_header(
2722 (struct MFMT_STRUCT*)&header_bmp,
2723 &bmp_file, 0, mfile_get_sz( &bmp_file ), &bmp_flags );
2724 maug_cleanup_if_not_ok();
2725
2726 retval = retroflat_create_bitmap(
2727 header_bmp.info.width, header_bmp.info.height, bmp_out, flags );
2728 maug_cleanup_if_not_ok();
2729
2730 retroflat_draw_lock( bmp_out );
2731
2732 retval = mfmt_read_bmp_px_cb(
2733 (struct MFMT_STRUCT*)&header_bmp,
2734 &bmp_file,
2735 header_bmp.px_offset,
2736 mfile_get_sz( &bmp_file ) - header_bmp.px_offset,
2737 bmp_flags,
2738 retroflat_load_bitmap_px_cb,
2739 bmp_out );
2740 maug_cleanup_if_not_ok();
2741
2742 retroflat_draw_release( bmp_out );
2743
2744cleanup:
2745
2746 mfile_close( &bmp_file );
2747
2748 return retval;
2749}
2750
2751#endif /* RETROFLAT_LOAD_BITMAP_GENERIC */
2752
2753#elif !defined( RETROVDP_C ) /* End of RETROFLT_C */
2754
2759
2760extern MAUG_CONST int16_t SEG_MCONST gc_retroflat_offsets8_x[8];
2761extern MAUG_CONST int16_t SEG_MCONST gc_retroflat_offsets8_y[8];
2762extern MAUG_CONST int16_t SEG_MCONST gc_retroflat_offsets4_x[4];
2763extern MAUG_CONST int16_t SEG_MCONST gc_retroflat_offsets4_y[4];
2764extern MAUG_CONST int16_t SEG_MCONST gc_retroflat_offsets4_x_tile[4];
2765extern MAUG_CONST int16_t SEG_MCONST gc_retroflat_offsets4_y_tile[4];
2766
2768
2769#define RETROFLAT_COLOR_TABLE_CONSTS( idx, name_l, name_u, r, g, b, cgac, cgad ) \
2770 extern MAUG_CONST RETROFLAT_COLOR SEG_MCONST RETROFLAT_COLOR_ ## name_u;
2771
2772RETROFLAT_COLOR_TABLE( RETROFLAT_COLOR_TABLE_CONSTS )
2773
2774extern MAUG_CONST char* SEG_MCONST gc_retroflat_color_names[];
2775
2776extern struct RETROFLAT_STATE* SEG_MGLOBAL g_retroflat_state;
2777
2778# if (defined( RETROFLAT_SOFT_SHAPES ) || defined( RETROFLAT_SOFT_LINES ) || \
2779 defined( RETROFLAT_3D ))
2780# define RETROSOFT_PRESENT
2781# endif
2782
2783# ifdef RETROFLAT_3D
2784# include <retro3dp.h>
2785# include <retro3d.h>
2786# include <retro3du.h>
2787# endif /* RETROFLAT_3D */
2788
2789# ifdef RETROSOFT_PRESENT
2790# include <retrosft.h>
2791# endif /* RETROFLAT_SOFT_SHAPES */
2792
2793/* Second retapis.h include for function bodies not needed. */
2794
2795/* Second retapii.h include for function bodies not needed. */
2796
2797#endif /* RETROFLT_C */
2798
2799#ifdef RETROFLAT_XPM
2800#include <retroxpm.h>
2801#endif /* RETROFLAT_XPM */
2802
2803#ifdef RETROVDP_C
2804
2805/* Declarations for VDP sources. */
2806
2807#endif /* RETROVDP_C */
2808 /* maug_retroflt */
2810
2811#endif /* RETROFLT_H */
2812
MERROR_RETVAL maug_add_arg(const char *arg, int arg_sz, const char *help, int help_sz, maug_cli_cb arg_cb, void *data)
Add a command-line argument to the built-in parser.
#define MAUG_CLI_SIGIL
Default flag to prepend to CLI arguments. Is "/" on Windows/DOS and "-" on other platforms....
Definition marge.h:39
uint16_t MERROR_RETVAL
Return type indicating function returns a value from this list.
Definition merror.h:28
void maug_critical_error(const char *msg)
Display an error dialog. This is a minimal function that can be called early on (e....
MERROR_RETVAL mfmt_read_bmp_px_cb(struct MFMT_STRUCT *header, mfile_t *p_file_in, uint32_t px_offset, off_t file_sz, uint8_t flags, mfmt_read_1px_cb px_cb, void *px_cb_data)
Read mfmt_bitmap pixels and process them using a callback.
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:38
void mfile_close(mfile_t *p_file)
Close a file opened with mfile_open_read().
char maug_path[MAUG_PATH_SZ_MAX]
Path/name used to load an asset from disk or access other files.
Definition mfile.h:138
#define RETROFLAT_BITMAP_EXT
The filename suffix to be appended with a "." to filenames passed to retroflat_load_bitmap()....
Definition retroflt.h:624
#define RETROFLAT_BITMAP_FLAG_LITERAL_PATH
Flag for retroflat_load_bitmap() to not use assets path.
Definition retroflt.h:580
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...
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:328
#define RETROFLAT_COLOR_TABLE(f)
This macro defines all colors supported by RetroFlat for primative operations, particularly using ret...
Definition retroflt.h:309
#define RETROFLAT_VDP_ARGS_SZ_MAX
Definition retroflt.h:769
#define RETROFLAT_PATH_SEP
The valid path separator on the target platform.
Definition retroflt.h:790
void retroflat_ellipse(struct RETROFLAT_BITMAP *target, const RETROFLAT_COLOR color, retroflat_pxxy_t x, retroflat_pxxy_t y, retroflat_pxxy_t w, retroflat_pxxy_t h, uint8_t flags)
Draw an ellipse onto the target RETROFLAT_BITMAP.
MERROR_RETVAL retroflat_draw_lock(struct RETROFLAT_BITMAP *bmp)
Lock a bitmap for drawing. This will be done automatically if necessary and not called explicitly,...
#define RETROFLAT_DRAW_FLAG_FILL
Flag for retroflat_rect() or retroflat_ellipse(), indicating drawn shape should be filled.
Definition retroflt.h:376
void retroflat_line(struct RETROFLAT_BITMAP *target, const RETROFLAT_COLOR color, retroflat_pxxy_t x1, retroflat_pxxy_t y1, retroflat_pxxy_t x2, retroflat_pxxy_t y2, uint8_t flags)
Draw a straight line onto the target RETROFLAT_BITMAP.
void retroflat_rect(struct RETROFLAT_BITMAP *target, const RETROFLAT_COLOR color, retroflat_pxxy_t x, retroflat_pxxy_t y, retroflat_pxxy_t w, retroflat_pxxy_t h, uint8_t flags)
Draw a rectangle onto the target RETROFLAT_BITMAP.
RETROFLAT_IN_KEY retroflat_poll_input(struct RETROFLAT_INPUT *input)
Poll input devices (keyboard/mouse) and return the latest event.
void retroflat_message(uint8_t flags, const char *title, const char *format,...)
Display a message in a dialog box and/or on stderr.
#define RETROFLAT_MSG_FLAG_ERROR
This icon/type flag indicates an error. It will try to display messages in an urgent way with a red i...
Definition retroflt.h:456
void retroflat_timer_handle(void)
This should be called in the API HAL on every iteration of the main loop (this is done automatically ...
void retroflat_resize_v(void)
Platform-specific function to resize virtual screen to match physical window size.
uint8_t retroflat_focus_platform(void)
Platform-specific function to get current focus flags for the generic loop.
void retroflat_set_proc_quit(retroflat_proc_quit_t on_quit_in, void *data_in)
Set the procedure to call when the window is attempted to be closed (on platforms that support window...
MERROR_RETVAL(* retroflat_proc_quit_t)(void *data)
Callback to call when window closing is attempted. Should return MERROR_PREEMPT to defer or MERROR_OK...
Definition retroflt.h:532
ssize_t retroflat_timer_add(retroflat_ms_t time, retroflat_timer_cb_t cb, void *data)
Add a timer callback to be executed at the given time.
void retroflat_set_proc_focus(retroflat_proc_focus_t on_focus_in, void *data_in)
Set the procedure to call when the window gains or loses focus (on platforms that support multitaskin...
void retroflat_set_proc_resize(retroflat_proc_resize_t on_resize_in, void *data_in)
Set the procedure to call when the window is resized (on platforms that support resizing).
MERROR_RETVAL retroflat_vdp_call(const char *proc_name)
Call a function from the retroflat VDP.
MERROR_RETVAL(* retroflat_vdp_proc_t)(struct RETROFLAT_STATE *)
VDP function called from the VDP library.
Definition retroflt.h:508
#define RETROFLAT_VDP_FLAG_PXLOCK
Flag for RETROFLAT_STATE::vdp_flags indicating the VDP requires RetroFlat to pixel-lock the frame bef...
Definition retroflt.h:495
#define retroflat_heartbeat_update()
Check and update RETROFLAT_STATE::heartbeat_frame.
Definition retroflt.h:1108
void retroflat_shutdown(int retval)
Deinitialize RetroFlat and its underlying layers. This should be called once at the end of the progra...
#define RETROFLAT_STATE_FLAG_KEY_REPEAT
Flag indicating keyboard repeat is enabled.
Definition retroflt.h:408
#define retroflat_screen_colors()
Get the number of available on-screen colors.
Definition retroflt.h:1083
#define RETROFLAT_STATE_FLAG_SCREENSAVER
Flag indicating the current application is running as a screensaver.
Definition retroflt.h:415
int16_t retroflat_pxxy_t
Type used for surface pixel coordinates.
Definition retroflt.h:925
#define RETROFLAT_STATE_FLAG_WAIT_FOR_FPS
Do not execute any more inter-frame loops until next frame.
Definition retroflt.h:426
#define RETROFLAT_STATE_FLAG_RUNNING
Flag indicating that retroflat_loop() should continue executing.
Definition retroflt.h:394
#define retroflat_heartbeat_set(len, max)
Set parameters for the RETROFLAT_STATE::heartbeat_frame.
Definition retroflt.h:1091
#define retroflat_system_task()
Platform-specific task that should be called on every iteration of the generic loop....
Definition retroflt.h:1023
void(* retroflat_loop_iter)(void *data)
Prototype for the main loop function passed to retroflat_loop().
Definition retroflt.h:813
MERROR_RETVAL retroflat_loop(retroflat_loop_iter frame_iter, retroflat_loop_iter loop_iter, void *data)
This should be called once in the main body of the program in order to enter the main loop....
MERROR_RETVAL retroflat_init(int argc, char *argv[], struct RETROFLAT_ARGS *args)
Initialize RetroFlat and its underlying layers. This should be called once at the beginning of the pr...
#define RETROFLAT_STATE_FLAG_UNLOCK_FPS
Flag indicating FPS should not be capped.
Definition retroflt.h:401
void retrosoft_rect(retroflat_blit_t *target, const RETROFLAT_COLOR color_idx, int x, int y, int w, int h, uint8_t flags)
Draw a rectangle at the given coordinates, with the given dimensions.
void retrosoft_ellipse(retroflat_blit_t *target, RETROFLAT_COLOR color, int x, int y, int w, int h, uint8_t flags)
Draw an ellipsoid at the given coordinates, with the given dimensions.
void retrosoft_line(retroflat_blit_t *target, RETROFLAT_COLOR color, int x1, int y1, int x2, int y2, uint8_t flags)
Draw a line from x1, y1 to x2, y2.
int16_t retroflat_tile_t
Value for an individual tile in a RETROTILE_LAYER.
Definition retroflt.h:19
Lower-level retargetable 3D engine wrapper.
Tools for drawing shape primatives.
Abstraction layer header for sound on retro systems.
Definition mfmt.h:124
Generic image description struct.
Definition mfmt.h:69
Definition retroflt.h:1000
Struct containing configuration values for a RetroFlat program.
Definition retroflt.h:1035
char * config_path
Relative path of local config file (if not using registry).
Definition retroflt.h:1045
char * assets_path
Relative path under which bitmap assets are stored.
Definition retroflt.h:1043
int screen_w
Desired screen or window width in pixels.
Definition retroflt.h:1052
int screen_h
Desired screen or window height in pixels.
Definition retroflt.h:1059
int screen_colors
Desired colors (2, 4, or 16, for now). \TODO Implement command-line argument for this....
Definition retroflt.h:1071
char * title
Title to set for the main program Window if applicable on the target platform.
Definition retroflt.h:1041
int screen_y
Desired window Y position in pixels.
Definition retroflt.h:1063
int screen_x
Desired window X position in pixels.
Definition retroflt.h:1061
Struct passed to retroflat_poll_input() to hold return data.
Definition retroflt.h:865
int mouse_y
Y-coordinate of the mouse pointer in pixels if the returned event is a mouse click.
Definition retroflt.h:875
int mouse_x
X-coordinate of the mouse pointer in pixels if the returned event is a mouse click.
Definition retroflt.h:870
Global singleton containing state for the current platform.
Definition retroflt.h:1130
size_t screen_h
The screen height as seen by the system, after scaling.
Definition retroflt.h:1198
uint8_t vdp_flags
Flags set by the RetroFlat VDP API.
Definition retroflt.h:1174
size_t screen_v_w
The screen width as seen by our program, before scaling.
Definition retroflt.h:1187
size_t screen_colors
The number of colors the screen is capable of displaying.
Definition retroflt.h:1200
RETROFLAT_COLOR_DEF palette[RETROFLAT_COLORS_CT_MAX]
Index of available colors, initialized on platform init.
Definition retroflt.h:1245
uint8_t retroflat_flags
maug_retroflt_flags indicating current system status.
Definition retroflt.h:1140
uint8_t heartbeat_max
When RETROFLAT_STATE::heartbeat_frame reaches this value, it will reset to 0.
Definition retroflt.h:1227
retroflat_timer_cb_t timers_cb[RETROFLAT_TIMER_CT_MAX]
List of installable timers that should be tended every frame with retroflat_handle_timers().
Definition retroflt.h:1267
uint8_t heartbeat_frame
Simple iteration loop that can be used to time e.g. perpetual sprite animations. Modify parameters wi...
Definition retroflt.h:1222
size_t screen_w
The screen width as seen by the system, after scaling.
Definition retroflt.h:1196
void * vdp_exe
A handle for the loaded RetroFlat VDP API module.
Definition retroflt.h:1164
uint8_t last_focus_flags
Used internally to track whether to call on_focus().
Definition retroflt.h:1237
uint16_t heartbeat_len
Number of ms to stay on current value of RETROFLAT_STATE::heartbeat_frame before incrementing....
Definition retroflt.h:1216
void * vdp_data
Pointer to data defined by the RetroFlat VDP API for its use.
Definition retroflt.h:1170
struct RETROFLAT_BITMAP * vdp_buffer_in
A buffer assembled and passed to the RetroFlat VDP API for it to modify, or NULL if no VDP is loaded.
Definition retroflt.h:1158
size_t screen_v_h
The screen height as seen by our program, before scaling.
Definition retroflt.h:1194
int screen_scale
Off-screen buffer bitmap.
Definition retroflt.h:1147
retroflat_proc_quit_t on_quit
Should be set with retroflat_set_proc_focus().
Definition retroflt.h:1240
char vdp_args[RETROFLAT_VDP_ARGS_SZ_MAX+1]
CLI args passed with -vdp to the RetroFlat VDP API.
Definition retroflt.h:1172
retroflat_proc_focus_t on_focus
Should be set with retroflat_set_proc_focus().
Definition retroflt.h:1234
retroflat_proc_resize_t on_resize
Should be set with retroflat_set_proc_resize().
Definition retroflt.h:1230