#include #include #include #include #include #include #include "scenes.h" #include "../ui_elements/UI.h" #include "../resources/resources.h" #include "../utils/arena.h" static const size_t ARENA_SIZE = 1024 * 64; typedef struct { Arena arena; char *DirPath; char *Name; CollectionElement *collection; size_t count; } CollectionSceneData; CollectionSceneData *getData(void *self) { return ((Scene *)self)->data; } void collectionElement_onSelect(CollectionElement *caller) { if (caller == NULL) return; TraceLog(LOG_INFO, "Selected element %s", caller->name); } void DrawCollectionName(char *name) { Vector2 width = MeasureTextEx(*GetMainFont(), name, 50, 1); int screen_width = GetScreenWidth(); Vector2 pos = { .y = 20, .x = (screen_width - width.x) / 2, }; DrawTextEx(*GetMainFont(), name, pos, HEADER_FONT_SIZE, 1, COLORSCHEME_BROWN); } // Does not allocate the memory char *getCollectionName(char *path) { char *outp = path; size_t len = strlen(path); for (size_t i = 0; i < len; ++i) { if ((path[i] == '/' || path[i] == '\\') && path[i+1] != 0x0) { outp = path + i + 1; } } return outp; } bool updateCollectionScene(void *self) { CollectionSceneData *data = getData(self); if (data->collection != NULL) for (size_t i = 0; i < data->collection->childCount; ++i) { UpdateCollectionElement(&data->collection->children[i]); } return true; } bool drawCollectionScene(void *self) { CollectionSceneData *data = getData(self); DrawBackground(); DrawCollectionName(data->Name); DrawPageView(GetScreenHeight() - 60, 5, 2); for (size_t i = 0; i < data->collection->childCount; ++i) { DrawCollectionElement(&data->collection->children[i], data->collection, (Rectangle) { .x = 10 + 210 * i, .y = 80, .width = 200, .height = 450, }); } return true; } void deinitCollectionScene(void *self) { TraceLog(LOG_INFO, "Deleting a collection scene"); CollectionSceneData *data = getData(self); if (data != NULL) { free(data->arena.mem); free(data); } free((Scene *)self); } Scene *CreatePuzzleCollectionScene(char *path, CollectionElement *el) { TraceLog(LOG_INFO, "Creating a collection scene"); Scene *outp = malloc(sizeof(Scene)); if (outp == NULL) return NULL; outp->Update = updateCollectionScene; outp->Draw = drawCollectionScene; outp->Deint = deinitCollectionScene; outp->IsTransparent = false; CollectionSceneData *data = malloc(sizeof(CollectionSceneData)); if (data == NULL) { free(outp); return NULL; } data->arena = (Arena){0}; ArenaInit(&data->arena, ARENA_SIZE); data->DirPath = ArenaStrdup(&data->arena, path); data->Name = getCollectionName(path); if (!el->IsInitialized) InitializeCollection(el, collectionElement_onSelect); data->collection = el; outp->data = data; return outp; }