diff options
| author | physick <mynameisgennadiy@vk.com> | 2026-08-30 16:14:59 +0500 |
|---|---|---|
| committer | physick <mynameisgennadiy@vk.com> | 2026-08-30 16:14:59 +0500 |
| commit | 1e7ec08dc9c62df43940d00c1fae4a6e54464b86 (patch) | |
| tree | ee4f3170153fd4dc88feb35b13d541e910780711 /src/scenes/collectionScene.c | |
| parent | 935ed0235d0454cd18559fb0f6ee58e0647723fd (diff) | |
puzzle collection rewrite
Diffstat (limited to 'src/scenes/collectionScene.c')
| -rw-r--r-- | src/scenes/collectionScene.c | 68 |
1 files changed, 5 insertions, 63 deletions
diff --git a/src/scenes/collectionScene.c b/src/scenes/collectionScene.c index 8ed545a..2a9fc22 100644 --- a/src/scenes/collectionScene.c +++ b/src/scenes/collectionScene.c @@ -9,10 +9,6 @@ #include "../resources/resources.h" #include "../utils/arena.h" -typedef struct { - char *Name; -} CollectionElement; - static const size_t ARENA_SIZE = 1024 * 64; typedef struct { @@ -20,61 +16,10 @@ typedef struct { char *DirPath; char *Name; - CollectionElement *elements; + CollectionElement *collection; size_t count; } CollectionSceneData; -bool readCollectionFromDir(char *path, CollectionSceneData *data) { - DIR *dir = opendir(path); - if (dir == NULL) { - perror("Failed to open a dir"); - return false; - } - - CollectionElement *outp = malloc(sizeof(CollectionElement)); - if (outp == NULL) { - TraceLog(LOG_ERROR, "Failed to create the buffer in the heap"); - closedir(dir); - return false; - }; - - struct dirent *entry; - size_t cap = 16; - CollectionElement *elements = malloc(sizeof(CollectionElement) * cap); - - while ((entry = readdir(dir)) != NULL) { - // The element is not a file. Also eliminates `.` and `..` - if (entry->d_type != 8) continue; - if (data->count == cap) { - CollectionElement *new = realloc(elements, cap * 2); - if (new == NULL) break; - cap *= 2; - elements = new; - } - CollectionElement *current = &elements[data->count]; - current->Name = ArenaStrdup(&data->arena, entry->d_name); - data->count += 1; - } - - // Moving all of this to the arena - size_t array_size = sizeof(CollectionElement) * data->count; - data->elements = ArenaAlloc(&data->arena, array_size); - - if (data->elements == NULL) { - TraceLog(LOG_ERROR, "The arena failed to allocate the element array"); - // The arena is full - free(elements); - data->count = 0; - closedir(dir); - return false; - } - - memcpy(data->elements, elements, array_size); - free(elements); - closedir(dir); - return true; -} - CollectionSceneData *getData(void *self) { return ((Scene *)self)->data; } @@ -120,7 +65,7 @@ void deinitCollectionScene(void *self) { free((Scene *)self); } -Scene *CreatePuzzleCollectionScene(char *path) { +Scene *CreatePuzzleCollectionScene(char *path, CollectionElement *el) { TraceLog(LOG_INFO, "Creating a collection scene"); Scene *outp = malloc(sizeof(Scene)); if (outp == NULL) return NULL; @@ -138,12 +83,9 @@ Scene *CreatePuzzleCollectionScene(char *path) { ArenaInit(&data->arena, ARENA_SIZE); data->DirPath = ArenaStrdup(&data->arena, path); data->Name = getCollectionName(path); - if (!readCollectionFromDir(path, data)) { - TraceLog(LOG_ERROR, "Failed to read the collection directory"); - } - for (int i = 0; i < data->count; i++) { - TraceLog(LOG_INFO, "Puzzle: %s", data->elements[i].Name); - } + + if (!el->IsInitialized) InitializeCollection(el); + data->collection = el; outp->data = data; |
