bgfx/examples/common/entry/entry.cpp

939 lines
21 KiB
C++
Raw Normal View History

/*
2017-01-01 11:18:41 +03:00
* Copyright 2011-2017 Branimir Karadzic. All rights reserved.
2016-01-01 11:11:04 +03:00
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
*/
2015-11-15 03:39:15 +03:00
#include <bx/bx.h>
#include <bgfx/bgfx.h>
#include <bx/string.h>
2016-02-22 00:59:38 +03:00
#include <bx/crtimpl.h>
2017-06-26 07:44:04 +03:00
#include <bx/sort.h>
2013-10-24 09:48:16 +04:00
#include <time.h>
#if BX_PLATFORM_EMSCRIPTEN
# include <emscripten.h>
#endif // BX_PLATFORM_EMSCRIPTEN
#include "entry_p.h"
#include "cmd.h"
#include "input.h"
2015-11-15 03:39:15 +03:00
#define RMT_ENABLED ENTRY_CONFIG_PROFILER
2015-11-16 17:42:56 +03:00
#include <remotery/lib/Remotery.h>
2015-11-13 09:26:50 +03:00
2017-06-30 08:23:18 +03:00
extern "C" int32_t _main_(int32_t _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;
2015-11-13 09:26:50 +03:00
static Remotery* s_rmt = NULL;
2014-05-04 02:18:28 +04:00
static bx::FileReaderI* s_fileReader = NULL;
static bx::FileWriterI* s_fileWriter = NULL;
2015-11-07 09:03:06 +03:00
extern bx::AllocatorI* getDefaultAllocator();
2016-12-08 09:46:54 +03:00
bx::AllocatorI* g_allocator = getDefaultAllocator();
2016-12-08 09:46:54 +03:00
typedef bx::StringT<&g_allocator> String;
2015-11-14 08:11:19 +03:00
void* rmtMalloc(void* /*_context*/, rmtU32 _size)
{
2016-12-08 09:46:54 +03:00
return BX_ALLOC(g_allocator, _size);
2015-11-14 08:11:19 +03:00
}
void* rmtRealloc(void* /*_context*/, void* _ptr, rmtU32 _size)
{
2016-12-08 09:46:54 +03:00
return BX_REALLOC(g_allocator, _ptr, _size);
2015-11-14 08:11:19 +03:00
}
void rmtFree(void* /*_context*/, void* _ptr)
{
2016-12-08 09:46:54 +03:00
BX_FREE(g_allocator, _ptr);
2015-11-14 08:11:19 +03:00
}
static String s_currentDir;
2017-06-12 07:01:38 +03:00
class FileReader : public bx::FileReader
{
2017-06-12 07:01:38 +03:00
typedef bx::FileReader super;
public:
virtual bool open(const char* _filePath, bx::Error* _err) BX_OVERRIDE
{
String filePath(s_currentDir);
filePath.append(_filePath);
return super::open(filePath.getPtr(), _err);
}
};
2017-06-12 07:01:38 +03:00
class FileWriter : public bx::FileWriter
{
2017-06-12 07:01:38 +03:00
typedef bx::FileWriter super;
public:
virtual bool open(const char* _filePath, bool _append, bx::Error* _err) BX_OVERRIDE
{
String filePath(s_currentDir);
filePath.append(_filePath);
return super::open(filePath.getPtr(), _append, _err);
}
};
void setCurrentDir(const char* _dir)
{
s_currentDir.set(_dir);
}
#if ENTRY_CONFIG_IMPLEMENT_DEFAULT_ALLOCATOR
2015-11-07 09:03:06 +03:00
bx::AllocatorI* getDefaultAllocator()
{
2015-04-05 05:10:35 +03:00
BX_PRAGMA_DIAGNOSTIC_PUSH();
2015-03-30 08:40:35 +03:00
BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4459); // warning C4459: declaration of 's_allocator' hides global declaration
2015-04-05 05:10:35 +03:00
BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wshadow");
2017-06-11 08:31:59 +03:00
static bx::DefaultAllocator s_allocator;
return &s_allocator;
2015-04-05 05:10:35 +03:00
BX_PRAGMA_DIAGNOSTIC_POP();
}
#endif // ENTRY_CONFIG_IMPLEMENT_DEFAULT_ALLOCATOR
2015-05-30 20:06:26 +03:00
static const char* s_keyName[] =
{
"None",
"Esc",
"Return",
"Tab",
"Space",
"Backspace",
"Up",
"Down",
"Left",
"Right",
"Insert",
"Delete",
"Home",
"End",
"PageUp",
"PageDown",
"Print",
"Plus",
"Minus",
"LeftBracket",
"RightBracket",
"Semicolon",
"Quote",
"Comma",
"Period",
"Slash",
"Backslash",
2015-05-30 23:11:18 +03:00
"Tilde",
2015-05-30 20:06:26 +03:00
"F1",
"F2",
"F3",
"F4",
"F5",
"F6",
"F7",
"F8",
"F9",
"F10",
"F11",
"F12",
"NumPad0",
"NumPad1",
"NumPad2",
"NumPad3",
"NumPad4",
"NumPad5",
"NumPad6",
"NumPad7",
"NumPad8",
"NumPad9",
"Key0",
"Key1",
"Key2",
"Key3",
"Key4",
"Key5",
"Key6",
"Key7",
"Key8",
"Key9",
"KeyA",
"KeyB",
"KeyC",
"KeyD",
"KeyE",
"KeyF",
"KeyG",
"KeyH",
"KeyI",
"KeyJ",
"KeyK",
"KeyL",
"KeyM",
"KeyN",
"KeyO",
"KeyP",
"KeyQ",
"KeyR",
"KeyS",
"KeyT",
"KeyU",
"KeyV",
"KeyW",
"KeyX",
"KeyY",
"KeyZ",
"GamepadA",
"GamepadB",
"GamepadX",
"GamepadY",
"GamepadThumbL",
"GamepadThumbR",
"GamepadShoulderL",
"GamepadShoulderR",
"GamepadUp",
"GamepadDown",
"GamepadLeft",
"GamepadRight",
"GamepadBack",
"GamepadStart",
"GamepadGuide",
};
BX_STATIC_ASSERT(Key::Count == BX_COUNTOF(s_keyName) );
const char* getName(Key::Enum _key)
{
BX_CHECK(_key < Key::Count, "Invalid key %d.", _key);
return s_keyName[_key];
}
2015-03-09 05:52:09 +03:00
char keyToAscii(Key::Enum _key, uint8_t _modifiers)
{
const bool isAscii = (Key::Key0 <= _key && _key <= Key::KeyZ)
|| (Key::Esc <= _key && _key <= Key::Minus);
if (!isAscii)
{
return '\0';
}
const bool isNumber = (Key::Key0 <= _key && _key <= Key::Key9);
if (isNumber)
{
2016-05-07 07:29:47 +03:00
return '0' + char(_key - Key::Key0);
2015-03-09 05:52:09 +03:00
}
const bool isChar = (Key::KeyA <= _key && _key <= Key::KeyZ);
if (isChar)
{
enum { ShiftMask = Modifier::LeftShift|Modifier::RightShift };
const bool shift = !!(_modifiers&ShiftMask);
2016-05-07 07:29:47 +03:00
return (shift ? 'A' : 'a') + char(_key - Key::KeyA);
2015-03-09 05:52:09 +03:00
}
switch (_key)
{
2015-03-12 08:45:34 +03:00
case Key::Esc: return 0x1b;
2015-03-12 09:30:41 +03:00
case Key::Return: return '\n';
case Key::Tab: return '\t';
case Key::Space: return ' ';
2015-03-12 08:45:34 +03:00
case Key::Backspace: return 0x08;
2015-03-12 09:30:41 +03:00
case Key::Plus: return '+';
case Key::Minus: return '-';
2015-03-12 08:45:34 +03:00
default: break;
2015-03-09 05:52:09 +03:00
}
2015-03-12 08:45:34 +03:00
return '\0';
2015-03-09 05:52:09 +03:00
}
bool setOrToggle(uint32_t& _flags, const char* _name, uint32_t _bit, int _first, int _argc, char const* const* _argv)
{
2017-04-23 00:47:02 +03:00
if (0 == bx::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;
}
int cmdMouseLock(CmdContext* /*_context*/, void* /*_userData*/, int _argc, char const* const* _argv)
{
if (_argc > 1)
{
inputSetMouseLock(_argc > 1 ? bx::toBool(_argv[1]) : !inputIsMouseLocked() );
2017-06-30 08:23:18 +03:00
return bx::kExitSuccess;
}
2017-06-30 08:23:18 +03:00
return bx::kExitFailure;
}
int cmdGraphics(CmdContext* /*_context*/, void* /*_userData*/, int _argc, char const* const* _argv)
{
if (_argc > 1)
{
2015-05-15 23:14:35 +03: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)
|| setOrToggle(s_reset, "flush", BGFX_RESET_FLUSH_AFTER_RENDER, 1, _argc, _argv)
|| setOrToggle(s_reset, "flip", BGFX_RESET_FLIP_AFTER_RENDER, 1, _argc, _argv)
2015-08-18 02:43:56 +03:00
|| setOrToggle(s_reset, "hidpi", BGFX_RESET_HIDPI, 1, _argc, _argv)
2015-12-13 23:33:27 +03:00
|| setOrToggle(s_reset, "depthclamp", BGFX_RESET_DEPTH_CLAMP, 1, _argc, _argv)
2015-04-15 06:03:05 +03:00
)
{
2017-06-30 08:23:18 +03:00
return bx::kExitSuccess;
}
2013-08-09 09: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);
2017-06-30 08:23:18 +03:00
return bx::kExitSuccess;
}
2017-04-23 00:47:02 +03:00
else if (0 == bx::strCmp(_argv[1], "screenshot") )
2013-10-24 09:48:16 +04:00
{
bgfx::FrameBufferHandle fbh = BGFX_INVALID_HANDLE;
2013-10-24 09:48:16 +04:00
if (_argc > 2)
{
2017-03-03 06:29:34 +03:00
bgfx::requestScreenShot(fbh, _argv[2]);
2013-10-24 09:48:16 +04:00
}
else
{
time_t tt;
time(&tt);
char filePath[256];
bx::snprintf(filePath, sizeof(filePath), "temp/screenshot-%d", tt);
2017-03-03 06:29:34 +03:00
bgfx::requestScreenShot(fbh, filePath);
2013-10-24 09:48:16 +04:00
}
2017-06-30 08:23:18 +03:00
return bx::kExitSuccess;
2013-10-24 09:48:16 +04:00
}
2017-04-23 00:47:02 +03:00
else if (0 == bx::strCmp(_argv[1], "fullscreen") )
2015-06-01 01:57:52 +03:00
{
WindowHandle window = { 0 };
toggleFullscreen(window);
2017-06-30 08:23:18 +03:00
return bx::kExitSuccess;
2015-06-01 01:57:52 +03:00
}
}
2017-06-30 08:23:18 +03:00
return bx::kExitFailure;
}
int cmdExit(CmdContext* /*_context*/, void* /*_userData*/, int /*_argc*/, char const* const* /*_argv*/)
{
s_exit = true;
2017-06-30 08:23:18 +03:00
return bx::kExitSuccess;
}
2015-01-08 09:36:36 +03:00
static const InputBinding s_bindings[] =
{
2015-05-31 08:11:42 +03:00
{ entry::Key::KeyQ, entry::Modifier::LeftCtrl, 1, NULL, "exit" },
2015-06-01 01:57:52 +03:00
{ entry::Key::KeyQ, entry::Modifier::RightCtrl, 1, NULL, "exit" },
{ entry::Key::KeyF, entry::Modifier::LeftCtrl, 1, NULL, "graphics fullscreen" },
{ entry::Key::KeyF, entry::Modifier::RightCtrl, 1, NULL, "graphics fullscreen" },
2015-09-02 08:15:47 +03:00
{ entry::Key::Return, entry::Modifier::RightAlt, 1, NULL, "graphics fullscreen" },
2015-05-31 08:11:42 +03:00
{ entry::Key::F1, entry::Modifier::None, 1, NULL, "graphics stats" },
2017-01-11 04:14:50 +03:00
{ entry::Key::F1, entry::Modifier::LeftCtrl, 1, NULL, "graphics ifh" },
2015-05-31 08:11:42 +03:00
{ entry::Key::GamepadStart, entry::Modifier::None, 1, NULL, "graphics stats" },
{ entry::Key::F1, entry::Modifier::LeftShift, 1, NULL, "graphics stats 0\ngraphics text 0" },
{ entry::Key::F3, entry::Modifier::None, 1, NULL, "graphics wireframe" },
{ entry::Key::F4, entry::Modifier::None, 1, NULL, "graphics hmd" },
{ entry::Key::F4, entry::Modifier::LeftShift, 1, NULL, "graphics hmdrecenter" },
{ entry::Key::F4, entry::Modifier::LeftCtrl, 1, NULL, "graphics hmddbg" },
{ entry::Key::F7, entry::Modifier::None, 1, NULL, "graphics vsync" },
{ entry::Key::F8, entry::Modifier::None, 1, NULL, "graphics msaa" },
{ entry::Key::F9, entry::Modifier::None, 1, NULL, "graphics flush" },
2015-08-18 02:43:56 +03:00
{ entry::Key::F10, entry::Modifier::None, 1, NULL, "graphics hidpi" },
2015-05-31 08:11:42 +03:00
{ entry::Key::Print, entry::Modifier::None, 1, NULL, "graphics screenshot" },
2017-01-06 08:28:43 +03:00
{ entry::Key::KeyP, entry::Modifier::LeftCtrl, 1, NULL, "graphics screenshot" },
2017-01-11 04:14:50 +03:00
INPUT_BINDING_END
};
#if BX_PLATFORM_EMSCRIPTEN
static AppI* s_app;
static void updateApp()
{
s_app->update();
}
#endif // BX_PLATFORM_EMSCRIPTEN
2017-06-30 08:23:18 +03:00
static AppI* s_currentApp = NULL;
static AppI* s_apps = NULL;
static uint32_t s_numApps = 0;
static char s_restartArgs[1024] = { '\0' };
int cmdApp(CmdContext* /*_context*/, void* /*_userData*/, int _argc, char const* const* _argv)
{
if (0 == bx::strCmp(_argv[1], "restart")
&& NULL != s_currentApp)
{
if (1 == _argc)
{
bx::strCopy(s_restartArgs, BX_COUNTOF(s_restartArgs), s_currentApp->getName() );
return bx::kExitSuccess;
}
if (0 == bx::strCmp(_argv[2], "next") )
{
AppI* next = s_currentApp->getNext();
if (NULL == next)
{
next = getFirstApp();
}
bx::strCopy(s_restartArgs, BX_COUNTOF(s_restartArgs), next->getName() );
return bx::kExitSuccess;
}
for (AppI* app = getFirstApp(); NULL != app; app = app->getNext() )
{
if (0 == bx::strCmp(_argv[2], app->getName() ) )
{
bx::strCopy(s_restartArgs, BX_COUNTOF(s_restartArgs), app->getName() );
return bx::kExitSuccess;
}
}
}
return bx::kExitFailure;
}
2016-11-20 05:38:13 +03:00
2017-06-26 07:44:04 +03:00
AppI::AppI(const char* _name, const char* _description)
2016-11-20 05:38:13 +03:00
{
2017-06-26 07:44:04 +03:00
m_name = _name;
m_description = _description;
m_next = s_apps;
2016-11-20 05:38:13 +03:00
s_apps = this;
2017-06-26 07:44:04 +03:00
s_numApps++;
}
const char* AppI::getName() const
{
return m_name;
}
const char* AppI::getDescription() const
{
return m_description;
2016-11-20 05:38:13 +03:00
}
2017-06-26 07:44:04 +03:00
AppI* AppI::getNext()
2016-11-20 05:38:13 +03:00
{
2017-06-26 07:44:04 +03:00
return m_next;
2016-11-20 05:38:13 +03:00
}
2017-06-26 07:44:04 +03:00
AppI* getFirstApp()
2016-11-20 05:38:13 +03:00
{
return s_apps;
}
2017-06-26 07:44:04 +03:00
uint32_t getNumApps()
{
return s_numApps;
}
2017-06-30 08:23:18 +03:00
int runApp(AppI* _app, int _argc, const char* const* _argv)
2017-06-26 07:44:04 +03:00
{
2017-06-30 08:23:18 +03:00
_app->init(_argc, _argv, ENTRY_DEFAULT_WIDTH, ENTRY_DEFAULT_HEIGHT);
2015-10-21 08:45:35 +03:00
bgfx::frame();
2015-10-21 05:00:13 +03:00
WindowHandle defaultWindow = { 0 };
setWindowSize(defaultWindow, ENTRY_DEFAULT_WIDTH, ENTRY_DEFAULT_HEIGHT);
#if BX_PLATFORM_EMSCRIPTEN
s_app = _app;
emscripten_set_main_loop(&updateApp, -1, 1);
#else
2017-06-30 08:23:18 +03:00
while (_app->update() )
{
if (0 != bx::strLen(s_restartArgs) )
{
break;
}
}
#endif // BX_PLATFORM_EMSCRIPTEN
return _app->shutdown();
}
2017-06-26 07:44:04 +03:00
static int32_t sortApp(const void* _lhs, const void* _rhs)
{
const AppI* lhs = *(const AppI**)_lhs;
const AppI* rhs = *(const AppI**)_rhs;
return bx::strCmpI(lhs->getName(), rhs->getName() );
}
static void sortApps()
{
if (2 > s_numApps)
{
return;
}
AppI** apps = (AppI**)BX_ALLOC(g_allocator, s_numApps*sizeof(AppI*) );
uint32_t ii = 0;
for (AppI* app = getFirstApp(); NULL != app; app = app->getNext() )
{
apps[ii++] = app;
}
bx::quickSort(apps, s_numApps, sizeof(AppI*), sortApp);
s_apps = apps[0];
for (ii = 1; ii < s_numApps; ++ii)
{
AppI* app = apps[ii-1];
app->m_next = apps[ii];
}
apps[s_numApps-1]->m_next = NULL;
BX_FREE(g_allocator, apps);
}
2017-06-30 08:23:18 +03:00
int main(int _argc, const char* const* _argv)
{
//DBG(BX_COMPILER_NAME " / " BX_CPU_NAME " / " BX_ARCH_NAME " / " BX_PLATFORM_NAME);
2013-12-25 09:30:28 +04:00
2015-11-13 09:26:50 +03:00
if (BX_ENABLED(ENTRY_CONFIG_PROFILER) )
{
2015-11-14 08:11:19 +03:00
rmtSettings* settings = rmt_Settings();
BX_WARN(NULL != settings, "Remotery is not enabled.");
if (NULL != settings)
2015-11-13 09:26:50 +03:00
{
2015-11-14 08:11:19 +03:00
settings->malloc = rmtMalloc;
settings->realloc = rmtRealloc;
settings->free = rmtFree;
rmtError err = rmt_CreateGlobalInstance(&s_rmt);
BX_WARN(RMT_ERROR_NONE != err, "Remotery failed to create global instance.");
2015-11-15 04:09:58 +03:00
if (RMT_ERROR_NONE == err)
{
rmt_SetCurrentThreadName("Main");
}
else
2015-11-14 08:11:19 +03:00
{
s_rmt = NULL;
}
2015-11-13 09:26:50 +03:00
}
}
2016-12-08 09:46:54 +03:00
s_fileReader = BX_NEW(g_allocator, FileReader);
s_fileWriter = BX_NEW(g_allocator, FileWriter);
2014-05-04 02:18:28 +04:00
2015-02-10 07:12:46 +03:00
cmdInit();
cmdAdd("mouselock", cmdMouseLock);
cmdAdd("graphics", cmdGraphics );
cmdAdd("exit", cmdExit );
2017-06-30 08:23:18 +03:00
cmdAdd("app", cmdApp );
2015-02-10 07:12:46 +03:00
inputInit();
inputAddBindings("bindings", s_bindings);
2014-09-19 09:32:33 +04:00
entry::WindowHandle defaultWindow = { 0 };
2015-07-17 06:28:43 +03:00
entry::setWindowTitle(defaultWindow, bx::baseName(_argv[0]) );
2015-10-21 05:00:13 +03:00
setWindowSize(defaultWindow, ENTRY_DEFAULT_WIDTH, ENTRY_DEFAULT_HEIGHT);
2017-06-26 07:44:04 +03:00
sortApps();
const char* find = "";
if (1 < _argc)
{
find = _argv[_argc-1];
}
restart:
AppI* selected = NULL;
for (AppI* app = getFirstApp(); NULL != app; app = app->getNext() )
{
if (NULL == selected
&& bx::strFindI(app->getName(), find) )
{
selected = app;
}
#if 0
DBG("%c %s, %s"
, app == selected ? '>' : ' '
, app->getName()
, app->getDescription()
);
#endif // 0
}
int32_t result = bx::kExitSuccess;
s_restartArgs[0] = '\0';
if (0 == s_numApps)
{
2017-06-30 08:23:18 +03:00
result = ::_main_(_argc, (char**)_argv);
2017-06-26 07:44:04 +03:00
}
else
{
2017-06-30 08:23:18 +03:00
s_currentApp = NULL == selected ? getFirstApp() : selected;
result = runApp(s_currentApp, _argc, _argv);
2017-06-26 07:44:04 +03:00
}
if (0 != bx::strLen(s_restartArgs) )
{
find = s_restartArgs;
goto restart;
}
setCurrentDir("");
2014-05-04 02:18:28 +04:00
2015-02-10 07:12:46 +03:00
inputRemoveBindings("bindings");
inputShutdown();
cmdShutdown();
2016-12-08 09:46:54 +03:00
BX_DELETE(g_allocator, s_fileReader);
2014-05-04 02:18:28 +04:00
s_fileReader = NULL;
2016-12-08 09:46:54 +03:00
BX_DELETE(g_allocator, s_fileWriter);
2014-05-04 02:18:28 +04:00
s_fileWriter = NULL;
2015-11-13 09:26:50 +03:00
if (BX_ENABLED(ENTRY_CONFIG_PROFILER)
&& NULL != s_rmt)
{
rmt_DestroyGlobalInstance(s_rmt);
}
return result;
}
2014-09-23 06: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 08:08:46 +04:00
s_reset = _reset;
2014-09-19 09: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-16 07:58:54 +03: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 08:07:40 +03:00
case Event::Gamepad:
{
2016-04-24 18:49:49 +03:00
// const GamepadEvent* gev = static_cast<const GamepadEvent*>(ev);
// DBG("gamepad %d, %d", gev->m_gamepad.idx, gev->m_connected);
2014-12-18 08:07:40 +03:00
}
break;
case Event::Mouse:
{
const MouseEvent* mouse = static_cast<const MouseEvent*>(ev);
2014-09-19 09:32:33 +04:00
handle = mouse->m_handle;
inputSetMousePos(mouse->m_mx, mouse->m_my, mouse->m_mz);
if (!mouse->m_move)
{
inputSetMouseButtonState(mouse->m_button, mouse->m_down);
}
if (NULL != _mouse
&& !mouseLock)
{
_mouse->m_mx = mouse->m_mx;
_mouse->m_my = mouse->m_my;
_mouse->m_mz = mouse->m_mz;
2016-11-20 05:38:13 +03:00
if (!mouse->m_move)
{
_mouse->m_buttons[mouse->m_button] = mouse->m_down;
}
}
}
break;
case Event::Key:
{
const KeyEvent* key = static_cast<const KeyEvent*>(ev);
2014-09-19 09: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 09:32:33 +04:00
handle = size->m_handle;
_width = size->m_width;
_height = size->m_height;
2014-09-19 09:32:33 +04:00
_reset = !s_reset; // force reset
}
break;
2014-09-19 09:32:33 +04:00
case Event::Window:
break;
2015-10-21 05:00:13 +03:00
case Event::Suspend:
break;
2015-10-21 05:00:13 +03:00
default:
break;
}
}
2013-10-24 09:48:16 +04:00
inputProcess();
} while (NULL != ev);
2013-08-15 08:08:46 +04:00
2014-09-19 09:32:33 +04:00
if (handle.idx == 0
&& _reset != s_reset)
{
2015-02-12 06:42:20 +03:00
_reset = s_reset;
bgfx::reset(_width, _height, _reset);
2016-05-07 07:29:47 +03:00
inputSetMouseResolution(uint16_t(_width), uint16_t(_height) );
}
_debug = s_debug;
return s_exit;
}
2014-09-23 06: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 09:36:36 +03:00
2014-09-23 06: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-16 07:58:54 +03: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-23 06:34:10 +04:00
case Event::Exit:
return true;
2014-12-18 08:07:40 +03: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-23 06: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;
2015-10-21 05:00:13 +03:00
case Event::Suspend:
break;
2014-09-23 06:34:10 +04:00
default:
break;
}
}
inputProcess();
} while (NULL != ev);
if (isValid(handle) )
{
const WindowState& win = s_window[handle.idx];
_state = win;
if (handle.idx == 0)
{
2016-05-07 07:29:47 +03:00
inputSetMouseResolution(uint16_t(win.m_width), uint16_t(win.m_height) );
2014-09-23 06:34:10 +04:00
}
}
if (_reset != s_reset)
{
_reset = s_reset;
bgfx::reset(s_window[0].m_width, s_window[0].m_height, _reset);
2016-05-07 07:29:47 +03:00
inputSetMouseResolution(uint16_t(s_window[0].m_width), uint16_t(s_window[0].m_height) );
2014-09-23 06:34:10 +04:00
}
_debug = s_debug;
return s_exit;
}
2014-05-04 02:18:28 +04:00
bx::FileReaderI* getFileReader()
{
return s_fileReader;
}
bx::FileWriterI* getFileWriter()
{
return s_fileWriter;
}
2015-11-07 09:03:06 +03:00
bx::AllocatorI* getAllocator()
2015-01-08 09:36:36 +03:00
{
2016-12-08 09:46:54 +03:00
return g_allocator;
2015-01-08 09:36:36 +03:00
}
2015-02-08 21:25:53 +03: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 11: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);
}