diff --git a/Makefile b/Makefile index f87aa9e..fd00872 100644 --- a/Makefile +++ b/Makefile @@ -15,6 +15,7 @@ SRC = src/engine/engine.c \ src/component.c \ src/components/movable_component.c \ src/components/position_component.c \ + src/components/texture_renderer.c \ src/scene/scene.c \ src/utility/my_strdup.c \ src/utility/my_strlen.c \ diff --git a/include/components/movable_component.h b/include/components/movable_component.h index 910690f..f651341 100644 --- a/include/components/movable_component.h +++ b/include/components/movable_component.h @@ -17,4 +17,4 @@ struct movable_component int jump_key; }; -const struct movable_component movable_component; \ No newline at end of file +extern const struct movable_component movable_component; \ No newline at end of file diff --git a/include/components/position_component.h b/include/components/position_component.h index b4cdb09..35eec23 100644 --- a/include/components/position_component.h +++ b/include/components/position_component.h @@ -13,7 +13,7 @@ struct position_component { gc_component base; - vector2 position; + gc_vector2 position; }; -const struct position_component position_component; \ No newline at end of file +extern const struct position_component position_component; \ No newline at end of file diff --git a/include/components/texture_renderer.h b/include/components/texture_renderer.h index caf00cc..608c7db 100644 --- a/include/components/texture_renderer.h +++ b/include/components/texture_renderer.h @@ -16,4 +16,4 @@ struct texture_renderer gc_texture *texture; }; -const struct texture_renderer texture_renderer; \ No newline at end of file +extern const struct texture_renderer texture_renderer; \ No newline at end of file diff --git a/include/engine.h b/include/engine.h index 5a1ccc3..2ae9536 100644 --- a/include/engine.h +++ b/include/engine.h @@ -9,6 +9,8 @@ typedef struct gc_engine gc_engine; #pragma once #include "scene.h" +#include "vector2.h" +#include "texture.h" #include #include @@ -18,8 +20,10 @@ 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); sfRenderWindow *window; + sfSprite *sprite; void (*draw)(gc_engine *engine); }; diff --git a/include/renderer.h b/include/renderer.h index 427d7a5..a27e1e2 100644 --- a/include/renderer.h +++ b/include/renderer.h @@ -10,4 +10,5 @@ #include "engine.h" void renderer_draw(gc_engine *engine); -gc_texture *texture_create(const char *path); \ No newline at end of file +gc_texture *texture_create(const char *path); +void renderer_draw_texture(gc_engine *engine, gc_texture *text, gc_vector2 pos); \ No newline at end of file diff --git a/include/system.h b/include/system.h index 169cfcb..9348edc 100644 --- a/include/system.h +++ b/include/system.h @@ -18,10 +18,10 @@ struct gc_system const char *name; const char *component_name; bool (*check_dependencies)(const gc_system *, const gc_entity *); - void (*update_entity)(const gc_entity *entity); + void (*update_entity)(gc_engine *engine, gc_entity *entity); }; -const gc_system *all_systems[2]; +extern const gc_system *all_systems[]; bool system_check_dependencies(const gc_system *sys, const gc_entity *entity); void system_destroy(void *system); \ No newline at end of file diff --git a/include/vector2.h b/include/vector2.h index 3d82ced..259eaef 100644 --- a/include/vector2.h +++ b/include/vector2.h @@ -7,8 +7,8 @@ #pragma once -typedef struct vector2 +typedef struct gc_vector2 { - int x; - int y; -} vector2; \ No newline at end of file + float x; + float y; +} gc_vector2; \ No newline at end of file diff --git a/src/component.c b/src/component.c index 6a88b36..dd02021 100644 --- a/src/component.c +++ b/src/component.c @@ -8,12 +8,14 @@ #include "component.h" #include "components/position_component.h" #include "components/movable_component.h" +#include "components/texture_renderer.h" #include "utility.h" #include const void *all_components[] = { &position_component, &movable_component, + &texture_renderer, NULL }; diff --git a/src/components/position_component.c b/src/components/position_component.c index be96d3a..4b07c3c 100644 --- a/src/components/position_component.c +++ b/src/components/position_component.c @@ -14,7 +14,7 @@ void position_ctr(void *component, va_list args) { struct position_component *cmp = (struct position_component *)component; - cmp->position = va_arg(args, vector2); + cmp->position = va_arg(args, gc_vector2); } void position_fdctr(gc_engine *engine, void *component, char *args) diff --git a/src/components/texture_renderer.c b/src/components/texture_renderer.c index b24e519..fcddc38 100644 --- a/src/components/texture_renderer.c +++ b/src/components/texture_renderer.c @@ -6,7 +6,7 @@ */ #include "engine.h" -#include "position_component.h" +#include "components/position_component.h" #include "components/texture_renderer.h" static void texture_rend_ctr(void *component, va_list args) @@ -20,7 +20,7 @@ static void texture_rend_fdctr(gc_engine *engine, void *component, char *args) { struct texture_renderer *cmp = (struct texture_renderer *)component; - cmp->texture = get_texture(engine->scene, args); + cmp->texture = get_texture(engine, args); } static void texture_rend_dtr(void *component) @@ -28,16 +28,17 @@ static void texture_rend_dtr(void *component) (void)component; } -static void texture_rend_serialize(void *component) +static char *texture_rend_serialize(void *component) { (void)component; + return (NULL); } const struct texture_renderer texture_renderer = { base: { name: "TextureRenderer", size: sizeof(struct texture_renderer), - dependencies: { &position_component, NULL }, + dependencies: (void *){ &position_component, NULL }, ctr: &texture_rend_ctr, fdctr: &texture_rend_fdctr, dtr: &texture_rend_dtr, @@ -47,4 +48,4 @@ const struct texture_renderer texture_renderer = { prev: NULL }, texture: NULL, -} \ No newline at end of file +}; \ No newline at end of file diff --git a/src/deserializer/deserialize_entity.c b/src/deserializer/deserialize_entity.c index 2a95427..6a048d7 100644 --- a/src/deserializer/deserialize_entity.c +++ b/src/deserializer/deserialize_entity.c @@ -18,7 +18,7 @@ gc_component *deserialize_component(gc_engine *engine, int fd) gc_component *cmp = NULL; char *args; - if (!component || my_strcmp(component, "")) + if (!component || !my_strcmp(component, "")) return (NULL); args = my_strchr(component, ' '); if (!args) diff --git a/src/engine/engine.c b/src/engine/engine.c index f143a35..7826fe6 100644 --- a/src/engine/engine.c +++ b/src/engine/engine.c @@ -11,11 +11,11 @@ #include #include -void update_entity(gc_entity *entity) +void update_entity(gc_engine *engine, gc_entity *entity) { for (int i = 0; all_systems[i]; i++) { if (all_systems[i]->check_dependencies(all_systems[i], entity)) - all_systems[i]->update_entity(entity); + all_systems[i]->update_entity(engine, entity); } } @@ -25,7 +25,7 @@ int game_loop(gc_engine *engine) handle_events(engine); for (gc_entity *entity = entities; entity; entity = entity->next) - update_entity(entity); + update_entity(engine, entity); engine->draw(engine); return (0); } @@ -36,11 +36,17 @@ gc_engine *engine_create(char *title) sfVideoMode mode = {800, 600, 32}; sfWindowStyle style = sfDefaultStyle; + if (!engine) + return (NULL); engine->window = sfRenderWindow_create(mode, title, style, NULL); + engine->sprite = sfSprite_create(); + if (!engine->window || !engine->sprite) + return (NULL); engine->is_open = &engine_is_open; engine->game_loop = &game_loop; engine->change_scene = &change_scene; engine->draw = &renderer_draw; + engine->draw_texture = &renderer_draw_texture; engine->scene = NULL; return (engine); } \ No newline at end of file diff --git a/src/entity/entity.c b/src/entity/entity.c index 8a58f33..e2b8205 100644 --- a/src/entity/entity.c +++ b/src/entity/entity.c @@ -10,7 +10,7 @@ #include #include -gc_component *entity_get_component(const gc_entity *entity, const char *name) +void *entity_get_component(const gc_entity *entity, const char *name) { for (gc_component *cmp = entity->components; cmp; cmp = cmp->next) { if (!my_strcmp(cmp->name, name)) diff --git a/src/renderer/renderer.c b/src/renderer/renderer.c index c251237..012953a 100644 --- a/src/renderer/renderer.c +++ b/src/renderer/renderer.c @@ -9,8 +9,17 @@ #include +void renderer_draw_texture(gc_engine *engine, gc_texture *text, gc_vector2 pos) +{ + if (!text) + return; + sfSprite_setTexture(engine->sprite, text->texture, true); + sfSprite_setPosition(engine->sprite, (sfVector2f){pos.x, pos.y}); + sfRenderWindow_drawSprite(engine->window, engine->sprite, NULL); +} + void renderer_draw(gc_engine *engine) { - sfRenderWindow_clear(engine->window, sfBlack); sfRenderWindow_display(engine->window); + sfRenderWindow_clear(engine->window, sfBlack); } \ No newline at end of file diff --git a/src/renderer/texture_utility.c b/src/renderer/texture_utility.c index e4a6a6e..64b8d6e 100644 --- a/src/renderer/texture_utility.c +++ b/src/renderer/texture_utility.c @@ -14,8 +14,11 @@ gc_texture *get_texture(gc_engine *engine, char *name) { - gc_texture **textures = engine->scene->textures; + gc_texture **textures; + if (!engine->scene) + return (NULL); + textures = engine->scene->textures; for (int i = 0; textures[i]; i++) { if (!my_strcmp(textures[i]->name, name)) return (textures[i]); diff --git a/src/systems/texture_renderer_system.c b/src/systems/texture_renderer_system.c index 02b5cb6..f245809 100644 --- a/src/systems/texture_renderer_system.c +++ b/src/systems/texture_renderer_system.c @@ -7,11 +7,20 @@ #include "entity.h" #include "system.h" +#include "texture.h" +#include "vector2.h" #include "systems/texture_renderer_system.h" +#include "components/position_component.h" +#include "components/texture_renderer.h" -void tex_rend_update_entity(const gc_entity *entity) +void tex_rend_update_entity(gc_engine *engine, gc_entity *entity) { - struct position_component *pos = (struct position_component *)entity->get_component(entity, "PositionComponent"); + struct position_component *pos = \ +(struct position_component *)entity->get_component(entity, "PositionComponent"); + struct texture_renderer *text = (\ +struct texture_renderer *)entity->get_component(entity, "TextureRenderer"); + + engine->draw_texture(engine, text->texture, pos->position); } const gc_system texture_renderer_system = { diff --git a/src/utility/intparser.c b/src/utility/intparser.c index d688762..a7f1c19 100644 --- a/src/utility/intparser.c +++ b/src/utility/intparser.c @@ -28,6 +28,7 @@ int parse_end(char **str, int strlen, int start, int mult) return (0); *str = *str + start; nbr = get_nbr(str, strlen) * mult; + *str += 1; if (nbr > 2147483647 || nbr < -2147483648) return (0); return (nbr);