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

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