bgfx/examples/common/entry/entry_p.h

268 lines
5.4 KiB
C
Raw Normal View History

2013-01-18 10:38:13 +04:00
/*
2015-01-02 02:04:46 +03:00
* Copyright 2011-2015 Branimir Karadzic. All rights reserved.
2013-01-18 10:38:13 +04:00
* License: http://www.opensource.org/licenses/BSD-2-Clause
*/
#ifndef ENTRY_PRIVATE_H_HEADER_GUARD
#define ENTRY_PRIVATE_H_HEADER_GUARD
2013-01-18 10:38:13 +04:00
2015-02-08 21:25:53 +03:00
#define TINYSTL_ALLOCATOR entry::TinyStlAllocator
#include <bx/spscqueue.h>
#include "entry.h"
2014-11-10 20:08:26 +03:00
#include <string.h> // memcpy
2013-01-18 10:38:13 +04:00
2014-03-30 06:58:24 +04:00
#ifndef ENTRY_CONFIG_USE_SDL
2014-01-31 08:31:49 +04:00
# define ENTRY_CONFIG_USE_SDL 0
2014-03-30 06:58:24 +04:00
#endif // ENTRY_CONFIG_USE_SDL
#if !ENTRY_CONFIG_USE_SDL && \
!defined(ENTRY_CONFIG_USE_NATIVE)
2014-01-31 08:31:49 +04:00
# define ENTRY_CONFIG_USE_NATIVE 1
2014-03-30 06:58:24 +04:00
#else
# define ENTRY_CONFIG_USE_NATIVE 0
2014-01-31 08:31:49 +04:00
#endif // ...
2013-08-15 08:08:46 +04:00
2014-09-19 09:32:33 +04:00
#ifndef ENTRY_CONFIG_MAX_WINDOWS
# define ENTRY_CONFIG_MAX_WINDOWS 8
#endif // ENTRY_CONFIG_MAX_WINDOWS
2014-12-16 07:58:54 +03:00
#ifndef ENTRY_CONFIG_MAX_GAMEPADS
# define ENTRY_CONFIG_MAX_GAMEPADS 4
#endif // ENTRY_CONFIG_MAX_GAMEPADS
2013-08-15 08:08:46 +04:00
#if !defined(ENTRY_DEFAULT_WIDTH) && !defined(ENTRY_DEFAULT_HEIGHT)
# define ENTRY_DEFAULT_WIDTH 1280
# define ENTRY_DEFAULT_HEIGHT 720
#elif !defined(ENTRY_DEFAULT_WIDTH) || !defined(ENTRY_DEFAULT_HEIGHT)
# error "Both ENTRY_DEFAULT_WIDTH and ENTRY_DEFAULT_HEIGHT must be defined."
#endif // ENTRY_DEFAULT_WIDTH
#ifndef ENTRY_CONFIG_ALLOCATOR
# define ENTRY_CONFIG_ALLOCATOR bx::CrtAllocator
#endif // ENTRY_CONFIG_ALLOCATOR
2014-09-19 09:32:33 +04:00
#define ENTRY_IMPLEMENT_EVENT(_class, _type) \
_class(WindowHandle _handle) : Event(_type, _handle) {}
2013-01-18 10:38:13 +04:00
namespace entry
{
2015-02-08 21:25:53 +03:00
struct TinyStlAllocator
{
static void* static_allocate(size_t _bytes);
static void static_deallocate(void* _ptr, size_t /*_bytes*/);
};
int main(int _argc, char** _argv);
struct Event
{
enum Enum
{
2014-12-16 07:58:54 +03:00
Axis,
Char,
Exit,
2014-12-18 08:07:40 +03:00
Gamepad,
Key,
Mouse,
Size,
2014-09-19 09:32:33 +04:00
Window,
};
2014-09-19 09:32:33 +04:00
Event(Enum _type)
: m_type(_type)
{
m_handle.idx = UINT16_MAX;
}
Event(Enum _type, WindowHandle _handle)
: m_type(_type)
, m_handle(_handle)
{
}
Event::Enum m_type;
2014-09-19 09:32:33 +04:00
WindowHandle m_handle;
};
2014-12-16 07:58:54 +03:00
struct AxisEvent : public Event
{
2014-12-16 07:58:54 +03:00
ENTRY_IMPLEMENT_EVENT(AxisEvent, Event::Axis);
2014-09-19 09:32:33 +04:00
2014-12-16 07:58:54 +03:00
GamepadAxis::Enum m_axis;
int32_t m_value;
GamepadHandle m_gamepad;
};
struct CharEvent : public Event
{
ENTRY_IMPLEMENT_EVENT(CharEvent, Event::Char);
uint8_t m_len;
uint8_t m_char[4];
};
2014-12-18 08:07:40 +03:00
struct GamepadEvent : public Event
{
2014-12-18 08:07:40 +03:00
ENTRY_IMPLEMENT_EVENT(GamepadEvent, Event::Gamepad);
2014-09-19 09:32:33 +04:00
2014-12-18 08:07:40 +03:00
GamepadHandle m_gamepad;
bool m_connected;
};
2014-12-16 07:58:54 +03:00
struct KeyEvent : public Event
{
ENTRY_IMPLEMENT_EVENT(KeyEvent, Event::Key);
Key::Enum m_key;
uint8_t m_modifiers;
bool m_down;
};
2014-12-18 08:07:40 +03:00
struct MouseEvent : public Event
{
ENTRY_IMPLEMENT_EVENT(MouseEvent, Event::Mouse);
int32_t m_mx;
int32_t m_my;
int32_t m_mz;
MouseButton::Enum m_button;
bool m_down;
bool m_move;
};
struct SizeEvent : public Event
{
2014-09-19 09:32:33 +04:00
ENTRY_IMPLEMENT_EVENT(SizeEvent, Event::Size);
uint32_t m_width;
uint32_t m_height;
};
2014-09-19 09:32:33 +04:00
struct WindowEvent : public Event
{
ENTRY_IMPLEMENT_EVENT(WindowEvent, Event::Window);
void* m_nwh;
};
const Event* poll();
2014-09-23 06:34:10 +04:00
const Event* poll(WindowHandle _handle);
void release(const Event* _event);
class EventQueue
{
public:
2014-12-16 07:58:54 +03:00
void postAxisEvent(WindowHandle _handle, GamepadHandle _gamepad, GamepadAxis::Enum _axis, int32_t _value)
{
AxisEvent* ev = new AxisEvent(_handle);
ev->m_gamepad = _gamepad;
ev->m_axis = _axis;
ev->m_value = _value;
m_queue.push(ev);
}
void postCharEvent(WindowHandle _handle, uint8_t _len, const uint8_t _char[4])
{
CharEvent* ev = new CharEvent(_handle);
ev->m_len = _len;
memcpy(ev->m_char, _char, 4);
m_queue.push(ev);
}
void postExitEvent()
{
2014-09-19 09:32:33 +04:00
Event* ev = new Event(Event::Exit);
m_queue.push(ev);
}
2014-12-18 08:07:40 +03:00
void postGamepadEvent(WindowHandle _handle, GamepadHandle _gamepad, bool _connected)
{
GamepadEvent* ev = new GamepadEvent(_handle);
ev->m_gamepad = _gamepad;
ev->m_connected = _connected;
m_queue.push(ev);
}
2014-09-19 09:32:33 +04:00
void postKeyEvent(WindowHandle _handle, Key::Enum _key, uint8_t _modifiers, bool _down)
{
2014-09-19 09:32:33 +04:00
KeyEvent* ev = new KeyEvent(_handle);
ev->m_key = _key;
ev->m_modifiers = _modifiers;
2014-09-19 09:32:33 +04:00
ev->m_down = _down;
m_queue.push(ev);
}
2014-09-19 09:32:33 +04:00
void postMouseEvent(WindowHandle _handle, int32_t _mx, int32_t _my, int32_t _mz)
2013-01-18 11:22:38 +04:00
{
2014-09-19 09:32:33 +04:00
MouseEvent* ev = new MouseEvent(_handle);
ev->m_mx = _mx;
ev->m_my = _my;
ev->m_mz = _mz;
2013-01-18 11:22:38 +04:00
ev->m_button = MouseButton::None;
2014-09-19 09:32:33 +04:00
ev->m_down = false;
ev->m_move = true;
2013-01-18 11:22:38 +04:00
m_queue.push(ev);
}
2014-09-19 09:32:33 +04:00
void postMouseEvent(WindowHandle _handle, int32_t _mx, int32_t _my, int32_t _mz, MouseButton::Enum _button, bool _down)
{
2014-09-19 09:32:33 +04:00
MouseEvent* ev = new MouseEvent(_handle);
ev->m_mx = _mx;
ev->m_my = _my;
ev->m_mz = _mz;
ev->m_button = _button;
2014-09-19 09:32:33 +04:00
ev->m_down = _down;
ev->m_move = false;
m_queue.push(ev);
}
2014-09-19 09:32:33 +04:00
void postSizeEvent(WindowHandle _handle, uint32_t _width, uint32_t _height)
{
2014-09-19 09:32:33 +04:00
SizeEvent* ev = new SizeEvent(_handle);
ev->m_width = _width;
ev->m_height = _height;
m_queue.push(ev);
}
2014-09-19 09:32:33 +04:00
void postWindowEvent(WindowHandle _handle, void* _nwh = NULL)
{
WindowEvent* ev = new WindowEvent(_handle);
ev->m_nwh = _nwh;
m_queue.push(ev);
}
const Event* poll()
{
return m_queue.pop();
}
2014-09-23 06:34:10 +04:00
const Event* poll(WindowHandle _handle)
{
if (isValid(_handle) )
{
Event* ev = m_queue.peek();
if (NULL == ev
|| ev->m_handle.idx != _handle.idx)
{
return NULL;
}
}
return poll();
}
void release(const Event* _event) const
{
delete _event;
}
private:
bx::SpScUnboundedQueue<Event> m_queue;
};
2013-01-18 10:38:13 +04:00
} // namespace entry
#endif // ENTRY_PRIVATE_H_HEADER_GUARD