Using the xml parser

This commit is contained in:
AnonymusRaccoon
2019-12-09 12:09:25 +01:00
parent 3633f6935d
commit ee89f69e7a
25 changed files with 343 additions and 125 deletions
+2 -1
View File
@@ -14,8 +14,9 @@ SRC = src/engine/engine.c \
src/entity/entity_factory.c \
src/component.c \
src/components/movable_component.c \
src/components/position_component.c \
src/components/transform_component.c \
src/components/texture_renderer.c \
src/components/parallax_component.c \
src/scene/scene.c \
src/utility/my_strdup.c \
src/utility/my_strlen.c \
+2 -2
View File
@@ -16,9 +16,9 @@ struct gc_component
{
char *name;
unsigned size;
gc_component *dependencies;
char **dependencies;
void (*ctr)(void *component, va_list);
void (*fdctr)(gc_engine *engine, void *component, char *args);
void (*fdctr)(gc_engine *engine, void *component, node *n);
void (*dtr)(void *component);
char *(*serialize)(void *component);
void (*destroy)(void *component);
+18
View File
@@ -0,0 +1,18 @@
/*
** EPITECH PROJECT, 2019
** Gamacon
** File description:
** parallax_component
*/
#pragma once
#include "component.h"
struct parallax_component
{
gc_component base;
float speed;
};
extern const struct parallax_component parallax_component;
+2 -2
View File
@@ -8,13 +8,13 @@
#pragma once
#include "component.h"
#include "texture.h"
#include "sprite.h"
#include "vector2.h"
struct texture_renderer
{
gc_component base;
gc_texture *texture;
gc_sprite *sprite;
};
extern const struct texture_renderer texture_renderer;
@@ -10,11 +10,11 @@
#include "component.h"
#include "vector2.h"
struct position_component
struct transform_component
{
gc_component base;
gc_vector2 position;
gc_vector2 size;
};
extern const struct position_component position_component;
extern const struct transform_component transform_component;
+4 -1
View File
@@ -8,11 +8,13 @@ typedef struct gc_engine gc_engine;
#pragma once
#include "xml.h"
#include "scene.h"
#include "vector2.h"
#include "texture.h"
#include "system.h"
#include "list.h"
#include "sprite.h"
#include <stdbool.h>
#include <SFML/Graphics.h>
@@ -22,7 +24,7 @@ struct gc_engine
bool (*is_open)(gc_engine *engine);
int (*game_loop)(gc_engine *engine);
int (*change_scene)(gc_engine *engine, gc_scene *scene);
void (*draw_texture)(gc_engine *, gc_texture *, gc_vector2, gc_vector2);
void (*draw_texture)(gc_engine *, gc_sprite *);
void (*destroy)(gc_engine *engine);
gc_list *systems;
@@ -30,6 +32,7 @@ struct gc_engine
sfRenderWindow *window;
sfSprite *sprite;
sfClock *clock;
void (*draw)(gc_engine *engine);
};
+1 -1
View File
@@ -12,4 +12,4 @@
gc_entity *prefab_load(gc_engine *engine, const char *path);
gc_entity *deserialize_entity(gc_engine *engine, int fd);
gc_entity *deserialize_entity(gc_engine *engine, node *n);
+1 -1
View File
@@ -11,4 +11,4 @@
void renderer_draw(gc_engine *engine);
gc_texture *texture_create(const char *path);
void renderer_draw_texture(gc_engine *, gc_texture *, gc_vector2, gc_vector2);
void renderer_draw_texture(gc_engine *, gc_sprite *);
+27
View File
@@ -0,0 +1,27 @@
/*
** EPITECH PROJECT, 2019
** MUL_my_runner_2019
** File description:
** sprite
*/
#pragma once
#include "texture.h"
#include "vector2.h"
#include <SFML/Graphics.h>
typedef struct gc_int_rect
{
float height;
float width;
float top;
float left;
} gc_int_rect;
typedef struct gc_sprite {
gc_texture *texture;
gc_int_rect rect;
gc_vector2 pos;
gc_vector2 size;
} gc_sprite;
+1 -1
View File
@@ -18,7 +18,7 @@ struct gc_system
const char *name;
const char *component_name;
bool (*check_dependencies)(const gc_system *, const gc_entity *);
void (*update_entity)(gc_engine *engine, gc_entity *entity);
void (*update_entity)(gc_engine *engine, gc_entity *entity, float dtime);
void (*destroy)(void *system);
};
+35
View File
@@ -0,0 +1,35 @@
/*
** EPITECH PROJECT, 2019
** MUL_my_runner_2019
** File description:
** xml
*/
typedef struct node node;
typedef struct dictionary dictionary;
#pragma once
struct dictionary
{
char *key;
char *value;
dictionary *next;
};
struct node
{
char *name;
dictionary *properties;
node *child;
node *next;
};
node *xml_parse(const char *path);
node *xml_getnode(node *parent, const char *name);
char *xml_getproperty(node *n, const char *key);
int xml_getintprop(node *n, const char *key);
float xml_getfloatprop(node *n, const char *key);
void xml_destroy(node *n);
+2 -2
View File
@@ -6,7 +6,7 @@
*/
#include "component.h"
#include "components/position_component.h"
#include "components/transform_component.h"
#include "components/movable_component.h"
#include "components/texture_renderer.h"
#include "utility.h"
@@ -15,7 +15,7 @@
const void *get_component(char *name)
{
static const void *all_components[] = {
&position_component,
&transform_component,
&movable_component,
&texture_renderer,
NULL
+6 -5
View File
@@ -5,6 +5,7 @@
** movable_component
*/
#include "xml.h"
#include "component.h"
#include "components/movable_component.h"
#include "utility.h"
@@ -19,13 +20,13 @@ static void movable_ctr(void *component, va_list args)
cmp->jump_key = va_arg(args, int);
}
static void movable_fdctr(gc_engine *engine, void *component, char *args)
static void movable_fdctr(gc_engine *engine, void *component, node *n)
{
struct movable_component *cmp = (struct movable_component *)component;
cmp->left_key = parse_arg_int(&args);
cmp->right_key = parse_arg_int(&args);
cmp->jump_key = parse_arg_int(&args);
cmp->left_key = xml_getproperty(n, "left")[0];
cmp->right_key = xml_getproperty(n, "right")[0];
cmp->jump_key = xml_getproperty(n, "jump")[0];
(void)engine;
}
@@ -44,7 +45,7 @@ const struct movable_component movable_component = {
base: {
name: "MovableComponent",
size: sizeof(struct movable_component),
dependencies: NULL,
dependencies: (char *[]){NULL},
ctr: &movable_ctr,
fdctr: &movable_fdctr,
dtr: &movable_dtr,
+50
View File
@@ -0,0 +1,50 @@
/*
** EPITECH PROJECT, 2019
** MUL_my_runner_2019
** File description:
** parralax_component
*/
#include "xml.h"
#include "components/parallax_component.h"
#include "components/transform_component.h"
static void parallax_ctr(void *component, va_list args)
{
struct parallax_component *cmp = (struct parallax_component *)component;
cmp->speed = va_arg(args, double);
}
static void parallax_fdctr(gc_engine *engine, void *component, node *n)
{
struct parallax_component *cmp = (struct parallax_component *)component;
cmp->speed = xml_getfloatprop(n, "speed");
(void)engine;
}
static void parallax_dtr(void *component)
{
(void)component;
}
static char *parallax_serialize(void *component)
{
(void)component;
return (NULL);
}
const struct parallax_component parallax_component = {
base: {
name: "ParallaxComponent",
size: sizeof(struct parallax_component),
dependencies: (char *[]){"TransformComponent", "TextureRenderer", NULL},
ctr: &parallax_ctr,
fdctr: &parallax_fdctr,
dtr: &parallax_dtr,
serialize: &parallax_serialize,
destroy: &component_destroy
},
speed: 0.5
};
-51
View File
@@ -1,51 +0,0 @@
/*
** EPITECH PROJECT, 2019
** MUL_my_runner_2019
** File description:
** position_component
*/
#include "component.h"
#include "components/position_component.h"
#include "utility.h"
#include <stddef.h>
void position_ctr(void *component, va_list args)
{
struct position_component *cmp = (struct position_component *)component;
cmp->position = va_arg(args, gc_vector2);
cmp->size = va_arg(args, gc_vector2);
}
void position_fdctr(gc_engine *engine, void *component, char *args)
{
struct position_component *cmp = (struct position_component *)component;
cmp->position.x = parse_arg_int(&args);
cmp->position.y = parse_arg_int(&args);
cmp->size.x = parse_arg_float(&args);
cmp->size.y = parse_arg_float(&args);
(void)engine;
}
char *position_serialize(void *component)
{
(void)component;
return (NULL);
}
const struct position_component position_component = {
base: {
name: "PositionComponent",
size: sizeof(struct position_component),
dependencies: NULL,
ctr: &position_ctr,
fdctr: &position_fdctr,
dtr: NULL,
serialize: &position_serialize,
destroy: &component_destroy
},
position: {0, 0},
size: {0, 0}
};
+32 -6
View File
@@ -6,22 +6,48 @@
*/
#include "engine.h"
#include "xml.h"
#include "utility.h"
#include "components/position_component.h"
#include "components/transform_component.h"
#include "components/texture_renderer.h"
#include <stdlib.h>
static void texture_rend_ctr(void *component, va_list args)
{
struct texture_renderer *cmp = (struct texture_renderer *)component;
sfVector2u size;
cmp->texture = va_arg(args, gc_texture *);
cmp->sprite = malloc(sizeof(gc_sprite));
if (!cmp->sprite)
return;
cmp->sprite->texture = va_arg(args, gc_texture *);
cmp->sprite->rect = va_arg(args, gc_int_rect);
if (cmp->sprite->texture && cmp->sprite->rect.height < 0) {
size = sfTexture_getSize(cmp->sprite->texture->texture);
cmp->sprite->rect.height = (float)size.y;
cmp->sprite->rect.width = (float)size.x;
}
}
static void texture_rend_fdctr(gc_engine *engine, void *component, char *args)
static void texture_rend_fdctr(gc_engine *engine, void *component, node *n)
{
struct texture_renderer *cmp = (struct texture_renderer *)component;
node *rect = xml_getnode(n, "Rect");
sfVector2u size;
cmp->texture = get_texture(engine, parse_arg_str(&args));
cmp->sprite = malloc(sizeof(gc_sprite));
if (!cmp->sprite)
return;
cmp->sprite->texture = get_texture(engine, xml_getproperty(n, "src"));
cmp->sprite->rect.height = xml_getfloatprop(rect, "height");
cmp->sprite->rect.width = xml_getfloatprop(rect, "width");
cmp->sprite->rect.top = xml_getfloatprop(rect, "top");
cmp->sprite->rect.left = xml_getfloatprop(rect, "left");
if (cmp->sprite && cmp->sprite->rect.height < 0) {
size = sfTexture_getSize(cmp->sprite->texture->texture);
cmp->sprite->rect.height = (float)size.y;
cmp->sprite->rect.width = (float)size.x;
}
}
static void texture_rend_dtr(void *component)
@@ -39,7 +65,7 @@ const struct texture_renderer texture_renderer = {
base: {
name: "TextureRenderer",
size: sizeof(struct texture_renderer),
dependencies: (void *){ &position_component, NULL },
dependencies: (char *[]){"TransformComponent", NULL},
ctr: &texture_rend_ctr,
fdctr: &texture_rend_fdctr,
dtr: &texture_rend_dtr,
@@ -48,5 +74,5 @@ const struct texture_renderer texture_renderer = {
next: NULL,
prev: NULL
},
texture: NULL,
sprite: NULL,
};
+64
View File
@@ -0,0 +1,64 @@
/*
** EPITECH PROJECT, 2019
** MUL_my_runner_2019
** File description:
** position_component
*/
#include "component.h"
#include "xml.h"
#include "components/transform_component.h"
#include "utility.h"
#include <stddef.h>
void transform_ctr(void *component, va_list args)
{
struct transform_component *cmp = (struct transform_component *)component;
cmp->position = va_arg(args, gc_vector2);
cmp->size = va_arg(args, gc_vector2);
}
void transform_fdctr(gc_engine *engine, void *component, node *n)
{
struct transform_component *cmp = (struct transform_component *)component;
node *pos = xml_getnode(n, "Position");
node *size = xml_getnode(n, "Size");
if (pos) {
cmp->position.x = xml_getintprop(pos, "x");
cmp->position.y = xml_getintprop(pos, "y");
} else {
cmp->position.x = 0;
cmp->position.y = 0;
}
if (size) {
cmp->size.x = xml_getintprop(size, "x");
cmp->size.y = xml_getintprop(size, "y");
} else {
cmp->size.x = 0;
cmp->size.y = 0;
}
(void)engine;
}
char *transform_serialize(void *component)
{
(void)component;
return (NULL);
}
const struct transform_component transform_component = {
base: {
name: "TransformComponent",
size: sizeof(struct transform_component),
dependencies: (char *[]){NULL},
ctr: &transform_ctr,
fdctr: &transform_fdctr,
dtr: NULL,
serialize: &transform_serialize,
destroy: &component_destroy
},
position: {0, 0},
size: {0, 0}
};
+10 -21
View File
@@ -6,52 +6,41 @@
*/
#include <stdio.h>
#include "xml.h"
#include "read_line.h"
#include "utility.h"
#include "entity.h"
#include "engine.h"
gc_component *deserialize_component(gc_engine *engine, int fd)
gc_component *deserialize_component(gc_engine *engine, node *n)
{
char *component = read_line(fd);
const gc_component *model;
gc_component *cmp = NULL;
char *args;
if (!component || !my_strcmp(component, ""))
return (NULL);
args = my_strchr(component, ' ');
if (!args)
return (NULL);
*args = '\0';
args += 1;
model = get_component(component);
model = get_component(n->name);
if (!model)
return (NULL);
cmp = new_component(model, 0, 0, 0, 0, 0, 0, 0);
cmp->fdctr(engine, cmp, args);
cmp->fdctr(engine, cmp, n);
return (cmp);
}
gc_entity *deserialize_entity(gc_engine *engine, int fd)
gc_entity *deserialize_entity(gc_engine *engine, node *n)
{
gc_entity *entity = NULL;
char *id_str = read_line(fd);
int id;
gc_component *cmp = NULL;
int id = xml_getintprop(n, "id");
if (!id_str)
return (NULL);
id = my_atoi(id_str);
if (id < 0)
return (NULL);
if (id > 0)
entity = entity_create_with_id(id);
else
entity = entity_create();
if (!entity)
return (NULL);
while ((cmp = deserialize_component(engine, fd)))
for (node *cmp_n = n->child; n; n = n->next) {
cmp = deserialize_component(engine, cmp_n);
entity->add_component(entity, cmp);
}
return (entity);
}
+7 -8
View File
@@ -5,6 +5,7 @@
** prefab
*/
#include "xml.h"
#include "engine.h"
#include "entity.h"
#include "prefab.h"
@@ -16,15 +17,13 @@
gc_entity *prefab_load(gc_engine *engine, const char *path)
{
gc_entity *entity = NULL;
int fd = open(path, O_RDONLY);
char *object_type = NULL;
node *n = xml_parse(path);
if (fd == -1)
if (!n)
return (NULL);
while ((object_type = read_line(fd))) {
if (!my_strcmp(object_type, "Entity"))
entity = entity_add(entity, deserialize_entity(engine, fd));
}
close(fd);
n = xml_getnode(n, "gc_scene");
for (node *ent_n = n->child; n; n = n->next)
entity = entity_add(entity, deserialize_entity(engine, ent_n));
xml_destroy(n);
return (entity);
}
+8 -4
View File
@@ -10,22 +10,24 @@
#include "renderer.h"
#include <stdlib.h>
#include <SFML/Graphics.h>
#include <SFML/System.h>
void update_entity(gc_engine *engine, gc_entity *entity)
void update_entity(gc_engine *engine, gc_entity *entity, float dtime)
{
for (gc_list *sys = engine->systems; sys; sys = sys->next) {
if (((gc_system *)sys->data)->check_dependencies(sys->data, entity))
((gc_system *)sys->data)->update_entity(engine, entity);
((gc_system *)sys->data)->update_entity(engine, entity, dtime);
}
}
int game_loop(gc_engine *engine)
{
gc_entity *entities = engine->scene->entities;
float dtime = sfTime_asSeconds(sfClock_restart(engine->clock));
handle_events(engine);
for (gc_entity *entity = entities; entity; entity = entity->next)
update_entity(engine, entity);
update_entity(engine, entity, dtime);
engine->draw(engine);
return (0);
}
@@ -43,6 +45,7 @@ void engine_destroy(gc_engine *engine)
}
sfSprite_destroy(engine->sprite);
sfRenderWindow_destroy(engine->window);
sfClock_destroy(engine->clock);
free(engine);
}
@@ -53,7 +56,8 @@ int engine_create_sfdata(gc_engine *engine, char *title, unsigned framerate)
engine->window = sfRenderWindow_create(mode, title, style, NULL);
engine->sprite = sfSprite_create();
if (!engine->window || !engine->sprite)
engine->clock = sfClock_create();
if (!engine->window || !engine->sprite || !engine->clock)
return (-1);
sfRenderWindow_setFramerateLimit(engine->window, framerate);
return (0);
+15 -8
View File
@@ -6,20 +6,27 @@
*/
#include "engine.h"
#include "sprite.h"
#include <SFML/Graphics.h>
void renderer_draw_texture(gc_engine *engine, gc_texture *text, \
gc_vector2 pos, gc_vector2 size)
void renderer_draw_texture(gc_engine *engine, gc_sprite *sprite)
{
sfVector2f pos = (sfVector2f){sprite->pos.x, sprite->pos.y};
sfVector2f scale;
sfVector2u t;
sfIntRect rect = {
(int)sprite->rect.left, (int)sprite->rect.top,
(int)sprite->rect.width, (int)sprite->rect.height
};
if (!text)
if (!sprite->texture)
return;
t = sfTexture_getSize(text->texture);
sfSprite_setTexture(engine->sprite, text->texture, true);
sfSprite_setPosition(engine->sprite, (sfVector2f){pos.x, pos.y});
sfSprite_setScale(engine->sprite, (sfVector2f){size.x / t.x, size.y / t.y});
t = sfTexture_getSize(sprite->texture->texture);
scale = (sfVector2f){sprite->size.x / t.x, sprite->size.y / t.y};
sfSprite_setTexture(engine->sprite, sprite->texture->texture, true);
sfSprite_setPosition(engine->sprite, pos);
sfSprite_setScale(engine->sprite, scale);
// sfSprite_setTextureRect(engine->sprite, rect);
sfRenderWindow_drawSprite(engine->window, engine->sprite, NULL);
}
+1 -1
View File
@@ -16,7 +16,7 @@ gc_texture *get_texture(gc_engine *engine, char *name)
{
gc_texture **textures;
if (!engine->scene)
if (!engine->scene || !name)
return (NULL);
textures = engine->scene->textures;
for (int i = 0; textures[i]; i++) {
+3 -3
View File
@@ -14,10 +14,10 @@ bool system_check_dependencies(const gc_system *sys, const gc_entity *entity)
void *cmp = entity->get_component(entity, sys->component_name);
gc_component *comp = (gc_component *)cmp;
if (!cmp)
if (!comp)
return (false);
for (gc_component *dep = comp->dependencies; dep; dep = comp->next) {
if (!entity->has_component(entity, dep->name))
for (int i = 0; comp->dependencies[i]; i++) {
if (!entity->has_component(entity, comp->dependencies[i]))
return (false);
}
return (true);
+40
View File
@@ -0,0 +1,40 @@
/*
** EPITECH PROJECT, 2019
** MUL_my_runner_2019
** File description:
** parallax_system
*/
#include "entity.h"
#include "system.h"
#include "texture.h"
#include "vector2.h"
#include "systems/texture_renderer_system.h"
#include "components/parallax_component.h"
#include "components/texture_renderer.h"
#include <stddef.h>
void parallax_update_entity(gc_engine *engine, gc_entity *entity, float dtime)
{
struct texture_renderer *text = \
(struct texture_renderer *)entity->get_component(entity, "TextureRenderer");
struct parallax_component *parallax = \
(struct parallax_component *)entity->get_component(entity, "ParallaxComponent");
if (!text->sprite)
return;
text->sprite->rect.left += parallax->speed * dtime;
}
void parallax_destroy(void *system)
{
(void)system;
}
const gc_system texture_renderer_system = {
name: "ParallaxSystem",
component_name: "ParallaxComponent",
check_dependencies: &system_check_dependencies,
update_entity: &parallax_update_entity,
destroy: &parallax_destroy
};
+10 -5
View File
@@ -10,18 +10,23 @@
#include "texture.h"
#include "vector2.h"
#include "systems/texture_renderer_system.h"
#include "components/position_component.h"
#include "components/transform_component.h"
#include "components/texture_renderer.h"
#include <stddef.h>
void tex_rend_update_entity(gc_engine *engine, gc_entity *entity)
void tex_rend_update_entity(gc_engine *engine, gc_entity *entity, float dtime)
{
struct position_component *pos = \
(struct position_component *)entity->get_component(entity, "PositionComponent");
struct transform_component *pos = \
(struct transform_component *)entity->get_component(entity, "TransformComponent");
struct texture_renderer *text = (\
struct texture_renderer *)entity->get_component(entity, "TextureRenderer");
engine->draw_texture(engine, text->texture, pos->position, pos->size);
if (!text->sprite)
return;
text->sprite->pos = pos->position;
text->sprite->size = pos->size;
engine->draw_texture(engine, text->sprite);
(void)dtime;
}
void tex_rend_destroy(void *system)