Skip to content

Commit

Permalink
C api pass, error_t -> result_t, enum refactor, bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
RandyGaul committed Nov 1, 2022
1 parent 983d9fe commit 16fa587
Show file tree
Hide file tree
Showing 54 changed files with 1,386 additions and 1,317 deletions.
3 changes: 1 addition & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ set(CUTE_SRCS
src/cute_circular_buffer.cpp
src/cute_clipboard.cpp
src/cute_concurrency.cpp
src/cute_error.cpp
src/cute_file_system.cpp
src/cute_handle_table.cpp
src/cute_input.cpp
Expand Down Expand Up @@ -132,7 +131,7 @@ set(CUTE_PUBLIC_HDRS
include/cute_clipboard.h
include/cute_concurrency.h
include/cute_defines.h
include/cute_error.h
include/cute_result.h
include/cute_file_system.h
include/cute_file_system_utils.h
include/cute_handle_table.h
Expand Down
16 changes: 10 additions & 6 deletions include/cute_aabb_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,16 @@
extern "C" {
#endif // __cplusplus

typedef struct cf_leaf_t
{
#ifdef CUTE_CPP
int id = -1;
#else
int id; /*= -1;*/
#endif
} cf_leaf_t;

typedef struct cf_aabb_tree_t cf_aabb_tree_t;
typedef struct cf_leaf_t { int id; /*= -1;*/ } cf_leaf_t;
typedef bool (cf_aabb_tree_query_fn)(cf_leaf_t leaf, cf_aabb_t aabb, void* leaf_udata, void* fn_udata);

CUTE_INLINE cf_leaf_t cf_leaf_defaults()
Expand Down Expand Up @@ -131,15 +139,11 @@ namespace cute
{
using aabb_tree_t = cf_aabb_tree_t;

using leaf_t = cf_leaf_t;
using aabb_tree_query_fn = cf_aabb_tree_query_fn;
using aabb_t = cf_aabb_t;
using ray_t = cf_ray_t;

struct leaf_t : public cf_leaf_t
{
leaf_t() { *(cf_leaf_t*)this = cf_leaf_defaults(); }
};

CUTE_INLINE aabb_tree_t* create_aabb_tree(int initial_capacity = 0, void* user_allocator_context = NULL) { return cf_create_aabb_tree(initial_capacity, user_allocator_context); }
CUTE_INLINE aabb_tree_t* create_aabb_tree_from_memory(const void* buffer, size_t size, void* user_allocator_context = NULL) { return cf_create_aabb_tree_from_memory(buffer, size, user_allocator_context); }
CUTE_INLINE void destroy_aabb_tree(aabb_tree_t* tree) { cf_destroy_aabb_tree(tree); }
Expand Down
79 changes: 54 additions & 25 deletions include/cute_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
#define CUTE_APP_H

#include "cute_defines.h"
#include "cute_error.h"
#include "cute_result.h"

#include "sokol/sokol_gfx.h"

//--------------------------------------------------------------------------------------------------
// C API
Expand All @@ -37,40 +39,53 @@ typedef struct sg_imgui_t sg_imgui_t;
typedef struct sg_image sg_image;
typedef struct cf_strpool_t cf_strpool_t;

#define CUTE_APP_OPTIONS_OPENGL_CONTEXT (1 << 0)
#define CUTE_APP_OPTIONS_OPENGLES_CONTEXT (1 << 1)
#define CUTE_APP_OPTIONS_D3D11_CONTEXT (1 << 2)
#define CUTE_APP_OPTIONS_DEFAULT_GFX_CONTEXT (1 << 3)
#define CUTE_APP_OPTIONS_FULLSCREEN (1 << 4)
#define CUTE_APP_OPTIONS_RESIZABLE (1 << 5)
#define CUTE_APP_OPTIONS_HIDDEN (1 << 6)
#define CUTE_APP_OPTIONS_WINDOW_POS_CENTERED (1 << 7)
#define CUTE_APP_OPTIONS_FILE_SYSTEM_DONT_DEFAULT_MOUNT (1 << 8)

CUTE_API cf_error_t CUTE_CALL cf_app_make(const char* window_title, int x, int y, int w, int h, uint32_t options /*= 0*/, const char* argv0 /*= NULL*/, void* user_allocator_context /*= NULL*/);
#define CF_APP_OPTION_DEFS \
CF_ENUM(APP_OPTIONS_OPENGL_CONTEXT, 1 << 0) \
CF_ENUM(APP_OPTIONS_OPENGLES_CONTEXT, 1 << 1) \
CF_ENUM(APP_OPTIONS_D3D11_CONTEXT, 1 << 2) \
CF_ENUM(APP_OPTIONS_DEFAULT_GFX_CONTEXT, 1 << 3) \
CF_ENUM(APP_OPTIONS_FULLSCREEN, 1 << 4) \
CF_ENUM(APP_OPTIONS_RESIZABLE, 1 << 5) \
CF_ENUM(APP_OPTIONS_HIDDEN, 1 << 6) \
CF_ENUM(APP_OPTIONS_WINDOW_POS_CENTERED, 1 << 7) \
CF_ENUM(APP_OPTIONS_FILE_SYSTEM_DONT_DEFAULT_MOUNT, 1 << 8) \

enum
{
#define CF_ENUM(K, V) CF_##K = V,
CF_APP_OPTION_DEFS
#undef CF_ENUM
};

CUTE_API cf_result_t CUTE_CALL cf_app_make(const char* window_title, int x, int y, int w, int h, int options /*= 0*/, const char* argv0 /*= NULL*/, void* user_allocator_context /*= NULL*/);
CUTE_API void CUTE_CALL cf_app_destroy();

CUTE_API bool CUTE_CALL cf_app_is_running();
CUTE_API void CUTE_CALL cf_app_stop_running();
CUTE_API void CUTE_CALL cf_app_update(float dt);
CUTE_API void CUTE_CALL cf_app_get_offscreen_buffer(sg_image* out_buffer);
CUTE_API sg_image CUTE_CALL cf_app_get_offscreen_buffer();
CUTE_API void CUTE_CALL cf_app_present(bool draw_offscreen_buffer /*= true*/);

CUTE_API cf_error_t CUTE_CALL cf_app_init_audio(bool spawn_mix_thread /*= true*/, int max_simultaneous_sounds /*= 5000*/);
CUTE_API cf_result_t CUTE_CALL cf_app_init_audio(bool spawn_mix_thread /*= true*/, int max_simultaneous_sounds /*= 5000*/);
CUTE_API void CUTE_CALL cf_app_do_mixing();
CUTE_API ImGuiContext* CUTE_CALL cf_app_init_imgui(bool no_default_font /*= false*/);
CUTE_API sg_imgui_t* CUTE_CALL cf_app_get_sokol_imgui();
CUTE_API cf_strpool_t* CUTE_CALL cf_app_get_strpool();

CUTE_API cf_error_t CUTE_CALL cf_app_set_offscreen_buffer(int offscreen_w, int offscreen_h);
CUTE_API cf_result_t CUTE_CALL cf_app_set_offscreen_buffer(int offscreen_w, int offscreen_h);

#define CF_POWER_STATE_DEFS \
CF_ENUM(POWER_STATE_UNKNOWN, 0) /* Cannot determine power status. */ \
CF_ENUM(POWER_STATE_ON_BATTERY, 1) /* Not plugged in and running on battery. */ \
CF_ENUM(POWER_STATE_NO_BATTERY, 2) /* Plugged in with no battery available. */ \
CF_ENUM(POWER_STATE_CHARGING, 3) /* Plugged in and charging battery. */ \
CF_ENUM(POWER_STATE_CHARGED, 4) /* Plugged in and battery is charged. */ \

typedef enum cf_power_state_t
{
CF_POWER_STATE_UNKNOWN, // Cannot determine power status.
CF_POWER_STATE_ON_BATTERY, // Not plugged in and running on battery.
CF_POWER_STATE_NO_BATTERY, // Plugged in with no battery available.
CF_POWER_STATE_CHARGING, // Plugged in and charging battery.
CF_POWER_STATE_CHARGED, // Plugged in and battery is charged.
#define CF_ENUM(K, V) CF_##K = V,
CF_POWER_STATE_DEFS
#undef CF_ENUM
} cf_power_state_t;

typedef struct cf_power_info_t
Expand Down Expand Up @@ -98,22 +113,36 @@ namespace cute
{

using power_info_t = cf_power_info_t;
using power_state_t = cf_power_state_t;

enum power_state_t : int
{
#define CF_ENUM(K, V) K = V,
CF_POWER_STATE_DEFS
#undef CF_ENUM
};

using strpool_t = cf_strpool_t;

CUTE_INLINE error_t app_make(const char* window_title, int x, int y, int w, int h, uint32_t options = 0, const char* argv0 = NULL, void* user_allocator_context = NULL) { return cf_app_make(window_title, x, y, w, h, options, argv0, user_allocator_context); }
enum : int
{
#define CF_ENUM(K, V) K = V,
CF_APP_OPTION_DEFS
#undef CF_ENUM
};

CUTE_INLINE result_t app_make(const char* window_title, int x, int y, int w, int h, uint32_t options = 0, const char* argv0 = NULL, void* user_allocator_context = NULL) { return cf_app_make(window_title, x, y, w, h, options, argv0, user_allocator_context); }
CUTE_INLINE void app_destroy() { cf_app_destroy(); }
CUTE_INLINE bool app_is_running() { return cf_app_is_running(); }
CUTE_INLINE void app_stop_running() { cf_app_stop_running(); }
CUTE_INLINE void app_update(float dt) { cf_app_update(dt); }
CUTE_INLINE void app_get_offscreen_buffer(sg_image* out_buffer) { cf_app_get_offscreen_buffer(out_buffer); }
CUTE_INLINE sg_image app_get_offscreen_buffer() { return cf_app_get_offscreen_buffer(); }
CUTE_INLINE void app_present(bool draw_offscreen_buffer = true) { cf_app_present(draw_offscreen_buffer); }
CUTE_INLINE error_t app_init_audio(bool spawn_mix_thread = true, int max_simultaneous_sounds = 5000) { return cf_app_init_audio(spawn_mix_thread, max_simultaneous_sounds); }
CUTE_INLINE result_t app_init_audio(bool spawn_mix_thread = true, int max_simultaneous_sounds = 5000) { return cf_app_init_audio(spawn_mix_thread, max_simultaneous_sounds); }
CUTE_INLINE void app_do_mixing() { cf_app_do_mixing(); }
CUTE_INLINE ImGuiContext* app_init_imgui(bool no_default_font = false) { return cf_app_init_imgui(no_default_font); }
CUTE_INLINE sg_imgui_t* app_get_sokol_imgui() { return cf_app_get_sokol_imgui(); }
CUTE_INLINE strpool_t* app_get_strpool() { return cf_app_get_strpool(); }
CUTE_INLINE error_t app_set_offscreen_buffer(int offscreen_w, int offscreen_h) { return cf_app_set_offscreen_buffer(offscreen_w, offscreen_h); }
CUTE_INLINE result_t app_set_offscreen_buffer(int offscreen_w, int offscreen_h) { return cf_app_set_offscreen_buffer(offscreen_w, offscreen_h); }
CUTE_INLINE power_info_t app_power_info() { return cf_app_power_info(); }
CUTE_INLINE void sleep(int milliseconds) { cf_sleep(milliseconds); }

Expand Down
10 changes: 5 additions & 5 deletions include/cute_aseprite_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#define CUTE_ASEPRITE_CACHE_H

#include "cute_defines.h"
#include "cute_error.h"
#include "cute_result.h"
#include "cute_batch.h"

#include "cute/cute_aseprite.h"
Expand Down Expand Up @@ -66,7 +66,7 @@ CUTE_API void CUTE_CALL cf_aseprite_cache_destroy(cf_aseprite_cache_t* cache);
* Returns a sprite from the cache. If it does not exist in the cache, it is loaded from disk
* and placed into the cache.
*/
CUTE_API cf_error_t CUTE_CALL cf_aseprite_cache_load(cf_aseprite_cache_t* cache, const char* aseprite_path, cf_sprite_t* sprite);
CUTE_API cf_result_t CUTE_CALL cf_aseprite_cache_load(cf_aseprite_cache_t* cache, const char* aseprite_path, cf_sprite_t* sprite);

/**
* Removes a sprite from the cache. This will cause the next call to `cf_aseprite_cache_load` to fetch from disk.
Expand All @@ -82,7 +82,7 @@ CUTE_API void CUTE_CALL cf_aseprite_cache_unload(cf_aseprite_cache_t* cache, con
*
* Only call this function if you know what you're doing.
*/
CUTE_API cf_error_t CUTE_CALL cf_aseprite_cache_load_ase(cf_aseprite_cache_t* cache, const char* aseprite_path, ase_t** ase);
CUTE_API cf_result_t CUTE_CALL cf_aseprite_cache_load_ase(cf_aseprite_cache_t* cache, const char* aseprite_path, ase_t** ase);

/**
* `cf_png_cache_get_pixels_fn` is needed to hook up to `cf_batch_t` in order to draw sprites.
Expand Down Expand Up @@ -115,9 +115,9 @@ using get_pixels_fn = cf_get_pixels_fn;

CUTE_INLINE aseprite_cache_t* caseprite_cache_make(void* mem_ctx = NULL) { return cf_aseprite_cache_make(mem_ctx); }
CUTE_INLINE void aseprite_cache_destroy(aseprite_cache_t* cache) { cf_aseprite_cache_destroy(cache); }
CUTE_INLINE error_t aseprite_cache_load(aseprite_cache_t* cache, const char* aseprite_path, sprite_t* sprite) { return cf_aseprite_cache_load(cache, aseprite_path, (cf_sprite_t*)sprite); }
CUTE_INLINE result_t aseprite_cache_load(aseprite_cache_t* cache, const char* aseprite_path, sprite_t* sprite) { return cf_aseprite_cache_load(cache, aseprite_path, (cf_sprite_t*)sprite); }
CUTE_INLINE void aseprite_cache_unload(aseprite_cache_t* cache, const char* aseprite_path) { cf_aseprite_cache_unload(cache, aseprite_path); }
CUTE_INLINE error_t aseprite_cache_load_ase(aseprite_cache_t* cache, const char* aseprite_path, ase_t** ase) { return cf_aseprite_cache_load_ase(cache, aseprite_path, ase); }
CUTE_INLINE result_t aseprite_cache_load_ase(aseprite_cache_t* cache, const char* aseprite_path, ase_t** ase) { return cf_aseprite_cache_load_ase(cache, aseprite_path, ase); }
CUTE_INLINE get_pixels_fn* aseprite_cache_get_pixels_fn(aseprite_cache_t* cache) { return cf_aseprite_cache_get_pixels_fn(cache); }
CUTE_INLINE strpool_t* aseprite_cache_get_strpool_ptr(aseprite_cache_t* cache) { return cf_aseprite_cache_get_strpool_ptr(cache); }

Expand Down
26 changes: 13 additions & 13 deletions include/cute_audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

#include "cute_defines.h"
#include "cute_concurrency.h"
#include "cute_error.h"
#include "cute_result.h"

//--------------------------------------------------------------------------------------------------
// C API
Expand All @@ -44,20 +44,20 @@ CUTE_API void CUTE_CALL cf_audio_stream_ogg(const char* path, cf_promise_t promi
CUTE_API void CUTE_CALL cf_audio_stream_wav(const char* path, cf_promise_t promise, void* user_allocator_context /*= NULL*/);
CUTE_API void CUTE_CALL cf_audio_stream_ogg_from_memory(void* memory, int byte_count, cf_promise_t promise, void* user_allocator_context /*= NULL*/);
CUTE_API void CUTE_CALL cf_audio_stream_wav_from_memory(void* memory, int byte_count, cf_promise_t promise, void* user_allocator_context /*= NULL*/);
CUTE_API cf_error_t CUTE_CALL cf_audio_destroy(cf_audio_t* audio);
CUTE_API cf_result_t CUTE_CALL cf_audio_destroy(cf_audio_t* audio);
CUTE_API int CUTE_CALL cf_audio_ref_count(cf_audio_t* audio);

// -------------------------------------------------------------------------------------------------

CUTE_API cf_error_t CUTE_CALL cf_music_play(cf_audio_t* audio_source, float fade_in_time /*= 0*/);
CUTE_API cf_error_t CUTE_CALL cf_music_stop(float fade_out_time /*= 0*/);
CUTE_API cf_result_t CUTE_CALL cf_music_play(cf_audio_t* audio_source, float fade_in_time /*= 0*/);
CUTE_API cf_result_t CUTE_CALL cf_music_stop(float fade_out_time /*= 0*/);
CUTE_API void CUTE_CALL cf_music_set_volume(float volume);
CUTE_API void CUTE_CALL cf_music_set_pitch(float pitch);
CUTE_API void CUTE_CALL cf_music_set_loop(bool true_to_loop);
CUTE_API void CUTE_CALL cf_music_pause(cf_app_t* app);
CUTE_API void CUTE_CALL cf_music_resume(cf_app_t* app);
CUTE_API cf_error_t CUTE_CALL cf_music_switch_to(cf_audio_t* audio_source, float fade_out_time /*= 0*/, float fade_in_time /*= 0*/);
CUTE_API cf_error_t CUTE_CALL cf_music_crossfade(cf_audio_t* audio_source, float cross_fade_time /*= 0*/);
CUTE_API cf_result_t CUTE_CALL cf_music_switch_to(cf_audio_t* audio_source, float fade_out_time /*= 0*/, float fade_in_time /*= 0*/);
CUTE_API cf_result_t CUTE_CALL cf_music_crossfade(cf_audio_t* audio_source, float cross_fade_time /*= 0*/);

// -------------------------------------------------------------------------------------------------

Expand All @@ -74,7 +74,7 @@ typedef struct cf_sound_params_t
typedef struct cf_sound_t { uint64_t id; /*= 0;*/ } cf_sound_t;

CUTE_API cf_sound_params_t CUTE_CALL cf_sound_params_defaults();
CUTE_API cf_sound_t CUTE_CALL cf_sound_play(cf_audio_t* audio_source, cf_sound_params_t params /*= cf_sound_params_defaults()*/, cf_error_t* err /*= NULL*/);
CUTE_API cf_sound_t CUTE_CALL cf_sound_play(cf_audio_t* audio_source, cf_sound_params_t params /*= cf_sound_params_defaults()*/, cf_result_t* err /*= NULL*/);

CUTE_API bool CUTE_CALL cf_sound_is_active(cf_sound_t sound);
CUTE_API bool CUTE_CALL cf_sound_get_is_paused(cf_sound_t sound);
Expand Down Expand Up @@ -129,24 +129,24 @@ CUTE_INLINE void audio_stream_ogg(const char* path, promise_t promise, void* use
CUTE_INLINE void audio_stream_wav(const char* path, promise_t promise, void* user_allocator_context = NULL) { cf_audio_stream_wav(path, promise, user_allocator_context); }
CUTE_INLINE void audio_stream_ogg_from_memory(void* memory, int byte_count, promise_t promise, void* user_allocator_context = NULL) { cf_audio_stream_ogg_from_memory(memory, byte_count, promise, user_allocator_context); }
CUTE_INLINE void audio_stream_wav_from_memory(void* memory, int byte_count, promise_t promise, void* user_allocator_context = NULL) { cf_audio_stream_wav_from_memory(memory, byte_count, promise, user_allocator_context); }
CUTE_INLINE error_t audio_destroy(audio_t* audio) { return cf_audio_destroy(audio); }
CUTE_INLINE result_t audio_destroy(audio_t* audio) { return cf_audio_destroy(audio); }
CUTE_INLINE int audio_ref_count(audio_t* audio) { return cf_audio_ref_count(audio); }

// -------------------------------------------------------------------------------------------------

CUTE_INLINE error_t music_play(audio_t* audio_source, float fade_in_time = 0) { return cf_music_play(audio_source, fade_in_time); }
CUTE_INLINE error_t music_stop(float fade_out_time = 0) { return cf_music_stop(fade_out_time = 0); }
CUTE_INLINE result_t music_play(audio_t* audio_source, float fade_in_time = 0) { return cf_music_play(audio_source, fade_in_time); }
CUTE_INLINE result_t music_stop(float fade_out_time = 0) { return cf_music_stop(fade_out_time = 0); }
CUTE_INLINE void music_set_volume(float volume) { cf_music_set_volume(volume); }
CUTE_INLINE void music_set_pitch(float pitch) { cf_music_set_pitch(pitch); }
CUTE_INLINE void music_set_loop(bool true_to_loop) { cf_music_set_loop(true_to_loop); }
CUTE_INLINE void music_pause(app_t* app) { cf_music_pause(app); }
CUTE_INLINE void music_resume(app_t* app) { cf_music_resume(app); }
CUTE_INLINE error_t music_switch_to(audio_t* audio_source, float fade_out_time = 0, float fade_in_time = 0) { return cf_music_switch_to(audio_source, fade_out_time, fade_in_time); }
CUTE_INLINE error_t music_crossfade(audio_t* audio_source, float cross_fade_time = 0) { return cf_music_crossfade(audio_source, cross_fade_time); }
CUTE_INLINE result_t music_switch_to(audio_t* audio_source, float fade_out_time = 0, float fade_in_time = 0) { return cf_music_switch_to(audio_source, fade_out_time, fade_in_time); }
CUTE_INLINE result_t music_crossfade(audio_t* audio_source, float cross_fade_time = 0) { return cf_music_crossfade(audio_source, cross_fade_time); }

// -------------------------------------------------------------------------------------------------

CUTE_INLINE sound_t sound_play(audio_t* audio_source, sound_params_t params = sound_params_t(), error_t* err = NULL) { return cf_sound_play(audio_source, params, err); }
CUTE_INLINE sound_t sound_play(audio_t* audio_source, sound_params_t params = sound_params_t(), result_t* err = NULL) { return cf_sound_play(audio_source, params, err); }

CUTE_INLINE bool sound_is_active(sound_t sound) { return cf_sound_is_active(sound); }
CUTE_INLINE bool sound_get_is_paused(sound_t sound) { return cf_sound_get_is_paused(sound); }
Expand Down
10 changes: 5 additions & 5 deletions include/cute_base64.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#define CUTE_BASE64_H

#include "cute_defines.h"
#include "cute_error.h"
#include "cute_result.h"

//--------------------------------------------------------------------------------------------------
// C API
Expand All @@ -37,8 +37,8 @@ extern "C" {
#define CUTE_BASE64_ENCODED_SIZE(size) ((((size) + 2) / 3) * 4)
#define CUTE_BASE64_DECODED_SIZE(size) ((((size) + 3) / 4) * 3)

CUTE_API cf_error_t CUTE_CALL cf_base64_encode(void* dst, size_t dst_size, const void* src, size_t src_size);
CUTE_API cf_error_t CUTE_CALL cf_base64_decode(void* dst, size_t dst_size, const void* src, size_t src_size);
CUTE_API cf_result_t CUTE_CALL cf_base64_encode(void* dst, size_t dst_size, const void* src, size_t src_size);
CUTE_API cf_result_t CUTE_CALL cf_base64_decode(void* dst, size_t dst_size, const void* src, size_t src_size);

#ifdef __cplusplus
}
Expand All @@ -52,8 +52,8 @@ CUTE_API cf_error_t CUTE_CALL cf_base64_decode(void* dst, size_t dst_size, const
namespace cute
{

CUTE_INLINE error_t base64_encode(void* dst, size_t dst_size, const void* src, size_t src_size) { return cf_base64_encode(dst, dst_size, src, src_size); }
CUTE_INLINE error_t base64_decode(void* dst, size_t dst_size, const void* src, size_t src_size) { return cf_base64_decode(dst, dst_size, src, src_size); }
CUTE_INLINE result_t base64_encode(void* dst, size_t dst_size, const void* src, size_t src_size) { return cf_base64_encode(dst, dst_size, src, src_size); }
CUTE_INLINE result_t base64_decode(void* dst, size_t dst_size, const void* src, size_t src_size) { return cf_base64_decode(dst, dst_size, src, src_size); }

}

Expand Down

0 comments on commit 16fa587

Please sign in to comment.