diff --git a/.vscode/settings.json b/.vscode/settings.json index 721a7ec..3cd8cb3 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -2,6 +2,8 @@ "files.associations": { "asset.h": "c", "stdlib.h": "c", - "view.h": "c" + "view.h": "c", + "__node_handle": "c", + "font.h": "c" } } \ No newline at end of file diff --git a/external/engine/array.h b/external/engine/array.h new file mode 100644 index 0000000..6886b5f --- /dev/null +++ b/external/engine/array.h @@ -0,0 +1,88 @@ +#ifndef ARRAY_H +#define ARRAY_H + +typedef struct { + void** elements; + unsigned int count; + unsigned int capacity; +} Array; + +Array* CreateArray(unsigned int initCapacity){ + Array* array = malloc(sizeof(Array)); + if (!array) return NULL; + + array->elements = malloc(sizeof(void* ) * initCapacity); + if (!array->elements) { + free(array); + return NULL; + } + + array->count = 0; + array->capacity = initCapacity; + return array; +} + +int AddElementToArray(Array* array, void* element){ + if(array->count >= array->capacity) { + unsigned int newCapacity = array->capacity * 2; + void** newElements = realloc(array->elements, sizeof(void*) * newCapacity); + if (!newElements) return 0; + + array->elements = newElements; + array->capacity = newCapacity; + } + array->elements[array->count++] = element; + return 1; +} + +// NORMAL REMOVE +// int RemoveElementFromArray(Array* array, unsigned int index){ +// if (index >= array->count) return 0; + +// for (unsigned int i = index; i < array->count - 1; i++) { +// array->elements[i] = array->elements[i + 1]; +// } + +// array->count--; +// return 1; +// } + +// SWAP AND POP REMOVE +int RemoveElementFromArray(Array* array, unsigned int index) { + if (index >= array->count) return 0; + + array->elements[index] = array->elements[array->count - 1]; + + array->count--; + + return 1; +} + +void* GetRandomElementFromArray(Array* array){ + if (array == NULL || array->elements == NULL || array->count == 0) { + return NULL; + } + int randomIndex = GetRandomValue(0, (int)array->count-1); + return &array->elements[randomIndex]; +} + +void RenderArray(Array* array, void (*renderFunc)(void*)) +{ + if (!array || !renderFunc) return; + for (unsigned int i = 0; i < array->count; i++) { + renderFunc(array->elements[i]); + } +} + +void FreeArray(Array** array) { + if (!array || !*array) return; + + free((*array)->elements); + (*array)->elements = NULL; + + free(*array); + *array = NULL; +} + + +#endif // ARRAY_H diff --git a/include/flowers.h b/include/flowers.h index d4e3ecf..9fe4905 100644 --- a/include/flowers.h +++ b/include/flowers.h @@ -1,30 +1,54 @@ #ifndef FLOWERS_H #define FLOWERS_H -#include +#include "array.h" -ObjectArray CreateFlowers(Texture2D texture, float frameWidth, float rotation, float fieldSize) +typedef enum { + CHILD, + TEEN, + ADULT +} AGE; + +typedef struct { + Sprite sprite; + Vector2 position; + AGE age; +} Flower; + +Array* CreateFlowers(Texture2D texture, float frameWidth, float rotation, float fieldSize) { - ObjectArray flowers = CreateObjectArray(&(Object){ - .sprite = { - .texture = texture, - .sourceRec = {0.0f, 0.0f, frameWidth, frameWidth}, - .destRec = {0.0f, 0.0f, frameWidth, frameWidth}, - .origin = {frameWidth / 2, frameWidth / 2}, - .rotation = rotation, - .color = WHITE}, - .position = {0.0f, 0.0f}}, - 10); + Array* flowers = CreateArray(10); - if (!flowers.data) - { + if(!flowers) { printf("Memory allocation failed.\n"); exit(1); } - SetRandomPositionForObjectArray(&flowers, (Rectangle){-fieldSize, -fieldSize, fieldSize * 2, fieldSize * 2}); - + for (int i = 0; i < 10; i++) { + Flower* flower = malloc(sizeof(Flower)); + *flower = (Flower){ + .sprite = { + .texture = texture, + .sourceRec = {0.0f, 0.0f, frameWidth, frameWidth}, + .destRec = {(float)GetRandomValue(-fieldSize,fieldSize), (float)GetRandomValue(-fieldSize,fieldSize), frameWidth, frameWidth}, + .origin = {frameWidth / 2, frameWidth / 2}, + .rotation = rotation, + .color = WHITE, + }, + .age = CHILD + }; + AddElementToArray(flowers, flower); + } return flowers; } +void RenderFlower(void* element) +{ + Flower* f = (Flower*)element; + if (!f) return; + RenderSprite(&(f->sprite)); +} + + + #endif // FLOWERS_H diff --git a/include/game.h b/include/game.h index 897f4cc..69949cc 100644 --- a/include/game.h +++ b/include/game.h @@ -7,6 +7,8 @@ #include #include #include +#include +#include #include "gameassets.h" #include "floor.h" @@ -24,7 +26,7 @@ typedef struct RenderTexture2D target; Floor floor; Object hive; - ObjectArray flowers; + Array* flowers; Player player; Bee bee1; Camera2D camera; @@ -65,7 +67,7 @@ void InitGame(GameState *g, Window *window) // Animated Entities g->player = CreatePlayer(IDLE_DOWN, HOE, g->textures[PLAYER], g->assets.textureAssets[PLAYER].frameWidth, 4, 2, g->hive.position, g->assets.textureAssets[PLAYER].rotation, 50.0f); - g->bee1 = CreateBee(FLYING, g->textures[BEE], g->assets.textureAssets[BEE].frameWidth, 4, 10, g->hive.position, 10.0f, &g->flowers, &g->hive); + // g->bee1 = CreateBee(FLYING, g->textures[BEE], g->assets.textureAssets[BEE].frameWidth, 4, 10, g->hive.position, 10.0f, &g->flowers, &g->hive); // I/O g->camera = CreateCamera(window->width, window->height, g->player.physics.position, 10.0f); @@ -100,14 +102,14 @@ void UpdateGame(GameState *g, View *currentView, bool *running) UpdateCamera2D(&g->camera, &g->player.physics.position, &g->mouse); UpdateFloor(&g->floor, &g->camera); UpdatePlayer(&g->player); - UpdateBee(&g->bee1, &g->flowers, &g->hive); + // UpdateBee(&g->bee1, &g->flowers, &g->hive); } void RenderComponents(GameState *g) { RenderFloor(&g->floor); RenderSprite(&g->hive.sprite); - RenderObjectArray(&g->flowers); + RenderArray(g->flowers, RenderFlower); RenderSprite(&g->player.sprite); RenderSprite(&g->bee1.sprite); } @@ -155,7 +157,7 @@ void CleanupGame(GameState *g) UnloadMusicStream(g->soundtracks[i].music); } UnloadRenderTexture(g->target); - free(g->flowers.data); + FreeArray(&g->flowers); } #endif // GAME_H