From 7c37c06e4c632fd6b047a7ff50a3cc500bf594c8 Mon Sep 17 00:00:00 2001 From: vurtun Date: Sat, 2 Jan 2016 14:54:45 +0100 Subject: [PATCH] event-based updates round.2 --- demo/nanovg/nanovg.c | 17 +++++++++++++---- example/demo/demo.c | 12 ++++-------- example/filex/filex.c | 16 +++++++++------- example/nodedit/nodedit.c | 15 +++++---------- 4 files changed, 31 insertions(+), 29 deletions(-) diff --git a/demo/nanovg/nanovg.c b/demo/nanovg/nanovg.c index cb49eb6..8da1097 100644 --- a/demo/nanovg/nanovg.c +++ b/demo/nanovg/nanovg.c @@ -40,6 +40,7 @@ #include "dep/nanovg.c" /* macros */ +#define DTIME 30 #include "../../zahnrad.h" static void @@ -245,6 +246,7 @@ main(int argc, char *argv[]) SDL_Window *win; SDL_GLContext glContext; NVGcontext *vg = NULL; + int poll = 1; /* GUI */ struct demo gui; @@ -253,7 +255,7 @@ main(int argc, char *argv[]) exit(EXIT_FAILURE); } font_path = argv[1]; - font_height = 14; + font_height = 15; /* SDL */ SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_EVENTS); @@ -292,10 +294,17 @@ main(int argc, char *argv[]) while (running) { /* Input */ + int ret; SDL_Event evt; uint64_t dt, started = SDL_GetTicks(); + + zr_input_begin(&gui.ctx.input); - while (SDL_PollEvent(&evt)) { + if (!poll) { + ret = SDL_WaitEvent(&evt); + poll = 1; + } else ret = SDL_PollEvent(&evt); + while (ret) { if (evt.type == SDL_WINDOWEVENT) resize(&evt); else if (evt.type == SDL_QUIT) goto cleanup; else if (evt.type == SDL_KEYUP) key(&gui.ctx.input, &evt, zr_false); @@ -306,6 +315,7 @@ main(int argc, char *argv[]) else if (evt.type == SDL_TEXTINPUT) text(&gui.ctx.input, &evt); else if (evt.type == SDL_MOUSEWHEEL) zr_input_scroll(&gui.ctx.input,(float)evt.wheel.y); + ret = SDL_PollEvent(&evt); } zr_input_end(&gui.ctx.input); @@ -317,9 +327,8 @@ main(int argc, char *argv[]) glClearColor(0.2f, 0.2f, 0.2f, 1.0f); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); draw(vg, &gui.ctx, width, height); - dt = SDL_GetTicks() - started; - /*fprintf(stdout, "%lu\n", dt);*/ SDL_GL_SwapWindow(win); + poll = ((poll+1) & 4); } cleanup: diff --git a/example/demo/demo.c b/example/demo/demo.c index 7aefe51..db2a252 100644 --- a/example/demo/demo.c +++ b/example/demo/demo.c @@ -769,9 +769,9 @@ main(int argc, char *argv[]) if (!poll) { ret = SDL_WaitEvent(&evt); poll = 1; - } + } else ret = SDL_PollEvent(&evt); - while (SDL_PollEvent(&evt)) { + while (ret) { if (evt.type == SDL_WINDOWEVENT && evt.window.event == SDL_WINDOWEVENT_RESIZED) glViewport(0, 0, evt.window.data1, evt.window.data2); @@ -790,6 +790,7 @@ main(int argc, char *argv[]) text(&ctx.input, &evt); else if (evt.type == SDL_MOUSEWHEEL) zr_input_scroll(&ctx.input, evt.wheel.y); + ret = SDL_PollEvent(&evt); } zr_input_end(&ctx.input); @@ -804,12 +805,7 @@ main(int argc, char *argv[]) SDL_GetWindowSize(win, &width, &height); draw(vg, &ctx, width, height); SDL_GL_SwapWindow(win); - poll = (!((poll+1) & 4)); - - /* Timing */ - dt = SDL_GetTicks() - started; - if (dt < DTIME) - SDL_Delay(DTIME - dt); + poll = ((poll+1) & 4); } cleanup: diff --git a/example/filex/filex.c b/example/filex/filex.c index 7e8b8c5..7ad3ed1 100644 --- a/example/filex/filex.c +++ b/example/filex/filex.c @@ -830,14 +830,15 @@ main(int argc, char *argv[]) while (running) { /* Input */ + int ret; SDL_Event evt; - if (!poll) { - SDL_WaitEvent(&evt); - poll = 1; - } - zr_input_begin(&ctx.input); - while (SDL_PollEvent(&evt)) { + if (!poll) { + ret = SDL_WaitEvent(&evt); + poll = 1; + } else ret = SDL_PollEvent(&evt); + + while (ret) { if (evt.type == SDL_WINDOWEVENT) resize(&evt); else if (evt.type == SDL_QUIT) goto cleanup; else if (evt.type == SDL_KEYUP) key(&ctx.input, &evt, zr_false); @@ -847,6 +848,7 @@ main(int argc, char *argv[]) else if (evt.type == SDL_MOUSEMOTION) motion(&ctx.input, &evt); else if (evt.type == SDL_TEXTINPUT) text(&ctx.input, &evt); else if (evt.type == SDL_MOUSEWHEEL) zr_input_scroll(&ctx.input, evt.wheel.y); + ret = SDL_PollEvent(&evt); } zr_input_end(&ctx.input); @@ -858,7 +860,7 @@ main(int argc, char *argv[]) glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); draw(vg, &ctx, width, height); SDL_GL_SwapWindow(win); - poll = (!((poll+1) & 4)); + poll = ((poll+1) & 4); } cleanup: diff --git a/example/nodedit/nodedit.c b/example/nodedit/nodedit.c index ee4f9b5..296528d 100644 --- a/example/nodedit/nodedit.c +++ b/example/nodedit/nodedit.c @@ -647,13 +647,12 @@ main(int argc, char *argv[]) SDL_Event evt; started = SDL_GetTicks(); + zr_input_begin(&ctx.input); if (!poll) { ret = SDL_WaitEvent(&evt); poll = 1; - } - - zr_input_begin(&ctx.input); - while (SDL_PollEvent(&evt)) { + } else ret = SDL_PollEvent(&evt); + while (ret) { if (evt.type == SDL_WINDOWEVENT) resize(&evt); else if (evt.type == SDL_QUIT) goto cleanup; else if (evt.type == SDL_KEYUP) key(&ctx.input, &evt, zr_false); @@ -663,6 +662,7 @@ main(int argc, char *argv[]) else if (evt.type == SDL_MOUSEMOTION) motion(&ctx.input, &evt); else if (evt.type == SDL_TEXTINPUT) text(&ctx.input, &evt); else if (evt.type == SDL_MOUSEWHEEL) zr_input_scroll(&ctx.input, evt.wheel.y); + ret = SDL_PollEvent(&evt); } zr_input_end(&ctx.input); @@ -682,12 +682,7 @@ main(int argc, char *argv[]) SDL_GetWindowSize(win, &width, &height); draw(vg, &ctx, width, height); SDL_GL_SwapWindow(win); - poll = (!((poll+1) & 4)); - - /* Timing */ - dt = SDL_GetTicks() - started; - if (dt < DTIME) - SDL_Delay(DTIME - dt); + poll = ((poll+1) & 4); } cleanup: