Add timers and ability to set a target fps for the application (#36)

* working on windows

* minor fix

* working on macosx

* Oops! I reversed resolution and frequency on macos

* working on X11

* working on wayland

* update the readme

* format the readme

Co-authored-by: Carlos Aragones <>
This commit is contained in:
Carlos Aragonés 2020-04-22 13:00:15 +02:00 committed by GitHub
parent 4175cec89e
commit 35b8439b26
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 734 additions and 239 deletions

View File

@ -14,6 +14,7 @@ set(SrcLib
src/MiniFB_cpp.cpp
src/MiniFB_internal.c
src/MiniFB_internal.h
src/MiniFB_timer.c
src/WindowData.h
)
@ -34,11 +35,13 @@ set(SrcMacOSX
set(SrcWayland
src/wayland/WaylandMiniFB.c
src/wayland/WindowData_Way.h
src/MiniFB_linux.c
)
set(SrcX11
src/x11/X11MiniFB.c
src/x11/WindowData_X11.h
src/MiniFB_linux.c
)
# Avoid RelWithDebInfo and MinSizeRel
@ -52,6 +55,12 @@ if(NOT CMAKE_BUILD_TYPE)
message(STATUS "Build type not specified: Use Release by default")
endif(NOT CMAKE_BUILD_TYPE)
# Set features
#--------------------------------------
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
# Set GCC/Clang flags
#--------------------------------------
if (NOT MSVC)
@ -78,7 +87,6 @@ if (NOT MSVC)
add_compile_options("$<$<CONFIG:Debug>:-g>")
add_compile_options("$<IF:$<CONFIG:Debug>,-O0,-O2>")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -pedantic -Wno-switch -Wno-unused-function -Wno-implicit-fallthrough")
set (CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -std=c++11")
set (CMAKE_OBJC_FLAGS "${CMAKE_C_FLAGS}")
set (CMAKE_OBJCXX_FLAGS "${CMAKE_CXX_FLAGS}")
endif()

View File

@ -1,8 +1,8 @@
MiniFB
======
# MiniFB
MiniFB (Mini FrameBuffer) is a small cross platform library that makes it easy to render (32-bit) pixels in a window. An example is the best way to show how it works:
```c
struct mfb_window *window = mfb_open_ex("my display", 800, 600, WF_RESIZABLE);
if (!window)
return 0;
@ -18,10 +18,12 @@ MiniFB (Mini FrameBuffer) is a small cross platform library that makes it easy t
if (state < 0)
break;
}
```
Furthermore, you can add callbacks to the windows:
Furthermore, you can _add callbacks to the windows_:
```c
void active(struct mfb_window *window, bool isActive) {
...
}
@ -72,17 +74,21 @@ Furthermore, you can add callbacks to the windows:
mfb_set_mouse_button_callback(window, mouse_btn);
mfb_set_mouse_move_callback(window, mouse_move);
mfb_set_mouse_scroll_callback(window, mouse_scroll);
```
Additionally you can set data per window and recover it
Additionally you can _set data per window and recover it_:
```c
mfb_set_user_data(window, (void *) myData);
...
myData = (someCast *) mfb_get_user_data(window);
```
Finally, you can get information about the events in the window directly:
Finally, you can _get information about the events in the window directly_:
```c
bool mfb_is_window_active(struct mfb_window *window);
unsigned mfb_get_window_width(struct mfb_window *window);
@ -97,21 +103,71 @@ Finally, you can get information about the events in the window directly:
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.
```
First the code creates window with the mfb_open call that is used to display the data, next it's the applications responsibility to allocate a buffer (which has to be at least the size of the window and in 32-bit) Next when calling mfb_update function the buffer will be copied over to the window and displayed. Currently the mfb_update will return -1 if ESC key is pressed but later on it will support to return a key code for a pressed button. See https://github.com/emoon/minifb/blob/master/tests/noise.c for a complete example
MiniFB has been tested on Windows, Mac OS X and Linux but may of course have trouble depending on your setup. Currently the code will not do any converting of data if not a proper 32-bit display can be created.
Build instructions
------------------
**Extra: Timers and target FPS**
You can create timers for your own purposes.
```c
struct mfb_timer * mfb_timer_create();
void mfb_timer_destroy(struct mfb_timer *tmr);
void mfb_timer_reset(struct mfb_timer *tmr);
double mfb_timer_now(struct mfb_timer *tmr);
double mfb_timer_delta(struct mfb_timer *tmr);
double mfb_timer_get_frequency();
double mfb_timer_get_resolution();
```
Furthermore you can set a target fps for the application. The default is 60 frames per second.
```c
void mfb_set_target_fps(uint32_t fps);
```
This avoid the problem of update too fast the window collapsing the redrawing in fast processors.
To use this you need to call the function:
```c
bool mfb_wait_sync(struct mfb_window *window);
```
_Example_:
```c
do {
int i;
mfb_update_state state;
// TODO: add some awesome rendering to the buffer
state = mfb_update(window, g_buffer);
if (state != STATE_OK) {
window = 0x0;
break;
}
} while(mfb_wait_sync(window));
```
Note that if you have several windows running on the same thread it makes no sense to wait them all...
## Build instructions
MiniFB uses tundra https://github.com/deplinenoise/tundra as build system and is required to build the code as is but not many changes should be needed if you want to use it directly in your own code.
You can also use CMake as build system.
Mac
---
### Mac
Cocoa and clang is assumed to be installed on the system (downloading latest XCode + installing the command line tools should do the trick) then to build run: tundra2 macosx-clang-debug and you should be able to run the noise example (t2-output/macosx-clang-debug-default/noise)
@ -120,44 +176,49 @@ To enable it just compile defining the preprocessor macro USE_METAL_API.
If you use CMake just enable the flag:
```
mkdir build
cd build
cmake .. -DUSE_METAL_API=ON
```
or if you don't want to use Metal API:
```
mkdir build
cd build
cmake .. -DUSE_METAL_API=OFF
```
Windows
-------
### Windows
Visual Studio (ver 2012 express has been tested) tools needed (using the vcvars32.bat (for 32-bit) will set up the enviroment) to build run: tundra2 win32-msvc-debug and you should be able to run noise in t2-output/win32-msvc-debug-default/noise.exe
If you use CMake the Visual Studio project will be generated (2015, 2017 and 2019 have been tested).
x11 (FreeBSD, Linux, *nix)
--------------------------
### x11 (FreeBSD, Linux, *nix)
gcc and x11-dev libs needs to be installed. To build the code run tundra2 x11-gcc-debug and you should be able to run t2-output/x11-gcc-debug-default/noise
If you use CMake just disable the flag:
```
mkdir build
cd build
cmake .. -DUSE_WAYLAND_API=OFF
```
wayland (Linux)
--------------------------
### Wayland (Linux)
Depends on gcc and wayland-client and wayland-cursor. Built using the wayland-gcc variants.
If you use CMake just enable the flag:
```
mkdir build
cd build
cmake .. -DUSE_WAYLAND_API=ON
```

View File

@ -56,6 +56,17 @@ float mfb_get_mouse_scroll_y(struct mfb_window *window); // M
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.
void mfb_set_target_fps(uint32_t fps);
bool mfb_wait_sync(struct mfb_window *window);
struct mfb_timer * mfb_timer_create();
void mfb_timer_destroy(struct mfb_timer *tmr);
void mfb_timer_reset(struct mfb_timer *tmr);
double mfb_timer_now(struct mfb_timer *tmr);
double mfb_timer_delta(struct mfb_timer *tmr);
double mfb_timer_get_frequency();
double mfb_timer_get_resolution();
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#ifdef __cplusplus

View File

@ -172,6 +172,7 @@ typedef enum {
// Opaque pointer
struct mfb_window;
struct mfb_timer;
// Event callbacks
typedef void(*mfb_active_func)(struct mfb_window *window, bool isActive);

View File

@ -6,6 +6,13 @@
#define kCall(func, ...) if(window_data && window_data->func) window_data->func((struct mfb_window *) window_data, __VA_ARGS__);
#define kUnused(var) (void) var;
typedef struct mfb_timer {
int64_t start_time;
int64_t delta_counter;
uint64_t time;
} mfb_timer;
#if defined(__cplusplus)
extern "C" {
#endif

32
src/MiniFB_linux.c Normal file
View File

@ -0,0 +1,32 @@
#include <time.h>
#include <MiniFB.h>
extern double g_timer_frequency;
extern double g_timer_resolution;
#define kClock CLOCK_MONOTONIC
//#define kClock CLOCK_REALTIME
uint64_t
mfb_timer_tick() {
struct timespec time;
if (clock_gettime(kClock, &time) != 0) {
return 0.0;
}
return time.tv_sec * 1e+9 + time.tv_nsec;
}
void
mfb_timer_init() {
struct timespec res;
if (clock_getres(kClock, &res) != 0) {
g_timer_frequency = 1e+9;
}
else {
g_timer_frequency = res.tv_sec + res.tv_nsec * 1e+9;
}
g_timer_resolution = 1.0 / g_timer_frequency;
}

102
src/MiniFB_timer.c Normal file
View File

@ -0,0 +1,102 @@
#include "MiniFB.h"
#include "MiniFB_internal.h"
#include <stdlib.h>
//-------------------------------------
double g_timer_frequency;
double g_timer_resolution;
double g_time_for_frame = 1.0 / 60.0;
//-------------------------------------
extern uint64_t mfb_timer_tick();
extern void mfb_timer_init();
//-------------------------------------
void
mfb_set_target_fps(uint32_t fps) {
if(fps == 0) {
g_time_for_frame = 0;
}
else {
g_time_for_frame = 1.0 / fps;
}
}
//-------------------------------------
struct mfb_timer *
mfb_timer_create() {
static int once = 1;
mfb_timer *tmr;
if(once) {
once = 0;
mfb_timer_init();
}
tmr = malloc(sizeof(mfb_timer));
mfb_timer_reset(tmr);
return tmr;
}
//-------------------------------------
void
mfb_timer_destroy(struct mfb_timer *tmr) {
if(tmr != 0x0) {
free(tmr);
}
}
//-------------------------------------
void
mfb_timer_reset(struct mfb_timer *tmr) {
if(tmr == 0x0)
return;
tmr->start_time = mfb_timer_tick();
tmr->delta_counter = tmr->start_time;
tmr->time = 0;
}
//-------------------------------------
double
mfb_timer_now(struct mfb_timer *tmr) {
uint64_t counter;
if(tmr == 0x0)
return 0.0;
counter = mfb_timer_tick();
tmr->time += (counter - tmr->start_time);
tmr->start_time = counter;
return tmr->time * g_timer_resolution;
}
//-------------------------------------
double
mfb_timer_delta(struct mfb_timer *tmr) {
int64_t counter;
uint64_t delta;
if(tmr == 0x0)
return 0.0;
counter = mfb_timer_tick();
delta = (counter - tmr->delta_counter);
tmr->delta_counter = counter;
return delta * g_timer_resolution;
}
//-------------------------------------
double
mfb_timer_get_frequency() {
return g_timer_frequency;
}
//-------------------------------------
double
mfb_timer_get_resolution() {
return g_timer_resolution;
}

View File

@ -10,6 +10,8 @@
#include <MetalKit/MetalKit.h>
#endif
#include <unistd.h>
#include <sched.h>
#include <mach/mach_time.h>
void init_keycodes();
@ -250,6 +252,7 @@ mfb_open_ex(const char *title, unsigned width, unsigned height, unsigned flags)
[window_data_osx->window setAcceptsMouseMovedEvents:YES];
[window_data_osx->window center];
window_data_osx->timer = mfb_timer_create();
[NSApp activateIgnoringOtherApps:YES];
@ -286,6 +289,8 @@ destroy_window_data(SWindowData *window_data)
[window removeWindowData];
[window performClose:nil];
mfb_timer_destroy(window_data_osx->timer);
memset(window_data_osx, 0, sizeof(SWindowData_OSX));
free(window_data_osx);
}
@ -303,14 +308,12 @@ update_events(SWindowData *window_data)
NSEvent* event;
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
do
{
do {
event = [NSApp nextEventMatchingMask:NSEventMaskAny untilDate:[NSDate distantPast] inMode:NSDefaultRunLoopMode dequeue:YES];
if (event) {
[NSApp sendEvent:event];
}
}
while ((window_data->close == false) && event);
} while ((window_data->close == false) && event);
[pool release];
}
@ -375,6 +378,59 @@ mfb_update_events(struct mfb_window *window)
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
extern double g_time_for_frame;
bool
mfb_wait_sync(struct mfb_window *window)
{
NSEvent* event;
if(window == 0x0) {
return STATE_INVALID_WINDOW;
}
SWindowData *window_data = (SWindowData *) window;
if(window_data->close) {
destroy_window_data(window_data);
return false;
}
//NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
SWindowData_OSX *window_data_osx = (SWindowData_OSX *) window_data->specific;
double current;
uint32_t millis = 1;
while(1) {
event = [NSApp nextEventMatchingMask:NSEventMaskAny untilDate:[NSDate distantPast] inMode:NSDefaultRunLoopMode dequeue:YES];
if (event) {
[NSApp sendEvent:event];
}
if(window_data->close) {
destroy_window_data(window_data);
return false;
}
current = mfb_timer_now(window_data_osx->timer);
if (current >= g_time_for_frame) {
mfb_timer_reset(window_data_osx->timer);
return true;
}
else if(current >= g_time_for_frame * 0.8) {
millis = 0;
}
usleep(millis * 1000);
//sched_yield();
}
//[pool release];
return true;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool
mfb_set_viewport(struct mfb_window *window, unsigned offset_x, unsigned offset_y, unsigned width, unsigned height)
{
@ -540,3 +596,35 @@ init_keycodes()
g_keycodes[0x43] = KB_KEY_KP_MULTIPLY;
g_keycodes[0x4E] = KB_KEY_KP_SUBTRACT;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
extern double g_timer_frequency;
extern double g_timer_resolution;
uint64_t
mfb_timer_tick() {
static mach_timebase_info_data_t timebase = { 0 };
if (timebase.denom == 0) {
(void) mach_timebase_info(&timebase);
}
uint64_t time = mach_absolute_time();
//return (time * s_timebase_info.numer) / s_timebase_info.denom;
// Perform the arithmetic at 128-bit precision to avoid the overflow!
uint64_t high = (time >> 32) * timebase.numer;
uint64_t highRem = ((high % timebase.denom) << 32) / timebase.denom;
uint64_t low = (time & 0xFFFFFFFFull) * timebase.numer / timebase.denom;
high /= timebase.denom;
return (high << 32) + highRem + low;
}
void
mfb_timer_init() {
g_timer_frequency = 1e+9;
g_timer_resolution = 1.0 / g_timer_frequency;
}

View File

@ -11,6 +11,8 @@
typedef struct {
OSXWindow *window;
struct mfb_timer *timer;
#if defined(USE_METAL_API)
struct {
id<MTLCommandQueue> command_queue;

View File

@ -27,6 +27,7 @@ destroy_window_data(SWindowData *window_data)
SWindowData_Way *window_data_way = (SWindowData_Way *) window_data->specific;
if(window_data_way != 0x0) {
mfb_timer_destroy(window_data_way->timer);
memset(window_data_way, 0, sizeof(SWindowData_Way));
free(window_data_way);
}
@ -596,8 +597,8 @@ mfb_open(const char *title, unsigned width, unsigned height)
init_keycodes();
if (wl_display_dispatch(window_data_way->display) == -1 || wl_display_roundtrip(window_data_way->display) == -1)
{
if (wl_display_dispatch(window_data_way->display) == -1 ||
wl_display_roundtrip(window_data_way->display) == -1) {
return 0x0;
}
@ -667,6 +668,8 @@ 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);
window_data_way->timer = mfb_timer_create();
mfb_set_keyboard_callback((struct mfb_window *) window_data, keyboard_default);
printf("Window created using Wayland API\n");
@ -762,7 +765,7 @@ mfb_update_events(struct mfb_window *window)
if (!window_data_way->display || wl_display_get_error(window_data_way->display) != 0)
return STATE_INTERNAL_ERROR;
if (wl_display_dispatch(window_data_way->display) == -1 || wl_display_roundtrip(window_data_way->display) == -1) {
if (wl_display_dispatch_pending(window_data_way->display) == -1) {
return STATE_INTERNAL_ERROR;
}
@ -771,6 +774,51 @@ mfb_update_events(struct mfb_window *window)
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
extern double g_time_for_frame;
bool
mfb_wait_sync(struct mfb_window *window) {
if(window == 0x0) {
return false;
}
SWindowData *window_data = (SWindowData *) window;
if(window_data->close) {
destroy(window_data);
return false;
}
SWindowData_Way *window_data_way = (SWindowData_Way *) window_data->specific;
double current;
uint32_t millis = 1;
while(1) {
if (wl_display_dispatch_pending(window_data_way->display) == -1) {
return false;
}
if(window_data->close) {
destroy_window_data(window_data);
return false;
}
current = mfb_timer_now(window_data_way->timer);
if (current >= g_time_for_frame) {
mfb_timer_reset(window_data_way->timer);
return true;
}
else if(current >= g_time_for_frame * 0.8) {
millis = 0;
}
usleep(millis * 1000);
//sched_yield();
}
return true;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
extern short int g_keycodes[512];
void

View File

@ -53,5 +53,6 @@ typedef struct
uint32_t buffer_stride;
uint32_t mod_keys;
struct mfb_timer *timer;
bool close;
} SWindowData_Way;

View File

@ -48,10 +48,8 @@ WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
StretchDIBits(window_data_win->hdc, window_data->dst_offset_x, window_data->dst_offset_y, window_data->dst_width, window_data->dst_height, 0, 0, window_data->buffer_width, window_data->buffer_height, window_data->draw_buffer,
window_data_win->bitmapInfo, DIB_RGB_COLORS, SRCCOPY);
ValidateRect(hWnd, 0x0);
}
ValidateRect(hWnd, 0x0);
break;
}
@ -359,6 +357,8 @@ mfb_open_ex(const char *title, unsigned width, unsigned height, unsigned flags)
window_data_win->hdc = GetDC(window_data_win->window);
window_data_win->timer = mfb_timer_create();
mfb_set_keyboard_callback((struct mfb_window *) window_data, keyboard_default);
return (struct mfb_window *) window_data;
@ -421,9 +421,8 @@ mfb_update_events(struct mfb_window *window) {
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)) {
if(msg.message == WM_PAINT)
return STATE_OK;
//if(msg.message == WM_PAINT)
// return STATE_OK;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
@ -433,6 +432,53 @@ mfb_update_events(struct mfb_window *window) {
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
extern double g_time_for_frame;
bool
mfb_wait_sync(struct mfb_window *window) {
MSG msg;
if (window == 0x0) {
return false;
}
SWindowData *window_data = (SWindowData *)window;
if (window_data->close) {
destroy_window_data(window_data);
return false;
}
SWindowData_Win *window_data_win = (SWindowData_Win *) window_data->specific;
double current;
uint32_t millis = 1;
while (1) {
if(PeekMessage(&msg, window_data_win->window, 0, 0, PM_REMOVE)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if (window_data->close) {
destroy_window_data(window_data);
return false;
}
current = mfb_timer_now(window_data_win->timer);;
if (current >= g_time_for_frame) {
mfb_timer_reset(window_data_win->timer);
return true;
}
else if(current >= g_time_for_frame * 0.8) {
millis = 0;
}
Sleep(millis);
}
return true;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
destroy_window_data(SWindowData *window_data) {
if (window_data == 0x0)
@ -453,6 +499,8 @@ destroy_window_data(SWindowData *window_data) {
window_data_win->window = 0;
window_data_win->hdc = 0;
window_data_win->bitmapInfo = 0x0;
mfb_timer_destroy(window_data_win->timer);
window_data_win->timer = 0x0;
window_data->close = true;
}
@ -659,3 +707,27 @@ mfb_set_viewport(struct mfb_window *window, unsigned offset_x, unsigned offset_y
return true;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
extern double g_timer_frequency;
extern double g_timer_resolution;
uint64_t
mfb_timer_tick() {
int64_t counter;
QueryPerformanceCounter((LARGE_INTEGER *) &counter);
return counter;
}
void
mfb_timer_init() {
uint64_t frequency;
QueryPerformanceFrequency((LARGE_INTEGER *) &frequency);
g_timer_frequency = (double) ((int64_t) frequency);
g_timer_resolution = 1.0 / g_timer_frequency;
}

View File

@ -11,5 +11,6 @@ typedef struct {
WNDCLASS wc;
HDC hdc;
BITMAPINFO *bitmapInfo;
struct mfb_timer *timer;
bool mouse_inside;
} SWindowData_Win;

View File

@ -17,4 +17,5 @@ typedef struct {
XImage *image_scaler;
uint32_t image_scaler_width;
uint32_t image_scaler_height;
struct mfb_timer *timer;
} SWindowData_X11;

View File

@ -7,6 +7,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <MiniFB.h>
#include <MiniFB_internal.h>
#include "WindowData.h"
@ -185,6 +186,8 @@ 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);
window_data_x11->timer = mfb_timer_create();
mfb_set_keyboard_callback((struct mfb_window *) window_data, keyboard_default);
printf("Window created using X11 API\n");
@ -204,20 +207,14 @@ int translate_mod(int state);
int translate_mod_ex(int key, int state, int is_pressed);
static void
processEvents(SWindowData *window_data) {
XEvent event;
SWindowData_X11 *window_data_x11 = (SWindowData_X11 *) window_data->specific;
while ((window_data->close == false) && XPending(window_data_x11->display)) {
XNextEvent(window_data_x11->display, &event);
switch (event.type) {
processEvent(SWindowData *window_data, XEvent *event) {
switch (event->type) {
case KeyPress:
case KeyRelease:
{
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);
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, (mfb_key_mod) window_data->mod_keys, is_pressed);
@ -227,9 +224,9 @@ processEvents(SWindowData *window_data) {
case ButtonPress:
case ButtonRelease:
{
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);
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);
switch (button) {
case Button1:
case Button2:
@ -259,20 +256,21 @@ processEvents(SWindowData *window_data) {
break;
case MotionNotify:
window_data->mouse_pos_x = event.xmotion.x;
window_data->mouse_pos_y = event.xmotion.y;
kCall(mouse_move_func, event.xmotion.x, event.xmotion.y);
window_data->mouse_pos_x = event->xmotion.x;
window_data->mouse_pos_y = event->xmotion.y;
kCall(mouse_move_func, event->xmotion.x, event->xmotion.y);
break;
case ConfigureNotify:
{
window_data->window_width = event.xconfigure.width;
window_data->window_height = event.xconfigure.height;
window_data->window_width = event->xconfigure.width;
window_data->window_height = event->xconfigure.height;
window_data->dst_offset_x = 0;
window_data->dst_offset_y = 0;
window_data->dst_width = window_data->window_width;
window_data->dst_height = window_data->window_height;
SWindowData_X11 *window_data_x11 = (SWindowData_X11 *) window_data->specific;
XClearWindow(window_data_x11->display, window_data_x11->window);
kCall(resize_func, window_data->window_width, window_data->window_height);
}
@ -298,11 +296,21 @@ processEvents(SWindowData *window_data) {
break;
}
}
static void
processEvents(SWindowData *window_data) {
XEvent event;
SWindowData_X11 *window_data_x11 = (SWindowData_X11 *) window_data->specific;
while ((window_data->close == false) && XPending(window_data_x11->display)) {
XNextEvent(window_data_x11->display, &event);
processEvent(window_data, &event);
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void destroy(SWindowData *window_data);
void destroy_window_data(SWindowData *window_data);
mfb_update_state
mfb_update(struct mfb_window *window, void *buffer) {
@ -312,7 +320,7 @@ mfb_update(struct mfb_window *window, void *buffer) {
SWindowData *window_data = (SWindowData *) window;
if (window_data->close) {
destroy(window_data);
destroy_window_data(window_data);
return STATE_EXIT;
}
@ -368,7 +376,7 @@ mfb_update_events(struct mfb_window *window) {
SWindowData *window_data = (SWindowData *) window;
if (window_data->close) {
destroy(window_data);
destroy_window_data(window_data);
return STATE_EXIT;
}
@ -381,8 +389,56 @@ mfb_update_events(struct mfb_window *window) {
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
extern double g_time_for_frame;
bool
mfb_wait_sync(struct mfb_window *window) {
if (window == 0x0) {
return STATE_INVALID_WINDOW;
}
SWindowData *window_data = (SWindowData *) window;
if (window_data->close) {
destroy_window_data(window_data);
return false;
}
SWindowData_X11 *window_data_x11 = (SWindowData_X11 *) window_data->specific;
XFlush(window_data_x11->display);
XEvent event;
double current;
uint32_t millis = 1;
while(1) {
if(XEventsQueued(window_data_x11->display, QueuedAlready) > 0) {
XNextEvent(window_data_x11->display, &event);
processEvent(window_data, &event);
}
if(window_data->close) {
destroy_window_data(window_data);
return false;
}
current = mfb_timer_now(window_data_x11->timer);
if (current >= g_time_for_frame) {
mfb_timer_reset(window_data_x11->timer);
return true;
}
else if(current >= g_time_for_frame * 0.8) {
millis = 0;
}
usleep(millis * 1000);
//sched_yield();
}
return true;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
destroy(SWindowData *window_data) {
destroy_window_data(SWindowData *window_data) {
if (window_data != 0x0) {
if (window_data->specific != 0x0) {
SWindowData_X11 *window_data_x11 = (SWindowData_X11 *) window_data->specific;
@ -392,6 +448,7 @@ destroy(SWindowData *window_data) {
XDestroyWindow(window_data_x11->display, window_data_x11->window);
XCloseDisplay(window_data_x11->display);
}
mfb_timer_destroy(window_data_x11->timer);
memset(window_data_x11, 0, sizeof(SWindowData_X11));
free(window_data_x11);
}

View File

@ -114,8 +114,7 @@ main()
mfb_set_user_data(window, (void *) "Input Events Test");
for (;;)
{
do {
int i;
mfb_update_state state;
@ -143,7 +142,7 @@ main()
window = 0x0;
break;
}
}
} while(mfb_wait_sync(window));
return 0;
}

View File

@ -110,13 +110,11 @@ main()
mfb_set_user_data(window, (void *) "Input Events CPP Test");
for (;;)
{
do {
int i;
mfb_update_state state;
for (i = 0; i < WIDTH * HEIGHT; ++i)
{
for (i = 0; i < WIDTH * HEIGHT; ++i) {
noise = seed;
noise >>= 3;
noise ^= seed;
@ -133,7 +131,7 @@ main()
window = 0x0;
break;
}
}
} while(mfb_wait_sync(window));
return 0;
}

View File

@ -138,6 +138,8 @@ main()
pallete[64*7 + c] = MFB_RGB(255-col, 0, 0);
}
mfb_set_target_fps(10);
//--
float time = 0;
for (;;)
@ -191,6 +193,12 @@ main()
if(window_a == 0x0 && window_b == 0x0) {
break;
}
// Don't need to do this for both windows in the same thread
if(window_a != 0x0)
mfb_wait_sync(window_a);
else if(window_b != 0x0)
mfb_wait_sync(window_b);
}
return 0;

View File

@ -17,13 +17,11 @@ main()
if (!window)
return 0;
for (;;)
{
do {
int i;
mfb_update_state state;
for (i = 0; i < WIDTH * HEIGHT; ++i)
{
for (i = 0; i < WIDTH * HEIGHT; ++i) {
noise = seed;
noise >>= 3;
noise ^= seed;
@ -40,7 +38,7 @@ main()
window = 0x0;
break;
}
}
} while(mfb_wait_sync(window));
return 0;
}