added reading in csv and rending chuncks of spritesheets

This commit is contained in:
herbglitch 2023-01-22 18:45:49 -07:00
parent 551bda6114
commit b024e8d5c1
3 changed files with 141 additions and 0 deletions

105
src/files/config.c Normal file
View file

@ -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 <stdlib.h>
#include <stdint.h>
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);
}

View file

@ -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 <SDL_render.h>
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;
}