bool OpenGLGame::update()
{
	if ( !isActive() )						// 程序激活的么?
	{
		return true;
	}

	if ( isKeyPressed( VK_ESCAPE ) )				// ESC 按下了么?
	{
		return false;				// ESC 发出退出信号
	}

	// 不是退出的时候,刷新屏幕
	drawGLScene();				// 绘制场景
	::SwapBuffers( m_hDC );			// 交换缓存 (双缓存)

	processKeyEvent();

	if ( isKeyPressed( VK_F1 ) )					// F1键按下了么?
	{
		onKeyReleased( VK_F1 );			// 若是,使对应的Key数组中的值为 FALSE
		killGLWindow();					// 销毁当前的窗口
		m_bFullScreen = !m_bFullScreen; // 切换 全屏 / 窗口 模式

		// 重建 OpenGL 窗口
		if ( !createGLWindow() )
		{
			return false;				// 如果窗口未能创建,程序退出
		}
	}

	return true;
}
Example #2
0
void InputListener::onEvent(const sf::Event& event)
{
	switch (event.type)
	{
		/* Window */
		case sf::Event::LostFocus:               onFocusLost();                                   break;
		case sf::Event::GainedFocus:             onFocusGained();                                 break;
		case sf::Event::Closed:                  onWindowClosed();                                break;
		case sf::Event::Resized:                 onWindowResized(event.size);                     break;
			
		/* Keyboard */
		case sf::Event::TextEntered:             onTextEntered(event.text);                       break;
		case sf::Event::KeyPressed:              onKeyPressed(event.key);                         break;
		case sf::Event::KeyReleased:             onKeyReleased(event.key);                        break;
			
		/* Mouse */
		case sf::Event::MouseEntered:            onMouseEntered();                                break;
		case sf::Event::MouseLeft:               onMouseLeft();                                   break;
		case sf::Event::MouseMoved:              onMouseMoved(event.mouseMove);                   break;
		case sf::Event::MouseWheelMoved:         onMouseWheelMoved(event.mouseWheel);             break;
		case sf::Event::MouseButtonPressed:      onMouseButtonPressed(event.mouseButton);         break;
		case sf::Event::MouseButtonReleased:     onMouseButtonReleased(event.mouseButton);        break;
			
		/* Joystick */
		case sf::Event::JoystickConnected:       onJoystickConnected(event.joystickConnect);      break;
		case sf::Event::JoystickDisconnected:    onJoystickDisconnected(event.joystickConnect);   break;
		case sf::Event::JoystickButtonPressed:   onJoystickButtonPressed(event.joystickButton);   break;
		case sf::Event::JoystickButtonReleased:  onJoystickButtonReleased(event.joystickButton);  break;
		case sf::Event::JoystickMoved:           onJoystickMoved(event.joystickMove);             break;
			
		default:
			Logger::log("Warning", "Unknow event type: %d", event.type);
			break;
	}
}
Example #3
0
void SFMLInputSource::dispatchOne(sf::Event const& event) {
    switch (event.type) {
    case sf::Event::KeyPressed:
        onKeyPressed(event.key);
        break;

    case sf::Event::KeyReleased:
        onKeyReleased(event.key);
        break;

    case sf::Event::MouseButtonPressed:
        onMouseButtonPressed(event.mouseButton);
        break;

    case sf::Event::MouseButtonReleased:
        onMouseButtonReleased(event.mouseButton);
        break;

    case sf::Event::MouseMoved:
        onMouseMoved(event.mouseMove);
        break;

    case sf::Event::MouseWheelMoved:
        onMouseWheelMoved(event.mouseWheel);
        break;

    default:
        break;
    }
}
Example #4
0
void Event::eventProc(SDL_Event* e)
{

    switch (e->type) {
    case SDL_WINDOWEVENT:
        break;
    case SDL_KEYDOWN:   //key pressed
        onKeyPressed(e->key.keysym);
        break;
    case SDL_KEYUP: //key released
        onKeyReleased(e->key.keysym);
        break;
    case SDL_MOUSEMOTION:
        onMouseMove(e->motion);
        break;
    case SDL_MOUSEBUTTONDOWN:
        if (e->button.button == SDL_BUTTON_LEFT)
            onLMouseButtonPressed(e->button);
        else if (e->button.button == SDL_BUTTON_RIGHT)
            onRMouseButtonPressed(e->button);
        else if (e->button.button == SDL_BUTTON_MIDDLE)
            onMMouseButtonPressed(e->button);
        else
            ;
        break;
    case SDL_MOUSEBUTTONUP:
        if (e->button.button == SDL_BUTTON_LEFT)
            onLMouseButtonReleased(e->button);
        else if (e->button.button == SDL_BUTTON_RIGHT)
            onRMouseButtonReleased(e->button);
        else if (e->button.button == SDL_BUTTON_MIDDLE)
            onMMouseButtonReleased(e->button);
        else
            ;
        break;
    case SDL_MOUSEWHEEL:
        onMouseWheelMove(e->wheel);
        break;
    case SDL_QUIT:
        onExit();
        break;
    default:
        onUser();
    }
}
bool EventListenerKeyboard::init()
{
    auto listener = [this](Event* event){
        auto keyboardEvent = static_cast<EventKeyboard*>(event);
        if (keyboardEvent->_isPressed)
        {
            if (onKeyPressed != nullptr)
                onKeyPressed(keyboardEvent->_keyCode, event);
        }
        else
        {
            if (onKeyReleased != nullptr)
                onKeyReleased(keyboardEvent->_keyCode, event);
        }
    };
    
    if (EventListener::init(Type::KEYBOARD, LISTENER_ID, listener))
    {
        return true;
    }
    
    return false;
}
bool OpenGLGame::processKeyEvent()
{
	//light
	if ( isKeyPressed( 'L' ) || isKeyPressed( 'l' ) )
	{
		onKeyReleased( 'L' );
		onKeyReleased( 'l' );

		m_bLightOn = !m_bLightOn;

		if ( m_bLightOn )
		{
			glEnable( GL_LIGHTING ); // 启用光源
		}
		else
		{
			glDisable( GL_LIGHTING ); // 禁用光源
		}
	}

	//filter
	if ( isKeyPressed( 'F' ) || isKeyPressed( 'f' ) )
	{
		onKeyReleased( 'F' );
		onKeyReleased( 'f' );

		++m_nFilterIndex;

		if ( m_nFilterIndex > 2 )
		{
			m_nFilterIndex = 0;
		}
	}


	if ( isKeyPressed( VK_PRIOR ) ) // PageUp按下了?
	{
		onKeyReleased( VK_PRIOR );
		z -= 0.2f; // 若按下,将木箱移向屏幕内部
	}

	if ( isKeyPressed( VK_NEXT ) ) // PageDown按下了么
	{
		onKeyReleased( VK_NEXT );
		z += 0.2f; // 若按下的话,将木箱移向观察者
	}

	if ( isKeyPressed( VK_UP ) ) // Up方向键按下了么?
	{
		onKeyReleased( VK_UP );
		xspeed -= 0.01f; // 若是,减少xspeed
	}

	if ( isKeyPressed( VK_DOWN ) ) // Down方向键按下了么?
	{
		onKeyReleased( VK_DOWN );
		xspeed += 0.01f; // 若是,增加xspeed
	}

	if ( isKeyPressed( VK_RIGHT ) ) // Right方向键按下了么?
	{
		onKeyReleased( VK_RIGHT );
		yspeed -= 0.01f; // 若是,增加yspeed
	}

	if ( isKeyPressed( VK_LEFT ) ) // Left方向键按下了么?
	{
		onKeyReleased( VK_LEFT );
		yspeed += 0.01f; // 若是, 减少yspeed
	}

	return true;
}
//
// CGLib is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with CGLib; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
//
// Copyright 2007 Carlos Martinho

#include "KeyboardEventNotifier.h"

namespace cg {

	SINGLETON_IMPLEMENTATION(KeyboardEventNotifier)

    void KeyboardEventNotifier::handleKeyPressed(unsigned char key) {
		FOR_EACH_LISTENER(onKeyPressed(key))
    }
    void KeyboardEventNotifier::handleKeyReleased(unsigned char key) {
		FOR_EACH_LISTENER(onKeyReleased(key))
    }
    void KeyboardEventNotifier::handleSpecialKeyPressed(int key) {
		FOR_EACH_LISTENER(onSpecialKeyPressed(key))
    }
    void KeyboardEventNotifier::handleSpecialKeyReleased(int key) {
		FOR_EACH_LISTENER(onSpecialKeyReleased(key))
    }
}