Fixed static initializer order.

This commit is contained in:
Branimir Karadžić 2015-02-09 20:12:46 -08:00
parent 74b59f1692
commit 3b2c0dc001
5 changed files with 73 additions and 28 deletions

View File

@ -7,6 +7,8 @@
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> // size_t #include <stdlib.h> // size_t
#include <string.h> // strlen #include <string.h> // strlen
#include <bx/allocator.h>
#include <bx/hash.h> #include <bx/hash.h>
#include <bx/tokenizecmd.h> #include <bx/tokenizecmd.h>
@ -90,14 +92,24 @@ struct CmdContext
CmdLookup m_lookup; CmdLookup m_lookup;
}; };
static CmdContext s_cmdContext; static CmdContext* s_cmdContext;
void cmdInit()
{
s_cmdContext = BX_NEW(entry::getAllocator(), CmdContext);
}
void cmdShutdown()
{
BX_DELETE(entry::getAllocator(), s_cmdContext);
}
void cmdAdd(const char* _name, ConsoleFn _fn, void* _userData) void cmdAdd(const char* _name, ConsoleFn _fn, void* _userData)
{ {
s_cmdContext.add(_name, _fn, _userData); s_cmdContext->add(_name, _fn, _userData);
} }
void cmdExec(const char* _cmd) void cmdExec(const char* _cmd)
{ {
s_cmdContext.exec(_cmd); s_cmdContext->exec(_cmd);
} }

View File

@ -9,7 +9,16 @@
struct CmdContext; struct CmdContext;
typedef int (*ConsoleFn)(CmdContext* _context, void* _userData, int _argc, char const* const* _argv); typedef int (*ConsoleFn)(CmdContext* _context, void* _userData, int _argc, char const* const* _argv);
///
void cmdInit();
///
void cmdShutdown();
///
void cmdAdd(const char* _name, ConsoleFn _fn, void* _userData = NULL); void cmdAdd(const char* _name, ConsoleFn _fn, void* _userData = NULL);
///
void cmdExec(const char* _cmd); void cmdExec(const char* _cmd);
#endif // CMD_H_HEADER_GUARD #endif // CMD_H_HEADER_GUARD

View File

@ -137,10 +137,12 @@ namespace entry
s_fileWriter = new bx::CrtFileWriter; s_fileWriter = new bx::CrtFileWriter;
#endif // BX_CONFIG_CRT_FILE_READER_WRITER #endif // BX_CONFIG_CRT_FILE_READER_WRITER
cmdInit();
cmdAdd("mouselock", cmdMouseLock); cmdAdd("mouselock", cmdMouseLock);
cmdAdd("graphics", cmdGraphics ); cmdAdd("graphics", cmdGraphics );
cmdAdd("exit", cmdExit ); cmdAdd("exit", cmdExit );
inputInit();
inputAddBindings("bindings", s_bindings); inputAddBindings("bindings", s_bindings);
entry::WindowHandle defaultWindow = { 0 }; entry::WindowHandle defaultWindow = { 0 };
@ -148,6 +150,11 @@ namespace entry
int32_t result = ::_main_(_argc, _argv); int32_t result = ::_main_(_argc, _argv);
inputRemoveBindings("bindings");
inputShutdown();
cmdShutdown();
#if BX_CONFIG_CRT_FILE_READER_WRITER #if BX_CONFIG_CRT_FILE_READER_WRITER
delete s_fileReader; delete s_fileReader;
s_fileReader = NULL; s_fileReader = NULL;

View File

@ -8,6 +8,7 @@
#include "entry_p.h" #include "entry_p.h"
#include "input.h" #include "input.h"
#include <bx/allocator.h>
#include <bx/ringbuffer.h> #include <bx/ringbuffer.h>
#include <tinystl/allocator.h> #include <tinystl/allocator.h>
#include <tinystl/unordered_map.h> #include <tinystl/unordered_map.h>
@ -249,95 +250,105 @@ struct Input
Gamepad m_gamepad[ENTRY_CONFIG_MAX_GAMEPADS]; Gamepad m_gamepad[ENTRY_CONFIG_MAX_GAMEPADS];
}; };
static Input s_input; static Input* s_input;
void inputInit()
{
s_input = BX_NEW(entry::getAllocator(), Input);
}
void inputShutdown()
{
BX_DELETE(entry::getAllocator(), s_input);
}
void inputAddBindings(const char* _name, const InputBinding* _bindings) void inputAddBindings(const char* _name, const InputBinding* _bindings)
{ {
s_input.addBindings(_name, _bindings); s_input->addBindings(_name, _bindings);
} }
void inputRemoveBindings(const char* _name) void inputRemoveBindings(const char* _name)
{ {
s_input.removeBindings(_name); s_input->removeBindings(_name);
} }
void inputProcess() void inputProcess()
{ {
s_input.process(); s_input->process();
} }
void inputSetMouseResolution(uint16_t _width, uint16_t _height) void inputSetMouseResolution(uint16_t _width, uint16_t _height)
{ {
s_input.m_mouse.setResolution(_width, _height); s_input->m_mouse.setResolution(_width, _height);
} }
void inputSetKeyState(entry::Key::Enum _key, uint8_t _modifiers, bool _down) void inputSetKeyState(entry::Key::Enum _key, uint8_t _modifiers, bool _down)
{ {
s_input.m_keyboard.setKeyState(_key, _modifiers, _down); s_input->m_keyboard.setKeyState(_key, _modifiers, _down);
} }
void inputChar(uint8_t _len, const uint8_t _char[4]) void inputChar(uint8_t _len, const uint8_t _char[4])
{ {
s_input.m_keyboard.pushChar(_len, _char); s_input->m_keyboard.pushChar(_len, _char);
} }
const uint8_t* inputGetChar() const uint8_t* inputGetChar()
{ {
return s_input.m_keyboard.popChar(); return s_input->m_keyboard.popChar();
} }
void inputCharFlush() void inputCharFlush()
{ {
s_input.m_keyboard.charFlush(); s_input->m_keyboard.charFlush();
} }
void inputSetMousePos(int32_t _mx, int32_t _my, int32_t _mz) void inputSetMousePos(int32_t _mx, int32_t _my, int32_t _mz)
{ {
s_input.m_mouse.setPos(_mx, _my, _mz); s_input->m_mouse.setPos(_mx, _my, _mz);
} }
void inputSetMouseButtonState(entry::MouseButton::Enum _button, uint8_t _state) void inputSetMouseButtonState(entry::MouseButton::Enum _button, uint8_t _state)
{ {
s_input.m_mouse.setButtonState(_button, _state); s_input->m_mouse.setButtonState(_button, _state);
} }
void inputGetMouse(float _mouse[3]) void inputGetMouse(float _mouse[3])
{ {
_mouse[0] = s_input.m_mouse.m_norm[0]; _mouse[0] = s_input->m_mouse.m_norm[0];
_mouse[1] = s_input.m_mouse.m_norm[1]; _mouse[1] = s_input->m_mouse.m_norm[1];
_mouse[2] = s_input.m_mouse.m_norm[2]; _mouse[2] = s_input->m_mouse.m_norm[2];
s_input.m_mouse.m_norm[0] = 0.0f; s_input->m_mouse.m_norm[0] = 0.0f;
s_input.m_mouse.m_norm[1] = 0.0f; s_input->m_mouse.m_norm[1] = 0.0f;
s_input.m_mouse.m_norm[2] = 0.0f; s_input->m_mouse.m_norm[2] = 0.0f;
} }
bool inputIsMouseLocked() bool inputIsMouseLocked()
{ {
return s_input.m_mouse.m_lock; return s_input->m_mouse.m_lock;
} }
void inputSetMouseLock(bool _lock) void inputSetMouseLock(bool _lock)
{ {
if (s_input.m_mouse.m_lock != _lock) if (s_input->m_mouse.m_lock != _lock)
{ {
s_input.m_mouse.m_lock = _lock; s_input->m_mouse.m_lock = _lock;
entry::WindowHandle defaultWindow = { 0 }; entry::WindowHandle defaultWindow = { 0 };
entry::setMouseLock(defaultWindow, _lock); entry::setMouseLock(defaultWindow, _lock);
if (_lock) if (_lock)
{ {
s_input.m_mouse.m_norm[0] = 0.0f; s_input->m_mouse.m_norm[0] = 0.0f;
s_input.m_mouse.m_norm[1] = 0.0f; s_input->m_mouse.m_norm[1] = 0.0f;
s_input.m_mouse.m_norm[2] = 0.0f; s_input->m_mouse.m_norm[2] = 0.0f;
} }
} }
} }
void inputSetGamepadAxis(entry::GamepadHandle _handle, entry::GamepadAxis::Enum _axis, int32_t _value) void inputSetGamepadAxis(entry::GamepadHandle _handle, entry::GamepadAxis::Enum _axis, int32_t _value)
{ {
s_input.m_gamepad[_handle.idx].setAxis(_axis, _value); s_input->m_gamepad[_handle.idx].setAxis(_axis, _value);
} }
int32_t inputGetGamepadAxis(entry::GamepadHandle _handle, entry::GamepadAxis::Enum _axis) int32_t inputGetGamepadAxis(entry::GamepadHandle _handle, entry::GamepadAxis::Enum _axis)
{ {
return s_input.m_gamepad[_handle.idx].getAxis(_axis); return s_input->m_gamepad[_handle.idx].getAxis(_axis);
} }

View File

@ -21,6 +21,12 @@ struct InputBinding
#define INPUT_BINDING_END { entry::Key::None, entry::Modifier::None, 0, NULL, NULL } #define INPUT_BINDING_END { entry::Key::None, entry::Modifier::None, 0, NULL, NULL }
///
void inputInit();
///
void inputShutdown();
/// ///
void inputAddBindings(const char* _name, const InputBinding* _bindings); void inputAddBindings(const char* _name, const InputBinding* _bindings);