1 /* |
|
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
3 * Copyright 2023 Mike Becker. All rights reserved. |
|
4 * |
|
5 * Redistribution and use in source and binary forms, with or without |
|
6 * modification, are permitted provided that the following conditions are met: |
|
7 * |
|
8 * 1. Redistributions of source code must retain the above copyright |
|
9 * notice, this list of conditions and the following disclaimer. |
|
10 * |
|
11 * 2. Redistributions in binary form must reproduce the above copyright |
|
12 * notice, this list of conditions and the following disclaimer in the |
|
13 * documentation and/or other materials provided with the distribution. |
|
14 * |
|
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
25 * POSSIBILITY OF SUCH DAMAGE. |
|
26 */ |
|
27 |
|
28 #include "ascension/core.h" |
|
29 |
|
30 #include <cx/linked_list.h> |
|
31 #include <cx/printf.h> |
|
32 |
|
33 #include <SDL2/SDL.h> |
|
34 #include <SDL2/SDL_ttf.h> |
|
35 |
|
36 AscContext asc_context; |
|
37 |
|
38 // forward declare the destructor functions that reside in other units |
|
39 void asc_window_destroy_impl(void* obj); |
|
40 |
|
41 void asc_context_initialize(void) { |
|
42 if (asc_test_flag(asc_context.flags, ASC_FLAG_INITILIZED)) |
|
43 return; |
|
44 asc_clear_flag(&asc_context.flags, ASC_FLAG_HAS_ERROR); |
|
45 |
|
46 // initialize error buffer |
|
47 cxBufferInit( |
|
48 &asc_context.error_buffer, |
|
49 NULL, |
|
50 256, |
|
51 NULL, |
|
52 CX_BUFFER_AUTO_EXTEND |
|
53 ); |
|
54 |
|
55 // initialize lists |
|
56 asc_context.windows = cxLinkedListCreateSimple(CX_STORE_POINTERS); |
|
57 asc_context.windows->simple_destructor = |
|
58 (cx_destructor_func)asc_window_destroy_impl; |
|
59 |
|
60 // initialize SDL |
|
61 if (SDL_Init(SDL_INIT_VIDEO) < 0) { |
|
62 asc_error(SDL_GetError()); |
|
63 } else { |
|
64 if (TTF_Init() < 0) { |
|
65 asc_error(TTF_GetError()); |
|
66 } |
|
67 } |
|
68 SDL_ClearError(); |
|
69 asc_set_flag(&asc_context.flags, ASC_FLAG_INITILIZED); |
|
70 asc_dprintf("Ascension context initialized."); |
|
71 } |
|
72 |
|
73 void asc_context_destroy(void) { |
|
74 // destroy lists |
|
75 cxListDestroy(asc_context.windows); |
|
76 |
|
77 // quit SDL |
|
78 if (TTF_WasInit()) |
|
79 TTF_Quit(); |
|
80 SDL_Quit(); |
|
81 |
|
82 // destroy the error buffer |
|
83 cxBufferDestroy(&asc_context.error_buffer); |
|
84 asc_context.flags = 0; |
|
85 asc_dprintf("Ascension context destroyed."); |
|
86 } |
|
87 |
|
88 void asc_error_cchar(char const* text) { |
|
89 asc_error_cxstr(cx_str(text)); |
|
90 } |
|
91 |
|
92 void asc_error_cuchar(unsigned char const* text) { |
|
93 asc_error_cxstr(cx_str((char const*)text)); |
|
94 } |
|
95 |
|
96 void asc_error_cxstr(cxstring text) { |
|
97 if (text.length == 0) return; |
|
98 |
|
99 // write error to debug output |
|
100 asc_dprintf("ERROR: %*.s", (int)text.length, text.ptr); |
|
101 |
|
102 // write error to buffer |
|
103 CxBuffer* buf = &asc_context.error_buffer; |
|
104 cxBufferWrite(text.ptr, 1, text.length, buf); |
|
105 cxBufferPut(buf, '\n'); |
|
106 |
|
107 asc_set_flag(&asc_context.flags, ASC_FLAG_HAS_ERROR); |
|
108 } |
|
109 |
|
110 bool asc_has_error(void) { |
|
111 return asc_test_flag(asc_context.flags, ASC_FLAG_HAS_ERROR); |
|
112 } |
|
113 |
|
114 char const* asc_get_error(void) { |
|
115 // we zero-terminate the current buffer contents before providing them |
|
116 cxBufferPut(&asc_context.error_buffer, 0); |
|
117 --asc_context.error_buffer.pos; |
|
118 --asc_context.error_buffer.size; |
|
119 return asc_context.error_buffer.space; |
|
120 } |
|
121 |
|
122 void asc_clear_error(void) { |
|
123 cxBufferClear(&asc_context.error_buffer); |
|
124 asc_clear_flag(&asc_context.flags, ASC_FLAG_HAS_ERROR); |
|
125 } |
|