/* Dune 3 */ /* Main entry */ #include #include #include #include #include #include #include #include #define WIDTH 320 #define HEIGHT 256 #define DEPTH 8 #define FLAGS BMF_DISPLAYABLE | BMF_CLEAR #define BUFFERS 2 #define ESC_KEY 0x45 enum Ports { SAFEPORT, DISPPORT, PORTS }; /* Print optional error message then exit with given return code */ void cleanup( int result, char *message ); struct Library *IntuitionBase = NULL, *GfxBase = NULL, *IFFParseBase = NULL, *UtilityBase = NULL; struct BitMap *bitmaps[ BUFFERS ] = { 0 }; struct Screen *screen = NULL; struct ScreenBuffer *buffers[ BUFFERS ] = { 0 }; struct Window *window = NULL; struct MsgPort *ports[ PORTS ] = { 0 }; struct TagItem screenTags[] = { SA_BitMap, 0, /* To be filled! */ SA_Left, 0, SA_Top, 0, SA_Width, WIDTH, SA_Height, HEIGHT, SA_Depth, DEPTH, SA_Quiet, TRUE, SA_Exclusive, TRUE, SA_Draggable, FALSE, SA_ShowTitle, FALSE, TAG_DONE }; struct TagItem windowTags[] = { WA_CustomScreen, 0, /* To be filled! */ WA_Left, 0, WA_Top, 0, WA_Width, WIDTH, WA_Height, HEIGHT, WA_Backdrop, TRUE, WA_Borderless, TRUE, WA_Activate, TRUE, WA_RMBTrap, TRUE, WA_ReportMouse, TRUE, WA_IDCMP, IDCMP_RAWKEY | IDCMP_MOUSEBUTTONS | IDCMP_MOUSEMOVE, WA_SimpleRefresh, TRUE, WA_NoCareRefresh, TRUE, TAG_DONE }; /* Main routine: init and load start elements, run intro */ int main( void ) { /* * Init and load start elements. * These elements are: * - Amiga shared libraries, * - Amiga devices, * - [ Game configuration ], * - The game screen, * - Basic font and graphics. * The rest of elements will be loaded * when they are needed. * Run intro. * Save settings. */ initLibraries(); initMsgPorts(); initDevices(); initScreen(); initFont(); initGraphics(); runIntro(); saveSettings(); cleanup( RETURN_OK, "" ); /* No errors encountered */ } /* Close all opened libraries */ void cleanLibraries( void ) { if( UtilityBase ) CloseLibrary( UtilityBase ); if( IFFParseBase ) CloseLibrary( IFFParseBase ); if( GfxBase ) CloseLibrary( GfxBase ); if( IntuitionBase ) CloseLibrary( IntuitionBase ); } /* Open Amiga shared libraries */ int initLibraries( void ) { /* * Open required and optional libraries. * Print message and exit on failure. */ atexit( cleanLibraries ); if( !( IntuitionBase = OpenLibrary( "intuition.library", 39L ) ) ) cleanup( RETURN_WARN, "Could not open intuition.library v39!" ); if( !( GfxBase = OpenLibrary( "graphics.library", 39L ) ) ) cleanup( RETURN_WARN, "Could not open graphics.library v39!" ); if( !( IFFParseBase = OpenLibrary( "iffparse.library", 39L ) ) ) cleanup( RETURN_WARN, "Could not open iffparse.library v%39!" ); if( !( UtilityBase = OpenLibrary( "utility.library", 39L ) ) ) cleanup( RETURN_WARN, "Could not open utility.library v%39!" ); return( RETURN_OK ); } /* Delete message ports */ void cleanMsgPorts( void ) { WORD i; atexit( cleanMsgPorts ); for( i = 0; i < PORTS; i++ ) if( ports[ i ] ) DeleteMsgPort( ports[ i ] ); } /* Create message ports */ int initMsgPorts( void ) { WORD i; atexit( cleanMsgPorts ); for( i = 0; i < PORTS; i++ ) if( !( ports[ i ] = CreateMsgPort() ) ) cleanup( RETURN_WARN, "Could not create a message port!" ); return( RETURN_OK ); } /* Open Amiga devices */ int initDevices( void ) { } void cleanScreen( void ) { if( window ) CloseWindow( window ); if( buffers[ 1 ] ) FreeScreenBuffer( screen, buffers[ 1 ] ); if( buffers[ 0 ] ) FreeScreenBuffer( screen, buffers[ 0 ] ); if( screen ) CloseScreen( screen ); if( bitmaps[ 1 ] ) FreeBitMap( bitmaps[ 1 ] ); if( bitmaps[ 0 ] ) FreeBitMap( bitmaps[ 0 ] ); } /* Open game screen */ int initScreen( void ) { /* * Setup double-buffered screen with backdrop window. */ struct TagItem screenTagChanges[] = { SA_BitMap, 0, TAG_DONE }; struct TagItem windowTagChanges[] = { WA_CustomScreen, 0, TAG_DONE }; atexit( cleanScreen ); if( !( bitmaps[ 0 ] = AllocBitMap( WIDTH, HEIGHT, DEPTH, FLAGS, NULL ) ) || !( bitmaps[ 1 ] = AllocBitMap( WIDTH, HEIGHT, DEPTH, FLAGS, NULL ) ) ) cleanup( RETURN_WARN, "Out of graphics mem!" ); screenTagChanges[ 0 ].ti_Data = ( ULONG ) bitmaps[ 0 ]; ApplyTagChanges( screenTags, screenTagChanges ); if( !( screen = OpenScreenTagList( NULL, screenTags ) ) ) cleanup( RETURN_WARN, "Could not open screen!" ); if( !( buffers[ 0 ] = AllocScreenBuffer( screen, bitmaps[ 0 ], 0 ) ) || !( buffers[ 1 ] = AllocScreenBuffer( screen, bitmaps[ 1 ], 0 ) ) ) cleanup( RETURN_WARN, "Could not create screen buffer!" ); windowTagChanges[ 0 ].ti_Data = ( ULONG ) screen; ApplyTagChanges( windowTags, windowTagChanges ); if( !( window = OpenWindowTagList( NULL, windowTags ) ) ) cleanup( RETURN_WARN, "Could not open window!" ); struct DBufInfo *dbi[ 2 ] = { buffers[ 0 ]->sb_DBufInfo, buffers[ 1 ]->sb_DBufInfo }; dbi[ 0 ]->dbi_UserData1 = ( APTR ) 1; dbi[ 0 ]->dbi_UserData2 = ( APTR ) 1; dbi[ 1 ]->dbi_UserData1 = ( APTR ) 0; dbi[ 1 ]->dbi_UserData2 = ( APTR ) 0; dbi[ 0 ]->dbi_SafeMessage.mn_ReplyPort = ports[ SAFEPORT ]; dbi[ 0 ]->dbi_DispMessage.mn_ReplyPort = ports[ DISPPORT ]; dbi[ 1 ]->dbi_SafeMessage.mn_ReplyPort = ports[ SAFEPORT ]; dbi[ 1 ]->dbi_DispMessage.mn_ReplyPort = ports[ DISPPORT ]; while( !ChangeScreenBuffer( screen, buffers[ 0 ] ) ) WaitTOF(); return( RETURN_OK ); } int initFont( void ) { } int initGraphics( void ) { } int runIntro( void ) { ULONG signals[] = { 1L << window->UserPort->mp_SigBit, 1L << ports[ SAFEPORT ]->mp_SigBit, 1L << ports[ DISPPORT ]->mp_SigBit }; ULONG signal = signals[ 0 ] | signals[ 1 ] | signals[ 2 ]; ULONG result; struct IntuiMessage *msg; BOOL done = FALSE; printf( "SAFE: $%X\n", signals[ 1 ] ); printf( "DISP: $%X\n", signals[ 2 ] ); while( !done ) { result = Wait( signal ); printf( "$%X\n", result ); if( result & signals[ 0 ] ) { while( msg = ( struct IntuiMessage * ) GetMsg( window->UserPort ) ) { if( msg->Class == IDCMP_RAWKEY ) { if( msg->Code == ESC_KEY ) { done = TRUE; } } ReplyMsg( ( struct Message * ) msg ); } /* while */ } /* if */ } /* while */ return( RETURN_OK ); } int saveSettings( void ) { } /* Print optional error message then exit with given return code */ void cleanup( int result, char *message ) { if( *message ) puts( message ); exit( result ); } /** EOF **/