From 9bfcd5552ef6f5ff65121b5128cca2a06b5018fd Mon Sep 17 00:00:00 2001 From: herbglitch Date: Wed, 13 Sep 2023 21:32:30 -0600 Subject: [PATCH 01/13] added reaaly basic audio --- CMakeLists.txt | 6 ++++- include/arc/audio/audio.h | 16 +++++++++++++ include/arc/audio/config.h | 22 ++++++++++++++++++ include/arc/audio/sdl/audio.h | 12 ++++++++++ src/audio/sdl/audio.c | 7 ++++++ src/audio/sdl/config.c | 43 +++++++++++++++++++++++++++++++++++ src/engine/engine.c | 6 ++++- src/graphics/sdl/window.c | 2 +- 8 files changed, 111 insertions(+), 3 deletions(-) create mode 100644 include/arc/audio/audio.h create mode 100644 include/arc/audio/config.h create mode 100644 include/arc/audio/sdl/audio.h create mode 100644 src/audio/sdl/audio.c create mode 100644 src/audio/sdl/config.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 6c9b71b..4e13d2d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,6 +35,7 @@ if(ARCHEUS_STD_SDL) find_package(SDL2 REQUIRED) find_package(SDL2_image REQUIRED) find_package(SDL2_ttf REQUIRED) + find_package(SDL2_mixer REQUIRED) string(APPEND ARCHEUS_STD_FLAGS "-DARC_SDL ") endif() @@ -75,6 +76,9 @@ set(ARCHEUS_STD_SOURCES ) set(ARCHEUS_STD_SDL_SOURCES + src/audio/sdl/audio.c + src/audio/sdl/config.c + src/input/sdl/keyboard.c src/input/sdl/mouse.c @@ -130,7 +134,7 @@ if(ARCHEUS_STD_SDL) PRIVATE ${SDL2IMAGE_INCLUDE_DIRS} ) - target_link_libraries(archeus_std PUBLIC ${SDL2_LIBRARIES} SDL2_image::SDL2_image SDL2_ttf::SDL2_ttf) + target_link_libraries(archeus_std PUBLIC ${SDL2_LIBRARIES} SDL2_image::SDL2_image SDL2_ttf::SDL2_ttf SDL2_mixer::SDL2_mixer) endif() if(ARCHEUS_STD_OPENGL) diff --git a/include/arc/audio/audio.h b/include/arc/audio/audio.h new file mode 100644 index 0000000..e342783 --- /dev/null +++ b/include/arc/audio/audio.h @@ -0,0 +1,16 @@ +#ifndef ARC_AUDIO_H_ +#define ARC_AUDIO_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct ARC_Audio ARC_Audio; + +void ARC_Audio_Play(ARC_Audio *audio); + +#ifdef __cplusplus +} +#endif + +#endif // !ARC_AUDIO_H_ diff --git a/include/arc/audio/config.h b/include/arc/audio/config.h new file mode 100644 index 0000000..2eb9750 --- /dev/null +++ b/include/arc/audio/config.h @@ -0,0 +1,22 @@ +#ifndef ARC_AUDIO_CONFIG_H_ +#define ARC_AUDIO_CONFIG_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include "arc/std/string.h" + +typedef struct ARC_Config ARC_Config; +void ARC_AudioConfig_Init(ARC_Config *config); + +uint8_t ARC_Audio_Read(ARC_Config *config, ARC_String *string, void **value); + +void ARC_Audio_Delete(ARC_Config *config, ARC_String *string, void *value); + +#ifdef __cplusplus +} +#endif + +#endif //ARC_AUDIO_CONFIG_H_ \ No newline at end of file diff --git a/include/arc/audio/sdl/audio.h b/include/arc/audio/sdl/audio.h new file mode 100644 index 0000000..92afa3e --- /dev/null +++ b/include/arc/audio/sdl/audio.h @@ -0,0 +1,12 @@ +#ifndef ARC_SDL_AUDIO_H_ +#define ARC_SDL_AUDIO_H_ + +#ifdef ARC_SDL +#include + +typedef struct ARC_Audio { + Mix_Chunk *chunk; +} ARC_Audio; + +#endif // !ARC_SDL +#endif // !ARC_SDL_AUDIO_H_ diff --git a/src/audio/sdl/audio.c b/src/audio/sdl/audio.c new file mode 100644 index 0000000..8709787 --- /dev/null +++ b/src/audio/sdl/audio.c @@ -0,0 +1,7 @@ +#include "arc/audio/audio.h" +#include "arc/audio/sdl/audio.h" +#include + +void ARC_Audio_Play(ARC_Audio *audio){ + Mix_PlayChannel(-1, audio->chunk, 0); +} \ No newline at end of file diff --git a/src/audio/sdl/config.c b/src/audio/sdl/config.c new file mode 100644 index 0000000..141b23d --- /dev/null +++ b/src/audio/sdl/config.c @@ -0,0 +1,43 @@ +#include "arc/audio/config.h" +#include "arc/audio/sdl/audio.h" +#include +#include +#include "arc/std/config.h" +#include "arc/std/errno.h" +#include "arc/audio/audio.h" + +// #define ARC_DEFAULT_CONFIG +#include "arc/std/defaults/config.h" + +void ARC_AudioConfig_Init(ARC_Config *config){ + ARC_Config_AddKeyCString(config, (char *)"ARC_Audio", 9, ARC_Audio_Read, ARC_Audio_Delete); +} + +uint8_t ARC_Audio_Read(ARC_Config *config, ARC_String *string, void **value){ + ARC_Config_Get(config, string, value); + if(*value){ + return 1; + } + + if(string->data[0] != '"' || string->data[string->length - 1] != '"'){ + ARC_DEBUG_LOG(arc_errno, "in ARC_Point_Read(config, string, value); no matching quotes: %s", string->data); + arc_errno = ARC_ERRNO_DATA; + return 0; + } + + ARC_Audio *audio = (ARC_Audio *)malloc(sizeof(ARC_Audio)); + + ARC_String *path; + ARC_String_CopySubstring(&path, string, 1, string->length - 2); + audio->chunk = Mix_LoadWAV(path->data); + + //TODO: get error message if not loaded + + *value = (void *)audio; + return 0; +} + +void ARC_Audio_Delete(ARC_Config* config, ARC_String *string, void *value){ + Mix_FreeChunk(((ARC_Audio *)value)->chunk); + free((ARC_Audio *)value); +} \ No newline at end of file diff --git a/src/engine/engine.c b/src/engine/engine.c index 3b0fa67..5f3ddf3 100644 --- a/src/engine/engine.c +++ b/src/engine/engine.c @@ -15,8 +15,8 @@ #include "arc/input/sdl/mouse.h" #include "arc/input/sdl/keyboard.h" #include -#include #include +#include #elif ARC_OPENGL #include "arc/graphics/opengl/window.h" #include "arc/graphics/opengl/renderer.h" @@ -43,6 +43,8 @@ void ARC_EngineData_Create(ARC_EngineData **data, ARC_Handler_CleanDataFn cleanf //TEMP #ifdef ARC_SDL TTF_Init(); + Mix_Init(0); + Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 2, 1024); #endif #ifdef ARC_SDL @@ -85,6 +87,8 @@ void ARC_EngineData_Create(ARC_EngineData **data, ARC_Handler_CleanDataFn cleanf void ARC_EngineData_Destroy(ARC_EngineData *data){ #ifdef ARC_SDL free(data->mouse->event); + TTF_Quit(); + Mix_Quit(); #endif // ARC_SDL ARC_Mouse_Destroy(data->mouse); diff --git a/src/graphics/sdl/window.c b/src/graphics/sdl/window.c index 73b4785..7921a96 100644 --- a/src/graphics/sdl/window.c +++ b/src/graphics/sdl/window.c @@ -12,7 +12,7 @@ void ARC_Window_Create(ARC_Window **window, ARC_WindowInfo *info){ return; } - if(SDL_Init(SDL_INIT_VIDEO) < 0){ + if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0){ arc_errno = ARC_ERRNO_INIT; printf("Error: initializing SDL\nSDL Error: %s\n", SDL_GetError()); return; From f431d45af5f438ade9ea818036d85b3d5198ecc1 Mon Sep 17 00:00:00 2001 From: herbglitch Date: Thu, 14 Sep 2023 01:34:08 -0600 Subject: [PATCH 02/13] working on ARC_FPoint_Lerp --- CMakeLists.txt | 1 + include/arc/math/point.h | 2 ++ 2 files changed, 3 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4e13d2d..afd87f6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -66,6 +66,7 @@ set(ARCHEUS_STD_SOURCES src/math/circle.c src/math/config.c src/math/obround.c + src/math/point.c src/math/rectangle.c src/math/vector2.c diff --git a/include/arc/math/point.h b/include/arc/math/point.h index addf55d..953c08f 100644 --- a/include/arc/math/point.h +++ b/include/arc/math/point.h @@ -22,6 +22,8 @@ typedef struct ARC_FPoint { float y; } ARC_FPoint; +ARC_FPoint ARC_FPoint_Lerp(ARC_FPoint *start, ARC_FPoint *end, float t); + #ifdef __cplusplus } #endif From 1ed3cc394667612e8045cc05b77c2b766aeb9c69 Mon Sep 17 00:00:00 2001 From: herbglitch Date: Thu, 14 Sep 2023 01:34:29 -0600 Subject: [PATCH 03/13] working on ARC_FPoint_Lerp --- src/math/point.c | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 src/math/point.c diff --git a/src/math/point.c b/src/math/point.c new file mode 100644 index 0000000..7e1767c --- /dev/null +++ b/src/math/point.c @@ -0,0 +1,8 @@ +#include "arc/math/point.h" + +ARC_FPoint ARC_FPoint_Lerp(ARC_FPoint *start, ARC_FPoint *end, float t){ + return (ARC_FPoint){ + (1.0f - t) * start->x + t * end->x, + (1.0f - t) * start->y + t * end->y + }; +} \ No newline at end of file From 267172908a8813651be4d074bd52df4eff345dad Mon Sep 17 00:00:00 2001 From: herbglitch Date: Sun, 17 Sep 2023 19:37:37 -0600 Subject: [PATCH 04/13] added enter key to keybinds --- include/arc/input/keyboard.h | 1 + src/input/sdl/keyboard.c | 1 + 2 files changed, 2 insertions(+) diff --git a/include/arc/input/keyboard.h b/include/arc/input/keyboard.h index 849558c..10e7e5c 100644 --- a/include/arc/input/keyboard.h +++ b/include/arc/input/keyboard.h @@ -64,6 +64,7 @@ typedef enum ARC_KeyboardKey { ARC_KEY_SPACE, ARC_KEY_ESC, + ARC_KEY_ENTER, } ARC_Keyboard_Key; ARC_KeyboardState ARC_Keyboard_GetState(ARC_Keyboard *keyboard, enum ARC_KeyboardKey key); diff --git a/src/input/sdl/keyboard.c b/src/input/sdl/keyboard.c index c120335..a827536 100644 --- a/src/input/sdl/keyboard.c +++ b/src/input/sdl/keyboard.c @@ -92,6 +92,7 @@ ARC_KeyboardState ARC_Keyboard_GetState(ARC_Keyboard *keyboard, enum ARC_Keyboar case ARC_KEY_SPACE: return keyboard->keys[SDLK_SPACE ]; case ARC_KEY_ESC: return keyboard->keys[SDLK_ESCAPE]; + case ARC_KEY_ENTER: return keyboard->keys[SDLK_RETURN]; default: return ARC_KEY_NONE; } From b77b09f22b71d6a226faa12cdf363e0d8d05f1e7 Mon Sep 17 00:00:00 2001 From: herbglitch Date: Wed, 20 Sep 2023 02:24:15 -0600 Subject: [PATCH 05/13] added opacity to sprite --- include/arc/graphics/sdl/sprite.h | 2 ++ include/arc/graphics/sprite.h | 12 ++++++++++++ src/engine/engine.c | 1 - src/graphics/sdl/config.c | 10 ++++------ src/graphics/sdl/renderer.c | 2 ++ src/graphics/sdl/sprite.c | 11 +++++++++++ 6 files changed, 31 insertions(+), 7 deletions(-) diff --git a/include/arc/graphics/sdl/sprite.h b/include/arc/graphics/sdl/sprite.h index eb0adb9..6e7cb59 100644 --- a/include/arc/graphics/sdl/sprite.h +++ b/include/arc/graphics/sdl/sprite.h @@ -9,6 +9,8 @@ struct ARC_Sprite { ARC_Spritesheet *spritesheet; ARC_Array *frames; uint32_t *frameIndex; + //TODO: temp + uint8_t opacity; }; #endif // !ARC_SDL diff --git a/include/arc/graphics/sprite.h b/include/arc/graphics/sprite.h index e339886..ddccceb 100644 --- a/include/arc/graphics/sprite.h +++ b/include/arc/graphics/sprite.h @@ -42,6 +42,18 @@ void ARC_Sprite_Destroy(ARC_Sprite *sprite); */ void ARC_Sprite_Copy(ARC_Sprite **newSprite, ARC_Sprite *oldSprite); +//TODO: temp +/** + * @brief sets ARC_Sprite's opacity + * + * @param sprite ARC_Sprite that is changing opacity + * @param opacity new opacity for ARC_Sprite + * + * @note this is temp because opacity probably should be a value + * bigger than 255 +*/ +void ARC_Sprite_SetOpacity(ARC_Sprite *sprite, uint8_t opacity); + /** * @brief renders ARC_Sprite type * diff --git a/src/engine/engine.c b/src/engine/engine.c index 5f3ddf3..cfb7b90 100644 --- a/src/engine/engine.c +++ b/src/engine/engine.c @@ -104,7 +104,6 @@ void ARC_Engine_Run(ARC_EngineData *data){ } #ifdef ARC_SDL - SDL_SetRenderDrawBlendMode((SDL_Renderer *)data->renderer, SDL_BLENDMODE_BLEND); SDL_Event *event = data->mouse->event; double lastTime = 0, currentTime; diff --git a/src/graphics/sdl/config.c b/src/graphics/sdl/config.c index 22c8f0f..b92c5e7 100644 --- a/src/graphics/sdl/config.c +++ b/src/graphics/sdl/config.c @@ -53,7 +53,10 @@ int32_t ARC_SDL_Texture_Load(const char *path, SDL_Texture **texture){ return 1; // GE_SDL_ERRNO_ } + SDL_BlendMode tempMode; + SDL_GetSurfaceBlendMode(surface, &tempMode); *texture = SDL_CreateTextureFromSurface(global_renderer, surface); + SDL_GetTextureBlendMode(*texture, &tempMode); SDL_FreeSurface(surface); IMG_Quit(); @@ -213,12 +216,7 @@ uint8_t ARC_Sprite_Read(ARC_Config* config, ARC_String *string, void **value){ } } //sprite - *value = malloc(sizeof(ARC_Sprite)); - ((ARC_Sprite *) *value)->frameIndex = malloc(sizeof(uint32_t)); - - ((ARC_Sprite *) *value)->spritesheet = spritesheet; - ((ARC_Sprite *) *value)->frames = frames; - *((ARC_Sprite *) *value)->frameIndex = 0; + ARC_Sprite_Create((ARC_Sprite **)value, spritesheet, frames); return 0; } diff --git a/src/graphics/sdl/renderer.c b/src/graphics/sdl/renderer.c index cfb8de6..9c065c6 100644 --- a/src/graphics/sdl/renderer.c +++ b/src/graphics/sdl/renderer.c @@ -21,6 +21,8 @@ void ARC_Renderer_Create(ARC_Renderer **renderer, ARC_RenderInfo *info){ ARC_DEBUG_LOG(arc_errno, "SDL_CreateRenderer(%p, %d, %u);", info->window, info->index, info->flags); free(renderer); } + + SDL_SetRenderDrawBlendMode((SDL_Renderer *)*renderer, SDL_BLENDMODE_BLEND); } void ARC_Renderer_Destroy(ARC_Renderer *renderer){ diff --git a/src/graphics/sdl/sprite.c b/src/graphics/sdl/sprite.c index 8fa6d4f..d6efabd 100644 --- a/src/graphics/sdl/sprite.c +++ b/src/graphics/sdl/sprite.c @@ -14,6 +14,7 @@ void ARC_Sprite_Create(ARC_Sprite **sprite, ARC_Spritesheet *spritesheet, ARC_Ar (*sprite)->frames = frames; (*sprite)->frameIndex = (uint32_t *)malloc(sizeof(uint32_t)); *(*sprite)->frameIndex = 0; + (*sprite)->opacity = 255; } void ARC_Sprite_Destroy(ARC_Sprite *sprite){ @@ -28,7 +29,13 @@ void ARC_Sprite_Copy(ARC_Sprite **newSprite, ARC_Sprite *oldSprite){ *(*newSprite)->frameIndex = *oldSprite->frameIndex; } +void ARC_Sprite_SetOpacity(ARC_Sprite *sprite, uint8_t opacity){ + sprite->opacity = opacity; +} + void ARC_Sprite_Render(ARC_Sprite *sprite, ARC_Renderer *renderer, ARC_Rect *renderBounds){ + //TODO: note, this is set here so not all entities in the sheet get opacity set + SDL_SetTextureAlphaMod((SDL_Texture *)sprite->spritesheet->texture, sprite->opacity); SDL_RenderCopy((SDL_Renderer *)renderer, sprite->spritesheet->texture, (SDL_Rect *)sprite->frames->data + *sprite->frameIndex, (SDL_Rect *)renderBounds); } @@ -42,10 +49,14 @@ void ARC_Sprite_RenderFlip(ARC_Sprite *sprite, ARC_Renderer *renderer, ARC_Rect flip |= SDL_FLIP_VERTICAL; } + //TODO: note, this is set here so not all entities in the sheet get opacity set + SDL_SetTextureAlphaMod((SDL_Texture *)sprite->spritesheet->texture, sprite->opacity); SDL_RenderCopyEx((SDL_Renderer *)renderer, sprite->spritesheet->texture, (SDL_Rect *)sprite->frames->data + *sprite->frameIndex, (SDL_Rect *)renderBounds, 0.0, NULL, flip); } void ARC_Sprite_RenderRotated(ARC_Sprite *sprite, ARC_Renderer *renderer, ARC_Rect *renderBounds, ARC_Point *center, double angle){ + //TODO: note, this is set here so not all entities in the sheet get opacity set + SDL_SetTextureAlphaMod((SDL_Texture *)sprite->spritesheet->texture, sprite->opacity); SDL_RenderCopyEx((SDL_Renderer *)renderer, sprite->spritesheet->texture, (SDL_Rect *)sprite->frames->data + *sprite->frameIndex, (SDL_Rect *)renderBounds, angle, (SDL_Point *)center, SDL_FLIP_NONE); } From 75f98548db061d9a59d4025e50b9da722b58be2b Mon Sep 17 00:00:00 2001 From: herbglitch Date: Mon, 20 Nov 2023 21:34:46 -0700 Subject: [PATCH 06/13] added function to get sprite index --- include/arc/graphics/sprite.h | 9 +++++++++ src/graphics/sdl/sprite.c | 4 ++++ 2 files changed, 13 insertions(+) diff --git a/include/arc/graphics/sprite.h b/include/arc/graphics/sprite.h index ddccceb..fe71bf9 100644 --- a/include/arc/graphics/sprite.h +++ b/include/arc/graphics/sprite.h @@ -106,6 +106,15 @@ void ARC_Sprite_SetFrameIndex(ARC_Sprite *sprite, uint32_t index); */ void ARC_Sprite_IterateFrame(ARC_Sprite *sprite); +/** + * @brief gets ARC_Sprite's current frame + * + * @param sprite ARC_Sprite to get frame from + * + * @return index ARC_Sprite's current frame index + */ +uint32_t ARC_Sprite_GetFrameIndex(ARC_Sprite *sprite); + /** * @brief returns the current bounds based on the ARC_Sprite's frames * diff --git a/src/graphics/sdl/sprite.c b/src/graphics/sdl/sprite.c index d6efabd..ace0d0f 100644 --- a/src/graphics/sdl/sprite.c +++ b/src/graphics/sdl/sprite.c @@ -77,6 +77,10 @@ void ARC_Sprite_IterateFrame(ARC_Sprite *sprite){ } } +uint32_t ARC_Sprite_GetFrameIndex(ARC_Sprite *sprite){ + return *sprite->frameIndex; +} + ARC_Rect *ARC_Sprite_GetBounds(ARC_Sprite *sprite){ return (ARC_Rect *)sprite->frames->data + *sprite->frameIndex; } From 1354d7c915a9258889b7ba86287c57c33fe972e1 Mon Sep 17 00:00:00 2001 From: herbglitch Date: Sun, 3 Dec 2023 09:25:28 +0000 Subject: [PATCH 07/13] added views and temp circlefill --- CMakeLists.txt | 1 + include/arc/graphics/circle.h | 2 +- src/graphics/sdl/circle.c | 9 +++++++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index afd87f6..aaffd48 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -92,6 +92,7 @@ set(ARCHEUS_STD_SDL_SOURCES src/graphics/sdl/sprite.c src/graphics/sdl/spritesheet.c src/graphics/sdl/text.c + src/graphics/sdl/view.c src/graphics/sdl/window.c ) diff --git a/include/arc/graphics/circle.h b/include/arc/graphics/circle.h index fd35323..11696f1 100644 --- a/include/arc/graphics/circle.h +++ b/include/arc/graphics/circle.h @@ -12,7 +12,7 @@ extern "C" { void ARC_Circle_Render(ARC_Circle *circle, ARC_Renderer *renderer, ARC_Color *color); -// void ARC_Rect_RenderFill(ARC_Rect *rect, ARC_Renderer *renderer, ARC_Color *color); +void ARC_Circle_RenderFill(ARC_Circle *circle, ARC_Renderer *renderer, ARC_Color *color); #ifdef __cplusplus } diff --git a/src/graphics/sdl/circle.c b/src/graphics/sdl/circle.c index cf53a70..2472404 100644 --- a/src/graphics/sdl/circle.c +++ b/src/graphics/sdl/circle.c @@ -40,4 +40,13 @@ void ARC_Circle_Render(ARC_Circle *circle, ARC_Renderer *renderer, ARC_Color *co } } +//TODO: very temp +void ARC_Circle_RenderFill(ARC_Circle *circle, ARC_Renderer *renderer, ARC_Color *color){ + ARC_Circle temp = *circle; + + for(; temp.r; temp.r--){ + ARC_Circle_Render(&temp, renderer, color); + } +} + #endif // ARC_SDL \ No newline at end of file From 5750185bd8a38ce2a450f47e3bd8d5315b455561 Mon Sep 17 00:00:00 2001 From: herbglitch Date: Sun, 3 Dec 2023 09:25:34 +0000 Subject: [PATCH 08/13] added views and temp circlefill --- include/arc/graphics/view.h | 59 +++++++++++++++++++++++++++++++++++++ src/graphics/sdl/view.c | 35 ++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 include/arc/graphics/view.h create mode 100644 src/graphics/sdl/view.c diff --git a/include/arc/graphics/view.h b/include/arc/graphics/view.h new file mode 100644 index 0000000..594a30a --- /dev/null +++ b/include/arc/graphics/view.h @@ -0,0 +1,59 @@ +#ifndef ARC_GRAPHICS_VIEW_H_ +#define ARC_GRAPHICS_VIEW_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "arc/graphics/renderer.h" +#include "arc/math/rectangle.h" + +typedef struct ARC_View { + ARC_Renderer *renderer; + ARC_Rect bounds; +} ARC_View; + +/** + * @brief a function for ARC_View where contents of the function will be rendered within a view + * + * @param data data to be used within ARC_View_RenderFn + */ +typedef void (* ARC_View_RenderFn)(void *data); + +/** + * @brief creates ARC_View type + * + * @param view ARC_View to initialize + * @param renderer ARC_Renderer the view will render to + * @param bounds ARC_Rect bounds of the view within the renderer + */ +void ARC_View_Create(ARC_View **view, ARC_Renderer *renderer, ARC_Rect bounds); + +/** + * @brief destroys ARC_View type + */ +void ARC_View_Destroy(ARC_View *view); + +/** + * @brief renders callbacks contents within an ARC_View + * + * @param view ARC_View to be renedered to + * @param renderFn function which contents will render to given ARC_View + * @param data data to be used in renderFn + */ +void ARC_View_Render(ARC_View *view, ARC_View_RenderFn renderFn, void *data); + +/** + * @brief gets bounds from ARC_View type + * + * @param view ARC_View to get bounds from + * + * @return bounds of the view + */ +ARC_Rect ARC_View_GetBounds(ARC_View *view); + +#ifdef __cplusplus +} +#endif + +#endif // !ARC_GRAPHICS_VIEW_H_ diff --git a/src/graphics/sdl/view.c b/src/graphics/sdl/view.c new file mode 100644 index 0000000..ffa9483 --- /dev/null +++ b/src/graphics/sdl/view.c @@ -0,0 +1,35 @@ +#include "arc/graphics/view.h" + +#include "arc/std/errno.h" +#include +#include + +void ARC_View_Create(ARC_View **view, ARC_Renderer *renderer, ARC_Rect bounds){ + *view = (ARC_View *)malloc(sizeof(ARC_View)); + (*view)->renderer = renderer; + (*view)->bounds = bounds; +} + +void ARC_View_Destroy(ARC_View *view){ + free(view); +} + +void ARC_View_Render(ARC_View *view, ARC_View_RenderFn renderFn, void *data){ + int err = SDL_RenderSetViewport((SDL_Renderer *)view->renderer, (const SDL_Rect *)&(view->bounds)); + if(err){ + ARC_DEBUG_LOG(ARC_ERRNO_DATA, "in src/graphics/sdl/view.c ARC_View_Render(view, renderFn), SDL_RenderSetViewport(...) returned: %d", err); + return; + } + + renderFn(data); + + err = SDL_RenderSetViewport((SDL_Renderer *)view->renderer, NULL); + if(err){ + ARC_DEBUG_LOG(ARC_ERRNO_DATA, "in src/graphics/sdl/view.c ARC_View_Render(view, NULL), SDL_RenderSetViewport(...) returned: %d", err); + return; + } +} + +ARC_Rect ARC_View_GetBounds(ARC_View *view){ + return view->bounds; +} From eaef5a584ba3d873fcaf1a9847a74138236617e0 Mon Sep 17 00:00:00 2001 From: herbglitch Date: Fri, 12 Jan 2024 20:02:35 -0700 Subject: [PATCH 09/13] apprently merge wasn't saved in cmakelists.txt, so this should fix that --- CMakeLists.txt | 72 -------------------------------------------------- 1 file changed, 72 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c65ff60..a748f21 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -31,46 +31,7 @@ set_property(CACHE ARCHEUS_STD_GRAPHICS_BACKEND PROPERTY STRINGS NONE SDL2 OPENG set(ARCHEUS_STD_FLAGS "") set(ARCHEUS_STD_LIBRARIES "") -<<<<<<< HEAD # ~ ARCHEUS_SOURCES ~ # -======= -if(ARCHEUS_STD_DEBUG) - string(APPEND ARCHEUS_STD_FLAGS "-Wall -Werror -g -ggdb -DARC_DEBUG ") -endif() - -if(ARCHEUS_STD_DEFAULT_CONFIG) - string(APPEND ARCHEUS_STD_FLAGS "-DARC_DEFAULT_CONFIG ") -endif() - -if(ARCHEUS_STD_SDL) - if(NOT PNG AND WIN32 AND NOT MSVC) - set(PNG_LIBRARY "C:/Program Files(x86)/libpng") - set(PNG_PNG_INCLUDE_DIR "C:/Program Files(x86)/libpng/include") - endif() - - find_package(SDL2 REQUIRED) - find_package(SDL2_image REQUIRED) - find_package(SDL2_ttf REQUIRED) - find_package(SDL2_mixer REQUIRED) - - string(APPEND ARCHEUS_STD_FLAGS "-DARC_SDL ") -endif() - -if(ARCHEUS_STD_GLFW) - string(APPEND ARCHEUS_STD_FLAGS "-DARC_GLFW ") -endif() - -if(ARCHEUS_STD_GLEW) - string(APPEND ARCHEUS_STD_FLAGS "-DARC_GLEW ") -endif() - -if(ARCHEUS_STD_OPENGL) - string(APPEND ARCHEUS_STD_FLAGS "-DARC_OPENGL") -endif() - -set(CMAKE_C_FLAGS ${ARCHEUS_STD_FLAGS}) - ->>>>>>> 5750185bd8a38ce2a450f47e3bd8d5315b455561 set(ARCHEUS_STD_SOURCES src/std/config.c src/std/errno.c @@ -94,7 +55,6 @@ set(ARCHEUS_STD_SOURCES src/engine/state.c ) -<<<<<<< HEAD if(CMAKE_BUILD_TYPE STREQUAL "Debug") string(APPEND ARCHEUS_STD_FLAGS "-Wall -Werror -g -ggdb -DARC_DEBUG ") endif() @@ -126,27 +86,6 @@ if(ARCHEUS_STD_GLEW) endif() set(CMAKE_C_FLAGS ${ARCHEUS_STD_FLAGS}) -======= -set(ARCHEUS_STD_SDL_SOURCES - src/audio/sdl/audio.c - src/audio/sdl/config.c - - src/input/sdl/keyboard.c - src/input/sdl/mouse.c - - src/graphics/sdl/circle.c - src/graphics/sdl/config.c - src/graphics/sdl/line.c - src/graphics/sdl/obround.c - src/graphics/sdl/rectangle.c - src/graphics/sdl/renderer.c - src/graphics/sdl/sprite.c - src/graphics/sdl/spritesheet.c - src/graphics/sdl/text.c - src/graphics/sdl/view.c - src/graphics/sdl/window.c -) ->>>>>>> 5750185bd8a38ce2a450f47e3bd8d5315b455561 set(ARCHEUS_STD_OPENGL_SOURCES src/graphics/opengl/config.c @@ -181,20 +120,9 @@ target_include_directories(archeus_std PUBLIC $ ) -<<<<<<< HEAD target_link_libraries(archeus_std PUBLIC m ) -======= - target_link_libraries(archeus_std PUBLIC ${SDL2_LIBRARIES} SDL2_image::SDL2_image SDL2_ttf::SDL2_ttf SDL2_mixer::SDL2_mixer) -endif() - -if(ARCHEUS_STD_OPENGL) - target_include_directories(archeus_std PRIVATE - PUBLIC $ - ) -endif() ->>>>>>> 5750185bd8a38ce2a450f47e3bd8d5315b455561 install(TARGETS archeus_std EXPORT archeus_std_Exports LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} From d64340525a2c0b5a4976a1a6de9608273d1a31c3 Mon Sep 17 00:00:00 2001 From: herbglitch Date: Mon, 15 Jan 2024 02:14:35 -0700 Subject: [PATCH 10/13] added a few functions to string and added some logs to string --- include/arc/std/string.h | 12 ++++++++++++ src/std/string.c | 15 +++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/include/arc/std/string.h b/include/arc/std/string.h index 1a604c9..91c9b40 100644 --- a/include/arc/std/string.h +++ b/include/arc/std/string.h @@ -87,6 +87,18 @@ uint8_t ARC_String_Equals(ARC_String *first, ARC_String *second); */ uint8_t ARC_String_EqualsCString(ARC_String *string, const char *cstring, uint64_t length); +/** + * @brief check if ARC_String and cstring match + * + * @param string ARC_string to check + * @param offset postion based on string to start comparing against cstring + * @param cstring cstring to check + * @param length length of cstring + * + * @return 1 if match, 0 if they don't match +*/ +uint8_t ARC_String_SubstringEqualsCString(ARC_String *string, uint64_t offset, const char *cstring, uint64_t length); + /** * @brief checks if string is alphabetic * diff --git a/src/std/string.c b/src/std/string.c index fe29aa5..8759fa7 100644 --- a/src/std/string.c +++ b/src/std/string.c @@ -100,6 +100,18 @@ uint8_t ARC_String_EqualsCString(ARC_String *string, const char *cstring, uint64 return 1; } +uint8_t ARC_String_SubstringEqualsCString(ARC_String *string, uint64_t offset, const char *cstring, uint64_t length){ + if(string->length - offset < length){ + return 0; + } + + if(strncmp(string->data + offset, cstring, length)){ + return 0; + } + + return 1; +} + uint8_t ARC_String_Alpha(ARC_String *string){ for(uint64_t length = string->length; length; length--){ if(string->data[length - 1] >= 'a' && string->data[length - 1] <= 'z'){ @@ -131,6 +143,7 @@ double ARC_String_ToDouble(ARC_String *string){ uint64_t ARC_String_Find(ARC_String *string, ARC_String *substring){ if(!string || !substring){ + ARC_DEBUG_ERR("ARC_String_Find(string, substring), string or substring was null"); arc_errno = ARC_ERRNO_NULL; return ~(uint64_t)0; } @@ -152,6 +165,7 @@ uint64_t ARC_String_Find(ARC_String *string, ARC_String *substring){ uint64_t ARC_String_FindCString(ARC_String *string, const char *cstring, uint64_t length){ if(!string || !cstring){ arc_errno = ARC_ERRNO_NULL; + ARC_DEBUG_ERR("ARC_String_FindCString(string, cstring, length), string or cstring was null"); return ~(uint64_t)0; } @@ -172,6 +186,7 @@ uint64_t ARC_String_FindCString(ARC_String *string, const char *cstring, uint64_ uint64_t ARC_String_FindBack(ARC_String *string, ARC_String *substring){ if(!string || !substring){ arc_errno = ARC_ERRNO_NULL; + ARC_DEBUG_ERR("ARC_String_FindBack(string, substring), string or substring was null"); return ~(uint64_t)0; } From af9a1f1040e60a663e175a3b36d752913941b973 Mon Sep 17 00:00:00 2001 From: herbglitch Date: Tue, 16 Jan 2024 23:58:31 -0700 Subject: [PATCH 11/13] fixed string class so parameters that can be stored to will allways be the first parameter, might have broken everything, need to test --- include/arc/std/string.h | 16 ++++++++-------- src/math/config.c | 12 ++++++------ src/std/config.c | 14 +++++++------- src/std/defaults/config.c | 4 ++-- src/std/string.c | 10 +++++----- 5 files changed, 28 insertions(+), 28 deletions(-) diff --git a/include/arc/std/string.h b/include/arc/std/string.h index 91c9b40..bc7b02d 100644 --- a/include/arc/std/string.h +++ b/include/arc/std/string.h @@ -169,39 +169,39 @@ uint64_t ARC_String_FindBack(ARC_String *string, ARC_String *substring); /** * @brief strips the ends based on a given char * - * @param original the string which whill have the matching char stripped from * @param stripped where to store the string which has witespace stripped * will be null if there is an error + * @param original the string which whill have the matching char stripped from * @param charToStrip the char that will be stripped from the ends */ -void ARC_String_StripEnds(ARC_String *original, ARC_String **stripped, char charToStrip); +void ARC_String_StripEnds(ARC_String **stripped, ARC_String *original, char charToStrip); /** * @brief strips whitespace from a ARC_String * - * @param original the string which whill have whitespace stripped from * @param stripped where to store the string which has witespace stripped * will be null if there is an error + * @param original the string which whill have whitespace stripped from */ -void ARC_String_StripWhitespace(ARC_String *original, ARC_String **stripped); +void ARC_String_StripWhitespace(ARC_String **stripped, ARC_String *original); /** * @brief strips the whitespace from the ends of a string * - * @param original the string which whill have the whitespace stripped from its ends * @param stripped where to store the string which has witespace stripped from the ends * will be null if there is an error + * @param original the string which whill have the whitespace stripped from its ends */ -void ARC_String_StripEndsWhitespace(ARC_String *original, ARC_String **stripped); +void ARC_String_StripEndsWhitespace(ARC_String **stripped, ARC_String *original); /** * @brief merges two strings together * + * @param combined new ARC_String of combined strings frist + second * @param first first part of string to combine * @param second second part of string to combine - * @param combined new ARC_String of combined strings frist + second */ -void ARC_String_Merge(ARC_String *first, ARC_String *second, ARC_String **combined); +void ARC_String_Merge(ARC_String **combined, ARC_String *first, ARC_String *second); /** * @brief copy a subtring from a givin ARC_String diff --git a/src/math/config.c b/src/math/config.c index 11c0214..2e48868 100644 --- a/src/math/config.c +++ b/src/math/config.c @@ -85,7 +85,7 @@ uint8_t ARC_Rect_Read(ARC_Config *config, ARC_String *string, void **value){ } ARC_String_CopySubstring(&temp, current, 0, separator - 1); - ARC_String_StripEndsWhitespace(temp, &tempStripped); + ARC_String_StripEndsWhitespace(&tempStripped, temp); x = ARC_String_ToInt64_t(tempStripped); ARC_String_Destroy(temp); ARC_String_Destroy(tempStripped); @@ -101,7 +101,7 @@ uint8_t ARC_Rect_Read(ARC_Config *config, ARC_String *string, void **value){ } ARC_String_CopySubstring(&temp, current, 0, separator - 1); - ARC_String_StripEndsWhitespace(temp, &tempStripped); + ARC_String_StripEndsWhitespace(&tempStripped, temp); y = ARC_String_ToInt64_t(tempStripped); ARC_String_Destroy(temp); ARC_String_Destroy(tempStripped); @@ -117,7 +117,7 @@ uint8_t ARC_Rect_Read(ARC_Config *config, ARC_String *string, void **value){ } ARC_String_CopySubstring(&temp, current, 0, separator - 1); - ARC_String_StripEndsWhitespace(temp, &tempStripped); + ARC_String_StripEndsWhitespace(&tempStripped, temp); w = ARC_String_ToInt64_t(tempStripped); ARC_String_Destroy(temp); ARC_String_Destroy(tempStripped); @@ -133,7 +133,7 @@ uint8_t ARC_Rect_Read(ARC_Config *config, ARC_String *string, void **value){ } ARC_String_CopySubstring(&temp, current, 0, separator); - ARC_String_StripEndsWhitespace(temp, &tempStripped); + ARC_String_StripEndsWhitespace(&tempStripped, temp); h = ARC_String_ToInt64_t(tempStripped); ARC_String_Destroy(temp); ARC_String_Destroy(tempStripped); @@ -150,7 +150,7 @@ uint8_t ARC_Rect_Read(ARC_Config *config, ARC_String *string, void **value){ void ARC_RectArray_ReadRect(ARC_Config* config, ARC_String *stripped, uint64_t index, uint64_t length, uint64_t *arrayIndex, void **value){ ARC_String *substr, *temp; ARC_String_CopySubstring(&temp, stripped, index, length); - ARC_String_StripEndsWhitespace(temp, &substr); + ARC_String_StripEndsWhitespace(&substr, temp); ARC_String_Destroy(temp); // reading in reference @@ -194,7 +194,7 @@ uint8_t ARC_RectArray_Read(ARC_Config* config, ARC_String *string, void **value) ARC_String *temp, *stripped; ARC_String_CopySubstring(&temp, string, 1, string->length - 2); - ARC_String_StripEndsWhitespace(temp, &stripped); + ARC_String_StripEndsWhitespace(&stripped, temp); ARC_String_Destroy(temp); uint64_t arraySize = 1; diff --git a/src/std/config.c b/src/std/config.c index b8c91c7..78b294a 100644 --- a/src/std/config.c +++ b/src/std/config.c @@ -195,7 +195,7 @@ void ARC_Config_SetKeyGroup(ARC_Config *config, ARC_String **data, uint8_t *comm ARC_String *name, *temp; ARC_String_CopySubstring(&temp, *data, index, nextIndex - index - 1); - ARC_String_StripEndsWhitespace(temp, &name); + ARC_String_StripEndsWhitespace(&name, temp); ARC_String_Destroy(temp); temp = *data; @@ -258,11 +258,11 @@ void ARC_Config_GetNameAndValue(ARC_String *data, ARC_String **name, ARC_String index++; ARC_String *dataTemp = *name; - ARC_String_StripEndsWhitespace(dataTemp, name); + ARC_String_StripEndsWhitespace(name, dataTemp); ARC_String_Destroy(dataTemp); ARC_String_CopySubstring(&dataTemp, data, index, data->length - index); - ARC_String_StripEndsWhitespace(dataTemp, value); + ARC_String_StripEndsWhitespace(value, dataTemp); ARC_String_Destroy(dataTemp); } @@ -276,7 +276,7 @@ void ARC_Config_Recurse(ARC_Config *config, ARC_String **data, ARC_String *group while(*data && (*data)->length){ ARC_String *dataTemp = *data; - ARC_String_StripEndsWhitespace(dataTemp, data); + ARC_String_StripEndsWhitespace(data, dataTemp); ARC_String_Destroy(dataTemp); // break out of current group @@ -302,7 +302,7 @@ void ARC_Config_Recurse(ARC_Config *config, ARC_String **data, ARC_String *group ARC_String *keyType, *keyTypeTemp; ARC_String_CopySubstring(&keyTypeTemp, *data, 0, index); - ARC_String_StripEndsWhitespace(keyTypeTemp, &keyType); + ARC_String_StripEndsWhitespace(&keyType, keyTypeTemp); ARC_String_Destroy(keyTypeTemp); if(ARC_String_EqualsCString(keyType, "group", 5)){ @@ -468,7 +468,7 @@ void ARC_Config_RunCommand(ARC_Config *config, ARC_String *command){ ARC_String *commandArgTemp, *commandArg; ARC_String_CopySubstring(&commandArgTemp, command, index + space->length, command->length - (index + space->length)); - ARC_String_StripWhitespace(commandArgTemp, &commandArg); + ARC_String_StripWhitespace(&commandArg, commandArgTemp); ARC_String_Destroy(commandArgTemp); if(ARC_String_EqualsCString(command, "load", 4)){ @@ -548,7 +548,7 @@ void ARC_Config_FileIO(ARC_Config *config, ARC_String *path, uint8_t command){ ARC_String_Destroy(temp); temp = data; - ARC_String_StripEndsWhitespace(temp, &data); + ARC_String_StripEndsWhitespace(&data, temp); ARC_String_Destroy(temp); ARC_Config_Recurse(config, &data, NULL, &command); diff --git a/src/std/defaults/config.c b/src/std/defaults/config.c index 844f988..f6304b6 100644 --- a/src/std/defaults/config.c +++ b/src/std/defaults/config.c @@ -196,7 +196,7 @@ uint8_t ARC_ConfigKey_Read_String(ARC_Config* config, ARC_String *string, void * void ARC_ConfigKey_StringArray_ReadString(ARC_Config* config, ARC_String *stripped, uint64_t index, uint64_t length, uint64_t *arrayIndex, void **value){ ARC_String *substr, *temp; ARC_String_CopySubstring(&temp, stripped, index, length); - ARC_String_StripEndsWhitespace(temp, &substr); + ARC_String_StripEndsWhitespace(&substr, temp); ARC_String_Destroy(temp); // reading in reference @@ -239,7 +239,7 @@ uint8_t ARC_ConfigKey_Read_StringArray(ARC_Config* config, ARC_String *string, v ARC_String *temp, *stripped; ARC_String_CopySubstring(&temp, string, 1, string->length - 2); - ARC_String_StripEndsWhitespace(temp, &stripped); + ARC_String_StripEndsWhitespace(&stripped, temp); ARC_String_Destroy(temp); uint64_t arraySize = 1; diff --git a/src/std/string.c b/src/std/string.c index 8759fa7..bee9680 100644 --- a/src/std/string.c +++ b/src/std/string.c @@ -204,7 +204,7 @@ uint64_t ARC_String_FindBack(ARC_String *string, ARC_String *substring){ return ~(uint64_t)0; } -void ARC_String_StripEnds(ARC_String *original, ARC_String **stripped, char charToStrip){ +void ARC_String_StripEnds(ARC_String **stripped, ARC_String *original, char charToStrip){ if(!original){ arc_errno = ARC_ERRNO_NULL; *stripped = NULL; @@ -247,7 +247,7 @@ void ARC_String_StripEnds(ARC_String *original, ARC_String **stripped, char char ARC_String_Create(stripped, original->data + start, length); } -void ARC_String_StripWhitespace(ARC_String *original, ARC_String **stripped){ +void ARC_String_StripWhitespace(ARC_String **stripped, ARC_String *original){ if(!original){ arc_errno = ARC_ERRNO_NULL; *stripped = NULL; @@ -318,7 +318,7 @@ void ARC_String_StripWhitespace(ARC_String *original, ARC_String **stripped){ ARC_String_Create(stripped, data, length); } -void ARC_String_StripEndsWhitespace(ARC_String *original, ARC_String **stripped){ +void ARC_String_StripEndsWhitespace(ARC_String **stripped, ARC_String *original){ uint64_t index; for(uint64_t i = 0; i < original->length; i++){ if(original->data[i] == ' '){ @@ -366,7 +366,7 @@ void ARC_String_StripEndsWhitespace(ARC_String *original, ARC_String **stripped) ARC_String_CopySubstring(stripped, original, index, endIndex - index); } -void ARC_String_Merge(ARC_String *first, ARC_String *second, ARC_String **combined){ +void ARC_String_Merge(ARC_String **combined, ARC_String *first, ARC_String *second){ char data[first->length + second->length]; for(uint32_t i = 0; i < first->length; i++){ data[i] = first->data[i]; @@ -400,7 +400,7 @@ void ARC_String_RemoveSection(ARC_String **newString, ARC_String *original, uint ARC_String_CopySubstring(&first , original, 0 , removeIndex ); ARC_String_CopySubstring(&second, original, removeIndex + removeLength, original->length - (removeIndex + removeLength)); - ARC_String_Merge(first, second, newString); + ARC_String_Merge(newString, first, second); ARC_String_Destroy(first ); ARC_String_Destroy(second); From 7dc58b60a67b0f0109b106302218d5912b843642 Mon Sep 17 00:00:00 2001 From: herbglitch Date: Tue, 23 Jan 2024 21:16:43 -0700 Subject: [PATCH 12/13] write with io --- include/arc/std/io.h | 8 ++++++++ src/std/io.c | 21 +++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/include/arc/std/io.h b/include/arc/std/io.h index cc5a1ed..59bb217 100644 --- a/include/arc/std/io.h +++ b/include/arc/std/io.h @@ -17,6 +17,14 @@ extern "C" { */ void ARC_IO_FileToStr(ARC_String *path, ARC_String **data); +/** + * @brief write string to file + * + * @param path a string to path of target file + * @param data data to be written +*/ +void ARC_IO_WriteStrToFile(ARC_String *path, ARC_String *data); + #ifdef __cplusplus } #endif diff --git a/src/std/io.c b/src/std/io.c index 6d219a2..c400f3f 100644 --- a/src/std/io.c +++ b/src/std/io.c @@ -9,6 +9,7 @@ void ARC_IO_FileToStr(ARC_String *path, ARC_String **data){ FILE *file = fopen(path->data, "rb"); if(!file){ arc_errno = ARC_ERRNO_NULL; + ARC_DEBUG_LOG(arc_errno, "ARC_IO_FileToStr(ARC_String *path, ARC_String **data), could not open file \"%s\"", path->data); return; } @@ -20,6 +21,7 @@ void ARC_IO_FileToStr(ARC_String *path, ARC_String **data){ if(fileData == NULL){ fclose(file); arc_errno = ARC_ERRNO_NULL; + ARC_DEBUG_ERR("ARC_IO_FileToStr(ARC_String *path, ARC_String **data), file data is NULL"); *data = NULL; return; } @@ -27,6 +29,7 @@ void ARC_IO_FileToStr(ARC_String *path, ARC_String **data){ if(1 != fread(fileData, length, 1, file)){ fclose(file); arc_errno = ARC_ERRNO_COPY; + ARC_DEBUG_ERR("ARC_IO_FileToStr(ARC_String *path, ARC_String **data), could not copy file data"); *data = NULL; return; } @@ -35,3 +38,21 @@ void ARC_IO_FileToStr(ARC_String *path, ARC_String **data){ ARC_String_Create(data, fileData, length); free(fileData); } + +void ARC_IO_WriteStrToFile(ARC_String *path, ARC_String *data){ + FILE *file = fopen(path->data, "wb"); + if(!file){ + arc_errno = ARC_ERRNO_NULL; + ARC_DEBUG_LOG(arc_errno, "ARC_IO_WriteStrToFile(ARC_String *path, ARC_String *data), could not open file \"%s\"", path->data); + return; + } + + if(1 != fwrite(data->data, data->length, 1, file)){ + fclose(file); + arc_errno = ARC_ERRNO_COPY; + ARC_DEBUG_ERR("ARC_IO_WriteStrToFile(ARC_String *path, ARC_String **data), could not write file data"); + return; + } + + fclose(file); +} \ No newline at end of file From 7b9bdf0f92eac5f0effa96c9f0488de96860a98d Mon Sep 17 00:00:00 2001 From: herbglitch Date: Sat, 27 Jan 2024 03:14:04 -0700 Subject: [PATCH 13/13] read in bytes --- include/arc/std/io.h | 10 ++++++++++ src/std/io.c | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/include/arc/std/io.h b/include/arc/std/io.h index 59bb217..9a79e5d 100644 --- a/include/arc/std/io.h +++ b/include/arc/std/io.h @@ -8,6 +8,16 @@ extern "C" { #include #include "arc/std/string.h" +/** + * @brief get string and size from file + * + * @param path a string to path of target file + * @param data pointer to where uint8_t array will be created + * this will need to be freed once done using it + * @param length length of the data read in +*/ +void ARC_IO_ReadFileToUint8t(ARC_String *path, uint8_t **data, uint64_t *length); + /** * @brief get string and size from file * diff --git a/src/std/io.c b/src/std/io.c index c400f3f..7779852 100644 --- a/src/std/io.c +++ b/src/std/io.c @@ -5,6 +5,41 @@ #include #include +void ARC_IO_ReadFileToUint8t(ARC_String *path, uint8_t **data, uint64_t *length){ + FILE *file = fopen(path->data, "rb"); + if(!file){ + arc_errno = ARC_ERRNO_NULL; + ARC_DEBUG_LOG(arc_errno, "ARC_IO_FileToStr(ARC_String *path, ARC_String **data), could not open file \"%s\"", path->data); + *length = 0; + *data = NULL; + return; + } + + fseek(file, 0L, SEEK_END); + *length = ftell(file); + rewind(file); + + *data = (uint8_t *) calloc(1, *length + 1); + if(*data == NULL){ + fclose(file); + arc_errno = ARC_ERRNO_NULL; + ARC_DEBUG_ERR("ARC_IO_FileToStr(ARC_String *path, ARC_String **data), file data is NULL"); + *length = 0; + return; + } + + if(1 != fread(*data, *length, 1, file)){ + fclose(file); + arc_errno = ARC_ERRNO_COPY; + ARC_DEBUG_ERR("ARC_IO_FileToStr(ARC_String *path, ARC_String **data), could not copy file data"); + *length = 0; + *data = NULL; + return; + } + + fclose(file); +} + void ARC_IO_FileToStr(ARC_String *path, ARC_String **data){ FILE *file = fopen(path->data, "rb"); if(!file){