bgfx/examples/common/entry/entry.h

325 lines
5.3 KiB
C
Raw Normal View History

2013-02-22 09:07:31 +04:00
/*
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
2013-02-22 09:07:31 +04:00
*/
#ifndef ENTRY_H_HEADER_GUARD
#define ENTRY_H_HEADER_GUARD
2013-02-22 09:07:31 +04:00
#include "dbg.h"
#include <bx/bx.h>
2016-11-20 05:38:13 +03:00
#include <bx/string.h>
2015-11-07 09:03:06 +03:00
namespace bx { struct FileReaderI; struct FileWriterI; struct AllocatorI; }
2014-05-04 02:18:28 +04:00
2014-06-01 23:01:50 +04:00
extern "C" int _main_(int _argc, char** _argv);
2014-09-23 06:34:10 +04:00
#define ENTRY_WINDOW_FLAG_NONE UINT32_C(0x00000000)
#define ENTRY_WINDOW_FLAG_ASPECT_RATIO UINT32_C(0x00000001)
#define ENTRY_WINDOW_FLAG_FRAME UINT32_C(0x00000002)
2014-09-23 06:34:10 +04:00
2017-06-26 07:44:04 +03:00
#ifndef ENTRY_CONFIG_IMPLEMENT_MAIN
# define ENTRY_CONFIG_IMPLEMENT_MAIN 0
#endif // ENTRY_CONFIG_IMPLEMENT_MAIN
#if ENTRY_CONFIG_IMPLEMENT_MAIN
#define ENTRY_IMPLEMENT_MAIN(_app, _name, _description) \
int _main_(int _argc, char** _argv) \
{ \
_app app(_name, _description); \
return entry::runApp(&app, _argc, _argv); \
}
#else
#define ENTRY_IMPLEMENT_MAIN(_app, _name, _description) \
_app s_ ## _app ## App(_name, _description)
#endif // ENTRY_CONFIG_IMPLEMENT_MAIN
2013-02-22 09:07:31 +04:00
namespace entry
{
2014-12-16 07:58:54 +03:00
struct WindowHandle { uint16_t idx; };
inline bool isValid(WindowHandle _handle) { return UINT16_MAX != _handle.idx; }
struct GamepadHandle { uint16_t idx; };
inline bool isValid(GamepadHandle _handle) { return UINT16_MAX != _handle.idx; }
2014-09-19 09:32:33 +04:00
2013-02-22 09:07:31 +04:00
struct MouseButton
{
enum Enum
{
None,
Left,
Middle,
Right,
Count
};
};
2014-12-16 07:58:54 +03:00
struct GamepadAxis
{
enum Enum
{
LeftX,
LeftY,
LeftZ,
RightX,
RightY,
RightZ,
Count
};
};
2013-02-22 09:07:31 +04:00
struct Modifier
{
enum Enum
2013-02-22 09:07:31 +04:00
{
None = 0,
LeftAlt = 0x01,
RightAlt = 0x02,
LeftCtrl = 0x04,
RightCtrl = 0x08,
LeftShift = 0x10,
RightShift = 0x20,
LeftMeta = 0x40,
RightMeta = 0x80,
};
};
struct Key
{
enum Enum
{
None = 0,
Esc,
Return,
Tab,
Space,
Backspace,
Up,
Down,
Left,
Right,
2015-05-30 20:06:26 +03:00
Insert,
Delete,
2013-02-22 09:07:31 +04:00
Home,
End,
2015-05-30 20:06:26 +03:00
PageUp,
PageDown,
2013-02-22 09:07:31 +04:00
Print,
Plus,
Minus,
2015-05-30 20:06:26 +03:00
LeftBracket,
RightBracket,
Semicolon,
Quote,
Comma,
Period,
Slash,
Backslash,
2015-05-30 23:11:18 +03:00
Tilde,
2013-02-22 09:07:31 +04: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,
2014-08-06 04:49:21 +04:00
2014-12-16 07:58:54 +03:00
GamepadA,
GamepadB,
GamepadX,
GamepadY,
GamepadThumbL,
GamepadThumbR,
GamepadShoulderL,
GamepadShoulderR,
GamepadUp,
GamepadDown,
GamepadLeft,
GamepadRight,
GamepadBack,
GamepadStart,
2014-12-20 21:38:40 +03:00
GamepadGuide,
2014-12-16 07:58:54 +03:00
Count
2013-02-22 09:07:31 +04:00
};
};
struct Suspend
{
enum Enum
{
WillSuspend,
DidSuspend,
WillResume,
DidResume,
Count
};
};
2015-05-30 20:06:26 +03:00
const char* getName(Key::Enum _key);
struct MouseState
2013-02-22 09:07:31 +04:00
{
MouseState()
: m_mx(0)
, m_my(0)
2014-08-06 08:13:50 +04:00
, m_mz(0)
2013-02-22 09:07:31 +04:00
{
for (uint32_t ii = 0; ii < entry::MouseButton::Count; ++ii)
{
m_buttons[ii] = entry::MouseButton::None;
}
}
2013-02-22 09:07:31 +04:00
2014-08-06 08:13:50 +04:00
int32_t m_mx;
int32_t m_my;
int32_t m_mz;
uint8_t m_buttons[entry::MouseButton::Count];
2013-02-22 09:07:31 +04:00
};
2014-12-16 07:58:54 +03:00
struct GamepadState
{
GamepadState()
{
2017-02-09 06:55:31 +03:00
bx::memSet(m_axis, 0, sizeof(m_axis) );
2014-12-16 07:58:54 +03:00
}
int32_t m_axis[entry::GamepadAxis::Count];
};
2014-09-23 06:34:10 +04:00
bool processEvents(uint32_t& _width, uint32_t& _height, uint32_t& _debug, uint32_t& _reset, MouseState* _mouse = NULL);
2013-02-22 09:07:31 +04:00
2014-08-06 04:49:34 +04:00
bx::FileReaderI* getFileReader();
bx::FileWriterI* getFileWriter();
2015-11-07 09:03:06 +03:00
bx::AllocatorI* getAllocator();
2014-08-06 04:49:34 +04:00
2014-09-23 06:34:10 +04:00
WindowHandle createWindow(int32_t _x, int32_t _y, uint32_t _width, uint32_t _height, uint32_t _flags = ENTRY_WINDOW_FLAG_NONE, const char* _title = "");
2014-09-19 09:32:33 +04:00
void destroyWindow(WindowHandle _handle);
2014-09-23 06:34:10 +04:00
void setWindowPos(WindowHandle _handle, int32_t _x, int32_t _y);
2014-09-19 09:32:33 +04:00
void setWindowSize(WindowHandle _handle, uint32_t _width, uint32_t _height);
void setWindowTitle(WindowHandle _handle, const char* _title);
void toggleWindowFrame(WindowHandle _handle);
void toggleFullscreen(WindowHandle _handle);
2014-09-19 09:32:33 +04:00
void setMouseLock(WindowHandle _handle, bool _lock);
void setCurrentDir(const char* _dir);
2014-08-05 23:57:52 +04:00
2014-09-23 06:34:10 +04:00
struct WindowState
{
WindowState()
2015-11-02 07:11:31 +03:00
: m_width(0)
, m_height(0)
, m_nwh(NULL)
2014-09-23 06:34:10 +04:00
{
m_handle.idx = UINT16_MAX;
}
WindowHandle m_handle;
uint32_t m_width;
uint32_t m_height;
MouseState m_mouse;
void* m_nwh;
};
bool processWindowEvents(WindowState& _state, uint32_t& _debug, uint32_t& _reset);
2017-06-26 07:44:04 +03:00
class BX_NO_VTABLE AppI
{
2017-06-26 07:44:04 +03:00
public:
///
AppI(const char* _name, const char* _description);
///
virtual ~AppI() = 0;
2017-06-26 07:44:04 +03:00
///
2017-06-30 08:23:18 +03:00
virtual void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) = 0;
2017-06-26 07:44:04 +03:00
///
virtual int shutdown() = 0;
2017-06-26 07:44:04 +03:00
///
virtual bool update() = 0;
2017-06-26 07:44:04 +03:00
///
const char* getName() const;
2016-11-20 05:38:13 +03:00
2017-06-26 07:44:04 +03:00
///
const char* getDescription() const;
2016-11-20 05:38:13 +03:00
2017-06-26 07:44:04 +03:00
///
AppI* getNext();
2016-11-20 05:38:13 +03:00
2017-06-26 07:44:04 +03:00
AppI* m_next;
2016-11-20 05:38:13 +03:00
private:
const char* m_name;
2017-06-26 07:44:04 +03:00
const char* m_description;
2016-11-20 05:38:13 +03:00
};
2017-06-26 07:44:04 +03:00
///
AppI* getFirstApp();
///
uint32_t getNumApps();
2016-11-20 05:38:13 +03:00
///
2017-06-30 08:23:18 +03:00
int runApp(AppI* _app, int _argc, const char* const* _argv);
2013-02-22 09:07:31 +04:00
} // namespace entry
#endif // ENTRY_H_HEADER_GUARD