examples-common: Reogranized sources. Added command interpreter and input bindings support.

This commit is contained in:
bkaradzic 2013-08-07 21:45:56 -07:00
parent 640c61fe45
commit 4c4c2eb5cb
34 changed files with 835 additions and 280 deletions

View file

@ -266,6 +266,7 @@ Todo
- Occlusion queries.
- DX11: MSAA.
- Fullscreen mode.
- GLES: Support for ETC1 and PVR compressed textures.
Contact
-------

View file

@ -6,9 +6,7 @@
#include "common.h"
#include <bgfx.h>
#include "entry.h"
#include "dbg.h"
#include "processevents.h"
#include "entry/entry.h"
int _main_(int /*_argc*/, char** /*_argv*/)
{
@ -31,7 +29,7 @@ int _main_(int /*_argc*/, char** /*_argv*/)
, 0
);
while (!processEvents(width, height, debug, reset) )
while (!entry::processEvents(width, height, debug, reset) )
{
// Set view 0 default viewport.
bgfx::setViewRect(0, 0, 0, width, height);

View file

@ -7,10 +7,8 @@
#include <bgfx.h>
#include <bx/timer.h>
#include "entry.h"
#include "dbg.h"
#include "entry/entry.h"
#include "fpumath.h"
#include "processevents.h"
#include <stdio.h>
#include <string.h>
@ -186,7 +184,7 @@ int _main_(int /*_argc*/, char** /*_argv*/)
int64_t timeOffset = bx::getHPCounter();
while (!processEvents(width, height, debug, reset) )
while (!entry::processEvents(width, height, debug, reset) )
{
// Set view 0 default viewport.
bgfx::setViewRect(0, 0, 0, width, height);

View file

@ -7,10 +7,8 @@
#include <bgfx.h>
#include <bx/timer.h>
#include "entry.h"
#include "dbg.h"
#include "entry/entry.h"
#include "fpumath.h"
#include "processevents.h"
#include <stdio.h>
#include <string.h>
@ -528,7 +526,7 @@ int _main_(int /*_argc*/, char** /*_argv*/)
int64_t timeOffset = bx::getHPCounter();
while (!processEvents(width, height, debug, reset) )
while (!entry::processEvents(width, height, debug, reset) )
{
// Set view 0 default viewport.
bgfx::setViewRect(0, 0, 0, width, height);

View file

@ -7,10 +7,8 @@
#include <bgfx.h>
#include <bx/timer.h>
#include "entry.h"
#include "dbg.h"
#include "entry/entry.h"
#include "fpumath.h"
#include "processevents.h"
#include <stdio.h>
#include <string.h>
@ -235,7 +233,7 @@ int _main_(int /*_argc*/, char** /*_argv*/)
int64_t timeOffset = bx::getHPCounter();
while (!processEvents(width, height, debug, reset) )
while (!entry::processEvents(width, height, debug, reset) )
{
// Set view 0 default viewport.
bgfx::setViewRect(0, 0, 0, width, height);

View file

@ -8,10 +8,8 @@
#include <bgfx.h>
#include <bx/timer.h>
#include <bx/readerwriter.h>
#include "entry.h"
#include "dbg.h"
#include "entry/entry.h"
#include "fpumath.h"
#include "processevents.h"
#include <stdio.h>
#include <string.h>
@ -334,7 +332,7 @@ int _main_(int /*_argc*/, char** /*_argv*/)
int64_t timeOffset = bx::getHPCounter();
while (!processEvents(width, height, debug, reset) )
while (!entry::processEvents(width, height, debug, reset) )
{
// Set view 0 default viewport.
bgfx::setViewRect(0, 0, 0, width, height);

View file

@ -7,10 +7,7 @@
#include <bgfx.h>
#include <bx/timer.h>
#include "entry.h"
#include "dbg.h"
#include "fpumath.h"
#include "processevents.h"
#include <stdio.h>
#include <string.h>
@ -175,7 +172,7 @@ int _main_(int /*_argc*/, char** /*_argv*/)
int64_t timeOffset = bx::getHPCounter();
while (!processEvents(width, height, debug, reset) )
while (!entry::processEvents(width, height, debug, reset) )
{
// Set view 0 default viewport.
bgfx::setViewRect(0, 0, 0, width, height);

View file

@ -7,10 +7,7 @@
#include <bgfx.h>
#include <bx/timer.h>
#include "entry.h"
#include "dbg.h"
#include "fpumath.h"
#include "processevents.h"
#include <stdio.h>
#include <string.h>
@ -335,7 +332,7 @@ int _main_(int /*_argc*/, char** /*_argv*/)
// We can destroy vertex and fragment shader here since
// their reference is kept inside bgfx after calling createProgram.
// Vertex and fragment shader will be destroyed once program is^
// Vertex and fragment shader will be destroyed once program is
// destroyed.
bgfx::destroyVertexShader(vsh);
bgfx::destroyFragmentShader(fsh);
@ -350,7 +347,7 @@ int _main_(int /*_argc*/, char** /*_argv*/)
int64_t timeOffset = bx::getHPCounter();
while (!processEvents(width, height, debug, reset) )
while (!entry::processEvents(width, height, debug, reset) )
{
// Set view 0 default viewport.
bgfx::setViewRect(0, 0, 0, width, height);

View file

@ -9,7 +9,6 @@
#include <bx/timer.h>
#include <bx/readerwriter.h>
#include <bx/string.h>
#include "dbg.h"
#include "fpumath.h"
#include "aviwriter.h"

View file

@ -8,10 +8,7 @@
#include <bgfx.h>
#include <bx/timer.h>
#include <bx/uint32_t.h>
#include "entry.h"
#include "dbg.h"
#include "fpumath.h"
#include "processevents.h"
#include "packrect.h"
#include <stdio.h>
@ -231,7 +228,7 @@ int _main_(int /*_argc*/, char** /*_argv*/)
int64_t timeOffset = bx::getHPCounter();
while (!processEvents(width, height, debug, reset) )
while (!entry::processEvents(width, height, debug, reset) )
{
// Set view 0 default viewport.
bgfx::setViewRect(0, 0, 0, width, height);

View file

@ -8,10 +8,7 @@
#include <bgfx.h>
#include <bx/timer.h>
#include <bx/readerwriter.h>
#include "entry.h"
#include "dbg.h"
#include "fpumath.h"
#include "processevents.h"
#include "imgui/imgui.h"
#include <string.h>
@ -514,11 +511,11 @@ int _main_(int /*_argc*/, char** /*_argv*/)
uint32_t oldWidth = 0;
uint32_t oldHeight = 0;
MouseState mouseState;
entry::MouseState mouseState;
float time = 0.0f;
while (!processEvents(width, height, debug, reset, &mouseState) )
while (!entry::processEvents(width, height, debug, reset, &mouseState) )
{
if (oldWidth != width
|| oldHeight != height)

View file

@ -8,10 +8,7 @@
#include <bgfx.h>
#include <bx/timer.h>
#include <bx/string.h>
#include "entry.h"
#include "dbg.h"
#include "fpumath.h"
#include "processevents.h"
#include "font/font_manager.h"
#include "font/text_buffer_manager.h"
@ -170,7 +167,7 @@ int _main_(int /*_argc*/, char** /*_argv*/)
// Create a transient buffer for real-time data.
TextBufferHandle transientText = textBufferManager->createTextBuffer(FONT_TYPE_ALPHA, BufferType::Transient);
while (!processEvents(width, height, debug, reset) )
while (!entry::processEvents(width, height, debug, reset) )
{
// Set view 0 default viewport.
bgfx::setViewRect(0, 0, 0, width, height);

View file

@ -4,12 +4,10 @@
*/
#include "common.h"
#include <bgfx.h>
#include <bx/timer.h>
#include "entry.h"
#include "dbg.h"
#include "fpumath.h"
#include "processevents.h"
#include "font/font_manager.h"
#include "font/text_metrics.h"
@ -172,7 +170,7 @@ int _main_(int /*_argc*/, char** /*_argv*/)
textBufferManager->appendText(scrollableBuffer, fontScaled, textBegin, textEnd);
MouseState mouseState;
entry::MouseState mouseState;
int32_t scrollArea = 0;
const int32_t guiPanelWidth = 250;
const int32_t guiPanelHeight = 200;
@ -181,7 +179,7 @@ int _main_(int /*_argc*/, char** /*_argv*/)
float textScale = 1.0f;
float textSize = 14.0f;
while (!processEvents(width, height, debug, reset, &mouseState) )
while (!entry::processEvents(width, height, debug, reset, &mouseState) )
{
imguiBeginFrame(mouseState.m_mx
, mouseState.m_my

View file

@ -8,10 +8,7 @@
#include <bgfx.h>
#include <bx/timer.h>
#include <bx/readerwriter.h>
#include "entry.h"
#include "dbg.h"
#include "fpumath.h"
#include "processevents.h"
#include "imgui/imgui.h"
#include <stdio.h>
@ -405,8 +402,8 @@ int _main_(int /*_argc*/, char** /*_argv*/)
float at[3] = { 0.0f, 1.0f, 0.0f };
float eye[3] = { 0.0f, 1.0f, -2.0f };
MouseState mouseState;
while (!processEvents(width, height, debug, reset, &mouseState) )
entry::MouseState mouseState;
while (!entry::processEvents(width, height, debug, reset, &mouseState) )
{
imguiBeginFrame(mouseState.m_mx
, mouseState.m_my

View file

@ -3,36 +3,4 @@
* License: http://www.opensource.org/licenses/BSD-2-Clause
*/
#ifndef __COMMON_H__
#define __COMMON_H__
#include "dbg.h"
#if 0
# define BX_TRACE(_format, ...) \
do { \
DBG(BX_FILE_LINE_LITERAL "BGFX " _format "\n", ##__VA_ARGS__); \
} while(0)
# define BX_WARN(_condition, _format, ...) \
do { \
if (!(_condition) ) \
{ \
DBG("WARN " _format, ##__VA_ARGS__); \
} \
} while(0)
# define BX_CHECK(_condition, _format, ...) \
do { \
if (!(_condition) ) \
{ \
DBG("CHECK " _format, ##__VA_ARGS__); \
bx::debugBreak(); \
} \
} while(0)
#endif // 0
#include <bx/bx.h>
#include <bx/debug.h>
#endif // __COMMON_H__
#include "entry/entry.h"

View file

@ -0,0 +1,228 @@
/*
* Copyright 2010-2013 Branimir Karadzic. All rights reserved.
* License: http://www.opensource.org/licenses/BSD-2-Clause
*/
#include <stdint.h>
#include <stdlib.h> // size_t
// #include <stdio.h>
#include <ctype.h> // isspace
#include <bx/hash.h>
#include "dbg.h"
#include "cmd.h"
#include <string>
#include <unordered_map>
// Reference:
// http://msdn.microsoft.com/en-us/library/a1y7w461.aspx
static const char* tokenizeCommandLine(const char* _commandLine, char* _buffer, uint32_t& _bufferSize, int& _argc, char* _argv[], int _maxArgvs, char _term)
{
int argc = 0;
const char* curr = _commandLine;
char* currOut = _buffer;
char term = ' ';
bool sub = false;
enum ParserState
{
SkipWhitespace,
SetTerm,
Copy,
Escape,
End,
};
ParserState state = SkipWhitespace;
while ('\0' != *curr
&& _term != *curr
&& argc < _maxArgvs)
{
switch (state)
{
case SkipWhitespace:
for (; isspace(*curr); ++curr); // skip whitespace
state = SetTerm;
break;
case SetTerm:
if ('"' == *curr)
{
term = '"';
++curr; // skip begining quote
}
else
{
term = ' ';
}
_argv[argc] = currOut;
++argc;
state = Copy;
break;
case Copy:
if ('\\' == *curr)
{
state = Escape;
}
else if ('"' == *curr
&& '"' != term)
{
sub = !sub;
}
else if (isspace(*curr) && !sub)
{
state = End;
}
else if (term != *curr || sub)
{
*currOut = *curr;
++currOut;
}
else
{
state = End;
}
++curr;
break;
case Escape:
{
const char* start = --curr;
for (; '\\' == *curr; ++curr);
if ('"' != *curr)
{
int count = (int)(curr-start);
curr = start;
for (int ii = 0; ii < count; ++ii)
{
*currOut = *curr;
++currOut;
++curr;
}
}
else
{
curr = start+1;
*currOut = *curr;
++currOut;
++curr;
}
}
state = Copy;
break;
case End:
*currOut = '\0';
++currOut;
state = SkipWhitespace;
break;
}
}
*currOut = '\0';
if (0 < argc
&& '\0' == _argv[argc-1][0])
{
--argc;
}
_bufferSize = (uint32_t)(currOut - _buffer);
_argc = argc;
if ('\0' != *curr)
{
++curr;
}
return curr;
}
struct CmdContext
{
CmdContext()
{
}
~CmdContext()
{
}
void add(const char* _name, ConsoleFn _fn, void* _userData)
{
uint32_t cmd = bx::hashMurmur2A(_name, (uint32_t)strlen(_name) );
BX_CHECK(m_lookup.end() == m_lookup.find(cmd), "Command \"%s\" already exist.", _name);
Func fn = { _fn, _userData };
m_lookup.insert(std::make_pair(cmd, fn) );
}
void exec(const char* _cmd)
{
for (const char* next = _cmd; '\0' != *next; _cmd = next)
{
char commandLine[1024];
uint32_t size = sizeof(commandLine);
int argc;
char* argv[64];
next = tokenizeCommandLine(_cmd, commandLine, size, argc, argv, BX_COUNTOF(argv), '\n');
if (argc > 0)
{
int err = -1;
uint32_t cmd = bx::hashMurmur2A(argv[0], (uint32_t)strlen(argv[0]) );
CmdLookup::iterator it = m_lookup.find(cmd);
if (it != m_lookup.end() )
{
Func& fn = it->second;
err = fn.m_fn(this, fn.m_userData, argc, argv);
}
switch (err)
{
case 0:
break;
case -1:
{
std::string tmp(_cmd, next-_cmd - (*next == '\0' ? 0 : 1) );
DBG("Command '%s' doesn't exist.", tmp.c_str() );
}
break;
default:
{
std::string tmp(_cmd, next-_cmd - (*next == '\0' ? 0 : 1) );
DBG("Failed '%s' err: %d.", tmp.c_str(), err);
}
break;
}
}
}
}
struct Func
{
ConsoleFn m_fn;
void* m_userData;
};
typedef std::unordered_map<uint32_t, Func> CmdLookup;
CmdLookup m_lookup;
};
static CmdContext s_cmdContext;
void cmdAdd(const char* _name, ConsoleFn _fn, void* _userData)
{
s_cmdContext.add(_name, _fn, _userData);
}
void cmdExec(const char* _cmd)
{
s_cmdContext.exec(_cmd);
}

View file

@ -0,0 +1,15 @@
/*
* Copyright 2010-2013 Branimir Karadzic. All rights reserved.
* License: http://www.opensource.org/licenses/BSD-2-Clause
*/
#ifndef __CMD_H__
#define __CMD_H__
struct CmdContext;
typedef int (*ConsoleFn)(CmdContext* _context, void* _userData, int _argc, char const* const* _argv);
void cmdAdd(const char* _name, ConsoleFn _fn, void* _userData = NULL);
void cmdExec(const char* _cmd);
#endif // __CMD_H__

View file

@ -3,8 +3,6 @@
* License: http://www.opensource.org/licenses/BSD-2-Clause
*/
#include "common.h"
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>

View file

@ -0,0 +1,190 @@
/*
* Copyright 2011-2013 Branimir Karadzic. All rights reserved.
* License: http://www.opensource.org/licenses/BSD-2-Clause
*/
#include <bgfx.h>
#include <bx/string.h>
#include "entry_p.h"
#include "cmd.h"
#include "input.h"
extern 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;
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)
{
if (setOrToggle(s_reset, "vsync", BGFX_RESET_VSYNC, 1, _argc, _argv)
|| setOrToggle(s_reset, "msaa", BGFX_RESET_MSAA_X16, 1, _argc, _argv) )
{
return 0;
}
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) )
{
bgfx::setDebug(s_debug);
return 0;
}
}
return 1;
}
int cmdExit(CmdContext* /*_context*/, void* /*_userData*/, int /*_argc*/, char const* const* /*_argv*/)
{
s_exit = true;
return 0;
}
static const InputBinding s_bindings[] =
{
{ entry::Key::KeyQ, entry::Modifier::LeftCtrl, 1, cmd, "exit" },
{ entry::Key::F1, entry::Modifier::None, 1, cmd, "graphics stats" },
{ entry::Key::F7, entry::Modifier::None, 1, cmd, "graphics vsync" },
{ entry::Key::F8, entry::Modifier::None, 1, cmd, "graphics msaa" },
INPUT_BINDING_END
};
int main(int _argc, char** _argv)
{
cmdAdd("mouselock", cmdMouseLock);
cmdAdd("graphics", cmdGraphics );
cmdAdd("exit", cmdExit );
inputAddBindings("bindings", s_bindings);
int32_t result = ::_main_(_argc, _argv);
return result;
}
bool processEvents(uint32_t& _width, uint32_t& _height, uint32_t& _debug, uint32_t& _reset, MouseState* _mouse)
{
s_debug = _debug;
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)
{
case Event::Exit:
return true;
case Event::Mouse:
{
const MouseEvent* mouse = static_cast<const MouseEvent*>(ev);
if (mouse->m_move)
{
inputSetMousePos(mouse->m_mx, mouse->m_my);
}
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;
}
else
{
_mouse->m_buttons[mouse->m_button] = mouse->m_down;
}
}
}
break;
case Event::Key:
{
const KeyEvent* key = static_cast<const KeyEvent*>(ev);
inputSetKeyState(key->m_key, key->m_modifiers, key->m_down);
}
break;
case Event::Size:
{
const SizeEvent* size = static_cast<const SizeEvent*>(ev);
_width = size->m_width;
_height = size->m_height;
_reset = !s_reset; // force reset
}
break;
default:
break;
}
}
} while (NULL != ev);
if (_reset != s_reset)
{
_reset = s_reset;
bgfx::reset(_width, _height, _reset);
inputSetMouseResolution(_width, _height);
}
inputProcess();
_debug = s_debug;
return s_exit;
}
} // namespace entry

View file

@ -6,6 +6,11 @@
#ifndef __ENTRY_H__
#define __ENTRY_H__
#include "dbg.h"
#include <bx/bx.h>
#include <bx/debug.h>
namespace entry
{
struct MouseButton
@ -119,47 +124,24 @@ namespace entry
};
};
struct Event
struct MouseState
{
enum Enum
MouseState()
: m_mx(0)
, m_my(0)
{
Exit,
Key,
Mouse,
Size,
};
for (uint32_t ii = 0; ii < entry::MouseButton::Count; ++ii)
{
m_buttons[ii] = entry::MouseButton::None;
}
}
Event::Enum m_type;
uint32_t m_mx;
uint32_t m_my;
uint8_t m_buttons[entry::MouseButton::Count];
};
struct KeyEvent : public Event
{
Key::Enum m_key;
uint8_t m_modifiers;
bool m_down;
};
struct MouseEvent : public Event
{
int32_t m_mx;
int32_t m_my;
MouseButton::Enum m_button;
bool m_down;
bool m_move;
};
struct SizeEvent : public Event
{
uint32_t m_width;
uint32_t m_height;
};
const Event* poll();
void release(const Event* _event);
void setWindowSize(uint32_t _width, uint32_t _height);
void toggleWindowFrame();
void setMouseLock(bool _lock);
bool processEvents(uint32_t& _width, uint32_t& _height, uint32_t& _debug, uint32_t& _reset, MouseState* _mouse = NULL);
} // namespace entry

View file

@ -3,7 +3,7 @@
* License: http://www.opensource.org/licenses/BSD-2-Clause
*/
#include "common.h"
#include "entry.h"
#if BX_PLATFORM_ANDROID
@ -23,8 +23,6 @@ extern "C"
#include <android_native_app_glue.c>
} // extern "C"
extern int _main_(int _argc, char** _argv);
namespace entry
{
struct MainThreadEntry
@ -218,15 +216,13 @@ namespace entry
int32_t MainThreadEntry::threadFunc(void* _userData)
{
MainThreadEntry* self = (MainThreadEntry*)_userData;
int32_t result = _main_(self->m_argc, self->m_argv);
int32_t result = main(self->m_argc, self->m_argv);
// PostMessage(s_ctx.m_hwnd, WM_QUIT, 0, 0);
return result;
}
} // namespace entry
extern int _main_(int _argc, char** _argv);
extern "C" void android_main(android_app* _app)
{
using namespace entry;

View file

@ -3,7 +3,7 @@
* License: http://www.opensource.org/licenses/BSD-2-Clause
*/
#include "common.h"
#include "entry.h"
#if BX_PLATFORM_LINUX
@ -24,8 +24,6 @@
#define DEFAULT_WIDTH 1280
#define DEFAULT_HEIGHT 720
extern int _main_(int _argc, char** _argv);
namespace entry
{
static uint8_t s_translateKey[512];
@ -295,7 +293,7 @@ namespace entry
int32_t MainThreadEntry::threadFunc(void* _userData)
{
MainThreadEntry* self = (MainThreadEntry*)_userData;
int32_t result = _main_(self->m_argc, self->m_argv);
int32_t result = main(self->m_argc, self->m_argv);
s_ctx.m_exit = true;
return result;
}

View file

@ -3,7 +3,7 @@
* License: http://www.opensource.org/licenses/BSD-2-Clause
*/
#include "common.h"
#include "entry.h"
#if BX_PLATFORM_NACL
@ -32,8 +32,6 @@
#include "entry.h"
extern int _main_(int _argc, char** _argv);
namespace entry
{
const PPB_Core* g_coreInterface;
@ -112,7 +110,7 @@ namespace entry
PP_Resource resource = g_messageLoopInterface->Create(g_instance);
g_messageLoopInterface->AttachToCurrentThread(resource);
int32_t result = _main_(self->m_argc, self->m_argv);
int32_t result = main(self->m_argc, self->m_argv);
return result;
}

View file

View file

@ -9,11 +9,54 @@
#include <bgfxplatform.h>
#include <bx/spscqueue.h>
#include "dbg.h"
#include "entry.h"
namespace entry
{
int main(int _argc, char** _argv);
struct Event
{
enum Enum
{
Exit,
Key,
Mouse,
Size,
};
Event::Enum m_type;
};
struct KeyEvent : public Event
{
Key::Enum m_key;
uint8_t m_modifiers;
bool m_down;
};
struct MouseEvent : public Event
{
int32_t m_mx;
int32_t m_my;
MouseButton::Enum m_button;
bool m_down;
bool m_move;
};
struct SizeEvent : public Event
{
uint32_t m_width;
uint32_t m_height;
};
const Event* poll();
void release(const Event* _event);
void setWindowSize(uint32_t _width, uint32_t _height);
void toggleWindowFrame();
void setMouseLock(bool _lock);
class EventQueue
{
public:

View file

@ -3,7 +3,7 @@
* License: http://www.opensource.org/licenses/BSD-2-Clause
*/
#include "common.h"
#include "entry.h"
#if BX_PLATFORM_QNX
@ -35,11 +35,9 @@ namespace entry
} // namespace entry
extern int _main_(int _argc, char** _argv);
int main(int _argc, char** _argv)
{
_main_(_argc, _argv);
entry::main(_argc, _argv);
}
#endif // BX_PLATFORM_QNX

View file

@ -3,7 +3,7 @@
* License: http://www.opensource.org/licenses/BSD-2-Clause
*/
#include "common.h"
#include "entry.h"
#if BX_PLATFORM_WINDOWS
@ -21,8 +21,6 @@
#define WM_USER_TOGGLE_WINDOW_FRAME (WM_USER+1)
#define WM_USER_MOUSE_LOCK (WM_USER+2)
extern int _main_(int _argc, char** _argv);
namespace entry
{
struct TranslateKeyModifiers
@ -625,7 +623,7 @@ namespace entry
int32_t MainThreadEntry::threadFunc(void* _userData)
{
MainThreadEntry* self = (MainThreadEntry*)_userData;
int32_t result = _main_(self->m_argc, self->m_argv);
int32_t result = main(self->m_argc, self->m_argv);
PostMessage(s_ctx.m_hwnd, WM_QUIT, 0, 0);
return result;
}

View file

@ -0,0 +1,251 @@
/*
* Copyright 2010-2013 Branimir Karadzic. All rights reserved.
* License: http://www.opensource.org/licenses/BSD-2-Clause
*/
#include "entry_p.h"
#include "input.h"
#include <memory.h>
#include <string>
#include <unordered_map>
struct Mouse
{
Mouse()
: m_width(1280)
, m_height(720)
, m_lock(false)
{
}
void reset()
{
if (m_lock)
{
m_norm[0] = 0.0f;
m_norm[1] = 0.0f;
}
memset(m_buttons, 0, sizeof(m_buttons) );
}
void setResolution(uint16_t _width, uint16_t _height)
{
m_width = _width;
m_height = _height;
}
void setPos(int32_t _mx, int32_t _my)
{
m_absolute[0] = _mx;
m_absolute[1] = _my;
m_norm[0] = float(_mx)/float(m_width);
m_norm[1] = float(_my)/float(m_height);
}
void setButtonState(entry::MouseButton::Enum _button, uint8_t _state)
{
m_buttons[_button] = _state;
}
void read(bool _mouseLock)
{
if (_mouseLock != m_lock)
{
m_lock = _mouseLock;
if (_mouseLock)
{
m_norm[0] = 0.0f;
m_norm[1] = 0.0f;
}
}
}
int32_t m_absolute[2];
float m_norm[2];
int32_t m_wheel;
uint8_t m_buttons[entry::MouseButton::Count];
uint16_t m_width;
uint16_t m_height;
bool m_lock;
};
struct Keyboard
{
Keyboard()
{
}
void reset()
{
memset(m_key, 0, sizeof(m_key) );
memset(m_once, 0xff, sizeof(m_once) );
}
static uint32_t encodeKeyState(uint8_t _modifiers, bool _down)
{
uint32_t state = 0;
state |= uint32_t(_modifiers)<<16;
state |= uint32_t(_down)<<8;
return state;
}
static void decodeKeyState(uint32_t _state, uint8_t& _modifiers, bool& _down)
{
_modifiers = (_state>>16)&0xff;
_down = 0 != ( (_state>>8)&0xff);
}
void setKeyState(entry::Key::Enum _key, uint8_t _modifiers, bool _down)
{
m_key[_key] = encodeKeyState(_modifiers, _down);
m_once[_key] = false;
}
uint32_t m_key[256];
bool m_once[256];
};
struct Input
{
Input()
{
reset();
}
~Input()
{
}
void addBindings(const char* _name, const InputBinding* _bindings)
{
m_inputBindingsMap.insert(std::make_pair(_name, _bindings) );
}
void removeBindings(const char* _name)
{
m_inputBindingsMap.erase(_name);
}
void process(const InputBinding* _bindings)
{
for (const InputBinding* binding = _bindings; binding->m_key != entry::Key::None; ++binding)
{
uint8_t modifiers;
bool down;
Keyboard::decodeKeyState(m_keyboard.m_key[binding->m_key], modifiers, down);
if (binding->m_flags == 1)
{
if (down
&& modifiers == binding->m_modifiers)
{
if (!m_keyboard.m_once[binding->m_key])
{
binding->m_fn(binding->m_userData);
m_keyboard.m_once[binding->m_key] = true;
}
}
else
{
m_keyboard.m_once[binding->m_key] = false;
}
}
else
{
if (down
&& modifiers == binding->m_modifiers)
{
binding->m_fn(binding->m_userData);
}
}
}
}
void process()
{
m_mouse.read(m_mouse.m_lock);
for (InputBindingMap::const_iterator it = m_inputBindingsMap.begin(); it != m_inputBindingsMap.end(); ++it)
{
process(it->second);
}
}
void reset()
{
m_mouse.reset();
m_keyboard.reset();
}
typedef std::unordered_map<std::string, const InputBinding*> InputBindingMap;
InputBindingMap m_inputBindingsMap;
Mouse m_mouse;
Keyboard m_keyboard;
};
static Input s_input;
void inputAddBindings(const char* _name, const InputBinding* _bindings)
{
s_input.addBindings(_name, _bindings);
}
void inputRemoveBindings(const char* _name)
{
s_input.removeBindings(_name);
}
void inputProcess()
{
s_input.process();
}
void inputSetMouseResolution(uint16_t _width, uint16_t _height)
{
s_input.m_mouse.setResolution(_width, _height);
}
void inputSetKeyState(entry::Key::Enum _key, uint8_t _modifiers, bool _down)
{
s_input.m_keyboard.setKeyState(_key, _modifiers, _down);
}
void inputSetMousePos(int32_t _mx, int32_t _my)
{
s_input.m_mouse.setPos(_mx, _my);
}
void inputSetMouseButtonState(entry::MouseButton::Enum _button, uint8_t _state)
{
s_input.m_mouse.setButtonState(_button, _state);
}
void inputGetMouse(float _mouse[2])
{
_mouse[0] = s_input.m_mouse.m_norm[0];
_mouse[1] = s_input.m_mouse.m_norm[1];
s_input.m_mouse.m_norm[0] = 0.0f;
s_input.m_mouse.m_norm[1] = 0.0f;
}
bool inputIsMouseLocked()
{
return s_input.m_mouse.m_lock;
}
void inputSetMouseLock(bool _lock)
{
if (s_input.m_mouse.m_lock != _lock)
{
s_input.m_mouse.m_lock = _lock;
entry::setMouseLock(_lock);
if (_lock)
{
s_input.m_mouse.m_norm[0] = 0.0f;
s_input.m_mouse.m_norm[1] = 0.0f;
}
}
}

View file

@ -0,0 +1,55 @@
/*
* Copyright 2010-2013 Branimir Karadzic. All rights reserved.
* License: http://www.opensource.org/licenses/BSD-2-Clause
*/
#ifndef __INPUT_H__
#define __INPUT_H__
#include <stdint.h>
#include "entry.h"
typedef void (*InputBindingFn)(const void* _userData);
struct InputBinding
{
entry::Key::Enum m_key;
uint8_t m_modifiers;
uint8_t m_flags;
InputBindingFn m_fn;
const void* m_userData;
};
#define INPUT_BINDING_END { entry::Key::None, entry::Modifier::None, NULL, NULL }
///
void inputAddBindings(const char* _name, const InputBinding* _bindings);
///
void inputRemoveBindings(const char* _name);
///
void inputProcess();
///
void inputSetKeyState(entry::Key::Enum _key, uint8_t _modifiers, bool _down);
///
void inputSetMouseResolution(uint16_t _width, uint16_t _height);
///
void inputSetMousePos(int32_t _mx, int32_t _my);
///
void inputSetMouseButtonState(entry::MouseButton::Enum _button, uint8_t _state);
///
void inputSetMouseLock(bool _lock);
///
void inputGetMouse(float _mouse[2]);
///
bool inputIsMouseLocked();
#endif // __INPUT_H__

View file

@ -1,130 +0,0 @@
/*
* 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__
struct MouseState
{
MouseState()
: m_mx(0)
, m_my(0)
{
for (uint32_t ii = 0; ii < entry::MouseButton::Count; ++ii)
{
m_buttons[ii] = entry::MouseButton::None;
}
}
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)
{
using namespace entry;
bool reset = false;
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:
if (NULL != _mouse)
{
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;
}
}
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) ) )
|| ( (key->m_key == Key::Esc) ) )
{
return true;
}
else if (key->m_down)
{
if (key->m_key == Key::F1)
{
_debug ^= BGFX_DEBUG_STATS;
bgfx::setDebug(_debug);
return false;
}
else if (key->m_key == Key::F7)
{
_reset ^= BGFX_RESET_VSYNC;
reset = true;
}
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();
}
}
}
break;
case Event::Size:
{
const SizeEvent* size = static_cast<const SizeEvent*>(ev);
_width = size->m_width;
_height = size->m_height;
reset = true;
}
break;
default:
break;
}
}
} while (NULL != ev);
if (reset)
{
bgfx::reset(_width, _height, _reset);
}
return false;
}
#endif // __PROCESS_EVENTS_H__

View file

@ -11,7 +11,6 @@
#include <string.h>
#include <algorithm>
#include <vector>
namespace std { namespace tr1 {} using namespace tr1; } // namespace std
#include <unordered_map>
#include <forsythtriangleorderoptimizer.h>

View file

@ -18,8 +18,6 @@
#include <vector>
#include <unordered_map>
namespace std { namespace tr1 {} using namespace tr1; } // namespace std
#define MAX_TAGS 256
extern "C"
{