src/context.c

changeset 46
d3285aed65b3
parent 44
b3da4096c607
child 47
44457f6cb0a2
equal deleted inserted replaced
45:18de2af03531 46:d3285aed65b3
31 #include "ascension/shader.h" 31 #include "ascension/shader.h"
32 32
33 #include <SDL2/SDL.h> 33 #include <SDL2/SDL.h>
34 #include <SDL2/SDL_ttf.h> 34 #include <SDL2/SDL_ttf.h>
35 35
36 #include <time.h>
37
36 AscContext asc_context; 38 AscContext asc_context;
39
40 static uint64_t asc_nanos(void) {
41 struct timespec ts;
42 clock_gettime(CLOCK_MONOTONIC, &ts);
43 return 1000000000ull*(uint64_t)ts.tv_sec + (uint64_t)ts.tv_nsec;
44 }
37 45
38 void asc_context_initialize(void) { 46 void asc_context_initialize(void) {
39 if (asc_test_flag(asc_context.flags, ASC_FLAG_INITILIZED)) 47 if (asc_test_flag(asc_context.flags, ASC_FLAG_INITILIZED))
40 return; 48 return;
41 memset(&asc_context, 0, sizeof(AscContext)); 49 memset(&asc_context, 0, sizeof(AscContext));
56 if (TTF_Init() < 0) { 64 if (TTF_Init() < 0) {
57 asc_error(TTF_GetError()); 65 asc_error(TTF_GetError());
58 } 66 }
59 } 67 }
60 SDL_ClearError(); 68 SDL_ClearError();
69 asc_context.total_nanos = asc_nanos();
61 asc_set_flag(&asc_context.flags, ASC_FLAG_INITILIZED); 70 asc_set_flag(&asc_context.flags, ASC_FLAG_INITILIZED);
62 asc_dprintf("Ascension context initialized."); 71 asc_dprintf("Ascension context initialized.");
63 } 72 }
64 73
65 void asc_context_destroy(void) { 74 void asc_context_destroy(void) {
126 asc_window_sync(&asc_context.windows[i]); 135 asc_window_sync(&asc_context.windows[i]);
127 } 136 }
128 } 137 }
129 138
130 // compute frame time 139 // compute frame time
131 static Uint32 ticks; 140 uint64_t frame_nanos, ns;
132 Uint32 ticks_elapsed = SDL_GetTicks() - ticks; 141 do {
133 ticks = SDL_GetTicks(); 142 ns = asc_nanos();
134 asc_context.elapsed_millis = ticks_elapsed; 143 frame_nanos = ns - asc_context.total_nanos;
135 asc_context.elapsed = (float) ticks_elapsed / 1000.0f; 144 } while (frame_nanos == 0);
145 asc_context.frame_nanos = frame_nanos;
146 asc_context.total_nanos = ns;
136 147
137 return !asc_test_flag(asc_context.flags, ASC_FLAG_QUIT); 148 return !asc_test_flag(asc_context.flags, ASC_FLAG_QUIT);
138 } 149 }

mercurial