formatted license comment

This commit is contained in:
Lorenzo Torres 2025-11-04 00:00:52 +01:00
parent db5a728846
commit 0ffdb8c3ac
30 changed files with 16218 additions and 14914 deletions

3
.gitignore vendored
View file

@ -2,3 +2,6 @@
.clang-format
compile-commands.json
topaz
**/*.BAK
**/*.bak
**/*~

1
.indent.pro vendored
View file

@ -12,3 +12,4 @@
-npsl
-di1
-ldi1
-nfc1

View file

@ -1,11 +1,11 @@
// SPDX-License-Identifier: BSD-3-Clause
/* SPDX-License-Identifier:BSD-3-Clause */
#include "arena.h"
#include <stdlib.h>
struct arena_allocator *arena_init(usize size)
{
struct arena_allocator *allocator = (struct arena_allocator *) malloc(sizeof(struct arena_allocator));
struct arena_allocator *allocator = (struct arena_allocator *)malloc(sizeof(struct arena_allocator));
allocator->size = size;
allocator->base = (usize) malloc(size);
allocator->position = 0;
@ -27,15 +27,15 @@ void *arena_alloc(struct arena_allocator *allocator, usize size)
}
void *ptr = (void *)(allocator->base + allocator->position);
allocator->position += size;
return ptr;
}
void *arena_zalloc(struct arena_allocator *allocator, usize size)
{
void *ptr = arena_alloc(allocator, size);
for (usize i=0; i < size; i++) {
((u8 *)ptr)[i] = 0x0;
for (usize i = 0; i < size; i++) {
((u8 *) ptr)[i] = 0x0;
}
return ptr;

View file

@ -1,15 +1,13 @@
// SPDX-License-Identifier: BSD-3-Clause
/* SPDX-License-Identifier:BSD-3-Clause */
#ifndef ARENA_H
#define ARENA_H
#include "../types.h"
/*
* An arena is a fast allocator that just
* keeps everything in a contiguous chunk
* of memory and moves a "pointer" when
* allocating new memory. The allocated
* memory is then free'd all at once.
* An arena is a fast allocator that just keeps everything in a contiguous
* chunk of memory and moves a "pointer" when allocating new memory. The
* allocated memory is then free'd all at once.
*/
struct arena_allocator {
usize size;
@ -25,19 +23,16 @@ struct arena_allocator *arena_init(usize size);
void arena_deinit(struct arena_allocator *allocator);
/*
* Allocate a chunk of memory of size `size` on the
* arena.
* Allocate a chunk of memory of size `size` on the arena.
*/
void *arena_alloc(struct arena_allocator *allocator, usize size);
/*
* Same as `arena_alloc()` but also set all the allocated
* memory to zero.
* Same as `arena_alloc()` but also set all the allocated memory to zero.
*/
void *arena_zalloc(struct arena_allocator *allocator, usize size);
/*
* Free all the allocated memory at once.
* This just sets the allocator cursor to its
* starting position.
* Free all the allocated memory at once. This just sets the allocator cursor
* to its starting position.
*/
void arena_bump(struct arena_allocator *allocator);

View file

@ -1,4 +1,4 @@
// SPDX-License-Identifier: BSD-3-Clause
/* SPDX-License-Identifier:BSD-3-Clause */
#ifndef LOG_H
#define LOG_H

View file

@ -1,11 +1,12 @@
// SPDX-License-Identifier: BSD-3-Clause
/* SPDX-License-Identifier:BSD-3-Clause */
#include "vector.h"
#include "log.h"
struct vector *vector_init(usize size, usize element_size)
{
if (size == 0) size = 1;
struct vector *vector = (struct vector *) malloc(sizeof(struct vector));
if (size == 0)
size = 1;
struct vector *vector = (struct vector *)malloc(sizeof(struct vector));
vector->length = 0;
vector->size = size;
vector->data = malloc(size * element_size);
@ -14,12 +15,13 @@ struct vector *vector_init(usize size, usize element_size)
void *vector_shrink(struct vector *vector, usize element_size)
{
#ifdef DEBUG
if (vector->length == 0) log_error("Popping from an empty vector.\n");
#endif
#ifdef DEBUG
if (vector->length == 0)
log_error("Popping from an empty vector.\n");
#endif
vector->length -= 1;
if (vector->length <= vector->size/3) {
vector->size = vector->length + vector->length/2;
if (vector->length <= vector->size / 3) {
vector->size = vector->length + vector->length / 2;
vector->data = realloc(vector->data, vector->size * element_size + 1);
}
return vector->data;

View file

@ -1,41 +1,39 @@
// SPDX-License-Identifier: BSD-3-Clause
#ifndef VECTOR_H
/* SPDX-License-Identifier:BSD-3-Clause */
#ifndef VECTOR_H
#define VECTOR_H
#include <stdlib.h>
#include "../types.h"
/* The vector is a dynamically growing array
* that can be operated as a regular array
* by accessing its `data` member and as a
* stack using `vector_push()` and `vector_pop()`.
/*
* The vector is a dynamically growing array that can be operated as a regular
* array by accessing its `data` member and as a stack using `vector_push()`
* and `vector_pop()`.
*/
struct vector {
usize length, size;
void *data;
};
/*
* Create a new vector with starting capacity of `size`
* where each element has size `element_size`.
/*
* Create a new vector with starting capacity of `size` where each element has
* size `element_size`.
*/
struct vector *vector_init(usize size, usize element_size);
void vector_deinit(struct vector *vector);
/*
* This function reduces the lenth of the vector thus
* removing the last element. If the used memory (length)
* is less than 1/3 of the allocated memory (size) the
* memory is reallocated to fit 1.5x the new length.
* Consider using the `vector_pop()` macro instead.
* This function reduces the lenth of the vector thus removing the last
* element. If the used memory (length) is less than 1/3 of the allocated
* memory (size) the memory is reallocated to fit 1.5x the new length. Consider
* using the `vector_pop()` macro instead.
*/
void *vector_shrink(struct vector *vector, usize element_size);
/*
* Push `value` on the vector. If there isn't enough
* allocated memory, reallocate the internal array
* to be 1.5x the current size and copy all the elements
* to the new allocated memory.
* Push `value` on the vector. If there isn't enough allocated memory,
* reallocate the internal array to be 1.5x the current size and copy all the
* elements to the new allocated memory.
*/
#define vector_push(vec, T, value) do {\
if (vec->length + 1 >= vec->size) {\
@ -47,8 +45,8 @@ void *vector_shrink(struct vector *vector, usize element_size);
} while (0)
/*
* Return the last element of the vector and calls
* `vector_shrink()`. Check out that function description.
* Return the last element of the vector and calls `vector_shrink()`. Check out
* that function description.
*/
#define vector_pop(vector, T) (((T*)vector_shrink((vector), sizeof(T)))[vector->length])

3310
gl/gl.c

File diff suppressed because it is too large Load diff

3160
gl/gl.h

File diff suppressed because it is too large Load diff

View file

@ -2,96 +2,86 @@
#define __khrplatform_h_
/*
** Copyright (c) 2008-2018 The Khronos Group Inc.
**
** Permission is hereby granted, free of charge, to any person obtaining a
** copy of this software and/or associated documentation files (the
** "Materials"), to deal in the Materials without restriction, including
** without limitation the rights to use, copy, modify, merge, publish,
** distribute, sublicense, and/or sell copies of the Materials, and to
** permit persons to whom the Materials are furnished to do so, subject to
** the following conditions:
**
** The above copyright notice and this permission notice shall be included
** in all copies or substantial portions of the Materials.
**
** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
*/
/* Khronos platform-specific types and definitions.
* * Copyright (c) 2008-2018 The Khronos Group Inc. *
*
* The master copy of khrplatform.h is maintained in the Khronos EGL
* Registry repository at https://github.com/KhronosGroup/EGL-Registry
* The last semantic modification to khrplatform.h was at commit ID:
* 67a3e0864c2d75ea5287b9f3d2eb74a745936692
* * Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and/or associated documentation files (the *
* "Materials"), to deal in the Materials without restriction, including *
* without limitation the rights to use, copy, modify, merge, publish, *
* distribute, sublicense, and/or sell copies of the Materials, and to * permit
* persons to whom the Materials are furnished to do so, subject to * the
* following conditions: *
*
* * The above copyright notice and this permission notice shall be included *
* in all copies or substantial portions of the Materials. *
*
* * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, *
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN
* NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, * TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * MATERIALS OR THE
* USE OR OTHER DEALINGS IN THE MATERIALS.
*/
/*
* Khronos platform-specific types and definitions.
*
* The master copy of khrplatform.h is maintained in the Khronos EGL Registry
* repository at https://github.com/KhronosGroup/EGL-Registry The last semantic
* modification to khrplatform.h was at commit ID:
* 67a3e0864c2d75ea5287b9f3d2eb74a745936692
*
* Adopters may modify this file to suit their platform. Adopters are
* encouraged to submit platform specific modifications to the Khronos
* group so that they can be included in future versions of this file.
* Please submit changes by filing pull requests or issues on
* the EGL Registry repository linked above.
* encouraged to submit platform specific modifications to the Khronos group so
* that they can be included in future versions of this file. Please submit
* changes by filing pull requests or issues on the EGL Registry repository
* linked above.
*
*
* See the Implementer's Guidelines for information about where this file
* should be located on your system and for more details of its use:
* http://www.khronos.org/registry/implementers_guide.pdf
* http://www.khronos.org/registry/implementers_guide.pdf
*
* This file should be included as
* #include <KHR/khrplatform.h>
* by Khronos client API header files that use its types and defines.
* This file should be included as #include <KHR/khrplatform.h> by Khronos
* client API header files that use its types and defines.
*
* The types in khrplatform.h should only be used to define API-specific types.
*
* Types defined in khrplatform.h:
* khronos_int8_t signed 8 bit
* khronos_uint8_t unsigned 8 bit
* khronos_int16_t signed 16 bit
* khronos_uint16_t unsigned 16 bit
* khronos_int32_t signed 32 bit
* khronos_uint32_t unsigned 32 bit
* khronos_int64_t signed 64 bit
* khronos_uint64_t unsigned 64 bit
* khronos_intptr_t signed same number of bits as a pointer
* khronos_uintptr_t unsigned same number of bits as a pointer
* khronos_ssize_t signed size
* khronos_usize_t unsigned size
* khronos_float_t signed 32 bit floating point
* khronos_time_ns_t unsigned 64 bit time in nanoseconds
* khronos_utime_nanoseconds_t unsigned time interval or absolute time in
* nanoseconds
* khronos_stime_nanoseconds_t signed time interval in nanoseconds
* khronos_boolean_enum_t enumerated boolean type. This should
* only be used as a base type when a client API's boolean type is
* an enum. Client APIs which use an integer or other type for
* booleans cannot use this as the base type for their boolean.
* Types defined in khrplatform.h: khronos_int8_t signed 8 bit
* khronos_uint8_t unsigned 8 bit khronos_int16_t signed 16 bit
* khronos_uint16_t unsigned 16 bit khronos_int32_t signed 32 bit
* khronos_uint32_t unsigned 32 bit khronos_int64_t signed 64 bit
* khronos_uint64_t unsigned 64 bit khronos_intptr_t signed same
* number of bits as a pointer khronos_uintptr_t unsigned same number of bits
* as a pointer khronos_ssize_t signed size khronos_usize_t
* unsigned size khronos_float_t signed 32 bit floating point
* khronos_time_ns_t unsigned 64 bit time in nanoseconds
* khronos_utime_nanoseconds_t unsigned time interval or absolute time in
* nanoseconds khronos_stime_nanoseconds_t signed time interval in nanoseconds
* khronos_boolean_enum_t enumerated boolean type. This should only be
* used as a base type when a client API's boolean type is an enum. Client APIs
* which use an integer or other type for booleans cannot use this as the base
* type for their boolean.
*
* Tokens defined in khrplatform.h:
*
* KHRONOS_FALSE, KHRONOS_TRUE Enumerated boolean false/true values.
* KHRONOS_FALSE, KHRONOS_TRUE Enumerated boolean false/true values.
*
* KHRONOS_SUPPORT_INT64 is 1 if 64 bit integers are supported; otherwise 0.
* KHRONOS_SUPPORT_FLOAT is 1 if floats are supported; otherwise 0.
* KHRONOS_SUPPORT_INT64 is 1 if 64 bit integers are supported; otherwise 0.
* KHRONOS_SUPPORT_FLOAT is 1 if floats are supported; otherwise 0.
*
* Calling convention macros defined in this file:
* KHRONOS_APICALL
* KHRONOS_APIENTRY
* KHRONOS_APIATTRIBUTES
* Calling convention macros defined in this file: KHRONOS_APICALL
* KHRONOS_APIENTRY KHRONOS_APIATTRIBUTES
*
* These may be used in function prototypes as:
*
* KHRONOS_APICALL void KHRONOS_APIENTRY funcname(
* int arg1,
* int arg2) KHRONOS_APIATTRIBUTES;
* KHRONOS_APICALL void KHRONOS_APIENTRY funcname( int arg1, int arg2)
* KHRONOS_APIATTRIBUTES;
*/
#if defined(__SCITECH_SNAP__) && !defined(KHRONOS_STATIC)
# define KHRONOS_STATIC 1
#define KHRONOS_STATIC 1
#endif
/*-------------------------------------------------------------------------
@ -100,17 +90,19 @@
* This precedes the return type of the function in the function prototype.
*/
#if defined(KHRONOS_STATIC)
/* If the preprocessor constant KHRONOS_STATIC is defined, make the
* header compatible with static linking. */
# define KHRONOS_APICALL
/*
* If the preprocessor constant KHRONOS_STATIC is defined, make the header
* compatible with static linking.
*/
#define KHRONOS_APICALL
#elif defined(_WIN32)
# define KHRONOS_APICALL __declspec(dllimport)
#define KHRONOS_APICALL __declspec(dllimport)
#elif defined (__SYMBIAN32__)
# define KHRONOS_APICALL IMPORT_C
#define KHRONOS_APICALL IMPORT_C
#elif defined(__ANDROID__)
# define KHRONOS_APICALL __attribute__((visibility("default")))
#define KHRONOS_APICALL __attribute__((visibility("default")))
#else
# define KHRONOS_APICALL
#define KHRONOS_APICALL
#endif
/*-------------------------------------------------------------------------
@ -120,10 +112,10 @@
* name in the function prototype.
*/
#if defined(_WIN32) && !defined(_WIN32_WCE) && !defined(__SCITECH_SNAP__)
/* Win32 but not WinCE */
# define KHRONOS_APIENTRY __stdcall
/* Win32 but not WinCE */
#define KHRONOS_APIENTRY __stdcall
#else
# define KHRONOS_APIENTRY
#define KHRONOS_APIENTRY
#endif
/*-------------------------------------------------------------------------
@ -147,20 +139,20 @@
* Using <stdint.h>
*/
#include <stdint.h>
typedef int32_t khronos_int32_t;
typedef uint32_t khronos_uint32_t;
typedef int64_t khronos_int64_t;
typedef uint64_t khronos_uint64_t;
typedef int32_t khronos_int32_t;
typedef uint32_t khronos_uint32_t;
typedef int64_t khronos_int64_t;
typedef uint64_t khronos_uint64_t;
#define KHRONOS_SUPPORT_INT64 1
#define KHRONOS_SUPPORT_FLOAT 1
/*
* To support platform where unsigned long cannot be used interchangeably with
* inptr_t (e.g. CHERI-extended ISAs), we can use the stdint.h intptr_t.
* Ideally, we could just use (u)intptr_t everywhere, but this could result in
* ABI breakage if khronos_uintptr_t is changed from unsigned long to
* unsigned long long or similar (this results in different C++ name mangling).
* To avoid changes for existing platforms, we restrict usage of intptr_t to
* platforms where the size of a pointer is larger than the size of long.
* ABI breakage if khronos_uintptr_t is changed from unsigned long to unsigned
* long long or similar (this results in different C++ name mangling). To avoid
* changes for existing platforms, we restrict usage of intptr_t to platforms
* where the size of a pointer is larger than the size of long.
*/
#if defined(__SIZEOF_LONG__) && defined(__SIZEOF_POINTER__)
#if __SIZEOF_POINTER__ > __SIZEOF_LONG__
@ -174,10 +166,10 @@ typedef uint64_t khronos_uint64_t;
* Using <inttypes.h>
*/
#include <inttypes.h>
typedef int32_t khronos_int32_t;
typedef uint32_t khronos_uint32_t;
typedef int64_t khronos_int64_t;
typedef uint64_t khronos_uint64_t;
typedef int32_t khronos_int32_t;
typedef uint32_t khronos_uint32_t;
typedef int64_t khronos_int64_t;
typedef uint64_t khronos_uint64_t;
#define KHRONOS_SUPPORT_INT64 1
#define KHRONOS_SUPPORT_FLOAT 1
@ -186,10 +178,10 @@ typedef uint64_t khronos_uint64_t;
/*
* Win32
*/
typedef __int32 khronos_int32_t;
typedef unsigned __int32 khronos_uint32_t;
typedef __int64 khronos_int64_t;
typedef unsigned __int64 khronos_uint64_t;
typedef __int32 khronos_int32_t;
typedef unsigned __int32 khronos_uint32_t;
typedef __int64 khronos_int64_t;
typedef unsigned __int64 khronos_uint64_t;
#define KHRONOS_SUPPORT_INT64 1
#define KHRONOS_SUPPORT_FLOAT 1
@ -198,15 +190,15 @@ typedef unsigned __int64 khronos_uint64_t;
/*
* Sun or Digital
*/
typedef int khronos_int32_t;
typedef unsigned int khronos_uint32_t;
typedef int khronos_int32_t;
typedef unsigned int khronos_uint32_t;
#if defined(__arch64__) || defined(_LP64)
typedef long int khronos_int64_t;
typedef unsigned long int khronos_uint64_t;
typedef long int khronos_int64_t;
typedef unsigned long int khronos_uint64_t;
#else
typedef long long int khronos_int64_t;
typedef unsigned long long int khronos_uint64_t;
#endif /* __arch64__ */
typedef long long int khronos_int64_t;
typedef unsigned long long int khronos_uint64_t;
#endif /* __arch64__ */
#define KHRONOS_SUPPORT_INT64 1
#define KHRONOS_SUPPORT_FLOAT 1
@ -215,8 +207,8 @@ typedef unsigned long long int khronos_uint64_t;
/*
* Hypothetical platform with no float or int64 support
*/
typedef int khronos_int32_t;
typedef unsigned int khronos_uint32_t;
typedef int khronos_int32_t;
typedef unsigned int khronos_uint32_t;
#define KHRONOS_SUPPORT_INT64 0
#define KHRONOS_SUPPORT_FLOAT 0
@ -226,10 +218,10 @@ typedef unsigned int khronos_uint32_t;
* Generic fallback
*/
#include <stdint.h>
typedef int32_t khronos_int32_t;
typedef uint32_t khronos_uint32_t;
typedef int64_t khronos_int64_t;
typedef uint64_t khronos_uint64_t;
typedef int32_t khronos_int32_t;
typedef uint32_t khronos_uint32_t;
typedef int64_t khronos_int64_t;
typedef uint64_t khronos_uint64_t;
#define KHRONOS_SUPPORT_INT64 1
#define KHRONOS_SUPPORT_FLOAT 1
@ -239,54 +231,55 @@ typedef uint64_t khronos_uint64_t;
/*
* Types that are (so far) the same on all platforms
*/
typedef signed char khronos_int8_t;
typedef unsigned char khronos_uint8_t;
typedef signed short int khronos_int16_t;
typedef unsigned short int khronos_uint16_t;
typedef signed char khronos_int8_t;
typedef unsigned char khronos_uint8_t;
typedef signed short int khronos_int16_t;
typedef unsigned short int khronos_uint16_t;
/*
* Types that differ between LLP64 and LP64 architectures - in LLP64,
* pointers are 64 bits, but 'long' is still 32 bits. Win64 appears
* to be the only LLP64 architecture in current use.
* Types that differ between LLP64 and LP64 architectures - in LLP64, pointers
* are 64 bits, but 'long' is still 32 bits. Win64 appears to be the only LLP64
* architecture in current use.
*/
#ifdef KHRONOS_USE_INTPTR_T
typedef intptr_t khronos_intptr_t;
typedef uintptr_t khronos_uintptr_t;
typedef intptr_t khronos_intptr_t;
typedef uintptr_t khronos_uintptr_t;
#elif defined(_WIN64)
typedef signed long long int khronos_intptr_t;
typedef signed long long int khronos_intptr_t;
typedef unsigned long long int khronos_uintptr_t;
#else
typedef signed long int khronos_intptr_t;
typedef unsigned long int khronos_uintptr_t;
typedef signed long int khronos_intptr_t;
typedef unsigned long int khronos_uintptr_t;
#endif
#if defined(_WIN64)
typedef signed long long int khronos_ssize_t;
typedef signed long long int khronos_ssize_t;
typedef unsigned long long int khronos_usize_t;
#else
typedef signed long int khronos_ssize_t;
typedef unsigned long int khronos_usize_t;
typedef signed long int khronos_ssize_t;
typedef unsigned long int khronos_usize_t;
#endif
#if KHRONOS_SUPPORT_FLOAT
/*
* Float type
*/
typedef float khronos_float_t;
typedef float khronos_float_t;
#endif
#if KHRONOS_SUPPORT_INT64
/* Time types
/*
* Time types
*
* These types can be used to represent a time interval in nanoseconds or
* an absolute Unadjusted System Time. Unadjusted System Time is the number
* of nanoseconds since some arbitrary system event (e.g. since the last
* time the system booted). The Unadjusted System Time is an unsigned
* 64 bit value that wraps back to 0 every 584 years. Time intervals
* may be either signed or unsigned.
* These types can be used to represent a time interval in nanoseconds or an
* absolute Unadjusted System Time. Unadjusted System Time is the number of
* nanoseconds since some arbitrary system event (e.g. since the last time the
* system booted). The Unadjusted System Time is an unsigned 64 bit value that
* wraps back to 0 every 584 years. Time intervals may be either signed or
* unsigned.
*/
typedef khronos_uint64_t khronos_utime_nanoseconds_t;
typedef khronos_int64_t khronos_stime_nanoseconds_t;
typedef khronos_uint64_t khronos_utime_nanoseconds_t;
typedef khronos_int64_t khronos_stime_nanoseconds_t;
#endif
/*
@ -303,9 +296,9 @@ typedef khronos_int64_t khronos_stime_nanoseconds_t;
* comparisons should not be made against KHRONOS_TRUE.
*/
typedef enum {
KHRONOS_FALSE = 0,
KHRONOS_TRUE = 1,
KHRONOS_BOOLEAN_ENUM_FORCE_SIZE = KHRONOS_MAX_ENUM
KHRONOS_FALSE = 0,
KHRONOS_TRUE = 1,
KHRONOS_BOOLEAN_ENUM_FORCE_SIZE = KHRONOS_MAX_ENUM
} khronos_boolean_enum_t;
#endif /* __khrplatform_h_ */
#endif /* __khrplatform_h_ */

View file

@ -1,4 +1,4 @@
// SPDX-License-Identifier: BSD-3-Clause
/* SPDX-License-Identifier:BSD-3-Clause */
#include "gl.h"
#define RGFW_IMPLEMENTATION
@ -6,21 +6,20 @@
#include "../rgfw.h"
/*
* This function is the entrypoint for the whole
* game. Its role is to initialize OpenGL, create
* the renderer and start the game loop.
* This function is the entrypoint for the whole game. Its role is to
* initialize OpenGL, create the renderer and start the game loop.
*/
int platform_run(i32 argc, u8 **argv)
int platform_run(i32 argc, u8 * *argv)
{
(void) argc;
(void) argv;
(void)argc;
(void)argv;
RGFW_glHints* hints = RGFW_getGlobalHints_OpenGL();
RGFW_glHints *hints = RGFW_getGlobalHints_OpenGL();
hints->major = 3;
hints->minor = 3;
RGFW_setGlobalHints_OpenGL(hints);
RGFW_window* win = RGFW_createWindow("Topaz", 0, 0, 800, 600, RGFW_windowCenter | RGFW_windowNoResize | RGFW_windowHide);
RGFW_window *win = RGFW_createWindow("Topaz", 0, 0, 800, 600, RGFW_windowCenter | RGFW_windowNoResize | RGFW_windowHide);
RGFW_window_createContext_OpenGL(win, hints);
int glad_version = gladLoadGL(RGFW_getProcAddress_OpenGL);

250
linear.c
View file

@ -1,179 +1,205 @@
// SPDX-License-Identifier: BSD-3-Clause
/* SPDX-License-Identifier:BSD-3-Clause */
#include "linear.h"
float vec2_dot(vec2 a, vec2 b)
{
float result = 0.0f;
for (int i=0; i<2; i++) {
result += a[i] * b[i];
}
float result = 0.0f;
for (int i = 0; i < 2; i++) {
result += a[i] * b[i];
}
return result;
return result;
}
float vec3_dot(vec3 a, vec3 b)
float
vec3_dot(vec3 a, vec3 b)
{
float result = 0.0f;
for (int i=0; i<3; i++) {
result += a[i] * b[i];
}
float result = 0.0f;
for (int i = 0; i < 3; i++) {
result += a[i] * b[i];
}
return result;
return result;
}
float vec4_dot(vec4 a, vec4 b)
float
vec4_dot(vec4 a, vec4 b)
{
float result = 0.0f;
for (int i=0; i<4; i++) {
result += a[i] * b[i];
}
float result = 0.0f;
for (int i = 0; i < 4; i++) {
result += a[i] * b[i];
}
return result;
return result;
}
void vec3_cross(vec3 dest, vec3 a, vec3 b)
void
vec3_cross(vec3 dest, vec3 a, vec3 b)
{
vec3 res = {0};
res[0] = a[1] * b[2] - a[2] * b[1];
res[1] = a[2] * b[0] - a[0] * b[2];
res[2] = a[0] * b[1] - a[1] * b[0];
memcpy(dest, res, sizeof(vec3));
vec3 res = {0};
res[0] = a[1] * b[2] - a[2] * b[1];
res[1] = a[2] * b[0] - a[0] * b[2];
res[2] = a[0] * b[1] - a[1] * b[0];
memcpy(dest, res, sizeof(vec3));
}
void mat4_perspective(mat4 dest, float fov, float aspect, float near, float far)
void
mat4_perspective(mat4 dest, float fov, float aspect, float near, float far)
{
mat4 perspective = {
{ 1.0f/(aspect*tan(fov/2.0f)), 0.0f, 0.0f, 0.0f },
{ 0.0f, 1.0f/tan(fov/2.0f), 0.0f, 0.0f },
{ 0.0f, 0.0f, -((far+near)/(far-near)), -((2*far*near)/(far-near)) },
{ 0.0f, 0.0f, -1.0f, 0.0f }
};
mat4 perspective = {
{1.0f / (aspect * tan(fov / 2.0f)), 0.0f, 0.0f, 0.0f},
{0.0f, 1.0f / tan(fov / 2.0f), 0.0f, 0.0f},
{0.0f, 0.0f, -((far + near) / (far - near)), -((2 * far * near) / (far - near))},
{0.0f, 0.0f, -1.0f, 0.0f}
};
memcpy(dest, perspective, sizeof(mat4));
memcpy(dest, perspective, sizeof(mat4));
}
void mat4_lookat(mat4 dest, vec3 eye, vec3 target, vec3 up)
void
mat4_lookat(mat4 dest, vec3 eye, vec3 target, vec3 up)
{
vec3 target_cpy = {0};
memcpy(target_cpy, target, sizeof(vec3));
vec3_sub(target_cpy, target_cpy, eye);
vec3 target_cpy = {0};
memcpy(target_cpy, target, sizeof(vec3));
vec3_sub(target_cpy, target_cpy, eye);
vec3 zaxis = {0};
vec3_normalize(zaxis, target_cpy);
vec3 zaxis = {0};
vec3_normalize(zaxis, target_cpy);
vec3 zaxis_cpy = {0};
memcpy(zaxis_cpy, zaxis, sizeof(vec3));
vec3_cross(zaxis_cpy, zaxis, up);
vec3 zaxis_cpy = {0};
memcpy(zaxis_cpy, zaxis, sizeof(vec3));
vec3_cross(zaxis_cpy, zaxis, up);
vec3 xaxis = {0};
vec3_normalize(xaxis, zaxis_cpy);
vec3 xaxis = {0};
vec3_normalize(xaxis, zaxis_cpy);
vec3 yaxis = {0};
vec3_cross(yaxis, xaxis, zaxis);
vec3 yaxis = {0};
vec3_cross(yaxis, xaxis, zaxis);
zaxis[0] = -zaxis[0];
zaxis[1] = -zaxis[1];
zaxis[2] = -zaxis[2];
zaxis[0] = -zaxis[0];
zaxis[1] = -zaxis[1];
zaxis[2] = -zaxis[2];
mat4 view = {
{ xaxis[0], xaxis[1], xaxis[2], -vec3_dot(xaxis, eye) },
{ yaxis[0], yaxis[1], yaxis[2], -vec3_dot(yaxis, eye) },
{ zaxis[0], zaxis[1], zaxis[2], -vec3_dot(zaxis, eye) },
{ 0.0f, 0.0f, 0.0f, 1.0f }
};
mat4 view = {
{xaxis[0], xaxis[1], xaxis[2], -vec3_dot(xaxis, eye)},
{yaxis[0], yaxis[1], yaxis[2], -vec3_dot(yaxis, eye)},
{zaxis[0], zaxis[1], zaxis[2], -vec3_dot(zaxis, eye)},
{0.0f, 0.0f, 0.0f, 1.0f}
};
memcpy(dest, view, sizeof(mat4));
memcpy(dest, view, sizeof(mat4));
}
void vec2_sub(vec2 dest, vec2 a, vec2 b)
void
vec2_sub(vec2 dest, vec2 a, vec2 b)
{
vec2 res = {0};
for (int i=0; i<2; i++) res[i] = a[i] - b[i];
memcpy(dest, res, sizeof(vec2));
vec2 res = {0};
for (int i = 0; i < 2; i++)
res[i] = a[i] - b[i];
memcpy(dest, res, sizeof(vec2));
}
void vec3_sub(vec3 dest, vec3 a, vec3 b)
void
vec3_sub(vec3 dest, vec3 a, vec3 b)
{
vec3 res = {0};
for (int i=0; i<3; i++) res[i] = a[i] - b[i];
memcpy(dest, res, sizeof(vec3));
vec3 res = {0};
for (int i = 0; i < 3; i++)
res[i] = a[i] - b[i];
memcpy(dest, res, sizeof(vec3));
}
void vec4_sub(vec4 dest, vec4 a, vec4 b)
void
vec4_sub(vec4 dest, vec4 a, vec4 b)
{
vec4 res = {0};
for (int i=0; i<4; i++) res[i] = a[i] - b[i];
memcpy(dest, res, sizeof(vec4));
vec4 res = {0};
for (int i = 0; i < 4; i++)
res[i] = a[i] - b[i];
memcpy(dest, res, sizeof(vec4));
}
void vec2_add(vec2 dest, vec2 a, vec2 b)
void
vec2_add(vec2 dest, vec2 a, vec2 b)
{
vec2 res = {0};
for (int i=0; i<2; i++) res[i] = a[i] + b[i];
memcpy(dest, res, sizeof(vec2));
vec2 res = {0};
for (int i = 0; i < 2; i++)
res[i] = a[i] + b[i];
memcpy(dest, res, sizeof(vec2));
}
void vec3_add(vec3 dest, vec3 a, vec3 b)
void
vec3_add(vec3 dest, vec3 a, vec3 b)
{
vec3 res = {0};
for (int i=0; i<3; i++) res[i] = a[i] + b[i];
memcpy(dest, res, sizeof(vec3));
vec3 res = {0};
for (int i = 0; i < 3; i++)
res[i] = a[i] + b[i];
memcpy(dest, res, sizeof(vec3));
}
void vec4_add(vec4 dest, vec4 a, vec4 b)
void
vec4_add(vec4 dest, vec4 a, vec4 b)
{
vec4 res = {0};
for (int i=0; i<4; i++) res[i] = a[i] + b[i];
memcpy(dest, res, sizeof(vec4));
vec4 res = {0};
for (int i = 0; i < 4; i++)
res[i] = a[i] + b[i];
memcpy(dest, res, sizeof(vec4));
}
void vec2_normalize(vec2 dest, vec2 a)
void
vec2_normalize(vec2 dest, vec2 a)
{
vec2 res = {0};
memcpy(res, a, sizeof(vec2));
float w = sqrt(a[0] * a[0] + a[1] * a[1]);
res[0] /= w;
res[1] /= w;
memcpy(dest, res, sizeof(vec2));
vec2 res = {0};
memcpy(res, a, sizeof(vec2));
float w = sqrt(a[0] * a[0] + a[1] * a[1]);
res[0] /= w;
res[1] /= w;
memcpy(dest, res, sizeof(vec2));
}
void vec3_normalize(vec3 dest, vec3 a)
void
vec3_normalize(vec3 dest, vec3 a)
{
vec3 res = {0};
memcpy(res, a, sizeof(vec3));
float w = sqrt(a[0] * a[0] + a[1] * a[1] + a[2] * a[2]);
res[0] /= w;
res[1] /= w;
res[2] /= w;
memcpy(dest, res, sizeof(vec3));
vec3 res = {0};
memcpy(res, a, sizeof(vec3));
float w = sqrt(a[0] * a[0] + a[1] * a[1] + a[2] * a[2]);
res[0] /= w;
res[1] /= w;
res[2] /= w;
memcpy(dest, res, sizeof(vec3));
}
void vec4_normalize(vec4 dest, vec4 a)
void
vec4_normalize(vec4 dest, vec4 a)
{
vec4 res = {0};
memcpy(res, a, sizeof(vec4));
float w = sqrt(a[0] * a[0] + a[1] * a[1] + a[2] * a[2]);
res[0] /= w;
res[1] /= w;
res[2] /= w;
res[3] /= w;
memcpy(dest, res, sizeof(vec4));
vec4 res = {0};
memcpy(res, a, sizeof(vec4));
float w = sqrt(a[0] * a[0] + a[1] * a[1] + a[2] * a[2]);
res[0] /= w;
res[1] /= w;
res[2] /= w;
res[3] /= w;
memcpy(dest, res, sizeof(vec4));
}
void vec2_scale(vec2 dest, vec2 a, float scale)
void
vec2_scale(vec2 dest, vec2 a, float scale)
{
memcpy(dest, a, sizeof(vec2));
for (int i=0; i<2; i++) dest[i] *= scale;
memcpy(dest, a, sizeof(vec2));
for (int i = 0; i < 2; i++)
dest[i] *= scale;
}
void vec3_scale(vec3 dest, vec3 a, float scale)
void
vec3_scale(vec3 dest, vec3 a, float scale)
{
memcpy(dest, a, sizeof(vec3));
for (int i=0; i<3; i++) dest[i] *= scale;
memcpy(dest, a, sizeof(vec3));
for (int i = 0; i < 3; i++)
dest[i] *= scale;
}
void vec4_scale(vec4 dest, vec4 a, float scale)
void
vec4_scale(vec4 dest, vec4 a, float scale)
{
memcpy(dest, a, sizeof(vec4));
for (int i=0; i<4; i++) dest[i] *= scale;
memcpy(dest, a, sizeof(vec4));
for (int i = 0; i < 4; i++)
dest[i] *= scale;
}

View file

@ -1,4 +1,4 @@
// SPDX-License-Identifier: BSD-3-Clause
/* SPDX-License-Identifier:BSD-3-Clause */
#ifndef LINEAR_H
#define LINEAR_H

View file

@ -1,4 +1,4 @@
// SPDX-License-Identifier: BSD-3-Clause
/* SPDX-License-Identifier:BSD-3-Clause */
#ifndef PLATFORM_H
#define PLATFORM_H

View file

@ -1,13 +1,12 @@
// SPDX-License-Identifier: BSD-3-Clause
/* SPDX-License-Identifier:BSD-3-Clause */
#ifndef RENDERER_H
#define RENDERER_H
#include "../types.h"
/*
* A mesh is a drawable object represented
* as an index (offset) in the global vertex
* and index buffers and a size.
* A mesh is a drawable object represented as an index (offset) in the global
* vertex and index buffers and a size.
*/
struct mesh {
usize vertex_offset;
@ -15,10 +14,9 @@ struct mesh {
usize size;
};
/* The renderer context stores objects
* related to rendering. Implementation
* depends on the graphics backend used
* so for reference see gl/gl.h or vk/vk.h
/*
* The renderer context stores objects related to rendering. Implementation
* depends on the graphics backend used so for reference see gl/gl.h or vk/vk.h
*/
struct renderer_context;

23908
rgfw.h

File diff suppressed because it is too large Load diff

View file

@ -1,4 +1,4 @@
// SPDX-License-Identifier: BSD-3-Clause
/* SPDX-License-Identifier:BSD-3-Clause */
#include <stdio.h>
#include "platform.h"

View file

@ -1,4 +1,4 @@
// SPDX-License-Identifier: BSD-3-Clause
/* SPDX-License-Identifier:BSD-3-Clause */
#ifndef TYPES_H
#define TYPES_H

View file

@ -1,7 +0,0 @@
// SPDX-License-Identifier: BSD-3-Clause
#ifndef UTILS_H
#define UTILS_H
#endif

View file

@ -1,4 +1,4 @@
// SPDX-License-Identifier: BSD-3-Clause
/* SPDX-License-Identifier:BSD-3-Clause */
#include "device.h"
#include "physical_device.h"
#include "../core/vector.h"
@ -13,10 +13,10 @@ void vk_device_init(struct renderer_context *context)
for (usize i = 0; i < physical_device_extensions->length; i++) {
if (strcmp(((char **)physical_device_extensions->data)[i], "VK_KHR_portability_subset") == 0) {
/*
* The spec states that if VK_KHR_portability_subset
* is present in the physical device extensions,
* the device should also have that extension enabled.
*/
* The spec states that if VK_KHR_portability_subset is
* present in the physical device extensions, the
* device should also have that extension enabled.
*/
vector_push(device_extensions, char *, "VK_KHR_portability_subset");
}

View file

@ -1,4 +1,4 @@
// SPDX-License-Identifier: BSD-3-Clause
/* SPDX-License-Identifier:BSD-3-Clause */
#ifndef DEVICE_H
#define DEVICE_H

View file

@ -1,4 +1,4 @@
// SPDX - License - Identifier:BSD - 3 - Clause
/* SPDX-License-Identifier:BSD-3-Clause */
#include "instance.h"
#include "../core/log.h"
#define RGFW_VULKAN

View file

@ -1,4 +1,4 @@
// SPDX-License-Identifier: BSD-3-Clause
/* SPDX-License-Identifier:BSD-3-Clause */
#ifndef INSTANCE_H
#define INSTANCE_H

View file

@ -1,4 +1,4 @@
// SPDX-License-Identifier: BSD-3-Clause
/* SPDX-License-Identifier:BSD-3-Clause */
#include "physical_device.h"
#include "../core/log.h"
#include <stdio.h>
@ -34,7 +34,7 @@ struct vector *vk_physical_device_get_extensions(struct renderer_context *contex
VkExtensionProperties *properties = NULL;
vkEnumerateDeviceExtensionProperties(context->physical_device, NULL, &property_count, NULL);
struct vector *extensions = vector_init(property_count, sizeof(char *));
properties = (VkExtensionProperties *) malloc(sizeof(VkExtensionProperties) * property_count);
@ -50,7 +50,7 @@ struct vector *vk_physical_device_get_extensions(struct renderer_context *contex
}
free(properties);
return extensions;
}

View file

@ -1,4 +1,4 @@
// SPDX-License-Identifier: BSD-3-Clause
/* SPDX-License-Identifier:BSD-3-Clause */
#ifndef PHYSICAL_DEVICE_H
#define PHYSICAL_DEVICE_H
@ -8,21 +8,18 @@
/*
* Get the list of all available devices and
* pick the best option.
* Get the list of all available devices and pick the best option.
*/
void vk_physical_device_pick(struct renderer_context *context);
/*
* Get the list of all available device
* extensions and return a vector containing
* those.
* Get the list of all available device extensions and return a vector
* containing those.
*/
struct vector *vk_physical_device_get_extensions(struct renderer_context *context);
/*
* The physical device is responsible of selecting
* the queue family indices, used later by the
* device to create the queues. This function
* sets the family indices in the renderer context.
* The physical device is responsible of selecting the queue family indices,
* used later by the device to create the queues. This function sets the family
* indices in the renderer context.
*/
void vk_physical_device_select_family_indices(struct renderer_context *context);

View file

@ -1,4 +1,4 @@
// SPDX-License-Identifier: BSD-3-Clause
/* SPDX-License-Identifier:BSD-3-Clause */
#define RGFW_VULKAN
#define RGFW_IMPLEMENTATION
@ -14,18 +14,17 @@
#include "../rendering/renderer.h"
/*
* This function is the entrypoint for the whole
* game. Its role is to initialize Vulkan, create
* the renderer and start the game loop.
* This function is the entrypoint for the whole game. Its role is to
* initialize Vulkan, create the renderer and start the game loop.
*/
int platform_run(i32 argc, u8 **argv)
int platform_run(i32 argc, u8 * *argv)
{
(void) argc;
(void) argv;
(void)argc;
(void)argv;
log_info("Using Vulkan as rendering backend.\n");
RGFW_window* win = RGFW_createWindow("topaz", 0, 0, 800, 600, RGFW_windowCenter | RGFW_windowNoResize | RGFW_windowHide);
RGFW_window *win = RGFW_createWindow("topaz", 0, 0, 800, 600, RGFW_windowCenter | RGFW_windowNoResize | RGFW_windowHide);
RGFW_window_show(win);
RGFW_window_setExitKey(win, RGFW_escape);

View file

@ -1,4 +1,4 @@
// SPDX-License-Identifier: BSD-3-Clause
/* SPDX-License-Identifier:BSD-3-Clause */
#include "../rendering/renderer.h"
#include "instance.h"
#include "physical_device.h"
@ -10,7 +10,7 @@
struct renderer_context *renderer_context_init(void)
{
struct renderer_context *context = (struct renderer_context *) arena_alloc(global_arena, (sizeof(struct renderer_context)));
struct renderer_context *context = (struct renderer_context *)arena_alloc(global_arena, (sizeof(struct renderer_context)));
vk_instance_init(context);
vk_physical_device_pick(context);
@ -32,10 +32,10 @@ struct mesh *renderer_build_chunk_mesh(void)
void renderer_draw_mesh(struct mesh mesh)
{
(void) mesh;
(void)mesh;
}
void renderer_draw_chunk(struct mesh mesh)
{
(void) mesh;
(void)mesh;
}

View file

@ -1,4 +1,4 @@
// SPDX-License-Identifier: BSD-3-Clause
/* SPDX-License-Identifier:BSD-3-Clause */
#ifndef VK_H
#define VK_H

View file

@ -1,14 +0,0 @@
// SPDX-License-Identifier: BSD-3-Clause
#ifdef BLOCK_H
#define BLOCK_H
#include "../types.h"
typedef u16 block_id;
#define BLOCK_AIR_ID 0
#define BLOCK_STONE_ID 1
#define BLOCK_DIRT_ID 1
#define BLOCK_GRASS_ID 1
#endif

View file

@ -1,22 +0,0 @@
// SPDX-License-Identifier: BSD-3-Clause
#ifndef CHUNK_H
#define CHUNK_H
#include "block.h"
#define CHUNK_SIZE 16
#define CHUNK_INDEX(x, y, z) (CHUNK_SIZE * CHUNK_SIZE * (x) + CHUNK_SIZE * (y) + (z))
typedef usize chunk_position[2];
/*
* Chunk are a group of blocks with
* size 16x16x16. The world is composed
* of infinite chunks in each axis.
*/
struct chunk {
block_id blocks[CHUNK_SIZE * CHUNK_SIZE * CHUNK_SIZE];
chunk_position position;
};
#endif