Пример #1
0
void mainLoop()
{
	static Uint32 lastEvent = 0;
	SDL_Event evt;
	while( SDL_PollEvent(&evt) )
	{
		lastEvent = SDL_GetTicks();
		if(evt.type == SDL_KEYUP || evt.type == SDL_KEYDOWN)
		{
			if(evt.key.keysym.sym == SDLK_UNDO)
				exit(0);
			processKeyInput(evt.key.keysym.sym, evt.key.state == SDL_PRESSED);
			//printf("Got key %d %d", evt.key.keysym.sym, evt.key.state == SDL_PRESSED);
		}
		// PC mouse events
		if(evt.type == SDL_MOUSEBUTTONUP || evt.type == SDL_MOUSEBUTTONDOWN)
		{
			// TODO: implement PC input
			touchPointers[evt.jbutton.button].pressed = (evt.button.state == SDL_PRESSED);
		}
		if(evt.type == SDL_MOUSEMOTION)
		{
			// TODO: implement PC input
			touchPointers[evt.jbutton.button].x = evt.motion.x;
			touchPointers[evt.jbutton.button].y = evt.motion.y;
		}
		// Android-specific events - accelerometer, multitoush, and on-screen joystick
		if( evt.type == SDL_JOYAXISMOTION )
		{
			if(evt.jaxis.which == 0) // Multitouch and on-screen joysticks
			{
				if(evt.jaxis.axis >= 4)
					touchPointers[evt.jaxis.axis - 4].pressure = evt.jaxis.value;
			}
		}
		if( evt.type == SDL_JOYBUTTONDOWN || evt.type == SDL_JOYBUTTONUP )
		{
			if(evt.jbutton.which == 0) // Multitouch and on-screen joystick
				touchPointers[evt.jbutton.button].pressed = (evt.jbutton.state == SDL_PRESSED);
			//printf("Touch press %d: %d", evt.jbutton.button, evt.jbutton.state);
		}
		if( evt.type == SDL_JOYBALLMOTION )
		{
			if(evt.jball.which == 0) // Multitouch and on-screen joystick
			{
				touchPointers[evt.jball.ball].x = evt.jball.xrel;
				touchPointers[evt.jball.ball].y = evt.jball.yrel;
				//printf("Touch %d: %d %d", evt.jball.ball, evt.jball.xrel, evt.jball.yrel);
			}
		}
	}

	processGui();
	processMouseInput();

	SDL_Flip(SDL_GetVideoSurface());
	SDL_FillRect(SDL_GetVideoSurface(), NULL, 0);

	if( lastEvent + 1000 < SDL_GetTicks() )
		SDL_Delay(150);
}
Пример #2
0
static void keyInputCallback(GuiElement_t * elem, bool pressed, int x, int y)
{
	if( toggleElement(elem, pressed) )
		processKeyInput(key, elem->toggled || SDL_GetKeyState(NULL)[key]);
}
Пример #3
0
void mainLoop(bool noHid)
{
	static Uint32 lastEvent = 0;
	SDL_Event evt;

	for( int i = 0; i < MAX_POINTERS; i++)
	{
		if( touchPointers[i].delayRelease )
			touchPointers[i].pressed = false;
		touchPointers[i].delayRelease = false;
	}

	while( SDL_PollEvent(&evt) )
	{
		lastEvent = SDL_GetTicks();
		if( evt.type == SDL_KEYUP || evt.type == SDL_KEYDOWN )
		{
#ifndef __ANDROID__
			if( (evt.key.keysym.unicode & 0xFF80) == 0 )
				evt.key.keysym.unicode = 0; // That's how Android sends keypresses
#endif
			if( evt.key.keysym.sym == SDLK_UNDO )
			{
				if( evt.type == SDL_KEYDOWN )
				{
					if( sInsideSettings ) // TODO: dialog stack?
						settingsCloseGui();
					else
						exit(0);
				}
			}
			else
			{
				if( sInsideSettings ) // TODO: dialog stack?
					settingsProcessKeyInput(evt.key.keysym.sym, evt.key.keysym.unicode, evt.key.state == SDL_PRESSED);
				else
				{
					if( !processKeyInput(evt.key.keysym.sym, evt.key.keysym.unicode, evt.key.state == SDL_PRESSED) )
					{
						settingsShowGui();
						settingsDefineKeycode(evt.key.keysym.sym, evt.key.keysym.unicode);
					}
					else if( evt.key.keysym.unicode != 0 && evt.key.state == SDL_PRESSED )
						processKeyInput(evt.key.keysym.sym, evt.key.keysym.unicode, false); // Keyboards do not send key-up for international keys
				}
				//printf("Got key %d %d", evt.key.keysym.sym, evt.key.state == SDL_PRESSED);
			}
		}
		// PC mouse events
		if( evt.type == SDL_MOUSEBUTTONUP || evt.type == SDL_MOUSEBUTTONDOWN )
		{
			if( evt.type == SDL_MOUSEBUTTONUP )
				touchPointers[evt.jbutton.button - SDL_BUTTON_LEFT].delayRelease = true; // Unpress the button after one extra loop, so we won't lose quick keypresses
			else
				touchPointers[evt.jbutton.button - SDL_BUTTON_LEFT].pressed = true;
		}
		if( evt.type == SDL_MOUSEMOTION )
		{
			// TODO: implement PC input
			touchPointers[0].x = evt.motion.x;
			touchPointers[0].y = evt.motion.y;
		}
		// Android-specific events - accelerometer, multitoush, and on-screen joystick
		if( evt.type == SDL_JOYAXISMOTION )
		{
			if(evt.jaxis.which == 0) // Multitouch and on-screen joysticks
			{
				if(evt.jaxis.axis >= 4)
					touchPointers[evt.jaxis.axis - 4].pressure = evt.jaxis.value;
			}
		}
		if( evt.type == SDL_JOYBUTTONDOWN || evt.type == SDL_JOYBUTTONUP )
		{
			if(evt.jbutton.which == 0) // Multitouch and on-screen joystick
				touchPointers[evt.jbutton.button].pressed = (evt.jbutton.state == SDL_PRESSED);
			//printf("Touch press %d: %d", evt.jbutton.button, evt.jbutton.state);
		}
		if( evt.type == SDL_JOYBALLMOTION )
		{
			if(evt.jball.which == 0) // Multitouch and on-screen joystick
			{
				touchPointers[evt.jball.ball].x = evt.jball.xrel;
				touchPointers[evt.jball.ball].y = evt.jball.yrel;
				//printf("Touch %d: %d %d", evt.jball.ball, evt.jball.xrel, evt.jball.yrel);
			}
		}
		if( evt.type == SDL_QUIT )
		{
			exit(0);
		}
	}

	SDL_SemTryWait(screenRedrawSemaphore); // Drain the semaphore
	SDL_FillRect(SDL_GetVideoSurface(), NULL, 0);
	processGui();
	if( !noHid )
	{
		processTouchpad(TOUCHPAD_X0, TOUCHPAD_Y0, TOUCHPAD_X1, TOUCHPAD_Y1);
		processMouseInput();
	}

	SDL_Flip(SDL_GetVideoSurface());
	SDL_SemPost(screenRedrawSemaphore);

	if( lastEvent + 1000 < SDL_GetTicks() )
		SDL_Delay(150);
}