Direct mode (#23)

* update documentation

* Fix typo

* Added some examples

* changed window names

* Minor fix

* renamed callback setters (added _set_)
Direct / Poll Mode for asking events:
 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_scrool_x(struct Window *window);     // Mouse wheel X as a sum. When you call this function it resets.
float           mfb_get_mouse_scrool_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.
const uint8_t * mfb_get_key_buffer(struct Window *window);           // One byte for every key. Press (1), Release 0.

* Minor fixes

* Fixes related to mouse poll

* Minor fix on Win64
This commit is contained in:
Carlos Aragonés
2019-11-14 16:18:42 +01:00
committed by Daniel Collin
parent 25a440f822
commit cdaa54f5d6
17 changed files with 732 additions and 602 deletions

View File

@ -9,37 +9,47 @@ extern "C" {
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define MFB_RGB(r, g, b) (((unsigned int) r) << 16) | (((unsigned int) g) << 8) | (b)
#define MFB_RGB(r, g, b) (((uint32_t) r) << 16) | (((uint32_t) g) << 8) | ((uint32_t) b)
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 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, int width, int height);
struct Window *mfb_open_ex(const char *title, int width, int height, int flags);
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);
// 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.
UpdateState mfb_update(struct Window *window, void* buffer);
UpdateState mfb_update(struct Window *window, void *buffer);
// Close the window
void mfb_close(struct Window *window);
void mfb_close(struct 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 Window *window, void *user_data);
void * mfb_get_user_data(struct 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 Window *window, unsigned offset_x, unsigned offset_y, unsigned width, unsigned height);
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);
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);
const char *mfb_get_key_name(Key key);
const char * mfb_get_key_name(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_scrool_x(struct Window *window); // Mouse wheel X as a sum. When you call this function it resets.
float mfb_get_mouse_scrool_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.
const uint8_t * mfb_get_key_buffer(struct Window *window); // One byte for every key. Press (1), Release 0.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -6,46 +6,48 @@
#include "MiniFB.h"
template <class T>
void mfb_active_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, bool));
void mfb_set_active_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, bool));
template <class T>
void mfb_resize_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, int, int));
void mfb_set_resize_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, int, int));
template <class T>
void mfb_keyboard_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, Key, KeyMod, bool));
void mfb_set_keyboard_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, Key, KeyMod, bool));
template <class T>
void mfb_char_input_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, unsigned int));
void mfb_set_char_input_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, unsigned int));
template <class T>
void mfb_mouse_button_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, MouseButton, KeyMod, bool));
void mfb_set_mouse_button_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, MouseButton, KeyMod, bool));
template <class T>
void mfb_mouse_move_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, int, int));
void mfb_set_mouse_move_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, int, int));
template <class T>
void mfb_mouse_scroll_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, KeyMod, float, float));
void mfb_set_mouse_scroll_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, KeyMod, float, float));
//-------------------------------------
// To avoid clumsy hands
//-------------------------------------
class Stub {
Stub() : m_window(0x0) {}
template <class T>
friend void mfb_active_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, bool));
friend void mfb_set_active_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, bool));
template <class T>
friend void mfb_resize_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, int, int));
friend void mfb_set_resize_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, int, int));
template <class T>
friend void mfb_mouse_button_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, MouseButton, KeyMod, bool));
friend void mfb_set_mouse_button_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, MouseButton, KeyMod, bool));
template <class T>
friend void mfb_keyboard_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, Key, KeyMod, bool));
friend void mfb_set_keyboard_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, Key, KeyMod, bool));
template <class T>
friend void mfb_char_input_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, unsigned int));
friend void mfb_set_char_input_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, unsigned int));
template <class T>
friend void mfb_mouse_button_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, MouseButton, KeyMod, bool));
friend void mfb_set_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(struct Window *window, T *obj, void (T::*method)(struct Window *, int, int));
friend void mfb_set_mouse_move_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, int, int));
template <class T>
friend void mfb_mouse_scroll_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, KeyMod, float, float));
friend void mfb_set_mouse_scroll_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, KeyMod, float, float));
static Stub *GetInstance(struct Window *window);
@ -69,66 +71,66 @@ class Stub {
//-------------------------------------
template <class T>
inline void mfb_active_callback(struct Window *window, T *obj, void (T::*method)(struct Window *window, bool)) {
inline void mfb_set_active_callback(struct Window *window, T *obj, void (T::*method)(struct Window *window, bool)) {
using namespace std::placeholders;
Stub *stub = Stub::GetInstance(window);
stub->m_active = std::bind(method, obj, _1, _2);
mfb_active_callback(window, Stub::active_stub);
mfb_set_active_callback(window, Stub::active_stub);
}
template <class T>
inline void mfb_resize_callback(struct Window *window, T *obj, void (T::*method)(struct Window *window, int, int)) {
inline void mfb_set_resize_callback(struct Window *window, T *obj, void (T::*method)(struct Window *window, int, int)) {
using namespace std::placeholders;
Stub *stub = Stub::GetInstance(window);
stub->m_resize = std::bind(method, obj, _1, _2, _3);
mfb_resize_callback(window, Stub::resize_stub);
mfb_set_resize_callback(window, Stub::resize_stub);
}
template <class T>
inline void mfb_keyboard_callback(struct Window *window, T *obj, void (T::*method)(struct Window *window, Key, KeyMod, bool)) {
inline void mfb_set_keyboard_callback(struct Window *window, T *obj, void (T::*method)(struct Window *window, Key, KeyMod, bool)) {
using namespace std::placeholders;
Stub *stub = Stub::GetInstance(window);
stub->m_keyboard = std::bind(method, obj, _1, _2, _3, _4);
mfb_keyboard_callback(window, Stub::keyboard_stub);
mfb_set_keyboard_callback(window, Stub::keyboard_stub);
}
template <class T>
inline void mfb_char_input_callback(struct Window *window, T *obj, void (T::*method)(struct Window *window, unsigned int)) {
inline void mfb_set_char_input_callback(struct Window *window, T *obj, void (T::*method)(struct Window *window, unsigned int)) {
using namespace std::placeholders;
Stub *stub = Stub::GetInstance(window);
stub->m_char_input = std::bind(method, obj, _1, _2);
mfb_char_input_callback(window, Stub::char_input_stub);
mfb_set_char_input_callback(window, Stub::char_input_stub);
}
template <class T>
inline void mfb_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 Window *window, T *obj, void (T::*method)(struct Window *window, MouseButton, KeyMod, bool)) {
using namespace std::placeholders;
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);
mfb_set_mouse_button_callback(window, Stub::mouse_btn_stub);
}
template <class T>
inline void mfb_mouse_move_callback(struct Window *window, T *obj, void (T::*method)(struct Window *window, int, int)) {
inline void mfb_set_mouse_move_callback(struct Window *window, T *obj, void (T::*method)(struct Window *window, int, int)) {
using namespace std::placeholders;
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);
mfb_set_mouse_move_callback(window, Stub::mouse_move_stub);
}
template <class T>
inline void mfb_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 Window *window, T *obj, void (T::*method)(struct Window *window, KeyMod, float, float)) {
using namespace std::placeholders;
Stub *stub = Stub::GetInstance(window);
stub->m_scroll = std::bind(method, obj, _1, _2, _3, _4);
mfb_mouse_scroll_callback(window, Stub::scroll_stub);
mfb_set_mouse_scroll_callback(window, Stub::scroll_stub);
}
#endif

View File

@ -1,5 +1,6 @@
#pragma once
#include <stdint.h>
#include <stdbool.h>
// Enums
@ -20,12 +21,10 @@ typedef enum {
MOUSE_BTN_5,
MOUSE_BTN_6,
MOUSE_BTN_7,
MOUSE_BTN_8
} MouseButton;
#define MOUSE_LAST MOUSE_BTN_8
#define MOUSE_LEFT MOUSE_BTN_0
#define MOUSE_RIGHT MOUSE_BTN_1
#define MOUSE_MIDDLE MOUSE_BTN_2
#define MOUSE_LEFT MOUSE_BTN_1
#define MOUSE_RIGHT MOUSE_BTN_2
#define MOUSE_MIDDLE MOUSE_BTN_3
typedef enum {
KB_KEY_UNKNOWN = -1,
@ -155,12 +154,12 @@ typedef enum {
#define KB_KEY_LAST KB_KEY_MENU
typedef enum {
KB_MOD_SHIFT = 0x0001,
KB_MOD_CONTROL = 0x0002,
KB_MOD_ALT = 0x0004,
KB_MOD_SUPER = 0x0008,
KB_MOD_CAPS_LOCK = 0x0010,
KB_MOD_NUM_LOCK = 0x0020
KB_MOD_SHIFT = 0x0001,
KB_MOD_CONTROL = 0x0002,
KB_MOD_ALT = 0x0004,
KB_MOD_SUPER = 0x0008,
KB_MOD_CAPS_LOCK = 0x0010,
KB_MOD_NUM_LOCK = 0x0020
} KeyMod;
typedef enum {
@ -179,7 +178,7 @@ 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_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);