bgfx/examples/common/processevents.h

131 lines
2.5 KiB
C
Raw Normal View History

2013-01-19 00:12:04 -05:00
/*
* Copyright 2011-2013 Branimir Karadzic. All rights reserved.
* License: http://www.opensource.org/licenses/BSD-2-Clause
*/
#ifndef __PROCESS_EVENTS_H__
#define __PROCESS_EVENTS_H__
2013-04-25 00:01:11 -04:00
struct MouseState
{
2013-04-25 01:03:04 -04:00
MouseState()
: m_mx(0)
, m_my(0)
{
2013-05-03 12:18:53 -04:00
for (uint32_t ii = 0; ii < entry::MouseButton::Count; ++ii)
{
m_buttons[ii] = entry::MouseButton::None;
}
2013-04-25 01:03:04 -04:00
}
2013-04-25 00:01:11 -04:00
uint32_t m_mx;
uint32_t m_my;
uint8_t m_buttons[entry::MouseButton::Count];
};
inline bool processEvents(uint32_t& _width, uint32_t& _height, uint32_t& _debug, uint32_t& _reset, MouseState* _mouse = NULL)
2013-01-19 00:12:04 -05:00
{
using namespace entry;
2013-02-22 01:05:33 -05:00
bool reset = false;
2013-01-19 15:11:24 -05:00
2013-01-19 00:12:04 -05:00
const Event* ev;
do
{
struct SE { const Event* m_ev; SE() : m_ev(poll() ) {} ~SE() { if (NULL != m_ev) { release(m_ev); } } } scopeEvent;
ev = scopeEvent.m_ev;
if (NULL != ev)
{
switch (ev->m_type)
{
case Event::Exit:
return true;
case Event::Mouse:
2013-04-25 00:01:11 -04:00
if (NULL != _mouse)
2013-01-19 00:12:04 -05:00
{
2013-04-25 00:01:11 -04:00
const MouseEvent* mouse = static_cast<const MouseEvent*>(ev);
if (mouse->m_move)
{
_mouse->m_mx = mouse->m_mx;
_mouse->m_my = mouse->m_my;
}
else
{
_mouse->m_buttons[mouse->m_button] = mouse->m_down;
}
2013-01-19 00:12:04 -05:00
}
break;
case Event::Key:
{
const KeyEvent* key = static_cast<const KeyEvent*>(ev);
if ( (key->m_key == Key::KeyQ && (key->m_modifiers & (Modifier::LeftCtrl|Modifier::RightCtrl) ) )
2013-01-19 00:12:04 -05:00
|| ( (key->m_key == Key::Esc) ) )
{
return true;
}
2013-02-22 01:05:33 -05:00
else if (key->m_down)
2013-01-19 00:12:04 -05:00
{
2013-02-22 01:05:33 -05:00
if (key->m_key == Key::F1)
{
_debug ^= BGFX_DEBUG_STATS;
bgfx::setDebug(_debug);
return false;
}
2013-04-27 18:16:05 -04:00
else if (key->m_key == Key::F7)
{
_reset ^= BGFX_RESET_VSYNC;
reset = true;
}
2013-02-22 01:05:33 -05:00
else if (key->m_key == Key::F8)
{
_reset ^= BGFX_RESET_MSAA_X16;
reset = true;
}
else if (key->m_key == Key::F9)
{
setWindowSize(640, 480);
_width = 640;
_height = 480;
}
else if (key->m_key == Key::F10)
{
setWindowSize(1280, 720);
_width = 1280;
_height = 720;
}
else if (key->m_key == Key::F11)
{
toggleWindowFrame();
}
2013-01-19 00:12:04 -05:00
}
}
break;
case Event::Size:
{
const SizeEvent* size = static_cast<const SizeEvent*>(ev);
_width = size->m_width;
_height = size->m_height;
2013-02-22 01:05:33 -05:00
reset = true;
2013-01-19 00:12:04 -05:00
}
break;
default:
break;
}
}
} while (NULL != ev);
2013-02-22 01:05:33 -05:00
if (reset)
2013-01-19 15:11:24 -05:00
{
2013-02-22 01:05:33 -05:00
bgfx::reset(_width, _height, _reset);
2013-01-19 15:11:24 -05:00
}
2013-01-19 00:12:04 -05:00
return false;
}
#endif // __PROCESS_EVENTS_H__