1.1 --- a/src/core.c Wed Nov 01 20:09:49 2023 +0100 1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 1.3 @@ -1,125 +0,0 @@ 1.4 -/* 1.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 1.6 - * Copyright 2023 Mike Becker. All rights reserved. 1.7 - * 1.8 - * Redistribution and use in source and binary forms, with or without 1.9 - * modification, are permitted provided that the following conditions are met: 1.10 - * 1.11 - * 1. Redistributions of source code must retain the above copyright 1.12 - * notice, this list of conditions and the following disclaimer. 1.13 - * 1.14 - * 2. Redistributions in binary form must reproduce the above copyright 1.15 - * notice, this list of conditions and the following disclaimer in the 1.16 - * documentation and/or other materials provided with the distribution. 1.17 - * 1.18 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 1.19 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1.20 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1.21 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 1.22 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 1.23 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 1.24 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 1.25 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 1.26 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 1.27 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 1.28 - * POSSIBILITY OF SUCH DAMAGE. 1.29 - */ 1.30 - 1.31 -#include "ascension/core.h" 1.32 - 1.33 -#include <cx/linked_list.h> 1.34 -#include <cx/printf.h> 1.35 - 1.36 -#include <SDL2/SDL.h> 1.37 -#include <SDL2/SDL_ttf.h> 1.38 - 1.39 -AscContext asc_context; 1.40 - 1.41 -// forward declare the destructor functions that reside in other units 1.42 -void asc_window_destroy_impl(void* obj); 1.43 - 1.44 -void asc_context_initialize(void) { 1.45 - if (asc_test_flag(asc_context.flags, ASC_FLAG_INITILIZED)) 1.46 - return; 1.47 - asc_clear_flag(&asc_context.flags, ASC_FLAG_HAS_ERROR); 1.48 - 1.49 - // initialize error buffer 1.50 - cxBufferInit( 1.51 - &asc_context.error_buffer, 1.52 - NULL, 1.53 - 256, 1.54 - NULL, 1.55 - CX_BUFFER_AUTO_EXTEND 1.56 - ); 1.57 - 1.58 - // initialize lists 1.59 - asc_context.windows = cxLinkedListCreateSimple(CX_STORE_POINTERS); 1.60 - asc_context.windows->simple_destructor = 1.61 - (cx_destructor_func)asc_window_destroy_impl; 1.62 - 1.63 - // initialize SDL 1.64 - if (SDL_Init(SDL_INIT_VIDEO) < 0) { 1.65 - asc_error(SDL_GetError()); 1.66 - } else { 1.67 - if (TTF_Init() < 0) { 1.68 - asc_error(TTF_GetError()); 1.69 - } 1.70 - } 1.71 - SDL_ClearError(); 1.72 - asc_set_flag(&asc_context.flags, ASC_FLAG_INITILIZED); 1.73 - asc_dprintf("Ascension context initialized."); 1.74 -} 1.75 - 1.76 -void asc_context_destroy(void) { 1.77 - // destroy lists 1.78 - cxListDestroy(asc_context.windows); 1.79 - 1.80 - // quit SDL 1.81 - if (TTF_WasInit()) 1.82 - TTF_Quit(); 1.83 - SDL_Quit(); 1.84 - 1.85 - // destroy the error buffer 1.86 - cxBufferDestroy(&asc_context.error_buffer); 1.87 - asc_context.flags = 0; 1.88 - asc_dprintf("Ascension context destroyed."); 1.89 -} 1.90 - 1.91 -void asc_error_cchar(char const* text) { 1.92 - asc_error_cxstr(cx_str(text)); 1.93 -} 1.94 - 1.95 -void asc_error_cuchar(unsigned char const* text) { 1.96 - asc_error_cxstr(cx_str((char const*)text)); 1.97 -} 1.98 - 1.99 -void asc_error_cxstr(cxstring text) { 1.100 - if (text.length == 0) return; 1.101 - 1.102 - // write error to debug output 1.103 - asc_dprintf("ERROR: %*.s", (int)text.length, text.ptr); 1.104 - 1.105 - // write error to buffer 1.106 - CxBuffer* buf = &asc_context.error_buffer; 1.107 - cxBufferWrite(text.ptr, 1, text.length, buf); 1.108 - cxBufferPut(buf, '\n'); 1.109 - 1.110 - asc_set_flag(&asc_context.flags, ASC_FLAG_HAS_ERROR); 1.111 -} 1.112 - 1.113 -bool asc_has_error(void) { 1.114 - return asc_test_flag(asc_context.flags, ASC_FLAG_HAS_ERROR); 1.115 -} 1.116 - 1.117 -char const* asc_get_error(void) { 1.118 - // we zero-terminate the current buffer contents before providing them 1.119 - cxBufferPut(&asc_context.error_buffer, 0); 1.120 - --asc_context.error_buffer.pos; 1.121 - --asc_context.error_buffer.size; 1.122 - return asc_context.error_buffer.space; 1.123 -} 1.124 - 1.125 -void asc_clear_error(void) { 1.126 - cxBufferClear(&asc_context.error_buffer); 1.127 - asc_clear_flag(&asc_context.flags, ASC_FLAG_HAS_ERROR); 1.128 -}