Sync updates at 60hz

This commit is contained in:
Bad Diode 2022-03-04 18:43:38 +01:00
parent 87ce7bbb4b
commit 6b29a0cb08
2 changed files with 85 additions and 70 deletions

View File

@ -6,7 +6,7 @@ EXE_NAME ?= uxnfb
BIN := $(BUILD_DIR)/$(EXE_NAME) BIN := $(BUILD_DIR)/$(EXE_NAME)
UXN_HEAD := $(BASE_UXN)/src/uxn.h UXN_HEAD := $(BASE_UXN)/src/uxn.h
TAL_SRC ?= $(BASE_UXN)/projects/examples/devices/screen.tal TAL_SRC ?= $(BASE_UXN)/projects/examples/devices/screen.tal
UXN_ROM ?= $(BUILD_DIR)/screen.rom UXN_ROM ?= $(BUILD_DIR)/rom.rom
UXN_ASM ?= $(BUILD_DIR)/uxnasm UXN_ASM ?= $(BUILD_DIR)/uxnasm
CC ?= cc CC ?= cc

View File

@ -1,9 +1,11 @@
#include <fcntl.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <time.h>
#include <stdbool.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h>
#include <linux/fb.h> #include <linux/fb.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/mman.h> #include <sys/mman.h>
@ -18,6 +20,21 @@ static Device *devctrl;
static Device *devmouse; static Device *devmouse;
static Device *devaudio; static Device *devaudio;
typedef struct timespec Time;
Time
time_now(){
struct timespec t;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t);
return t;
}
size_t
time_elapsed(Time since){
struct timespec now = time_now();
return (now.tv_sec - since.tv_sec) * 1e9 + (now.tv_nsec - since.tv_nsec);
}
void void
nil_talk(Device *d, u8 b0, u8 w) { nil_talk(Device *d, u8 b0, u8 w) {
(void)d; (void)d;
@ -228,19 +245,17 @@ main(int argc, char *argv[]) {
// Main loop. // Main loop.
uxn_eval(&u, 0x0100); uxn_eval(&u, 0x0100);
// u64 current_ticks = timer_get_ticks(); Time frame_time = time_now();
while (true) { while (true) {
size_t elapsed = time_elapsed(frame_time);
if (elapsed >= 16666666) {
// Echo input to standard output. // Echo input to standard output.
uxn_eval(&u, mempeek16(devscreen->dat, 0)); uxn_eval(&u, mempeek16(devscreen->dat, 0));
// Blit ppu.pixels to the framebuffer. // Blit ppu.pixels to the framebuffer.
blit_framebuffer(); blit_framebuffer();
frame_time = time_now();
// TODO: Sync at 60 Hz. }
// if ((timer_get_ticks() - current_ticks) < 16666) {
// wait(16666 - (timer_get_ticks() - current_ticks));
// }
// current_ticks = timer_get_ticks();
} }
return 0; return 0;