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();
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();
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();
1345
1346uint8_t* retroflat_vdp_get_vdp_out();
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
1525 struct RETROFLAT_BITMAP* target, const char* str, size_t str_sz,
1526 const char* font_str,
1527 retroflat_pxxy_t* w_out, retroflat_pxxy_t* h_out, uint8_t flags );
1528
1547 struct RETROFLAT_BITMAP* target, const RETROFLAT_COLOR color,
1548 const char* str, int str_sz, const char* font_str,
1549 retroflat_pxxy_t x_orig, retroflat_pxxy_t y_orig,
1550 uint8_t flags );
1551
1552/* TODO: Documentation! */
1553void retroflat_get_palette( uint8_t idx, uint32_t* rgb );
1554
1555MERROR_RETVAL retroflat_set_palette( uint8_t idx, uint32_t rgb );
1556 /* maug_retroflt_bitmap */
1558
1564
1575 retroflat_proc_resize_t on_resize_in, void* data_in );
1576
1582 retroflat_ms_t time, retroflat_timer_cb_t cb, void* data );
1583
1593 retroflat_proc_focus_t on_focus_in, void* data_in );
1594
1604 retroflat_proc_quit_t on_quit_in, void* data_in );
1605
1614
1619#define retroflat_soft_quit( retval ) \
1620 if( \
1621 NULL == g_retroflat_state->on_quit || \
1622 MERROR_PREEMPT != g_retroflat_state->on_quit( \
1623 g_retroflat_state->on_quit_data \
1624 ) ) { \
1625 retroflat_quit( retval ); \
1626 }
1627
1633
1639
1645 /* maug_retroflt_platform_handler */
1647 /* maug_retroflt_platform */
1649
1654
1660RETROFLAT_IN_KEY retroflat_poll_input( struct RETROFLAT_INPUT* input );
1661 /* maug_retroflt_input */
1663
1664#ifdef RETROFLT_C
1665
1666MAUG_CONST int16_t SEG_MCONST gc_retroflat_offsets8_x[8] =
1667 { 0, 1, 1, 1, 0, -1, -1, -1 };
1668MAUG_CONST int16_t SEG_MCONST gc_retroflat_offsets8_y[8] =
1669 { -1, -1, 0, 1, 1, 1, 0, -1 };
1670
1671MAUG_CONST int16_t SEG_MCONST gc_retroflat_offsets4_x[4] =
1672 { 0, 1, 0, -1 };
1673MAUG_CONST int16_t SEG_MCONST gc_retroflat_offsets4_y[4] =
1674 { -1, 0, 1, 0 };
1675
1676# ifdef RETROFLAT_STATE_ON_STACK
1677struct RETROFLAT_STATE SEG_MGLOBAL g_retroflat_state_stack;
1678# else
1679MAUG_MHANDLE SEG_MGLOBAL g_retroflat_state_h = (MAUG_MHANDLE)NULL;
1680# endif /* RETROFLAT_STATE_ON_STACK */
1681struct RETROFLAT_STATE* SEG_MGLOBAL g_retroflat_state = NULL;
1682
1683# define RETROFLAT_COLOR_TABLE_CONSTS( idx, name_l, name_u, r, g, b, cgac, cgad ) \
1684 MAUG_CONST RETROFLAT_COLOR SEG_MCONST RETROFLAT_COLOR_ ## name_u = idx;
1685
1686RETROFLAT_COLOR_TABLE( RETROFLAT_COLOR_TABLE_CONSTS )
1687
1688# define RETROFLAT_COLOR_TABLE_NAMES( idx, name_l, name_u, r, g, b, cgac, cgad ) \
1689 #name_u,
1690
1691MAUG_CONST char* SEG_MCONST gc_retroflat_color_names[] = {
1692 RETROFLAT_COLOR_TABLE( RETROFLAT_COLOR_TABLE_NAMES )
1693};
1694
1695/* Call a second time, to add function bodies. */
1696#include <retrovi2.h>
1697
1698/* Callback table is down below, after the statically-defined callbacks. */
1699
1700/* === Function Definitions === */
1701
1702MERROR_RETVAL retroflat_build_filename_path(
1703 const maug_path filename_in, const char* filename_ext,
1704 char* buffer_out, size_t buffer_out_sz, uint8_t flags
1705) {
1706 MERROR_RETVAL retval = MERROR_OK;
1707
1708 assert( 1 < buffer_out_sz );
1709
1710 /* Build the path to the bitmap. */
1711 maug_mzero( buffer_out, buffer_out_sz );
1712 if(
1715 ) {
1716 /* TODO: Error checking. */
1717 maug_snprintf( buffer_out, buffer_out_sz - 1, "%s", filename_in );
1718 } else if( NULL != filename_ext ) {
1719 /* TODO: Error checking. */
1720 maug_snprintf( buffer_out, buffer_out_sz - 1, "%s%c%s.%s",
1721 g_retroflat_state->assets_path, RETROFLAT_PATH_SEP,
1722 filename_in, filename_ext );
1723 } else {
1724 maug_snprintf( buffer_out, buffer_out_sz - 1, "%s%c%s",
1725 g_retroflat_state->assets_path, RETROFLAT_PATH_SEP,
1726 filename_in );
1727 }
1728
1729 return retval;
1730}
1731
1732/* === */
1733
1734# if (defined( RETROFLAT_SOFT_SHAPES ) || defined( RETROFLAT_SOFT_LINES ) || \
1735 defined( RETROFLAT_3D ))
1736/* RETROSOFT_PRESENT is different from RETROFLAT_SOFT_SHAPES in that it only
1737 * indicates that the retrosoft library is loaded, not that it is the default
1738 * for drawing primatives!
1739 */
1740# define RETROSOFT_PRESENT
1741# endif
1742
1743# if defined( RETROFLAT_3D )
1744# if !defined( MAUG_NO_AUTO_C )
1745# define RETRO3D_C
1746# define RETRO3DP_C
1747# define RETROFP_C
1748# endif /* MAUG_NO_AUTO_C */
1749# include <retro3dp.h>
1750# include <retro3d.h>
1751# include <retro3du.h>
1752# include <retapi3.h>
1753# endif /* RETROFLAT_3D */
1754
1755# ifdef RETROSOFT_PRESENT
1756# if !defined( MAUG_NO_AUTO_C )
1757# define RETROSFT_C
1758# endif /* !MAUG_NO_AUTO_C */
1759# define RETROSOFT_PRESENT
1760# include <retrosft.h>
1761# endif /* RETROFLAT_SOFT_SHAPES */
1762
1763# ifndef RETROFLAT_NO_SOUND
1764# include <retapis.h>
1765# endif /* !RETROFLAT_NO_SOUND */
1766
1767# include <retapii.h>
1768
1769# if defined( RETROFLAT_VDP ) && defined( RETROFLAT_OS_UNIX )
1770# include <dlfcn.h>
1771# endif
1772
1773/* Still inside RETROFLT_C! */
1774
1775/* === */
1776
1777#ifndef RETROFLAT_NO_GENERIC_LOOP
1778
1779MERROR_RETVAL retroflat_loop_generic(
1780 retroflat_loop_iter frame_iter, retroflat_loop_iter loop_iter, void* data
1781) {
1782 MERROR_RETVAL retval = MERROR_OK;
1783 retroflat_ms_t next = 0,
1784 now = 0;
1785
1786 g_retroflat_state->loop_iter = (retroflat_loop_iter)loop_iter;
1787 g_retroflat_state->loop_data = (void*)data;
1788 g_retroflat_state->frame_iter = (retroflat_loop_iter)frame_iter;
1789
1790 if(
1792 (g_retroflat_state->retroflat_flags & RETROFLAT_STATE_FLAG_RUNNING)
1793 ) {
1794 /* Main loop is already running, so we're just changing the iter call
1795 * and leaving!
1796 */
1797 debug_printf( 1, "main loop already running!" );
1798 goto cleanup;
1799 }
1800
1801 g_retroflat_state->retroflat_flags |= RETROFLAT_STATE_FLAG_RUNNING;
1802 do {
1804
1805 if(
1806 /* Not waiting for the next frame? */
1808 (RETROFLAT_STATE_FLAG_WAIT_FOR_FPS & g_retroflat_state->retroflat_flags) &&
1809 /* Inter-frame loop present? */
1810 NULL != g_retroflat_state->loop_iter
1811 ) {
1812 /* Run the loop iter as many times as possible. */
1813 g_retroflat_state->loop_iter( g_retroflat_state->loop_data );
1814 }
1815 if(
1817 (RETROFLAT_STATE_FLAG_UNLOCK_FPS & g_retroflat_state->retroflat_flags) &&
1818 retroflat_get_ms() < next
1819 ) {
1820 /* Sleep/low power for a bit. */
1821 continue;
1822 }
1823
1825
1827
1828 if(
1829 NULL != g_retroflat_state->on_focus &&
1830 g_retroflat_state->last_focus_flags != retroflat_focus_platform()
1831 ) {
1832 g_retroflat_state->last_focus_flags = retroflat_focus_platform();
1833 retval = g_retroflat_state->on_focus(
1834 g_retroflat_state->last_focus_flags,
1835 g_retroflat_state->on_focus_data );
1836 maug_cleanup_if_not_ok();
1837 }
1838
1839 if( NULL != g_retroflat_state->frame_iter ) {
1840 /* Run the frame iterator once per FPS tick. */
1841 g_retroflat_state->frame_iter( g_retroflat_state->loop_data );
1842 }
1843 /* Reset wait-for-frame flag AFTER frame callback. */
1844 g_retroflat_state->retroflat_flags &= ~RETROFLAT_STATE_FLAG_WAIT_FOR_FPS;
1845 now = retroflat_get_ms();
1846 if( now + retroflat_fps_next() > now ) {
1847 next = now + retroflat_fps_next();
1848 } else {
1849 /* Rollover protection. */
1850 /* TODO: Add difference from now/next to 0 here. */
1851 next = 0;
1852 }
1853 } while(
1855 (RETROFLAT_STATE_FLAG_RUNNING & g_retroflat_state->retroflat_flags)
1856 );
1857 retval = g_retroflat_state->retval;
1858
1859cleanup:
1860
1861 /* This should be set by retroflat_quit(). */
1862 return retval;
1863}
1864
1865#endif /* !RETROFLAT_NO_GENERIC_LOOP */
1866
1867/* === */
1868
1869# if !defined( RETROFLAT_NO_KEYBOARD )
1870
1871char retroflat_vk_to_ascii( RETROFLAT_IN_KEY k, uint8_t flags ) {
1872 char c = 0;
1873 char offset_lower = 0;
1874
1875 if( RETROFLAT_INPUT_MOD_SHIFT != (RETROFLAT_INPUT_MOD_SHIFT & flags) ) {
1876 /* Shift is *not* being held down. */
1877
1878 if( RETROFLAT_KEY_A <= k && RETROFLAT_KEY_Z >= k ) {
1879 if(
1880 RETROFLAT_INPUT_FORCE_UPPER !=
1881 (RETROFLAT_INPUT_FORCE_UPPER & flags)
1882 ) {
1883 /* Key is alphabetical and we're not forcing uppercase. */
1884 offset_lower = 0x20;
1885 }
1886 } else {
1887 offset_lower = 1;
1888 }
1889 }
1890
1891 switch( k ) {
1892 case RETROFLAT_KEY_A: c = 0x41 + offset_lower; break;
1893 case RETROFLAT_KEY_B: c = 0x42 + offset_lower; break;
1894 case RETROFLAT_KEY_C: c = 0x43 + offset_lower; break;
1895 case RETROFLAT_KEY_D: c = 0x44 + offset_lower; break;
1896 case RETROFLAT_KEY_E: c = 0x45 + offset_lower; break;
1897 case RETROFLAT_KEY_F: c = 0x46 + offset_lower; break;
1898 case RETROFLAT_KEY_G: c = 0x47 + offset_lower; break;
1899 case RETROFLAT_KEY_H: c = 0x48 + offset_lower; break;
1900 case RETROFLAT_KEY_I: c = 0x49 + offset_lower; break;
1901 case RETROFLAT_KEY_J: c = 0x4a + offset_lower; break;
1902 case RETROFLAT_KEY_K: c = 0x4b + offset_lower; break;
1903 case RETROFLAT_KEY_L: c = 0x4c + offset_lower; break;
1904 case RETROFLAT_KEY_M: c = 0x4d + offset_lower; break;
1905 case RETROFLAT_KEY_N: c = 0x4e + offset_lower; break;
1906 case RETROFLAT_KEY_O: c = 0x4f + offset_lower; break;
1907 case RETROFLAT_KEY_P: c = 0x50 + offset_lower; break;
1908 case RETROFLAT_KEY_Q: c = 0x51 + offset_lower; break;
1909 case RETROFLAT_KEY_R: c = 0x52 + offset_lower; break;
1910 case RETROFLAT_KEY_S: c = 0x53 + offset_lower; break;
1911 case RETROFLAT_KEY_T: c = 0x54 + offset_lower; break;
1912 case RETROFLAT_KEY_U: c = 0x55 + offset_lower; break;
1913 case RETROFLAT_KEY_V: c = 0x56 + offset_lower; break;
1914 case RETROFLAT_KEY_W: c = 0x57 + offset_lower; break;
1915 case RETROFLAT_KEY_X: c = 0x58 + offset_lower; break;
1916 case RETROFLAT_KEY_Y: c = 0x59 + offset_lower; break;
1917 case RETROFLAT_KEY_Z: c = 0x60 + offset_lower; break;
1918 case RETROFLAT_KEY_0: c = offset_lower ? 0x30 : ')'; break;
1919 case RETROFLAT_KEY_1: c = offset_lower ? 0x31 : '!'; break;
1920 case RETROFLAT_KEY_2: c = offset_lower ? 0x32 : '@'; break;
1921 case RETROFLAT_KEY_3: c = offset_lower ? 0x33 : '#'; break;
1922 case RETROFLAT_KEY_4: c = offset_lower ? 0x34 : '$'; break;
1923 case RETROFLAT_KEY_5: c = offset_lower ? 0x35 : '%'; break;
1924 case RETROFLAT_KEY_6: c = offset_lower ? 0x36 : '^'; break;
1925 case RETROFLAT_KEY_7: c = offset_lower ? 0x37 : '&'; break;
1926 case RETROFLAT_KEY_8: c = offset_lower ? 0x38 : '*'; break;
1927 case RETROFLAT_KEY_9: c = offset_lower ? 0x39 : '('; break;
1928 case RETROFLAT_KEY_SPACE: c = ' '; break;
1929 case RETROFLAT_KEY_BKSP: c = 0x08; break;
1930 case RETROFLAT_KEY_ENTER: c = '\n'; break;
1931 case RETROFLAT_KEY_SEMICOLON: c = offset_lower ? ';' : ':'; break;
1932 case RETROFLAT_KEY_DASH: c = offset_lower ? '-' : '_'; break;
1933 case RETROFLAT_KEY_SLASH: c = offset_lower ? '/' : '?'; break;
1934 case RETROFLAT_KEY_PERIOD: c = offset_lower ? '.' : '>'; break;
1935 case RETROFLAT_KEY_COMMA: c = offset_lower ? ',' : '<'; break;
1936 case RETROFLAT_KEY_QUOTE: c = offset_lower ? '\'' : '"'; break;
1937 case RETROFLAT_KEY_EQUALS: c = offset_lower ? '=' : '+'; break;
1938 case RETROFLAT_KEY_BACKSLASH: c = offset_lower ? '\\' : '|'; break;
1939 case RETROFLAT_KEY_BRACKETL: c = offset_lower ? '[' : '{'; break;
1940 case RETROFLAT_KEY_BRACKETR: c = offset_lower ? ']' : '}'; break;
1941#ifndef RETROFLAT_API_PC_BIOS
1942 /* TODO: FIXME in DOS! */
1943 case RETROFLAT_KEY_GRAVE: c = offset_lower ? '`' : '~'; break;
1944#endif /* !RETROFLAT_API_PC_BIOS */
1945 }
1946
1947 debug_printf( RETROINPUT_TRACE_LVL, "0x%02x", c );
1948
1949 return c;
1950}
1951
1952#endif /* !RETROFLAT_NO_KEYBOARD */
1953
1954/* === */
1955
1956/* TODO: Migrate all platform-specific parts below to retapif.h. */
1957#include <retapif.h>
1958
1959/* === */
1960
1961# ifndef RETROFLAT_NO_CLI
1962
1963# if !defined( RETROFLAT_NO_SOUND ) && defined( RETROSND_ARGS )
1964
1965static MERROR_RETVAL retrosnd_cli_rsl(
1966 const char* arg, ssize_t arg_c, struct RETROFLAT_ARGS* args
1967) {
1968 if(
1969 0 <= arg_c &&
1970 0 == maug_strncmp( MAUG_CLI_SIGIL "rsl", arg, MAUG_CLI_SIGIL_SZ + 4 )
1971 ) {
1972 args->sound.flags |= RETROSND_ARGS_FLAG_LIST_DEVS;
1973 }
1974 return MERROR_OK;
1975}
1976
1977# endif /* !RETROFLAT_NO_SOUND && RETROSND_ARGS */
1978
1979# if !defined( RETROFLAT_API_PC_BIOS ) && !defined( RETROFLAT_NO_CLI_SZ )
1980
1981static MERROR_RETVAL retroflat_cli_rfs(
1982 const char* arg, ssize_t arg_c, struct RETROFLAT_ARGS* args
1983) {
1984 if( 1 < arg_c ) {
1985 args->screen_scale = atoi( arg );
1986 debug_printf( 3, "screen scale arg set to: %d", args->screen_scale );
1987 }
1988 return MERROR_OK;
1989}
1990
1991static MERROR_RETVAL retroflat_cli_rfx(
1992 const char* arg, ssize_t arg_c, struct RETROFLAT_ARGS* args
1993) {
1994 if( 0 > arg_c ) {
1995 if( 0 == args->screen_w ) {
1996 args->screen_x = 0;
1997 }
1998 } else if(
1999 0 == maug_strncmp( MAUG_CLI_SIGIL "rfx", arg, MAUG_CLI_SIGIL_SZ + 4 )
2000 ) {
2001 /* The next arg must be the new var. */
2002 } else {
2003 args->screen_x = atoi( arg );
2004 }
2005 return MERROR_OK;
2006}
2007
2008static MERROR_RETVAL retroflat_cli_rfy(
2009 const char* arg, ssize_t arg_c, struct RETROFLAT_ARGS* args
2010) {
2011 if( 0 > arg_c ) {
2012 if( 0 == args->screen_h ) {
2013 args->screen_y = 0;
2014 }
2015 } else if(
2016 0 == maug_strncmp( MAUG_CLI_SIGIL "rfy", arg, MAUG_CLI_SIGIL_SZ + 4 )
2017 ) {
2018 /* The next arg must be the new var. */
2019 } else {
2020 args->screen_y = atoi( arg );
2021 }
2022 return MERROR_OK;
2023}
2024
2025static MERROR_RETVAL retroflat_cli_rfw(
2026 const char* arg, ssize_t arg_c, struct RETROFLAT_ARGS* args
2027) {
2028 if( 0 > arg_c ) {
2029 if( 0 == args->screen_w ) {
2030 args->screen_w = RETROFLAT_DEFAULT_SCREEN_W;
2031 debug_printf( 1, "setting arg screen_w to default %d:",
2032 args->screen_w );
2033 }
2034 } else if(
2035 0 == maug_strncmp( MAUG_CLI_SIGIL "rfw", arg, MAUG_CLI_SIGIL_SZ + 4 )
2036 ) {
2037 /* The next arg must be the new var. */
2038 } else {
2039 args->screen_w = atoi( arg );
2040 debug_printf( 1, "setting arg screen_w to: %d",
2041 args->screen_w );
2042 }
2043 return MERROR_OK;
2044}
2045
2046static MERROR_RETVAL retroflat_cli_rfh(
2047 const char* arg, ssize_t arg_c, struct RETROFLAT_ARGS* args
2048) {
2049 if( 0 > arg_c ) {
2050 if( 0 == args->screen_h ) {
2051 args->screen_h = RETROFLAT_DEFAULT_SCREEN_H;
2052 debug_printf( 1, "setting arg screen_h to default: %d",
2053 args->screen_h );
2054 }
2055 } else if(
2056 0 == maug_strncmp( MAUG_CLI_SIGIL "rfh", arg, MAUG_CLI_SIGIL_SZ + 4 )
2057 ) {
2058 /* The next arg must be the new var. */
2059 } else {
2060 args->screen_h = atoi( arg );
2061 debug_printf( 1, "setting arg screen_h to: %d",
2062 args->screen_h );
2063 }
2064 return MERROR_OK;
2065}
2066
2067# endif /* !RETROFLAT_API_PC_BIOS && !RETROFLAT_NO_CLI_SZ */
2068
2069# ifdef RETROFLAT_VDP
2070static MERROR_RETVAL retroflat_cli_vdp(
2071 const char* arg, ssize_t arg_c, struct RETROFLAT_ARGS* args
2072) {
2073 if( 0 == maug_strncmp( MAUG_CLI_SIGIL "vdp", arg, MAUG_CLI_SIGIL_SZ + 4 ) ) {
2074 /* Next arg is VDP args str. */
2075 } else {
2076 maug_mzero( g_retroflat_state->vdp_args, RETROFLAT_VDP_ARGS_SZ_MAX + 1 );
2077 maug_strncpy(
2078 g_retroflat_state->vdp_args, arg, RETROFLAT_VDP_ARGS_SZ_MAX );
2079 debug_printf( 1, "VDP args: %s", g_retroflat_state->vdp_args );
2080 }
2081 return MERROR_OK;
2082}
2083# endif /* RETROFLAT_VDP */
2084
2085static MERROR_RETVAL retroflat_cli_u(
2086 const char* arg, ssize_t arg_c, struct RETROFLAT_ARGS* args
2087) {
2088 if( 0 > arg_c ) {
2089 args->flags &= ~RETROFLAT_STATE_FLAG_UNLOCK_FPS;
2090 } else if(
2091 0 == maug_strncmp( MAUG_CLI_SIGIL "rfu", arg, MAUG_CLI_SIGIL_SZ + 4 )
2092 ) {
2093 debug_printf( 1, "unlocking FPS..." );
2094 args->flags |= RETROFLAT_STATE_FLAG_UNLOCK_FPS;
2095 }
2096 return MERROR_OK;
2097}
2098
2099# ifndef RETROFLAT_NO_PAD
2100
2101static MERROR_RETVAL retroflat_cli_rfj(
2102 const char* arg, ssize_t arg_c, struct RETROFLAT_ARGS* args
2103) {
2104 if( 0 > arg_c ) {
2105 args->joystick_id = -1;
2106 debug_printf( 1, "setting arg joystick_id to default: %d",
2107 args->joystick_id );
2108 } else if(
2109 0 == maug_strncmp( MAUG_CLI_SIGIL "rfj", arg, MAUG_CLI_SIGIL_SZ + 4 )
2110 ) {
2111 /* The next arg must be the new var. */
2112 } else {
2113 args->joystick_id = atoi( arg );
2114 debug_printf( 1, "setting arg joystick_id to: %d",
2115 args->joystick_id );
2116 }
2117 return MERROR_OK;
2118}
2119
2120# endif /* !RETROFLAT_NO_PAD */
2121
2122#endif /* !RETROFLAT_NO_CLI */
2123
2124/* === */
2125
2126/* Still inside RETROFLT_C! */
2127
2129 int argc, char* argv[], struct RETROFLAT_ARGS* args
2130) {
2131
2132 /* = Declare Init Vars = */
2133
2134 MERROR_RETVAL retval = 0;
2135
2136 /* = Begin Init Procedure = */
2137
2138# ifdef RETROFLAT_COMMIT_HASH
2139 debug_printf( 1, "retroflat commit: " RETROFLAT_COMMIT_HASH );
2140# endif /* RETROFLAT_COMMIT_HASH */
2141
2142 debug_printf( 1, "retroflat: initializing..." );
2143
2144 /* System sanity checks. */
2145 assert( 2 <= sizeof( MERROR_RETVAL ) );
2146 assert( 4 == sizeof( uint32_t ) );
2147 assert( 4 == sizeof( int32_t ) );
2148 assert( 2 == sizeof( uint16_t ) );
2149 assert( 2 == sizeof( int16_t ) );
2150 assert( 1 == sizeof( uint8_t ) );
2151 assert( 1 == sizeof( int8_t ) );
2152 assert( NULL != args );
2153 assert( 1 << RETROFLAT_TILE_W_BITS == RETROFLAT_TILE_W );
2154 assert( 1 << RETROFLAT_TILE_H_BITS == RETROFLAT_TILE_H );
2155
2156 debug_printf( 1, "initializing drawing routines..." );
2157
2158 /* Initialize 2D callbacks depending on if we're in 2D or 3D mode.
2159 * Please see retrom2d.h for more information.
2160 */
2161# if defined( RETROFLAT_BMP_TEX )
2162 retroflat_2d_px = (retroflat_px_cb)retro3d_texture_px;
2163 retroflat_2d_line = (retroflat_line_cb)retrosoft_line;
2164 retroflat_2d_rect = (retroflat_rect_cb)retrosoft_rect;
2165 retroflat_2d_ellipse = (retroflat_ellipse_cb)retrosoft_ellipse;
2166 retroflat_2d_blit_bitmap = (retroflat_blit_bitmap_cb)retro3d_texture_blit;
2167 retroflat_2d_load_bitmap =
2168 (retroflat_load_bitmap_cb)retro3d_texture_load_bitmap;
2169 retroflat_2d_create_bitmap =
2170 (retroflat_create_bitmap_cb)retro3d_texture_create;
2171# else
2172 retroflat_2d_px = (retroflat_px_cb)retroflat_px;
2173# ifdef RETROFLAT_SOFT_SHAPES
2174 /* TODO: Work retrosoft routines to use retroflat_blit_t */
2175 retroflat_2d_line = (retroflat_line_cb)retrosoft_line;
2176 retroflat_2d_rect = (retroflat_rect_cb)retrosoft_rect;
2177 retroflat_2d_ellipse = (retroflat_ellipse_cb)retrosoft_ellipse;
2178# else
2179 retroflat_2d_line = (retroflat_line_cb)retroflat_line;
2180 retroflat_2d_rect = (retroflat_rect_cb)retroflat_rect;
2181 retroflat_2d_ellipse = (retroflat_ellipse_cb)retroflat_ellipse;
2182# endif /* RETROFLAT_SOFT_SHAPES */
2183 retroflat_2d_blit_bitmap = (retroflat_blit_bitmap_cb)retroflat_blit_bitmap;
2184 retroflat_2d_load_bitmap = (retroflat_load_bitmap_cb)retroflat_load_bitmap;
2185 retroflat_2d_create_bitmap =
2186 (retroflat_create_bitmap_cb)retroflat_create_bitmap;
2187# endif /* RETROFLAT_BMP_TEX */
2188
2189 debug_printf( 1, "retroflat: MFIX_PRECISION: %f", MFIX_PRECISION );
2190
2191 debug_printf( 1, "retroflat: allocating state (" SIZE_T_FMT " bytes)...",
2192 sizeof( struct RETROFLAT_STATE ) );
2193
2194 debug_printf( 1, "retroflat: size_t is (" SIZE_T_FMT " bytes)...",
2195 sizeof( size_t ) );
2196
2197 debug_printf( 1, "retroflat: ssize_t is (" SIZE_T_FMT " bytes)...",
2198 sizeof( ssize_t ) );
2199
2200 debug_printf( 1, "retroflat: off_t is (" SIZE_T_FMT " bytes)...",
2201 sizeof( off_t ) );
2202
2203 debug_printf( 1, "initializing global state..." );
2204
2205# ifdef RETROFLAT_STATE_ON_STACK
2206 g_retroflat_state = &g_retroflat_state_stack;
2207# else
2208 maug_malloc_test( g_retroflat_state_h, 1, sizeof( struct RETROFLAT_STATE ) );
2209
2210 maug_mlock( g_retroflat_state_h, g_retroflat_state );
2211 if( NULL == g_retroflat_state ) {
2213 "Error", "Could not lock global state!" );
2214 retval = MERROR_ALLOC;
2215 goto cleanup;
2216 }
2217# endif /* RETROFLAT_STATE_ON_STACK */
2218
2219 maug_mzero( g_retroflat_state, sizeof( struct RETROFLAT_STATE ) );
2220
2221 retroflat_heartbeat_set( 1000, 2 );
2222
2223 debug_printf( 1, "initializing platform filesystem..." );
2224 retval = mfile_plt_init();
2225 maug_cleanup_if_not_ok();
2226
2227# ifndef RETROFLAT_NO_CLI
2228
2229 debug_printf( 1, "retroflat: parsing args..." );
2230
2231 /* All platforms: add command-line args based on compile definitons. */
2232
2233 retval = maug_add_arg(
2234 MAUG_CLI_SIGIL "h", MAUG_CLI_SIGIL_SZ + 2, "Display command-line help",
2235 26, maug_cli_h, NULL );
2236
2237# if !defined( RETROFLAT_NO_SOUND ) && defined( RETROSND_ARGS )
2238 retval = maug_add_arg( MAUG_CLI_SIGIL "rsd", MAUG_CLI_SIGIL_SZ + 4,
2239 "Select MIDI device", 0, (maug_cli_cb)retrosnd_cli_rsd, args );
2240 maug_cleanup_if_not_ok();
2241 retval = maug_add_arg( MAUG_CLI_SIGIL "rsl", MAUG_CLI_SIGIL_SZ + 4,
2242 "List MIDI devices", 0, (maug_cli_cb)retrosnd_cli_rsl, args );
2243 maug_cleanup_if_not_ok();
2244# endif /* !RETROFLAT_NO_SOUND && RETROSND_ARGS */
2245
2246# ifdef RETROFLAT_SCREENSAVER
2247 retval = maug_add_arg( MAUG_CLI_SIGIL "p", MAUG_CLI_SIGIL_SZ + 2,
2248 "Preview screensaver", 0, (maug_cli_cb)retroflat_cli_p, args );
2249 maug_cleanup_if_not_ok();
2250 retval = maug_add_arg( MAUG_CLI_SIGIL "s", MAUG_CLI_SIGIL_SZ + 2,
2251 "Launch screensaver", 0, (maug_cli_cb)retroflat_cli_s, args );
2252 maug_cleanup_if_not_ok();
2253# endif /* RETROFLAT_SCREENSAVER */
2254
2255# ifdef RETROFLAT_API_PC_BIOS
2256 retval = maug_add_arg( MAUG_CLI_SIGIL "rfm", MAUG_CLI_SIGIL_SZ + 4,
2257 "Set the screen mode.", 0,
2258 (maug_cli_cb)retroflat_cli_rfm, args );
2259 maug_cleanup_if_not_ok();
2260# elif !defined( RETROFLAT_NO_CLI_SZ )
2261 retval = maug_add_arg( MAUG_CLI_SIGIL "rfs", MAUG_CLI_SIGIL_SZ + 4,
2262 "Set screen scale factor.", 0,
2263 (maug_cli_cb)retroflat_cli_rfs, args );
2264 maug_cleanup_if_not_ok();
2265 retval = maug_add_arg( MAUG_CLI_SIGIL "rfx", MAUG_CLI_SIGIL_SZ + 4,
2266 "Set the screen X position.", 0,
2267 (maug_cli_cb)retroflat_cli_rfx, args );
2268 maug_cleanup_if_not_ok();
2269 retval = maug_add_arg( MAUG_CLI_SIGIL "rfy", MAUG_CLI_SIGIL_SZ + 4,
2270 "Set the screen Y position.", 0,
2271 (maug_cli_cb)retroflat_cli_rfy, args );
2272 maug_cleanup_if_not_ok();
2273 retval = maug_add_arg( MAUG_CLI_SIGIL "rfw", MAUG_CLI_SIGIL_SZ + 4,
2274 "Set the screen width.", 0,
2275 (maug_cli_cb)retroflat_cli_rfw, args );
2276 maug_cleanup_if_not_ok();
2277 retval = maug_add_arg( MAUG_CLI_SIGIL "rfh", MAUG_CLI_SIGIL_SZ + 4,
2278 "Set the screen height.", 0,
2279 (maug_cli_cb)retroflat_cli_rfh, args );
2280 maug_cleanup_if_not_ok();
2281# endif /* !RETROFLAT_NO_CLI_SZ */
2282
2283# ifdef RETROFLAT_VDP
2284 retval = maug_add_arg( MAUG_CLI_SIGIL "vdp", MAUG_CLI_SIGIL_SZ + 4,
2285 "Pass a string of args to the VDP.", 0,
2286 (maug_cli_cb)retroflat_cli_vdp, args );
2287 maug_cleanup_if_not_ok();
2288# endif /* RETROFLAT_VDP */
2289
2290 retval = maug_add_arg( MAUG_CLI_SIGIL "rfu", MAUG_CLI_SIGIL_SZ + 4,
2291 "Unlock FPS.", 0,
2292 (maug_cli_cb)retroflat_cli_u, args );
2293 maug_cleanup_if_not_ok();
2294
2295# ifndef RETROFLAT_NO_PAD
2296 retval = maug_add_arg( MAUG_CLI_SIGIL "rfj", MAUG_CLI_SIGIL_SZ + 4,
2297 "Specify joystick ID to use.", 0,
2298 (maug_cli_cb)retroflat_cli_rfj, args );
2299 maug_cleanup_if_not_ok();
2300# endif /* !RETROFLAT_NO_PAD */
2301
2302 /* Parse command line args. */
2303 retval = maug_parse_args( argc, argv );
2304 maug_cleanup_if_not_ok();
2305
2306# endif /* !RETROFLAT_NO_CLI */
2307
2308 if( 0 == args->screen_w ) {
2309 args->screen_w = RETROFLAT_DEFAULT_SCREEN_W;
2310 debug_printf( 1, "setting arg screen_w to default: %d",
2311 args->screen_w );
2312 }
2313 if( 0 == args->screen_h ) {
2314 args->screen_h = RETROFLAT_DEFAULT_SCREEN_H;
2315 debug_printf( 1, "setting arg screen_h to default: %d",
2316 args->screen_h );
2317 }
2318 if( 0 == args->screen_scale ) {
2319 args->screen_scale = RETROFLAT_DEFAULT_SCREEN_SCALE;
2320 debug_printf( 1, "setting arg screen_scale to default: %d",
2321 args->screen_scale );
2322 }
2323 if( 0 == args->screen_colors ) {
2324 args->screen_colors = 16;
2325 debug_printf( 1, "setting arg screen_colors to default: %d",
2326 args->screen_colors );
2327 }
2328 args->joystick_id = -1;
2329
2330 if(
2332 ) {
2333 g_retroflat_state->retroflat_flags |= RETROFLAT_STATE_FLAG_UNLOCK_FPS;
2334 }
2335
2336 debug_printf( 1, "retroflat: setting config..." );
2337
2338 /* Set the assets path. */
2339 if( NULL != args->assets_path ) {
2340 maug_mzero( g_retroflat_state->assets_path, MAUG_PATH_SZ_MAX );
2341 maug_strncpy( g_retroflat_state->assets_path,
2342 args->assets_path, MAUG_PATH_SZ_MAX - 1 );
2343 }
2344
2345# if defined( RETROFLAT_SCREENSAVER )
2346 if(
2348 (RETROFLAT_STATE_FLAG_SCREENSAVER & args->flags)
2349 ) {
2350 g_retroflat_state->retroflat_flags |= RETROFLAT_STATE_FLAG_SCREENSAVER;
2351 }
2352# endif /* RETROFLAT_SCREENSAVER */
2353
2354# if !defined( RETROFLAT_NO_CLI_SZ )
2355 /* Setup intended screen size. */
2356 g_retroflat_state->screen_scale = args->screen_scale;
2357 g_retroflat_state->screen_v_w = args->screen_w;
2358 g_retroflat_state->screen_v_h = args->screen_h;
2359 g_retroflat_state->screen_w = args->screen_w *
2360 g_retroflat_state->screen_scale;
2361 g_retroflat_state->screen_h = args->screen_h *
2362 g_retroflat_state->screen_scale;
2363 g_retroflat_state->screen_colors = args->screen_colors;
2364
2365 debug_printf( 3, "attempting to initialize platform with: "
2366 SIZE_T_FMT "x" SIZE_T_FMT " pixels (scaled to " SIZE_T_FMT "x" SIZE_T_FMT
2367 ") and " SIZE_T_FMT " colors",
2368 g_retroflat_state->screen_v_w, g_retroflat_state->screen_v_h,
2369 g_retroflat_state->screen_w, g_retroflat_state->screen_h,
2370 g_retroflat_state->screen_colors );
2371# endif /* !RETROFLAT_NO_CLI_SZ */
2372
2373 /* == Platform-Specific Init == */
2374
2375 retval = retroflat_init_platform( argc, argv, args );
2376 maug_cleanup_if_not_ok();
2377
2378# if defined( RETROFLAT_NO_CLI_SZ )
2379 g_retroflat_state->screen_scale = args->screen_scale;
2380 g_retroflat_state->screen_v_w = args->screen_w;
2381 g_retroflat_state->screen_v_h = args->screen_h;
2382 g_retroflat_state->screen_w = args->screen_w *
2383 g_retroflat_state->screen_scale;
2384 g_retroflat_state->screen_h = args->screen_h *
2385 g_retroflat_state->screen_scale;
2386 g_retroflat_state->screen_colors = args->screen_colors;
2387
2388 debug_printf( 3, "initialized platform with: "
2389 SIZE_T_FMT "x" SIZE_T_FMT " pixels (scaled to " SIZE_T_FMT "x" SIZE_T_FMT
2390 ") and " SIZE_T_FMT " colors",
2391 g_retroflat_state->screen_v_w, g_retroflat_state->screen_v_h,
2392 g_retroflat_state->screen_w, g_retroflat_state->screen_h,
2393 g_retroflat_state->screen_colors );
2394# endif /* RETROFLAT_NO_CLI_SZ */
2395
2396 retval = retroflat_init_input( args );
2397 maug_cleanup_if_not_ok();
2398
2399 debug_printf( 3, "screen initialized with: " SIZE_T_FMT "x" SIZE_T_FMT
2400 " pixels (scaled to " SIZE_T_FMT "x" SIZE_T_FMT
2401 ") with " SIZE_T_FMT " colors",
2402 g_retroflat_state->screen_v_w, g_retroflat_state->screen_v_h,
2403 g_retroflat_state->screen_w, g_retroflat_state->screen_h,
2405
2406 /* Setup the refresh grid, if requested, only after screen space has been
2407 * determined by the platform!
2408 */
2409 maug_cleanup_if_eq(
2410 (size_t)0, retroflat_screen_w(), SIZE_T_FMT, MERROR_GUI );
2411 maug_cleanup_if_eq(
2412 (size_t)0, retroflat_screen_h(), SIZE_T_FMT, MERROR_GUI );
2413 maug_cleanup_if_eq(
2414 (size_t)0, retroflat_screen_colors(), SIZE_T_FMT, MERROR_GUI );
2415
2416 /* Init the viewport with no world size, just to get the refresh grid
2417 * allocated.
2418 */
2419 retval = retroview_init( 0, 0 );
2420 maug_cleanup_if_not_ok();
2421
2422# ifdef RETROFLAT_VDP
2423# if defined( RETROFLAT_OS_UNIX )
2424 g_retroflat_state->vdp_exe = dlopen(
2425 "./" RETROFLAT_VDP_LIB_NAME ".so", RTLD_LAZY );
2426# elif defined( RETROFLAT_OS_WIN )
2427 g_retroflat_state->vdp_exe = LoadLibrary(
2428 "./" RETROFLAT_VDP_LIB_NAME ".dll" );
2429# else
2430# error "dlopen undefined!"
2431# endif /* RETROFLAT_OS_UNIX */
2432
2433 if( !(g_retroflat_state->vdp_exe) ) {
2434 error_printf( "not loading VDP" );
2435 /* Skip creating the buffer or trying to run the init proc. */
2436 goto skip_vdp;
2437 }
2438
2439 debug_printf( 1, "initializing VDP..." );
2440 retval = retroflat_vdp_call( "retroflat_vdp_init" );
2441 maug_cleanup_if_not_ok();
2442
2443skip_vdp:
2444
2445# endif /* RETROFLAT_VDP */
2446
2447# ifdef RETROFLAT_3D
2448 retro3d_platform_init();
2449# endif /* RETROFLAT_3D */
2450
2451# if !defined( RETROFLAT_NO_BLANK_INIT ) && !defined( RETROFLAT_3D )
2452 retroflat_draw_lock( NULL );
2454 NULL, RETROFLAT_COLOR_BLACK, 0, 0,
2455 retroflat_screen_w(), retroflat_screen_h(),
2457 retroflat_draw_release( NULL );
2458# endif /* !RETROFLAT_NO_BLANK_INIT */
2459
2460cleanup:
2461
2462 return retval;
2463}
2464
2465/* === */
2466
2467void retroflat_shutdown( int retval ) {
2468
2469 debug_printf( 1, "retroflat shutdown called..." );
2470
2471 retroview_shutdown();
2472
2473# if defined( RETROFLAT_VDP )
2474 if( NULL != g_retroflat_state->vdp_exe ) {
2475 retroflat_vdp_call( "retroflat_vdp_shutdown" );
2476# ifdef RETROFLAT_OS_UNIX
2477 dlclose( g_retroflat_state->vdp_exe );
2478# elif defined( RETROFLAT_OS_WIN )
2479 FreeLibrary( g_retroflat_state->vdp_exe );
2480# else
2481# error "dlclose undefined!"
2482# endif /* RETROFLAT_OS_UNIX || RETROFLAT_OS_WIN */
2483 }
2484# endif /* RETROFLAT_VDP */
2485
2486 /* === Platform-Specific Shutdown === */
2487
2488#ifdef RETROFLAT_3D
2489 retro3d_platform_shutdown();
2490#endif /* RETROFLAT_3D */
2491
2492 retroflat_shutdown_platform( retval );
2493
2494#ifndef RETROFLAT_STATE_ON_STACK
2495 maug_munlock( g_retroflat_state_h, g_retroflat_state );
2496 maug_mfree( g_retroflat_state_h );
2497#endif /* !RETROFLAT_STATE_ON_STACK */
2498
2499}
2500
2501/* === */
2502
2503RETROFLAT_IN_KEY retroflat_repeat_input(
2504 RETROFLAT_IN_KEY key_out, struct RETROFLAT_INPUT* input,
2505 RETROFLAT_IN_KEY* prev_input, int* prev_delay
2506) {
2507
2508 /* Add a slight debounce for gamepad button repeat. */
2509 if( 0 < (*prev_delay) ) {
2510 debug_printf(
2511 RETROINPUT_TRACE_LVL,
2512 "repeat delay: %d", (*prev_delay) );
2513 (*prev_delay)--;
2514 }
2515
2516 /* If nothing else happened and repeat is enabled and a joypad button is
2517 * down, then emulate repeat for it.
2518 */
2519 if(
2520 0 == key_out &&
2522 (RETROFLAT_STATE_FLAG_KEY_REPEAT & g_retroflat_state->retroflat_flags) &&
2523 /* There is an input to repeat. */
2524 0 != *prev_input &&
2525 /* Delay countdown reached. */
2526 0 == *prev_delay
2527 ) {
2528 key_out = *prev_input;
2529 *prev_delay = 1;
2530 debug_printf( RETROINPUT_TRACE_LVL, "repeat: %d", key_out );
2531 }
2532
2533 return key_out;
2534}
2535
2536/* === */
2537
2538ssize_t retroflat_timer_add(
2539 retroflat_ms_t at_time, retroflat_timer_cb_t cb, void* data
2540) {
2541 if( retroflat_get_ms() > at_time ) {
2542 error_printf( "timer time is in the past!" );
2543 return merror_retval_to_sz( MERROR_EXEC );
2544 }
2545
2546 if( g_retroflat_state->timers_ct + 1 < RETROFLAT_TIMER_CT_MAX ) {
2547 g_retroflat_state->timers_cb[g_retroflat_state->timers_ct] = cb;
2548 g_retroflat_state->timers_at[g_retroflat_state->timers_ct] = at_time;
2549 g_retroflat_state->timers_ct++;
2550 return g_retroflat_state->timers_ct - 1;
2551 }
2552
2553 error_printf( "too many timers!" );
2554 return merror_retval_to_sz( MERROR_OVERFLOW );
2555}
2556
2557/* === */
2558
2560 size_t i = 0;
2561 retroflat_ms_t time_now = 0;
2562
2563 time_now = retroflat_get_ms();
2564
2565 for( i = 0 ; g_retroflat_state->timers_ct > i ; i++ ) {
2566 if( g_retroflat_state->timers_at[i] <= time_now ) {
2567 g_retroflat_state->timers_cb[i](
2568 time_now, g_retroflat_state->timers_data[i] );
2569 memmove(
2570 &(g_retroflat_state->timers_cb[i]),
2571 &(g_retroflat_state->timers_cb[i + 1]),
2572 sizeof( retroflat_timer_cb_t ) *
2573 ((g_retroflat_state->timers_ct - i) - 1)
2574 );
2575 memmove(
2576 &(g_retroflat_state->timers_at[i]),
2577 &(g_retroflat_state->timers_at[i + 1]),
2578 sizeof( retroflat_ms_t ) * ((g_retroflat_state->timers_ct - i) - 1)
2579 );
2580 memmove(
2581 &(g_retroflat_state->timers_data[i]),
2582 &(g_retroflat_state->timers_data[i + 1]),
2583 sizeof( void* ) * ((g_retroflat_state->timers_ct - i) - 1)
2584 );
2585 g_retroflat_state->timers_ct--;
2586 i--;
2587 }
2588 }
2589}
2590
2591/* === */
2592
2593# ifdef RETROFLAT_VDP
2594
2595MERROR_RETVAL retroflat_vdp_call( const char* proc_name ) {
2596 MERROR_RETVAL retval = MERROR_OK;
2598# ifdef RETROFLAT_OS_WIN
2599 char proc_name_ex[256];
2600# endif /* RETROFLAT_OS_WIN */
2601
2602 if( NULL == g_retroflat_state->vdp_exe ) {
2603 goto cleanup;
2604 }
2605
2606# ifdef RETROFLAT_OS_UNIX
2607 vdp_proc = dlsym( g_retroflat_state->vdp_exe, proc_name );
2608# elif defined( RETROFLAT_OS_WIN )
2609 /* Append a _ to the proc_name because Watcom? Windows? */
2610 maug_snprintf( proc_name_ex, 255, "%s_", proc_name );
2611 vdp_proc = (retroflat_vdp_proc_t)GetProcAddress(
2612 g_retroflat_state->vdp_exe, proc_name_ex );
2613# else
2614# error "dlsym undefined!"
2615# endif
2616 if( (retroflat_vdp_proc_t)NULL == vdp_proc ) {
2617 goto cleanup;
2618 }
2619
2620# ifdef RETROFLAT_OS_WIN
2621 retroflat_draw_lock( g_retroflat_state->vdp_buffer_in );
2622# endif /* RETROFLAT_OS_WIN */
2623
2624 if(
2625 /* Don't pxlock before init can set the flag! */
2626 0 == strcmp( "retroflat_vdp_flip", proc_name ) &&
2628 (RETROFLAT_VDP_FLAG_PXLOCK & g_retroflat_state->vdp_flags)
2629 ) {
2630 retroflat_vdp_lock( g_retroflat_state->vdp_buffer_in );
2631 retroflat_vdp_lock( g_retroflat_state->vdp_buffer_out );
2632 }
2633
2634 retval = vdp_proc( g_retroflat_state );
2635
2636 if(
2637 0 == strcmp( "retroflat_vdp_flip", proc_name ) &&
2639 (RETROFLAT_VDP_FLAG_PXLOCK & g_retroflat_state->vdp_flags)
2640 ) {
2641 retroflat_vdp_release( g_retroflat_state->vdp_buffer_out );
2642 retroflat_vdp_release( g_retroflat_state->vdp_buffer_in );
2643 }
2644
2645# ifdef RETROFLAT_OS_WIN
2646 retroflat_draw_release( g_retroflat_state->vdp_buffer_in );
2647# endif /* RETROFLAT_OS_WIN */
2648
2649cleanup:
2650 return retval;
2651}
2652
2653# endif /* RETROFLAT_VDP */
2654
2655/* === */
2656
2657#if 0
2658
2659void retroflat_cursor( struct RETROFLAT_BITMAP* target, uint8_t flags ) {
2660#if 0
2661 char mouse_str[11] = "";
2662
2663 maug_snprintf(
2664 mouse_str, 10, "%02d, %02d", g_retroflat_state->last_mouse_x, g_retroflat_state->last_mouse_y );
2665
2667 target, RETROFLAT_COLOR_BLACK,
2668 mouse_str, 10, NULL, 0, 0, 0 );
2670 target, RETROFLAT_COLOR_BLACK,
2671 g_retroflat_state->last_mouse_x - 5, g_retroflat_state->last_mouse_y - 5, 10, 10, 0 );
2672#endif
2673}
2674
2675#endif
2676
2677/* === */
2678
2679void maug_critical_error( const char* msg ) {
2681}
2682
2683/* === */
2684
2686 retroflat_proc_resize_t on_resize_in, void* data_in
2687) {
2688 g_retroflat_state->on_resize = on_resize_in;
2689 g_retroflat_state->on_resize_data = data_in;
2690}
2691
2692/* === */
2693
2695 retroflat_proc_focus_t on_focus_in, void* data_in
2696) {
2697 g_retroflat_state->on_focus = on_focus_in;
2698 g_retroflat_state->on_focus_data = data_in;
2699}
2700
2701/* === */
2702
2704 retroflat_proc_quit_t on_quit_in, void* data_in
2705) {
2706 g_retroflat_state->on_quit = on_quit_in;
2707 g_retroflat_state->on_quit_data = data_in;
2708}
2709
2710/* === */
2711
2712#ifdef RETROFLAT_LOAD_BITMAP_GENERIC
2713
2714/* This is a generic function that uses the callback
2715 * retroflat_load_bitmap_px_cb(), which should be defined by the
2716 * platform-specific API to draw the loaded bitmap onto a native canvas.
2717 */
2718
2720 const char* filename, struct RETROFLAT_BITMAP* bmp_out, uint8_t flags
2721) {
2722 maug_path filename_path;
2723 MERROR_RETVAL retval = MERROR_OK;
2724 mfile_t bmp_file;
2725 struct MFMT_STRUCT_BMPFILE header_bmp;
2726 uint8_t bmp_flags = 0;
2727
2728 assert( NULL != bmp_out );
2729 maug_mzero( bmp_out, sizeof( struct RETROFLAT_BITMAP ) );
2730 retval = retroflat_build_filename_path(
2731 filename, RETROFLAT_BITMAP_EXT, filename_path,
2732 MAUG_PATH_SZ_MAX, flags );
2733 maug_cleanup_if_not_ok();
2734 debug_printf( 1, "retroflat: loading bitmap: %s", filename_path );
2735
2736 bmp_out->flags = flags;
2737
2738 /* Open the bitmap file. */
2739 retval = mfile_open_read( filename_path, &bmp_file );
2740 maug_cleanup_if_not_ok();
2741
2742 /* mfmt file detection system. */
2743 maug_mzero( &header_bmp, sizeof( struct MFMT_STRUCT_BMPFILE ) );
2744 header_bmp.magic[0] = 'B';
2745 header_bmp.magic[1] = 'M';
2746 header_bmp.info.sz = 40;
2747
2748 retval = mfmt_read_bmp_header(
2749 (struct MFMT_STRUCT*)&header_bmp,
2750 &bmp_file, 0, mfile_get_sz( &bmp_file ), &bmp_flags );
2751 maug_cleanup_if_not_ok();
2752
2753 retval = retroflat_create_bitmap(
2754 header_bmp.info.width, header_bmp.info.height, bmp_out, flags );
2755 maug_cleanup_if_not_ok();
2756
2757 retroflat_draw_lock( bmp_out );
2758
2759 retval = mfmt_read_bmp_px_cb(
2760 (struct MFMT_STRUCT*)&header_bmp,
2761 &bmp_file,
2762 header_bmp.px_offset,
2763 mfile_get_sz( &bmp_file ) - header_bmp.px_offset,
2764 bmp_flags,
2765 retroflat_load_bitmap_px_cb,
2766 bmp_out );
2767 maug_cleanup_if_not_ok();
2768
2769 retroflat_draw_release( bmp_out );
2770
2771cleanup:
2772
2773 mfile_close( &bmp_file );
2774
2775 return retval;
2776}
2777
2778#endif /* RETROFLAT_LOAD_BITMAP_GENERIC */
2779
2780#elif !defined( RETROVDP_C ) /* End of RETROFLT_C */
2781
2786
2787extern MAUG_CONST int16_t SEG_MCONST gc_retroflat_offsets8_x[8];
2788extern MAUG_CONST int16_t SEG_MCONST gc_retroflat_offsets8_y[8];
2789extern MAUG_CONST int16_t SEG_MCONST gc_retroflat_offsets4_x[4];
2790extern MAUG_CONST int16_t SEG_MCONST gc_retroflat_offsets4_y[4];
2791
2793
2794#define RETROFLAT_COLOR_TABLE_CONSTS( idx, name_l, name_u, r, g, b, cgac, cgad ) \
2795 extern MAUG_CONST RETROFLAT_COLOR SEG_MCONST RETROFLAT_COLOR_ ## name_u;
2796
2797RETROFLAT_COLOR_TABLE( RETROFLAT_COLOR_TABLE_CONSTS )
2798
2799extern MAUG_CONST char* SEG_MCONST gc_retroflat_color_names[];
2800
2801extern struct RETROFLAT_STATE* SEG_MGLOBAL g_retroflat_state;
2802
2803# if (defined( RETROFLAT_SOFT_SHAPES ) || defined( RETROFLAT_SOFT_LINES ) || \
2804 defined( RETROFLAT_3D ))
2805# define RETROSOFT_PRESENT
2806# endif
2807
2808# ifdef RETROFLAT_3D
2809# include <retro3dp.h>
2810# include <retro3d.h>
2811# include <retro3du.h>
2812# endif /* RETROFLAT_3D */
2813
2814# ifdef RETROSOFT_PRESENT
2815# include <retrosft.h>
2816# endif /* RETROFLAT_SOFT_SHAPES */
2817
2818/* Second retapis.h include for function bodies not needed. */
2819
2820/* Second retapii.h include for function bodies not needed. */
2821
2822#endif /* RETROFLT_C */
2823
2824#ifdef RETROFLAT_XPM
2825#include <retroxpm.h>
2826#endif /* RETROFLAT_XPM */
2827
2828#ifdef RETROVDP_C
2829
2830/* Declarations for VDP sources. */
2831
2832#endif /* RETROVDP_C */
2833 /* maug_retroflt */
2835
2836#endif /* RETROFLT_H */
2837
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:41
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:141
#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_string(struct RETROFLAT_BITMAP *target, const RETROFLAT_COLOR color, const char *str, int str_sz, const char *font_str, retroflat_pxxy_t x_orig, retroflat_pxxy_t y_orig, uint8_t flags)
Draw a text string at the specified location in the specified font and color on the target RETROFLAT_...
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.
void retroflat_string_sz(struct RETROFLAT_BITMAP *target, const char *str, size_t str_sz, const char *font_str, retroflat_pxxy_t *w_out, retroflat_pxxy_t *h_out, uint8_t flags)
Get the size in pixels of a text string when drawn with a given font by retroflat_string().
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()
This should be called in the API HAL on every iteration of the main loop (this is done automatically ...
void retroflat_resize_v()
Platform-specific function to resize virtual screen to match physical window size.
uint8_t retroflat_focus_platform()
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