src/window.c

changeset 16
c5dde81b6fb2
parent 13
f04a49b2aeee
child 29
1d001eb694dc
     1.1 --- a/src/window.c	Wed Nov 08 23:17:07 2023 +0100
     1.2 +++ b/src/window.c	Wed Nov 15 22:51:40 2023 +0100
     1.3 @@ -45,61 +45,11 @@
     1.4      if (type == GL_DEBUG_TYPE_ERROR) {
     1.5          asc_error(buf.ptr);
     1.6      } else {
     1.7 -        asc_dprintf("GL debug: %*.s", (int)buf.length, buf.ptr);
     1.8 +        asc_dprintf("GL debug: %.*s", (int)buf.length, buf.ptr);
     1.9      }
    1.10      cx_strfree(&buf);
    1.11  }
    1.12  
    1.13 -
    1.14 -static void asc_event_window_resized(Uint32 id, Sint32 width, Sint32 height) {
    1.15 -    for (unsigned int i = 0 ; i < ASC_MAX_WINDOWS ; i++) {
    1.16 -        if (asc_context.windows[i].id == id) {
    1.17 -            asc_context.windows[i].dimensions.width = width;
    1.18 -            asc_context.windows[i].dimensions.height = height;
    1.19 -            asc_mat4f_ortho(asc_context.windows[i].projection, 0, (float) width, (float) height, 0);
    1.20 -            return;
    1.21 -        }
    1.22 -    }
    1.23 -}
    1.24 -
    1.25 -bool asc_loop_next(void) {
    1.26 -    // dispatch SDL events
    1.27 -    SDL_Event event;
    1.28 -    while (SDL_PollEvent(&event)) {
    1.29 -        switch (event.type) {
    1.30 -        case SDL_QUIT:
    1.31 -            asc_set_flag(&asc_context.flags, ASC_FLAG_QUIT);
    1.32 -            break;
    1.33 -        case SDL_WINDOWEVENT: {
    1.34 -            if (event.window.type == SDL_WINDOWEVENT_RESIZED)
    1.35 -                asc_event_window_resized(
    1.36 -                        event.window.windowID,
    1.37 -                        event.window.data1,
    1.38 -                        event.window.data2
    1.39 -                );
    1.40 -            break;
    1.41 -        }
    1.42 -        case SDL_KEYDOWN:
    1.43 -            // TODO: remove this code and implement key press map instead
    1.44 -            if (event.key.keysym.sym == SDLK_ESCAPE)
    1.45 -                return false;
    1.46 -            break;
    1.47 -        case SDL_KEYUP:
    1.48 -            // TODO: implement key press map
    1.49 -            break;
    1.50 -        }
    1.51 -    }
    1.52 -
    1.53 -    // sync the windows
    1.54 -    for (unsigned int i = 0 ; i < ASC_MAX_WINDOWS ; i++) {
    1.55 -        if (asc_context.windows[i].id > 0) {
    1.56 -            asc_window_sync(&asc_context.windows[i]);
    1.57 -        }
    1.58 -    }
    1.59 -
    1.60 -    return !asc_test_flag(asc_context.flags, ASC_FLAG_QUIT);
    1.61 -}
    1.62 -
    1.63  void asc_window_settings_init_defaults(AscWindowSettings* settings) {
    1.64      settings->depth_size = 24;
    1.65      settings->vsync = 1;
    1.66 @@ -170,8 +120,14 @@
    1.67              glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    1.68              glEnable(GL_DEBUG_OUTPUT);
    1.69              glDebugMessageCallback(asc_gl_debug_callback, NULL);
    1.70 +
    1.71              asc_dprintf("Window %u initialized", window->id);
    1.72 -            return window;
    1.73 +            if (asc_primitives_init(&window->primitives)) {
    1.74 +                asc_context.active_window = window;
    1.75 +                return window;
    1.76 +            } else {
    1.77 +                asc_dprintf("!!! Creating primitives for window %u failed !!!", window->id);
    1.78 +            }
    1.79          } else {
    1.80              asc_error(glewGetErrorString(err));
    1.81          }
    1.82 @@ -191,6 +147,15 @@
    1.83      // safeguard
    1.84      if (window->id == 0) return;
    1.85  
    1.86 +    // this window cannot be active anymore
    1.87 +    if (asc_context.active_window == window) {
    1.88 +        asc_context.active_window = NULL;
    1.89 +    }
    1.90 +
    1.91 +    // release context related data (we have to make the GL context current for this)
    1.92 +    SDL_GL_MakeCurrent(window->window, window->glctx);
    1.93 +    asc_primitives_destroy(&window->primitives);
    1.94 +
    1.95      // destroy the GL context and the window
    1.96      if (window->glctx != NULL) {
    1.97          SDL_GL_DeleteContext(window->glctx);
    1.98 @@ -199,15 +164,32 @@
    1.99          SDL_DestroyWindow(window->window);
   1.100      }
   1.101  
   1.102 +    // if another window was active, make the other context current again
   1.103 +    if (asc_context.active_window != NULL) {
   1.104 +        AscWindow const *aw = asc_context.active_window;
   1.105 +        SDL_GL_MakeCurrent(aw->window, aw->glctx);
   1.106 +    }
   1.107 +
   1.108      // clean the data
   1.109      asc_dprintf("Window %u and its OpenGL context destroyed.", window->id);
   1.110      memset(window, 0, sizeof(AscWindow));
   1.111  }
   1.112  
   1.113  void asc_window_sync(AscWindow const* window) {
   1.114 -    SDL_GL_MakeCurrent(window->window, window->glctx);
   1.115 +    AscWindow const *active_window = asc_context.active_window;
   1.116 +    if (window != active_window) {
   1.117 +        asc_window_activate(window);
   1.118 +    }
   1.119      SDL_GL_SwapWindow(window->window);
   1.120      glViewport(0, 0, window->dimensions.width, window->dimensions.height);
   1.121      glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
   1.122      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   1.123 +    if (window != active_window) {
   1.124 +        asc_window_activate(active_window);
   1.125 +    }
   1.126  }
   1.127 +
   1.128 +void asc_window_activate(AscWindow const *window) {
   1.129 +    SDL_GL_MakeCurrent(window->window, window->glctx);
   1.130 +    asc_context.active_window = window;
   1.131 +}

mercurial