Ejemplo n.º 1
0
TEST(keyboard_UT, KeyboardHandlerCallback)
{
	{
		if ( SDL_Init ( SDL_INIT_VIDEO ) < 0 )
		{

			std::cerr << "SDL_Init ( .. ) failed: " << SDL_GetError( ) <<std::endl;
			exit( -1 );
		}

		SDL_Window * window = SDL_CreateWindow( "SDL2 OpenGL"
                                    			, SDL_WINDOWPOS_CENTERED
		                                        , SDL_WINDOWPOS_CENTERED
		                                        , 1920 / 2
		                                        , 1080 / 2
		                                        , SDL_WINDOW_OPENGL      | SDL_WINDOW_BORDERLESS    |
		                                          SDL_WINDOW_SHOWN       | SDL_WINDOW_INPUT_GRABBED |
                		                          SDL_WINDOW_INPUT_FOCUS | SDL_WINDOW_MOUSE_FOCUS );

		SDL_GLContext context = SDL_GL_CreateContext( window );

		SDL_GL_SetSwapInterval( 1 );

		//SDL_MaximizeWindow( window );
		SDL_RaiseWindow( window );
		SDL_SetWindowGrab( window, SDL_TRUE );

		event quit;

		OnKeyboardEventCallback onKeyboardCharacter = [&] ( KeyboardEvent const & xEvent )
		{
			std::cout << "[ Key " << ( xEvent.getKeyUp( ) ? "up " : "down " ) << ": "
				  << std::hex << xEvent.getScanCode( ) << "]" << std::endl;

			if ( xEvent.getScanCode( ) == SDL_SCANCODE_Q )
			{
				std::cerr << "Q pressed. Exiting.. " << std::endl;
				quit.set( );
			}
		};


		Keyboard kb;

		kb.registerCallback( onKeyboardCharacter );
		kb.listen( );

		double accum = 0.0;


		while ( !quit.isSet( ) )
		{
			Stopwatch t;

			t.startMs( );

			SDL_PumpEvents( );

			SDL_GL_SwapWindow( window );

			if ( ( accum += t.stop( ) ) > 3000.0 )
			{
				std::cerr << "Timeout reached. Exiting.." << std::endl;
				break;
			}
		}

		kb.stopListening( );

		SDL_DestroyWindow(window);

		SDL_Quit( );

	}
}
Ejemplo n.º 2
0
TEST(mouse_UT, MouseHandlerCallback)
{
	{
		if ( SDL_Init ( SDL_INIT_VIDEO ) < 0 )
		{

			std::cerr << "SDL_Init ( .. ) failed: " << SDL_GetError( ) <<std::endl;
			exit( -1 );
		}

		SDL_Window * window = SDL_CreateWindow( "SDL2 OpenGL"
                                    			, SDL_WINDOWPOS_CENTERED
		                                        , SDL_WINDOWPOS_CENTERED
		                                        , 1920/2
		                                        , 1080/2
		                                        , SDL_WINDOW_OPENGL      | SDL_WINDOW_BORDERLESS    |
		                                          SDL_WINDOW_SHOWN       | SDL_WINDOW_INPUT_GRABBED |
                		                          SDL_WINDOW_INPUT_FOCUS | SDL_WINDOW_MOUSE_FOCUS );

		SDL_GLContext context = SDL_GL_CreateContext( window );

		SDL_GL_SetSwapInterval( 1 );

		SDL_RaiseWindow( window );
		SDL_SetWindowGrab( window, SDL_TRUE );

		event quit;

		OnMouseEventCallback  onMouseCharacter = [&] ( MouseEventInfo const & xEvent )
		{
			std::ios::fmtflags const f( std::cout.flags( ) );

			std::cout << "[ Mouse: "
				  << xEvent.x
				  << ", "
				  << xEvent.y
				  << " "
				  << std::hex
				  << xEvent.state;

			std::cout.flags( f );

			auto resolveClickStateName = []( eClickState const xState )->const char *
			{

				switch ( xState )
				{
					case eClickState_Up:
						return "Up";

					case eClickState_Down:
						return "Down";

					case eClickState_Continue:
						return "Continue";

					case eClickState_None:
						return "None";

					default:
						return "Invalid";
				}
			};


			std::cout 
				  << "left: "   << ( resolveClickStateName( xEvent.leftState   ) ) << " "
				  << "middle: " << ( resolveClickStateName( xEvent.middleState ) ) << " "
				  << "right: "  << ( resolveClickStateName( xEvent.rightState  ) ) << " "
				  << ( xEvent.wheelHasMovedUp( )      ? ", wheel up "     : "" )
				  << ( xEvent.wheelHasMovedDown( )    ? ", wheel down "   : "" )
				  << " ]"
				  << std::endl;



			if ( xEvent.leftState   == eClickState_Continue &&
			     xEvent.middleState == eClickState_Continue &&
			     xEvent.rightState  == eClickState_Continue )
			{
				std::cout << "All three buttons pressed. Exiting.." << std::endl;
				quit.set( );
			}

		};


		Mouse kb;

		kb.registerCallback(onMouseCharacter);
		kb.listen( );

		double accum = 0.0;

		Stopwatch t;

		while ( !quit.isSet( ) )
		{
			t.startMs( );
			SDL_PumpEvents( );
			SDL_GL_SwapWindow( window );

			if ( ( accum += t.stop( ) ) > 3000.0 )
			{
				break;
			}
		}

		kb.stopListening( );

		SDL_DestroyWindow(window);

		SDL_Quit( );
	}
}
Ejemplo n.º 3
0
			Keyboard( double const xDebounceTicks = 18000000.0 )
				: mM( )
				, mQuit( )
				, mCallbacks( )
				, mInitialized( false )
				, mKeyboardListener( [ & ] ( int64_t const xSleepIntervalNs ) -> uint32_t
		  		{
			                bool pressedLastTime[ SDL_NUM_SCANCODES ];

					for ( uint16_t ii = 0; ii < SDL_NUM_SCANCODES; ++ii )
					{
						pressedLastTime [ ii ] = false;
					}

					double continueDebounceAccum = 0.0;

					Stopwatch t;

					while ( !mQuit.isSet( ) )
					{
						if ( xSleepIntervalNs > 0 )
						{
							sleep::ns( xSleepIntervalNs );
						}

						const Uint8 * state = SDL_GetKeyboardState( NULL );

		        	                for ( uint16_t ii = 0; ii < SDL_NUM_SCANCODES; ++ii )
	        	        	        {
							eKeyState keyState = eKeyState_None;

							auto runCallbacks = [&]()
							{
								mM.run([&]( )
								{
									for ( auto callback : mCallbacks )
									{
										callback( KeyboardEvent( keyState, ii, state ) );
									}
								});
							};

		        	                        if ( state[ ii ] && !pressedLastTime[ ii ] )
		                	                {
								keyState = eKeyState_Down;
								t.startMs( );
			                                }
							else if ( state[ ii ] && pressedLastTime[ ii ] )
							{
								continueDebounceAccum += t.stop( );

								if ( continueDebounceAccum > mDebounceTicks )
								{
									keyState = eKeyState_Continue;
									continueDebounceAccum = 0;
									t.startMs( );
								}
							}
		        	                        else if ( !state[ ii ] && pressedLastTime[ ii ] )
		                	                {
								keyState = eKeyState_Up;
			                                }

							if ( keyState != eKeyState_None )
							{
								runCallbacks( );
							}

			                                pressedLastTime[ ii ] = state[ ii ];
						}
					}
				})
				, mDebounceTicks( xDebounceTicks )
			{
				SISULOG( "In Keyboard Ctor" );
			}
Ejemplo n.º 4
0
TEST(gameController_UT, GameControllerHandlerCallback)
{
	{
		if ( SDL_Init ( SDL_INIT_VIDEO ) < 0 )
		{

			std::cerr << "SDL_Init ( .. ) failed: " << SDL_GetError( ) <<std::endl;
			exit( -1 );
		}

		SDL_Window * window = SDL_CreateWindow( "SDL2 OpenGL"
                                    			, SDL_WINDOWPOS_CENTERED
		                                        , SDL_WINDOWPOS_CENTERED
		                                        , 1920/2
		                                        , 1080/2
		                                        , SDL_WINDOW_OPENGL      | SDL_WINDOW_BORDERLESS    |
		                                          SDL_WINDOW_SHOWN       | SDL_WINDOW_INPUT_GRABBED |
                		                          SDL_WINDOW_INPUT_FOCUS | SDL_WINDOW_MOUSE_FOCUS );

		SDL_GLContext context = SDL_GL_CreateContext( window );

		SDL_GL_SetSwapInterval( 1 );

		SDL_RaiseWindow( window );
		SDL_SetWindowGrab( window, SDL_TRUE );

		event quit;

		OnGameControllerEventCallback  onGameControllerUpdate = [&] ( GameControllerEventInfo const & xEvent )
		{
			std::ios::fmtflags const f( std::cout.flags( ) );

			std::cout << "[ GameController: ";

			std::cout.flags( f );

			std::cout
				  //<< ( xEvent.isLeftClickPressed( )   ? ", left click "   : "" )
				  << " ]"
				  << std::endl;
		};


		GameController gc;

		gc.registerCallback(onGameControllerUpdate);
		gc.listen( );

		Stopwatch t;
		double accum = 0.0;

		while ( !quit.isSet( ) )
		{
			t.startMs( );

			SDL_GameControllerUpdate( );
			SDL_PumpEvents( );
			SDL_GL_SwapWindow( window );

			if ( ( accum += t.stop( ) ) > 3000.0 )
			{
				break;
			}
		}

		gc.stopListening( );

		SDL_DestroyWindow(window);

		SDL_Quit( );

	}
}