array.h, still needs refactor of object and objectarray

This commit is contained in:
2025-04-12 19:39:21 +02:00
parent 0fd536673f
commit 77f2c38204
4 changed files with 138 additions and 22 deletions
+40 -16
View File
@@ -1,30 +1,54 @@
#ifndef FLOWERS_H
#define FLOWERS_H
#include <object.h>
#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
+7 -5
View File
@@ -7,6 +7,8 @@
#include <postfx.h>
#include <soundtrack.h>
#include <asset.h>
#include <array.h>
#include <stdlib.h>
#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