More organizations and include path cleanups.

This commit is contained in:
Relintai 2023-12-18 22:20:26 +01:00
parent 1f2c5ac185
commit c419d7e12d
61 changed files with 911 additions and 131 deletions

14
compile_linux.sh Executable file
View File

@ -0,0 +1,14 @@
#clang++ main.cpp -Wall -o3 -o ./bin/game -Ilibs/SDL2-linux/include -Llibs/SDL2-linux/lib -lSDL2 -lSDL2main
#g++ main.cpp -Wall -o3 -o ./bin/game -Ilibs/SDL2-linux/include -Llibs/SDL2-linux/lib -lSDL2 -lSDL2main
#g++ main.cpp shader.cpp material.cpp mesh.cpp mesh_instance.cpp mesh_utils.cpp texture.cpp camera.cpp application.cpp scene.cpp game_scene.cpp object_2d.cpp tile_map.cpp sprite.cpp \
# -g -Wall -o3 -o ./bin/game $(pkg-config --cflags --libs sdl2 glew)
#g++ -Wall -fexceptions -I/usr/include/SDL2 -D_REENTRANT -g -Isfw -c sfw/aabb.cpp -o sfw/aabb.o
g++ -Wall -D_REENTRANT -g -Isfw -c sfw/aabb.cpp -o sfw/aabb.o

View File

@ -32,11 +32,84 @@
<Add option="-Wall" />
<Add option="-fexceptions" />
<Add option="`pkg-config --cflags sdl2 glew`" />
<Add directory="libs/imgui/" />
<Add directory="sfw" />
</Compiler>
<Linker>
<Add option="`pkg-config --cflags --libs sdl2 glew`" />
</Linker>
<Unit filename="sfw/aabb.cpp" />
<Unit filename="sfw/aabb.h" />
<Unit filename="sfw/basis.cpp" />
<Unit filename="sfw/basis.h" />
<Unit filename="sfw/char_range.inc" />
<Unit filename="sfw/char_utils.h" />
<Unit filename="sfw/color.cpp" />
<Unit filename="sfw/color.h" />
<Unit filename="sfw/cowdata.h" />
<Unit filename="sfw/error_list.h" />
<Unit filename="sfw/error_macros.h" />
<Unit filename="sfw/face3.cpp" />
<Unit filename="sfw/face3.h" />
<Unit filename="sfw/hash_map.h" />
<Unit filename="sfw/hash_set.h" />
<Unit filename="sfw/hashfuncs.h" />
<Unit filename="sfw/int_types.h" />
<Unit filename="sfw/list.h" />
<Unit filename="sfw/local_vector.h" />
<Unit filename="sfw/logger.cpp" />
<Unit filename="sfw/logger.h" />
<Unit filename="sfw/math_defs.h" />
<Unit filename="sfw/math_funcs.cpp" />
<Unit filename="sfw/math_funcs.h" />
<Unit filename="sfw/memory.h" />
<Unit filename="sfw/paged_allocator.h" />
<Unit filename="sfw/pair.h" />
<Unit filename="sfw/pcg.cpp" />
<Unit filename="sfw/pcg.h" />
<Unit filename="sfw/plane.cpp" />
<Unit filename="sfw/plane.h" />
<Unit filename="sfw/projection.cpp" />
<Unit filename="sfw/projection.h" />
<Unit filename="sfw/quaternion.cpp" />
<Unit filename="sfw/quaternion.h" />
<Unit filename="sfw/queue.h" />
<Unit filename="sfw/random_pcg.cpp" />
<Unit filename="sfw/random_pcg.h" />
<Unit filename="sfw/rb_map.h" />
<Unit filename="sfw/rb_set.h" />
<Unit filename="sfw/rect2.cpp" />
<Unit filename="sfw/rect2.h" />
<Unit filename="sfw/rect2i.cpp" />
<Unit filename="sfw/rect2i.h" />
<Unit filename="sfw/ring_buffer.h" />
<Unit filename="sfw/rvector.h" />
<Unit filename="sfw/safe_refcount.cpp" />
<Unit filename="sfw/safe_refcount.h" />
<Unit filename="sfw/sort_array.h" />
<Unit filename="sfw/tight_local_vector.h" />
<Unit filename="sfw/transform.cpp" />
<Unit filename="sfw/transform.h" />
<Unit filename="sfw/transform_2d.cpp" />
<Unit filename="sfw/transform_2d.h" />
<Unit filename="sfw/typedefs.h" />
<Unit filename="sfw/ucaps.h" />
<Unit filename="sfw/ustring.cpp" />
<Unit filename="sfw/ustring.h" />
<Unit filename="sfw/vector.h" />
<Unit filename="sfw/vector2.cpp" />
<Unit filename="sfw/vector2.h" />
<Unit filename="sfw/vector2i.cpp" />
<Unit filename="sfw/vector2i.h" />
<Unit filename="sfw/vector3.cpp" />
<Unit filename="sfw/vector3.h" />
<Unit filename="sfw/vector3i.cpp" />
<Unit filename="sfw/vector3i.h" />
<Unit filename="sfw/vector4.cpp" />
<Unit filename="sfw/vector4.h" />
<Unit filename="sfw/vector4i.cpp" />
<Unit filename="sfw/vector4i.h" />
<Unit filename="sfw/vmap.h" />
<Unit filename="sfw/vset.h" />
<Extensions />
</Project>
</CodeBlocks_project_file>

View File

@ -31,9 +31,6 @@
#include "aabb.h"
#include "core/string/print_string.h"
#include "core/variant/variant.h"
real_t AABB::get_volume() const {
return size.x * size.y * size.z;
}

View File

@ -31,8 +31,7 @@
#include "basis.h"
#include "core/math/math_funcs.h"
#include "core/string/print_string.h"
#include "math_funcs.h"
#define cofac(row1, col1, row2, col2) \
(rows[row1][col1] * rows[row2][col2] - rows[row1][col2] * rows[row2][col1])

View File

@ -31,7 +31,7 @@
#ifndef CHAR_RANGE_INC
#define CHAR_RANGE_INC
#include "core/typedefs.h"
#include "typedefs.h"
struct CharRange {
char32_t start;

View File

@ -32,7 +32,7 @@
#ifndef CHAR_UTILS_H
#define CHAR_UTILS_H
#include "core/typedefs.h"
#include "typedefs.h"
#include "char_range.inc"

View File

@ -31,10 +31,9 @@
#include "color.h"
#include "core/containers/rb_map.h"
#include "core/math/color_names.inc"
#include "core/math/math_funcs.h"
#include "core/string/print_string.h"
#include "rb_map.h"
#include "math_funcs.h"
uint32_t Color::to_argb32() const {
uint32_t c = (uint8_t)Math::round(a * 255);

View File

@ -32,8 +32,8 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "core/math/math_funcs.h"
#include "core/string/ustring.h"
#include "math_funcs.h"
#include "ustring.h"
struct _NO_DISCARD_CLASS_ Color {
union {

View File

@ -34,9 +34,9 @@
#include <string.h>
#include "core/error/error_macros.h"
#include "core/os/memory.h"
#include "core/os/safe_refcount.h"
#include "error_macros.h"
#include "memory.h"
#include "safe_refcount.h"
template <class T>
class Vector;

View File

@ -31,8 +31,6 @@
#include "face3.h"
#include "core/math/geometry.h"
int Face3::split_by_plane(const Plane &p_plane, Face3 p_res[3], bool p_is_point_over[3]) const {
ERR_FAIL_COND_V(is_degenerate(), 0);

View File

@ -32,10 +32,10 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "core/math/aabb.h"
#include "core/math/plane.h"
#include "core/math/transform.h"
#include "core/math/vector3.h"
#include "aabb.h"
#include "plane.h"
#include "transform.h"
#include "vector3.h"
struct _NO_DISCARD_CLASS_ Face3 {
enum Side {

View File

@ -32,11 +32,11 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "core/containers/hashfuncs.h"
#include "core/containers/paged_allocator.h"
#include "core/containers/pair.h"
#include "hashfuncs.h"
#include "paged_allocator.h"
#include "pair.h"
#include "core/math/math_funcs.h"
#include "core/os/memory.h"
#include "memory.h"
/**
* A HashMap implementation that uses open addressing with Robin Hood hashing.

View File

@ -32,10 +32,10 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "core/containers/hash_map.h"
#include "core/containers/hashfuncs.h"
#include "hash_map.h"
#include "hashfuncs.h"
#include "core/math/math_funcs.h"
#include "core/os/memory.h"
#include "memory.h"
/**
* Implementation of Set using a bidi indexed hash map.

View File

@ -32,7 +32,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "core/containers/rid.h"
#include "rid.h"
#include "core/math/aabb.h"
#include "core/math/math_defs.h"
#include "core/math/math_funcs.h"

View File

@ -32,9 +32,9 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "core/containers/sort_array.h"
#include "core/error/error_macros.h"
#include "core/os/memory.h"
#include "sort_array.h"
#include "error_macros.h"
#include "memory.h"
/**
* Generic Templatized Linked List Implementation.

View File

@ -32,11 +32,11 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "core/containers/pool_vector.h"
#include "core/containers/sort_array.h"
#include "core/containers/vector.h"
#include "core/error/error_macros.h"
#include "core/os/memory.h"
#include "pool_vector.h"
#include "sort_array.h"
#include "vector.h"
#include "error_macros.h"
#include "memory.h"
template <class T, class U = uint32_t, bool force_trivial = false>
class LocalVector {

View File

@ -1,7 +1,7 @@
#include "logger.h"
#include "core/string.h"
#include "ustring.h"
#include <cstdio>
#include "logger.h"

View File

@ -31,7 +31,7 @@
#include "math_funcs.h"
#include "core/error/error_macros.h"
#include "error_macros.h"
RandomPCG Math::default_rand(RandomPCG::DEFAULT_SEED, RandomPCG::DEFAULT_INC);

View File

@ -32,8 +32,8 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "core/os/memory.h"
#include "core/os/spin_lock.h"
#include "memory.h"
#include "spin_lock.h"
#include "core/typedefs.h"
template <class T, bool thread_safe = false>

View File

@ -32,7 +32,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "core/containers/hashfuncs.h"
#include "hashfuncs.h"
#include "core/typedefs.h"
template <class F, class S>

View File

@ -4,7 +4,7 @@
#ifndef RANDOM_H
#define RANDOM_H
#include "core/typedefs.h"
#include "typedefs.h"
#define PCG_DEFAULT_INC_64 1442695040888963407ULL

View File

@ -31,8 +31,8 @@
#include "plane.h"
#include "core/math/math_funcs.h"
#include "core/variant/variant.h"
#include "math_funcs.h"
#include "variant/variant.h"
void Plane::set_normal(const Vector3 &p_normal) {
normal = p_normal;

View File

@ -31,14 +31,14 @@
#include "projection.h"
#include "core/math/aabb.h"
#include "core/math/math_funcs.h"
#include "core/math/plane.h"
#include "core/math/rect2.h"
#include "core/math/transform.h"
#include "core/string/print_string.h"
#include "core/variant/array.h"
#include "core/variant/variant.h"
#include "aabb.h"
#include "math_funcs.h"
#include "plane.h"
#include "rect2.h"
#include "transform.h"
#include "string/print_string.h"
#include "variant/array.h"
#include "variant/variant.h"
float Projection::determinant() const {
return matrix[0][3] * matrix[1][2] * matrix[2][1] * matrix[3][0] - matrix[0][2] * matrix[1][3] * matrix[2][1] * matrix[3][0] -

View File

@ -32,10 +32,10 @@
#ifndef PROJECTION_H
#define PROJECTION_H
#include "core/containers/vector.h"
#include "core/math/math_defs.h"
#include "core/math/vector3.h"
#include "core/math/vector4.h"
#include "vector.h"
#include "math_defs.h"
#include "vector3.h"
#include "vector4.h"
class Array;
struct AABB;

View File

@ -31,8 +31,8 @@
#include "quaternion.h"
#include "core/math/basis.h"
#include "core/string/print_string.h"
#include "basis.h"
#include "string/print_string.h"
real_t Quaternion::angle_to(const Quaternion &p_to) const {
real_t d = dot(p_to);

View File

@ -31,7 +31,7 @@
#include "random_pcg.h"
#include "core/os/os.h"
#include "os/os.h"
RandomPCG::RandomPCG(uint64_t p_seed, uint64_t p_inc) :
pcg(),

View File

@ -34,9 +34,9 @@
#include <math.h>
#include "core/math/math_defs.h"
#include "math_defs.h"
#include "thirdparty/misc/pcg.h"
#include "pcg.h"
#if defined(__GNUC__) || (_llvm_has_builtin(__builtin_clz))
#define CLZ32(x) __builtin_clz(x)

View File

@ -32,8 +32,8 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "core/error/error_macros.h"
#include "core/os/memory.h"
#include "error_macros.h"
#include "memory.h"
// based on the very nice implementation of rb-trees by:
// https://web.archive.org/web/20120507164830/http://web.mit.edu/~emin/www/source_code/red_black_tree/index.html

View File

@ -32,7 +32,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "core/os/memory.h"
#include "memory.h"
#include "core/typedefs.h"
// based on the very nice implementation of rb-trees by:

View File

@ -29,9 +29,9 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "core/math/transform_2d.h" // Includes rect2.h but Rect2 needs Transform2D
#include "transform_2d.h" // Includes rect2.h but Rect2 needs Transform2D
#include "core/math/rect2i.h"
#include "rect2i.h"
bool Rect2::is_equal_approx(const Rect2 &p_rect) const {
return position.is_equal_approx(p_rect.position) && size.is_equal_approx(p_rect.size);

View File

@ -32,8 +32,8 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "core/math/vector2.h" // also includes math_funcs and ustring
#include "core/math/vector2i.h"
#include "vector2.h" // also includes math_funcs and ustring
#include "vector2i.h"
struct Transform2D;
struct Rect2i;

View File

@ -29,7 +29,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "core/math/transform_2d.h" // Includes rect2.h but Rect2 needs Transform2D
#include "transform_2d.h" // Includes rect2.h but Rect2 needs Transform2D
Rect2i::operator String() const {
return "[P: " + position.operator String() + ", S: " + size + "]";

View File

@ -32,7 +32,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "core/math/vector2i.h" // also includes math_funcs and ustring
#include "vector2i.h" // also includes math_funcs and ustring
struct _NO_DISCARD_CLASS_ Rect2i {
Point2i position;

View File

@ -32,7 +32,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "core/containers/vector.h"
#include "vector.h"
template <typename T>
class RingBuffer {

46
sfw/safe_refcount.cpp Normal file
View File

@ -0,0 +1,46 @@
/*************************************************************************/
/* safe_refcount.cpp */
/*************************************************************************/
/* This file is part of: */
/* PANDEMONIUM ENGINE */
/* https://github.com/Relintai/pandemonium_engine */
/*************************************************************************/
/* Copyright (c) 2022-present Péter Magyar. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is 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 Software. */
/* */
/* THE SOFTWARE IS 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 */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#if defined(DEBUG_ENABLED) && !defined(NO_THREADS)
#include "safe_refcount.h"
#include "core/error/error_macros.h"
// On C++14 we don't have std::atomic::is_always_lockfree, so this is the best we can do
void check_lockless_atomics() {
// Doing the check for the types we actually care about
if (!std::atomic<uint32_t>{}.is_lock_free() || !std::atomic<uint64_t>{}.is_lock_free() || !std::atomic_bool{}.is_lock_free()) {
WARN_PRINT("Your compiler doesn't seem to support lockless atomics. Performance will be degraded. Please consider upgrading to a different or newer compiler.");
}
}
#endif

334
sfw/safe_refcount.h Normal file
View File

@ -0,0 +1,334 @@
#ifndef SAFE_REFCOUNT_H
#define SAFE_REFCOUNT_H
/*************************************************************************/
/* safe_refcount.h */
/*************************************************************************/
/* This file is part of: */
/* PANDEMONIUM ENGINE */
/* https://github.com/Relintai/pandemonium_engine */
/*************************************************************************/
/* Copyright (c) 2022-present Péter Magyar. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is 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 Software. */
/* */
/* THE SOFTWARE IS 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 */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "core/typedefs.h"
#if !defined(NO_THREADS)
#include <atomic>
#include <type_traits>
// Design goals for these classes:
// - No automatic conversions or arithmetic operators,
// to keep explicit the use of atomics everywhere.
// - Using acquire-release semantics, even to set the first value.
// The first value may be set relaxedly in many cases, but adding the distinction
// between relaxed and unrelaxed operation to the interface would make it needlessly
// flexible. There's negligible waste in having release semantics for the initial
// value and, as an important benefit, you can be sure the value is properly synchronized
// even with threads that are already running.
// This is used in very specific areas of the engine where it's critical that these guarantees are held
#define SAFE_NUMERIC_TYPE_PUN_GUARANTEES(m_type) \
static_assert(sizeof(SafeNumeric<m_type>) == sizeof(m_type), ""); \
static_assert(alignof(SafeNumeric<m_type>) == alignof(m_type), ""); \
static_assert(std::is_trivially_destructible<std::atomic<m_type>>::value, "");
#if defined(DEBUG_ENABLED)
void check_lockless_atomics();
#endif
template <class T>
class SafeNumeric {
std::atomic<T> value;
public:
_ALWAYS_INLINE_ void set(T p_value) {
value.store(p_value, std::memory_order_release);
}
_ALWAYS_INLINE_ T get() const {
return value.load(std::memory_order_acquire);
}
_ALWAYS_INLINE_ T increment() {
return value.fetch_add(1, std::memory_order_acq_rel) + 1;
}
// Returns the original value instead of the new one
_ALWAYS_INLINE_ T postincrement() {
return value.fetch_add(1, std::memory_order_acq_rel);
}
_ALWAYS_INLINE_ T decrement() {
return value.fetch_sub(1, std::memory_order_acq_rel) - 1;
}
// Returns the original value instead of the new one
_ALWAYS_INLINE_ T postdecrement() {
return value.fetch_sub(1, std::memory_order_acq_rel);
}
_ALWAYS_INLINE_ T add(T p_value) {
return value.fetch_add(p_value, std::memory_order_acq_rel) + p_value;
}
// Returns the original value instead of the new one
_ALWAYS_INLINE_ T postadd(T p_value) {
return value.fetch_add(p_value, std::memory_order_acq_rel);
}
_ALWAYS_INLINE_ T sub(T p_value) {
return value.fetch_sub(p_value, std::memory_order_acq_rel) - p_value;
}
// Returns the original value instead of the new one
_ALWAYS_INLINE_ T postsub(T p_value) {
return value.fetch_sub(p_value, std::memory_order_acq_rel);
}
_ALWAYS_INLINE_ T exchange_if_greater(T p_value) {
while (true) {
T tmp = value.load(std::memory_order_acquire);
if (tmp >= p_value) {
return tmp; // already greater, or equal
}
if (value.compare_exchange_weak(tmp, p_value, std::memory_order_acq_rel)) {
return p_value;
}
}
}
_ALWAYS_INLINE_ T conditional_increment() {
while (true) {
T c = value.load(std::memory_order_acquire);
if (c == 0) {
return 0;
}
if (value.compare_exchange_weak(c, c + 1, std::memory_order_acq_rel)) {
return c + 1;
}
}
}
_ALWAYS_INLINE_ explicit SafeNumeric<T>(T p_value = static_cast<T>(0)) {
set(p_value);
}
};
class SafeFlag {
std::atomic_bool flag;
public:
_ALWAYS_INLINE_ bool is_set() const {
return flag.load(std::memory_order_acquire);
}
_ALWAYS_INLINE_ void set() {
flag.store(true, std::memory_order_release);
}
_ALWAYS_INLINE_ void clear() {
flag.store(false, std::memory_order_release);
}
_ALWAYS_INLINE_ void set_to(bool p_value) {
flag.store(p_value, std::memory_order_release);
}
_ALWAYS_INLINE_ explicit SafeFlag(bool p_value = false) {
set_to(p_value);
}
};
class SafeRefCount {
SafeNumeric<uint32_t> count;
public:
_ALWAYS_INLINE_ bool ref() { // true on success
return count.conditional_increment() != 0;
}
_ALWAYS_INLINE_ uint32_t refval() { // none-zero on success
return count.conditional_increment();
}
_ALWAYS_INLINE_ bool unref() { // true if must be disposed of
return count.decrement() == 0;
}
_ALWAYS_INLINE_ uint32_t unrefval() { // 0 if must be disposed of
return count.decrement();
}
_ALWAYS_INLINE_ uint32_t get() const {
return count.get();
}
_ALWAYS_INLINE_ void init(uint32_t p_value = 1) {
count.set(p_value);
}
};
#else
template <class T>
class SafeNumeric {
protected:
T value;
public:
_ALWAYS_INLINE_ void set(T p_value) {
value = p_value;
}
_ALWAYS_INLINE_ T get() const {
return value;
}
_ALWAYS_INLINE_ T increment() {
return ++value;
}
_ALWAYS_INLINE_ T postincrement() {
return value++;
}
_ALWAYS_INLINE_ T decrement() {
return --value;
}
_ALWAYS_INLINE_ T postdecrement() {
return value--;
}
_ALWAYS_INLINE_ T add(T p_value) {
return value += p_value;
}
_ALWAYS_INLINE_ T postadd(T p_value) {
T old = value;
value += p_value;
return old;
}
_ALWAYS_INLINE_ T sub(T p_value) {
return value -= p_value;
}
_ALWAYS_INLINE_ T postsub(T p_value) {
T old = value;
value -= p_value;
return old;
}
_ALWAYS_INLINE_ T exchange_if_greater(T p_value) {
if (value < p_value) {
value = p_value;
}
return value;
}
_ALWAYS_INLINE_ T conditional_increment() {
if (value == 0) {
return 0;
} else {
return ++value;
}
}
_ALWAYS_INLINE_ explicit SafeNumeric<T>(T p_value = static_cast<T>(0)) :
value(p_value) {
}
};
class SafeFlag {
protected:
bool flag;
public:
_ALWAYS_INLINE_ bool is_set() const {
return flag;
}
_ALWAYS_INLINE_ void set() {
flag = true;
}
_ALWAYS_INLINE_ void clear() {
flag = false;
}
_ALWAYS_INLINE_ void set_to(bool p_value) {
flag = p_value;
}
_ALWAYS_INLINE_ explicit SafeFlag(bool p_value = false) :
flag(p_value) {}
};
class SafeRefCount {
uint32_t count;
public:
_ALWAYS_INLINE_ bool ref() { // true on success
if (count != 0) {
++count;
return true;
} else {
return false;
}
}
_ALWAYS_INLINE_ uint32_t refval() { // none-zero on success
if (count != 0) {
return ++count;
} else {
return 0;
}
}
_ALWAYS_INLINE_ bool unref() { // true if must be disposed of
return --count == 0;
}
_ALWAYS_INLINE_ uint32_t unrefval() { // 0 if must be disposed of
return --count;
}
_ALWAYS_INLINE_ uint32_t get() const {
return count;
}
_ALWAYS_INLINE_ void init(uint32_t p_value = 1) {
count = p_value;
}
SafeRefCount() :
count(0) {}
};
#endif
#endif // SAFE_REFCOUNT_H

322
sfw/sort_array.h Normal file
View File

@ -0,0 +1,322 @@
#ifndef SORT_ARRAY_H
#define SORT_ARRAY_H
/*************************************************************************/
/* sort_array.h */
/*************************************************************************/
/* This file is part of: */
/* PANDEMONIUM ENGINE */
/* https://github.com/Relintai/pandemonium_engine */
/*************************************************************************/
/* Copyright (c) 2022-present Péter Magyar. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is 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 Software. */
/* */
/* THE SOFTWARE IS 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 */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "error_macros.h"
#include "typedefs.h"
#define ERR_BAD_COMPARE(cond) \
if (unlikely(cond)) { \
ERR_PRINT("bad comparison function; sorting will be broken"); \
break; \
}
template <class T>
struct _DefaultComparator {
_FORCE_INLINE_ bool operator()(const T &a, const T &b) const { return (a < b); }
};
#ifdef DEBUG_ENABLED
#define SORT_ARRAY_VALIDATE_ENABLED true
#else
#define SORT_ARRAY_VALIDATE_ENABLED false
#endif
template <class T, class Comparator = _DefaultComparator<T>, bool Validate = SORT_ARRAY_VALIDATE_ENABLED>
class SortArray {
enum {
INTROSORT_THRESHOLD = 16
};
public:
Comparator compare;
inline const T &median_of_3(const T &a, const T &b, const T &c) const {
if (compare(a, b)) {
if (compare(b, c)) {
return b;
} else if (compare(a, c)) {
return c;
} else {
return a;
}
} else if (compare(a, c)) {
return a;
} else if (compare(b, c)) {
return c;
} else {
return b;
}
}
inline int bitlog(int n) const {
int k;
for (k = 0; n != 1; n >>= 1) {
++k;
}
return k;
}
/* Heap / Heapsort functions */
inline void push_heap(int p_first, int p_hole_idx, int p_top_index, T p_value, T *p_array) const {
int parent = (p_hole_idx - 1) / 2;
while (p_hole_idx > p_top_index && compare(p_array[p_first + parent], p_value)) {
p_array[p_first + p_hole_idx] = p_array[p_first + parent];
p_hole_idx = parent;
parent = (p_hole_idx - 1) / 2;
}
p_array[p_first + p_hole_idx] = p_value;
}
inline void pop_heap(int p_first, int p_last, int p_result, T p_value, T *p_array) const {
p_array[p_result] = p_array[p_first];
adjust_heap(p_first, 0, p_last - p_first, p_value, p_array);
}
inline void pop_heap(int p_first, int p_last, T *p_array) const {
pop_heap(p_first, p_last - 1, p_last - 1, p_array[p_last - 1], p_array);
}
inline void adjust_heap(int p_first, int p_hole_idx, int p_len, T p_value, T *p_array) const {
int top_index = p_hole_idx;
int second_child = 2 * p_hole_idx + 2;
while (second_child < p_len) {
if (compare(p_array[p_first + second_child], p_array[p_first + (second_child - 1)])) {
second_child--;
}
p_array[p_first + p_hole_idx] = p_array[p_first + second_child];
p_hole_idx = second_child;
second_child = 2 * (second_child + 1);
}
if (second_child == p_len) {
p_array[p_first + p_hole_idx] = p_array[p_first + (second_child - 1)];
p_hole_idx = second_child - 1;
}
push_heap(p_first, p_hole_idx, top_index, p_value, p_array);
}
inline void sort_heap(int p_first, int p_last, T *p_array) const {
while (p_last - p_first > 1) {
pop_heap(p_first, p_last--, p_array);
}
}
inline void make_heap(int p_first, int p_last, T *p_array) const {
if (p_last - p_first < 2) {
return;
}
int len = p_last - p_first;
int parent = (len - 2) / 2;
while (true) {
adjust_heap(p_first, parent, len, p_array[p_first + parent], p_array);
if (parent == 0) {
return;
}
parent--;
}
}
inline void partial_sort(int p_first, int p_last, int p_middle, T *p_array) const {
make_heap(p_first, p_middle, p_array);
for (int i = p_middle; i < p_last; i++) {
if (compare(p_array[i], p_array[p_first])) {
pop_heap(p_first, p_middle, i, p_array[i], p_array);
}
}
sort_heap(p_first, p_middle, p_array);
}
inline void partial_select(int p_first, int p_last, int p_middle, T *p_array) const {
make_heap(p_first, p_middle, p_array);
for (int i = p_middle; i < p_last; i++) {
if (compare(p_array[i], p_array[p_first])) {
pop_heap(p_first, p_middle, i, p_array[i], p_array);
}
}
}
inline int partitioner(int p_first, int p_last, T p_pivot, T *p_array) const {
const int unmodified_first = p_first;
const int unmodified_last = p_last;
while (true) {
while (compare(p_array[p_first], p_pivot)) {
if (Validate) {
ERR_BAD_COMPARE(p_first == unmodified_last - 1);
}
p_first++;
}
p_last--;
while (compare(p_pivot, p_array[p_last])) {
if (Validate) {
ERR_BAD_COMPARE(p_last == unmodified_first);
}
p_last--;
}
if (!(p_first < p_last)) {
return p_first;
}
SWAP(p_array[p_first], p_array[p_last]);
p_first++;
}
}
inline void introsort(int p_first, int p_last, T *p_array, int p_max_depth) const {
while (p_last - p_first > INTROSORT_THRESHOLD) {
if (p_max_depth == 0) {
partial_sort(p_first, p_last, p_last, p_array);
return;
}
p_max_depth--;
int cut = partitioner(
p_first,
p_last,
median_of_3(
p_array[p_first],
p_array[p_first + (p_last - p_first) / 2],
p_array[p_last - 1]),
p_array);
introsort(cut, p_last, p_array, p_max_depth);
p_last = cut;
}
}
inline void introselect(int p_first, int p_nth, int p_last, T *p_array, int p_max_depth) const {
while (p_last - p_first > 3) {
if (p_max_depth == 0) {
partial_select(p_first, p_nth + 1, p_last, p_array);
SWAP(p_first, p_nth);
return;
}
p_max_depth--;
int cut = partitioner(
p_first,
p_last,
median_of_3(
p_array[p_first],
p_array[p_first + (p_last - p_first) / 2],
p_array[p_last - 1]),
p_array);
if (cut <= p_nth) {
p_first = cut;
} else {
p_last = cut;
}
}
insertion_sort(p_first, p_last, p_array);
}
inline void unguarded_linear_insert(int p_last, T p_value, T *p_array) const {
int next = p_last - 1;
while (compare(p_value, p_array[next])) {
if (Validate) {
ERR_BAD_COMPARE(next == 0);
}
p_array[p_last] = p_array[next];
p_last = next;
next--;
}
p_array[p_last] = p_value;
}
inline void linear_insert(int p_first, int p_last, T *p_array) const {
T val = p_array[p_last];
if (compare(val, p_array[p_first])) {
for (int i = p_last; i > p_first; i--) {
p_array[i] = p_array[i - 1];
}
p_array[p_first] = val;
} else {
unguarded_linear_insert(p_last, val, p_array);
}
}
inline void insertion_sort(int p_first, int p_last, T *p_array) const {
if (p_first == p_last) {
return;
}
for (int i = p_first + 1; i != p_last; i++) {
linear_insert(p_first, i, p_array);
}
}
inline void unguarded_insertion_sort(int p_first, int p_last, T *p_array) const {
for (int i = p_first; i != p_last; i++) {
unguarded_linear_insert(i, p_array[i], p_array);
}
}
inline void final_insertion_sort(int p_first, int p_last, T *p_array) const {
if (p_last - p_first > INTROSORT_THRESHOLD) {
insertion_sort(p_first, p_first + INTROSORT_THRESHOLD, p_array);
unguarded_insertion_sort(p_first + INTROSORT_THRESHOLD, p_last, p_array);
} else {
insertion_sort(p_first, p_last, p_array);
}
}
inline void sort_range(int p_first, int p_last, T *p_array) const {
if (p_first != p_last) {
introsort(p_first, p_last, p_array, bitlog(p_last - p_first) * 2);
final_insertion_sort(p_first, p_last, p_array);
}
}
inline void sort(T *p_array, int p_len) const {
sort_range(0, p_len, p_array);
}
inline void nth_element(int p_first, int p_last, int p_nth, T *p_array) const {
if (p_first == p_last || p_nth == p_last) {
return;
}
introselect(p_first, p_nth, p_last, p_array, bitlog(p_last - p_first) * 2);
}
};
#endif // SORT_ARRAY_H

View File

@ -32,11 +32,11 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "core/containers/pool_vector.h"
#include "core/containers/sort_array.h"
#include "core/containers/vector.h"
#include "core/error/error_macros.h"
#include "core/os/memory.h"
#include "pool_vector.h"
#include "sort_array.h"
#include "vector.h"
#include "error_macros.h"
#include "memory.h"
// It grows strictly as much as needed. (The vanilla LocalVector is what you want in most cases).
template <class T, class U = uint32_t, bool force_trivial = false>

View File

@ -31,8 +31,8 @@
#include "transform.h"
#include "core/math/math_funcs.h"
#include "core/string/print_string.h"
#include "math_funcs.h"
#include "string/print_string.h"
void Transform::invert() {
basis.transpose();

View File

@ -32,11 +32,10 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "core/containers/pool_vector.h"
#include "core/math/aabb.h"
#include "core/math/basis.h"
#include "core/math/plane.h"
#include "core/math/vector3i.h"
#include "aabb.h"
#include "basis.h"
#include "plane.h"
#include "vector3i.h"
struct _NO_DISCARD_CLASS_ Transform {
Basis basis;

View File

@ -32,9 +32,9 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "core/containers/pool_vector.h"
#include "core/math/rect2.h" // also includes vector2, math_funcs, and ustring
#include "core/math/rect2i.h" // also includes vector2i, math_funcs, and ustring
#include "pool_vector.h"
#include "rect2.h" // also includes vector2, math_funcs, and ustring
#include "rect2i.h" // also includes vector2i, math_funcs, and ustring
struct _NO_DISCARD_CLASS_ Transform2D {
// Warning #1: basis of Transform2D is stored differently from Basis. In terms of columns array, the basis matrix looks like "on paper":

View File

@ -107,7 +107,7 @@ T *_nullptr() {
#include "int_types.h"
//#include "core/error_list.h"
//#include "error_list.h"
/** Generic ABS function, for math uses please use Math::abs */

View File

@ -35,15 +35,15 @@
#include "ustring.h"
#include "core/crypto/crypto_core.h"
#include "core/math/color.h"
#include "core/math/math_funcs.h"
#include "core/os/memory.h"
#include "core/string/print_string.h"
#include "core/string/translation.h"
#include "core/string/ucaps.h"
#include "core/variant/variant.h"
#include "core/version_generated.gen.h"
#include "crypto/crypto_core.h"
#include "color.h"
#include "math_funcs.h"
#include "os/memory.h"
#include "string/print_string.h"
#include "string/translation.h"
#include "string/ucaps.h"
#include "variant/variant.h"
#include "version_generated.gen.h"
#include <wchar.h>
#include <cstdint>

View File

@ -32,11 +32,10 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "core/containers/cowdata.h"
#include "core/containers/vector.h"
#include "core/string/char_utils.h"
#include "core/typedefs.h"
#include "core/variant/array.h"
#include "cowdata.h"
#include "vector.h"
#include "char_utils.h"
#include "typedefs.h"
/*************************************************************************/
/* CharProxy */

View File

@ -38,10 +38,10 @@
* Vector container. Regular Vector Container. Use with care and for smaller arrays when possible. Use PoolVector for large arrays.
*/
#include "core/containers/cowdata.h"
#include "core/containers/sort_array.h"
#include "core/error/error_macros.h"
#include "core/os/memory.h"
#include "cowdata.h"
#include "sort_array.h"
#include "error_macros.h"
#include "memory.h"
template <class T>
class VectorWriteProxy {

View File

@ -31,7 +31,7 @@
#include "vector2.h"
#include "core/string/ustring.h"
#include "string/ustring.h"
real_t Vector2::angle() const {
return Math::atan2(y, x);
@ -173,4 +173,4 @@ bool Vector2::is_equal_approx(const Vector2 &p_v) const {
Vector2::operator String() const {
return "(" + String::num_real(x) + ", " + String::num_real(y) + ")";
}
}

View File

@ -32,9 +32,9 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "core/math/math_funcs.h"
#include "math_funcs.h"
#include "core/error/error_macros.h"
#include "error/error_macros.h"
class String;

View File

@ -31,7 +31,7 @@
#include "vector2i.h"
#include "core/string/ustring.h"
#include "string/ustring.h"
Vector2i Vector2i::clamp(const Vector2i &p_min, const Vector2i &p_max) const {
return Vector2i(

View File

@ -32,8 +32,8 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "core/error/error_macros.h"
#include "core/math/math_funcs.h"
#include "error/error_macros.h"
#include "math_funcs.h"
#include "vector2.h"

View File

@ -31,7 +31,7 @@
#include "vector3.h"
#include "core/math/basis.h"
#include "basis.h"
void Vector3::rotate(const Vector3 &p_axis, real_t p_phi) {
*this = Basis(p_axis, p_phi).xform(*this);

View File

@ -31,8 +31,8 @@
#include "vector3i.h"
#include "core/math/vector3.h"
#include "core/string/ustring.h"
#include "vector3.h"
#include "string/ustring.h"
void Vector3i::set_axis(const int p_axis, const int32_t p_value) {
ERR_FAIL_INDEX(p_axis, 3);

View File

@ -32,8 +32,8 @@
#ifndef VECTOR3I_H
#define VECTOR3I_H
#include "core/error/error_macros.h"
#include "core/math/math_funcs.h"
#include "error_macros.h"
#include "math_funcs.h"
class String;
struct Vector3;

View File

@ -31,8 +31,8 @@
#include "vector4.h"
#include "core/math/basis.h"
#include "core/string/print_string.h"
#include "basis.h"
#include "string/print_string.h"
void Vector4::set_axis(const int p_axis, const real_t p_value) {
ERR_FAIL_INDEX(p_axis, 4);

View File

@ -32,9 +32,9 @@
#ifndef VECTOR4_H
#define VECTOR4_H
#include "core/math/math_defs.h"
#include "core/math/math_funcs.h"
#include "core/string/ustring.h"
#include "math_defs.h"
#include "math_funcs.h"
#include "string/ustring.h"
struct _NO_DISCARD_CLASS_ Vector4 {
enum Axis {

View File

@ -31,8 +31,8 @@
#include "vector4i.h"
#include "core/math/vector4.h"
#include "core/string/ustring.h"
#include "vector4.h"
#include "string/ustring.h"
void Vector4i::set_axis(const int p_axis, const int32_t p_value) {
ERR_FAIL_INDEX(p_axis, 4);

View File

@ -32,8 +32,8 @@
#ifndef VECTOR4I_H
#define VECTOR4I_H
#include "core/error/error_macros.h"
#include "core/math/math_funcs.h"
#include "error/error_macros.h"
#include "math_funcs.h"
class String;
struct Vector4;

View File

@ -32,7 +32,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "core/containers/cowdata.h"
#include "cowdata.h"
#include "core/typedefs.h"
template <class T, class V>

View File

@ -32,7 +32,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "core/containers/vector.h"
#include "vector.h"
#include "core/typedefs.h"
template <class T>