Add multiple windows2 (#19)
* MacOSX with input events and mfb_set_viewport * Windows with input events and mfb_set_viewport * Minor changes in macosx and windows * X11 with input and mfb_set_viewport * Minor changes on X11 * Fix bug in X11 resize event * Fix bug in windows resize * added scale image to X11 * Added stretch_image with & without bilinear interpolation * moved MiniFB_internal.h * Added wayland events * minor changes * unify a bit the window structs * More work on wayland * Added cmake file * minor fix on windows * modified test * Fix on wayland. Unset wayland as default for linux * Use stdbool instead of our own macro eBool Remove prefix e from all enums * Remove _ex sufix in common files * merge X11MiniFB_ex.c into X11MiniFB.c * Merge WaylandMiniFB_ex.c into WaylandMiniFB.c * Add user_data to callbacks * Merge WinMiniFB_ex.c into WinMiniFB.c * Some minor changes on Windows * Fix bug on Wayland * Multiple Windows for MacOS X & Linux * Multiple Windows for Windows Refactor * Refactor * Added mfb_get_key_name * remove some warnings * Events per window working on MacOS & Linux (maybe Windows) * Fix on Windows * Added default key callback on macos & wayland * Unify keyboard_default on all platforms * keyboard_default on all platforms
This commit is contained in:

committed by
Daniel Collin

parent
8b6148cf97
commit
21d9377822
@ -9,43 +9,35 @@ extern "C" {
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#define MFB_RGB(r, g, b) (((unsigned int)r) << 16) | (((unsigned int)g) << 8) | b
|
||||
#define MFB_RGB(r, g, b) (((unsigned int) r) << 16) | (((unsigned int) g) << 8) | (b)
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// Create a window that is used to display the buffer sent into the mfb_update function, returns 0 if fails
|
||||
int mfb_open(const char* name, int width, int height);
|
||||
int mfb_open_ex(const char* name, int width, int height, int flags);
|
||||
struct Window *mfb_open(const char *title, int width, int height);
|
||||
struct Window *mfb_open_ex(const char *title, int width, int height, int flags);
|
||||
|
||||
// Update the display. Input buffer is assumed to be a 32-bit buffer of the size given in the open call
|
||||
// Will return -1 when ESC key is pressed (later on will return keycode and -1 on other close signal)
|
||||
int mfb_update(void* buffer);
|
||||
// Will return a negative status if something went wrong or the user want to exit.
|
||||
UpdateState mfb_update(struct Window *window, void* buffer);
|
||||
|
||||
// Close the window
|
||||
void mfb_close();
|
||||
void mfb_close(struct Window *window);
|
||||
|
||||
// Set user data
|
||||
void mfb_set_user_data(void *user_data);
|
||||
void mfb_set_user_data(struct Window *window, void *user_data);
|
||||
void *mfb_get_user_data(struct Window *window);
|
||||
|
||||
// Set viewport (useful when resize)
|
||||
bool mfb_set_viewport(unsigned offset_x, unsigned offset_y, unsigned width, unsigned height);
|
||||
bool mfb_set_viewport(struct Window *window, unsigned offset_x, unsigned offset_y, unsigned width, unsigned height);
|
||||
|
||||
// Event callbacks
|
||||
typedef void(*mfb_active_func)(void *user_data, bool isActive);
|
||||
typedef void(*mfb_resize_func)(void *user_data, int width, int height);
|
||||
typedef void(*mfb_keyboard_func)(void *user_data, Key key, KeyMod mod, bool isPressed);
|
||||
typedef void(*mfb_char_input_func)(void *user_data, unsigned int code);
|
||||
typedef void(*mfb_mouse_btn_func)(void *user_data, MouseButton button, KeyMod mod, bool isPressed);
|
||||
typedef void(*mfb_mouse_move_func)(void *user_data, int x, int y);
|
||||
typedef void(*mfb_mouse_scroll_func)(void *user_data, KeyMod mod, float deltaX, float deltaY);
|
||||
|
||||
void mfb_active_callback(mfb_active_func callback);
|
||||
void mfb_resize_callback(mfb_resize_func callback);
|
||||
void mfb_keyboard_callback(mfb_keyboard_func callback);
|
||||
void mfb_char_input_callback(mfb_char_input_func callback);
|
||||
void mfb_mouse_button_callback(mfb_mouse_btn_func callback);
|
||||
void mfb_mouse_move_callback(mfb_mouse_move_func callback);
|
||||
void mfb_mouse_scroll_callback(mfb_mouse_scroll_func callback);
|
||||
void mfb_active_callback(struct Window *window, mfb_active_func callback);
|
||||
void mfb_resize_callback(struct Window *window, mfb_resize_func callback);
|
||||
void mfb_keyboard_callback(struct Window *window, mfb_keyboard_func callback);
|
||||
void mfb_char_input_callback(struct Window *window, mfb_char_input_func callback);
|
||||
void mfb_mouse_button_callback(struct Window *window, mfb_mouse_btn_func callback);
|
||||
void mfb_mouse_move_callback(struct Window *window, mfb_mouse_move_func callback);
|
||||
void mfb_mouse_scroll_callback(struct Window *window, mfb_mouse_scroll_func callback);
|
||||
|
||||
const char *mfb_get_key_name(Key key);
|
||||
|
||||
|
@ -6,119 +6,129 @@
|
||||
#include "MiniFB.h"
|
||||
|
||||
template <class T>
|
||||
void mfb_active_callback(T *obj, void (T::*method)(void *, bool));
|
||||
void mfb_active_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, bool));
|
||||
|
||||
template <class T>
|
||||
void mfb_resize_callback(T *obj, void (T::*method)(void *, int, int));
|
||||
void mfb_resize_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, int, int));
|
||||
|
||||
template <class T>
|
||||
void mfb_keyboard_callback(T *obj, void (T::*method)(void *, Key, KeyMod, bool));
|
||||
void mfb_keyboard_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, Key, KeyMod, bool));
|
||||
|
||||
template <class T>
|
||||
void mfb_char_input_callback(T *obj, void (T::*method)(void *, unsigned int));
|
||||
void mfb_char_input_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, unsigned int));
|
||||
|
||||
template <class T>
|
||||
void mfb_mouse_button_callback(T *obj, void (T::*method)(void *, MouseButton, KeyMod, bool));
|
||||
void mfb_mouse_button_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, MouseButton, KeyMod, bool));
|
||||
|
||||
template <class T>
|
||||
void mfb_mouse_move_callback(T *obj, void (T::*method)(void *, int, int));
|
||||
void mfb_mouse_move_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, int, int));
|
||||
|
||||
template <class T>
|
||||
void mfb_mouse_scroll_callback(T *obj, void (T::*method)(void *, KeyMod, float, float));
|
||||
void mfb_mouse_scroll_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, KeyMod, float, float));
|
||||
|
||||
//-------------------------------------
|
||||
// To avoid clumsy hands
|
||||
//-------------------------------------
|
||||
class Stub {
|
||||
template <class T>
|
||||
friend void mfb_active_callback(T *obj, void (T::*method)(void *, bool));
|
||||
friend void mfb_active_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, bool));
|
||||
template <class T>
|
||||
friend void mfb_resize_callback(T *obj, void (T::*method)(void *, int, int));
|
||||
friend void mfb_resize_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, int, int));
|
||||
template <class T>
|
||||
friend void mfb_mouse_button_callback(T *obj, void (T::*method)(void *, MouseButton, KeyMod, bool));
|
||||
friend void mfb_mouse_button_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, MouseButton, KeyMod, bool));
|
||||
template <class T>
|
||||
friend void mfb_keyboard_callback(T *obj, void (T::*method)(void *, Key, KeyMod, bool));
|
||||
friend void mfb_keyboard_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, Key, KeyMod, bool));
|
||||
template <class T>
|
||||
friend void mfb_char_input_callback(T *obj, void (T::*method)(void *, unsigned int));
|
||||
friend void mfb_char_input_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, unsigned int));
|
||||
template <class T>
|
||||
friend void mfb_mouse_button_callback(T *obj, void (T::*method)(void *, MouseButton, KeyMod, bool));
|
||||
friend void mfb_mouse_button_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, MouseButton, KeyMod, bool));
|
||||
template <class T>
|
||||
friend void mfb_mouse_move_callback(T *obj, void (T::*method)(void *, int, int));
|
||||
friend void mfb_mouse_move_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, int, int));
|
||||
template <class T>
|
||||
friend void mfb_mouse_scroll_callback(T *obj, void (T::*method)(void *, KeyMod, float, float));
|
||||
friend void mfb_mouse_scroll_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, KeyMod, float, float));
|
||||
|
||||
static void active_stub(void *user_data, bool isActive);
|
||||
static void resize_stub(void *user_data, int width, int height);
|
||||
static void keyboard_stub(void *user_data, Key key, KeyMod mod, bool isPressed);
|
||||
static void char_input_stub(void *user_data, unsigned int);
|
||||
static void mouse_btn_stub(void *user_data, MouseButton button, KeyMod mod, bool isPressed);
|
||||
static void mouse_move_stub(void *user_data, int x, int y);
|
||||
static void scroll_stub(void *user_data, KeyMod mod, float deltaX, float deltaY);
|
||||
static Stub *GetInstance(struct Window *window);
|
||||
|
||||
static std::function<void(void *user_data, bool)> m_active;
|
||||
static std::function<void(void *user_data, int, int)> m_resize;
|
||||
static std::function<void(void *user_data, Key, KeyMod, bool)> m_keyboard;
|
||||
static std::function<void(void *user_data, unsigned int)> m_char_input;
|
||||
static std::function<void(void *user_data, MouseButton, KeyMod, bool)> m_mouse_btn;
|
||||
static std::function<void(void *user_data, int, int)> m_mouse_move;
|
||||
static std::function<void(void *user_data, KeyMod, float, float)> m_scroll;
|
||||
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);
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
//-------------------------------------
|
||||
template <class T>
|
||||
inline void mfb_active_callback(T *obj, void (T::*method)(void *user_data, bool)) {
|
||||
inline void mfb_active_callback(struct Window *window, T *obj, void (T::*method)(struct Window *window, bool)) {
|
||||
using namespace std::placeholders;
|
||||
|
||||
Stub::m_active = std::bind(method, obj, _1, _2);
|
||||
mfb_active_callback(Stub::active_stub);
|
||||
Stub *stub = Stub::GetInstance(window);
|
||||
stub->m_active = std::bind(method, obj, _1, _2);
|
||||
mfb_active_callback(window, Stub::active_stub);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline void mfb_resize_callback(T *obj, void (T::*method)(void *user_data, int, int)) {
|
||||
inline void mfb_resize_callback(struct Window *window, T *obj, void (T::*method)(struct Window *window, int, int)) {
|
||||
using namespace std::placeholders;
|
||||
|
||||
Stub::m_resize = std::bind(method, obj, _1, _2, _3);
|
||||
mfb_resize_callback(Stub::resize_stub);
|
||||
Stub *stub = Stub::GetInstance(window);
|
||||
stub->m_resize = std::bind(method, obj, _1, _2, _3);
|
||||
mfb_resize_callback(window, Stub::resize_stub);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline void mfb_keyboard_callback(T *obj, void (T::*method)(void *user_data, Key, KeyMod, bool)) {
|
||||
inline void mfb_keyboard_callback(struct Window *window, T *obj, void (T::*method)(struct Window *window, Key, KeyMod, bool)) {
|
||||
using namespace std::placeholders;
|
||||
|
||||
Stub::m_keyboard = std::bind(method, obj, _1, _2, _3, _4);
|
||||
mfb_keyboard_callback(Stub::keyboard_stub);
|
||||
Stub *stub = Stub::GetInstance(window);
|
||||
stub->m_keyboard = std::bind(method, obj, _1, _2, _3, _4);
|
||||
mfb_keyboard_callback(window, Stub::keyboard_stub);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline void mfb_char_input_callback(T *obj, void (T::*method)(void *user_data, unsigned int)) {
|
||||
inline void mfb_char_input_callback(struct Window *window, T *obj, void (T::*method)(struct Window *window, unsigned int)) {
|
||||
using namespace std::placeholders;
|
||||
|
||||
Stub::m_char_input = std::bind(method, obj, _1, _2);
|
||||
mfb_char_input_callback(Stub::char_input_stub);
|
||||
Stub *stub = Stub::GetInstance(window);
|
||||
stub->m_char_input = std::bind(method, obj, _1, _2);
|
||||
mfb_char_input_callback(window, Stub::char_input_stub);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline void mfb_mouse_button_callback(T *obj, void (T::*method)(void *user_data, MouseButton, KeyMod, bool)) {
|
||||
inline void mfb_mouse_button_callback(struct Window *window, T *obj, void (T::*method)(struct Window *window, MouseButton, KeyMod, bool)) {
|
||||
using namespace std::placeholders;
|
||||
|
||||
Stub::m_mouse_btn = std::bind(method, obj, _1, _2, _3, _4);
|
||||
mfb_mouse_button_callback(Stub::mouse_btn_stub);
|
||||
Stub *stub = Stub::GetInstance(window);
|
||||
stub->m_mouse_btn = std::bind(method, obj, _1, _2, _3, _4);
|
||||
mfb_mouse_button_callback(window, Stub::mouse_btn_stub);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline void mfb_mouse_move_callback(T *obj, void (T::*method)(void *user_data, int, int)) {
|
||||
inline void mfb_mouse_move_callback(struct Window *window, T *obj, void (T::*method)(struct Window *window, int, int)) {
|
||||
using namespace std::placeholders;
|
||||
|
||||
Stub::m_mouse_move = std::bind(method, obj, _1, _2, _3);
|
||||
mfb_mouse_move_callback(Stub::mouse_move_stub);
|
||||
Stub *stub = Stub::GetInstance(window);
|
||||
stub->m_mouse_move = std::bind(method, obj, _1, _2, _3);
|
||||
mfb_mouse_move_callback(window, Stub::mouse_move_stub);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline void mfb_mouse_scroll_callback(T *obj, void (T::*method)(void *user_data, KeyMod, float, float)) {
|
||||
inline void mfb_mouse_scroll_callback(struct Window *window, T *obj, void (T::*method)(struct Window *window, KeyMod, float, float)) {
|
||||
using namespace std::placeholders;
|
||||
|
||||
Stub::m_scroll = std::bind(method, obj, _1, _2, _3, _4);
|
||||
mfb_mouse_scroll_callback(Stub::scroll_stub);
|
||||
Stub *stub = Stub::GetInstance(window);
|
||||
stub->m_scroll = std::bind(method, obj, _1, _2, _3, _4);
|
||||
mfb_mouse_scroll_callback(window, Stub::scroll_stub);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -2,6 +2,15 @@
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
// Enums
|
||||
typedef enum {
|
||||
STATE_OK = 0,
|
||||
STATE_EXIT = -1,
|
||||
STATE_INVALID_WINDOW = -2,
|
||||
STATE_INVALID_BUFFER = -3,
|
||||
STATE_INTERNAL_ERROR = -4,
|
||||
} UpdateState;
|
||||
|
||||
typedef enum {
|
||||
MOUSE_BTN_0, // No mouse button
|
||||
MOUSE_BTN_1,
|
||||
@ -161,3 +170,16 @@ typedef enum {
|
||||
WF_BORDERLESS = 0x08,
|
||||
WF_ALWAYS_ON_TOP = 0x10,
|
||||
} WindowFlags;
|
||||
|
||||
// Opaque pointer
|
||||
struct 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_btn_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);
|
||||
|
||||
|
Reference in New Issue
Block a user