src/context.c

changeset 66
8297afa1c29c
parent 65
9c44c55d327a
     1.1 --- a/src/context.c	Thu Apr 18 22:53:55 2024 +0200
     1.2 +++ b/src/context.c	Fri Apr 19 22:28:29 2024 +0200
     1.3 @@ -42,14 +42,24 @@
     1.4      return 1000000000ull*(uint64_t)ts.tv_sec + (uint64_t)ts.tv_nsec;
     1.5  }
     1.6  
     1.7 +// forward declare non-public font cache functions
     1.8 +void asc_font_cache_init(void);
     1.9 +void asc_font_cache_destroy(void);
    1.10 +
    1.11  void asc_context_initialize(void) {
    1.12      if (asc_test_flag(asc_context.flags, ASC_FLAG_INITILIZED))
    1.13          return;
    1.14      memset(&asc_context, 0, sizeof(AscContext));
    1.15  
    1.16 -    // nothing is active right after initialization
    1.17 +    // initialize the font cache
    1.18 +    asc_font_cache_init();
    1.19 +
    1.20 +    // set a default font, but do not load it
    1.21 +    asc_context.active_font.style = ASC_FONT_REGULAR;
    1.22 +    asc_context.active_font.size = 14;
    1.23 +
    1.24 +    // no window, yet
    1.25      asc_context.active_window = ASC_MAX_WINDOWS;
    1.26 -    asc_context.active_font = ASC_MAX_FONTS;
    1.27  
    1.28      // initialize error buffer
    1.29      cxBufferInit(
    1.30 @@ -75,10 +85,13 @@
    1.31  }
    1.32  
    1.33  void asc_context_destroy(void) {
    1.34 +    // destroy windows
    1.35      for (unsigned int i = 0 ; i < ASC_MAX_WINDOWS ; i++) {
    1.36          asc_window_destroy(i);
    1.37      }
    1.38 -    // TODO: fix that fonts are currently leaking by reworking the font cache
    1.39 +
    1.40 +    // destroy the font cache
    1.41 +    asc_font_cache_destroy();
    1.42  
    1.43      // quit SDL
    1.44      if (TTF_WasInit())
    1.45 @@ -95,6 +108,15 @@
    1.46      asc_set_flag(asc_context.flags, ASC_FLAG_QUIT);
    1.47  }
    1.48  
    1.49 +AscWindow *asc_active_window_assert() {
    1.50 +    if (asc_context.active_window < ASC_MAX_WINDOWS) {
    1.51 +        return &asc_context.windows[asc_context.active_window];
    1.52 +    } else {
    1.53 +        asc_dprintf("Window access attempted without active window");
    1.54 +        abort();
    1.55 +    }
    1.56 +}
    1.57 +
    1.58  static void asc_event_window_resized(Uint32 id, Sint32 width, Sint32 height) {
    1.59      for (unsigned int i = 0 ; i < ASC_MAX_WINDOWS ; i++) {
    1.60          if (asc_context.windows[i].id == id) {

mercurial