Type prefixes (#34)

* update documentation

* Fix typo

* Added some examples

* changed window names

* Minor fix

* Added mfb_update_events to all platforms. Checked on Windows, X11 and Wayland

* simplify CMake

* Upgrade to CMake 3.5, simplify script and generalize it
Now the users only have to add_directory and link_libraries(minifb) in CMake

* Renamed typo scrool by scroll
Added some checks
Removed some warnings

* working Windows, X11 and Wayland

* fix issue 32

Co-authored-by: Carlos Aragones <>
Co-authored-by: caragones <carlos.aragonesmartinez-external@gemalto.com>
This commit is contained in:
Carlos Aragonés
2020-03-06 07:06:54 +01:00
committed by GitHub
parent 4286f055c6
commit 6b30baa7e4
18 changed files with 1157 additions and 1046 deletions

View File

@ -14,47 +14,47 @@ extern "C" {
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Create a window that is used to display the buffer sent into the mfb_update function, returns 0 if fails
struct Window * mfb_open(const char *title, unsigned width, unsigned height);
struct Window * mfb_open_ex(const char *title, unsigned width, unsigned height, unsigned flags);
struct mfb_window * mfb_open(const char *title, unsigned width, unsigned height);
struct mfb_window * mfb_open_ex(const char *title, unsigned width, unsigned height, unsigned flags);
// Update the display
// Input buffer is assumed to be a 32-bit buffer of the size given in the open call
// Will return a negative status if something went wrong or the user want to exit
// Also updates the window events
UpdateState mfb_update(struct Window *window, void *buffer);
mfb_update_state mfb_update(struct mfb_window *window, void *buffer);
// Only updates the window events
UpdateState mfb_update_events(struct Window *window);
mfb_update_state mfb_update_events(struct mfb_window *window);
// Close the window
void mfb_close(struct Window *window);
void mfb_close(struct mfb_window *window);
// Set user data
void mfb_set_user_data(struct Window *window, void *user_data);
void * mfb_get_user_data(struct Window *window);
void mfb_set_user_data(struct mfb_window *window, void *user_data);
void * mfb_get_user_data(struct mfb_window *window);
// Set viewport (useful when resize)
bool mfb_set_viewport(struct Window *window, unsigned offset_x, unsigned offset_y, unsigned width, unsigned height);
bool mfb_set_viewport(struct mfb_window *window, unsigned offset_x, unsigned offset_y, unsigned width, unsigned height);
void mfb_set_active_callback(struct Window *window, mfb_active_func callback);
void mfb_set_resize_callback(struct Window *window, mfb_resize_func callback);
void mfb_set_keyboard_callback(struct Window *window, mfb_keyboard_func callback);
void mfb_set_char_input_callback(struct Window *window, mfb_char_input_func callback);
void mfb_set_mouse_button_callback(struct Window *window, mfb_mouse_button_func callback);
void mfb_set_mouse_move_callback(struct Window *window, mfb_mouse_move_func callback);
void mfb_set_mouse_scroll_callback(struct Window *window, mfb_mouse_scroll_func callback);
void mfb_set_active_callback(struct mfb_window *window, mfb_active_func callback);
void mfb_set_resize_callback(struct mfb_window *window, mfb_resize_func callback);
void mfb_set_keyboard_callback(struct mfb_window *window, mfb_keyboard_func callback);
void mfb_set_char_input_callback(struct mfb_window *window, mfb_char_input_func callback);
void mfb_set_mouse_button_callback(struct mfb_window *window, mfb_mouse_button_func callback);
void mfb_set_mouse_move_callback(struct mfb_window *window, mfb_mouse_move_func callback);
void mfb_set_mouse_scroll_callback(struct mfb_window *window, mfb_mouse_scroll_func callback);
const char * mfb_get_key_name(Key key);
const char * mfb_get_key_name(mfb_key key);
bool mfb_is_window_active(struct Window *window);
unsigned mfb_get_window_width(struct Window *window);
unsigned mfb_get_window_height(struct Window *window);
int mfb_get_mouse_x(struct Window *window); // Last mouse pos X
int mfb_get_mouse_y(struct Window *window); // Last mouse pos Y
float mfb_get_mouse_scroll_x(struct Window *window); // Mouse wheel X as a sum. When you call this function it resets.
float mfb_get_mouse_scroll_y(struct Window *window); // Mouse wheel Y as a sum. When you call this function it resets.
const uint8_t * mfb_get_mouse_button_buffer(struct Window *window); // One byte for every button. Press (1), Release 0. (up to 8 buttons)
const uint8_t * mfb_get_key_buffer(struct Window *window); // One byte for every key. Press (1), Release 0.
bool mfb_is_window_active(struct mfb_window *window);
unsigned mfb_get_window_width(struct mfb_window *window);
unsigned mfb_get_window_height(struct mfb_window *window);
int mfb_get_mouse_x(struct mfb_window *window); // Last mouse pos X
int mfb_get_mouse_y(struct mfb_window *window); // Last mouse pos Y
float mfb_get_mouse_scroll_x(struct mfb_window *window); // Mouse wheel X as a sum. When you call this function it resets.
float mfb_get_mouse_scroll_y(struct mfb_window *window); // Mouse wheel Y as a sum. When you call this function it resets.
const uint8_t * mfb_get_mouse_button_buffer(struct mfb_window *window); // One byte for every button. Press (1), Release 0. (up to 8 buttons)
const uint8_t * mfb_get_key_buffer(struct mfb_window *window); // One byte for every key. Press (1), Release 0.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -6,131 +6,131 @@
#include "MiniFB.h"
template <class T>
void mfb_set_active_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, bool));
void mfb_set_active_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *, bool));
template <class T>
void mfb_set_resize_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, int, int));
void mfb_set_resize_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *, int, int));
template <class T>
void mfb_set_keyboard_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, Key, KeyMod, bool));
void mfb_set_keyboard_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *, mfb_key, mfb_key_mod, bool));
template <class T>
void mfb_set_char_input_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, unsigned int));
void mfb_set_char_input_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *, unsigned int));
template <class T>
void mfb_set_mouse_button_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, MouseButton, KeyMod, bool));
void mfb_set_mouse_button_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *, mfb_mouse_button, mfb_key_mod, bool));
template <class T>
void mfb_set_mouse_move_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, int, int));
void mfb_set_mouse_move_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *, int, int));
template <class T>
void mfb_set_mouse_scroll_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, KeyMod, float, float));
void mfb_set_mouse_scroll_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *, mfb_key_mod, float, float));
//-------------------------------------
// To avoid clumsy hands
//-------------------------------------
class Stub {
Stub() : m_window(0x0) {}
class mfb_stub {
mfb_stub() : m_window(0x0) {}
template <class T>
friend void mfb_set_active_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, bool));
friend void mfb_set_active_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *, bool));
template <class T>
friend void mfb_set_resize_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, int, int));
friend void mfb_set_resize_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *, int, int));
template <class T>
friend void mfb_set_mouse_button_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, MouseButton, KeyMod, bool));
friend void mfb_set_mouse_button_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *, mfb_mouse_button, mfb_key_mod, bool));
template <class T>
friend void mfb_set_keyboard_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, Key, KeyMod, bool));
friend void mfb_set_keyboard_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *, mfb_key, mfb_key_mod, bool));
template <class T>
friend void mfb_set_char_input_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, unsigned int));
friend void mfb_set_char_input_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *, unsigned int));
template <class T>
friend void mfb_set_mouse_button_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, MouseButton, KeyMod, bool));
friend void mfb_set_mouse_button_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *, mfb_mouse_button, mfb_key_mod, bool));
template <class T>
friend void mfb_set_mouse_move_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, int, int));
friend void mfb_set_mouse_move_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *, int, int));
template <class T>
friend void mfb_set_mouse_scroll_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, KeyMod, float, float));
friend void mfb_set_mouse_scroll_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *, mfb_key_mod, float, float));
static Stub *GetInstance(struct Window *window);
static mfb_stub *GetInstance(struct mfb_window *window);
static void active_stub(struct Window *window, bool isActive);
static void resize_stub(struct Window *window, int width, int height);
static void keyboard_stub(struct Window *window, Key key, KeyMod mod, bool isPressed);
static void char_input_stub(struct Window *window, unsigned int);
static void mouse_btn_stub(struct Window *window, MouseButton button, KeyMod mod, bool isPressed);
static void mouse_move_stub(struct Window *window, int x, int y);
static void scroll_stub(struct Window *window, KeyMod mod, float deltaX, float deltaY);
static void active_stub(struct mfb_window *window, bool isActive);
static void resize_stub(struct mfb_window *window, int width, int height);
static void keyboard_stub(struct mfb_window *window, mfb_key key, mfb_key_mod mod, bool isPressed);
static void char_input_stub(struct mfb_window *window, unsigned int);
static void mouse_btn_stub(struct mfb_window *window, mfb_mouse_button button, mfb_key_mod mod, bool isPressed);
static void mouse_move_stub(struct mfb_window *window, int x, int y);
static void scroll_stub(struct mfb_window *window, mfb_key_mod mod, float deltaX, float deltaY);
struct Window *m_window;
std::function<void(struct Window *window, bool)> m_active;
std::function<void(struct Window *window, int, int)> m_resize;
std::function<void(struct Window *window, Key, KeyMod, bool)> m_keyboard;
std::function<void(struct Window *window, unsigned int)> m_char_input;
std::function<void(struct Window *window, MouseButton, KeyMod, bool)> m_mouse_btn;
std::function<void(struct Window *window, int, int)> m_mouse_move;
std::function<void(struct Window *window, KeyMod, float, float)> m_scroll;
struct mfb_window *m_window;
std::function<void(struct mfb_window *window, bool)> m_active;
std::function<void(struct mfb_window *window, int, int)> m_resize;
std::function<void(struct mfb_window *window, mfb_key, mfb_key_mod, bool)> m_keyboard;
std::function<void(struct mfb_window *window, unsigned int)> m_char_input;
std::function<void(struct mfb_window *window, mfb_mouse_button, mfb_key_mod, bool)> m_mouse_btn;
std::function<void(struct mfb_window *window, int, int)> m_mouse_move;
std::function<void(struct mfb_window *window, mfb_key_mod, float, float)> m_scroll;
};
//-------------------------------------
template <class T>
inline void mfb_set_active_callback(struct Window *window, T *obj, void (T::*method)(struct Window *window, bool)) {
inline void mfb_set_active_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *window, bool)) {
using namespace std::placeholders;
Stub *stub = Stub::GetInstance(window);
mfb_stub *stub = mfb_stub::GetInstance(window);
stub->m_active = std::bind(method, obj, _1, _2);
mfb_set_active_callback(window, Stub::active_stub);
mfb_set_active_callback(window, mfb_stub::active_stub);
}
template <class T>
inline void mfb_set_resize_callback(struct Window *window, T *obj, void (T::*method)(struct Window *window, int, int)) {
inline void mfb_set_resize_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *window, int, int)) {
using namespace std::placeholders;
Stub *stub = Stub::GetInstance(window);
mfb_stub *stub = mfb_stub::GetInstance(window);
stub->m_resize = std::bind(method, obj, _1, _2, _3);
mfb_set_resize_callback(window, Stub::resize_stub);
mfb_set_resize_callback(window, mfb_stub::resize_stub);
}
template <class T>
inline void mfb_set_keyboard_callback(struct Window *window, T *obj, void (T::*method)(struct Window *window, Key, KeyMod, bool)) {
inline void mfb_set_keyboard_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *window, mfb_key, mfb_key_mod, bool)) {
using namespace std::placeholders;
Stub *stub = Stub::GetInstance(window);
mfb_stub *stub = mfb_stub::GetInstance(window);
stub->m_keyboard = std::bind(method, obj, _1, _2, _3, _4);
mfb_set_keyboard_callback(window, Stub::keyboard_stub);
mfb_set_keyboard_callback(window, mfb_stub::keyboard_stub);
}
template <class T>
inline void mfb_set_char_input_callback(struct Window *window, T *obj, void (T::*method)(struct Window *window, unsigned int)) {
inline void mfb_set_char_input_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *window, unsigned int)) {
using namespace std::placeholders;
Stub *stub = Stub::GetInstance(window);
mfb_stub *stub = mfb_stub::GetInstance(window);
stub->m_char_input = std::bind(method, obj, _1, _2);
mfb_set_char_input_callback(window, Stub::char_input_stub);
mfb_set_char_input_callback(window, mfb_stub::char_input_stub);
}
template <class T>
inline void mfb_set_mouse_button_callback(struct Window *window, T *obj, void (T::*method)(struct Window *window, MouseButton, KeyMod, bool)) {
inline void mfb_set_mouse_button_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *window, mfb_mouse_button, mfb_key_mod, bool)) {
using namespace std::placeholders;
Stub *stub = Stub::GetInstance(window);
mfb_stub *stub = mfb_stub::GetInstance(window);
stub->m_mouse_btn = std::bind(method, obj, _1, _2, _3, _4);
mfb_set_mouse_button_callback(window, Stub::mouse_btn_stub);
mfb_set_mouse_button_callback(window, mfb_stub::mouse_btn_stub);
}
template <class T>
inline void mfb_set_mouse_move_callback(struct Window *window, T *obj, void (T::*method)(struct Window *window, int, int)) {
inline void mfb_set_mouse_move_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *window, int, int)) {
using namespace std::placeholders;
Stub *stub = Stub::GetInstance(window);
mfb_stub *stub = mfb_stub::GetInstance(window);
stub->m_mouse_move = std::bind(method, obj, _1, _2, _3);
mfb_set_mouse_move_callback(window, Stub::mouse_move_stub);
mfb_set_mouse_move_callback(window, mfb_stub::mouse_move_stub);
}
template <class T>
inline void mfb_set_mouse_scroll_callback(struct Window *window, T *obj, void (T::*method)(struct Window *window, KeyMod, float, float)) {
inline void mfb_set_mouse_scroll_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *window, mfb_key_mod, float, float)) {
using namespace std::placeholders;
Stub *stub = Stub::GetInstance(window);
mfb_stub *stub = mfb_stub::GetInstance(window);
stub->m_scroll = std::bind(method, obj, _1, _2, _3, _4);
mfb_set_mouse_scroll_callback(window, Stub::scroll_stub);
mfb_set_mouse_scroll_callback(window, mfb_stub::scroll_stub);
}
#endif

View File

@ -10,7 +10,7 @@ typedef enum {
STATE_INVALID_WINDOW = -2,
STATE_INVALID_BUFFER = -3,
STATE_INTERNAL_ERROR = -4,
} UpdateState;
} mfb_update_state;
typedef enum {
MOUSE_BTN_0, // No mouse button
@ -21,7 +21,7 @@ typedef enum {
MOUSE_BTN_5,
MOUSE_BTN_6,
MOUSE_BTN_7,
} MouseButton;
} mfb_mouse_button;
#define MOUSE_LEFT MOUSE_BTN_1
#define MOUSE_RIGHT MOUSE_BTN_2
#define MOUSE_MIDDLE MOUSE_BTN_3
@ -150,7 +150,7 @@ typedef enum {
KB_KEY_RIGHT_ALT = 346,
KB_KEY_RIGHT_SUPER = 347,
KB_KEY_MENU = 348
} Key;
} mfb_key;
#define KB_KEY_LAST KB_KEY_MENU
typedef enum {
@ -160,7 +160,7 @@ typedef enum {
KB_MOD_SUPER = 0x0008,
KB_MOD_CAPS_LOCK = 0x0010,
KB_MOD_NUM_LOCK = 0x0020
} KeyMod;
} mfb_key_mod;
typedef enum {
WF_RESIZABLE = 0x01,
@ -168,17 +168,17 @@ typedef enum {
WF_FULLSCREEN_DESKTOP = 0x04,
WF_BORDERLESS = 0x08,
WF_ALWAYS_ON_TOP = 0x10,
} WindowFlags;
} mfb_window_flags;
// Opaque pointer
struct Window;
struct mfb_window;
// Event callbacks
typedef void(*mfb_active_func)(struct Window *window, bool isActive);
typedef void(*mfb_resize_func)(struct Window *window, int width, int height);
typedef void(*mfb_keyboard_func)(struct Window *window, Key key, KeyMod mod, bool isPressed);
typedef void(*mfb_char_input_func)(struct Window *window, unsigned int code);
typedef void(*mfb_mouse_button_func)(struct Window *window, MouseButton button, KeyMod mod, bool isPressed);
typedef void(*mfb_mouse_move_func)(struct Window *window, int x, int y);
typedef void(*mfb_mouse_scroll_func)(struct Window *window, KeyMod mod, float deltaX, float deltaY);
typedef void(*mfb_active_func)(struct mfb_window *window, bool isActive);
typedef void(*mfb_resize_func)(struct mfb_window *window, int width, int height);
typedef void(*mfb_keyboard_func)(struct mfb_window *window, mfb_key key, mfb_key_mod mod, bool isPressed);
typedef void(*mfb_char_input_func)(struct mfb_window *window, unsigned int code);
typedef void(*mfb_mouse_button_func)(struct mfb_window *window, mfb_mouse_button button, mfb_key_mod mod, bool isPressed);
typedef void(*mfb_mouse_move_func)(struct mfb_window *window, int x, int y);
typedef void(*mfb_mouse_scroll_func)(struct mfb_window *window, mfb_key_mod mod, float deltaX, float deltaY);