⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sdl_main.c

📁 MPEG-4编解码的实现(包括MPEG4视音频编解码)
💻 C
📖 第 1 页 / 共 2 页
字号:
        
        /* Output-to-file option */
        offset += prefs->video_driver_name[0] + 1;
        *( *((char**)prefs_handle) + offset)     = (char)prefs->output_to_file;
        *( *((char**)prefs_handle) + offset + 1) = 0;
              
        AddResource   (prefs_handle, 'CLne', 128, "\pCommand Line");
        WriteResource (prefs_handle);
        UpdateResFile (resource_file);
        DisposeHandle (prefs_handle);
        
        return ResError() == noErr;
    }
    
    return 0;
}

static int readPreferences (PrefsRecord *prefs) {

    int    no_error = 1;
    FSSpec prefs_fsp;

    /* Check for prefs file first */
    if ( getPrefsFile (&prefs_fsp, 0) ) {
    
        short  prefs_resource;
        
        prefs_resource = FSpOpenResFile (&prefs_fsp, fsRdPerm);
        if ( prefs_resource == -1 ) /* this shouldn't happen, but... */
            return 0;
    
        UseResFile   (prefs_resource);
        no_error = readPrefsResource (prefs);     
        CloseResFile (prefs_resource);
    }
    
    /* Fall back to application's resource fork (reading only, so this is safe) */
    else {
    
          no_error = readPrefsResource (prefs);
     }

    return no_error;
}

static int writePreferences (PrefsRecord *prefs) {
    
    int    no_error = 1;
    FSSpec prefs_fsp;
    
    /* Get prefs file, create if it doesn't exist */
    if ( getPrefsFile (&prefs_fsp, 1) ) {
    
        short  prefs_resource;
        
        prefs_resource = FSpOpenResFile (&prefs_fsp, fsRdWrPerm);
        if (prefs_resource == -1)
            return 0;
        no_error = writePrefsResource (prefs, prefs_resource);
        CloseResFile (prefs_resource);
    }
    
    return no_error;
}

/* This is where execution begins */
int main(int argc, char *argv[])
{

#pragma unused(argc, argv)
	
#define DEFAULT_ARGS "\p"                /* pascal string for default args */
#define DEFAULT_VIDEO_DRIVER "\ptoolbox" /* pascal string for default video driver name */	
#define DEFAULT_OUTPUT_TO_FILE 1         /* 1 == output to file, 0 == no output */

#define VIDEO_ID_DRAWSPROCKET 1          /* these correspond to popup menu choices */
#define VIDEO_ID_TOOLBOX      2

    PrefsRecord prefs = { DEFAULT_ARGS, DEFAULT_VIDEO_DRIVER, DEFAULT_OUTPUT_TO_FILE }; 
	
	int     nargs;
	char   **args;
	char   *commandLine;
	
	StrFileName  appNameText;
	int     videodriver     = VIDEO_ID_TOOLBOX;
    int     settingsChanged = 0;
    
    long	i;

	/* Kyle's SDL command-line dialog code ... */
#if !TARGET_API_MAC_CARBON
	InitGraf    (&qd.thePort);
	InitFonts   ();
	InitWindows ();
	InitMenus   ();
	InitDialogs (nil);
#endif
	InitCursor ();
	FlushEvents(everyEvent,0);
#if !TARGET_API_MAC_CARBON
	MaxApplZone ();
#endif
	MoreMasters ();
	MoreMasters ();
#if 0
	/* Intialize SDL, and put up a dialog if we fail */
	if ( SDL_Init (0) < 0 ) {

#define kErr_OK		1
#define kErr_Text	2

        DialogPtr errorDialog;
        short	  dummyType;
    	Rect	  dummyRect;
	    Handle    dummyHandle;
	    short     itemHit;
	
		errorDialog = GetNewDialog (1001, nil, (WindowPtr)-1);
		DrawDialog (errorDialog);
		
		GetDialogItem (errorDialog, kErr_Text, &dummyType, &dummyHandle, &dummyRect);
		SetDialogItemText (dummyHandle, "\pError Initializing SDL");
		
		SetPort (errorDialog);
		do {
			ModalDialog (nil, &itemHit);
		} while (itemHit != kErr_OK);
		
		DisposeDialog (errorDialog);
		exit (-1);
	}
	atexit(cleanup_output);
	atexit(SDL_Quit);
#endif

/* Set up SDL's QuickDraw environment  */
#if !TARGET_API_MAC_CARBON
	SDL_InitQuickDraw(&qd);
#endif

	 if ( readPreferences (&prefs) ) {
		
        if (memcmp (prefs.video_driver_name+1, "DSp", 3) == 0)
            videodriver = 1;
        else if (memcmp (prefs.video_driver_name+1, "toolbox", 7) == 0)
            videodriver = 2;
	 }
	 	
	if ( CommandKeyIsDown() ) {

#define kCL_OK		1
#define kCL_Cancel	2
#define kCL_Text	3
#define kCL_File	4
#define kCL_Video   6
       
        DialogPtr commandDialog;
        short	  dummyType;
        Rect	  dummyRect;
        Handle    dummyHandle;
        short     itemHit;

        /* Assume that they will change settings, rather than do exhaustive check */
        settingsChanged = 1;
        
        /* Create dialog and display it */
        commandDialog = GetNewDialog (1000, nil, (DialogPtr)-1);
        SetPort (commandDialog);
            
        /* Setup controls */
        GetDialogItem   (commandDialog, kCL_File, &dummyType, &dummyHandle, &dummyRect); /* MJS */
        SetControlValue ((ControlHandle)dummyHandle, prefs.output_to_file );

        GetDialogItem     (commandDialog, kCL_Text, &dummyType, &dummyHandle, &dummyRect);
        SetDialogItemText (dummyHandle, prefs.command_line);

        GetDialogItem   (commandDialog, kCL_Video, &dummyType, &dummyHandle, &dummyRect);
        SetControlValue ((ControlRef)dummyHandle, videodriver);

        SetDialogDefaultItem (commandDialog, kCL_OK);
        SetDialogCancelItem  (commandDialog, kCL_Cancel);

        do {
        		
        	ModalDialog(nil, &itemHit); /* wait for user response */
            
            /* Toggle command-line output checkbox */	
        	if ( itemHit == kCL_File ) {
        		GetDialogItem(commandDialog, kCL_File, &dummyType, &dummyHandle, &dummyRect); /* MJS */
        		SetControlValue((ControlHandle)dummyHandle, !GetControlValue((ControlHandle)dummyHandle) );
        	}

        } while (itemHit != kCL_OK && itemHit != kCL_Cancel);

        /* Get control values, even if they did not change */
        GetDialogItem     (commandDialog, kCL_Text, &dummyType, &dummyHandle, &dummyRect); /* MJS */
        GetDialogItemText (dummyHandle, prefs.command_line);

        GetDialogItem (commandDialog, kCL_File, &dummyType, &dummyHandle, &dummyRect); /* MJS */
        prefs.output_to_file = GetControlValue ((ControlHandle)dummyHandle);

        GetDialogItem (commandDialog, kCL_Video, &dummyType, &dummyHandle, &dummyRect);
        videodriver = GetControlValue ((ControlRef)dummyHandle);

        DisposeDialog (commandDialog);

        if (itemHit == kCL_Cancel ) {
        	exit (0);
        }
	}
    
    /* Set pseudo-environment variables for video driver, update prefs */
	switch ( videodriver ) {
	   case VIDEO_ID_DRAWSPROCKET: 
	      putenv ("SDL_VIDEODRIVER=DSp");
	      memcpy (prefs.video_driver_name, "\pDSp", 4);
	      break;
	   case VIDEO_ID_TOOLBOX:
	      putenv ("SDL_VIDEODRIVER=toolbox");
	      memcpy (prefs.video_driver_name, "\ptoolbox", 8);
	      break;
	}

    /* Redirect standard I/O to files */
	if ( prefs.output_to_file ) {
		freopen (STDOUT_FILE, "w", stdout);
		freopen (STDERR_FILE, "w", stderr);
	} else {
		fclose (stdout);
		fclose (stderr);
	}
   
    if (settingsChanged) {
        /* Save the prefs, even if they might not have changed (but probably did) */
        if ( ! writePreferences (&prefs) )
            fprintf (stderr, "WARNING: Could not save preferences!\n");
    }
   
    getCurrentAppName (appNameText); /* check for error here ? */

    commandLine = (char*) malloc (appNameText[0] + prefs.command_line[0] + 2);
    if ( commandLine == NULL ) {
       exit(-1);
    }

    /* Rather than rewrite ParseCommandLine method, let's replace  */
    /* any spaces in application name with underscores,            */
    /* so that the app name is only 1 argument                     */   
    for (i = 1; i < 1+appNameText[0]; i++)
        if ( appNameText[i] == ' ' ) appNameText[i] = '_';

    /* Copy app name & full command text to command-line C-string */      
    memcpy (commandLine, appNameText + 1, appNameText[0]);
    commandLine[appNameText[0]] = ' ';
    memcpy (commandLine + appNameText[0] + 1, prefs.command_line + 1, prefs.command_line[0]);
    commandLine[ appNameText[0] + 1 + prefs.command_line[0] ] = '\0';

    /* Parse C-string into argv and argc */
    nargs = ParseCommandLine (commandLine, NULL);
    args = (char **)malloc((nargs+1)*(sizeof *args));
    if ( args == NULL ) {
		exit(-1);
	}
	ParseCommandLine (commandLine, args);
        
	/* Run the main application code */
	SDL_main(nargs, args);
	free (args);
	free (commandLine);
   
   	/* Remove useless stdout.txt and stderr.txt */
   	cleanup_output ();
   	
	/* Exit cleanly, calling atexit() functions */
	exit (0);    

	/* Never reached, but keeps the compiler quiet */
	return (0);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -