src/context.c

changeset 66
8297afa1c29c
parent 65
9c44c55d327a
equal deleted inserted replaced
65:9c44c55d327a 66:8297afa1c29c
40 struct timespec ts; 40 struct timespec ts;
41 clock_gettime(CLOCK_MONOTONIC, &ts); 41 clock_gettime(CLOCK_MONOTONIC, &ts);
42 return 1000000000ull*(uint64_t)ts.tv_sec + (uint64_t)ts.tv_nsec; 42 return 1000000000ull*(uint64_t)ts.tv_sec + (uint64_t)ts.tv_nsec;
43 } 43 }
44 44
45 // forward declare non-public font cache functions
46 void asc_font_cache_init(void);
47 void asc_font_cache_destroy(void);
48
45 void asc_context_initialize(void) { 49 void asc_context_initialize(void) {
46 if (asc_test_flag(asc_context.flags, ASC_FLAG_INITILIZED)) 50 if (asc_test_flag(asc_context.flags, ASC_FLAG_INITILIZED))
47 return; 51 return;
48 memset(&asc_context, 0, sizeof(AscContext)); 52 memset(&asc_context, 0, sizeof(AscContext));
49 53
50 // nothing is active right after initialization 54 // initialize the font cache
55 asc_font_cache_init();
56
57 // set a default font, but do not load it
58 asc_context.active_font.style = ASC_FONT_REGULAR;
59 asc_context.active_font.size = 14;
60
61 // no window, yet
51 asc_context.active_window = ASC_MAX_WINDOWS; 62 asc_context.active_window = ASC_MAX_WINDOWS;
52 asc_context.active_font = ASC_MAX_FONTS;
53 63
54 // initialize error buffer 64 // initialize error buffer
55 cxBufferInit( 65 cxBufferInit(
56 &asc_context.error_buffer, 66 &asc_context.error_buffer,
57 NULL, 67 NULL,
73 asc_set_flag(asc_context.flags, ASC_FLAG_INITILIZED); 83 asc_set_flag(asc_context.flags, ASC_FLAG_INITILIZED);
74 asc_dprintf("Ascension context initialized."); 84 asc_dprintf("Ascension context initialized.");
75 } 85 }
76 86
77 void asc_context_destroy(void) { 87 void asc_context_destroy(void) {
88 // destroy windows
78 for (unsigned int i = 0 ; i < ASC_MAX_WINDOWS ; i++) { 89 for (unsigned int i = 0 ; i < ASC_MAX_WINDOWS ; i++) {
79 asc_window_destroy(i); 90 asc_window_destroy(i);
80 } 91 }
81 // TODO: fix that fonts are currently leaking by reworking the font cache 92
93 // destroy the font cache
94 asc_font_cache_destroy();
82 95
83 // quit SDL 96 // quit SDL
84 if (TTF_WasInit()) 97 if (TTF_WasInit())
85 TTF_Quit(); 98 TTF_Quit();
86 SDL_Quit(); 99 SDL_Quit();
91 asc_dprintf("Ascension context destroyed."); 104 asc_dprintf("Ascension context destroyed.");
92 } 105 }
93 106
94 void asc_context_quit(void) { 107 void asc_context_quit(void) {
95 asc_set_flag(asc_context.flags, ASC_FLAG_QUIT); 108 asc_set_flag(asc_context.flags, ASC_FLAG_QUIT);
109 }
110
111 AscWindow *asc_active_window_assert() {
112 if (asc_context.active_window < ASC_MAX_WINDOWS) {
113 return &asc_context.windows[asc_context.active_window];
114 } else {
115 asc_dprintf("Window access attempted without active window");
116 abort();
117 }
96 } 118 }
97 119
98 static void asc_event_window_resized(Uint32 id, Sint32 width, Sint32 height) { 120 static void asc_event_window_resized(Uint32 id, Sint32 width, Sint32 height) {
99 for (unsigned int i = 0 ; i < ASC_MAX_WINDOWS ; i++) { 121 for (unsigned int i = 0 ; i < ASC_MAX_WINDOWS ; i++) {
100 if (asc_context.windows[i].id == id) { 122 if (asc_context.windows[i].id == id) {

mercurial