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:
@ -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
|
||||
|
@ -53,5 +53,6 @@ typedef struct
|
||||
uint32_t buffer_stride;
|
||||
|
||||
uint32_t mod_keys;
|
||||
struct mfb_timer *timer;
|
||||
bool close;
|
||||
} SWindowData_Way;
|
||||
|
Reference in New Issue
Block a user