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

@ -7,7 +7,8 @@ short int g_keycodes[512] = { 0 };
//-------------------------------------
//-------------------------------------
void mfb_set_active_callback(struct Window *window, mfb_active_func callback) {
void
mfb_set_active_callback(struct mfb_window *window, mfb_active_func callback) {
if(window != 0x0) {
SWindowData *window_data = (SWindowData *) window;
window_data->active_func = callback;
@ -15,7 +16,8 @@ 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_resize_callback(struct mfb_window *window, mfb_resize_func callback) {
if(window != 0x0) {
SWindowData *window_data = (SWindowData *) window;
window_data->resize_func = callback;
@ -23,7 +25,8 @@ 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_keyboard_callback(struct mfb_window *window, mfb_keyboard_func callback) {
if(window != 0x0) {
SWindowData *window_data = (SWindowData *) window;
window_data->keyboard_func = callback;
@ -31,7 +34,8 @@ 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_char_input_callback(struct mfb_window *window, mfb_char_input_func callback) {
if(window != 0x0) {
SWindowData *window_data = (SWindowData *) window;
window_data->char_input_func = callback;
@ -39,7 +43,8 @@ void mfb_set_char_input_callback(struct Window *window, mfb_char_input_func call
}
//-------------------------------------
void mfb_set_mouse_button_callback(struct Window *window, mfb_mouse_button_func callback) {
void
mfb_set_mouse_button_callback(struct mfb_window *window, mfb_mouse_button_func callback) {
if(window != 0x0) {
SWindowData *window_data = (SWindowData *) window;
window_data->mouse_btn_func = callback;
@ -47,7 +52,8 @@ void mfb_set_mouse_button_callback(struct Window *window, mfb_mouse_button_func
}
//-------------------------------------
void mfb_set_mouse_move_callback(struct Window *window, mfb_mouse_move_func callback) {
void
mfb_set_mouse_move_callback(struct mfb_window *window, mfb_mouse_move_func callback) {
if(window != 0x0) {
SWindowData *window_data = (SWindowData *) window;
window_data->mouse_move_func = callback;
@ -55,7 +61,8 @@ void mfb_set_mouse_move_callback(struct Window *window, mfb_mouse_move_func call
}
//-------------------------------------
void mfb_set_mouse_scroll_callback(struct Window *window, mfb_mouse_scroll_func callback) {
void
mfb_set_mouse_scroll_callback(struct mfb_window *window, mfb_mouse_scroll_func callback) {
if(window != 0x0) {
SWindowData *window_data = (SWindowData *) window;
window_data->mouse_wheel_func = callback;
@ -63,7 +70,8 @@ void mfb_set_mouse_scroll_callback(struct Window *window, mfb_mouse_scroll_func
}
//-------------------------------------
void mfb_set_user_data(struct Window *window, void *user_data) {
void
mfb_set_user_data(struct mfb_window *window, void *user_data) {
if(window != 0x0) {
SWindowData *window_data = (SWindowData *) window;
window_data->user_data = user_data;
@ -71,7 +79,8 @@ void mfb_set_user_data(struct Window *window, void *user_data) {
}
//-------------------------------------
void *mfb_get_user_data(struct Window *window) {
void *
mfb_get_user_data(struct mfb_window *window) {
if(window != 0x0) {
SWindowData *window_data = (SWindowData *) window;
return window_data->user_data;
@ -81,7 +90,8 @@ void *mfb_get_user_data(struct Window *window) {
}
//-------------------------------------
void mfb_close(struct Window *window) {
void
mfb_close(struct mfb_window *window) {
if(window != 0x0) {
SWindowData *window_data = (SWindowData *) window;
window_data->close = true;
@ -89,7 +99,8 @@ void mfb_close(struct Window *window) {
}
//-------------------------------------
void keyboard_default(struct Window *window, Key key, KeyMod mod, bool isPressed) {
void
keyboard_default(struct mfb_window *window, mfb_key key, mfb_key_mod mod, bool isPressed) {
kUnused(mod);
kUnused(isPressed);
if (key == KB_KEY_ESCAPE) {
@ -99,7 +110,8 @@ void keyboard_default(struct Window *window, Key key, KeyMod mod, bool isPressed
}
//-------------------------------------
bool mfb_is_window_active(struct Window *window) {
bool
mfb_is_window_active(struct mfb_window *window) {
if(window != 0x0) {
SWindowData *window_data = (SWindowData *) window;
return window_data->is_active;
@ -108,7 +120,8 @@ bool mfb_is_window_active(struct Window *window) {
}
//-------------------------------------
unsigned mfb_get_window_width(struct Window *window) {
unsigned
mfb_get_window_width(struct mfb_window *window) {
if(window != 0x0) {
SWindowData *window_data = (SWindowData *) window;
return window_data->window_width;
@ -117,7 +130,8 @@ unsigned mfb_get_window_width(struct Window *window) {
}
//-------------------------------------
unsigned mfb_get_window_height(struct Window *window) {
unsigned
mfb_get_window_height(struct mfb_window *window) {
if(window != 0x0) {
SWindowData *window_data = (SWindowData *) window;
return window_data->window_height;
@ -126,7 +140,8 @@ unsigned mfb_get_window_height(struct Window *window) {
}
//-------------------------------------
int mfb_get_mouse_x(struct Window *window) {
int
mfb_get_mouse_x(struct mfb_window *window) {
if(window != 0x0) {
SWindowData *window_data = (SWindowData *) window;
return window_data->mouse_pos_x;
@ -135,7 +150,8 @@ int mfb_get_mouse_x(struct Window *window) {
}
//-------------------------------------
int mfb_get_mouse_y(struct Window *window) {
int
mfb_get_mouse_y(struct mfb_window *window) {
if(window != 0x0) {
SWindowData *window_data = (SWindowData *) window;
return window_data->mouse_pos_y;
@ -144,7 +160,8 @@ int mfb_get_mouse_y(struct Window *window) {
}
//-------------------------------------
float mfb_get_mouse_scroll_x(struct Window *window) {
float
mfb_get_mouse_scroll_x(struct mfb_window *window) {
if(window != 0x0) {
SWindowData *window_data = (SWindowData *) window;
return window_data->mouse_wheel_x;
@ -153,7 +170,8 @@ float mfb_get_mouse_scroll_x(struct Window *window) {
}
//-------------------------------------
float mfb_get_mouse_scroll_y(struct Window *window) {
float
mfb_get_mouse_scroll_y(struct mfb_window *window) {
if(window != 0x0) {
SWindowData *window_data = (SWindowData *) window;
return window_data->mouse_wheel_y;
@ -162,7 +180,8 @@ float mfb_get_mouse_scroll_y(struct Window *window) {
}
//-------------------------------------
const uint8_t * mfb_get_mouse_button_buffer(struct Window *window) {
const uint8_t *
mfb_get_mouse_button_buffer(struct mfb_window *window) {
if(window != 0x0) {
SWindowData *window_data = (SWindowData *) window;
return window_data->mouse_button_status;
@ -171,7 +190,8 @@ const uint8_t * mfb_get_mouse_button_buffer(struct Window *window) {
}
//-------------------------------------
const uint8_t * mfb_get_key_buffer(struct Window *window) {
const uint8_t *
mfb_get_key_buffer(struct mfb_window *window) {
if(window != 0x0) {
SWindowData *window_data = (SWindowData *) window;
return window_data->key_status;
@ -179,10 +199,9 @@ const uint8_t * mfb_get_key_buffer(struct Window *window) {
return 0;
}
//-------------------------------------
const char * mfb_get_key_name(Key key) {
const char *
mfb_get_key_name(mfb_key key) {
switch (key)
{

View File

@ -2,53 +2,68 @@
#include <MiniFB_enums.h>
#include <vector>
Stub *
Stub::GetInstance(struct Window *window) {
static std::vector<Stub *> s_instances;
//-------------------------------------
mfb_stub *
mfb_stub::GetInstance(struct mfb_window *window) {
static std::vector<mfb_stub *> s_instances;
for(Stub *instance : s_instances) {
for(mfb_stub *instance : s_instances) {
if(instance->m_window == window) {
return instance;
}
}
s_instances.push_back(new Stub);
s_instances.push_back(new mfb_stub);
s_instances.back()->m_window = window;
return s_instances.back();
}
void Stub::active_stub(struct Window *window, bool isActive) {
Stub *stub = Stub::GetInstance(window);
//-------------------------------------
void
mfb_stub::active_stub(struct mfb_window *window, bool isActive) {
mfb_stub *stub = mfb_stub::GetInstance(window);
stub->m_active(window, isActive);
}
void Stub::resize_stub(struct Window *window, int width, int height) {
Stub *stub = Stub::GetInstance(window);
//-------------------------------------
void
mfb_stub::resize_stub(struct mfb_window *window, int width, int height) {
mfb_stub *stub = mfb_stub::GetInstance(window);
stub->m_resize(window, width, height);
}
void Stub::keyboard_stub(struct Window *window, Key key, KeyMod mod, bool isPressed) {
Stub *stub = Stub::GetInstance(window);
//-------------------------------------
void
mfb_stub::keyboard_stub(struct mfb_window *window, mfb_key key, mfb_key_mod mod, bool isPressed) {
mfb_stub *stub = mfb_stub::GetInstance(window);
stub->m_keyboard(window, key, mod, isPressed);
}
void Stub::char_input_stub(struct Window *window, unsigned int code) {
Stub *stub = Stub::GetInstance(window);
//-------------------------------------
void
mfb_stub::char_input_stub(struct mfb_window *window, unsigned int code) {
mfb_stub *stub = mfb_stub::GetInstance(window);
stub->m_char_input(window, code);
}
void Stub::mouse_btn_stub(struct Window *window, MouseButton button, KeyMod mod, bool isPressed) {
Stub *stub = Stub::GetInstance(window);
//-------------------------------------
void
mfb_stub::mouse_btn_stub(struct mfb_window *window, mfb_mouse_button button, mfb_key_mod mod, bool isPressed) {
mfb_stub *stub = mfb_stub::GetInstance(window);
stub->m_mouse_btn(window, button, mod, isPressed);
}
void Stub::mouse_move_stub(struct Window *window, int x, int y) {
Stub *stub = Stub::GetInstance(window);
//-------------------------------------
void
mfb_stub::mouse_move_stub(struct mfb_window *window, int x, int y) {
mfb_stub *stub = mfb_stub::GetInstance(window);
stub->m_mouse_move(window, x, y);
}
void Stub::scroll_stub(struct Window *window, KeyMod mod, float deltaX, float deltaY) {
Stub *stub = Stub::GetInstance(window);
//-------------------------------------
void
mfb_stub::scroll_stub(struct mfb_window *window, mfb_key_mod mod, float deltaX, float deltaY) {
mfb_stub *stub = mfb_stub::GetInstance(window);
stub->m_scroll(window, mod, deltaX, deltaY);
}

View File

@ -4,7 +4,8 @@
//#define kUseBilinearInterpolation
#if defined(kUseBilinearInterpolation)
static uint32_t interpolate(uint32_t *srcImage, uint32_t x, uint32_t y, uint32_t srcOffsetX, uint32_t srcOffsetY, uint32_t srcWidth, uint32_t srcHeight, uint32_t srcPitch) {
static uint32_t
interpolate(uint32_t *srcImage, uint32_t x, uint32_t y, uint32_t srcOffsetX, uint32_t srcOffsetY, uint32_t srcWidth, uint32_t srcHeight, uint32_t srcPitch) {
uint32_t incX = x + 1 < srcWidth ? 1 : 0;
uint32_t incY = y + 1 < srcHeight ? srcPitch : 0;
uint8_t *p00 = (uint8_t *) &srcImage[(srcOffsetX >> 16)];
@ -38,8 +39,9 @@ static uint32_t interpolate(uint32_t *srcImage, uint32_t x, uint32_t y, uint32_t
#endif
// Only for 32 bits images
void stretch_image(uint32_t *srcImage, uint32_t srcX, uint32_t srcY, uint32_t srcWidth, uint32_t srcHeight, uint32_t srcPitch,
uint32_t *dstImage, uint32_t dstX, uint32_t dstY, uint32_t dstWidth, uint32_t dstHeight, uint32_t dstPitch) {
void
stretch_image(uint32_t *srcImage, uint32_t srcX, uint32_t srcY, uint32_t srcWidth, uint32_t srcHeight, uint32_t srcPitch,
uint32_t *dstImage, uint32_t dstX, uint32_t dstY, uint32_t dstWidth, uint32_t dstHeight, uint32_t dstPitch) {
uint32_t x, y;
uint32_t srcOffsetX, srcOffsetY;

View File

@ -3,7 +3,7 @@
#include "MiniFB.h"
#include "MiniFB_enums.h"
#define kCall(func, ...) if(window_data && window_data->func) window_data->func((struct Window *) window_data, __VA_ARGS__);
#define kCall(func, ...) if(window_data && window_data->func) window_data->func((struct mfb_window *) window_data, __VA_ARGS__);
#define kUnused(var) (void) var;
#if defined(__cplusplus)
@ -12,7 +12,7 @@ extern "C" {
extern short int g_keycodes[512];
void init_keycodes();
void keyboard_default(struct Window *window, Key key, KeyMod mod, bool isPressed);
void keyboard_default(struct mfb_window *window, mfb_key key, mfb_key_mod mod, bool isPressed);
#if defined(__cplusplus)
}

View File

@ -85,7 +85,8 @@ NSString* g_shadersSrc = @
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#if defined(USE_METAL_API)
static bool create_shaders(SWindowData_OSX *window_data_osx) {
static bool
create_shaders(SWindowData_OSX *window_data_osx) {
// Error
NSError* nsError = 0x0;
NSError** nsErrorPtr = &nsError;
@ -136,14 +137,16 @@ static bool create_shaders(SWindowData_OSX *window_data_osx) {
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
struct Window *mfb_open(const char *title, unsigned width, unsigned height)
struct mfb_window *
mfb_open(const char *title, unsigned width, unsigned height)
{
return mfb_open_ex(title, width, height, 0);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
struct Window *mfb_open_ex(const char *title, unsigned width, unsigned height, unsigned flags)
struct mfb_window *
mfb_open_ex(const char *title, unsigned width, unsigned height, unsigned flags)
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
@ -254,7 +257,7 @@ struct Window *mfb_open_ex(const char *title, unsigned width, unsigned height, u
[NSApp finishLaunching];
#endif
mfb_set_keyboard_callback((struct Window *) window_data, keyboard_default);
mfb_set_keyboard_callback((struct mfb_window *) window_data, keyboard_default);
#if defined(USE_METAL_API)
NSLog(@"Window created using Metal API");
@ -264,12 +267,13 @@ struct Window *mfb_open_ex(const char *title, unsigned width, unsigned height, u
[pool drain];
return (struct Window *) window_data;
return (struct mfb_window *) window_data;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static void destroy_window_data(SWindowData *window_data)
static void
destroy_window_data(SWindowData *window_data)
{
if(window_data == 0x0)
return;
@ -293,7 +297,8 @@ static void destroy_window_data(SWindowData *window_data)
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static void update_events(SWindowData *window_data)
static void
update_events(SWindowData *window_data)
{
NSEvent* event;
@ -312,7 +317,8 @@ static void update_events(SWindowData *window_data)
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
UpdateState mfb_update(struct Window *window, void *buffer)
mfb_update_state
mfb_update(struct mfb_window *window, void *buffer)
{
if(window == 0x0) {
return STATE_INVALID_WINDOW;
@ -345,7 +351,8 @@ UpdateState mfb_update(struct Window *window, void *buffer)
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
UpdateState mfb_update_events(struct Window *window)
mfb_update_state
mfb_update_events(struct mfb_window *window)
{
if(window == 0x0) {
return STATE_INVALID_WINDOW;
@ -368,7 +375,8 @@ UpdateState mfb_update_events(struct Window *window)
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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)
{
SWindowData *window_data = (SWindowData *) window;
@ -410,7 +418,8 @@ bool mfb_set_viewport(struct Window *window, unsigned offset_x, unsigned offset_
extern short int g_keycodes[512];
void init_keycodes()
void
init_keycodes()
{
// Clear keys
for (unsigned int i = 0; i < sizeof(g_keycodes) / sizeof(g_keycodes[0]); ++i)

View File

@ -136,7 +136,7 @@ keyboard_key(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t
SWindowData *window_data = (SWindowData *) data;
if(key < 512) {
Key key_code = (Key) g_keycodes[key];
mfb_key key_code = (mfb_key) g_keycodes[key];
bool is_pressed = (bool) (state == WL_KEYBOARD_KEY_STATE_PRESSED);
switch (key_code)
{
@ -174,7 +174,7 @@ keyboard_key(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t
}
window_data->key_status[key_code] = is_pressed;
kCall(keyboard_func, key_code, (KeyMod)window_data->mod_keys, is_pressed);
kCall(keyboard_func, key_code, (mfb_key_mod) window_data->mod_keys, is_pressed);
}
}
@ -210,7 +210,8 @@ keyboard_repeat_info(void *data, struct wl_keyboard *keyboard, int32_t rate, int
kUnused(delay);
}
static const struct wl_keyboard_listener keyboard_listener = {
static const struct
wl_keyboard_listener keyboard_listener = {
.keymap = keyboard_keymap,
.enter = keyboard_enter,
.leave = keyboard_leave,
@ -319,7 +320,7 @@ pointer_button(void *data, struct wl_pointer *pointer, uint32_t serial, uint32_t
//printf("Pointer button '%d'(%d)\n", button, state);
SWindowData *window_data = (SWindowData *) data;
kCall(mouse_btn_func, (MouseButton) (button - BTN_MOUSE + 1), (KeyMod) window_data->mod_keys, state == 1);
kCall(mouse_btn_func, (mfb_mouse_button) (button - BTN_MOUSE + 1), (mfb_key_mod) window_data->mod_keys, state == 1);
}
// Scroll and other axis notifications.
@ -352,10 +353,10 @@ pointer_axis(void *data, struct wl_pointer *pointer, uint32_t time, uint32_t axi
//printf("Pointer handle axis: axis: %d (0x%x)\n", axis, value);
SWindowData *window_data = (SWindowData *) data;
if(axis == 0) {
kCall(mouse_wheel_func, (KeyMod) window_data->mod_keys, 0.0f, -(value / 256.0f));
kCall(mouse_wheel_func, (mfb_key_mod) window_data->mod_keys, 0.0f, -(value / 256.0f));
}
else if(axis == 1) {
kCall(mouse_wheel_func, (KeyMod) window_data->mod_keys, -(value / 256.0f), 0.0f);
kCall(mouse_wheel_func, (mfb_key_mod) window_data->mod_keys, -(value / 256.0f), 0.0f);
}
}
@ -388,7 +389,8 @@ axis_discrete(void *data, struct wl_pointer *pointer, uint32_t axis, int32_t dis
kUnused(discrete);
}
static const struct wl_pointer_listener pointer_listener = {
static const struct
wl_pointer_listener pointer_listener = {
.enter = pointer_enter,
.leave = pointer_leave,
.motion = pointer_motion,
@ -439,7 +441,8 @@ seat_name(void *data, struct wl_seat *seat, const char *name) {
printf("Seat '%s'n", name);
}
static const struct wl_seat_listener seat_listener = {
static const struct
wl_seat_listener seat_listener = {
.capabilities = seat_capabilities,
.name = 0x0,
};
@ -475,7 +478,8 @@ shm_format(void *data, struct wl_shm *shm, uint32_t format)
}
}
static const struct wl_shm_listener shm_listener = {
static const struct
wl_shm_listener shm_listener = {
.format = shm_format
};
@ -515,7 +519,8 @@ registry_global(void *data, struct wl_registry *registry, uint32_t id, char cons
}
}
static const struct wl_registry_listener registry_listener = {
static const struct
wl_registry_listener registry_listener = {
.global = registry_global,
.global_remove = 0x0,
};
@ -552,14 +557,14 @@ static const struct wl_shell_surface_listener shell_surface_listener = {
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
struct Window *
struct mfb_window *
mfb_open_ex(const char *title, unsigned width, unsigned height, unsigned flags) {
// TODO: Not yet
kUnused(flags);
return mfb_open(title, width, height);
}
struct Window *
struct mfb_window *
mfb_open(const char *title, unsigned width, unsigned height)
{
int fd = -1;
@ -662,11 +667,11 @@ mfb_open(const char *title, unsigned width, unsigned height)
wl_surface_damage(window_data_way->surface, window_data->dst_offset_x, window_data->dst_offset_y, window_data->dst_width, window_data->dst_height);
wl_surface_commit(window_data_way->surface);
mfb_set_keyboard_callback((struct Window *) window_data, keyboard_default);
mfb_set_keyboard_callback((struct mfb_window *) window_data, keyboard_default);
printf("Window created using Wayland API\n");
return (struct Window *) window_data;
return (struct mfb_window *) window_data;
out:
close(fd);
@ -688,12 +693,13 @@ frame_done(void *data, struct wl_callback *callback, uint32_t cookie)
*(uint32_t *)data = 1;
}
static const struct wl_callback_listener frame_listener = {
static const struct
wl_callback_listener frame_listener = {
.done = frame_done,
};
UpdateState
mfb_update(struct Window *window, void *buffer)
mfb_update_state
mfb_update(struct mfb_window *window, void *buffer)
{
uint32_t done = 0;
@ -739,8 +745,8 @@ mfb_update(struct Window *window, void *buffer)
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
UpdateState
mfb_update_events(struct Window *window)
mfb_update_state
mfb_update_events(struct mfb_window *window)
{
if(window == 0x0) {
return STATE_INVALID_WINDOW;
@ -896,7 +902,7 @@ init_keycodes(void)
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool
mfb_set_viewport(struct Window *window, unsigned offset_x, unsigned offset_y, unsigned width, unsigned height) {
mfb_set_viewport(struct mfb_window *window, unsigned offset_x, unsigned offset_y, unsigned width, unsigned height) {
SWindowData *window_data = (SWindowData *) window;

View File

@ -12,10 +12,11 @@ long s_window_style = WS_POPUP | WS_SYSMENU | WS_CAPTION;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
uint32_t translate_mod();
Key translate_key(unsigned int wParam, unsigned long lParam);
void destroy_window_data(SWindowData *window_data);
mfb_key translate_key(unsigned int wParam, unsigned long lParam);
void destroy_window_data(SWindowData *window_data);
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
LRESULT CALLBACK
WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
LRESULT res = 0;
SWindowData *window_data = (SWindowData *) GetWindowLongPtr(hWnd, GWLP_USERDATA);
@ -69,7 +70,7 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
case WM_SYSKEYUP:
{
if (window_data) {
Key key_code = translate_key((unsigned int)wParam, (unsigned long)lParam);
mfb_key key_code = translate_key((unsigned int)wParam, (unsigned long)lParam);
int is_pressed = !((lParam >> 31) & 1);
window_data->mod_keys = translate_mod();
@ -113,8 +114,8 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
case WM_XBUTTONDBLCLK:
{
if (window_data) {
MouseButton button = MOUSE_BTN_0;
window_data->mod_keys = translate_mod();
mfb_mouse_button button = MOUSE_BTN_0;
window_data->mod_keys = translate_mod();
int is_pressed = 0;
switch(message) {
case WM_LBUTTONDOWN:
@ -219,7 +220,8 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
struct Window *mfb_open_ex(const char *title, unsigned width, unsigned height, unsigned flags) {
struct mfb_window *
mfb_open_ex(const char *title, unsigned width, unsigned height, unsigned flags) {
RECT rect = { 0 };
int x = 0, y = 0;
@ -357,18 +359,20 @@ struct Window *mfb_open_ex(const char *title, unsigned width, unsigned height, u
window_data_win->hdc = GetDC(window_data_win->window);
mfb_set_keyboard_callback((struct Window *) window_data, keyboard_default);
mfb_set_keyboard_callback((struct mfb_window *) window_data, keyboard_default);
return (struct Window *) window_data;
return (struct mfb_window *) window_data;
}
struct Window *mfb_open(const char *title, unsigned width, unsigned height) {
struct mfb_window *
mfb_open(const char *title, unsigned width, unsigned height) {
return mfb_open_ex(title, width, height, 0);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
UpdateState mfb_update(struct Window *window, void *buffer) {
mfb_update_state
mfb_update(struct mfb_window *window, void *buffer) {
MSG msg;
if (window == 0x0) {
@ -401,7 +405,8 @@ UpdateState mfb_update(struct Window *window, void *buffer) {
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
UpdateState mfb_update_events(struct Window *window) {
mfb_update_state
mfb_update_events(struct mfb_window *window) {
MSG msg;
if (window == 0x0) {
@ -428,7 +433,8 @@ UpdateState mfb_update_events(struct Window *window) {
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void destroy_window_data(SWindowData *window_data) {
void
destroy_window_data(SWindowData *window_data) {
if (window_data == 0x0)
return;
@ -452,7 +458,8 @@ void destroy_window_data(SWindowData *window_data) {
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
uint32_t translate_mod() {
uint32_t
translate_mod() {
uint32_t mods = 0;
if (GetKeyState(VK_SHIFT) & 0x8000)
@ -475,7 +482,8 @@ uint32_t translate_mod() {
extern short int g_keycodes[512];
void init_keycodes() {
void
init_keycodes() {
// Clear keys
for (size_t i = 0; i < sizeof(g_keycodes) / sizeof(g_keycodes[0]); ++i)
@ -606,7 +614,8 @@ void init_keycodes() {
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Key translate_key(unsigned int wParam, unsigned long lParam) {
mfb_key
translate_key(unsigned int wParam, unsigned long lParam) {
if (wParam == VK_CONTROL) {
MSG next;
DWORD time;
@ -626,12 +635,13 @@ Key translate_key(unsigned int wParam, unsigned long lParam) {
if (wParam == VK_PROCESSKEY)
return KB_KEY_UNKNOWN;
return (Key) g_keycodes[HIWORD(lParam) & 0x1FF];
return (mfb_key) g_keycodes[HIWORD(lParam) & 0x1FF];
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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) {
SWindowData *window_data = (SWindowData *) window;
if (offset_x + width > window_data->window_width) {

View File

@ -1,20 +1,20 @@
#pragma once
#include <MiniFB_enums.h>
#include <stdint.h>
#include <X11/Xlib.h>
typedef struct {
Window window;
Display *display;
int screen;
GC gc;
XImage *image;
void *image_buffer;
XImage *image_scaler;
uint32_t image_scaler_width;
uint32_t image_scaler_height;
} SWindowData_X11;
#pragma once
#include <MiniFB_enums.h>
#include <stdint.h>
#include <X11/Xlib.h>
typedef struct {
Window window;
Display *display;
int screen;
GC gc;
XImage *image;
void *image_buffer;
XImage *image_scaler;
uint32_t image_scaler_width;
uint32_t image_scaler_height;
} SWindowData_X11;

View File

@ -20,7 +20,7 @@ stretch_image(uint32_t *srcImage, uint32_t srcX, uint32_t srcY, uint32_t srcWidt
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
struct Window *
struct mfb_window *
mfb_open_ex(const char *title, unsigned width, unsigned height, unsigned flags) {
int depth, i, formatCount, convDepth = -1;
XPixmapFormatValues* formats;
@ -185,14 +185,14 @@ mfb_open_ex(const char *title, unsigned width, unsigned height, unsigned flags)
window_data_x11->image = XCreateImage(window_data_x11->display, CopyFromParent, depth, ZPixmap, 0, 0x0, width, height, 32, width * 4);
mfb_set_keyboard_callback((struct Window *) window_data, keyboard_default);
mfb_set_keyboard_callback((struct mfb_window *) window_data, keyboard_default);
printf("Window created using X11 API\n");
return (struct Window *) window_data;
return (struct mfb_window *) window_data;
}
struct Window *
struct mfb_window *
mfb_open(const char *title, unsigned width, unsigned height) {
return mfb_open_ex(title, width, height, 0);
}
@ -203,7 +203,8 @@ int translate_key(int scancode);
int translate_mod(int state);
int translate_mod_ex(int key, int state, int is_pressed);
static void processEvents(SWindowData *window_data) {
static void
processEvents(SWindowData *window_data) {
XEvent event;
SWindowData_X11 *window_data_x11 = (SWindowData_X11 *) window_data->specific;
@ -214,44 +215,44 @@ static void processEvents(SWindowData *window_data) {
case KeyPress:
case KeyRelease:
{
Key key_code = (Key) translate_key(event.xkey.keycode);
int is_pressed = (event.type == KeyPress);
mfb_key key_code = (mfb_key) translate_key(event.xkey.keycode);
int is_pressed = (event.type == KeyPress);
window_data->mod_keys = translate_mod_ex(key_code, event.xkey.state, is_pressed);
window_data->key_status[key_code] = is_pressed;
kCall(keyboard_func, key_code, (KeyMod) window_data->mod_keys, is_pressed);
kCall(keyboard_func, key_code, (mfb_key_mod) window_data->mod_keys, is_pressed);
}
break;
case ButtonPress:
case ButtonRelease:
{
MouseButton button = (MouseButton) event.xbutton.button;
mfb_mouse_button button = (mfb_mouse_button) event.xbutton.button;
int is_pressed = (event.type == ButtonPress);
window_data->mod_keys = translate_mod(event.xkey.state);
window_data->mod_keys = translate_mod(event.xkey.state);
switch (button) {
case Button1:
case Button2:
case Button3:
kCall(mouse_btn_func, button, (KeyMod) window_data->mod_keys, is_pressed);
kCall(mouse_btn_func, button, (mfb_key_mod) window_data->mod_keys, is_pressed);
break;
case Button4:
kCall(mouse_wheel_func, (KeyMod) window_data->mod_keys, 0.0f, 1.0f);
kCall(mouse_wheel_func, (mfb_key_mod) window_data->mod_keys, 0.0f, 1.0f);
break;
case Button5:
kCall(mouse_wheel_func, (KeyMod) window_data->mod_keys, 0.0f, -1.0f);
kCall(mouse_wheel_func, (mfb_key_mod) window_data->mod_keys, 0.0f, -1.0f);
break;
case 6:
kCall(mouse_wheel_func, (KeyMod) window_data->mod_keys, 1.0f, 0.0f);
kCall(mouse_wheel_func, (mfb_key_mod) window_data->mod_keys, 1.0f, 0.0f);
break;
case 7:
kCall(mouse_wheel_func, (KeyMod) window_data->mod_keys, -1.0f, 0.0f);
kCall(mouse_wheel_func, (mfb_key_mod) window_data->mod_keys, -1.0f, 0.0f);
break;
default:
kCall(mouse_btn_func, (MouseButton) (button - 4), (KeyMod) window_data->mod_keys, is_pressed);
kCall(mouse_btn_func, (mfb_mouse_button) (button - 4), (mfb_key_mod) window_data->mod_keys, is_pressed);
break;
}
}
@ -303,7 +304,8 @@ static void processEvents(SWindowData *window_data) {
void destroy(SWindowData *window_data);
UpdateState mfb_update(struct Window *window, void *buffer) {
mfb_update_state
mfb_update(struct mfb_window *window, void *buffer) {
if (window == 0x0) {
return STATE_INVALID_WINDOW;
}
@ -358,7 +360,8 @@ UpdateState mfb_update(struct Window *window, void *buffer) {
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
UpdateState mfb_update_events(struct Window *window) {
mfb_update_state
mfb_update_events(struct mfb_window *window) {
if (window == 0x0) {
return STATE_INVALID_WINDOW;
}
@ -378,7 +381,8 @@ UpdateState mfb_update_events(struct Window *window) {
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void destroy(SWindowData *window_data) {
void
destroy(SWindowData *window_data) {
if (window_data != 0x0) {
if (window_data->specific != 0x0) {
SWindowData_X11 *window_data_x11 = (SWindowData_X11 *) window_data->specific;
@ -400,7 +404,8 @@ void destroy(SWindowData *window_data) {
extern short int g_keycodes[512];
static int translateKeyCodeB(int keySym) {
static int
translateKeyCodeB(int keySym) {
switch (keySym)
{
@ -563,7 +568,8 @@ static int translateKeyCodeA(int keySym) {
return KB_KEY_UNKNOWN;
}
void init_keycodes(SWindowData_X11 *window_data_x11) {
void
init_keycodes(SWindowData_X11 *window_data_x11) {
size_t i;
int keySym;
@ -585,7 +591,8 @@ void init_keycodes(SWindowData_X11 *window_data_x11) {
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int translate_key(int scancode) {
int
translate_key(int scancode) {
if (scancode < 0 || scancode > 255)
return KB_KEY_UNKNOWN;
@ -594,7 +601,8 @@ int translate_key(int scancode) {
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int translate_mod(int state) {
int
translate_mod(int state) {
int mod_keys = 0;
if (state & ShiftMask)
@ -615,7 +623,8 @@ int translate_mod(int state) {
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int translate_mod_ex(int key, int state, int is_pressed) {
int
translate_mod_ex(int key, int state, int is_pressed) {
int mod_keys = 0;
mod_keys = translate_mod(state);
@ -660,7 +669,8 @@ int translate_mod_ex(int key, int state, int is_pressed) {
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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) {
SWindowData *window_data = (SWindowData *) window;
if (offset_x + width > window_data->window_width) {