Added aliases with sizes for the integer and floating-point numbers

This commit is contained in:
abdelrahman committed 2022-11-27 15:04:34 +00:00
1 parent 45f6021f82
commit 5e6211c2a9
14 files changed
+146 -136

No files matched your search

+17 -18
View File
@@ -5,11 +5,10 @@
#include "SDL_mixer.h"
#include "SDL_render.h"
#include "SDL_ttf.h"
#include "aliases.h"
#include "constants.h"
#include "reusable_ids.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -33,27 +32,27 @@ struct id_node {
typedef struct id_node_list {
IDNode *start_node;
size_t node_count;
u64 node_count;
} IDNodeList;
struct asset_manager {
size_t count;
size_t deleted_count;
u64 count;
u64 deleted_count;
AssetID reusable_ids[MAX_ASSET_COUNT];
Asset assets[MAX_ASSET_COUNT];
size_t user_counter[MAX_ASSET_COUNT];
u64 user_counter[MAX_ASSET_COUNT];
IDNodeList hashed_path_ids[MAX_ASSET_COUNT];
};
size_t hash_path(const char *path) {
u64 hash_path(const char *path) {
/**
* Converts a string into a numerical index using a simple hashing algorithm
*/
size_t i = 0;
u64 i = 0;
for (size_t j = 0; path[j]; ++j) {
i += (size_t)path[j] * HASH_PRIME_01 % MAX_ASSET_COUNT;
for (u64 j = 0; path[j]; ++j) {
i += (u64)path[j] * HASH_PRIME_01 % MAX_ASSET_COUNT;
}
i = (i * HASH_PRIME_02) % MAX_ASSET_COUNT;
@@ -73,7 +72,7 @@ void insert_id_node(IDNodeList *node_list, AssetID id) {
}
IDNode *last_node = node_list->start_node;
for (size_t i = 1; i < node_list->node_count; ++i) {
for (u64 i = 1; i < node_list->node_count; ++i) {
last_node = last_node->next;
}
@@ -99,7 +98,7 @@ void remove_id_node(IDNodeList *node_list, AssetID id) {
IDNode *node = NULL;
IDNode *prev = NULL;
IDNode *current = node_list->start_node;
for (size_t i = 0; i < node_list->node_count; ++i) {
for (u64 i = 0; i < node_list->node_count; ++i) {
if (current->id == id) {
node = current;
break;
@@ -139,7 +138,7 @@ AssetID get_asset_id_from_id_node(AssetManager *am, IDNodeList *node_list,
}
IDNode *id_node = node_list->start_node;
for (size_t i = 0; i < node_list->node_count; ++i) {
for (u64 i = 0; i < node_list->node_count; ++i) {
if (!id_node) {
break;
}
@@ -178,7 +177,7 @@ AssetID load_asset_to_manager(AssetManager *am, const char *path,
return INVALID_ID;
}
size_t hash_index = hash_path(path);
u64 hash_index = hash_path(path);
AssetID existing_id =
get_asset_id_from_id_node(am, &(am->hashed_path_ids[hash_index]), path);
@@ -255,7 +254,7 @@ AssetID load_asset_to_manager(AssetManager *am, const char *path,
AssetID id;
if (am->deleted_count > 0) {
id = (AssetID)reuse_id((int32_t *)am->reusable_ids, am->deleted_count);
id = (AssetID)reuse_id((i32 *)am->reusable_ids, am->deleted_count);
am->deleted_count -= 1;
am->count += 1;
} else {
@@ -308,7 +307,7 @@ void remove_asset_from_manager(AssetManager *am, AssetID *id_ptr) {
return;
}
size_t hash_index = hash_path(am->assets[id].path);
u64 hash_index = hash_path(am->assets[id].path);
remove_id_node(&(am->hashed_path_ids[hash_index]), id);
free(am->assets[id].path);
@@ -380,14 +379,14 @@ void destroy_asset_manager(AssetManager **am) {
// TODO (Abdelrahman): Figure out if there is a better way to remove all of
// the existing assets without having to loop for the maximum number of
// assets allowed.
for (size_t i = 0; i < MAX_ASSET_COUNT; ++i) {
for (u64 i = 0; i < MAX_ASSET_COUNT; ++i) {
if (!(*am)->assets[i].path) {
continue;
}
(*am)->user_counter[i] = 0;
size_t hash_index = hash_path((*am)->assets[i].path);
u64 hash_index = hash_path((*am)->assets[i].path);
remove_id_node(&((*am)->hashed_path_ids[hash_index]), i);
free((*am)->assets[i].path);
+41 -44
View File
@@ -22,8 +22,6 @@
#include "engine_build_config.h"
#include "reusable_ids.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
@@ -33,8 +31,8 @@
INTERNAL struct game_engine {
bool running;
float delta;
uint64_t last_time;
f32 delta;
u64 last_time;
Window window;
SDL_Event event;
ObjectStore *object_store;
@@ -43,7 +41,7 @@ INTERNAL struct game_engine {
bool init_engine(const char *game_name, WindowSize window_size,
SDL_Color window_background_color, bool full_screen,
uint8_t audio_tracks) {
u8 audio_tracks) {
bool initialisation_complete = true;
const char *current_platform = SDL_GetPlatform();
@@ -59,7 +57,7 @@ bool init_engine(const char *game_name, WindowSize window_size,
return false;
}
uint32_t img_flags = IMG_INIT_PNG;
u32 img_flags = IMG_INIT_PNG;
if (strcmp(current_platform, "Android") != 0) {
// Apparently, Android devices don't support TIFF images, so we only enable
// TIFF support when we are not running Android.
@@ -177,7 +175,7 @@ void close_engine() {
#endif /* DEBUG_ENGINE */
}
int64_t get_time() { return SDL_GetTicks64(); }
i64 get_time() { return SDL_GetTicks64(); }
ObjectID add_object(void *object, EventHandlerPtr event_handler,
UpdateFuncPtr update_func, RenderFuncPtr render_func) {
@@ -252,10 +250,10 @@ void clip_render_rects_to_drawing_area(RenderRect *src, RenderRect *dest) {
return;
}
float x_offset = 0.0f;
float y_offset = 0.0f;
float x_clip = 0.0f;
float y_clip = 0.0f;
f32 x_offset = 0.0f;
f32 y_offset = 0.0f;
f32 x_clip = 0.0f;
f32 y_clip = 0.0f;
// If the rectangle is partially out of the horizontal bounds of the drawing
// area, calculate how much of its width will be clipped
@@ -302,8 +300,8 @@ RenderRect get_texture_asset_rect(AssetID id) {
SDL_Texture *texture = get_texture(GameEngine.asset_manager, id);
if (texture) {
int32_t w = 0;
int32_t h = 0;
i32 w = 0;
i32 h = 0;
if (SDL_QueryTexture(texture, NULL, NULL, &w, &h) == 0) {
rect.width = w;
rect.height = h;
@@ -344,7 +342,7 @@ TextSize get_text_size_from_font_asset(AssetID id, const char *text) {
return output;
}
void render_text_with_font_asset(AssetID id, const char *text, float x, float y,
void render_text_with_font_asset(AssetID id, const char *text, f32 x, f32 y,
SDL_Color color) {
if (id < 0) {
return;
@@ -361,8 +359,8 @@ void render_text_with_font_asset(AssetID id, const char *text, float x, float y,
}
// Get the width and height, in pixels, of the text rendered using the font
int32_t w = 0;
int32_t h = 0;
i32 w = 0;
i32 h = 0;
TTF_SizeText(font, text, &w, &h);
RenderRect rect = {x, y, w, h};
@@ -373,7 +371,7 @@ void render_text_with_font_asset(AssetID id, const char *text, float x, float y,
surface = NULL;
}
void set_font_asset_size(AssetID id, uint32_t size) {
void set_font_asset_size(AssetID id, u32 size) {
if (id < 0) {
return;
}
@@ -386,8 +384,8 @@ void set_font_asset_size(AssetID id, uint32_t size) {
TTF_SetFontSize(font, size);
}
int8_t play_audio_asset(AssetID id, int16_t loops, bool fade_in,
uint32_t fade_length_in_ms) {
i8 play_audio_asset(AssetID id, i16 loops, bool fade_in,
u32 fade_length_in_ms) {
if (id < 0) {
return -1;
}
@@ -404,7 +402,7 @@ int8_t play_audio_asset(AssetID id, int16_t loops, bool fade_in,
}
}
void pause_audio_asset_on_track(AssetID id, uint8_t track) {
void pause_audio_asset_on_track(AssetID id, u8 track) {
if (id < 0) {
return;
}
@@ -417,7 +415,7 @@ void pause_audio_asset_on_track(AssetID id, uint8_t track) {
Mix_Pause(track);
}
void resume_audio_asset_on_track(AssetID id, uint8_t track) {
void resume_audio_asset_on_track(AssetID id, u8 track) {
if (id < 0) {
return;
}
@@ -430,7 +428,7 @@ void resume_audio_asset_on_track(AssetID id, uint8_t track) {
Mix_Resume(track);
}
void stop_audio_asset_on_track(AssetID id, uint8_t track) {
void stop_audio_asset_on_track(AssetID id, u8 track) {
if (id < 0) {
return;
}
@@ -443,8 +441,8 @@ void stop_audio_asset_on_track(AssetID id, uint8_t track) {
Mix_HaltChannel(track);
}
void fade_out_audio_asset_on_track(AssetID id, uint8_t track,
uint32_t fade_length_in_ms) {
void fade_out_audio_asset_on_track(AssetID id, u8 track,
u32 fade_length_in_ms) {
if (id < 0) {
return;
}
@@ -467,9 +465,9 @@ void pause_all_instances_of_audio_asset(AssetID id) {
return;
}
size_t channel_count = Mix_AllocateChannels(-1);
u64 channel_count = Mix_AllocateChannels(-1);
for (size_t i = 0; i < channel_count; ++i) {
for (u64 i = 0; i < channel_count; ++i) {
if (chunk == Mix_GetChunk(i)) {
Mix_Pause(i);
}
@@ -486,9 +484,9 @@ void resume_all_instances_of_audio_asset(AssetID id) {
return;
}
size_t channel_count = Mix_AllocateChannels(-1);
u64 channel_count = Mix_AllocateChannels(-1);
for (size_t i = 0; i < channel_count; ++i) {
for (u64 i = 0; i < channel_count; ++i) {
if (chunk == Mix_GetChunk(i)) {
Mix_Resume(i);
}
@@ -505,17 +503,16 @@ void stop_all_instances_of_audio_asset(AssetID id) {
return;
}
size_t channel_count = Mix_AllocateChannels(-1);
u64 channel_count = Mix_AllocateChannels(-1);
for (size_t i = 0; i < channel_count; ++i) {
for (u64 i = 0; i < channel_count; ++i) {
if (chunk == Mix_GetChunk(i)) {
Mix_HaltChannel(i);
}
}
}
void fade_out_all_instances_of_audio_asset(AssetID id,
uint32_t fade_length_in_ms) {
void fade_out_all_instances_of_audio_asset(AssetID id, u32 fade_length_in_ms) {
if (id < 0) {
return;
}
@@ -525,16 +522,16 @@ void fade_out_all_instances_of_audio_asset(AssetID id,
return;
}
size_t channel_count = Mix_AllocateChannels(-1);
u64 channel_count = Mix_AllocateChannels(-1);
for (size_t i = 0; i < channel_count; ++i) {
for (u64 i = 0; i < channel_count; ++i) {
if (chunk == Mix_GetChunk(i)) {
Mix_FadeOutChannel(i, fade_length_in_ms);
}
}
}
int8_t get_audio_asset_volume(AssetID id) {
i8 get_audio_asset_volume(AssetID id) {
if (id < 0) {
return -1;
}
@@ -547,7 +544,7 @@ int8_t get_audio_asset_volume(AssetID id) {
return chunk->volume;
}
void set_audio_asset_volume(AssetID id, int8_t volume) {
void set_audio_asset_volume(AssetID id, i8 volume) {
if (id < 0) {
return;
}
@@ -560,8 +557,8 @@ void set_audio_asset_volume(AssetID id, int8_t volume) {
Mix_VolumeChunk(chunk, volume);
}
void play_music_asset(AssetID id, int16_t loops, bool fade_in,
uint32_t fade_length_in_ms) {
void play_music_asset(AssetID id, i16 loops, bool fade_in,
u32 fade_length_in_ms) {
if (id < 0 || is_music_asset_playing(id)) {
return;
}
@@ -617,7 +614,7 @@ void stop_music_asset(AssetID id) {
Mix_HaltMusic();
}
void fade_out_music_asset(AssetID id, uint32_t fade_length_in_ms) {
void fade_out_music_asset(AssetID id, u32 fade_length_in_ms) {
if (id < 0 || !is_music_asset_playing(id)) {
return;
}
@@ -630,7 +627,7 @@ void fade_out_music_asset(AssetID id, uint32_t fade_length_in_ms) {
Mix_FadeOutMusic(fade_length_in_ms);
}
int8_t get_music_asset_volume(AssetID id) {
i8 get_music_asset_volume(AssetID id) {
if (id < 0) {
return -1;
}
@@ -643,7 +640,7 @@ int8_t get_music_asset_volume(AssetID id) {
return Mix_GetMusicVolume(music);
}
void set_music_asset_volume(AssetID id, int8_t volume) {
void set_music_asset_volume(AssetID id, i8 volume) {
if (id < 0 || !is_music_asset_playing(id)) {
return;
}
@@ -693,11 +690,11 @@ void resume_playing_music() { Mix_ResumeMusic(); }
void stop_playing_music() { Mix_HaltMusic(); }
void fade_out_playing_music(uint32_t fade_length_in_ms) {
void fade_out_playing_music(u32 fade_length_in_ms) {
Mix_FadeOutMusic(fade_length_in_ms);
}
void set_playing_music_volume(int8_t volume) { Mix_VolumeMusic(volume); }
void set_playing_music_volume(i8 volume) { Mix_VolumeMusic(volume); }
bool is_music_playing() { return Mix_PlayingMusic(); }
@@ -726,7 +723,7 @@ void update() {
return;
}
uint64_t current_time = SDL_GetTicks64();
u64 current_time = SDL_GetTicks64();
// Get the delta time in seconds since the SDL_GetTicks64() returns the
// time in milliseconds
+7 -6
View File
@@ -1,4 +1,5 @@
#include "ObjectStore.h"
#include "aliases.h"
#include "constants.h"
#include "engine_build_config.h"
#include "reusable_ids.h"
@@ -13,8 +14,8 @@
#endif /* DEBUG_ENGINE */
struct object_store {
size_t count;
size_t deleted_count;
u64 count;
u64 deleted_count;
ObjectID reusable_ids[MAX_OBJ_COUNT];
void *object_ptrs[MAX_OBJ_COUNT];
EventHandlerPtr event_handlers[MAX_OBJ_COUNT];
@@ -45,7 +46,7 @@ bool object_exists(ObjectStore *os, void *object) {
// This is a crude implementation for checking that an object has already
// been added.
// TODO (Abdelrahman): Figure out if there is a better way of doing this
for (size_t i = 0; i < MAX_OBJ_COUNT; ++i) {
for (u64 i = 0; i < MAX_OBJ_COUNT; ++i) {
if (object == os->object_ptrs[i]) {
return true;
}
@@ -68,7 +69,7 @@ ObjectID add_object_to_store(ObjectStore *os, void *object, EventHandlerPtr eh,
ObjectID id;
if (os->deleted_count > 0) {
id = (ObjectID)reuse_id((int32_t *)os->reusable_ids, os->deleted_count);
id = (ObjectID)reuse_id((i32 *)os->reusable_ids, os->deleted_count);
os->deleted_count -= 1;
os->count += 1;
} else {
@@ -115,7 +116,7 @@ void handle_all_objects_events(ObjectStore *os, const SDL_Event *event) {
}
}
void update_all_objects(ObjectStore *os, float delta) {
void update_all_objects(ObjectStore *os, f32 delta) {
if (!os || os->count == 0) {
return;
}
@@ -149,7 +150,7 @@ void destroy_object_store(ObjectStore **os) {
// TODO (Abdelrahman): Figure out if there is a better way to remove all of
// the existing objects without having to loop for the maximum number of
// objects allowed.
for (size_t i = 0; i < MAX_OBJ_COUNT; ++i) {
for (u64 i = 0; i < MAX_OBJ_COUNT; ++i) {
(*os)->object_ptrs[i] = NULL;
(*os)->event_handlers[i] = NULL;
(*os)->update_funcs[i] = NULL;
+6 -5
View File
@@ -6,6 +6,7 @@
#include "SDL_rect.h"
#include "SDL_render.h"
#include "SDL_video.h"
#include "aliases.h"
#include "engine_build_config.h"
#include <stdbool.h>
#include <stdlib.h>
@@ -37,7 +38,7 @@ Renderer *create_renderer(SDL_Window *window) {
return renderer;
}
void get_render_output_size(Renderer *renderer, int32_t *w, int32_t *h) {
void get_render_output_size(Renderer *renderer, i32 *w, i32 *h) {
SDL_GetRendererOutputSize(renderer->SDL_base_renderer, w, h);
}
@@ -57,8 +58,8 @@ void render_clear(const Renderer *renderer, SDL_Color color) {
SDL_RenderClear(renderer->SDL_base_renderer);
}
void draw_rect(const Renderer *renderer, SDL_Color color, float x, float y,
float w, float h) {
void draw_rect(const Renderer *renderer, SDL_Color color, f32 x, f32 y, f32 w,
f32 h) {
SDL_FRect dest = {x, y, w, h};
SDL_SetRenderDrawColor(renderer->SDL_base_renderer, color.r, color.g, color.b,
@@ -66,8 +67,8 @@ void draw_rect(const Renderer *renderer, SDL_Color color, float x, float y,
SDL_RenderDrawRectF(renderer->SDL_base_renderer, &dest);
}
void fill_rect(const Renderer *renderer, SDL_Color color, float x, float y,
float w, float h) {
void fill_rect(const Renderer *renderer, SDL_Color color, f32 x, f32 y, f32 w,
f32 h) {
SDL_FRect dest = {x, y, w, h};
SDL_SetRenderDrawColor(renderer->SDL_base_renderer, color.r, color.g, color.b,
+5 -4
View File
@@ -2,6 +2,7 @@
#include "Renderer.h"
#include "SDL_platform.h"
#include "SDL_video.h"
#include "aliases.h"
#include "engine_build_config.h"
#include "math_utils.h"
#include <stdbool.h>
@@ -94,19 +95,19 @@ bool init_window(Window *wnd, const char *title, WindowSize size,
// Fit the window into the screen when running in full screen mode on
// a non-mobile platform. Mobile platforms will be handled differently
float window_size_ratio =
(float)wnd->defined_size.width / (float)wnd->defined_size.height;
f32 window_size_ratio =
(f32)wnd->defined_size.width / (f32)wnd->defined_size.height;
// Fit the drawing area size to the smallest dimension of the screen and
// make sure the drawing area is centered
if (wnd->real_size.height <= wnd->real_size.width) {
wnd->drawing_area.width =
(int32_t)(window_size_ratio * wnd->real_size.height);
(i32)(window_size_ratio * wnd->real_size.height);
wnd->drawing_area.x =
0.0f + ((wnd->real_size.width - wnd->drawing_area.width) / 2.0f);
} else {
wnd->drawing_area.height =
(int32_t)((float)wnd->real_size.width / window_size_ratio);
(i32)((f32)wnd->real_size.width / window_size_ratio);
wnd->drawing_area.y =
0.0f + ((wnd->real_size.height - wnd->drawing_area.height) / 2.0f);
}
+3 -3
View File
@@ -1,6 +1,6 @@
#include "math_utils.h"
#include <stdint.h>
#include "aliases.h"
int32_t max_int(int32_t a, int32_t b) { return a >= b ? a : b; }
i32 max_int(i32 a, i32 b) { return a >= b ? a : b; }
int32_t min_int(int32_t a, int32_t b) { return a <= b ? a : b; }
i32 min_int(i32 a, i32 b) { return a <= b ? a : b; }
+4 -3
View File
@@ -1,9 +1,10 @@
#include "reusable_ids.h"
#include "aliases.h"
int32_t reuse_id(int32_t *ids, size_t count) {
int32_t id = ids[0];
i32 reuse_id(i32 *ids, u64 count) {
i32 id = ids[0];
for (size_t i = 0; i < count; ++i) {
for (u64 i = 0; i < count; ++i) {
// Remove the first id and make sure the remaining ones are packed
ids[i] = ids[i + 1];
}