Ejemplo n.º 1
0
int main(int argc, char **argv) {
   // To know if there was an error or not
   int errcode = 0;

   // Scan all arguments
   int show_help = 0;
   int show_ver = 0;
   const char *filename = NULL;

   int scan_ok = 1;
   int err_manyfiles = 0;

   for (int curr_arg = 1; curr_arg < argc; curr_arg++) {
      // Get pointer to argument, to make our lives easier
      const char *arg = argv[curr_arg];

      // If it's an option, parse it
      if (scan_ok && arg[0] == '-') {
         // Stop parsing options?
         if (!strcmp(arg, "--"))
            scan_ok = 0;

         // Show help or version?
         else if (!strcmp(arg, "-h") || !strcmp(arg, "--help"))
            show_help = 1;
         else if (!strcmp(arg, "-v") || !strcmp(arg, "--version"))
            show_ver = 1;

         // Unknown argument
         else {
            fprintf(stderr, "Error: unknown option \"%s\"\n", arg);
            errcode = 1;
         }
      }

      // Filename?
      else if (filename == NULL)
         filename = arg;

      // Too many files specified?
      else
         err_manyfiles = 1;
   }

   // Look for error conditions
   if (!show_help && !show_ver) {
      if (filename == NULL) {
         errcode = 1;
         fprintf(stderr, "Error: batch filename missing\n");
      } else if (err_manyfiles) {
         errcode = 1;
         fprintf(stderr, "Error: too many filenames specified\n");
      }
   }

   // If there was an error then quit
   if (errcode)
      return EXIT_FAILURE;

   // Show tool version?
   if (show_ver) {
      puts("0.8");
      return EXIT_SUCCESS;
   }

   // Show tool usage?
   if (show_help) {
      printf("Usage:\n"
             "  %s <batchfile>\n"
             "\n"
             "Options:\n"
             "  -h or --help ...... Show this help\n"
             "  -v or --version ... Show tool version\n",
             argv[0]);
      return EXIT_SUCCESS;
   }

   // Parse batch file
   errcode = process_batch(filename);

   // If there was an error, show a message
   if (errcode) {
      // Determine message to show
      const char *msg;
      switch(errcode) {
         case ERR_OPENBATCH: msg = "can't open batch file"; break;
         case ERR_READBATCH: msg = "can't read from batch file"; break;
         case ERR_NOMEMORY: msg = "ran out of memory"; break;
         case ERR_PARSE: msg = "unable to process batch file"; break;
         default: msg = "unknown error"; break;
      }

      // Show message on screen
      fprintf(stderr, "Error: %s\n", msg);
   }

   // Quit program
   reset_events();
   return errcode ? EXIT_FAILURE : EXIT_SUCCESS;
}
Ejemplo n.º 2
0
bool handle_events( void )
{
	SDL_Event _event;

	reset_events();

	//DebugPrintf("processing events\n");

	while( SDL_PollEvent( &_event ) )
	{
		switch( _event.type )
		{


// previously mouse wheel was a button code
// now it's an x/y axis since it supports mouse wheel balls
#if SDL_VERSION_ATLEAST(2,0,0)
		case SDL_MOUSEWHEEL:
			app_mouse_wheel( &_event );
			break;
#endif


// this event name has changed
#if SDL_VERSION_ATLEAST(2,0,0)
// TODO - we should call something like app_window
//        which then delegates to app_active in correct case
		case SDL_WINDOWEVENT:
			app_active( &_event );
			break;
#else
		case SDL_ACTIVEEVENT:
			app_active( &_event.active );
			break;
#endif


// these have been moved into the window event above
#if !SDL_VERSION_ATLEAST(2,0,0)
		case SDL_VIDEORESIZE:
			app_resize( &_event.resize );
			break;

		case SDL_VIDEOEXPOSE: // need redraw
			render_flip(&render_info);
			break;
#endif

// the newer sdl now uses a union instead of sub structures
#if SDL_VERSION_ATLEAST(2,0,0)
		case SDL_KEYDOWN:
		case SDL_KEYUP:
			app_keyboard( &_event );
			break;

		case SDL_MOUSEBUTTONDOWN:
		case SDL_MOUSEBUTTONUP:
			app_mouse_button( &_event );
			break;

		case SDL_MOUSEMOTION:
			app_mouse_motion( &_event );
			break;

		case SDL_JOYAXISMOTION:
			app_joy_axis( &_event );
			break;

		case SDL_JOYBALLMOTION:
			app_joy_ball( &_event );
			break;

		case SDL_JOYBUTTONDOWN:
		case SDL_JOYBUTTONUP:
			app_joy_button( &_event );
			break;

		case SDL_JOYHATMOTION:
			app_joy_hat( &_event );
			break;

#else
		case SDL_KEYDOWN:
		case SDL_KEYUP:
			app_keyboard( &_event.key );
			break;

		case SDL_MOUSEBUTTONDOWN:
		case SDL_MOUSEBUTTONUP:
			app_mouse_button( &_event.button );
			break;

		case SDL_MOUSEMOTION:
			app_mouse_motion( &_event.motion );
			break;

		case SDL_JOYAXISMOTION:
			app_joy_axis( &_event.jaxis );
			break;

		case SDL_JOYBALLMOTION:
			app_joy_ball( &_event.jball );
			break;

		case SDL_JOYBUTTONDOWN:
		case SDL_JOYBUTTONUP:
			app_joy_button( &_event.jbutton );
			break;

		case SDL_JOYHATMOTION:
			app_joy_hat( &_event.jhat );
			break;

#endif

		case SDL_QUIT:
			app_quit();
			break;

		// platform specific _event type
		// must be enabled using SDL_EventState(SDL_SYSWMEVENT, SDL_ENABLE)
		case SDL_SYSWMEVENT:
			DebugPrintf("recived a platform specific _event type\n");
			break;

		// to avoid threading issues timers will add an event to the queue
		// so that we can call the callbacks from the same thread
		case SDL_USEREVENT:
			{
				void (*p) (void*) = _event.user.data1; // callback
				p(_event.user.data2); // callback( data )
			}
			break;

// TODO
		default:
			printf("Unknown event type: %d\n",_event.type);
			break;
		}
	}

	//DebugPrintf("DONE processing events\n");

	if ( quitting )
	{
		DebugPrintf("about to CleanUpAndPostQuit ( from WindowProc )\n");
		quitting = false;
		CleanUpAndPostQuit();
	}

	return true;
}