Skip to content

Commit

Permalink
Adding math macros for constant values and renamed things.
Browse files Browse the repository at this point in the history
  • Loading branch information
Chukobyte committed Dec 25, 2023
1 parent 2c9cdfb commit ebda0f8
Show file tree
Hide file tree
Showing 21 changed files with 220 additions and 204 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ Make sure to link seika to whatever uses it with `target_link_libraries`.

Example projects found [here](https://github.com/Chukobyte/seika-examples).

*Note: Using v0.0.x versions as pre-alpha, won't create full releases until minor version is incremented.*
*Warning: Using v0.0.x versions as pre-alpha, so expect naming inconsistencies and other holdovers from being a part of [crescent](https://github.com/Chukobyte/crescent) until v0.x.x.
26 changes: 13 additions & 13 deletions seika/data_structures/se_spatial_hash_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ typedef struct PositionHashes {
int32_t hashes[SE_SPATIAL_HASH_MAX_POSITION_HASH];
} PositionHashes;

void spatial_hash_map_update(SESpatialHashMap* hashMap, unsigned int entity, SESpatialHashMapGridSpacesHandle* handle, SERect2* collisionRect);
bool change_cell_size_if_needed(SESpatialHashMap* hashMap, SERect2* collisionRectToCheck);
int32_t spatial_hash(SESpatialHashMap* hashMap, SEVector2* position);
void spatial_hash_map_update(SESpatialHashMap* hashMap, unsigned int entity, SESpatialHashMapGridSpacesHandle* handle, SKARect2* collisionRect);
bool change_cell_size_if_needed(SESpatialHashMap* hashMap, SKARect2* collisionRectToCheck);
int32_t spatial_hash(SESpatialHashMap* hashMap, SKAVector2* position);
void spatial_hash_map_destroy_node(SESpatialHashMap* hashMap);
SESpatialHashMapGridSpace* get_or_create_grid_space(SESpatialHashMap* hashMap, int32_t positionHash);
bool link_object_by_position_hash(SESpatialHashMap* hashMap, SESpatialHashMapGridSpacesHandle* object, unsigned int value, int32_t positionHash, PositionHashes* hashes);
Expand Down Expand Up @@ -55,7 +55,7 @@ void se_spatial_hash_map_destroy(SESpatialHashMap* hashMap) {
}

// The purpose of this function is to make sure that 'cellSize' is twice as big as the largest object
bool change_cell_size_if_needed(SESpatialHashMap* hashMap, SERect2* collisionRectToCheck) {
bool change_cell_size_if_needed(SESpatialHashMap* hashMap, SKARect2* collisionRectToCheck) {
const int objectMaxSize = collisionRectToCheck->h > collisionRectToCheck->w ? (int)collisionRectToCheck->h : (int)collisionRectToCheck->w;
// Update largest object size of hashmap if applicable
if (objectMaxSize > hashMap->largestObjectSize) {
Expand All @@ -70,12 +70,12 @@ bool change_cell_size_if_needed(SESpatialHashMap* hashMap, SERect2* collisionRec
return false;
}

SESpatialHashMapGridSpacesHandle* se_spatial_hash_map_insert_or_update(SESpatialHashMap* hashMap, unsigned int entity, SERect2* collisionRect) {
SESpatialHashMapGridSpacesHandle* se_spatial_hash_map_insert_or_update(SESpatialHashMap* hashMap, unsigned int entity, SKARect2* collisionRect) {
// Create new object handle if it doesn't exist
if (!se_hash_map_has(hashMap->objectToGridMap, &entity)) {
SESpatialHashMapGridSpacesHandle* newHandle = SE_MEM_ALLOCATE(SESpatialHashMapGridSpacesHandle);
newHandle->gridSpaceCount = 0;
newHandle->collisionRect = (SERect2) {
newHandle->collisionRect = (SKARect2) {
0.0f, 0.0f, 0.0f, 0.0f
};
se_hash_map_add(hashMap->objectToGridMap, &entity, &newHandle);
Expand All @@ -98,31 +98,31 @@ SESpatialHashMapGridSpacesHandle* se_spatial_hash_map_insert_or_update(SESpatial
return objectHandle;
}

void spatial_hash_map_update(SESpatialHashMap* hashMap, unsigned int entity, SESpatialHashMapGridSpacesHandle* handle, SERect2* collisionRect) {
memcpy(&handle->collisionRect, collisionRect, sizeof(SERect2));
void spatial_hash_map_update(SESpatialHashMap* hashMap, unsigned int entity, SESpatialHashMapGridSpacesHandle* handle, SKARect2* collisionRect) {
memcpy(&handle->collisionRect, collisionRect, sizeof(SKARect2));

// Unlink all previous spaces and objects
unlink_all_objects_by_entity(hashMap, handle, entity);

// Add values to spaces and spaces to object handles (moving clockwise starting from top-left)
PositionHashes hashes = { .hashCount = 0 };
// Top left
const int32_t topLeftHash = spatial_hash(hashMap, &(SEVector2) {
const int32_t topLeftHash = spatial_hash(hashMap, &(SKAVector2) {
collisionRect->x, collisionRect->y
});
link_object_by_position_hash(hashMap, handle, entity, topLeftHash, &hashes);
// Top right
const int32_t topRightHash = spatial_hash(hashMap, &(SEVector2) {
const int32_t topRightHash = spatial_hash(hashMap, &(SKAVector2) {
collisionRect->x + collisionRect->w, collisionRect->y
});
link_object_by_position_hash(hashMap, handle, entity, topRightHash, &hashes);
// Bottom Left
const int32_t bottomLeftHash = spatial_hash(hashMap, &(SEVector2) {
const int32_t bottomLeftHash = spatial_hash(hashMap, &(SKAVector2) {
collisionRect->x, collisionRect->y + collisionRect->h
});
link_object_by_position_hash(hashMap, handle, entity, bottomLeftHash, &hashes);
// Bottom Right
const int32_t bottomRightHash = spatial_hash(hashMap, &(SEVector2) {
const int32_t bottomRightHash = spatial_hash(hashMap, &(SKAVector2) {
collisionRect->x + collisionRect->w, collisionRect->y + collisionRect->h
});
link_object_by_position_hash(hashMap, handle, entity, bottomRightHash, &hashes);
Expand Down Expand Up @@ -190,7 +190,7 @@ SESpatialHashMapCollisionResult se_spatial_hash_map_compute_collision(SESpatialH
}

// Internal Functions
int32_t spatial_hash(SESpatialHashMap* hashMap, SEVector2* position) {
int32_t spatial_hash(SESpatialHashMap* hashMap, SKAVector2* position) {
const int32_t x = (int32_t) position->x / hashMap->cellSize;
const int32_t y = (int32_t) position->y / hashMap->cellSize;
const int32_t hash = ((x * x) ^ (y * y)) % INT32_MAX;
Expand Down
4 changes: 2 additions & 2 deletions seika/data_structures/se_spatial_hash_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ typedef struct SESpatialHashMapGridSpace {
// Contains all grid spaces an object is assigned to
typedef struct SESpatialHashMapGridSpacesHandle {
size_t gridSpaceCount;
SERect2 collisionRect;
SKARect2 collisionRect;
SESpatialHashMapGridSpace* gridSpaces[4];
} SESpatialHashMapGridSpacesHandle;

Expand All @@ -37,7 +37,7 @@ typedef struct SESpatialHashMapCollisionResult {

SESpatialHashMap* se_spatial_hash_map_create(int initialCellSize);
void se_spatial_hash_map_destroy(SESpatialHashMap* hashMap);
SESpatialHashMapGridSpacesHandle* se_spatial_hash_map_insert_or_update(SESpatialHashMap* hashMap, unsigned int entity, SERect2* collisionRect);
SESpatialHashMapGridSpacesHandle* se_spatial_hash_map_insert_or_update(SESpatialHashMap* hashMap, unsigned int entity, SKARect2* collisionRect);
void se_spatial_hash_map_remove(SESpatialHashMap* hashMap, unsigned int entity);
SESpatialHashMapGridSpacesHandle* se_spatial_hash_map_get(SESpatialHashMap* hashMap, unsigned int entity);
SESpatialHashMapCollisionResult se_spatial_hash_map_compute_collision(SESpatialHashMap* hashMap, unsigned int entity);
10 changes: 5 additions & 5 deletions seika/data_structures/se_tile_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
#define SE_TILE_MAP_MAX_TILES 36

typedef struct SETile {
SEVector2 position;
SKAVector2 position;
} SETile;

typedef struct SETileMap {
SETexture* tileSpriteSheet;
SEVector2 tileSize;
SKAVector2 tileSize;
SETile tiles[36];
} SETileMap;

SETileMap* se_tile_map_create(SETexture* tileSpriteSheet);
void se_tile_map_add_tile(SETileMap* tileMap, SEVector2 position);
SETile* se_tile_map_get_tile(SETileMap* tileMap, SEVector2 position);
SETile* se_tile_map_has_tile(SETileMap* tileMap, SEVector2 position);
void se_tile_map_add_tile(SETileMap* tileMap, SKAVector2 position);
SETile* se_tile_map_get_tile(SETileMap* tileMap, SKAVector2 position);
SETile* se_tile_map_has_tile(SETileMap* tileMap, SKAVector2 position);
void se_tile_map_destroy(SETileMap* tileMap);
4 changes: 2 additions & 2 deletions seika/input/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -772,8 +772,8 @@ void input_gamepad_cleanup_flags() {
void se_input_gamepad_start_vibration(int device, float weakMagnitude, float strongMagnitude, float durationSeconds) {
for (int i = 0; i < activeGamepadCount; i++) {
if (i == device) {
weakMagnitude = se_math_clamp_float(weakMagnitude, 0.0f, 1.0f);
strongMagnitude = se_math_clamp_float(strongMagnitude, 0.0f, 1.0f);
weakMagnitude = ska_math_clamp_float(weakMagnitude, 0.0f, 1.0f);
strongMagnitude = ska_math_clamp_float(strongMagnitude, 0.0f, 1.0f);
const Uint16 weakMag = (Uint16)(weakMagnitude * 65535.0f + 0.5f);
const Uint16 strongMag = (Uint16)(strongMagnitude * 65535.0f + 0.5f);
const Uint32 durationMilliseconds = (Uint32)(durationSeconds * 1000.0f);
Expand Down
2 changes: 1 addition & 1 deletion seika/input/mouse.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "../math/se_math.h"

typedef struct SEMouse {
SEVector2 position;
SKAVector2 position;
} SEMouse;

SEMouse* se_mouse_get();
Expand Down
2 changes: 1 addition & 1 deletion seika/math/se_curve_float.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void se_curve_float_add_control_points(SECurveFloat* curve, SECurveControlPoint
bool se_curve_float_remove_control_point(SECurveFloat* curve, double x, double y) {
for (size_t i = 0; i < curve->controlPointCount; i++) {
SECurveControlPoint* point = &curve->controlPoints[i];
if (se_math_is_almost_equal_double_default(x, point->x) && se_math_is_almost_equal_double_default(y, point->y)) {
if (ska_math_is_almost_equal_double_default(x, point->x) && ska_math_is_almost_equal_double_default(y, point->y)) {
// We've found a matching point so set it's x to the highest value, sort it, then decrement the point count.
point->x = DBL_MAX;
selection_sort_curve_float(curve);
Expand Down
64 changes: 32 additions & 32 deletions seika/math/se_math.c
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
#include "se_math.h"

// --- Vector2 --- //
bool se_math_vec2_equals(const SEVector2* v1, const SEVector2* v2) {
bool ska_math_vec2_equals(const SKAVector2* v1, const SKAVector2* v2) {
return v1->x == v2->x && v1->y == v2->y;
}

SEVector2 se_math_vec2_lerp(const SEVector2* v1, const SEVector2* v2, float t) {
return (SEVector2) {
.x = se_math_lerpf(v1->x, v2->x, t),
.y = se_math_lerpf(v1->y, v2->y, t)
SKAVector2 ska_math_vec2_lerp(const SKAVector2* v1, const SKAVector2* v2, float t) {
return (SKAVector2) {
.x = ska_math_lerpf(v1->x, v2->x, t),
.y = ska_math_lerpf(v1->y, v2->y, t)
};
}

// --- Rect2 --- //
bool se_rect2_does_rectangles_overlap(const SERect2* sourceRect, const SERect2* targetRect) {
bool se_rect2_does_rectangles_overlap(const SKARect2* sourceRect, const SKARect2* targetRect) {
return (sourceRect->x + sourceRect->w >= targetRect->x) &&
(targetRect->x + targetRect->w >= sourceRect->x) &&
(sourceRect->y + sourceRect->h >= targetRect->y) &&
(targetRect->y + targetRect->h >= sourceRect->y);
}

// --- Color --- //
SEColor se_color_get_normalized_color_default_alpha(unsigned int r, unsigned int g, unsigned int b) {
SEColor color = {
SKAColor ska_color_get_normalized_color_default_alpha(unsigned int r, unsigned int g, unsigned int b) {
SKAColor color = {
.r = (float) r / 255.0f,
.g = (float) g / 255.0f,
.b = (float) b / 255.0f,
Expand All @@ -31,8 +31,8 @@ SEColor se_color_get_normalized_color_default_alpha(unsigned int r, unsigned int
return color;
}

SEColor se_color_get_normalized_color(unsigned int r, unsigned int g, unsigned int b, unsigned int a) {
SEColor color = {
SKAColor ska_color_get_normalized_color(unsigned int r, unsigned int g, unsigned int b, unsigned int a) {
SKAColor color = {
.r = (float) r / 255.0f,
.g = (float) g / 255.0f,
.b = (float) b / 255.0f,
Expand All @@ -41,8 +41,8 @@ SEColor se_color_get_normalized_color(unsigned int r, unsigned int g, unsigned i
return color;
}

SEColor se_color_get_normalized_color_from_color(const SEColor* color) {
SEColor newColor = {
SKAColor ska_color_get_normalized_color_from_color(const SKAColor* color) {
SKAColor newColor = {
.r = color->r / 255.0f,
.g = color->g / 255.0f,
.b = color->b / 255.0f,
Expand All @@ -51,33 +51,33 @@ SEColor se_color_get_normalized_color_from_color(const SEColor* color) {
return newColor;
}

SEColor se_color_get_white() {
SEColor white = { 1.0f, 1.0f, 1.0f, 1.0f };
SKAColor ska_color_get_white() {
SKAColor white = {1.0f, 1.0f, 1.0f, 1.0f };
return white;
}

// --- Misc --- //
float se_math_lerpf(float a, float b, float t) {
float ska_math_lerpf(float a, float b, float t) {
return a + (b - a) * t;
}

float se_math_map_to_range(float input, float inputMin, float inputMax, float outputMin, float outputMax) {
float ska_math_map_to_range(float input, float inputMin, float inputMax, float outputMin, float outputMax) {
return (((input - inputMin) / (inputMax - inputMin)) * (outputMax - outputMin) + outputMin);
}

float se_math_map_to_unit(float input, float inputMin, float inputMax) {
return se_math_map_to_range(input, inputMin, inputMax, 0.0f, 1.0f);
float ska_math_map_to_unit(float input, float inputMin, float inputMax) {
return ska_math_map_to_range(input, inputMin, inputMax, 0.0f, 1.0f);
}

double se_math_map_to_range_double(double input, double inputMin, double inputMax, double outputMin, double outputMax) {
double ska_math_map_to_range_double(double input, double inputMin, double inputMax, double outputMin, double outputMax) {
return (((input - inputMin) / (inputMax - inputMin)) * (outputMax - outputMin) + outputMin);
}

double se_math_map_to_unit_double(double input, double inputMin, double inputMax) {
return se_math_map_to_range_double(input, inputMin, inputMax, 0.0, 1.0);
double ska_math_map_to_unit_double(double input, double inputMin, double inputMax) {
return ska_math_map_to_range_double(input, inputMin, inputMax, 0.0, 1.0);
}

float se_math_signf(float value) {
float ska_math_signf(float value) {
if (value > 0.0f) {
return 1.0f;
} else if(value < 0.0f) {
Expand All @@ -86,36 +86,36 @@ float se_math_signf(float value) {
return 0.0f;
}

SEVector2 se_math_signvec2(SEVector2* value) {
SEVector2 sign_vec = {
.x = se_math_signf(value->x),
.y = se_math_signf(value->y)
SKAVector2 ska_math_signvec2(SKAVector2* value) {
SKAVector2 sign_vec = {
.x = ska_math_signf(value->x),
.y = ska_math_signf(value->y)
};
return sign_vec;
}

int se_math_clamp_int(int value, int min, int max) {
int ska_math_clamp_int(int value, int min, int max) {
return value < min ? min : (value > max ? max : value);
}

float se_math_clamp_float(float value, float min, float max) {
float ska_math_clamp_float(float value, float min, float max) {
return value < min ? min : (value > max ? max : value);
}

bool se_math_is_almost_equal_float(float v1, float v2, float epsilon) {
bool ska_math_is_almost_equal_float(float v1, float v2, float epsilon) {
return fabsf(v1 - v2) <= epsilon;
}

bool se_math_is_almost_equal_float_default(float v1, float v2) {
bool ska_math_is_almost_equal_float_default(float v1, float v2) {
static const double epsilon = 0.001f;
return fabsf(v1 - v2) <= epsilon;
}

bool se_math_is_almost_equal_double(double v1, double v2, double epsilon) {
bool ska_math_is_almost_equal_double(double v1, double v2, double epsilon) {
return fabs(v1 - v2) <= epsilon;
}

bool se_math_is_almost_equal_double_default(double v1, double v2) {
bool ska_math_is_almost_equal_double_default(double v1, double v2) {
static const double epsilon = 0.001;
return fabs(v1 - v2) <= epsilon;
}
Loading

0 comments on commit ebda0f8

Please sign in to comment.