if (ticks > last_repaint + 100) { gwin->paint_dirty(); last_repaint = ticks; }
static void Handle_event(SDL_Event& event);#ifdef XWINstatic Display *display = 0;static int xfd = -1;			// X connection #./* *	Here's a somewhat better way to delay in X: */inline void X_Delay	(	)	{	fd_set rfds;	struct timeval timer;	timer.tv_sec = 0;	timer.tv_usec = 20000;		// Try 1/50 second.	FD_ZERO(&rfds);	FD_SET(xfd, &rfds);					// Wait for timeout or event.	select(xfd + 1, &rfds, 0, 0, &timer);	}#endif/* *	Initialize and create main window. */static void Init	(	)	{	if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|						SDL_INIT_AUDIO) < 0)		{		cerr << "Unable to initialize SDL: " << SDL_GetError() << '\n';		exit(-1);		}	atexit(SDL_Quit);	audio.Init(9615*2,2);	SDL_SysWMinfo info;		// Get system info.	SDL_VERSION(&info.version);#ifdef XWIN	SDL_GetWMInfo(&info);	display = info.info.x11.display;	xfd = ConnectionNumber(display);	int screen_num = DefaultScreen(display);	unsigned int display_width = DisplayWidth(display, screen_num);	unsigned int display_height = DisplayHeight(display, screen_num);					// Make window 1/2 size of screen.	int w = display_width/2, h = display_height/2;	if (w < 500)		w = 500;	if (h < 400)		h = 400;	gwin = new Game_window(w, h);#else	gwin = new Game_window(640, 480);#endif	}/* *	Play game. */static int Play()	{	Handle_events(&quitting_time);	delete gwin;	return (0);	}/* *	Delay between animations. */inline void Delay	(	)	{#ifdef XWIN	X_Delay();#else					/* May use this for Linux too. */	SDL_Delay(20);			// Try 1/50 second.#endif	}/* *	Handle events until a flag is set. */static void Handle_events	(	unsigned char *stop	)	{	/*	 *	Main event loop.	 */	while (!*stop)		{		Delay();		// Wait a fraction of a second.#ifdef MOUSE		mouse->hide();		// Turn off mouse.#endif		SDL_Event event;		while (!*stop && SDL_PollEvent(&event))			Handle_event(event);					// Get current time.		unsigned long ticks = SDL_GetTicks();					// Animate unless modal or dormant.		if (gwin->have_focus() && gwin->get_mode() == 							Game_window::normal)			gwin->get_tqueue()->activate(ticks);#ifdef MOUSE		mouse->show();		// Re-display mouse.#endif		gwin->show();		// Blit to screen if necessary.		}	}/* *	Handle an event.  This should work for all platforms. */static void Handle_event	(	SDL_Event& event	)	{	static int dragging = 0;	// Object or gump being moved.	static int dragged = 0;		// Flag for when obj. moved.					// For detecting double-clicks.	static unsigned long last_b1_click = 0;	unsigned int mask;	char keybuf[10];//cout << "Event " << (int) event.type << " received\n";	switch (event.type)		{	case SDL_MOUSEBUTTONDOWN:		gwin->end_intro();		if (gwin->get_mode() != Game_window::normal &&		    gwin->get_mode() != Game_window::gump)			break;		if (event.button.button == 1)			{#if DEBUGcout << "Mouse down at (" << event.button.x << ", " <<	event.button.y << ")\n";#endif			dragging = gwin->start_dragging(event.button.x,							event.button.y);			dragged = 0;			}					// Move sprite toward mouse					//  when right button pressed.		if (event.button.button == 3 && 		    gwin->get_mode() == Game_window::normal)			gwin->start_actor(event.button.x, event.button.y);		break;	case SDL_MOUSEBUTTONUP:		if (event.button.button == 3)			{			if (gwin->get_mode() != Game_window::normal)				break;			gwin->stop_actor();			}		else if (event.button.button == 1)			{			unsigned long curtime = SDL_GetTicks();					// Last click within .5 secs?			if (curtime - last_b1_click < 500)				{				dragging = 0;				gwin->double_clicked(event.button.x, 							event.button.y);				if (gwin->get_mode() == Game_window::gump)					mouse->set_shape(Mouse::hand);				break;				}			last_b1_click = curtime;			if (dragging)				gwin->drop_dragged(event.button.x, 						event.button.y, dragged);			if (!dragged)					// Identify item(s) clicked on.				gwin->show_items(event.button.x, 							event.button.y);			dragging = 0;			}		break;	case SDL_MOUSEMOTION:		{#ifdef MOUSE		mouse->move(event.motion.x, event.motion.y);		if (gwin->get_mode() == Game_window::normal)			{			int ax, ay;	// Get Avatar screen location.			gwin->get_shape_location(gwin->get_main_actor(), 								ax, ay);			int dy = ay - event.motion.y, dx = event.motion.x - ax;			Direction dir = Get_direction(dy, dx);			int dist = dy*dy + dx*dx;			if (dist < 48*48)				mouse->set_short_arrow(dir);			else if (dist < 180*180)				mouse->set_medium_arrow(dir);			else				mouse->set_long_arrow(dir);			}		gwin->set_painted();	// We'll need to blit.#endif		if (gwin->get_mode() != Game_window::normal &&		    gwin->get_mode() != Game_window::gump)			break;					// Dragging with left button?		if (event.motion.state & SDL_BUTTON(1))			{			gwin->drag(event.motion.x, event.motion.y);			dragged = 1;			}					// Dragging with right?		if (event.motion.state & SDL_BUTTON(3))			gwin->start_actor(event.motion.x, event.motion.y);		break;		}	case SDL_ACTIVEEVENT:#ifdef MOUSE		if (event.active.state & SDL_APPMOUSEFOCUS)			{			if (event.active.gain)				{				int x, y;				SDL_GetMouseState(&x, &y);				mouse->set_location(x, y);				}			}#endif		if (event.active.state & SDL_APPINPUTFOCUS)			{			if (event.active.gain)				gwin->get_focus();			else				gwin->lose_focus();			}		if (event.active.state & SDL_APPACTIVE)					// Became active.			if (event.active.gain)				gwin->init_actors();		break;#if 0	case ConfigureNotify:		// Resize.		gwin->resized(event.xconfigure.window,			event.xconfigure.width, event.xconfigure.height);		break;#endif	case SDL_QUIT:		quitting_time = 1;		break;	case SDL_KEYDOWN:			// Keystroke.		Handle_keystroke(event.key.keysym.sym,			event.key.keysym.mod & KMOD_SHIFT);		break;		}	}