diff --git a/include/arc/files/config.h b/include/arc/files/config.h new file mode 100644 index 0000000..f1f4d2d --- /dev/null +++ b/include/arc/files/config.h @@ -0,0 +1,17 @@ +#ifndef ARC_FILES_CONFIG_H_ +#define ARC_FILES_CONFIG_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "arc/std/config.h" +#include "arc/graphics/renderer.h" + +void ARC_FilesConfig_Init(ARC_Config *config); + +#ifdef __cplusplus +} +#endif + +#endif // !ARC_FILES_CONFIG_H_ \ No newline at end of file diff --git a/src/files/config.c b/src/files/config.c new file mode 100644 index 0000000..033f5a9 --- /dev/null +++ b/src/files/config.c @@ -0,0 +1,105 @@ +#include "arc/files/config.h" +#include "arc/std/io.h" +#include "arc/std/errno.h" +#include "arc/std/string.h" +#include "arc/std/array.h" +#include +#include + +uint8_t ARC_CSV_Read(ARC_Config *config, ARC_String *string, void **value); + +void ARC_CSV_Delete(ARC_Config *config, ARC_String *string, void *value); + +void ARC_FilesConfig_Init(ARC_Config *config){ + ARC_Config_AddKeyCString(config, (char *)"CSV", 3, ARC_CSV_Read, ARC_CSV_Delete); +} + +uint8_t ARC_CSV_Read(ARC_Config *config, ARC_String *string, void **value){ + ARC_Config_Get(config, string, value); + if(*value){ + return 1; + } + + ARC_String *fileData; + ARC_IO_FileToStr(string, &fileData); + if(arc_errno){ + ARC_DEBUG_LOG(arc_errno, "ARC_CSV_Read(config, string, value) could not read in csv file: \"%s\"", string->data); + ARC_String_Destroy(fileData); + return 0; + } + + uint32_t height = 0; + for(uint32_t i = 0; i < fileData->length; i++){ + if(fileData->data[i] == '\n'){ + height++; + } + } + + *value = malloc(sizeof(ARC_Array)); + ((ARC_Array *)*value)->data = malloc(sizeof(ARC_Array *) * height); + ((ARC_Array *)*value)->size = malloc(sizeof(uint32_t) ); + *((ARC_Array *)*value)->size = height; + + uint32_t index = 0; + for(uint32_t y = 0; y < height; y++){ + uint32_t width = 0; + uint32_t data = 0; + uint32_t x = 0; + + for(uint32_t i = index; i < fileData->length; i++){ + if(fileData->data[i] == ','){ + width++; + } + + if(fileData->data[i] == '\n'){ + width++; + break; + } + } + + if(!width){ + arc_errno = ARC_ERRNO_DATA; + ARC_DEBUG_LOG(arc_errno, "ARC_CSV_Read(config, string, value) no width of line %d", y); + ARC_String_Destroy(fileData); + return 0; + } + + ((ARC_Array **)((ARC_Array *)*value)->data)[y] = (ARC_Array *) malloc(sizeof(ARC_Array)); + ((ARC_Array **)((ARC_Array *)*value)->data)[y]->data = malloc(sizeof(int32_t ) * width); + ((ARC_Array **)((ARC_Array *)*value)->data)[y]->size = malloc(sizeof(uint32_t) ); + *((ARC_Array **)((ARC_Array *)*value)->data)[y]->size = width; + + for(uint32_t i = index; i < fileData->length; i++){ + if(fileData->data[i] != ',' && fileData->data[i] != '\n'){ + continue; + } + ARC_String *indexValueString; + ARC_String_CopySubstring(&indexValueString, fileData, index, i - index); + index = i + 1; + + ((int32_t *)((ARC_Array **)((ARC_Array *)*value)->data)[y]->data)[x] = (int32_t)ARC_String_ToInt64_t(indexValueString); + ARC_String_Destroy(indexValueString); + x++; + + if(fileData->data[i] == '\n'){ + index = i + 1; + break; + } + } + } + + return 0; +} + +void ARC_CSV_Delete(ARC_Config *config, ARC_String *string, void *value){ + ARC_Array *valueArray = value; + for(uint32_t i = 0; i < *valueArray->size; i++){ + free((int32_t *)((ARC_Array **)valueArray->data)[i]->data); + free((uint32_t *)((ARC_Array **)valueArray->data)[i]->size); + free((ARC_Array *)((ARC_Array **)valueArray->data)[i]); + } + + free((ARC_Array **)valueArray->data); + free((uint32_t *)valueArray->size); + free(valueArray); +} \ No newline at end of file diff --git a/src/graphics/sdl/spritesheet.c b/src/graphics/sdl/spritesheet.c new file mode 100644 index 0000000..a109b46 --- /dev/null +++ b/src/graphics/sdl/spritesheet.c @@ -0,0 +1,19 @@ +#include "arc/graphics/spritesheet.h" +#include "arc/graphics/sdl/spritesheet.h" +#include "arc/graphics/sdl/renderer.h" +#include "arc/math/point.h" +#include + +void ARC_Spritesheet_RenderArea(ARC_Spritesheet *spritesheet, ARC_Rect *sheetBounds, ARC_Renderer *renderer, ARC_Rect *renderBounds){ + SDL_RenderCopy(renderer->renderer, spritesheet->texture, (SDL_Rect *)sheetBounds, (SDL_Rect *)renderBounds); +} + +ARC_Point ARC_Spritesheet_GetSize(ARC_Spritesheet *spritesheet){ + ARC_Point size; + SDL_QueryTexture(spritesheet->texture, NULL, NULL, &size.x, &size.y); + return size; +} + +uint32_t *ARC_Spritesheet_GetTileSize(ARC_Spritesheet *spritesheet){ + return spritesheet->size; +} \ No newline at end of file