mirror of
https://github.com/zoriya/Gamacon.git
synced 2026-06-02 03:35:43 +00:00
Adding destroyers and scene utility
This commit is contained in:
@@ -9,8 +9,16 @@ SRC = src/engine/engine.c \
|
||||
src/engine/engine_internal.c \
|
||||
src/engine/event_handler.c \
|
||||
src/renderer/renderer.c \
|
||||
src/renderer/texture_utility.c \
|
||||
src/entity/entity.c \
|
||||
src/entity/entity_factory.c \
|
||||
src/component.c \
|
||||
src/components/movable_component.c
|
||||
src/components/movable_component.c \
|
||||
src/components/position_component.c \
|
||||
src/scene/scene.c \
|
||||
src/utility/my_strdup.c \
|
||||
src/utility/my_strlen.c \
|
||||
src/utility/arraylen.c
|
||||
|
||||
OBJ = $(SRC:%.c=%.o)
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ typedef struct gc_component
|
||||
void (*ctr)(void *component, va_list);
|
||||
void (*dtr)(void *component);
|
||||
char *(*serialize)(void *component);
|
||||
void (*destroy)(void *component);
|
||||
|
||||
struct gc_component *next;
|
||||
struct gc_component *prev;
|
||||
|
||||
+6
-5
@@ -10,16 +10,17 @@ typedef struct gc_engine gc_engine;
|
||||
|
||||
#include "scene.h"
|
||||
#include <stdbool.h>
|
||||
#include <SFML/Graphics.h>
|
||||
|
||||
struct gc_engine
|
||||
struct gc_engine
|
||||
{
|
||||
gc_scene *scene;
|
||||
bool (*is_open)(struct gc_engine *engine);
|
||||
int (*game_loop)(struct gc_engine *engine);
|
||||
int (*change_scene)(struct gc_engine *engine, gc_scene *scene);
|
||||
bool (*is_open)(gc_engine *engine);
|
||||
int (*game_loop)(gc_engine *engine);
|
||||
int (*change_scene)(gc_engine *engine, gc_scene *scene);
|
||||
|
||||
sfRenderWindow *window;
|
||||
void (*draw)(struct gc_engine *engine);
|
||||
void (*draw)(gc_engine *engine);
|
||||
};
|
||||
|
||||
gc_engine *engine_create(char *title);
|
||||
|
||||
+9
-6
@@ -5,24 +5,27 @@
|
||||
** entity
|
||||
*/
|
||||
|
||||
typedef struct gc_entity gc_entity;
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "component.h"
|
||||
#include "vector2.h"
|
||||
#include "tags.h"
|
||||
|
||||
typedef struct gc_entity
|
||||
struct gc_entity
|
||||
{
|
||||
int id;
|
||||
char *tostr;
|
||||
gc_component *components;
|
||||
char *(*serialize)(struct gc_entity *entity);
|
||||
char *(*serialize)(gc_entity *entity);
|
||||
void (*destroy)(gc_entity *entity);
|
||||
|
||||
struct gc_entity *next;
|
||||
struct gc_entity *prev;
|
||||
} gc_entity;
|
||||
gc_entity *next;
|
||||
gc_entity *prev;
|
||||
};
|
||||
|
||||
char *entity_serialize(gc_entity *entity);
|
||||
|
||||
gc_entity *entity_create();
|
||||
gc_entity *entity_create(void);
|
||||
gc_entity *entity_add_component(gc_entity *entity, void *component);
|
||||
+2
-1
@@ -9,4 +9,5 @@
|
||||
|
||||
#include "engine.h"
|
||||
|
||||
void renderer_draw(gc_engine *engine);
|
||||
void renderer_draw(gc_engine *engine);
|
||||
gc_texture *texture_create(const char *path);
|
||||
+6
-4
@@ -8,17 +8,19 @@ typedef struct gc_scene gc_scene;
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "engine.h"
|
||||
#include "entity.h"
|
||||
#include "texture.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
struct gc_scene
|
||||
{
|
||||
gc_entity *entity_list;
|
||||
gc_entity *entities;
|
||||
gc_texture **textures;
|
||||
int (*add_entity)(gc_engine *engine, struct gc_scene *scene);
|
||||
int (*add_entity)(gc_scene *scene, gc_entity *entity);
|
||||
void (*destroy)(gc_scene *scene);
|
||||
};
|
||||
|
||||
gc_scene *scene_create(char **textures);
|
||||
void scene_add_entity(gc_scene *scene, gc_entity *entity);
|
||||
gc_scene *scene_create(const char **textures);
|
||||
int scene_add_entity(gc_scene *scene, gc_entity *entity);
|
||||
|
||||
|
||||
+5
-2
@@ -5,12 +5,15 @@
|
||||
** texture
|
||||
*/
|
||||
|
||||
typedef struct gc_texture gc_texture;
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <SFML/Graphics.h>
|
||||
|
||||
typedef struct gc_texture
|
||||
struct gc_texture
|
||||
{
|
||||
char *name;
|
||||
sfTexture *texture;
|
||||
} gc_texture;
|
||||
void (*destroy)(gc_texture *texture);
|
||||
};
|
||||
+2
-1
@@ -8,4 +8,5 @@
|
||||
#pragma once
|
||||
|
||||
char *my_strdup(const char *src);
|
||||
int my_strlen(const char *str);
|
||||
int my_strlen(const char *str);
|
||||
int arraylen(const char **array);
|
||||
@@ -23,4 +23,12 @@ void *new_component(void *component, ...)
|
||||
va_end(args);
|
||||
}
|
||||
return (new_cmp);
|
||||
}
|
||||
|
||||
void destroy(void *component)
|
||||
{
|
||||
gc_component *cmp = (gc_component *)component;
|
||||
if (cmp->dtr)
|
||||
cmp->dtr(component);
|
||||
free(cmp);
|
||||
}
|
||||
@@ -33,7 +33,7 @@ static char *movable_serialize(void *component)
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
const gc_component movable_component = {
|
||||
const gc_component movable_component = {
|
||||
name: "MovableComponent",
|
||||
size: sizeof(struct movable_component),
|
||||
dependencies: NULL,
|
||||
|
||||
@@ -18,10 +18,11 @@ void position_ctr(void *component, va_list args)
|
||||
|
||||
char *position_serialize(void *component)
|
||||
{
|
||||
(void)component;
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
const gc_component position_component = {
|
||||
const gc_component position_component = {
|
||||
name: "PositionComponent",
|
||||
size: sizeof(struct position_component),
|
||||
dependencies: NULL,
|
||||
|
||||
@@ -8,14 +8,15 @@
|
||||
#include <stdbool.h>
|
||||
#include "engine.h"
|
||||
|
||||
bool engine_is_open(gc_engine *engine)
|
||||
{
|
||||
return (sfRenderWindow_isOpen(engine->window));
|
||||
}
|
||||
bool engine_is_open(gc_engine *engine)
|
||||
{
|
||||
return (sfRenderWindow_isOpen(engine->window));
|
||||
}
|
||||
|
||||
int change_scene(gc_engine *engine, gc_scene *scene)
|
||||
{
|
||||
(void)engine;
|
||||
(void)scene;
|
||||
return (0);
|
||||
}
|
||||
int change_scene(gc_engine *engine, gc_scene *scene)
|
||||
{
|
||||
if (engine->scene)
|
||||
engine->scene->destroy(engine->scene);
|
||||
engine->scene = scene;
|
||||
return (0);
|
||||
}
|
||||
+14
-1
@@ -6,10 +6,23 @@
|
||||
*/
|
||||
|
||||
#include "entity.h"
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
char *entity_serialize(gc_entity *entity)
|
||||
{
|
||||
(void)entity;
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
void destroy(gc_entity *entity)
|
||||
{
|
||||
gc_component *next = NULL;
|
||||
|
||||
for (gc_component *cmp = entity->components; cmp; cmp = next) {
|
||||
next = cmp->next;
|
||||
cmp->destroy(cmp);
|
||||
}
|
||||
if (entity->tostr)
|
||||
free(entity->tostr);
|
||||
free(entity);
|
||||
}
|
||||
@@ -7,11 +7,11 @@
|
||||
|
||||
#include "entity.h"
|
||||
#include "component.h"
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
static unsigned int next_id = 0;
|
||||
|
||||
gc_entity *entity_create()
|
||||
gc_entity *entity_create(void)
|
||||
{
|
||||
gc_entity *entity = malloc(sizeof(gc_entity));
|
||||
|
||||
|
||||
@@ -8,8 +8,17 @@
|
||||
#include "texture.h"
|
||||
#include "utility.h"
|
||||
#include <SFML/Graphics.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
gc_texture *renderer_load_texture(char *path)
|
||||
|
||||
void texture_destroy(gc_texture *texture)
|
||||
{
|
||||
sfTexture_destroy(texture->texture);
|
||||
free(texture->name);
|
||||
free(texture);
|
||||
}
|
||||
|
||||
gc_texture *texture_create(const char *path)
|
||||
{
|
||||
gc_texture *texture = malloc(sizeof(gc_texture));
|
||||
|
||||
@@ -17,5 +26,6 @@ gc_texture *renderer_load_texture(char *path)
|
||||
return (NULL);
|
||||
texture->texture = sfTexture_createFromFile(path, NULL);
|
||||
texture->name = my_strdup(path);
|
||||
texture->destroy = &texture_destroy;
|
||||
return (texture);
|
||||
}
|
||||
+30
-11
@@ -7,46 +7,65 @@
|
||||
|
||||
#include "engine.h"
|
||||
#include "entity.h"
|
||||
#include <stdbool.h>
|
||||
#include "renderer.h"
|
||||
#include "utility.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
void scene_add_entity(gc_scene *scene, gc_entity *entity)
|
||||
int scene_add_entity(gc_scene *scene, gc_entity *entity)
|
||||
{
|
||||
gc_entity *list;
|
||||
|
||||
if (!scene)
|
||||
return;
|
||||
list = scene->entity_list;
|
||||
return (-1);
|
||||
list = scene->entities;
|
||||
if (!list) {
|
||||
scene->entity_list = entity;
|
||||
scene->entities = entity;
|
||||
} else {
|
||||
while (list->next)
|
||||
list = list->next;
|
||||
list->next = entity;
|
||||
entity->prev = list;
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
bool scene_load_textures(gc_scene *scene, char **textures)
|
||||
int scene_load_textures(gc_scene *scene, const char **textures)
|
||||
{
|
||||
gc_texture *texture;
|
||||
|
||||
scene->textures = malloc(sizeof(gc_texture) * (arraylen(textures)));
|
||||
for (int i = 0; textures[i]; i++) {
|
||||
texture = renderer_load_texture(textures[i]);
|
||||
texture = texture_create(textures[i]);
|
||||
if (!texture)
|
||||
return (NULL);
|
||||
return (-1);
|
||||
scene->textures[i] = texture;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
gc_scene *scene_create(char **textures)
|
||||
gc_scene *scene_create(const char **textures)
|
||||
{
|
||||
gc_scene *scene = malloc(sizeof(gc_scene));
|
||||
|
||||
if (!scene)
|
||||
return (NULL);
|
||||
if (!scene_load_textures(scene, textures))
|
||||
if (scene_load_textures(scene, textures) < 0)
|
||||
return (NULL);
|
||||
scene->entity_list = NULL;
|
||||
scene->entities = NULL;
|
||||
scene->add_entity = &scene_add_entity;
|
||||
return (scene);
|
||||
}
|
||||
|
||||
void scene_destroy(gc_scene *scene)
|
||||
{
|
||||
gc_entity *next = NULL;
|
||||
|
||||
for (gc_entity *entity = scene->entities; entity; entity = next) {
|
||||
next = entity->next;
|
||||
entity->destroy(entity);
|
||||
}
|
||||
for (int i = 0; scene->textures[i]; i++) {
|
||||
scene->textures[i]->destroy(scene->textures[i]);
|
||||
}
|
||||
free(scene);
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
/*
|
||||
** EPITECH PROJECT, 2019
|
||||
** MUL_my_runner_2019
|
||||
** File description:
|
||||
** arraylen
|
||||
*/
|
||||
|
||||
int arraylen(const char **array)
|
||||
{
|
||||
int i = 0;
|
||||
|
||||
while (array[i])
|
||||
i++;
|
||||
return (i);
|
||||
}
|
||||
Reference in New Issue
Block a user