universe@0: /* universe@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@0: * Copyright 2023 Mike Becker. All rights reserved. universe@0: * universe@0: * Redistribution and use in source and binary forms, with or without universe@0: * modification, are permitted provided that the following conditions are met: universe@0: * universe@0: * 1. Redistributions of source code must retain the above copyright universe@0: * notice, this list of conditions and the following disclaimer. universe@0: * universe@0: * 2. Redistributions in binary form must reproduce the above copyright universe@0: * notice, this list of conditions and the following disclaimer in the universe@0: * documentation and/or other materials provided with the distribution. universe@0: * universe@0: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@0: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@0: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@0: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@0: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@0: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@0: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@0: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@0: * POSSIBILITY OF SUCH DAMAGE. universe@0: */ universe@0: universe@0: #include "ascension/core.h" universe@0: #include "ascension/utils.h" universe@0: universe@0: #include universe@0: #include universe@0: universe@0: static void asc_gl_debug_callback( universe@0: GLenum source, GLenum type, GLuint id, GLenum severity, universe@0: GLsizei length, const GLchar* message, universe@0: const void* userParam universe@0: ) { universe@0: cxmutstr buf = cx_asprintf( universe@0: "source = %d, id = %u, type = %d, severity= %d, message = %.*s", universe@0: source, id, type, severity, length, message); universe@0: if (type == GL_DEBUG_TYPE_ERROR) { universe@0: asc_error(buf.ptr); universe@0: } else { universe@0: asc_dprintf("GL debug: %*.s", (int)buf.length, buf.ptr); universe@0: } universe@0: cx_strfree(&buf); universe@0: } universe@0: universe@0: AscContext asc_context; universe@0: universe@0: // forward declarations universe@0: static void asc_window_destroy_impl(AscWindow* window); universe@0: universe@0: void asc_context_initialize(void) { universe@0: if (asc_test_flag(asc_context.flags, ASC_FLAG_INITILIZED)) universe@0: return; universe@0: asc_clear_flag(&asc_context.flags, ASC_FLAG_HAS_ERROR); universe@0: universe@0: // initialize error buffer universe@0: cxBufferInit( universe@0: &asc_context.error_buffer, universe@0: NULL, universe@0: 256, universe@0: NULL, universe@0: CX_BUFFER_AUTO_EXTEND universe@0: ); universe@0: universe@0: // initialize lists universe@0: asc_context.windows = cxLinkedListCreateSimple(CX_STORE_POINTERS); universe@0: asc_context.windows->simple_destructor = universe@0: (cx_destructor_func)asc_window_destroy_impl; universe@0: universe@0: // initialize SDL universe@0: if (SDL_Init(SDL_INIT_VIDEO) < 0) { universe@0: asc_error(SDL_GetError()); universe@0: } else { universe@0: if (TTF_Init() < 0) { universe@0: asc_error(TTF_GetError()); universe@0: } universe@0: } universe@0: SDL_ClearError(); universe@0: asc_set_flag(&asc_context.flags, ASC_FLAG_INITILIZED); universe@0: asc_dprintf("Ascension context initialized."); universe@0: } universe@0: universe@0: void asc_context_destroy(void) { universe@0: // destroy lists universe@0: cxListDestroy(asc_context.windows); universe@0: universe@0: // quit SDL universe@0: if (TTF_WasInit()) universe@0: TTF_Quit(); universe@0: SDL_Quit(); universe@0: universe@0: // destroy the error buffer universe@0: cxBufferDestroy(&asc_context.error_buffer); universe@0: asc_context.flags = 0; universe@0: asc_dprintf("Ascension context destroyed."); universe@0: } universe@0: universe@0: void asc_error_cchar(char const* text) { universe@0: asc_error_cxstr(cx_str(text)); universe@0: } universe@0: universe@0: void asc_error_cuchar(unsigned char const* text) { universe@0: asc_error_cxstr(cx_str((char const*)text)); universe@0: } universe@0: universe@0: void asc_error_cxstr(cxstring text) { universe@0: if (text.length == 0) return; universe@0: universe@0: // write error to debug output universe@0: asc_dprintf("ERROR: %*.s", (int)text.length, text.ptr); universe@0: universe@0: // write error to buffer universe@0: CxBuffer* buf = &asc_context.error_buffer; universe@0: cxBufferWrite(text.ptr, 1, text.length, buf); universe@0: cxBufferPut(buf, '\n'); universe@0: universe@0: asc_set_flag(&asc_context.flags, ASC_FLAG_HAS_ERROR); universe@0: } universe@0: universe@0: bool asc_has_error(void) { universe@0: return asc_test_flag(asc_context.flags, ASC_FLAG_HAS_ERROR); universe@0: } universe@0: universe@0: char const* asc_get_error(void) { universe@0: // we zero-terminate the current buffer contents before providing them universe@0: cxBufferPut(&asc_context.error_buffer, 0); universe@0: --asc_context.error_buffer.pos; universe@0: --asc_context.error_buffer.size; universe@0: return asc_context.error_buffer.space; universe@0: } universe@0: universe@0: void asc_clear_error(void) { universe@0: cxBufferClear(&asc_context.error_buffer); universe@0: asc_clear_flag(&asc_context.flags, ASC_FLAG_HAS_ERROR); universe@0: } universe@0: universe@0: static void asc_event_window_resized(Uint32 id, Sint32 width, Sint32 height) { universe@0: CxIterator iter = cxListIterator(asc_context.windows); universe@0: cx_foreach(AscWindow*, w, iter) { universe@0: if (w->id == id) { universe@0: w->width = width; universe@0: w->height = height; universe@0: return; universe@0: } universe@0: } universe@0: } universe@0: universe@0: bool asc_loop_next(void) { universe@0: // dispatch SDL events universe@0: SDL_Event event; universe@0: while (SDL_PollEvent(&event)) { universe@0: switch (event.type) { universe@0: case SDL_QUIT:return false; universe@0: case SDL_WINDOWEVENT: { universe@0: if (event.window.type == SDL_WINDOWEVENT_RESIZED) universe@0: asc_event_window_resized( universe@0: event.window.windowID, universe@0: event.window.data1, universe@0: event.window.data2 universe@0: ); universe@0: break; universe@0: } universe@0: case SDL_KEYDOWN: universe@0: // TODO: remove this code and implement key press map instead universe@0: if (event.key.keysym.sym == SDLK_ESCAPE) universe@0: return false; universe@0: break; universe@0: case SDL_KEYUP: universe@0: // TODO: implement key press map universe@0: break; universe@0: } universe@0: } universe@0: universe@0: // sync the windows universe@0: CxMutIterator windows = cxListMutIterator(asc_context.windows); universe@0: cx_foreach(AscWindow*, w, windows) { universe@0: asc_window_sync(w); universe@0: } universe@0: return true; universe@0: } universe@0: universe@0: void asc_window_settings_init_defaults(AscWindowSettings* settings) { universe@0: settings->depth_size = 24; universe@0: settings->vsync = 1; universe@0: settings->width = 800; universe@0: settings->height = 600; universe@0: settings->fullscreen = 0; universe@0: settings->gl_major_version = 3; universe@0: settings->gl_minor_version = 3; universe@0: settings->title = "Ascended Window"; universe@0: } universe@0: universe@0: void asc_window_initialize(AscWindow* window, AscWindowSettings const* settings) { universe@0: Uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN; universe@0: flags |= settings->fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : SDL_WINDOW_RESIZABLE; universe@0: universe@0: window->window = SDL_CreateWindow( universe@0: settings->title, universe@0: SDL_WINDOWPOS_CENTERED, universe@0: SDL_WINDOWPOS_CENTERED, universe@0: settings->width, universe@0: settings->height, universe@0: flags universe@0: ); universe@0: if (window->window == NULL) { universe@0: asc_error(SDL_GetError()); universe@0: return; universe@0: } universe@0: universe@0: window->id = SDL_GetWindowID(window->window); universe@0: SDL_GetWindowSize(window->window, &window->width, &window->height); universe@0: universe@0: SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); universe@0: SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, settings->gl_major_version); universe@0: SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, settings->gl_minor_version); universe@0: SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, settings->depth_size); universe@0: SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); universe@0: window->glctx = SDL_GL_CreateContext(window->window); universe@0: if (window->glctx == NULL) { universe@0: asc_dprintf("Creating GL context failed for window %u", window->id); universe@0: } else { universe@0: glewExperimental = GL_TRUE; universe@0: GLenum err = glewInit(); universe@0: if (err == GLEW_OK) { universe@0: SDL_GL_SetSwapInterval(settings->vsync); universe@0: glEnable(GL_DEPTH_TEST); universe@0: glEnable(GL_BLEND); universe@0: glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); universe@0: glEnable(GL_DEBUG_OUTPUT); universe@0: glDebugMessageCallback(asc_gl_debug_callback, NULL); universe@0: asc_dprintf("Window %u initialized", window->id); universe@0: cxListAdd(asc_context.windows, window); universe@0: return; universe@0: } else { universe@0: asc_error(glewGetErrorString(err)); universe@0: } universe@0: } universe@0: universe@0: // cleanup on error universe@0: if (window->glctx != NULL) { universe@0: SDL_GL_DeleteContext(window->glctx); universe@0: } universe@0: window->glctx = NULL; universe@0: SDL_DestroyWindow(window->window); universe@0: window->window = NULL; universe@0: window->id = 0; universe@0: } universe@0: universe@0: void asc_window_destroy_impl(AscWindow* window) { universe@0: // destory the GL context and the window universe@0: if (window->glctx != NULL) { universe@0: SDL_GL_DeleteContext(window->glctx); universe@0: } universe@0: if (window->window != NULL) { universe@0: SDL_DestroyWindow(window->window); universe@0: } universe@0: universe@0: // clean the data universe@0: asc_dprintf("Window %u and its OpenGL context destroyed.", window->id); universe@0: memset(window, 0, sizeof(AscWindow)); universe@0: } universe@0: universe@0: void asc_window_destroy(AscWindow* window) { universe@0: // find the window in the context and remove it universe@2: bool found = false; universe@0: CxMutIterator iter = cxListMutIterator(asc_context.windows); universe@0: cx_foreach(AscWindow*, w, iter) { universe@0: if (w == window) { universe@2: found = true; universe@0: cxIteratorFlagRemoval(iter); universe@0: } universe@0: } universe@2: if (!found) asc_window_destroy_impl(window); universe@0: } universe@0: universe@0: void asc_window_sync(AscWindow const* window) { universe@0: SDL_GL_MakeCurrent(window->window, window->glctx); universe@0: SDL_GL_SwapWindow(window->window); universe@0: glViewport(0, 0, window->width, window->height); universe@0: glClearColor(0.0f, 0.0f, 0.0f, 1.0f); universe@0: glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); universe@0: }