Moved some code to shared dir
This commit is contained in:
@ -1,45 +0,0 @@
|
||||
// Copyright (c) 2022 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "dawnlibs.hpp"
|
||||
#include "assert/assert.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
/**
|
||||
* Append a list on to another list.
|
||||
*
|
||||
* @param list Pointer to list that is being appended to.
|
||||
* @param append Pointer to list that will be appended.
|
||||
*/
|
||||
template<typename T>
|
||||
void vectorAppend(std::vector<T> *list, std::vector<T> *append) {
|
||||
assertNotNull(list);
|
||||
assertNotNull(append);
|
||||
|
||||
auto it = append->begin();
|
||||
while(it != append->end()) {
|
||||
list->push_back(*it);
|
||||
++it;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Append a list on to another list.
|
||||
*
|
||||
* @param list Pointer to list that is being appended to.
|
||||
* @param append List that will be appended.
|
||||
*/
|
||||
template<typename T>
|
||||
void vectorAppend(std::vector<T> *list, std::vector<T> append) {
|
||||
assertNotNull(list);
|
||||
|
||||
auto it = append.begin();
|
||||
while(it != append.end()) {
|
||||
list->push_back(*it);
|
||||
++it;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,15 +0,0 @@
|
||||
// Copyright (c) 2022 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "dawnlibs.hpp"
|
||||
|
||||
typedef uint_fast8_t flag8_t;
|
||||
typedef uint_fast16_t flag16_t;
|
||||
typedef uint_fast32_t flag32_t;
|
||||
|
||||
typedef flag32_t flag_t;
|
||||
|
||||
#define FLAG_DEFINE(n) (1 << n)
|
@ -1,117 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2022 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "dawnlibs.hpp"
|
||||
|
||||
#define MATH_PI 3.1415926535897f
|
||||
|
||||
namespace Dawn {
|
||||
/**
|
||||
* Returns the largest of the two provided int32 numbers.
|
||||
*
|
||||
* @param left Left number to get the largest of.
|
||||
* @param right Right number to get the largest of.
|
||||
* @return The larger of the two numbers
|
||||
*/
|
||||
template<typename T>
|
||||
static T mathMax(T left, T right) {
|
||||
return left < right ? right : left;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the smallest of two provided int32 numbers.
|
||||
*
|
||||
* @param left Left number to get the smallest of.
|
||||
* @param right Right number to get the smallest of.
|
||||
* @return Smaller of the two numbers.
|
||||
*/
|
||||
template<typename T>
|
||||
static T mathMin(T left, T right) {
|
||||
return left < right ? left : right;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the input value, constrained between the min and max values, so that
|
||||
* the value cannot underceed the min, and cannot exceed the max.
|
||||
*
|
||||
* @param val Value to get the clamp for.
|
||||
* @param min Minimum clamping value.
|
||||
* @param max Maximum clamping value.
|
||||
* @return The value, or the closest clamped value.
|
||||
*/
|
||||
template<typename T>
|
||||
static T mathClamp(T val, T min, T max) {
|
||||
return mathMin<T>(mathMax<T>(val, min), max);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the absolute value (the non-negative representation of) for the given
|
||||
* int32 number.Abs values will be -value if value < 0.
|
||||
*
|
||||
* @param value Value to get the absolute value for.
|
||||
* @return The absolute value (-value if value < 0)
|
||||
*/
|
||||
template<typename T>
|
||||
static T mathAbs(T value) {
|
||||
return value < 0 ? -value : value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the modulous a result for b. Works for floating point numbers.
|
||||
*
|
||||
* @param a Number to modulo against. (a % b)
|
||||
* @param b Number to modulo with. (a % b)
|
||||
* @returns The modulo result.
|
||||
*/
|
||||
static float_t mathMod(float_t value, float_t modulo) {
|
||||
return (float_t)fmod(value, modulo);
|
||||
}
|
||||
|
||||
static int32_t mathMod(int32_t value, int32_t modulo) {
|
||||
return ((value % modulo) + modulo) % modulo;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert degrees to radians.
|
||||
*
|
||||
* @param n Degrees to convert.
|
||||
* @returns The number in radians.
|
||||
*/
|
||||
static float_t mathDeg2Rad(float_t degrees) {
|
||||
return degrees * (MATH_PI / 180.0f);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert radians to degrees.
|
||||
* @param n Radians to convert.
|
||||
* @returns The number in degrees.
|
||||
*/
|
||||
static float_t mathRad2Deg(float_t n) {
|
||||
return (n * 180.0f) / MATH_PI;
|
||||
}
|
||||
|
||||
/**
|
||||
* Round a number to the nearest whole number.
|
||||
* @param n Number to round.
|
||||
* @return Rounded number.
|
||||
*/
|
||||
template<typename T>
|
||||
static T mathRound(float_t n) {
|
||||
return (T)roundf(n);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rounds the number down to the nearest whole number.
|
||||
* @param n Number to round down.
|
||||
* @return Rounded number.
|
||||
*/
|
||||
template<typename T>
|
||||
static T mathFloor(float_t n) {
|
||||
return (T)floorf(n);
|
||||
}
|
||||
}
|
@ -1,108 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2022 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "dawnlibs.hpp"
|
||||
|
||||
/**
|
||||
* Allocate some space in memory to use for your needs. Memory allocation may
|
||||
* change how it functions later on to keep things nice and efficient. For now
|
||||
* this is just an API forward for malloc.
|
||||
*
|
||||
* @param size Size of the array you wish to buffer.
|
||||
* @return Pointer to the space in memory to use.
|
||||
*/
|
||||
static inline void * memoryAllocate(const size_t size) {
|
||||
return (void *)malloc(size);
|
||||
}
|
||||
|
||||
/**
|
||||
* Allocate space in memory, where all values are set to 0 (in binary space).
|
||||
*
|
||||
* @param size Size of the array.
|
||||
* @return Pointer to the space in memory to use.
|
||||
*/
|
||||
static inline void * memoryFillWithZero(const size_t size) {
|
||||
return (void *)calloc(1, size);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Free some previously allocated memory space.
|
||||
* @param pointer Pointer in memory to free.
|
||||
*/
|
||||
static inline void memoryFree(void *pointer) {
|
||||
free(pointer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies data from one buffer to another. Typically used for array operations.
|
||||
*
|
||||
* @param source Source pointer.
|
||||
* @param destination Destination buffer.
|
||||
* @param size Size in bytes of data to copy.
|
||||
*/
|
||||
static inline void memoryCopy(
|
||||
void *source,
|
||||
void *destination,
|
||||
size_t size
|
||||
) {
|
||||
memcpy(destination, source, size);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares the data within two memory banks. Shorthand for memcpy.
|
||||
*
|
||||
* @param left Left item to compare.
|
||||
* @param right Right item to compare.
|
||||
* @param size Count of bytes to compare.
|
||||
* @return 0 for equal, <0 for left being greater, >0 for right being greater.
|
||||
*/
|
||||
static inline int32_t memoryCompare(
|
||||
const void *left,
|
||||
const void *right,
|
||||
const size_t size
|
||||
) {
|
||||
return memcmp(left, right, size);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fill destination with a repeating set of bytes.
|
||||
*
|
||||
* @param dest Destination pointer in memory.
|
||||
* @param data Data byte to write.
|
||||
* @param length How many times to write that byte.
|
||||
*/
|
||||
static inline void memorySet(
|
||||
void *dest,
|
||||
uint8_t data,
|
||||
size_t length
|
||||
) {
|
||||
memset(dest, data, length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reallocate a part of memory. Reallocation simply creates a new buffer that
|
||||
* will take all of the existing contents and then free's the original buffer.
|
||||
*
|
||||
* @param pointer Pointer to pointer in memory that you wish to re-allocate.
|
||||
* @param currentSize Current size of the buffer that the pointer points to.
|
||||
* @param newSize The new size of the buffer.
|
||||
* @return The new size param you provided.
|
||||
*/
|
||||
static inline size_t memoryReallocate(
|
||||
void **pointer,
|
||||
size_t currentSize,
|
||||
size_t newSize
|
||||
) {
|
||||
// Create the new buffer
|
||||
void *newBuffer = memoryAllocate(newSize);
|
||||
memoryCopy(*pointer, newBuffer, currentSize);
|
||||
memoryFree(*pointer);
|
||||
*pointer = newBuffer;
|
||||
return newSize;
|
||||
}
|
@ -1,43 +0,0 @@
|
||||
// Copyright (c) 2022 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "dawnlibs.hpp"
|
||||
#include "mathutils.hpp"
|
||||
|
||||
/**
|
||||
* Seed the random number generator
|
||||
* @param seed Seed to use for the seeded random number generator.
|
||||
*/
|
||||
static void randSeed(int32_t seed) {
|
||||
srand(seed);
|
||||
}
|
||||
|
||||
static int32_t randomGeneratei32() {
|
||||
return (int32_t)rand();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a random number.
|
||||
* @returns A random number.
|
||||
*/
|
||||
template<typename T>
|
||||
static T randomGenerate() {
|
||||
return (T)((float_t)randomGeneratei32() * MATH_PI);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* Clamps a random number generation.
|
||||
*
|
||||
* @param min Minimum value to generate from. (Inclusive)
|
||||
* @param max Maximum value to generate to. (Exclusive)
|
||||
* @return Random number between min and max.
|
||||
*/
|
||||
template<typename T>
|
||||
static inline T randRange(T min, T max) {
|
||||
return mathMod(randomGenerate<T>(), (max - min)) + min;
|
||||
}
|
@ -1,35 +0,0 @@
|
||||
// Copyright (c) 2022 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "assert/assert.hpp"
|
||||
|
||||
/**
|
||||
* Finds the next instance of a character within a string, safely (with a
|
||||
* limit). The returned pointer will be NULL if not found, or a pointer to a
|
||||
* point within the string where the instance is.
|
||||
*
|
||||
* @param haystack String to search.
|
||||
* @param needle Character to search for.
|
||||
* @param limit Max length you want to search for to limit yourself to.
|
||||
* @return Pointer to the character found, or NULL if not found.
|
||||
*/
|
||||
static inline char * stringFindNext(
|
||||
char *haystack,
|
||||
char needle,
|
||||
size_t limit
|
||||
) {
|
||||
char *p;
|
||||
|
||||
assertNotNull(haystack);
|
||||
assertTrue(limit > 0);
|
||||
|
||||
for(p = haystack; (size_t)(p - haystack) < limit; p++) {
|
||||
if(*p == needle) return p;
|
||||
assertFalse(*p == '\0');// We don't allow you to have a limit > strlen
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
Reference in New Issue
Block a user