Put linux now time on microseconds so dt is computed correctly; fix a bug in linux_schedule_step

This commit is contained in:
Allen Webster 2020-02-27 20:04:46 -08:00
parent d934161447
commit e300b209c1
6 changed files with 88 additions and 85 deletions

View File

@ -11,8 +11,8 @@
// TOP // TOP
#define FPS 60 #define FPS 60
#define frame_useconds (1000000 / FPS) #define frame_useconds (Million(1) / FPS)
#define frame_nseconds (UINT64_C(1000000000) / FPS) #define frame_nseconds (Billion(1) / FPS)
#define SLASH '/' #define SLASH '/'
#define DLL "so" #define DLL "so"
@ -168,7 +168,6 @@ struct Linux_Vars {
int epoll; int epoll;
int step_timer_fd; int step_timer_fd;
u64 last_step_time; u64 last_step_time;
b32 step_pending;
XCursor xcursors[APP_MOUSE_CURSOR_COUNT]; XCursor xcursors[APP_MOUSE_CURSOR_COUNT];
Application_Mouse_Cursor cursor; Application_Mouse_Cursor cursor;
@ -336,8 +335,8 @@ linux_system_get_file_list_filter(const struct dirent *dirent) {
} }
internal u64 internal u64
linux_ns_from_timespec(const struct timespec timespec) { linux_us_from_timespec(const struct timespec timespec) {
return timespec.tv_nsec + UINT64_C(1000000000) * timespec.tv_sec; return timespec.tv_nsec/Thousand(1) + Million(1) * timespec.tv_sec;
} }
internal File_Attribute_Flag internal File_Attribute_Flag
@ -351,29 +350,28 @@ internal File_Attributes
linux_file_attributes_from_struct_stat(struct stat* file_stat) { linux_file_attributes_from_struct_stat(struct stat* file_stat) {
File_Attributes result = {}; File_Attributes result = {};
result.size = file_stat->st_size; result.size = file_stat->st_size;
result.last_write_time = linux_ns_from_timespec(file_stat->st_mtim); result.last_write_time = linux_us_from_timespec(file_stat->st_mtim);
result.flags = linux_convert_file_attribute_flags(file_stat->st_mode); result.flags = linux_convert_file_attribute_flags(file_stat->st_mode);
return(result); return(result);
} }
internal void internal void
linux_schedule_step(){ linux_schedule_step(){
if(!__sync_bool_compare_and_swap(&linuxvars.step_pending, 0, 1)) {
return;
}
u64 now = system_now_time(); u64 now = system_now_time();
u64 diff = (now - linuxvars.last_step_time); u64 diff = (now - linuxvars.last_step_time);
struct itimerspec its = {}; struct itimerspec its = {};
timerfd_gettime(linuxvars.step_timer_fd, &its);
if(diff >= frame_nseconds) { if (diff > frame_useconds) {
its.it_value.tv_nsec = 1; its.it_value.tv_nsec = 1;
} else {
its.it_value.tv_nsec = frame_nseconds - diff;
}
timerfd_settime(linuxvars.step_timer_fd, 0, &its, NULL); timerfd_settime(linuxvars.step_timer_fd, 0, &its, NULL);
} else {
if (its.it_value.tv_sec == 0 && its.it_value.tv_nsec == 0){
its.it_value.tv_nsec = (frame_useconds - diff) * 1000UL;
timerfd_settime(linuxvars.step_timer_fd, 0, &its, NULL);
}
}
} }
enum wm_state_mode { enum wm_state_mode {
@ -583,8 +581,7 @@ linux_find_font(Face_Description* desc) {
} }
} }
FcPattern *pattern = FcPatternBuild( FcPattern *pattern = FcPatternBuild(0,
0,
FC_POSTSCRIPT_NAME, FcTypeString, name, FC_POSTSCRIPT_NAME, FcTypeString, name,
FC_SIZE, FcTypeDouble, size, FC_SIZE, FcTypeDouble, size,
FC_FONTFORMAT, FcTypeString, "TrueType", FC_FONTFORMAT, FcTypeString, "TrueType",
@ -1455,8 +1452,7 @@ linux_handle_x11_events() {
// Notify WM that we're still responding (don't grey our window out). // Notify WM that we're still responding (don't grey our window out).
else if(atom == linuxvars.atom__NET_WM_PING) { else if(atom == linuxvars.atom__NET_WM_PING) {
event.xclient.window = DefaultRootWindow(linuxvars.dpy); event.xclient.window = DefaultRootWindow(linuxvars.dpy);
XSendEvent( XSendEvent(linuxvars.dpy,
linuxvars.dpy,
event.xclient.window, event.xclient.window,
False, False,
SubstructureRedirectMask | SubstructureNotifyMask, SubstructureRedirectMask | SubstructureNotifyMask,
@ -1599,7 +1595,7 @@ main(int argc, char **argv){
//InitializeCriticalSection(&win32vars.thread_launch_mutex); //InitializeCriticalSection(&win32vars.thread_launch_mutex);
//InitializeConditionVariable(&win32vars.thread_launch_cv); //InitializeConditionVariable(&win32vars.thread_launch_cv);
linuxvars.clipboard_catch_all = true; linuxvars.clipboard_catch_all = false;
// NOTE(allen): load core // NOTE(allen): load core
System_Library core_library = {}; System_Library core_library = {};
@ -1729,6 +1725,7 @@ main(int argc, char **argv){
linux_schedule_step(); linux_schedule_step();
b32 first_step = true; b32 first_step = true;
u64 timer_start = system_now_time();
for (;;) { for (;;) {
@ -1813,11 +1810,17 @@ main(int argc, char **argv){
gl_render(&render_target); gl_render(&render_target);
glXSwapBuffers(linuxvars.dpy, linuxvars.win); glXSwapBuffers(linuxvars.dpy, linuxvars.win);
// TODO(allen): don't let the screen size change until HERE after the render
// NOTE(allen): Schedule a step if necessary
if (result.animating){
linux_schedule_step();
}
first_step = false; first_step = false;
linalloc_clear(linuxvars.frame_arena); linalloc_clear(linuxvars.frame_arena);
block_zero_struct(&linuxvars.input.trans); block_zero_struct(&linuxvars.input.trans);
linuxvars.step_pending = false;
} }
return 0; return 0;

View File

@ -267,8 +267,8 @@ internal u64
system_now_time(void){ system_now_time(void){
//LINUX_FN_DEBUG(); //LINUX_FN_DEBUG();
struct timespec time; struct timespec time;
clock_gettime(CLOCK_MONOTONIC_RAW, &time); clock_gettime(CLOCK_MONOTONIC, &time);
return linux_ns_from_timespec(time); return linux_us_from_timespec(time);
} }
internal Plat_Handle internal Plat_Handle