Fix issue #32 (#33)

* 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

* fix issue 32
This commit is contained in:
Daniel Collin 2020-03-05 18:51:57 +01:00 committed by GitHub
parent 219e7d0c20
commit 4286f055c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 59 additions and 22 deletions

View File

@ -49,12 +49,12 @@ const char * mfb_get_key_name(Key key);
bool mfb_is_window_active(struct Window *window); bool mfb_is_window_active(struct Window *window);
unsigned mfb_get_window_width(struct Window *window); unsigned mfb_get_window_width(struct Window *window);
unsigned mfb_get_window_height(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_x(struct Window *window); // Last mouse pos X
int mfb_get_mouse_y(struct Window *window); // Last mouse pos Y 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_scroll_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. 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. 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. const uint8_t * mfb_get_key_buffer(struct Window *window); // One byte for every key. Press (1), Release 0.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -144,7 +144,7 @@ int mfb_get_mouse_y(struct Window *window) {
} }
//------------------------------------- //-------------------------------------
float mfb_get_mouse_scrool_x(struct Window *window) { float mfb_get_mouse_scroll_x(struct Window *window) {
if(window != 0x0) { if(window != 0x0) {
SWindowData *window_data = (SWindowData *) window; SWindowData *window_data = (SWindowData *) window;
return window_data->mouse_wheel_x; return window_data->mouse_wheel_x;
@ -153,7 +153,7 @@ float mfb_get_mouse_scrool_x(struct Window *window) {
} }
//------------------------------------- //-------------------------------------
float mfb_get_mouse_scrool_y(struct Window *window) { float mfb_get_mouse_scroll_y(struct Window *window) {
if(window != 0x0) { if(window != 0x0) {
SWindowData *window_data = (SWindowData *) window; SWindowData *window_data = (SWindowData *) window;
return window_data->mouse_wheel_y; return window_data->mouse_wheel_y;

View File

@ -565,17 +565,27 @@ mfb_open(const char *title, unsigned width, unsigned height)
int fd = -1; int fd = -1;
SWindowData *window_data = (SWindowData *) malloc(sizeof(SWindowData)); SWindowData *window_data = (SWindowData *) malloc(sizeof(SWindowData));
if(window_data == 0x0) {
return 0x0;
}
memset(window_data, 0, sizeof(SWindowData)); memset(window_data, 0, sizeof(SWindowData));
SWindowData_Way *window_data_way = (SWindowData_Way *) malloc(sizeof(SWindowData_Way)); SWindowData_Way *window_data_way = (SWindowData_Way *) malloc(sizeof(SWindowData_Way));
if(window_data_way == 0x0) {
free(window_data);
return 0x0;
}
memset(window_data_way, 0, sizeof(SWindowData_Way)); memset(window_data_way, 0, sizeof(SWindowData_Way));
window_data->specific = window_data_way; window_data->specific = window_data_way;
window_data_way->shm_format = -1u; window_data_way->shm_format = -1u;
window_data_way->display = wl_display_connect(0x0); window_data_way->display = wl_display_connect(0x0);
if (!window_data_way->display) if (!window_data_way->display) {
free(window_data);
free(window_data_way);
return 0x0; return 0x0;
}
window_data_way->registry = wl_display_get_registry(window_data_way->display); window_data_way->registry = wl_display_get_registry(window_data_way->display);
wl_registry_add_listener(window_data_way->registry, &registry_listener, window_data); wl_registry_add_listener(window_data_way->registry, &registry_listener, window_data);

View File

@ -69,14 +69,14 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
case WM_SYSKEYUP: case WM_SYSKEYUP:
{ {
if (window_data) { if (window_data) {
Key key_code = translate_key((unsigned int)wParam, (unsigned long)lParam); Key key_code = translate_key((unsigned int)wParam, (unsigned long)lParam);
int is_pressed = !((lParam >> 31) & 1); int is_pressed = !((lParam >> 31) & 1);
window_data->mod_keys = translate_mod(); window_data->mod_keys = translate_mod();
if (key_code == KB_KEY_UNKNOWN) if (key_code == KB_KEY_UNKNOWN)
return FALSE; return FALSE;
window_data->key_status[key_code] = is_pressed; window_data->key_status[key_code] = (uint8_t) is_pressed;
kCall(keyboard_func, key_code, window_data->mod_keys, is_pressed); kCall(keyboard_func, key_code, window_data->mod_keys, is_pressed);
} }
break; break;
@ -221,14 +221,21 @@ 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 Window *mfb_open_ex(const char *title, unsigned width, unsigned height, unsigned flags) {
RECT rect = { 0 }; RECT rect = { 0 };
int x, y; int x = 0, y = 0;
init_keycodes(); init_keycodes();
SWindowData *window_data = malloc(sizeof(SWindowData)); SWindowData *window_data = malloc(sizeof(SWindowData));
if (window_data == 0x0) {
return 0x0;
}
memset(window_data, 0, sizeof(SWindowData)); memset(window_data, 0, sizeof(SWindowData));
SWindowData_Win *window_data_win = malloc(sizeof(SWindowData_Win)); SWindowData_Win *window_data_win = malloc(sizeof(SWindowData_Win));
if(window_data_win == 0x0) {
free(window_data);
return 0x0;
}
memset(window_data_win, 0, sizeof(SWindowData_Win)); memset(window_data_win, 0, sizeof(SWindowData_Win));
window_data->specific = window_data_win; window_data->specific = window_data_win;
@ -239,8 +246,6 @@ struct Window *mfb_open_ex(const char *title, unsigned width, unsigned height, u
s_window_style = WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX & ~WS_THICKFRAME; s_window_style = WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX & ~WS_THICKFRAME;
if (flags & WF_FULLSCREEN) { if (flags & WF_FULLSCREEN) {
flags = WF_FULLSCREEN; // Remove all other flags flags = WF_FULLSCREEN; // Remove all other flags
x = 0;
y = 0;
rect.right = GetSystemMetrics(SM_CXSCREEN); rect.right = GetSystemMetrics(SM_CXSCREEN);
rect.bottom = GetSystemMetrics(SM_CYSCREEN); rect.bottom = GetSystemMetrics(SM_CYSCREEN);
s_window_style = WS_POPUP & ~(WS_CAPTION | WS_THICKFRAME | WS_MINIMIZE | WS_MAXIMIZE | WS_SYSMENU); s_window_style = WS_POPUP & ~(WS_CAPTION | WS_THICKFRAME | WS_MINIMIZE | WS_MAXIMIZE | WS_SYSMENU);
@ -284,8 +289,6 @@ struct Window *mfb_open_ex(const char *title, unsigned width, unsigned height, u
rect.bottom += (rect.bottom - height); rect.bottom += (rect.bottom - height);
rect.top = 0; rect.top = 0;
} }
x = 0;
y = 0;
} }
else if (!(flags & WF_FULLSCREEN)) { else if (!(flags & WF_FULLSCREEN)) {
rect.right = width; rect.right = width;
@ -323,8 +326,11 @@ struct Window *mfb_open_ex(const char *title, unsigned width, unsigned height, u
window_data->window_width, window_data->window_height, window_data->window_width, window_data->window_height,
0, 0, 0, 0); 0, 0, 0, 0);
if (!window_data_win->window) if (!window_data_win->window) {
free(window_data);
free(window_data_win);
return 0x0; return 0x0;
}
SetWindowLongPtr(window_data_win->window, GWLP_USERDATA, (LONG_PTR) window_data); SetWindowLongPtr(window_data_win->window, GWLP_USERDATA, (LONG_PTR) window_data);
@ -334,6 +340,11 @@ struct Window *mfb_open_ex(const char *title, unsigned width, unsigned height, u
ShowWindow(window_data_win->window, SW_NORMAL); ShowWindow(window_data_win->window, SW_NORMAL);
window_data_win->bitmapInfo = (BITMAPINFO *) calloc(1, sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 3); window_data_win->bitmapInfo = (BITMAPINFO *) calloc(1, sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 3);
if(window_data_win->bitmapInfo == 0x0) {
free(window_data);
free(window_data_win);
return 0x0;
}
window_data_win->bitmapInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); window_data_win->bitmapInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
window_data_win->bitmapInfo->bmiHeader.biPlanes = 1; window_data_win->bitmapInfo->bmiHeader.biPlanes = 1;
window_data_win->bitmapInfo->bmiHeader.biBitCount = 32; window_data_win->bitmapInfo->bmiHeader.biBitCount = 32;
@ -405,6 +416,9 @@ UpdateState mfb_update_events(struct Window *window) {
SWindowData_Win *window_data_win = (SWindowData_Win *) window_data->specific; SWindowData_Win *window_data_win = (SWindowData_Win *) window_data->specific;
while (window_data->close == false && PeekMessage(&msg, window_data_win->window, 0, 0, PM_REMOVE)) { while (window_data->close == false && PeekMessage(&msg, window_data_win->window, 0, 0, PM_REMOVE)) {
if(msg.message == WM_PAINT)
return;
TranslateMessage(&msg); TranslateMessage(&msg);
DispatchMessage(&msg); DispatchMessage(&msg);
} }

View File

@ -29,15 +29,25 @@ mfb_open_ex(const char *title, unsigned width, unsigned height, unsigned flags)
Visual* visual; Visual* visual;
SWindowData *window_data = (SWindowData *) malloc(sizeof(SWindowData)); SWindowData *window_data = (SWindowData *) malloc(sizeof(SWindowData));
if (!window_data) {
return 0x0;
}
memset(window_data, 0, sizeof(SWindowData)); memset(window_data, 0, sizeof(SWindowData));
SWindowData_X11 *window_data_x11 = (SWindowData_X11 *) malloc(sizeof(SWindowData_X11)); SWindowData_X11 *window_data_x11 = (SWindowData_X11 *) malloc(sizeof(SWindowData_X11));
if (!window_data_x11) {
free(window_data);
return 0x0;
}
memset(window_data_x11, 0, sizeof(SWindowData_X11)); memset(window_data_x11, 0, sizeof(SWindowData_X11));
window_data->specific = window_data_x11; window_data->specific = window_data_x11;
window_data_x11->display = XOpenDisplay(0); window_data_x11->display = XOpenDisplay(0);
if (!window_data_x11->display) if (!window_data_x11->display) {
free(window_data);
free(window_data_x11);
return 0x0; return 0x0;
}
init_keycodes(window_data_x11); init_keycodes(window_data_x11);
@ -322,6 +332,9 @@ UpdateState mfb_update(struct Window *window, void *buffer) {
} }
int depth = DefaultDepth(window_data_x11->display, window_data_x11->screen); int depth = DefaultDepth(window_data_x11->display, window_data_x11->screen);
window_data_x11->image_buffer = malloc(window_data->dst_width * window_data->dst_height * 4); window_data_x11->image_buffer = malloc(window_data->dst_width * window_data->dst_height * 4);
if(window_data_x11->image_buffer == 0x0) {
return STATE_INTERNAL_ERROR;
}
window_data_x11->image_scaler_width = window_data->dst_width; window_data_x11->image_scaler_width = window_data->dst_width;
window_data_x11->image_scaler_height = window_data->dst_height; window_data_x11->image_scaler_height = window_data->dst_height;
window_data_x11->image_scaler = XCreateImage(window_data_x11->display, CopyFromParent, depth, ZPixmap, 0, 0x0, window_data_x11->image_scaler_width, window_data_x11->image_scaler_height, 32, window_data_x11->image_scaler_width * 4); window_data_x11->image_scaler = XCreateImage(window_data_x11->display, CopyFromParent, depth, ZPixmap, 0, 0x0, window_data_x11->image_scaler_width, window_data_x11->image_scaler_height, 32, window_data_x11->image_scaler_width * 4);
@ -551,15 +564,15 @@ static int translateKeyCodeA(int keySym) {
} }
void init_keycodes(SWindowData_X11 *window_data_x11) { void init_keycodes(SWindowData_X11 *window_data_x11) {
size_t i; size_t i;
int keySym; int keySym;
// Clear keys // Clear keys
for (i = 0; i < sizeof(g_keycodes) / sizeof(g_keycodes[0]); ++i) for (i = 0; i < sizeof(g_keycodes) / sizeof(g_keycodes[0]); ++i)
g_keycodes[i] = KB_KEY_UNKNOWN; g_keycodes[i] = KB_KEY_UNKNOWN;
// Valid key code range is [8,255], according to the Xlib manual // Valid key code range is [8,255], according to the Xlib manual
for (int i=8; i<=255; ++i) { for (i=8; i<=255; ++i) {
// Try secondary keysym, for numeric keypad keys // Try secondary keysym, for numeric keypad keys
keySym = XkbKeycodeToKeysym(window_data_x11->display, i, 0, 1); keySym = XkbKeycodeToKeysym(window_data_x11->display, i, 0, 1);
g_keycodes[i] = translateKeyCodeB(keySym); g_keycodes[i] = translateKeyCodeB(keySym);