bgfx/examples/common/entry/entry.cpp

490 lines
11 KiB
C++
Raw Normal View History

/*
2015-01-01 18:04:46 -05:00
* Copyright 2011-2015 Branimir Karadzic. All rights reserved.
* License: http://www.opensource.org/licenses/BSD-2-Clause
*/
#include <bgfx.h>
#include <bx/string.h>
2014-05-03 18:18:28 -04:00
#include <bx/readerwriter.h>
2013-10-24 01:48:16 -04:00
#include <time.h>
#include "entry_p.h"
#include "cmd.h"
#include "input.h"
2014-06-01 15:01:50 -04:00
extern "C" int _main_(int _argc, char** _argv);
namespace entry
{
static uint32_t s_debug = BGFX_DEBUG_NONE;
static uint32_t s_reset = BGFX_RESET_NONE;
static bool s_exit = false;
2014-05-03 18:18:28 -04:00
static bx::FileReaderI* s_fileReader = NULL;
static bx::FileWriterI* s_fileWriter = NULL;
extern bx::ReallocatorI* getDefaultAllocator();
static bx::ReallocatorI* s_allocator = getDefaultAllocator();
#if ENTRY_CONFIG_IMPLEMENT_DEFAULT_ALLOCATOR
bx::ReallocatorI* getDefaultAllocator()
{
static bx::CrtAllocator s_allocator;
return &s_allocator;
}
#endif // ENTRY_CONFIG_IMPLEMENT_DEFAULT_ALLOCATOR
bool setOrToggle(uint32_t& _flags, const char* _name, uint32_t _bit, int _first, int _argc, char const* const* _argv)
{
if (0 == strcmp(_argv[_first], _name) )
{
int arg = _first+1;
if (_argc > arg)
{
_flags &= ~_bit;
_flags |= bx::toBool(_argv[arg]) ? _bit : 0;
}
else
{
_flags ^= _bit;
}
return true;
}
return false;
}
void cmd(const void* _userData)
{
cmdExec( (const char*)_userData);
}
int cmdMouseLock(CmdContext* /*_context*/, void* /*_userData*/, int _argc, char const* const* _argv)
{
if (_argc > 1)
{
inputSetMouseLock(_argc > 1 ? bx::toBool(_argv[1]) : !inputIsMouseLocked() );
return 0;
}
return 1;
}
int cmdGraphics(CmdContext* /*_context*/, void* /*_userData*/, int _argc, char const* const* _argv)
{
if (_argc > 1)
{
2015-01-21 23:39:42 -05:00
if (setOrToggle(s_reset, "vsync", BGFX_RESET_VSYNC, 1, _argc, _argv)
|| setOrToggle(s_reset, "maxaniso", BGFX_RESET_MAXANISOTROPY, 1, _argc, _argv)
|| setOrToggle(s_reset, "hmd", BGFX_RESET_HMD, 1, _argc, _argv)
|| setOrToggle(s_reset, "hmddbg", BGFX_RESET_HMD_DEBUG, 1, _argc, _argv)
|| setOrToggle(s_reset, "hmdrecenter", BGFX_RESET_HMD_RECENTER, 1, _argc, _argv)
|| setOrToggle(s_reset, "msaa", BGFX_RESET_MSAA_X16, 1, _argc, _argv) )
{
return 0;
}
2013-08-09 01:18:19 -04:00
else if (setOrToggle(s_debug, "stats", BGFX_DEBUG_STATS, 1, _argc, _argv)
|| setOrToggle(s_debug, "ifh", BGFX_DEBUG_IFH, 1, _argc, _argv)
|| setOrToggle(s_debug, "text", BGFX_DEBUG_TEXT, 1, _argc, _argv)
|| setOrToggle(s_debug, "wireframe", BGFX_DEBUG_WIREFRAME, 1, _argc, _argv) )
{
bgfx::setDebug(s_debug);
return 0;
}
2013-10-24 01:48:16 -04:00
else if (0 == strcmp(_argv[1], "screenshot") )
{
if (_argc > 2)
{
bgfx::saveScreenShot(_argv[2]);
}
else
{
time_t tt;
time(&tt);
char filePath[256];
bx::snprintf(filePath, sizeof(filePath), "temp/screenshot-%d", tt);
bgfx::saveScreenShot(filePath);
}
return 0;
}
}
return 1;
}
int cmdExit(CmdContext* /*_context*/, void* /*_userData*/, int /*_argc*/, char const* const* /*_argv*/)
{
s_exit = true;
return 0;
}
2015-01-08 01:36:36 -05:00
static const InputBinding s_bindings[] =
{
2014-12-15 23:58:54 -05:00
{ entry::Key::KeyQ, entry::Modifier::LeftCtrl, 1, cmd, "exit" },
{ entry::Key::F1, entry::Modifier::None, 1, cmd, "graphics stats" },
{ entry::Key::GamepadStart, entry::Modifier::None, 1, cmd, "graphics stats" },
{ entry::Key::F1, entry::Modifier::LeftShift, 1, cmd, "graphics stats 0\ngraphics text 0" },
{ entry::Key::F3, entry::Modifier::None, 1, cmd, "graphics wireframe" },
{ entry::Key::F4, entry::Modifier::None, 1, cmd, "graphics hmd" },
{ entry::Key::F4, entry::Modifier::LeftShift, 1, cmd, "graphics hmdrecenter" },
{ entry::Key::F4, entry::Modifier::LeftCtrl, 1, cmd, "graphics hmddbg" },
{ entry::Key::F7, entry::Modifier::None, 1, cmd, "graphics vsync" },
{ entry::Key::F8, entry::Modifier::None, 1, cmd, "graphics msaa" },
{ entry::Key::Print, entry::Modifier::None, 1, cmd, "graphics screenshot" },
INPUT_BINDING_END
};
int main(int _argc, char** _argv)
{
//DBG(BX_COMPILER_NAME " / " BX_CPU_NAME " / " BX_ARCH_NAME " / " BX_PLATFORM_NAME);
2013-12-25 00:30:28 -05:00
2014-05-03 18:18:28 -04:00
#if BX_CONFIG_CRT_FILE_READER_WRITER
s_fileReader = new bx::CrtFileReader;
s_fileWriter = new bx::CrtFileWriter;
#endif // BX_CONFIG_CRT_FILE_READER_WRITER
2015-02-09 23:12:46 -05:00
cmdInit();
cmdAdd("mouselock", cmdMouseLock);
cmdAdd("graphics", cmdGraphics );
cmdAdd("exit", cmdExit );
2015-02-09 23:12:46 -05:00
inputInit();
inputAddBindings("bindings", s_bindings);
2014-09-19 01:32:33 -04:00
entry::WindowHandle defaultWindow = { 0 };
entry::setWindowTitle(defaultWindow, bx::baseName(_argv[0]));
int32_t result = ::_main_(_argc, _argv);
2014-05-03 18:18:28 -04:00
2015-02-09 23:12:46 -05:00
inputRemoveBindings("bindings");
inputShutdown();
cmdShutdown();
2014-05-03 18:18:28 -04:00
#if BX_CONFIG_CRT_FILE_READER_WRITER
delete s_fileReader;
s_fileReader = NULL;
delete s_fileWriter;
s_fileWriter = NULL;
#endif // BX_CONFIG_CRT_FILE_READER_WRITER
return result;
}
2014-09-22 22:34:10 -04:00
bool processEvents(uint32_t& _width, uint32_t& _height, uint32_t& _debug, uint32_t& _reset, MouseState* _mouse)
{
s_debug = _debug;
2013-08-15 00:08:46 -04:00
s_reset = _reset;
2014-09-19 01:32:33 -04:00
WindowHandle handle = { UINT16_MAX };
bool mouseLock = inputIsMouseLocked();
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)
{
2014-12-15 23:58:54 -05:00
case Event::Axis:
{
const AxisEvent* axis = static_cast<const AxisEvent*>(ev);
inputSetGamepadAxis(axis->m_gamepad, axis->m_axis, axis->m_value);
}
break;
case Event::Char:
{
const CharEvent* chev = static_cast<const CharEvent*>(ev);
inputChar(chev->m_len, chev->m_char);
}
break;
case Event::Exit:
return true;
2014-12-18 00:07:40 -05:00
case Event::Gamepad:
{
const GamepadEvent* gev = static_cast<const GamepadEvent*>(ev);
DBG("gamepad %d, %d", gev->m_gamepad.idx, gev->m_connected);
}
break;
case Event::Mouse:
{
const MouseEvent* mouse = static_cast<const MouseEvent*>(ev);
2014-09-19 01:32:33 -04:00
handle = mouse->m_handle;
if (mouse->m_move)
{
2014-08-06 00:13:50 -04:00
inputSetMousePos(mouse->m_mx, mouse->m_my, mouse->m_mz);
}
else
{
inputSetMouseButtonState(mouse->m_button, mouse->m_down);
}
if (NULL != _mouse
&& !mouseLock)
{
if (mouse->m_move)
{
_mouse->m_mx = mouse->m_mx;
_mouse->m_my = mouse->m_my;
2014-08-06 00:13:50 -04:00
_mouse->m_mz = mouse->m_mz;
}
else
{
_mouse->m_buttons[mouse->m_button] = mouse->m_down;
}
}
}
break;
case Event::Key:
{
const KeyEvent* key = static_cast<const KeyEvent*>(ev);
2014-09-19 01:32:33 -04:00
handle = key->m_handle;
inputSetKeyState(key->m_key, key->m_modifiers, key->m_down);
}
break;
case Event::Size:
{
const SizeEvent* size = static_cast<const SizeEvent*>(ev);
2014-09-19 01:32:33 -04:00
handle = size->m_handle;
_width = size->m_width;
_height = size->m_height;
2014-09-19 01:32:33 -04:00
_reset = !s_reset; // force reset
}
break;
2014-09-19 01:32:33 -04:00
case Event::Window:
break;
default:
break;
}
}
2013-10-24 01:48:16 -04:00
inputProcess();
} while (NULL != ev);
2013-08-15 00:08:46 -04:00
2014-09-19 01:32:33 -04:00
if (handle.idx == 0
&& _reset != s_reset)
{
2015-02-11 22:42:20 -05:00
_reset = s_reset;
bgfx::reset(_width, _height, _reset);
inputSetMouseResolution(_width, _height);
}
_debug = s_debug;
return s_exit;
}
2014-09-22 22:34:10 -04:00
WindowState s_window[ENTRY_CONFIG_MAX_WINDOWS];
bool processWindowEvents(WindowState& _state, uint32_t& _debug, uint32_t& _reset)
{
s_debug = _debug;
s_reset = _reset;
WindowHandle handle = { UINT16_MAX };
bool mouseLock = inputIsMouseLocked();
const Event* ev;
do
{
struct SE
{
SE(WindowHandle _handle)
: m_ev(poll(_handle) )
{
}
2015-01-08 01:36:36 -05:00
2014-09-22 22:34:10 -04:00
~SE()
{
if (NULL != m_ev)
{
release(m_ev);
}
}
const Event* m_ev;
} scopeEvent(handle);
ev = scopeEvent.m_ev;
if (NULL != ev)
{
handle = ev->m_handle;
WindowState& win = s_window[handle.idx];
switch (ev->m_type)
{
2014-12-15 23:58:54 -05:00
case Event::Axis:
{
const AxisEvent* axis = static_cast<const AxisEvent*>(ev);
inputSetGamepadAxis(axis->m_gamepad, axis->m_axis, axis->m_value);
}
break;
case Event::Char:
{
const CharEvent* chev = static_cast<const CharEvent*>(ev);
win.m_handle = chev->m_handle;
inputChar(chev->m_len, chev->m_char);
}
break;
2014-09-22 22:34:10 -04:00
case Event::Exit:
return true;
2014-12-18 00:07:40 -05:00
case Event::Gamepad:
{
const GamepadEvent* gev = static_cast<const GamepadEvent*>(ev);
DBG("gamepad %d, %d", gev->m_gamepad.idx, gev->m_connected);
}
break;
2014-09-22 22:34:10 -04:00
case Event::Mouse:
{
const MouseEvent* mouse = static_cast<const MouseEvent*>(ev);
win.m_handle = mouse->m_handle;
if (mouse->m_move)
{
inputSetMousePos(mouse->m_mx, mouse->m_my, mouse->m_mz);
}
else
{
inputSetMouseButtonState(mouse->m_button, mouse->m_down);
}
if (!mouseLock)
{
if (mouse->m_move)
{
win.m_mouse.m_mx = mouse->m_mx;
win.m_mouse.m_my = mouse->m_my;
win.m_mouse.m_mz = mouse->m_mz;
}
else
{
win.m_mouse.m_buttons[mouse->m_button] = mouse->m_down;
}
}
}
break;
case Event::Key:
{
const KeyEvent* key = static_cast<const KeyEvent*>(ev);
win.m_handle = key->m_handle;
inputSetKeyState(key->m_key, key->m_modifiers, key->m_down);
}
break;
case Event::Size:
{
const SizeEvent* size = static_cast<const SizeEvent*>(ev);
win.m_handle = size->m_handle;
win.m_width = size->m_width;
win.m_height = size->m_height;
_reset = win.m_handle.idx == 0
? !s_reset
: _reset
; // force reset
}
break;
case Event::Window:
{
const WindowEvent* window = static_cast<const WindowEvent*>(ev);
win.m_handle = window->m_handle;
win.m_nwh = window->m_nwh;
ev = NULL;
}
break;
default:
break;
}
}
inputProcess();
} while (NULL != ev);
if (isValid(handle) )
{
const WindowState& win = s_window[handle.idx];
_state = win;
if (handle.idx == 0)
{
inputSetMouseResolution(win.m_width, win.m_height);
}
}
if (_reset != s_reset)
{
_reset = s_reset;
bgfx::reset(s_window[0].m_width, s_window[0].m_height, _reset);
inputSetMouseResolution(s_window[0].m_width, s_window[0].m_height);
}
_debug = s_debug;
return s_exit;
}
2014-05-03 18:18:28 -04:00
bx::FileReaderI* getFileReader()
{
return s_fileReader;
}
bx::FileWriterI* getFileWriter()
{
return s_fileWriter;
}
2015-01-08 01:36:36 -05:00
bx::ReallocatorI* getAllocator()
{
return s_allocator;
2015-01-08 01:36:36 -05:00
}
2015-02-08 13:25:53 -05:00
void* TinyStlAllocator::static_allocate(size_t _bytes)
{
return BX_ALLOC(getAllocator(), _bytes);
}
void TinyStlAllocator::static_deallocate(void* _ptr, size_t /*_bytes*/)
{
if (NULL != _ptr)
{
BX_FREE(getAllocator(), _ptr);
}
}
} // namespace entry
2014-05-31 03:18:45 -04:00
extern "C" bool entry_process_events(uint32_t* _width, uint32_t* _height, uint32_t* _debug, uint32_t* _reset)
{
return entry::processEvents(*_width, *_height, *_debug, *_reset, NULL);
}