improve code structure

Wed, 01 Nov 2023 21:00:33 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 01 Nov 2023 21:00:33 +0100
changeset 7
9dd76cbd6c90
parent 6
302971e8599b
child 8
756b49205a29

improve code structure

src/Makefile file | annotate | diff | comparison | revisions
src/ascension/ascension.h file | annotate | diff | comparison | revisions
src/ascension/context.h file | annotate | diff | comparison | revisions
src/ascension/core.h file | annotate | diff | comparison | revisions
src/ascension/error.h file | annotate | diff | comparison | revisions
src/ascension/utils.h file | annotate | diff | comparison | revisions
src/ascension/window.h file | annotate | diff | comparison | revisions
src/context.c file | annotate | diff | comparison | revisions
src/core.c file | annotate | diff | comparison | revisions
src/error.c file | annotate | diff | comparison | revisions
src/window.c file | annotate | diff | comparison | revisions
test/Makefile file | annotate | diff | comparison | revisions
test/sandbox.c file | annotate | diff | comparison | revisions
     1.1 --- a/src/Makefile	Wed Nov 01 20:09:49 2023 +0100
     1.2 +++ b/src/Makefile	Wed Nov 01 21:00:33 2023 +0100
     1.3 @@ -27,7 +27,7 @@
     1.4  
     1.5  BUILD_DIR=../build/lib
     1.6  
     1.7 -SRC  = core.c window.c
     1.8 +SRC  = context.c error.c window.c
     1.9  
    1.10  OBJ = $(SRC:%.c=$(BUILD_DIR)/%.o)
    1.11  
    1.12 @@ -40,12 +40,18 @@
    1.13  
    1.14  FORCE:
    1.15  
    1.16 -$(BUILD_DIR)/core.o: core.c ascension/core.h ascension/datatypes.h
    1.17 +$(BUILD_DIR)/context.o: context.c ascension/context.h ascension/window.h \
    1.18 + ascension/datatypes.h ascension/utils.h
    1.19  	@echo "Compiling $<"
    1.20  	$(CC) -o $@ $(CFLAGS) -c $<
    1.21  
    1.22 -$(BUILD_DIR)/window.o: window.c ascension/window.h ascension/core.h \
    1.23 - ascension/datatypes.h
    1.24 +$(BUILD_DIR)/error.o: error.c ascension/context.h ascension/window.h \
    1.25 + ascension/datatypes.h ascension/error.h ascension/utils.h
    1.26  	@echo "Compiling $<"
    1.27  	$(CC) -o $@ $(CFLAGS) -c $<
    1.28  
    1.29 +$(BUILD_DIR)/window.o: window.c ascension/window.h ascension/datatypes.h \
    1.30 + ascension/context.h ascension/window.h ascension/error.h
    1.31 +	@echo "Compiling $<"
    1.32 +	$(CC) -o $@ $(CFLAGS) -c $<
    1.33 +
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/ascension/ascension.h	Wed Nov 01 21:00:33 2023 +0100
     2.3 @@ -0,0 +1,35 @@
     2.4 +/*
     2.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     2.6 + * Copyright 2023 Mike Becker. All rights reserved.
     2.7 + *
     2.8 + * Redistribution and use in source and binary forms, with or without
     2.9 + * modification, are permitted provided that the following conditions are met:
    2.10 + *
    2.11 + *   1. Redistributions of source code must retain the above copyright
    2.12 + *      notice, this list of conditions and the following disclaimer.
    2.13 + *
    2.14 + *   2. Redistributions in binary form must reproduce the above copyright
    2.15 + *      notice, this list of conditions and the following disclaimer in the
    2.16 + *      documentation and/or other materials provided with the distribution.
    2.17 + *
    2.18 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    2.19 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    2.20 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    2.21 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    2.22 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    2.23 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    2.24 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    2.25 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    2.26 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    2.27 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    2.28 + * POSSIBILITY OF SUCH DAMAGE.
    2.29 + */
    2.30 +
    2.31 +#ifndef ASCENSION_H
    2.32 +#define ASCENSION_H
    2.33 +
    2.34 +#include "context.h"
    2.35 +#include "error.h"
    2.36 +
    2.37 +#endif /* ASCENSION_H */
    2.38 +
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/ascension/context.h	Wed Nov 01 21:00:33 2023 +0100
     3.3 @@ -0,0 +1,69 @@
     3.4 +/*
     3.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3.6 + * Copyright 2023 Mike Becker. All rights reserved.
     3.7 + *
     3.8 + * Redistribution and use in source and binary forms, with or without
     3.9 + * modification, are permitted provided that the following conditions are met:
    3.10 + *
    3.11 + *   1. Redistributions of source code must retain the above copyright
    3.12 + *      notice, this list of conditions and the following disclaimer.
    3.13 + *
    3.14 + *   2. Redistributions in binary form must reproduce the above copyright
    3.15 + *      notice, this list of conditions and the following disclaimer in the
    3.16 + *      documentation and/or other materials provided with the distribution.
    3.17 + *
    3.18 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    3.19 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    3.20 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    3.21 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    3.22 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    3.23 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    3.24 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    3.25 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    3.26 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    3.27 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    3.28 + * POSSIBILITY OF SUCH DAMAGE.
    3.29 + */
    3.30 +
    3.31 +#ifndef ASCENSION_CONTEXT_H
    3.32 +#define ASCENSION_CONTEXT_H
    3.33 +
    3.34 +#include "window.h"
    3.35 +
    3.36 +#include <cx/buffer.h>
    3.37 +#include <cx/list.h>
    3.38 +
    3.39 +#ifdef __cplusplus
    3.40 +extern "C" {
    3.41 +#endif
    3.42 +
    3.43 +/** The flag for the overall initialized state. */
    3.44 +#define ASC_FLAG_INITILIZED  0x01u
    3.45 +
    3.46 +/** Flag is set, when error buffer contains new error information. */
    3.47 +#define ASC_FLAG_HAS_ERROR  0x02u
    3.48 +
    3.49 +/** Flag is set, when SDL wants to quit the application. */
    3.50 +#define ASC_FLAG_QUIT 0x80000000u
    3.51 +
    3.52 +/**
    3.53 + * The global ascension context.
    3.54 + */
    3.55 +typedef struct AscContext {
    3.56 +    unsigned int flags;
    3.57 +    CxBuffer error_buffer;
    3.58 +    AscWindow windows[ASC_MAX_WINDOWS];
    3.59 +} AscContext;
    3.60 +
    3.61 +/** Global ascension context. */
    3.62 +extern AscContext asc_context;
    3.63 +
    3.64 +void asc_context_initialize(void);
    3.65 +void asc_context_destroy(void);
    3.66 +
    3.67 +#ifdef __cplusplus
    3.68 +} // extern "C"
    3.69 +#endif
    3.70 +
    3.71 +#endif /* ASCENSION_CONTEXT_H */
    3.72 +
     4.1 --- a/src/ascension/core.h	Wed Nov 01 20:09:49 2023 +0100
     4.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.3 @@ -1,100 +0,0 @@
     4.4 -/*
     4.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     4.6 - * Copyright 2023 Mike Becker. All rights reserved.
     4.7 - *
     4.8 - * Redistribution and use in source and binary forms, with or without
     4.9 - * modification, are permitted provided that the following conditions are met:
    4.10 - *
    4.11 - *   1. Redistributions of source code must retain the above copyright
    4.12 - *      notice, this list of conditions and the following disclaimer.
    4.13 - *
    4.14 - *   2. Redistributions in binary form must reproduce the above copyright
    4.15 - *      notice, this list of conditions and the following disclaimer in the
    4.16 - *      documentation and/or other materials provided with the distribution.
    4.17 - *
    4.18 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    4.19 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    4.20 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    4.21 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    4.22 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    4.23 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    4.24 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    4.25 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    4.26 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    4.27 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    4.28 - * POSSIBILITY OF SUCH DAMAGE.
    4.29 - */
    4.30 -
    4.31 -#ifndef ASCENSION_CORE_H
    4.32 -#define ASCENSION_CORE_H
    4.33 -
    4.34 -#include <cx/string.h>
    4.35 -#include <cx/buffer.h>
    4.36 -#include <cx/list.h>
    4.37 -
    4.38 -#include "datatypes.h"
    4.39 -
    4.40 -#ifdef __cplusplus
    4.41 -extern "C" {
    4.42 -#endif
    4.43 -
    4.44 -/** The flag for the overall initialized state. */
    4.45 -#define ASC_FLAG_INITILIZED  0x01u
    4.46 -
    4.47 -/** Flag is set, when error buffer contains new error information. */
    4.48 -#define ASC_FLAG_HAS_ERROR  0x02u
    4.49 -
    4.50 -/** Flag is set, when SDL wants to quit the application. */
    4.51 -#define ASC_FLAG_QUIT 0x80000000u
    4.52 -
    4.53 -typedef struct AscContext {
    4.54 -    CxBuffer error_buffer;
    4.55 -    CxList *windows;
    4.56 -    unsigned int flags;
    4.57 -} AscContext;
    4.58 -
    4.59 -/** Global ascension context. */
    4.60 -extern AscContext asc_context;
    4.61 -
    4.62 -void asc_context_initialize(void);
    4.63 -void asc_context_destroy(void);
    4.64 -
    4.65 -static inline bool asc_test_flag(unsigned int reg, int flag) {
    4.66 -    return (reg & flag) == flag;
    4.67 -}
    4.68 -
    4.69 -static inline void asc_set_flag(unsigned int *reg, int flag) {
    4.70 -    *reg |= flag;
    4.71 -}
    4.72 -
    4.73 -static inline void asc_clear_flag(unsigned int *reg, int flag) {
    4.74 -    *reg &= ~flag;
    4.75 -}
    4.76 -
    4.77 -void asc_error_cxstr(cxstring text);
    4.78 -void asc_error_cchar(char const* text);
    4.79 -void asc_error_cuchar(unsigned char const* text);
    4.80 -
    4.81 -#define asc_error(text) _Generic((text),     \
    4.82 -    char const*: asc_error_cchar,            \
    4.83 -    unsigned char const*: asc_error_cuchar,  \
    4.84 -    char*: asc_error_cchar,                  \
    4.85 -    unsigned char*: asc_error_cuchar,        \
    4.86 -    cxstring: asc_error_cxstr)(text)
    4.87 -
    4.88 -bool asc_has_error(void);
    4.89 -char const* asc_get_error(void);
    4.90 -void asc_clear_error(void);
    4.91 -
    4.92 -#ifdef NDEBUG
    4.93 -#define asc_dprintf(...)
    4.94 -#else
    4.95 -#define asc_dprintf(...) printf(__VA_ARGS__); putchar('\n')
    4.96 -#endif
    4.97 -
    4.98 -#ifdef __cplusplus
    4.99 -} // extern "C"
   4.100 -#endif
   4.101 -
   4.102 -#endif /* ASCENSION_CORE_H */
   4.103 -
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/src/ascension/error.h	Wed Nov 01 21:00:33 2023 +0100
     5.3 @@ -0,0 +1,63 @@
     5.4 +/*
     5.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     5.6 + * Copyright 2023 Mike Becker. All rights reserved.
     5.7 + *
     5.8 + * Redistribution and use in source and binary forms, with or without
     5.9 + * modification, are permitted provided that the following conditions are met:
    5.10 + *
    5.11 + *   1. Redistributions of source code must retain the above copyright
    5.12 + *      notice, this list of conditions and the following disclaimer.
    5.13 + *
    5.14 + *   2. Redistributions in binary form must reproduce the above copyright
    5.15 + *      notice, this list of conditions and the following disclaimer in the
    5.16 + *      documentation and/or other materials provided with the distribution.
    5.17 + *
    5.18 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    5.19 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    5.20 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    5.21 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    5.22 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    5.23 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    5.24 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    5.25 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    5.26 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    5.27 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    5.28 + * POSSIBILITY OF SUCH DAMAGE.
    5.29 + */
    5.30 +
    5.31 +#ifndef ASCENSION_ERROR_H
    5.32 +#define ASCENSION_ERROR_H
    5.33 +
    5.34 +#include <cx/string.h>
    5.35 +
    5.36 +#ifdef __cplusplus
    5.37 +extern "C" {
    5.38 +#endif
    5.39 +
    5.40 +void asc_error_cxstr(cxstring text);
    5.41 +void asc_error_cchar(char const* text);
    5.42 +void asc_error_cuchar(unsigned char const* text);
    5.43 +
    5.44 +#define asc_error(text) _Generic((text),     \
    5.45 +    char const*: asc_error_cchar,            \
    5.46 +    unsigned char const*: asc_error_cuchar,  \
    5.47 +    char*: asc_error_cchar,                  \
    5.48 +    unsigned char*: asc_error_cuchar,        \
    5.49 +    cxstring: asc_error_cxstr)(text)
    5.50 +
    5.51 +bool asc_has_error(void);
    5.52 +char const* asc_get_error(void);
    5.53 +void asc_clear_error(void);
    5.54 +
    5.55 +#ifdef NDEBUG
    5.56 +#define asc_dprintf(...)
    5.57 +#else
    5.58 +#define asc_dprintf(...) printf(__VA_ARGS__); putchar('\n')
    5.59 +#endif
    5.60 +
    5.61 +#ifdef __cplusplus
    5.62 +} // extern "C"
    5.63 +#endif
    5.64 +
    5.65 +#endif /* ASCENSION_ERROR_H */
    5.66 +
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/src/ascension/utils.h	Wed Nov 01 21:00:33 2023 +0100
     6.3 @@ -0,0 +1,55 @@
     6.4 +/*
     6.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     6.6 + * Copyright 2023 Mike Becker. All rights reserved.
     6.7 + *
     6.8 + * Redistribution and use in source and binary forms, with or without
     6.9 + * modification, are permitted provided that the following conditions are met:
    6.10 + *
    6.11 + *   1. Redistributions of source code must retain the above copyright
    6.12 + *      notice, this list of conditions and the following disclaimer.
    6.13 + *
    6.14 + *   2. Redistributions in binary form must reproduce the above copyright
    6.15 + *      notice, this list of conditions and the following disclaimer in the
    6.16 + *      documentation and/or other materials provided with the distribution.
    6.17 + *
    6.18 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    6.19 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    6.20 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    6.21 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    6.22 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    6.23 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    6.24 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    6.25 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    6.26 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    6.27 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    6.28 + * POSSIBILITY OF SUCH DAMAGE.
    6.29 + */
    6.30 +
    6.31 +#ifndef ASCENSION_UTILS_H
    6.32 +#define ASCENSION_UTILS_H
    6.33 +
    6.34 +#include <stdbool.h>
    6.35 +
    6.36 +#ifdef __cplusplus
    6.37 +extern "C" {
    6.38 +#endif
    6.39 +
    6.40 +
    6.41 +static inline bool asc_test_flag(unsigned int reg, int flag) {
    6.42 +    return (reg & flag) == flag;
    6.43 +}
    6.44 +
    6.45 +static inline void asc_set_flag(unsigned int *reg, int flag) {
    6.46 +    *reg |= flag;
    6.47 +}
    6.48 +
    6.49 +static inline void asc_clear_flag(unsigned int *reg, int flag) {
    6.50 +    *reg &= ~flag;
    6.51 +}
    6.52 +
    6.53 +#ifdef __cplusplus
    6.54 +} // extern "C"
    6.55 +#endif
    6.56 +
    6.57 +#endif /* ASCENSION_UTILS_H */
    6.58 +
     7.1 --- a/src/ascension/window.h	Wed Nov 01 20:09:49 2023 +0100
     7.2 +++ b/src/ascension/window.h	Wed Nov 01 21:00:33 2023 +0100
     7.3 @@ -30,7 +30,13 @@
     7.4  
     7.5  #include <SDL2/SDL.h>
     7.6  
     7.7 -#include "core.h"
     7.8 +#include "datatypes.h"
     7.9 +
    7.10 +#ifndef ASC_MAX_WINDOWS
    7.11 +/** The maximum number of windows that can exist simultaneously. */
    7.12 +#define ASC_MAX_WINDOWS 4
    7.13 +#endif // ASC_MAX_WINDOWS
    7.14 +
    7.15  
    7.16  #ifdef __cplusplus
    7.17  extern "C" {
    7.18 @@ -71,10 +77,14 @@
    7.19  /**
    7.20   * Creates and initializes a new window and a corresponding OpenGL context.
    7.21   *
    7.22 - * @param window the window structure
    7.23 + * The index specified must not be in use by another window already.
    7.24 + * The maximum number of windows is defined by #ASC_MAX_WINDOWS.
    7.25 + *
    7.26 + * @param index the index of the new window
    7.27   * @param settings the settings to be used for initialization
    7.28 + * @return a pointer to the window data or \c NULL if initialization failed
    7.29   */
    7.30 -void asc_window_initialize(AscWindow* window, AscWindowSettings const* settings);
    7.31 +AscWindow *asc_window_initialize(unsigned int index, AscWindowSettings const* settings);
    7.32  
    7.33  /**
    7.34   * Destroys the window and its OpenGL context.
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/src/context.c	Wed Nov 01 21:00:33 2023 +0100
     8.3 @@ -0,0 +1,84 @@
     8.4 +/*
     8.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     8.6 + * Copyright 2023 Mike Becker. All rights reserved.
     8.7 + *
     8.8 + * Redistribution and use in source and binary forms, with or without
     8.9 + * modification, are permitted provided that the following conditions are met:
    8.10 + *
    8.11 + *   1. Redistributions of source code must retain the above copyright
    8.12 + *      notice, this list of conditions and the following disclaimer.
    8.13 + *
    8.14 + *   2. Redistributions in binary form must reproduce the above copyright
    8.15 + *      notice, this list of conditions and the following disclaimer in the
    8.16 + *      documentation and/or other materials provided with the distribution.
    8.17 + *
    8.18 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    8.19 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    8.20 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    8.21 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    8.22 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    8.23 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    8.24 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    8.25 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    8.26 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    8.27 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    8.28 + * POSSIBILITY OF SUCH DAMAGE.
    8.29 + */
    8.30 +
    8.31 +#include "ascension/context.h"
    8.32 +#include "ascension/error.h"
    8.33 +#include "ascension/utils.h"
    8.34 +
    8.35 +#include <cx/linked_list.h>
    8.36 +
    8.37 +#include <SDL2/SDL.h>
    8.38 +#include <SDL2/SDL_ttf.h>
    8.39 +
    8.40 +AscContext asc_context;
    8.41 +
    8.42 +void asc_context_initialize(void) {
    8.43 +    if (asc_test_flag(asc_context.flags, ASC_FLAG_INITILIZED))
    8.44 +        return;
    8.45 +    asc_clear_flag(&asc_context.flags, ASC_FLAG_HAS_ERROR);
    8.46 +
    8.47 +    // initialize error buffer
    8.48 +    cxBufferInit(
    8.49 +            &asc_context.error_buffer,
    8.50 +            NULL,
    8.51 +            256,
    8.52 +            NULL,
    8.53 +            CX_BUFFER_AUTO_EXTEND
    8.54 +    );
    8.55 +
    8.56 +    // initialize data
    8.57 +    memset(asc_context.windows, 0, sizeof (asc_context.windows));
    8.58 +
    8.59 +    // initialize SDL
    8.60 +    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
    8.61 +        asc_error(SDL_GetError());
    8.62 +    } else {
    8.63 +        if (TTF_Init() < 0) {
    8.64 +            asc_error(TTF_GetError());
    8.65 +        }
    8.66 +    }
    8.67 +    SDL_ClearError();
    8.68 +    asc_set_flag(&asc_context.flags, ASC_FLAG_INITILIZED);
    8.69 +    asc_dprintf("Ascension context initialized.");
    8.70 +}
    8.71 +
    8.72 +void asc_context_destroy(void) {
    8.73 +    // destroy data
    8.74 +    for (unsigned int i = 0 ; i < ASC_MAX_WINDOWS ; i++) {
    8.75 +        asc_window_destroy(&asc_context.windows[i]);
    8.76 +    }
    8.77 +
    8.78 +    // quit SDL
    8.79 +    if (TTF_WasInit())
    8.80 +        TTF_Quit();
    8.81 +    SDL_Quit();
    8.82 +
    8.83 +    // destroy the error buffer
    8.84 +    cxBufferDestroy(&asc_context.error_buffer);
    8.85 +    asc_context.flags = 0;
    8.86 +    asc_dprintf("Ascension context destroyed.");
    8.87 +}
     9.1 --- a/src/core.c	Wed Nov 01 20:09:49 2023 +0100
     9.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.3 @@ -1,125 +0,0 @@
     9.4 -/*
     9.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     9.6 - * Copyright 2023 Mike Becker. All rights reserved.
     9.7 - *
     9.8 - * Redistribution and use in source and binary forms, with or without
     9.9 - * modification, are permitted provided that the following conditions are met:
    9.10 - *
    9.11 - *   1. Redistributions of source code must retain the above copyright
    9.12 - *      notice, this list of conditions and the following disclaimer.
    9.13 - *
    9.14 - *   2. Redistributions in binary form must reproduce the above copyright
    9.15 - *      notice, this list of conditions and the following disclaimer in the
    9.16 - *      documentation and/or other materials provided with the distribution.
    9.17 - *
    9.18 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    9.19 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    9.20 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    9.21 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    9.22 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    9.23 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    9.24 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    9.25 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    9.26 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    9.27 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    9.28 - * POSSIBILITY OF SUCH DAMAGE.
    9.29 - */
    9.30 -
    9.31 -#include "ascension/core.h"
    9.32 -
    9.33 -#include <cx/linked_list.h>
    9.34 -#include <cx/printf.h>
    9.35 -
    9.36 -#include <SDL2/SDL.h>
    9.37 -#include <SDL2/SDL_ttf.h>
    9.38 -
    9.39 -AscContext asc_context;
    9.40 -
    9.41 -// forward declare the destructor functions that reside in other units
    9.42 -void asc_window_destroy_impl(void* obj);
    9.43 -
    9.44 -void asc_context_initialize(void) {
    9.45 -    if (asc_test_flag(asc_context.flags, ASC_FLAG_INITILIZED))
    9.46 -        return;
    9.47 -    asc_clear_flag(&asc_context.flags, ASC_FLAG_HAS_ERROR);
    9.48 -
    9.49 -    // initialize error buffer
    9.50 -    cxBufferInit(
    9.51 -            &asc_context.error_buffer,
    9.52 -            NULL,
    9.53 -            256,
    9.54 -            NULL,
    9.55 -            CX_BUFFER_AUTO_EXTEND
    9.56 -    );
    9.57 -
    9.58 -    // initialize lists
    9.59 -    asc_context.windows = cxLinkedListCreateSimple(CX_STORE_POINTERS);
    9.60 -    asc_context.windows->simple_destructor =
    9.61 -            (cx_destructor_func)asc_window_destroy_impl;
    9.62 -
    9.63 -    // initialize SDL
    9.64 -    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
    9.65 -        asc_error(SDL_GetError());
    9.66 -    } else {
    9.67 -        if (TTF_Init() < 0) {
    9.68 -            asc_error(TTF_GetError());
    9.69 -        }
    9.70 -    }
    9.71 -    SDL_ClearError();
    9.72 -    asc_set_flag(&asc_context.flags, ASC_FLAG_INITILIZED);
    9.73 -    asc_dprintf("Ascension context initialized.");
    9.74 -}
    9.75 -
    9.76 -void asc_context_destroy(void) {
    9.77 -    // destroy lists
    9.78 -    cxListDestroy(asc_context.windows);
    9.79 -
    9.80 -    // quit SDL
    9.81 -    if (TTF_WasInit())
    9.82 -        TTF_Quit();
    9.83 -    SDL_Quit();
    9.84 -
    9.85 -    // destroy the error buffer
    9.86 -    cxBufferDestroy(&asc_context.error_buffer);
    9.87 -    asc_context.flags = 0;
    9.88 -    asc_dprintf("Ascension context destroyed.");
    9.89 -}
    9.90 -
    9.91 -void asc_error_cchar(char const* text) {
    9.92 -    asc_error_cxstr(cx_str(text));
    9.93 -}
    9.94 -
    9.95 -void asc_error_cuchar(unsigned char const* text) {
    9.96 -    asc_error_cxstr(cx_str((char const*)text));
    9.97 -}
    9.98 -
    9.99 -void asc_error_cxstr(cxstring text) {
   9.100 -    if (text.length == 0) return;
   9.101 -
   9.102 -    // write error to debug output
   9.103 -    asc_dprintf("ERROR: %*.s", (int)text.length, text.ptr);
   9.104 -
   9.105 -    // write error to buffer
   9.106 -    CxBuffer* buf = &asc_context.error_buffer;
   9.107 -    cxBufferWrite(text.ptr, 1, text.length, buf);
   9.108 -    cxBufferPut(buf, '\n');
   9.109 -
   9.110 -    asc_set_flag(&asc_context.flags, ASC_FLAG_HAS_ERROR);
   9.111 -}
   9.112 -
   9.113 -bool asc_has_error(void) {
   9.114 -    return asc_test_flag(asc_context.flags, ASC_FLAG_HAS_ERROR);
   9.115 -}
   9.116 -
   9.117 -char const* asc_get_error(void) {
   9.118 -    // we zero-terminate the current buffer contents before providing them
   9.119 -    cxBufferPut(&asc_context.error_buffer, 0);
   9.120 -    --asc_context.error_buffer.pos;
   9.121 -    --asc_context.error_buffer.size;
   9.122 -    return asc_context.error_buffer.space;
   9.123 -}
   9.124 -
   9.125 -void asc_clear_error(void) {
   9.126 -    cxBufferClear(&asc_context.error_buffer);
   9.127 -    asc_clear_flag(&asc_context.flags, ASC_FLAG_HAS_ERROR);
   9.128 -}
    10.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    10.2 +++ b/src/error.c	Wed Nov 01 21:00:33 2023 +0100
    10.3 @@ -0,0 +1,71 @@
    10.4 +/*
    10.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
    10.6 + * Copyright 2023 Mike Becker. All rights reserved.
    10.7 + *
    10.8 + * Redistribution and use in source and binary forms, with or without
    10.9 + * modification, are permitted provided that the following conditions are met:
   10.10 + *
   10.11 + *   1. Redistributions of source code must retain the above copyright
   10.12 + *      notice, this list of conditions and the following disclaimer.
   10.13 + *
   10.14 + *   2. Redistributions in binary form must reproduce the above copyright
   10.15 + *      notice, this list of conditions and the following disclaimer in the
   10.16 + *      documentation and/or other materials provided with the distribution.
   10.17 + *
   10.18 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
   10.19 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   10.20 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   10.21 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
   10.22 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   10.23 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   10.24 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   10.25 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   10.26 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   10.27 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   10.28 + * POSSIBILITY OF SUCH DAMAGE.
   10.29 + */
   10.30 +
   10.31 +#include "ascension/context.h"
   10.32 +#include "ascension/error.h"
   10.33 +#include "ascension/utils.h"
   10.34 +
   10.35 +#include <cx/buffer.h>
   10.36 +
   10.37 +void asc_error_cchar(char const* text) {
   10.38 +    asc_error_cxstr(cx_str(text));
   10.39 +}
   10.40 +
   10.41 +void asc_error_cuchar(unsigned char const* text) {
   10.42 +    asc_error_cxstr(cx_str((char const*)text));
   10.43 +}
   10.44 +
   10.45 +void asc_error_cxstr(cxstring text) {
   10.46 +    if (text.length == 0) return;
   10.47 +
   10.48 +    // write error to debug output
   10.49 +    asc_dprintf("ERROR: %*.s", (int)text.length, text.ptr);
   10.50 +
   10.51 +    // write error to buffer
   10.52 +    CxBuffer* buf = &asc_context.error_buffer;
   10.53 +    cxBufferWrite(text.ptr, 1, text.length, buf);
   10.54 +    cxBufferPut(buf, '\n');
   10.55 +
   10.56 +    asc_set_flag(&asc_context.flags, ASC_FLAG_HAS_ERROR);
   10.57 +}
   10.58 +
   10.59 +bool asc_has_error(void) {
   10.60 +    return asc_test_flag(asc_context.flags, ASC_FLAG_HAS_ERROR);
   10.61 +}
   10.62 +
   10.63 +char const* asc_get_error(void) {
   10.64 +    // we zero-terminate the current buffer contents before providing them
   10.65 +    cxBufferPut(&asc_context.error_buffer, 0);
   10.66 +    --asc_context.error_buffer.pos;
   10.67 +    --asc_context.error_buffer.size;
   10.68 +    return asc_context.error_buffer.space;
   10.69 +}
   10.70 +
   10.71 +void asc_clear_error(void) {
   10.72 +    cxBufferClear(&asc_context.error_buffer);
   10.73 +    asc_clear_flag(&asc_context.flags, ASC_FLAG_HAS_ERROR);
   10.74 +}
    11.1 --- a/src/window.c	Wed Nov 01 20:09:49 2023 +0100
    11.2 +++ b/src/window.c	Wed Nov 01 21:00:33 2023 +0100
    11.3 @@ -26,6 +26,8 @@
    11.4   */
    11.5  
    11.6  #include "ascension/window.h"
    11.7 +#include "ascension/context.h"
    11.8 +#include "ascension/error.h"
    11.9  
   11.10  #include <cx/linked_list.h>
   11.11  #include <cx/printf.h>
   11.12 @@ -50,11 +52,10 @@
   11.13  
   11.14  
   11.15  static void asc_event_window_resized(Uint32 id, Sint32 width, Sint32 height) {
   11.16 -    CxIterator iter = cxListIterator(asc_context.windows);
   11.17 -    cx_foreach(AscWindow*, w, iter) {
   11.18 -        if (w->id == id) {
   11.19 -            w->dimensions.width = width;
   11.20 -            w->dimensions.height = height;
   11.21 +    for (unsigned int i = 0 ; i < ASC_MAX_WINDOWS ; i++) {
   11.22 +        if (asc_context.windows[i].id == id) {
   11.23 +            asc_context.windows[i].dimensions.width = width;
   11.24 +            asc_context.windows[i].dimensions.height = height;
   11.25              return;
   11.26          }
   11.27      }
   11.28 @@ -87,9 +88,10 @@
   11.29      }
   11.30  
   11.31      // sync the windows
   11.32 -    CxMutIterator windows = cxListMutIterator(asc_context.windows);
   11.33 -    cx_foreach(AscWindow*, w, windows) {
   11.34 -        asc_window_sync(w);
   11.35 +    for (unsigned int i = 0 ; i < ASC_MAX_WINDOWS ; i++) {
   11.36 +        if (asc_context.windows[i].id > 0) {
   11.37 +            asc_window_sync(&asc_context.windows[i]);
   11.38 +        }
   11.39      }
   11.40      return true;
   11.41  }
   11.42 @@ -105,7 +107,18 @@
   11.43      settings->title = "Ascended Window";
   11.44  }
   11.45  
   11.46 -void asc_window_initialize(AscWindow* window, AscWindowSettings const* settings) {
   11.47 +AscWindow *asc_window_initialize(unsigned int index, AscWindowSettings const *settings) {
   11.48 +    if (index >= ASC_MAX_WINDOWS) {
   11.49 +        asc_error("Maximum number of windows exceeded.");
   11.50 +        return NULL;
   11.51 +    }
   11.52 +    AscWindow *window = &asc_context.windows[index];
   11.53 +    if (window->id > 0) {
   11.54 +        asc_error("Cannot create window - slot already occupied.");
   11.55 +        asc_dprintf("Tried to create window with index %u", index);
   11.56 +        return NULL;
   11.57 +    }
   11.58 +
   11.59      Uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN;
   11.60      flags |= settings->fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : SDL_WINDOW_RESIZABLE;
   11.61  
   11.62 @@ -119,7 +132,7 @@
   11.63      );
   11.64      if (window->window == NULL) {
   11.65          asc_error(SDL_GetError());
   11.66 -        return;
   11.67 +        return NULL;
   11.68      }
   11.69  
   11.70      window->id = SDL_GetWindowID(window->window);
   11.71 @@ -147,8 +160,7 @@
   11.72              glEnable(GL_DEBUG_OUTPUT);
   11.73              glDebugMessageCallback(asc_gl_debug_callback, NULL);
   11.74              asc_dprintf("Window %u initialized", window->id);
   11.75 -            cxListAdd(asc_context.windows, window);
   11.76 -            return;
   11.77 +            return window;
   11.78          } else {
   11.79              asc_error(glewGetErrorString(err));
   11.80          }
   11.81 @@ -164,8 +176,11 @@
   11.82      window->id = 0;
   11.83  }
   11.84  
   11.85 -void asc_window_destroy_impl(AscWindow* window) {
   11.86 -    // destory the GL context and the window
   11.87 +void asc_window_destroy(AscWindow* window) {
   11.88 +    // safeguard
   11.89 +    if (window->id == 0) return;
   11.90 +
   11.91 +    // destroy the GL context and the window
   11.92      if (window->glctx != NULL) {
   11.93          SDL_GL_DeleteContext(window->glctx);
   11.94      }
   11.95 @@ -178,19 +193,6 @@
   11.96      memset(window, 0, sizeof(AscWindow));
   11.97  }
   11.98  
   11.99 -void asc_window_destroy(AscWindow* window) {
  11.100 -    // find the window in the context and remove it
  11.101 -    bool found = false;
  11.102 -    CxMutIterator iter = cxListMutIterator(asc_context.windows);
  11.103 -    cx_foreach(AscWindow*, w, iter) {
  11.104 -        if (w == window) {
  11.105 -            found = true;
  11.106 -            cxIteratorFlagRemoval(iter);
  11.107 -        }
  11.108 -    }
  11.109 -    if (!found) asc_window_destroy_impl(window);
  11.110 -}
  11.111 -
  11.112  void asc_window_sync(AscWindow const* window) {
  11.113      SDL_GL_MakeCurrent(window->window, window->glctx);
  11.114      SDL_GL_SwapWindow(window->window);
    12.1 --- a/test/Makefile	Wed Nov 01 20:09:49 2023 +0100
    12.2 +++ b/test/Makefile	Wed Nov 01 21:00:33 2023 +0100
    12.3 @@ -38,8 +38,9 @@
    12.4  
    12.5  FORCE:
    12.6  
    12.7 -$(BUILD_DIR)/sandbox.o: sandbox.c ../src/ascension/window.h \
    12.8 - ../src/ascension/core.h ../src/ascension/datatypes.h
    12.9 +$(BUILD_DIR)/sandbox.o: sandbox.c ../src/ascension/ascension.h \
   12.10 + ../src/ascension/context.h ../src/ascension/window.h \
   12.11 + ../src/ascension/datatypes.h ../src/ascension/error.h
   12.12  	@echo "Compiling $<"
   12.13  	$(CC) -o $@ $(CFLAGS) -c $<
   12.14  
    13.1 --- a/test/sandbox.c	Wed Nov 01 20:09:49 2023 +0100
    13.2 +++ b/test/sandbox.c	Wed Nov 01 21:00:33 2023 +0100
    13.3 @@ -25,7 +25,7 @@
    13.4   * POSSIBILITY OF SUCH DAMAGE.
    13.5   */
    13.6  
    13.7 -#include "ascension/window.h"
    13.8 +#include <ascension/ascension.h>
    13.9  
   13.10  static bool show_message_box_on_error(SDL_Window* window) {
   13.11      if (asc_has_error()) {
   13.12 @@ -48,12 +48,11 @@
   13.13      asc_window_settings_init_defaults(&settings);
   13.14      settings.title = "Sandbox Application";
   13.15  
   13.16 -    AscWindow window;
   13.17 -    asc_window_initialize(&window, &settings);
   13.18 +    AscWindow *window = asc_window_initialize(0, &settings);
   13.19  
   13.20      while (asc_loop_next()) {
   13.21          // quit application on any error
   13.22 -        if (show_message_box_on_error(window.window)) break;
   13.23 +        if (show_message_box_on_error(window->window)) break;
   13.24  
   13.25  
   13.26      }

mercurial