src/ascension/datatypes.h

Wed, 06 Mar 2024 23:08:03 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 06 Mar 2024 23:08:03 +0100
changeset 33
e7ddb52facd3
parent 32
86468a71dd73
child 37
8a8cc6725b48
permissions
-rw-r--r--

add behavior nodes + restructure test program

Also, the test program will now officially be a game of snake.

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * Copyright 2023 Mike Becker. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef ASCENSION_DATATYPES_H
#define ASCENSION_DATATYPES_H

#ifdef __cplusplus
#error You cannot ascend using C++
#endif

#include <stdbool.h>
#include <string.h>
#include <SDL2/SDL_pixels.h>

// --------------------------------------------------------------------------
//    Datatype Definitions
// --------------------------------------------------------------------------

typedef unsigned char asc_ubyte;
typedef signed char asc_sbyte;

typedef union asc_vec2i {
    int data[2];
    struct { int x, y; };
    struct { int width, height; };
} asc_vec2i;

typedef struct asc_col4i {
    asc_ubyte red, green, blue, alpha;
} asc_col4i;

typedef struct asc_col4f {
    float red, green, blue, alpha;
} asc_col4f;

typedef float asc_mat4f[16];

// --------------------------------------------------------------------------
//    General Utility Functions
// --------------------------------------------------------------------------

static inline int asc_clamp_i(int v, int min, int max) {
    if (v < min) return min;
    if (v > max) return max;
    return v;
}

/**
 * Converts a float color (0.0f to 1.0f) to an int color (0 to 255).
 *
 * This operation is quite expensive. When you need to use it often, it's
 * quite obvious that you should change the data type.
 *
 * @param c the color using floats
 * @return the same color using ints
 */
static inline asc_col4i asc_col_ftoi(asc_col4f c) {
    int red = (int)(255*c.red);
    int green = (int)(255*c.green);
    int blue = (int)(255*c.blue);
    int alpha = (int)(255*c.alpha);
    asc_col4i r;
    r.red = (asc_ubyte)asc_clamp_i(red, 0, 255);
    r.green = (asc_ubyte)asc_clamp_i(green, 0, 255);
    r.blue = (asc_ubyte)asc_clamp_i(blue, 0, 255);
    r.alpha = (asc_ubyte)asc_clamp_i(alpha, 0, 255);
    return r;
}

static inline SDL_Color asc_col_sdl(asc_col4i col) {
    return (SDL_Color) {.r = col.red, .g = col.green, .b = col.blue, .a = col.alpha};
}

// --------------------------------------------------------------------------
//   Matrix Functions
// --------------------------------------------------------------------------

/**
 * Computes a matrix index in column-major order.
 * @param col the column
 * @param row the row
 * @param rows the number of rows
 */
#define asc_mat_index(col, row, rows) ((row) + (col) * (rows))

/**
 * Computes a 4x4 matrix index in column-major order.
 * @param col the column
 * @param row the row
 */
#define asc_mat4_index(col, row) asc_mat_index(col, row, 4)

static inline void asc_mat4f_ortho(
        asc_mat4f mat,
        float left,
        float right,
        float bottom,
        float top
) {
    memset(mat, 0, sizeof(float) * 16);
    mat[asc_mat4_index(0,0)] = 2.f / (right - left);
    mat[asc_mat4_index(1,1)] = 2.f / (top - bottom);
    mat[asc_mat4_index(2,2)] = -1;
    mat[asc_mat4_index(3,0)] = -(right + left) / (right - left);
    mat[asc_mat4_index(3,1)] = -(top + bottom) / (top - bottom);
    mat[asc_mat4_index(3,3)] = 1;
}

#endif //ASCENSION_DATATYPES_H

mercurial