big refactor: i see i need header files

This commit is contained in:
huaqo-games
2025-11-02 11:23:26 +01:00
parent 297a835780
commit c1a2448126
38 changed files with 582 additions and 560 deletions
-29
View File
@@ -1,29 +0,0 @@
#ifndef ANIMATION_H
#define ANIMATION_H
typedef struct {
int state;
int currentFrame;
int maxFrame;
int framesCounter;
int framesSpeed;
float animTimer;
} Animation;
void UpdateAnimation(Animation *animation, float deltaTime) {
animation->animTimer += deltaTime;
float frameTime = 1.0f / animation->framesSpeed;
if (animation->animTimer < frameTime) {
return;
}
animation->animTimer -= frameTime;
animation->currentFrame++;
if (animation->currentFrame >= animation->maxFrame) {
animation->currentFrame = 0;
}
}
#endif // ANIMATION_H
-96
View File
@@ -1,96 +0,0 @@
#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 UpdateArray(Array* array, void (*updateFunc)(void*))
{
if (!array || !updateFunc) return;
for (unsigned int i = 0; i < array->count; i++) {
updateFunc(array->elements[i]);
}
}
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
-30
View File
@@ -1,30 +0,0 @@
#ifndef ASSET_H
#define ASSET_H
typedef struct {
const char* path;
float frameWidth;
float frameHeight;
float rotation;
} TextureAsset;
typedef struct {
const char* path;
} ShaderAsset;
typedef struct {
const char* path;
} SoundtrackAsset;
typedef struct {
const char* path;
} SoundAsset;
typedef struct {
const TextureAsset *textureAssets;
const ShaderAsset *shaderAssets;
const SoundtrackAsset *soundtrackAssets;
const SoundAsset *soundAssets;
} Assets;
#endif // ASSET_H
-33
View File
@@ -1,33 +0,0 @@
#ifndef CAMERA_H
#define CAMERA_H
#include "input.h"
#include <raymath.h>
Camera2D CreateCamera(Vector2 playerPosition, float initZoom){
int windowWidth = GetScreenWidth();
int windowHeight = GetScreenHeight();
return (Camera2D){
.offset = (Vector2){windowWidth / 2.0f, windowHeight / 2.0f},
.target = playerPosition,
.rotation = 0.0f,
.zoom = initZoom
};
}
void UpdateCameraTarget(Camera2D *camera, Vector2 *position, const float smoothness){
camera->target = Vector2Lerp(camera->target, *position, smoothness * GetFrameTime());
}
void UpdateCameraZoom(Camera2D *camera, const float *scaleFactor, const float *minZoom, const float *maxZoom){
float targetZoom = Clamp(camera->zoom * *scaleFactor, *minZoom, *maxZoom);
camera->zoom = Lerp(camera->zoom, targetZoom, 0.1f);
}
void UpdateCamera2D(Camera2D* camera, Vector2* position, Mouse* mouse){
UpdateCameraTarget(camera, position, 5.0f);
UpdateCameraZoom(camera, &mouse->scaleFactor, &mouse->minZoom, &mouse->maxZoom);
}
#endif // CAMERA_H
-66
View File
@@ -1,66 +0,0 @@
#ifndef CONFIG_H
#define CONFIG_H
#define RINI_IMPLEMENTATION
#define RINI_VALUE_DELIMITER '='
#include <rini.h>
#include <stdio.h>
typedef struct {
char *path;
rini_config rini;
} Config;
void InitConfig(Config *config, const char* path) {
config->rini = rini_load_config(path);
if (config->rini.count == 0) {
printf("Warning: Loaded config has no entries. Path: %s\n", config->path);
}
}
int GetConfigInt(Config *config, const char *key) {
return rini_get_config_value_fallback(config->rini, key, 0);
}
int GetConfigIntOrDefault(Config *config, const char *key, int default_value) {
return rini_get_config_value_fallback(config->rini, key, default_value);
}
const char* GetConfigString(Config *config, const char *key) {
return rini_get_config_value_text(config->rini, key);
}
const char* GetConfigStringOrDefault(Config *config, const char *key, const char *default_value) {
return rini_get_config_value_text_fallback(config->rini, key, default_value);
}
const char* GetConfigDescription(Config *config, const char *key) {
return rini_get_config_value_description(config->rini, key);
}
int SetConfigInt(Config *config, const char *key, int value, const char *desc) {
return rini_set_config_value(&config->rini, key, value, desc);
}
int SetConfigString(Config *config, const char *key, const char *text, const char *desc) {
return rini_set_config_value_text(&config->rini, key, text, desc);
}
int SetConfigDescription(Config *config, const char *key, const char *desc) {
return rini_set_config_value_description(&config->rini, key, desc);
}
int AddCommentLine(Config *config, const char *comment) {
return rini_set_config_comment_line(&config->rini, comment);
}
void SaveConfig(Config *config) {
rini_save_config(config->rini, config->path);
}
void CleanUpConfig(Config *config) {
rini_unload_config(&config->rini);
}
#endif // CONFIG_H
-21
View File
@@ -1,21 +0,0 @@
#ifndef ENGINE_H
#define ENGINE_H
#include "animation.h"
//#include "array.h"
#include "asset.h"
#include "camera.h"
#include "config.h"
#include "gamegui.h"
#include "gui.h"
#include "input.h"
#include "object.h"
#include "physics.h"
#include "postfx.h"
#include "soundtrack.h"
#include "sprite.h"
#include "vector.h"
#include "window.h"
#include "font.h"
#endif //ENGINE_H
-13
View File
@@ -1,13 +0,0 @@
#ifndef FONT_H
#define FONT_H
void InitFont(Font *font, const char* path){
*font = LoadFont(path);
SetTextureFilter(font->texture, TEXTURE_FILTER_POINT);
}
void CleanupFont(Font *font){
UnloadFont(*font);
}
#endif //FONT_H
-99
View File
@@ -1,99 +0,0 @@
#ifndef GAMEGUI_H
#define GAMEGUI_H
#include "sprite.h"
#include <raylib.h>
typedef enum {
NOT_CLICKED,
HOVER,
CLICKED
} ButtonState;
typedef struct
{
Sprite sprite;
Vector2 notClicked;
Vector2 hover;
Vector2 clicked;
bool isHovered;
bool isClicked;
} ImageButton;
typedef struct
{
Sprite sprite;
} ImageElement;
ImageButton CreateImageButton(ButtonState state, Texture2D *texture, const TextureAsset *asset, Vector2 *source, Vector2 *clicked, Vector2 *hover, Rectangle *destRec){
return (ImageButton){
.sprite = {
.texture = *texture,
.sourceRec = (Rectangle){ source->x, source->y, asset->frameWidth, asset->frameHeight},
.destRec = *destRec,
.frameSize = (Vector2){asset->frameWidth, asset->frameHeight},
.origin = (Vector2){0.0f, 0.0f},
.rotation = 0.0f,
.color = WHITE
},
.notClicked = (Vector2){source->x, source->y},
.hover = *hover,
.clicked = *clicked
};
}
ImageElement CreateImageElement(Texture2D *texture, const TextureAsset *asset, Vector2 *source, Rectangle *destRec){
return (ImageElement){
.sprite = {
.texture = *texture,
.sourceRec = (Rectangle){ source->x, source->y, asset->frameWidth, asset->frameHeight},
.destRec = *destRec,
.frameSize = (Vector2){asset->frameWidth, asset->frameHeight},
.origin = (Vector2){0.0f, 0.0f},
.rotation = 0.0f,
.color = WHITE
}
};
}
int isImageButtonClicked(ImageButton *b, Sound soundHover, Sound soundClick){
if (CheckCollisionPointRec(GetMousePosition(), b->sprite.destRec)) {
if (!b->isHovered) {
PlaySound(soundHover);
b->isHovered = true;
UpdateSpriteSourceRec(&b->sprite, &b->hover);
}
if (IsMouseButtonDown(MOUSE_LEFT_BUTTON)){
if (!b->isClicked) {
PlaySound(soundClick);
b->isClicked = true;
UpdateSpriteSourceRec(&b->sprite, &b->clicked);
}
}
if (IsMouseButtonReleased(MOUSE_LEFT_BUTTON))
{
b->isClicked = false;
b->isHovered = false;
UpdateSpriteSourceRec(&b->sprite, &b->notClicked);
return true;
}
} else {
UpdateSpriteSourceRec(&b->sprite, &b->notClicked);
b->isClicked = false;
b->isHovered = false;
}
return false;
}
void renderImageButton(ImageButton *b) {
RenderSprite(&b->sprite);
}
void renderImageElement(ImageElement *e){
RenderSprite(&e->sprite);
}
#endif // GAMEGUI_H
-32
View File
@@ -1,32 +0,0 @@
#ifndef GUI_H
#define GUI_H
#define RAYGUI_IMPLEMENTATION
#include <raygui.h>
typedef struct
{
Rectangle rec;
char *text;
} Button;
Button initButton(char *text, Vector2 dimensions, Vector2 position)
{
Button b;
b.rec = (Rectangle){position.x - dimensions.x / 2, position.y - dimensions.y / 2, dimensions.x, dimensions.y};
b.text = text;
return b;
}
int isButtonClicked(Button *b)
{
return (CheckCollisionPointRec(GetMousePosition(), b->rec) && IsMouseButtonPressed(MOUSE_LEFT_BUTTON));
}
void renderButton(Button *b)
{
GuiButton(b->rec, b->text);
}
#endif // GUI_H
-61
View File
@@ -1,61 +0,0 @@
#ifndef INPUT_H
#define INPUT_H
#include <raymath.h>
typedef struct {
Vector2 screenPosition;
Vector2 worldPosition;
float zoomSpeed;
float minZoom;
float maxZoom;
float scaleFactor;
float wheelMove;
bool leftButton;
bool rightButton;
Texture2D cursorTexture;
} Mouse;
Mouse CreateMouse(float zoomSpeed, float minZoom, float maxZoom, Texture2D *texture){
HideCursor();
return (Mouse){
.zoomSpeed = zoomSpeed,
.minZoom = minZoom,
.maxZoom = maxZoom,
.cursorTexture = *texture
};
}
void UpdateMouseScreen(Mouse *mouse){
mouse->screenPosition = GetMousePosition();
}
void UpdateMouse(Mouse *mouse, Camera2D *camera) {
Vector2 pos = GetMousePosition();
mouse->screenPosition = pos;
mouse->worldPosition = GetScreenToWorld2D(pos, *camera);
mouse->leftButton = IsMouseButtonDown(MOUSE_LEFT_BUTTON);
mouse->rightButton = IsMouseButtonDown(MOUSE_RIGHT_BUTTON);
mouse->wheelMove = GetMouseWheelMove();
if (mouse->wheelMove != 0) {
float zoomFactor = 1.0f + (mouse->zoomSpeed * fabsf(mouse->wheelMove));
mouse->scaleFactor = (mouse->wheelMove < 0) ? (1.0f / zoomFactor) : zoomFactor;
} else {
mouse->scaleFactor = 1.0f;
}
}
Vector2 GetDirectionVector(void){
Vector2 vector = {.x = 0.0f, .y = 0.0f};
if (IsKeyDown(KEY_RIGHT) || IsKeyDown(KEY_D)){vector.x += 1.0f; }
if (IsKeyDown(KEY_LEFT) || IsKeyDown(KEY_A)){ vector.x -= 1.0f; }
if (IsKeyDown(KEY_UP) || IsKeyDown(KEY_W)){ vector.y -= 1.0f; }
if (IsKeyDown(KEY_DOWN) || IsKeyDown(KEY_S)){ vector.y += 1.0f; }
if (Vector2Length(vector) > 0.0f) {vector = Vector2Normalize(vector); }
return vector;
}
#endif //INPUT_H
-129
View File
@@ -1,129 +0,0 @@
#ifndef OBJECT_H
#define OBJECT_H
#include "sprite.h"
#include "vector.h"
typedef struct {
Sprite sprite;
Vector2 position;
} Object;
typedef struct {
Object *data;
const Object *prototype;
size_t count;
} ObjectArray;
ObjectArray CreateObjectArray(const Object *prototype, const size_t count) {
ObjectArray objects = {0};
if (count == 0) {
printf("Objects size must be greater than 0.\n");
return objects;
}
objects.count = count;
objects.data = (Object* )malloc(count * sizeof(Object));
if (!objects.data) {
printf("Memory allocation of objects failed.\n");
return objects;
}
objects.prototype = prototype;
for (int i = 0; i < count; i++) {
objects.data[i].position = prototype->position;
objects.data[i].sprite = prototype->sprite;
}
return objects;
}
Object* GetRandomObject(ObjectArray *objects){
if (objects->count == 0 || objects->data == NULL) {
return NULL;
}
return &objects->data[GetRandomValue(0, (int)objects->count-1)];
}
void AddObjectToArray(ObjectArray *objects, const Object *prototype, Vector2 position) {
Object *newData = (Object* )realloc(objects->data, (objects->count + 1) * sizeof(Object));
if (newData) {
objects->data = newData;
objects->data[objects->count] = *prototype;
objects->data[objects->count].position = position;
objects->data[objects->count].sprite.destRec.x = position.x;
objects->data[objects->count].sprite.destRec.y = position.y;
objects->count++;
} else {
printf("Memory allocation failed while adding an object.\n");
}
}
void SetPositionForObjectArray(ObjectArray *objects, const Vector2Array *positions) {
if (positions->count != objects->count) {
printf("Warning: Position array size (%zu) does not match ObjectArray size (%zu)\n", positions->count, objects->count);
return;
}
for (size_t i = 0; i < objects->count; i++) {
objects->data[i].position = positions->data[i];
objects->data[i].sprite.destRec.x = positions->data[i].x;
objects->data[i].sprite.destRec.y = positions->data[i].y;
}
}
void SetRandomPositionForObjectArray(ObjectArray *objects, const Rectangle bounds){
for (int i = 0; i < objects->count; i++) {
Vector2 position = GetRandomVector(bounds);
objects->data[i].position = position;
objects->data[i].sprite.destRec.x = position.x;
objects->data[i].sprite.destRec.y = position.y;
}
}
void SwapAndPopObjectsArrayOnRecCollision(ObjectArray *objects, const Rectangle rec){
for (int i = 0; i < objects->count; i++){
if (CheckCollisionRecs(rec, objects->data[i].sprite.destRec)){
if (objects->count == 1) {
objects->count = 0;
break;
}
objects->data[i] = objects->data[objects->count - 1];
objects->count--;
i--;
}
}
objects->data = (Object*)realloc(objects->data, objects->count * sizeof(Object));
}
void SwapAndPopObjectsArrayOnPointCollision(ObjectArray *objects, const Vector2 point){
for (int i = 0; i < objects->count; i++){
if (CheckCollisionPointRec(point, objects->data[i].sprite.destRec)){
if (objects->count == 1) {
objects->count = 0;
break;
}
objects->data[i] = objects->data[objects->count - 1];
objects->count--;
i--;
}
}
objects->data = (Object*)realloc(objects->data, objects->count * sizeof(Object));
}
Vector2 GetDirectionToObject(Object *object, Vector2 position){
return Vector2Normalize(Vector2Subtract(object->position, position));
}
void RenderObjectArray(const ObjectArray *objects){
for (int i = 0; i < objects->count; i++){
RenderSprite(&objects->data[i].sprite);
}
}
#endif // OBJECT_H
-41
View File
@@ -1,41 +0,0 @@
#ifndef PHYSICS_H
#define PHYSICS_H
#include <raymath.h>
typedef struct
{
Vector2 position;
Vector2 direction;
Vector2 velocity;
float speed;
} Physics;
void UpdatePhysicsDirection(Vector2 *direction, Vector2 vector)
{
*direction = Vector2Normalize(vector);
}
void UpdatePhysicsVelocity(Vector2 *velocity, Vector2 *direction, float *speed)
{
*velocity = Vector2Scale(*direction, *speed * GetFrameTime());
}
void UpdatePhysicsPosition(Vector2 *position, Vector2 *velocity)
{
*position = Vector2Add(*position, *velocity);
}
void UpdatePhysicsSpeed(float *speed, float *newSpeed)
{
*speed = *newSpeed;
}
void UpdatePhysics(Physics *physics, Vector2 direction)
{
UpdatePhysicsDirection(&physics->direction, direction);
UpdatePhysicsVelocity(&physics->velocity, &physics->direction, &physics->speed);
UpdatePhysicsPosition(&physics->position, &physics->velocity);
}
#endif // PHYSICS_H
-46
View File
@@ -1,46 +0,0 @@
#ifndef POSTFX_H
#define POSTFX_H
typedef struct {
Shader shader;
int resolutionLoc;
int strengthLoc;
float strength;
} PostProcessing;
PostProcessing CreatePostFX(const char* shaderPath){
PostProcessing postFX = {0};
postFX.shader = LoadShader(NULL, shaderPath);
if (postFX.shader.id == 0) {
TraceLog(LOG_ERROR, "Failed to load shader: %s", shaderPath);
return postFX;
}
postFX.strength = 0.0f;
postFX.resolutionLoc = GetShaderLocation(postFX.shader, "resolution");
postFX.strengthLoc = GetShaderLocation(postFX.shader, "strength");
Vector2 resolution = { (float)GetScreenWidth(), (float)GetScreenHeight() };
SetShaderValue(postFX.shader, postFX.resolutionLoc, &resolution, SHADER_UNIFORM_VEC2);
return postFX;
}
void UpdatePostFX(PostProcessing *postFX){
if (postFX->shader.id == 0) return;
SetShaderValue(postFX->shader, postFX->strengthLoc, &postFX->strength, SHADER_UNIFORM_FLOAT);
Vector2 resolution = { (float)GetScreenWidth(), (float)GetScreenHeight() };
SetShaderValue(postFX->shader, postFX->resolutionLoc, &resolution, SHADER_UNIFORM_VEC2);
}
void RenderPostFX(PostProcessing *postFX, RenderTexture2D *target){
BeginShaderMode(postFX->shader);
DrawTextureRec(target->texture, (Rectangle){ 0, 0, target->texture.width, -target->texture.height }, (Vector2){ 0, 0 }, WHITE);
EndShaderMode();
}
void UnloadPostFX(PostProcessing *postFX){
if (postFX->shader.id == 0) return;
UnloadShader(postFX->shader);
}
#endif // POSTFX_H
-39
View File
@@ -1,39 +0,0 @@
#ifndef SOUNDTRACK_H
#define SOUNDTRACK_H
typedef struct {
Music music;
bool playing;
bool paused;
} Soundtrack;
Soundtrack LoadSoundtrack(const char* musicPath){
Soundtrack soundtrack = {
.music = LoadMusicStream(musicPath),
.playing = false,
.paused = false
};
PlayMusicStream(soundtrack.music);
return soundtrack;
}
void UpdateSoundtrack(Soundtrack *soundtrack, bool playing){
if (playing && !soundtrack->playing){
ResumeMusicStream(soundtrack->music);
soundtrack->paused = false;
} else if (!playing && soundtrack->playing){
PauseMusicStream(soundtrack->music);
soundtrack->paused = true;
}
soundtrack->playing = playing;
if (soundtrack->playing) {
UpdateMusicStream(soundtrack->music);
}
}
#endif // SOUNDTRACK_H
-62
View File
@@ -1,62 +0,0 @@
#ifndef SPRITE_H
#define SPRITE_H
#include <raymath.h>
typedef struct
{
Texture2D texture;
Rectangle sourceRec;
Rectangle destRec;
Vector2 frameSize;
Vector2 origin;
float rotation;
Color color;
} Sprite;
void UpdateSpriteSourceRec(Sprite *sprite, const Vector2 *vector)
{
sprite->sourceRec.x = vector->x;
sprite->sourceRec.y = vector->y;
}
void UpdateSpriteDestRec(Sprite *sprite, const Vector2 *vector)
{
sprite->destRec.x = vector->x;
sprite->destRec.y = vector->y;
}
void UpdateSpriteRotation(Sprite *sprite, Vector2 *vector)
{
if (Vector2Length(*vector) <= 0.0f)
{
return;
}
float targetRotation = (float)atan2(vector->y, vector->x) * (180.0f / PI) + 90.0f;
sprite->rotation = Lerp(sprite->rotation, targetRotation, 0.1f);
}
void RenderSprite(const Sprite *sprite)
{
DrawTexturePro(
sprite->texture,
sprite->sourceRec,
sprite->destRec,
sprite->origin,
sprite->rotation,
sprite->color);
}
void PrintSprite(const Sprite *sprite)
{
printf("Sprite:\n");
printf(" Sprite.texture: id = %d\n", sprite->texture.id);
printf(" Sprite.sourceRec: x = %f, y = %f, width = %f, height = %f\n", sprite->sourceRec.x, sprite->sourceRec.y, sprite->sourceRec.width, sprite->sourceRec.height);
printf(" Sprite.destRec: x = %f, y = %f, width = %f, height = %f\n", sprite->destRec.x, sprite->destRec.y, sprite->destRec.width, sprite->destRec.height);
printf(" Sprite.frameSize: x = %f, y = %f\n", sprite->frameSize.x, sprite->frameSize.y);
printf(" Sprite.origin: x = %f, y = %f\n", sprite->origin.x, sprite->origin.y);
printf(" Sprite.rotation: %f\n", sprite->rotation);
printf(" Sprite.color: r = %d, g = %d, b = %d, a = %d\n", sprite->color.r, sprite->color.g, sprite->color.b, sprite->color.a);
}
#endif // SPRITE_H
-21
View File
@@ -1,21 +0,0 @@
#ifndef VECTOR_H
#define VECTOR_H
typedef struct {
Vector2 *data;
size_t count;
} Vector2Array;
bool IsVectorZero(Vector2 v) {
float epsilon = 0.01f;
return (fabs(v.x) < epsilon && fabs(v.y) < epsilon);
}
Vector2 GetRandomVector(const Rectangle bounds) {
return (Vector2){
bounds.x + (float)GetRandomValue(0, 10000) / 10000.0f * bounds.width,
bounds.y + (float)GetRandomValue(0, 10000) / 10000.0f * bounds.height
};
}
#endif // VECTOR_H
-10
View File
@@ -1,10 +0,0 @@
#ifndef DISPLAY_H
#define DISPLAY_H
typedef struct {
int width;
int height;
const char* title;
} Window;
#endif // DISPLAY_H