summaryrefslogtreecommitdiff
path: root/src/scenes
diff options
context:
space:
mode:
Diffstat (limited to 'src/scenes')
-rw-r--r--src/scenes/collectionScene.c68
-rw-r--r--src/scenes/scenes.h2
2 files changed, 6 insertions, 64 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;
diff --git a/src/scenes/scenes.h b/src/scenes/scenes.h
index 828f2f3..659dcfd 100644
--- a/src/scenes/scenes.h
+++ b/src/scenes/scenes.h
@@ -33,6 +33,6 @@ Scene *CreatePuzzleScene(Puzzle *puzzle);
Scene *CreateDistractionScene(DistractionPuzzle *puzzle, void (*addTime)(Scene *, size_t));
Scene *CreateResultView(SubmitionResult *result);
-Scene *CreatePuzzleCollectionScene(char *path);
+Scene *CreatePuzzleCollectionScene(char *path, CollectionElement *el);
#endif // SCENES_H