diff --git a/include/component.h b/include/component.h index d16c163..6413b94 100644 --- a/include/component.h +++ b/include/component.h @@ -32,6 +32,7 @@ void *new_component(const void *component, ...); void component_destroy(void *component); gc_component *component_remove(gc_component *cmp, const char *name); -#define GETCMP(entity, x) ((struct x *)entity->get_component(entity, #x)) +#define GETCMP(entity, x) ((struct x *)((gc_entity *)entity)->\ +get_component(entity, #x)) #define GETCOLCMP(x) ((struct x *)entity_get_component(\ entity_get(engine->scene, id), #x)) diff --git a/include/components/tag_component.h b/include/components/tag_component.h new file mode 100644 index 0000000..59647fd --- /dev/null +++ b/include/components/tag_component.h @@ -0,0 +1,18 @@ +// +// Created by anonymus-raccoon on 3/3/20. +// + +#ifndef _TAG_COMPONENT_H_ +#define _TAG_COMPONENT_H_ + +#include "component.h" + +struct tag_component +{ + gc_component base; + char *tag; +}; + +const struct tag_component tag_component; + +#endif //_TAG_COMPONENT_H_ diff --git a/include/entity.h b/include/entity.h index 27ed8c3..5f8cfa6 100644 --- a/include/entity.h +++ b/include/entity.h @@ -23,7 +23,7 @@ struct gc_entity void (*remove_component)(const gc_scene *scene, const gc_entity *entity, \ const char *name); char *(*serialize)(gc_entity *entity, int fd); - void (*destroy)(gc_entity *entity); + void (*destroy)(gc_entity *entity, gc_scene *scene); }; gc_entity *entity_create(void); diff --git a/include/list.h b/include/list.h index 7ebfba5..a3a13de 100644 --- a/include/list.h +++ b/include/list.h @@ -16,5 +16,7 @@ struct gc_list }; gc_list *list_add(gc_list *list, void *obj); +gc_list *list_remove(gc_list *list, void *obj); -#define LISTADD(list, obj) (list = list_add(list, obj)) \ No newline at end of file +#define LISTADD(list, obj) (list = list_add(list, obj)) +#define LISTREM(list, obj) (list = list_remove(list, obj)) \ No newline at end of file diff --git a/include/prefab.h b/include/prefab.h index 50d91f7..3c4075b 100644 --- a/include/prefab.h +++ b/include/prefab.h @@ -11,4 +11,6 @@ int prefab_load(gc_engine *engine, const char *path); int prefab_loadentities(node *n, gc_engine *engine, gc_scene *scene); -gc_entity *deserialize_entity(gc_engine *engine, gc_scene *scene, node *n); \ No newline at end of file +gc_entity *deserialize_entity(gc_engine *engine, gc_scene *scene, node *n); +gc_component *deserialize_component(gc_engine *engine, gc_entity *entity, \ +gc_scene *scene, node *n); \ No newline at end of file diff --git a/src/components/tag_component.c b/src/components/tag_component.c new file mode 100644 index 0000000..6fa1d8e --- /dev/null +++ b/src/components/tag_component.c @@ -0,0 +1,55 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** parralax_component +*/ + +#include "components/tag_component.h" +#include +#include "xml.h" +#include "components/parallax_component.h" +#include "components/transform_component.h" + +static void ctr(void *component, va_list args) +{ + struct tag_component *cmp = (struct tag_component *)component; + + cmp->tag = va_arg(args, char *); +} + +static void fdctr(gc_entity *entity, gc_scene *scene, void *component, node *n) +{ + struct tag_component *cmp = (struct tag_component *)component; + + cmp->tag = xml_getproperty(n, "tag"); + (void)scene; + (void)entity; +} + +static void dtr(void *component) +{ + struct tag_component *cmp = (struct tag_component *)component; + + free(cmp->tag); +} + +static char *serialize(void *component) +{ + (void)component; + return (NULL); +} + +const struct tag_component tag_component = { + base: { + name: "tag_component", + size: sizeof(struct tag_component), + dependencies: (char *[]){NULL}, + ctr: &ctr, + fdctr: &fdctr, + dtr: &dtr, + serialize: &serialize, + destroy: &component_destroy + }, + tag: NULL +}; \ No newline at end of file diff --git a/src/deserializer/deserialize_entity.c b/src/deserializer/deserialize_entity.c index d4ce4d0..5a9483b 100644 --- a/src/deserializer/deserialize_entity.c +++ b/src/deserializer/deserialize_entity.c @@ -7,7 +7,6 @@ #include #include "xml.h" -#include "read_line.h" #include "utility.h" #include "entity.h" #include "engine.h" diff --git a/src/deserializer/prefab.c b/src/deserializer/prefab.c index 7ddf4c0..423f45e 100644 --- a/src/deserializer/prefab.c +++ b/src/deserializer/prefab.c @@ -44,6 +44,7 @@ int prefab_loadentities(node *n, gc_engine *engine, gc_scene *scene) return (-1); scene->add_entity(scene, entity); } - + if (engine->on_resize && engine->get_screen_size && engine->scene) + engine->on_resize(engine, engine->get_screen_size(engine)); return (0); } \ No newline at end of file diff --git a/src/engine/engine_component_builder.c b/src/engine/engine_component_builder.c index 7923f4d..a223185 100644 --- a/src/engine/engine_component_builder.c +++ b/src/engine/engine_component_builder.c @@ -6,7 +6,6 @@ */ #include "engine.h" -#include "system.h" #include "components/movable_component.h" #include "components/parallax_component.h" #include "components/fixed_to_cam_component.h" @@ -21,6 +20,7 @@ #include "components/vertex_component.h" #include #include "components/clickable_component.h" +#include "components/tag_component.h" #include "components/input_component.h" void engine_add_component(gc_engine *engine, const void *component) @@ -47,4 +47,5 @@ void engine_add_buildin_components(gc_engine *engine) engine->add_component(engine, &clickable_component); engine->add_component(engine, &vertex_component); engine->add_component(engine, &input_component); + engine->add_component(engine, &tag_component); } \ No newline at end of file diff --git a/src/engine/engine_system_builder.c b/src/engine/engine_system_builder.c index a6cc765..8c086f6 100644 --- a/src/engine/engine_system_builder.c +++ b/src/engine/engine_system_builder.c @@ -15,7 +15,6 @@ #include "systems/collision_system.h" #include "systems/camerafollow_system.h" #include "sfml_renderer.h" -#include void engine_add_system(gc_engine *engine, const void *system) { diff --git a/src/entity/entity.c b/src/entity/entity.c index 18fac9a..0e8a1ce 100644 --- a/src/entity/entity.c +++ b/src/entity/entity.c @@ -53,11 +53,15 @@ char *entity_serialize(gc_entity *entity, int fd) return (NULL); } -static void destroy(gc_entity *entity) +static void destroy(gc_entity *entity, gc_scene *scene) { gc_component *next = NULL; for (gc_component *cmp = entity->components; cmp; cmp = next) { + for (gc_tupple *tup = scene->entities_by_cmp; tup; tup = tup->next) + if (!my_strcmp(tup->name, cmp->name)) + tup_remove(tup, entity->id); + LISTREM(scene->entities, entity); next = cmp->next; cmp->destroy(cmp); } diff --git a/src/scene/scene_destroy.c b/src/scene/scene_destroy.c index 7ef7f9d..da26520 100644 --- a/src/scene/scene_destroy.c +++ b/src/scene/scene_destroy.c @@ -32,7 +32,7 @@ void scene_destroy(gc_scene *scene) for (gc_list *entity = scene->entities; entity; entity = next) { next = entity->next; - ((gc_entity *)entity->data)->destroy(entity->data); + ((gc_entity *)entity->data)->destroy(entity->data, scene); free(entity); } free_data(scene); diff --git a/src/ui/button.c b/src/ui/button.c index 0fac4ff..2d6e893 100644 --- a/src/ui/button.c +++ b/src/ui/button.c @@ -14,6 +14,7 @@ #include "systems/sfml_renderer_system.h" #include "ui.h" #include +#include "components/tag_component.h" #include "components/input_component.h" gc_entity *background_from_text(gc_engine *engine, gc_scene *scene, node *n, \ @@ -58,6 +59,9 @@ gc_list *new_button(gc_engine *engine, gc_scene *scene, node *n) background->add_component(background, new_component(&input_component)); background->add_component(background, new_component(&clickable_component, scene, xml_getproperty(n, "click"))); + if (xml_hasproperty(n, "tag")) + background->add_component(background, new_component(&tag_component, + xml_getproperty(n, "tag"))); LISTADD(entities, background); LISTADD(entities, new_text(engine, scene, n)); return (entities); diff --git a/src/ui/setup_ui.c b/src/ui/setup_ui.c index 50ac471..5df3711 100644 --- a/src/ui/setup_ui.c +++ b/src/ui/setup_ui.c @@ -8,9 +8,11 @@ #include "components/transform_component.h" #include "components/renderer.h" #include "components/fixed_to_cam_component.h" +#include "components/tag_component.h" #include "systems/sfml_renderer_system.h" #include "ui.h" #include +#include "prefab.h" gc_entity *new_text(gc_engine *engine, gc_scene *scene, node *n) { @@ -21,11 +23,9 @@ gc_entity *new_text(gc_engine *engine, gc_scene *scene, node *n) else entity = entity_create(); entity->add_component(entity, new_component(&transform_component, - (gc_vector2){0, 0}, - (gc_vector2){0, 0})); + (gc_vector2){0, 0}, (gc_vector2){0, 0})); entity->add_component(entity, new_component(&renderer_component, - GC_TXTREND, - xml_getproperty(n, "text"), + GC_TXTREND, xml_getproperty(n, "text"), scene->get_data(scene, "font", NULL), xml_getintprop(n, "size"), xml_gettempprop(n, "color"), xml_getbool(n, "resize", true))); @@ -33,6 +33,9 @@ gc_entity *new_text(gc_engine *engine, gc_scene *scene, node *n) (gc_vector2){xml_getintprop(n, "x"),xml_getintprop(n, "y")}, xml_propcontains(n, "x", "%"), xml_propcontains(n, "y", "%"), 0, 0, false, false)); + if (xml_hasproperty(n, "tag")) + entity->add_component(entity, new_component(&tag_component, + xml_getproperty(n, "tag"))); return (entity); } @@ -52,6 +55,9 @@ gc_entity *new_sprite(gc_engine *engine, gc_scene *scene, node *n) xml_propcontains(n, "x", "%"), xml_propcontains(n, "y", "%"), xml_getintprop(n, "width"), xml_getintprop(n, "height"), xml_propcontains(n, "width", "%"), xml_propcontains(n, "height", "%"))); + if (xml_hasproperty(n, "tag")) + entity->add_component(entity, new_component(&tag_component, + xml_getproperty(n, "tag"))); return (entity); } @@ -72,8 +78,14 @@ gc_data *text_make(gc_engine *engine, gc_scene *scene, node *n) { gc_list *list = NULL; gc_data *data = malloc(sizeof(*data)); + gc_component *cmp; + gc_entity *txt = new_text(engine, scene, n); - LISTADD(list, new_text(engine, scene, n)); + LISTADD(list, txt); + for (n = n->child; n; n = n->next) { + cmp = deserialize_component(engine, txt, scene, n); + txt->add_component(txt, cmp); + } data->name = "text"; data->type = "ui"; data->destroy = NULL; diff --git a/src/utility/list.c b/src/utility/list.c index da5154a..fd2f7fb 100644 --- a/src/utility/list.c +++ b/src/utility/list.c @@ -26,4 +26,20 @@ gc_list *list_add(gc_list *list, void *obj) list->data = obj; list->next = NULL; return (listconst); +} + +gc_list *list_remove(gc_list *list, void *obj) +{ + gc_list *listconst = list; + + if (!list) + return (NULL); + if (list->data == obj) + return (list->next); + while (list->next && list->next->data != obj) + list = list->next; + if (!list->next) + return (listconst); + list->next = list->next->next; + return (listconst); } \ No newline at end of file