From ee89f69e7afad1c9288c2d377f784571be5a2fed Mon Sep 17 00:00:00 2001 From: AnonymusRaccoon Date: Mon, 9 Dec 2019 12:09:25 +0100 Subject: [PATCH] Using the xml parser --- Makefile | 3 +- include/component.h | 4 +- include/components/parallax_component.h | 18 ++++++ include/components/texture_renderer.h | 4 +- ...tion_component.h => transform_component.h} | 4 +- include/engine.h | 5 +- include/prefab.h | 2 +- include/renderer.h | 2 +- include/sprite.h | 27 ++++++++ include/system.h | 2 +- include/xml.h | 35 ++++++++++ src/component.c | 4 +- src/components/movable_component.c | 11 ++-- src/components/parallax_component.c | 50 +++++++++++++++ src/components/position_component.c | 51 --------------- src/components/texture_renderer.c | 38 +++++++++-- src/components/transform_component.c | 64 +++++++++++++++++++ src/deserializer/deserialize_entity.c | 31 +++------ src/deserializer/prefab.c | 15 ++--- src/engine/engine.c | 12 ++-- src/renderer/renderer.c | 23 ++++--- src/renderer/texture_utility.c | 2 +- src/system.c | 6 +- src/systems/parallax_system.c | 40 ++++++++++++ src/systems/texture_renderer_system.c | 15 +++-- 25 files changed, 343 insertions(+), 125 deletions(-) create mode 100644 include/components/parallax_component.h rename include/components/{position_component.h => transform_component.h} (72%) create mode 100644 include/sprite.h create mode 100644 include/xml.h create mode 100644 src/components/parallax_component.c delete mode 100644 src/components/position_component.c create mode 100644 src/components/transform_component.c create mode 100644 src/systems/parallax_system.c diff --git a/Makefile b/Makefile index 5163a82..cd17e85 100644 --- a/Makefile +++ b/Makefile @@ -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 \ diff --git a/include/component.h b/include/component.h index 0383ff3..7608c75 100644 --- a/include/component.h +++ b/include/component.h @@ -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); diff --git a/include/components/parallax_component.h b/include/components/parallax_component.h new file mode 100644 index 0000000..fd3d50e --- /dev/null +++ b/include/components/parallax_component.h @@ -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; \ No newline at end of file diff --git a/include/components/texture_renderer.h b/include/components/texture_renderer.h index 2d51474..f4410e9 100644 --- a/include/components/texture_renderer.h +++ b/include/components/texture_renderer.h @@ -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; \ No newline at end of file diff --git a/include/components/position_component.h b/include/components/transform_component.h similarity index 72% rename from include/components/position_component.h rename to include/components/transform_component.h index bf6c7d1..675a37f 100644 --- a/include/components/position_component.h +++ b/include/components/transform_component.h @@ -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; \ No newline at end of file +extern const struct transform_component transform_component; \ No newline at end of file diff --git a/include/engine.h b/include/engine.h index 5032199..fe062d4 100644 --- a/include/engine.h +++ b/include/engine.h @@ -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 #include @@ -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); }; diff --git a/include/prefab.h b/include/prefab.h index 1e264c2..6b84084 100644 --- a/include/prefab.h +++ b/include/prefab.h @@ -12,4 +12,4 @@ gc_entity *prefab_load(gc_engine *engine, const char *path); -gc_entity *deserialize_entity(gc_engine *engine, int fd); \ No newline at end of file +gc_entity *deserialize_entity(gc_engine *engine, node *n); \ No newline at end of file diff --git a/include/renderer.h b/include/renderer.h index b8950e5..70be2a6 100644 --- a/include/renderer.h +++ b/include/renderer.h @@ -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); \ No newline at end of file +void renderer_draw_texture(gc_engine *, gc_sprite *); \ No newline at end of file diff --git a/include/sprite.h b/include/sprite.h new file mode 100644 index 0000000..3571915 --- /dev/null +++ b/include/sprite.h @@ -0,0 +1,27 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** sprite +*/ + +#pragma once + +#include "texture.h" +#include "vector2.h" +#include + +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; \ No newline at end of file diff --git a/include/system.h b/include/system.h index e909a15..c302ef8 100644 --- a/include/system.h +++ b/include/system.h @@ -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); }; diff --git a/include/xml.h b/include/xml.h new file mode 100644 index 0000000..d1c906d --- /dev/null +++ b/include/xml.h @@ -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); \ No newline at end of file diff --git a/src/component.c b/src/component.c index 6b7d960..b265e0e 100644 --- a/src/component.c +++ b/src/component.c @@ -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 diff --git a/src/components/movable_component.c b/src/components/movable_component.c index b22f16b..b4e74ed 100644 --- a/src/components/movable_component.c +++ b/src/components/movable_component.c @@ -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, diff --git a/src/components/parallax_component.c b/src/components/parallax_component.c new file mode 100644 index 0000000..636987b --- /dev/null +++ b/src/components/parallax_component.c @@ -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: ¶llax_ctr, + fdctr: ¶llax_fdctr, + dtr: ¶llax_dtr, + serialize: ¶llax_serialize, + destroy: &component_destroy + }, + speed: 0.5 +}; \ No newline at end of file diff --git a/src/components/position_component.c b/src/components/position_component.c deleted file mode 100644 index dca74c3..0000000 --- a/src/components/position_component.c +++ /dev/null @@ -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 - -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} -}; \ No newline at end of file diff --git a/src/components/texture_renderer.c b/src/components/texture_renderer.c index 82aa706..55ddb66 100644 --- a/src/components/texture_renderer.c +++ b/src/components/texture_renderer.c @@ -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 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, }; \ No newline at end of file diff --git a/src/components/transform_component.c b/src/components/transform_component.c new file mode 100644 index 0000000..c20709a --- /dev/null +++ b/src/components/transform_component.c @@ -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 + +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} +}; \ No newline at end of file diff --git a/src/deserializer/deserialize_entity.c b/src/deserializer/deserialize_entity.c index 6a048d7..27f630b 100644 --- a/src/deserializer/deserialize_entity.c +++ b/src/deserializer/deserialize_entity.c @@ -6,52 +6,41 @@ */ #include +#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); } \ No newline at end of file diff --git a/src/deserializer/prefab.c b/src/deserializer/prefab.c index 4d5c973..6b98a41 100644 --- a/src/deserializer/prefab.c +++ b/src/deserializer/prefab.c @@ -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); } diff --git a/src/engine/engine.c b/src/engine/engine.c index 5b8dcac..f2a41b6 100644 --- a/src/engine/engine.c +++ b/src/engine/engine.c @@ -10,22 +10,24 @@ #include "renderer.h" #include #include +#include -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); diff --git a/src/renderer/renderer.c b/src/renderer/renderer.c index 0f58ca8..0e68307 100644 --- a/src/renderer/renderer.c +++ b/src/renderer/renderer.c @@ -6,20 +6,27 @@ */ #include "engine.h" +#include "sprite.h" #include - -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); } diff --git a/src/renderer/texture_utility.c b/src/renderer/texture_utility.c index 64b8d6e..fad1aad 100644 --- a/src/renderer/texture_utility.c +++ b/src/renderer/texture_utility.c @@ -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++) { diff --git a/src/system.c b/src/system.c index 7e563c5..020a858 100644 --- a/src/system.c +++ b/src/system.c @@ -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); diff --git a/src/systems/parallax_system.c b/src/systems/parallax_system.c new file mode 100644 index 0000000..4027b5f --- /dev/null +++ b/src/systems/parallax_system.c @@ -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 + +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: ¶llax_update_entity, + destroy: ¶llax_destroy +}; \ No newline at end of file diff --git a/src/systems/texture_renderer_system.c b/src/systems/texture_renderer_system.c index f8be158..7c1e028 100644 --- a/src/systems/texture_renderer_system.c +++ b/src/systems/texture_renderer_system.c @@ -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 -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)