commit b11a19a95615ea5f64222e37c184c81d5397fc98 Author: AnonymusRaccoon Date: Tue Feb 18 14:11:52 2020 +0100 Initial diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..92e62ae --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,43 @@ +name: CI + +on: [push] + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v1 + with: + submodules: true + - name: Run a one-line script + run: | + mkdir -p ~/.ssh + echo -ne $SSH_PRIVATE_KEY >> ~/.ssh/blih + chmod 400 ~/.ssh/blih + find . -regex '.*\/\.git.*' -delete + ls -lRa + echo -ne "Host git.epitech.eu\n\tHostname git.epitech.eu\n\tUser git\n\tIdentityFile $(readlink -f ~/.ssh/blih)\n" >> ~/.ssh/config + git config --global user.email "tristan.roux@epitech.eu" + git config --global user.name "Tristan Roux" + ssh-keyscan git.epitech.eu >> ~/.ssh/known_hosts + git clone git@git.epitech.eu:/tristan.roux@epitech.eu/MUL_my_runner_2019 /tmp/blihRepo + echo cloneDone + export REPOVAR=`pwd` + cd /tmp/blihRepo + echo cdDone + find . -not -path "./.git*" -delete + echo findDone + cp -r $REPOVAR/* . + echo cpDone + ls -lRa + git add --all + echo addDone + git commit -m "Sync" + echo Commid + git push origin master + echo Pushed + env: + SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }} + TEST: ${{ secrets.TEST }} diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..c44da6a --- /dev/null +++ b/.gitmodules @@ -0,0 +1,9 @@ +[submodule "Gamacon"] + path = lib/gamacon + url = https://github.com/AnonymusRaccoon/Gamacon +[submodule "lib/quadtree"] + path = lib/quadtree + url = https://github.com/AnonymusRaccoon/Quadtree +[submodule "lib/xmlparser"] + path = lib/xmlparser + url = https://github.com/AnonymusRaccoon/xmlParser diff --git a/lib/gamacon/.gitignore b/lib/gamacon/.gitignore new file mode 100644 index 0000000..d48d625 --- /dev/null +++ b/lib/gamacon/.gitignore @@ -0,0 +1,4 @@ +*.o +libgamacon.a +*.gcda +*.gcno \ No newline at end of file diff --git a/lib/gamacon/Makefile b/lib/gamacon/Makefile new file mode 100644 index 0000000..2ab9b7f --- /dev/null +++ b/lib/gamacon/Makefile @@ -0,0 +1,105 @@ +## +## EPITECH PROJECT, 2019 +## Gamacon +## File description: +## Makefile +## + +SRC = src/engine/engine.c \ + src/engine/engine_internal.c \ + src/engine/discard_player.c \ + src/engine/engine_dataloader.c \ + src/entity/entity.c \ + src/entity/entity_factory.c \ + src/component.c \ + src/components/movable_component.c \ + src/components/collision_component.c \ + src/components/transform_component.c \ + src/components/renderer.c \ + src/components/camera_follow.c \ + src/components/fixed_to_cam.c \ + src/components/parallax_component.c \ + src/components/controllable_component.c \ + src/components/friction_component.c \ + src/components/friction_giver.c \ + src/components/controllers/keyboard_controller.c \ + src/components/renderers/sprite_renderer.c \ + src/components/renderers/anim_renderer.c \ + src/components/renderers/text_renderer.c \ + src/scene/scene.c \ + src/scene/scene_loader.c \ + src/scene/scene_destroy.c \ + src/utility/arraylen.c \ + src/utility/list.c \ + src/utility/tupple.c \ + src/deserializer/deserialize_entity.c \ + src/deserializer/prefab.c \ + src/system.c \ + src/systems/sfml_renderer_system.c \ + src/systems/movable_system.c \ + src/systems/camera_follow_system.c \ + src/systems/collision_system.c \ + src/systems/parallax_system.c \ + src/systems/controllers/keyboard_controller_system.c \ + src/systems/friction_system.c \ + src/systems/fixed_to_cam_pseudosystem.c \ + src/engine/engine_system_builder.c \ + src/engine/engine_component_builder.c \ + src/utility/vector2.c \ + src/utility/my_realloc.c \ + src/sfml_renderer/sfml_dataloaders.c \ + src/sfml_renderer/sfml_music_player.c \ + src/sfml_renderer/texture_utility.c \ + src/sfml_renderer/sfml_drawer.c \ + src/sfml_renderer/sfml_functions.c + +OBJ = $(SRC:%.c=%.o) + +GCDA = *.gcda + +GCNO = *.gcno + +TESTS = tests/deserializations.c \ + tests/game_loop.c + +INCLUDE = -I ./include + +CFLAGS = $(INCLUDE) -Wall -Wshadow -Wextra + +LDFLAGS = -L ../my -L ../xmlparser -L ../quadtree\ +-lxmlparser -lquadtree -lmy -lcsfml-system -lcsfml-graphics -lcsfml-window -lm + +COVERAGE = --coverage -lcriterion + +NAME = libgamacon.a + +UT = ./ut + +CC = gcc + +AR = ar rc + +all: build + +build: $(OBJ) + $(AR) $(NAME) $(OBJ) + +tests_run: + $(CC) -o $(UT) $(SRC) $(TESTS) $(COVERAGE) $(CFLAGS) $(LDFLAGS) + $(UT) + +clean: + $(RM) $(OBJ) + $(RM) $(GCDA) + $(RM) $(GCNO) + +fclean: clean + $(RM) $(NAME) + $(RM) $(UT) + +re: fclean all + +dbg: CFLAGS += -g +dbg: re + +.PHONY: all build clean fclean \ No newline at end of file diff --git a/lib/gamacon/include/component.h b/lib/gamacon/include/component.h new file mode 100644 index 0000000..690fd89 --- /dev/null +++ b/lib/gamacon/include/component.h @@ -0,0 +1,36 @@ +/* +** EPITECH PROJECT, 2019 +** Gamacon +** File description: +** component +*/ + +typedef struct gc_component gc_component; + +#pragma once + +#include +#include "engine.h" +#include "entity.h" + +struct gc_component +{ + char *name; + unsigned size; + char **dependencies; + void (*ctr)(void *component, va_list); + void (*fdctr)(gc_entity *entity, gc_scene *scene, void *component, node *n); + void (*dtr)(void *component); + char *(*serialize)(void *component); + void (*destroy)(void *component); + + gc_component *next; + gc_component *prev; +}; + +void *new_component(const void *component, ...); +void component_destroy(void *component); +gc_component *component_remove(gc_component *cmp, const char *name); + +#define GETCMP(x) ((struct x *)entity->get_component(entity, #x)) +#define GETCOLCMP(x) ((struct x *)entity_get_component(entity_get(engine->scene, id), #x)) \ No newline at end of file diff --git a/lib/gamacon/include/components/camerafollow_component.h b/lib/gamacon/include/components/camerafollow_component.h new file mode 100644 index 0000000..11d50a9 --- /dev/null +++ b/lib/gamacon/include/components/camerafollow_component.h @@ -0,0 +1,12 @@ +/* +** EPITECH PROJECT, 2019 +** Gamacon +** File description: +** movable_component +*/ + +#pragma once + +#include "component.h" + +extern const gc_component camerafollow_component; \ No newline at end of file diff --git a/lib/gamacon/include/components/collision_component.h b/lib/gamacon/include/components/collision_component.h new file mode 100644 index 0000000..a1145da --- /dev/null +++ b/lib/gamacon/include/components/collision_component.h @@ -0,0 +1,30 @@ +/* +** EPITECH PROJECT, 2020 +** Twac +** File description: +** collision_component +*/ + +#pragma once + +#include "engine.h" +#include "entity.h" +#include + +struct collision_component +{ + gc_component base; + float distance_down; + float distance_top; + float distance_left; + float distance_right; + int layer; + void (**on_collide)(gc_engine *engine, gc_entity *entity, int id); + int collide_size; +}; + +typedef void (*collide_listener)(gc_engine *, gc_entity *, int); + +void add_on_collide(struct collision_component *, collide_listener); + +extern const struct collision_component collision_component; \ No newline at end of file diff --git a/lib/gamacon/include/components/controllable_component.h b/lib/gamacon/include/components/controllable_component.h new file mode 100644 index 0000000..0498b49 --- /dev/null +++ b/lib/gamacon/include/components/controllable_component.h @@ -0,0 +1,21 @@ +/* +** EPITECH PROJECT, 2019 +** Gamacon +** File description: +** movable_component +*/ + +#pragma once + +#include "component.h" +#include + +struct controllable_component +{ + gc_component base; + bool moving_left; + bool moving_right; + bool jumping; +}; + +extern const struct controllable_component controllable_component; \ No newline at end of file diff --git a/lib/gamacon/include/components/controllers/keyboard_controller.h b/lib/gamacon/include/components/controllers/keyboard_controller.h new file mode 100644 index 0000000..294fee0 --- /dev/null +++ b/lib/gamacon/include/components/controllers/keyboard_controller.h @@ -0,0 +1,21 @@ +/* +** EPITECH PROJECT, 2019 +** Gamacon +** File description: +** keyboard_component +*/ + +#pragma once + +#include "component.h" +#include + +struct keyboard_controller +{ + gc_component base; + int left_key; + int right_key; + int jump_key; +}; + +extern const struct keyboard_controller keyboard_controller; \ No newline at end of file diff --git a/lib/gamacon/include/components/fixed_to_cam_component.h b/lib/gamacon/include/components/fixed_to_cam_component.h new file mode 100644 index 0000000..9e2d794 --- /dev/null +++ b/lib/gamacon/include/components/fixed_to_cam_component.h @@ -0,0 +1,19 @@ +/* +** EPITECH PROJECT, 2019 +** Gamacon +** File description: +** movable_component +*/ + +#pragma once + +#include "component.h" +#include "vector2.h" + +struct fixed_to_cam +{ + gc_component base; + gc_vector2 offset; +}; + +extern const struct fixed_to_cam fixed_to_cam; \ No newline at end of file diff --git a/lib/gamacon/include/components/friction_component.h b/lib/gamacon/include/components/friction_component.h new file mode 100644 index 0000000..2afa367 --- /dev/null +++ b/lib/gamacon/include/components/friction_component.h @@ -0,0 +1,19 @@ +/* +** EPITECH PROJECT, 2019 +** Gamacon +** File description: +** gravity_component +*/ + +#pragma once + +#include "component.h" + +struct friction_component +{ + gc_component base; + int value; + int default_value; +}; + +extern const struct friction_component friction_component; \ No newline at end of file diff --git a/lib/gamacon/include/components/friction_giver.h b/lib/gamacon/include/components/friction_giver.h new file mode 100644 index 0000000..5b584b0 --- /dev/null +++ b/lib/gamacon/include/components/friction_giver.h @@ -0,0 +1,18 @@ +/* +** EPITECH PROJECT, 2020 +** Twac +** File description: +** friction_giver +*/ + +#pragma once + +#include "component.h" + +struct friction_giver +{ + gc_component base; + int value; +}; + +extern const struct friction_giver friction_giver; \ No newline at end of file diff --git a/lib/gamacon/include/components/movable_component.h b/lib/gamacon/include/components/movable_component.h new file mode 100644 index 0000000..f2a3866 --- /dev/null +++ b/lib/gamacon/include/components/movable_component.h @@ -0,0 +1,20 @@ +/* +** EPITECH PROJECT, 2019 +** Gamacon +** File description: +** movable_component +*/ + +#pragma once + +#include "component.h" +#include + +struct movable_component +{ + gc_component base; + gc_vector2 acceleration; + gc_vector2 velocity; +}; + +extern const struct movable_component movable_component; \ No newline at end of file diff --git a/lib/gamacon/include/components/parallax_component.h b/lib/gamacon/include/components/parallax_component.h new file mode 100644 index 0000000..21cb07f --- /dev/null +++ b/lib/gamacon/include/components/parallax_component.h @@ -0,0 +1,19 @@ +/* +** EPITECH PROJECT, 2019 +** Gamacon +** File description: +** parallax_component +*/ + +#pragma once + +#include "component.h" + +struct parallax_component +{ + gc_component base; + gc_vector2 old_pos; + float speed; +}; + +extern const struct parallax_component parallax_component; \ No newline at end of file diff --git a/lib/gamacon/include/components/renderer.h b/lib/gamacon/include/components/renderer.h new file mode 100644 index 0000000..7b25f30 --- /dev/null +++ b/lib/gamacon/include/components/renderer.h @@ -0,0 +1,37 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** texture_renderer +*/ + +#pragma once + +#include "component.h" +#include "vector2.h" + +typedef enum GC_TEXTURETYPE { + GC_NONE, + GC_TEXTUREREND, + GC_ANIMREND, + GC_TXTREND +} GC_TEXTURETYPE; + +struct renderer +{ + gc_component base; + enum GC_TEXTURETYPE type; + void *data; +}; + +void sprite_ctr(struct renderer *cmp, va_list args); +void sprite_fdctr(gc_scene *scene, struct renderer *cmp, node *n); + +void anim_ctr(struct renderer *cmp, va_list args); +void anim_fdctr(gc_scene *scene, struct renderer *cmp, node *n); +void rend_set_anim(struct renderer *rend, const char *name); + +void text_ctr(struct renderer *cmp, va_list args); +void text_fdctr(gc_scene *scene, struct renderer *cmp, node *n); + +extern const struct renderer renderer_component; \ No newline at end of file diff --git a/lib/gamacon/include/components/transform_component.h b/lib/gamacon/include/components/transform_component.h new file mode 100644 index 0000000..675a37f --- /dev/null +++ b/lib/gamacon/include/components/transform_component.h @@ -0,0 +1,20 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** position_component +*/ + +#pragma once + +#include "component.h" +#include "vector2.h" + +struct transform_component +{ + gc_component base; + gc_vector2 position; + gc_vector2 size; +}; + +extern const struct transform_component transform_component; \ No newline at end of file diff --git a/lib/gamacon/include/data.h b/lib/gamacon/include/data.h new file mode 100644 index 0000000..497d2a6 --- /dev/null +++ b/lib/gamacon/include/data.h @@ -0,0 +1,27 @@ +/* +** EPITECH PROJECT, 2020 +** Twac +** File description: +** data +*/ + +#pragma once + +#include "xml.h" + +typedef struct gc_data gc_data; +struct gc_data +{ + char *type; + char *name; + void *custom; + void (*destroy)(gc_data *data); +}; + +typedef int (*gc_loader)(gc_data *data, node *n); + +typedef struct gc_dataloader +{ + char *type; + gc_loader load; +} gc_dataloader; \ No newline at end of file diff --git a/lib/gamacon/include/engine.h b/lib/gamacon/include/engine.h new file mode 100644 index 0000000..8b1ee46 --- /dev/null +++ b/lib/gamacon/include/engine.h @@ -0,0 +1,71 @@ +/* +** EPITECH PROJECT, 2019 +** Gamacon +** File description: +** engine +*/ +typedef struct gc_engine gc_engine; + +#pragma once + +#include "xml.h" +#include "scene.h" +#include "vector2.h" +#include "system.h" +#include "list.h" +#include "data.h" +#include + +struct gc_engine +{ + gc_scene *scene; + bool should_close; + bool (*is_open)(gc_engine *engine); + bool (*has_focus)(gc_engine *engine); + bool (*is_keypressed)(int key); + void (*handle_events)(gc_engine *engine); + int (*game_loop)(gc_engine *engine, float dtime); + void (*draw)(gc_engine *engine); + int (*change_scene)(gc_engine *engine, gc_scene *scene); + void (*destroy)(gc_engine *engine); + + gc_list *systems; + void (*add_system)(gc_engine *engine, const void *system); + void *(*get_system)(gc_engine *engine, const char *name); + void (*finish_physics)(gc_engine *engine); + + gc_list *components; + void (*add_component)(gc_engine *engine, const void *component); + const void *(*get_component)(gc_engine *engine, const char *name); + + void (*play_music)(void *music); + void (*stop_music)(gc_engine *engine); + + gc_list *dataloaders; + void (*add_dataloader)(gc_engine *engine, char *type, gc_loader loader); +}; + +gc_engine *engine_create(void); +bool engine_is_open(gc_engine *engine); +bool engine_has_focus(gc_engine *engine); +bool engine_is_keypressed(int key); +void handle_events(gc_engine *engine); +void engine_draw(gc_engine *engine); +void engine_play_music(void *music); +void engine_stop_music(gc_engine *engine); + +int change_scene(gc_engine *engine, gc_scene *scene); + +void engine_add_buildin_systems(gc_engine *engine); +void *engine_get_system(gc_engine *engine, const char *name); +void engine_add_system(gc_engine *engine, const void *system); + +void engine_add_buildin_components(gc_engine *engine); +const void *engine_get_component(gc_engine *engine, const char *name); +void engine_add_component(gc_engine *engine, const void *component); + +void engine_add_dataloader(gc_engine *engine, char *type, gc_loader loader); + +int engine_use_sfml(gc_engine *engine, const char *title, int framerate); + +#define GETSYS(x) ((struct x *)engine->get_system(engine, #x)) \ No newline at end of file diff --git a/lib/gamacon/include/entity.h b/lib/gamacon/include/entity.h new file mode 100644 index 0000000..27ed8c3 --- /dev/null +++ b/lib/gamacon/include/entity.h @@ -0,0 +1,37 @@ +/* +** EPITECH PROJECT, 2019 +** Gamacon +** File description: +** entity +*/ + +typedef struct gc_entity gc_entity; + +#pragma once + +#include "component.h" +#include "vector2.h" +#include + +struct gc_entity +{ + int id; + gc_component *components; + gc_entity *(*add_component)(gc_entity *entity, void *component); + void *(*get_component)(const gc_entity *entity, const char *name); + bool (*has_component)(const gc_entity *entity, const char *name); + 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); +}; + +gc_entity *entity_create(void); +gc_entity *entity_create_with_id(int id); +gc_entity *entity_get(gc_scene *scene, int id); +int entity_add(gc_scene *scene, gc_entity *entity); +gc_entity *entity_add_component(gc_entity *entity, void *component); +void *entity_get_component(const gc_entity *entity, const char *name); +void entity_remove_component(const gc_scene *scene, const gc_entity *entity, \ +const char *name); +char *entity_serialize(gc_entity *entity, int fd); \ No newline at end of file diff --git a/lib/gamacon/include/entity_factory.h b/lib/gamacon/include/entity_factory.h new file mode 100644 index 0000000..c35a2e6 --- /dev/null +++ b/lib/gamacon/include/entity_factory.h @@ -0,0 +1,9 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** entity_factory +*/ + +#pragma once + diff --git a/lib/gamacon/include/list.h b/lib/gamacon/include/list.h new file mode 100644 index 0000000..1a5f3fb --- /dev/null +++ b/lib/gamacon/include/list.h @@ -0,0 +1,18 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** list +*/ + +typedef struct gc_list gc_list; + +#pragma once + +struct gc_list +{ + void *data; + gc_list *next; +}; + +gc_list *list_add(gc_list *list, void *obj); \ No newline at end of file diff --git a/lib/gamacon/include/my.h b/lib/gamacon/include/my.h new file mode 100644 index 0000000..dc4a2d5 --- /dev/null +++ b/lib/gamacon/include/my.h @@ -0,0 +1,125 @@ +/* +** EPITECH PROJECT, 2019 +** My lib +** File description: +** Header file +*/ +#pragma once + +int my_str_islower_or_num(const char *str); + +int my_printf(const char *str, ...); + +int count_valid_queens_placements(int n); + +char *my_strchr(const char *str, char c); + +int my_compute_power_it(int n, int p); + +int my_compute_power_rec(int n, int p); + +int my_compute_factorial_it(int n); + +int my_compute_factorial_rec(int n); + +int my_compute_square_root(int n); + +char *my_evil_str(char *str); + +int my_find_prime_sup(int n); + +int my_getnbr_base(const char *str, const char *base); + +int my_getnbr(const char *str); + +int my_isneg(int n); + +int my_is_prime(int n); + +void my_print_alpha(void); + +void my_print_comb2(void); + +void my_print_combn(int n); + +void my_print_comb(void); + +void my_print_digits(void); + +void my_print_revalpha(void); + +void my_putchar(char c); + +void my_putlong_base(long n, const char *base); + +void my_putnbr_base(int n, const char *base); + +void my_put_nbr(int n); + +void my_putstr(const char *str); + +void my_revstr(char *str); + +void my_showmem(const char *str); + +void my_showstr(const char *str); + +void my_sort_int_array(int *array); + +int is_letter(char c); + +int is_alpha(char c); + +int is_num(char c); + +int is_digit(char c); + +char *my_strcapitalize(char *str); + +char *my_strcat(char *dest, const char *src); + +int my_strcmp(const char *s1, const char *s2); + +char *my_strcpy(const char *str); + +int my_str_isalpha(const char *str); + +int is_lowercase(char c); + +int my_str_islower(const char *str); + +int my_str_isnum(const char *str); + +int is_printable(char c); +int my_str_isprintable(const char *str); + +int is_upper(char c); +int my_str_isupper(const char *str); + +int my_strlen(const char *str); + +int my_strlowcase(const char *str); + +char *my_strncat(char *dest, const char *src, int n); + +int my_strncmp(const char *s1, const char *s2, int n); + +char *my_strncpy(char *dest, const char *src, int n); + +char *my_strstr(const char *str, const char *to_find); + +int my_strupcase(const char *str); + +void my_swap(int *a, int *b); + +char *my_strdup(const char *str); + +char **my_str_to_word_array(const char *str); + +int my_show_word_array(const char **words); + +int is_alphanum(char c); + +int first_alphanum(const char *str); + +int index_of(const char *str, char c); \ No newline at end of file diff --git a/lib/gamacon/include/prefab.h b/lib/gamacon/include/prefab.h new file mode 100644 index 0000000..50d91f7 --- /dev/null +++ b/lib/gamacon/include/prefab.h @@ -0,0 +1,14 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** prefab +*/ + +#pragma once + +#include "entity.h" + +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 diff --git a/lib/gamacon/include/quadtree.h b/lib/gamacon/include/quadtree.h new file mode 100644 index 0000000..64d3d07 --- /dev/null +++ b/lib/gamacon/include/quadtree.h @@ -0,0 +1,54 @@ +/* +** EPITECH PROJECT, 2019 +** quadtree +** File description: +** quadtree +*/ + +#pragma once + +#include + +#define MAXCOL 1024 +#define MAX_ENTITY 20 + +typedef struct quadtree quadtree; + +typedef struct qt_intrect +{ + float x; + float y; + int h; + int w; +} qt_intrect; + +typedef struct qt_object +{ + int id; + qt_intrect rect; + int layer; +} qt_object; + +typedef struct qt_collision +{ + float distance_left; + float distance_right; + float distance_top; + float distance_down; + int *collide_with; +} qt_collision; + +struct quadtree +{ + qt_intrect rect; + int capacity; + void *objects; +}; + +quadtree *qt_create(qt_intrect rect, int capacity); +int qt_add(quadtree *tree, qt_object obj); +qt_collision collision_get_info(quadtree *tree, int entity_id); +bool qt_collide(qt_intrect r1, qt_intrect r2); +qt_object *qt_getobj(quadtree *tree, int id); +int qt_update(quadtree *tree, qt_object obj); +void qt_destroy(quadtree *tree); \ No newline at end of file diff --git a/lib/gamacon/include/read_line.h b/lib/gamacon/include/read_line.h new file mode 100644 index 0000000..ca47c6b --- /dev/null +++ b/lib/gamacon/include/read_line.h @@ -0,0 +1,12 @@ +/* +** EPITECH PROJECT, 2019 +** Read line +** File description: +** read_file +*/ + +#pragma once + +#define READ_SIZE 1000 + +char *read_line(int fd); \ No newline at end of file diff --git a/lib/gamacon/include/scene.h b/lib/gamacon/include/scene.h new file mode 100644 index 0000000..40dca58 --- /dev/null +++ b/lib/gamacon/include/scene.h @@ -0,0 +1,36 @@ +/* +** EPITECH PROJECT, 2019 +** Gamacon +** File description: +** scene +*/ +typedef struct gc_scene gc_scene; + +#pragma once + +#include "engine.h" +#include "entity.h" +#include "list.h" +#include "tupple.h" +#include + +struct gc_scene +{ + gc_list *data; + void *(*get_data)(gc_scene *scene, const char *type, const char *name); + + void (*destroy)(gc_scene *scene); + + gc_list *entities; + gc_tupple *entities_by_cmp; + int (*add_entity)(gc_scene *scene, gc_entity *entity); + gc_entity *(*get_entity)(gc_scene *scene, int id); + gc_list *(*get_entity_by_cmp)(gc_scene *scene, const char *cmp_name); +}; + +gc_scene *scene_create(gc_engine *engine, const char *mappath); +int scene_add_entity(gc_scene *scene, gc_entity *entity); +void scene_load_data(gc_engine *engine, gc_scene *scene, node *n); +void *scene_get_data(gc_scene *scene, const char *type, const char *name); +void scene_destroy(gc_scene *scene); +int scene_load_musics(gc_scene *scene, node *n); \ No newline at end of file diff --git a/lib/gamacon/include/sfml_renderer.h b/lib/gamacon/include/sfml_renderer.h new file mode 100644 index 0000000..b283ccf --- /dev/null +++ b/lib/gamacon/include/sfml_renderer.h @@ -0,0 +1,43 @@ +/* +** EPITECH PROJECT, 2020 +** Twac +** File description: +** sfml_renderer +*/ + +#pragma once + +#include +#include "scene.h" +#include "sprite.h" +#include "text.h" +#include "components/transform_component.h" +#include "systems/sfml_renderer_system.h" + +int sfml_music_loader(gc_data *data, node *n); +int sfml_sprite_loader(gc_data *data, node *n); +int sfml_font_loader(gc_data *data, node *n); + +sfTexture *get_texture(gc_scene *scene, char *name); + +void sfmlrenderer_draw_texture(struct sfml_renderer_system *renderer, \ +struct transform_component *tra, gc_sprite *sprite); +void sfmlrenderer_draw_anim(struct sfml_renderer_system *renderer, \ +struct transform_component *tra, gc_animholder *holder, float dtime); +void sfmlrenderer_draw_txt(struct sfml_renderer_system *renderer, \ +struct transform_component *tra, gc_text *txt); + +void sfml_texture_destroy(gc_data *data); +void sfml_music_destroy(gc_data *data); +void sfml_font_destroy(gc_data *data); + +bool sfml_is_open(gc_engine *engine); +bool sfml_has_focus(gc_engine *engine); +bool sfml_is_keypressed(int key); +void sfml_handle_events(gc_engine *engine); +void sfml_draw(gc_engine *engine); +void sfml_play_music(void *music); +void sfml_stop_music(gc_engine *engine); +void entities_update_to_cam(gc_scene *scene, \ +struct sfml_renderer_system *renderer, struct camerafollow_system *cam); +void entities_update_to_cam_size(gc_scene *scene, gc_vector2 size); diff --git a/lib/gamacon/include/sprite.h b/lib/gamacon/include/sprite.h new file mode 100644 index 0000000..51325a7 --- /dev/null +++ b/lib/gamacon/include/sprite.h @@ -0,0 +1,42 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** sprite +*/ + +#pragma once + +#include "vector2.h" +#include + +typedef struct gc_int_rect +{ + float height; + float width; + float top; + float left; +} gc_int_rect; + +typedef struct gc_sprite { + void *texture; + gc_int_rect rect; + gc_vector2 pos; + gc_vector2 scale; +} gc_sprite; + +typedef struct gc_anim { + char *name; + int frame_count; + float frame_rate; + gc_int_rect rect; +} gc_anim; + +typedef struct gc_animholder { + gc_sprite *sprite; + gc_anim *anims; + int animcount; + + gc_anim *current; + float timesince_up; +} gc_animholder; \ No newline at end of file diff --git a/lib/gamacon/include/system.h b/lib/gamacon/include/system.h new file mode 100644 index 0000000..1f3306b --- /dev/null +++ b/lib/gamacon/include/system.h @@ -0,0 +1,30 @@ +/* +** EPITECH PROJECT, 2019 +** Gamacon +** File description: +** system +*/ + +typedef struct gc_system gc_system; + +#pragma once + +#include "entity.h" +#include +#include + +struct gc_system +{ + const char *name; + const char *component_name; + size_t size; + void (*ctr)(void *system, va_list list); + void (*dtr)(void *system); + bool (*check_dependencies)(const gc_system *, const gc_entity *); + void (*update_entity)(gc_engine *, void *system, gc_entity *, float dtime); + void (*destroy)(void *system); +}; + +bool system_check_dependencies(const gc_system *sys, const gc_entity *entity); +void *new_system(const void *system, ...); +void system_destroy(void *system); \ No newline at end of file diff --git a/lib/gamacon/include/systems/camerafollow_system.h b/lib/gamacon/include/systems/camerafollow_system.h new file mode 100644 index 0000000..d6246ef --- /dev/null +++ b/lib/gamacon/include/systems/camerafollow_system.h @@ -0,0 +1,18 @@ +/* +** EPITECH PROJECT, 2019 +** Gamacon +** File description: +** movable_component +*/ + +#pragma once + +#include "component.h" + +struct camerafollow_system +{ + gc_system base; + gc_vector2 cam_pos; +}; + +extern const struct camerafollow_system camerafollow_system; \ No newline at end of file diff --git a/lib/gamacon/include/systems/collision_system.h b/lib/gamacon/include/systems/collision_system.h new file mode 100644 index 0000000..bbf567e --- /dev/null +++ b/lib/gamacon/include/systems/collision_system.h @@ -0,0 +1,20 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** movable_system +*/ + +#pragma once + +#include "system.h" +#include "quadtree.h" + +typedef struct gc_collision_system +{ + gc_system base; + quadtree *tree; + +} gc_collision_system; + +extern const gc_collision_system collision_system; \ No newline at end of file diff --git a/lib/gamacon/include/systems/controllers/keyboard_controller_system.h b/lib/gamacon/include/systems/controllers/keyboard_controller_system.h new file mode 100644 index 0000000..4ea1f3a --- /dev/null +++ b/lib/gamacon/include/systems/controllers/keyboard_controller_system.h @@ -0,0 +1,12 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** texture_renderer_system +*/ + +#pragma once + +#include "system.h" + +extern const gc_system keyboard_controller_system; \ No newline at end of file diff --git a/lib/gamacon/include/systems/friction_system.h b/lib/gamacon/include/systems/friction_system.h new file mode 100644 index 0000000..224fa4f --- /dev/null +++ b/lib/gamacon/include/systems/friction_system.h @@ -0,0 +1,12 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** texture_renderer_system +*/ + +#pragma once + +#include "system.h" + +extern const gc_system friction_system; \ No newline at end of file diff --git a/lib/gamacon/include/systems/movable_system.h b/lib/gamacon/include/systems/movable_system.h new file mode 100644 index 0000000..19d105d --- /dev/null +++ b/lib/gamacon/include/systems/movable_system.h @@ -0,0 +1,13 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** movable_system +*/ + +#pragma once + +#include "system.h" +#include "quadtree.h" + +extern const gc_system movable_system; \ No newline at end of file diff --git a/lib/gamacon/include/systems/parallax_system.h b/lib/gamacon/include/systems/parallax_system.h new file mode 100644 index 0000000..f614f52 --- /dev/null +++ b/lib/gamacon/include/systems/parallax_system.h @@ -0,0 +1,12 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** texture_renderer_system +*/ + +#pragma once + +#include "system.h" + +extern const gc_system parallax_system; \ No newline at end of file diff --git a/lib/gamacon/include/systems/sfml_renderer_system.h b/lib/gamacon/include/systems/sfml_renderer_system.h new file mode 100644 index 0000000..cea8cd0 --- /dev/null +++ b/lib/gamacon/include/systems/sfml_renderer_system.h @@ -0,0 +1,28 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** texture_renderer_system +*/ + +#pragma once + +#include "system.h" +#include +#include "scene.h" +#include "systems/sfml_renderer_system.h" +#include "systems/camerafollow_system.h" + +struct sfml_renderer_system +{ + gc_system system; + sfRenderWindow *window; + sfSprite *sprite; + sfText *text; + sfView *view; +}; + +gc_system *gc_new_sfml_renderer(gc_engine *engine, \ +const char *title, int framerate); + +extern const struct sfml_renderer_system sfml_renderer; \ No newline at end of file diff --git a/lib/gamacon/include/tags.h b/lib/gamacon/include/tags.h new file mode 100644 index 0000000..718d73f --- /dev/null +++ b/lib/gamacon/include/tags.h @@ -0,0 +1,13 @@ +/* +** EPITECH PROJECT, 2019 +** Gamacon +** File description: +** tag +*/ + +#pragma once + +typedef enum tag +{ + none = 0 +} tag; \ No newline at end of file diff --git a/lib/gamacon/include/text.h b/lib/gamacon/include/text.h new file mode 100644 index 0000000..330f861 --- /dev/null +++ b/lib/gamacon/include/text.h @@ -0,0 +1,14 @@ +/* +** EPITECH PROJECT, 2020 +** Twac +** File description: +** text +*/ + +#pragma once + +typedef struct gc_text +{ + char *text; + void *font; +} gc_text; \ No newline at end of file diff --git a/lib/gamacon/include/tupple.h b/lib/gamacon/include/tupple.h new file mode 100644 index 0000000..c300c6c --- /dev/null +++ b/lib/gamacon/include/tupple.h @@ -0,0 +1,23 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** tupple +*/ + +typedef struct gc_tupple gc_tupple; + +#pragma once + +#include "list.h" +#include "entity.h" + +struct gc_tupple +{ + char *name; + gc_list *entities; + gc_tupple *next; +}; + +gc_tupple *tupple_add(gc_tupple *list, const char *name, gc_entity *entity); +void tup_remove(gc_tupple *tup, int id); \ No newline at end of file diff --git a/lib/gamacon/include/utility.h b/lib/gamacon/include/utility.h new file mode 100644 index 0000000..f5fa19e --- /dev/null +++ b/lib/gamacon/include/utility.h @@ -0,0 +1,38 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** utility +*/ + +#pragma once + +#include + +char *my_strdup(const char *src); +int my_printf(const char *fmt, ...); +int my_strlen(const char *str); +int arraylen(const char **array); +char *tostr(int n); +int my_atoi(const char *str); +int my_strcmp(const char *str1, const char *str2); +int my_strncmp(const char *str1, const char *str2, int n); +char *my_strchr(const char *str, char c); +int parse_arg_int(char **str); +float parse_arg_float(char **str); +char *parse_arg_str(char **str); +int my_pow(int nb, int p); +int my_sqrt(int nb); +void *my_realloc(void *oldptr, size_t oldsize, size_t newsize); + +#define MIN(x, y) ((x) < (y) ? (x) : (y)) +#define MAX(x, y) ((x) > (y) ? (x) : (y)) + +#define CLAMP(x, y) (((x) > (y)) ? ((x) = (y)) : (x)) +#define NCLAMP(x, y) (((x) < (y)) ? ((x) = (y)) : (x)) +#define ABSCLAMP(x, y) (((x) > 0) ? CLAMP((x), (y)) : NCLAMP((x), -(y))) + +#define GETSIGN(x) (((x) < 0) ? (-1) : (1)) +#define SET_SIGN(x, s) (x = (x) * (s) > 0 ? (x) : ((x) * (-1))) + +#define ABS(x) ((x) > 0 ? (x) : -(x)) \ No newline at end of file diff --git a/lib/gamacon/include/vector2.h b/lib/gamacon/include/vector2.h new file mode 100644 index 0000000..7071ed4 --- /dev/null +++ b/lib/gamacon/include/vector2.h @@ -0,0 +1,17 @@ +/* +** EPITECH PROJECT, 2019 +** Gamacon +** File description: +** vector2 +*/ + +#pragma once + +typedef struct gc_vector2 +{ + float x; + float y; +} gc_vector2; + +float gcvector_magnitude(gc_vector2 vec); +gc_vector2 gcvector2_normilize(gc_vector2 vec); \ No newline at end of file diff --git a/lib/gamacon/include/xml.h b/lib/gamacon/include/xml.h new file mode 100644 index 0000000..389ddb3 --- /dev/null +++ b/lib/gamacon/include/xml.h @@ -0,0 +1,44 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** xml +*/ + +typedef struct node node; +typedef struct dictionary dictionary; + +#pragma once + +#include +#include + +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); +bool xml_hasproperty(node *n, const char *key); +char *xml_getproperty(node *n, const char *key); +char *xml_gettempprop(node *n, const char *key); +int xml_getintprop(node *n, const char *key); +int xml_getbinaprop(node *n, const char *key); +int xml_gethexaprop(node *n, const char *key); +float xml_getfloatprop(node *n, const char *key); +int xml_getchildcount(node *n); +int xml_getchildcount_filtered(node *n, char *name); +void xml_destroy(node *n); \ No newline at end of file diff --git a/lib/gamacon/src/component.c b/lib/gamacon/src/component.c new file mode 100644 index 0000000..1764083 --- /dev/null +++ b/lib/gamacon/src/component.c @@ -0,0 +1,62 @@ +/* +** EPITECH PROJECT, 2019 +** Gamacon +** File description: +** component +*/ + +#include "component.h" +#include "components/transform_component.h" +#include "components/movable_component.h" +#include "components/renderer.h" +#include "components/parallax_component.h" +#include "utility.h" +#include + +const void *engine_get_component(gc_engine *engine, const char *name) +{ + for (gc_list *cmp = engine->components; cmp; cmp = cmp->next) { + if (!my_strcmp(((const gc_component *)cmp->data)->name, name)) + return (cmp->data); + } + return (NULL); +} + +void *new_component(const void *component, ...) +{ + gc_component *base = (gc_component *)component; + va_list args; + void *new_cmp = malloc(base->size); + + if (!new_cmp) + return (NULL); + *(gc_component *)new_cmp = *base; + if (((gc_component *)new_cmp)->ctr) { + va_start(args, component); + ((gc_component *)new_cmp)->ctr(new_cmp, args); + va_end(args); + } + return (new_cmp); +} + +void component_destroy(void *component) +{ + gc_component *cmp = (gc_component *)component; + if (cmp->dtr) + cmp->dtr(component); + free(component); +} + +gc_component *component_remove(gc_component *cmp, const char *name) +{ + for (; cmp; cmp = cmp->next) { + if (my_strcmp(cmp->name, name)) + continue; + if (cmp->prev) + cmp->prev->next = cmp->next; + if (cmp->next) + cmp->next->prev = cmp->prev; + return (cmp); + } + return (NULL); +} \ No newline at end of file diff --git a/lib/gamacon/src/components/camera_follow.c b/lib/gamacon/src/components/camera_follow.c new file mode 100644 index 0000000..2e58aea --- /dev/null +++ b/lib/gamacon/src/components/camera_follow.c @@ -0,0 +1,52 @@ +/* +** EPITECH PROJECT, 2020 +** Twac +** File description: +** camera_follow +*/ + +#include "xml.h" +#include "component.h" +#include "components/controllable_component.h" +#include "components/camerafollow_component.h" +#include "utility.h" +#include + +static void ctr(void *component, va_list args) +{ + (void)component; + (void)args; +} + +static void fdctr(gc_entity *entity, gc_scene *scene, void *component, node *n) +{ + (void)scene; + (void)entity; + (void)component; + (void)n; +} + +static void dtr(void *component) +{ + (void)component; +} + +static char *serialize(void *component) +{ + (void)component; + return (NULL); +} + +const gc_component camerafollow_component = { + name: "camerafollow_component", + size: sizeof(struct gc_component), + dependencies: (char *[]){ + "transform_component", + NULL + }, + ctr: &ctr, + fdctr: &fdctr, + dtr: &dtr, + serialize: &serialize, + destroy: &component_destroy +}; \ No newline at end of file diff --git a/lib/gamacon/src/components/collision_component.c b/lib/gamacon/src/components/collision_component.c new file mode 100644 index 0000000..f05e2ce --- /dev/null +++ b/lib/gamacon/src/components/collision_component.c @@ -0,0 +1,79 @@ +/* +** EPITECH PROJECT, 2020 +** Twac +** File description: +** collision_component +*/ + +#include "xml.h" +#include "component.h" +#include "components/collision_component.h" +#include "utility.h" +#include + +static void ctr(void *component, va_list args) +{ + struct collision_component *cmp = (struct collision_component *)component; + + cmp->distance_down = 1024; + cmp->distance_top = 1024; + cmp->distance_left = 1024; + cmp->distance_right = 1024; + cmp->layer = va_arg(args, int); + cmp->on_collide = NULL; + cmp->collide_size = 0; +} + +static void fdctr(gc_entity *entity, gc_scene *scene, void *component, node *n) +{ + struct collision_component *cmp = (struct collision_component *)component; + cmp->distance_down = 1024; + cmp->distance_top = 1024; + cmp->distance_left = 1024; + cmp->distance_right = 1024; + cmp->layer = xml_getbinaprop(n, "layer"); + cmp->on_collide = NULL; + cmp->collide_size = 0; + (void)scene; + (void)entity; +} + +static void dtr(void *component) +{ + struct collision_component *cmp = (struct collision_component *)component; + + if (cmp->on_collide) + free(cmp->on_collide); +} + +static char *serialize(void *component) +{ + (void)component; + return (NULL); +} + +void add_on_collide(struct collision_component *col, collide_listener list) +{ + int old = col->collide_size; + + col->collide_size += sizeof(list); + col->on_collide = my_realloc(col->on_collide, old, col->collide_size); + if (!col->on_collide) { + col->collide_size = 0; + return; + } + col->on_collide[old / sizeof(void (*)())] = list; +} + +const struct collision_component collision_component = { + base: { + name: "collision_component", + size: sizeof(struct collision_component), + dependencies: (char *[]){"collision_component", NULL}, + ctr: &ctr, + fdctr: &fdctr, + dtr: &dtr, + serialize: &serialize, + destroy: &component_destroy + } +}; \ No newline at end of file diff --git a/lib/gamacon/src/components/controllable_component.c b/lib/gamacon/src/components/controllable_component.c new file mode 100644 index 0000000..62fa2fc --- /dev/null +++ b/lib/gamacon/src/components/controllable_component.c @@ -0,0 +1,68 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** controllable_component +*/ + +#include "xml.h" +#include "component.h" +#include "components/controllable_component.h" +#include "utility.h" +#include + +static void controllable_ctr(void *component, va_list args) +{ + struct controllable_component *cmp = (struct controllable_component *)\ +component; + + cmp->moving_left = false; + cmp->moving_right = false; + cmp->jumping = false; + (void)args; +} + +static void controllable_fdctr(gc_entity *entity, gc_scene *scene, \ +void *component, node *n) +{ + struct controllable_component *cmp = (struct controllable_component *)\ +component; + + cmp->moving_left = false; + cmp->moving_right = false; + cmp->jumping = false; + (void)scene; + (void)entity; + (void)n; +} + +static void controllable_dtr(void *component) +{ + (void)component; +} + +static char *controllable_serialize(void *component) +{ + (void)component; + return (NULL); +} + +const struct controllable_component controllable_component = { + base: { + name: "controllable_component", + size: sizeof(struct controllable_component), + dependencies: (char *[]){ + "movable_component", + "transform_component", + NULL + }, + ctr: &controllable_ctr, + fdctr: &controllable_fdctr, + dtr: &controllable_dtr, + serialize: &controllable_serialize, + destroy: &component_destroy + }, + moving_left: false, + moving_right: false, + jumping: false +}; \ No newline at end of file diff --git a/lib/gamacon/src/components/controllers/keyboard_controller.c b/lib/gamacon/src/components/controllers/keyboard_controller.c new file mode 100644 index 0000000..9e07f2a --- /dev/null +++ b/lib/gamacon/src/components/controllers/keyboard_controller.c @@ -0,0 +1,68 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** keyboard_controller +*/ + + +#include "xml.h" +#include "component.h" +#include "components/controllable_component.h" +#include "components/controllers/keyboard_controller.h" +#include "utility.h" +#include + +static void ctr(void *component, va_list args) +{ + struct keyboard_controller *cmp = (struct keyboard_controller *)\ +component; + + cmp->left_key = va_arg(args, int); + cmp->right_key = va_arg(args, int); + cmp->jump_key = va_arg(args, int); +} + +static void fdctr(gc_entity *entity, gc_scene *scene, void *component, node *n) +{ + struct keyboard_controller *cmp = (struct keyboard_controller *)\ +component; + + cmp->left_key = xml_getintprop(n, "left"); + cmp->right_key = xml_getintprop(n, "right"); + cmp->jump_key = xml_getintprop(n, "jump"); + (void)scene; + (void)entity; +} + +static void dtr(void *component) +{ + (void)component; +} + +static char *serialize(void *component) +{ + (void)component; + return (NULL); +} + +const struct keyboard_controller keyboard_controller = { + base: { + name: "keyboard_controller", + size: sizeof(struct keyboard_controller), + dependencies: (char *[]){ + "movable_component", + "transform_component", + "controllable_component", + NULL + }, + ctr: &ctr, + fdctr: &fdctr, + dtr: &dtr, + serialize: &serialize, + destroy: &component_destroy + }, + left_key: 16, + right_key: 3, + jump_key: 57 +}; \ No newline at end of file diff --git a/lib/gamacon/src/components/fixed_to_cam.c b/lib/gamacon/src/components/fixed_to_cam.c new file mode 100644 index 0000000..7bcf6ab --- /dev/null +++ b/lib/gamacon/src/components/fixed_to_cam.c @@ -0,0 +1,53 @@ +/* +** EPITECH PROJECT, 2020 +** Twac +** File description: +** camera_follow +*/ + +#include "xml.h" +#include "component.h" +#include "components/fixed_to_cam_component.h" +#include "utility.h" +#include + +static void ctr(void *component, va_list args) +{ + struct fixed_to_cam *cmp = (struct fixed_to_cam *)component; + + cmp->offset = va_arg(args, gc_vector2); +} + +static void fdctr(gc_entity *entity, gc_scene *scene, void *component, node *n) +{ + struct fixed_to_cam *cmp = (struct fixed_to_cam *)component; + + n = xml_getnode(n, "Position"); + cmp->offset.x = xml_getintprop(n, "x"); + cmp->offset.y = xml_getintprop(n, "y"); + (void)scene; + (void)entity; +} + +static char *serialize(void *component) +{ + (void)component; + return (NULL); +} + +const struct fixed_to_cam fixed_to_cam = { + base: { + name: "fixed_to_cam", + size: sizeof(struct fixed_to_cam), + dependencies: (char *[]){ + "transform_component", + NULL + }, + ctr: &ctr, + fdctr: &fdctr, + dtr: NULL, + serialize: &serialize, + destroy: &component_destroy + }, + offset: (gc_vector2){0, 0} +}; \ No newline at end of file diff --git a/lib/gamacon/src/components/friction_component.c b/lib/gamacon/src/components/friction_component.c new file mode 100644 index 0000000..5a14b63 --- /dev/null +++ b/lib/gamacon/src/components/friction_component.c @@ -0,0 +1,75 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** friction_component +*/ + +#include "xml.h" +#include "component.h" +#include "components/friction_component.h" +#include "components/friction_giver.h" +#include "components/collision_component.h" +#include "utility.h" +#include + +static void on_collide(gc_engine *engine, gc_entity *entity, int id) +{ + struct friction_component *fric = GETCMP(friction_component); + struct friction_giver *f = GETCOLCMP(friction_giver); + + if (!f) + return; + fric->value = f->value; +} + +static void ctr(void *component, va_list args) +{ + struct friction_component *cmp = (struct friction_component *)component; + + cmp->value = va_arg(args, int); + cmp->default_value = cmp->value; +} + +static void fdctr(gc_entity *entity, gc_scene *scene, void *component, node *n) +{ + struct friction_component *cmp = (struct friction_component *)component; + struct collision_component *col = GETCMP(collision_component); + + cmp->value = xml_getfloatprop(n, "value"); + cmp->default_value = cmp->value; + if (!col) + return ((void)my_printf("Collision not yet setup, you should place the \ +friction component after the collision component.\n")); + add_on_collide(col, &on_collide); + (void)scene; +} + +static void dtr(void *component) +{ + (void)component; +} + +static char *serialize(void *component) +{ + (void)component; + return (NULL); +} + +const struct friction_component friction_component = { + base: { + name: "friction_component", + size: sizeof(struct friction_component), + dependencies: (char *[]){ + "movable_component", + "transform_component", + NULL + }, + ctr: &ctr, + fdctr: &fdctr, + dtr: &dtr, + serialize: &serialize, + destroy: &component_destroy + }, + value: 10 +}; \ No newline at end of file diff --git a/lib/gamacon/src/components/friction_giver.c b/lib/gamacon/src/components/friction_giver.c new file mode 100644 index 0000000..6b2358d --- /dev/null +++ b/lib/gamacon/src/components/friction_giver.c @@ -0,0 +1,56 @@ +/* +** EPITECH PROJECT, 2020 +** Twac +** File description: +** friction_giver +*/ + +#include "xml.h" +#include "entity.h" +#include "scene.h" +#include "components/friction_giver.h" + +static void ctr(void *component, va_list args) +{ + struct friction_giver *cmp = (struct friction_giver *)component; + + cmp->value = va_arg(args, double); +} + +static void fdctr(gc_entity *entity, gc_scene *scene, void *component, node *n) +{ + struct friction_giver *cmp = (struct friction_giver *)component; + + cmp->value = xml_getfloatprop(n, "value"); + (void)scene; + (void)entity; +} + +static void dtr(void *component) +{ + (void)component; +} + +static char *serialize(void *component) +{ + (void)component; + return (NULL); +} + +const struct friction_giver friction_giver = { + base: { + name: "friction_giver", + size: sizeof(struct friction_giver), + dependencies: (char *[]){ + "transform_component", + "friction_giver", + NULL + }, + ctr: &ctr, + fdctr: &fdctr, + dtr: &dtr, + serialize: &serialize, + destroy: &component_destroy + }, + value: 10 +}; \ No newline at end of file diff --git a/lib/gamacon/src/components/movable_component.c b/lib/gamacon/src/components/movable_component.c new file mode 100644 index 0000000..60f28ff --- /dev/null +++ b/lib/gamacon/src/components/movable_component.c @@ -0,0 +1,59 @@ +/* +** EPITECH PROJECT, 2019 +** Gamacon +** File description: +** movable_component +*/ + +#include "xml.h" +#include "component.h" +#include "components/movable_component.h" +#include "utility.h" +#include + +static void ctr(void *component, va_list args) +{ + struct movable_component *cmp = (struct movable_component *)component; + + cmp->acceleration.x = 0; + cmp->acceleration.y = 0; + cmp->velocity.x = 0; + cmp->velocity.y = 0; + (void)args; +} + +static void fdctr(gc_entity *entity, gc_scene *scene, void *component, node *n) +{ + (void)component; + (void)n; + (void)scene; + (void)entity; +} + +static void dtr(void *component) +{ + (void)component; +} + +static char *serialize(void *component) +{ + (void)component; + return (NULL); +} + +const struct movable_component movable_component = { + base: { + name: "movable_component", + size: sizeof(struct movable_component), + dependencies: (char *[]){ + "transform_component", + "collision_component", + NULL + }, + ctr: &ctr, + fdctr: &fdctr, + dtr: &dtr, + serialize: &serialize, + destroy: &component_destroy + } +}; \ No newline at end of file diff --git a/lib/gamacon/src/components/parallax_component.c b/lib/gamacon/src/components/parallax_component.c new file mode 100644 index 0000000..1e32346 --- /dev/null +++ b/lib/gamacon/src/components/parallax_component.c @@ -0,0 +1,53 @@ +/* +** 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 ctr(void *component, va_list args) +{ + struct parallax_component *cmp = (struct parallax_component *)component; + + cmp->speed = va_arg(args, double); + cmp->old_pos = (gc_vector2){0, 0}; +} + +static void fdctr(gc_entity *entity, gc_scene *scene, void *component, node *n) +{ + struct parallax_component *cmp = (struct parallax_component *)component; + + cmp->speed = xml_getfloatprop(n, "speed"); + cmp->old_pos = (gc_vector2){0, 0}; + (void)scene; + (void)entity; +} + +static void dtr(void *component) +{ + (void)component; +} + +static char *serialize(void *component) +{ + (void)component; + return (NULL); +} + +const struct parallax_component parallax_component = { + base: { + name: "parallax_component", + size: sizeof(struct parallax_component), + dependencies: (char *[]){"transform_component", "renderer", NULL}, + ctr: &ctr, + fdctr: &fdctr, + dtr: &dtr, + serialize: &serialize, + destroy: &component_destroy + }, + speed: 0.5 +}; \ No newline at end of file diff --git a/lib/gamacon/src/components/renderer.c b/lib/gamacon/src/components/renderer.c new file mode 100644 index 0000000..6118a41 --- /dev/null +++ b/lib/gamacon/src/components/renderer.c @@ -0,0 +1,97 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** texture_renderer +*/ + +#include "engine.h" +#include "xml.h" +#include "utility.h" +#include "sprite.h" +#include "text.h" +#include "components/transform_component.h" +#include "components/renderer.h" +#include + +static void ctr(void *component, va_list args) +{ + struct renderer *cmp = (struct renderer *)component; + GC_TEXTURETYPE type = va_arg(args, GC_TEXTURETYPE); + + cmp->data = NULL; + if (type == GC_TEXTUREREND) + sprite_ctr(cmp, args); + if (type == GC_ANIMREND) + anim_ctr(cmp, args); + if (type == GC_TXTREND) + text_ctr(cmp, args); +} + +GC_TEXTURETYPE renderer_get_type(node *n) +{ + if (xml_getnode(n, "animation")) + return (GC_ANIMREND); + if (xml_hasproperty(n, "text")) + return (GC_TXTREND); + return (GC_TEXTUREREND); +} + +static void fdctr(gc_entity *entity, gc_scene *scene, void *component, node *n) +{ + struct renderer *cmp = (struct renderer *)component; + GC_TEXTURETYPE type = renderer_get_type(n); + + if (type == GC_TEXTUREREND) + sprite_fdctr(scene, cmp, n); + if (type == GC_ANIMREND) + anim_fdctr(scene, cmp, n); + if (type == GC_TXTREND) + text_fdctr(scene, cmp, n); + (void)entity; +} + +static void dtr(void *component) +{ + struct renderer *cmp = (struct renderer *)component; + + switch (cmp->type) { + case GC_ANIMREND: + for (int i = 0; i < ((gc_animholder *)cmp->data)->animcount; i++) { + if (my_strcmp(((gc_animholder *)cmp->data)->anims[i].name, "none")) + free(((gc_animholder *)cmp->data)->anims[i].name); + } + free(((gc_animholder *)cmp->data)->sprite); + free(((gc_animholder *)cmp->data)->anims); + break; + case GC_TXTREND: + free(((gc_text *)cmp->data)->text); + break; + default: + break; + } + free(cmp->data); +} + +static char *serialize(void *component) +{ + (void)component; + return (NULL); +} + +const struct renderer renderer_component = { + base: { + name: "renderer", + size: sizeof(struct renderer), + dependencies: (char *[]){"transform_component", NULL}, + ctr: &ctr, + fdctr: &fdctr, + dtr: &dtr, + serialize: &serialize, + destroy: &component_destroy, + next: NULL, + prev: NULL + }, + data: NULL, + type: GC_TEXTUREREND +}; \ No newline at end of file diff --git a/lib/gamacon/src/components/renderers/anim_renderer.c b/lib/gamacon/src/components/renderers/anim_renderer.c new file mode 100644 index 0000000..66f6d49 --- /dev/null +++ b/lib/gamacon/src/components/renderers/anim_renderer.c @@ -0,0 +1,102 @@ +/* +** EPITECH PROJECT, 2020 +** Twac +** File description: +** anim_renderer +*/ + +#include "components/renderer.h" +#include "vector2.h" +#include "sprite.h" +#include "xml.h" +#include "my.h" +#include +#include + +void rend_set_anim(struct renderer *rend, const char *name) +{ + gc_animholder *holder = (gc_animholder *)rend->data; + + if (holder->current && !my_strcmp(holder->current->name, name)) + return; + for (int i = 0; i < holder->animcount; i++) { + if (!my_strcmp(holder->anims[i].name, name)) { + holder->current = &holder->anims[i]; + holder->sprite->rect = holder->anims[i].rect; + return; + } + } + my_printf("Gamacon: unknown animation %s.\n", name); +} + +void anim_ctr(struct renderer *cmp, va_list args) +{ + sfVector2u size; + gc_sprite *sprite = malloc(sizeof(gc_sprite)); + + if (!sprite) + return; + cmp->data = sprite; + sprite->texture = va_arg(args, sfTexture *); + sprite->rect = va_arg(args, gc_int_rect); + if (sprite->texture && sprite->rect.height < 0) { + size = sfTexture_getSize(sprite->texture); + sprite->rect.height = (float)size.y; + sprite->rect.width = (float)size.x; + } +} + +void animation_fdctr(gc_anim *anim, gc_sprite *sprite, node *n) +{ + node *rect = xml_getnode(n, "Rect"); + int tmp; + + anim->name = xml_getproperty(n, "name"); + anim->frame_count = xml_getintprop(n, "frame_count"); + anim->frame_rate = xml_getfloatprop(n, "frame_rate"); + anim->rect.height = sprite->rect.height; + anim->rect.width = sprite->rect.width; + anim->rect.top = sprite->rect.top; + anim->rect.left = sprite->rect.left; + if ((tmp = xml_getfloatprop(rect, "height")) != 0) + anim->rect.height = tmp; + if ((tmp = xml_getfloatprop(rect, "width")) != 0) + anim->rect.width = tmp; + if ((tmp = xml_getfloatprop(rect, "top")) != 0) + anim->rect.top = tmp; + if ((tmp = xml_getfloatprop(rect, "left")) != 0) + anim->rect.left = tmp; +} + +void animation_setnone(gc_anim *anim, gc_sprite *sprite) +{ + anim->name = "none"; + anim->frame_count = 1; + anim->frame_rate = 0; + anim->rect = sprite->rect; +} + +void anim_fdctr(gc_scene *scene, struct renderer *cmp, node *n) +{ + gc_animholder *hold = malloc(sizeof(gc_animholder)); + int animcount = xml_getchildcount_filtered(n, "animation") + 1; + int i = 1; + + hold->anims = malloc(sizeof(gc_anim) * animcount); + if (!hold || !hold->anims) + return; + sprite_fdctr(scene, cmp, n); + hold->sprite = (gc_sprite *)cmp->data; + hold->current = NULL; + hold->animcount = animcount; + hold->timesince_up = 0; + cmp->data = hold; + animation_setnone(&hold->anims[0], hold->sprite); + for (n = n->child; n; n = n->next) { + if (my_strcmp(n->name, "animation")) + continue; + animation_fdctr(&hold->anims[i], hold->sprite, n); + i++; + } + cmp->type = GC_ANIMREND; +} \ No newline at end of file diff --git a/lib/gamacon/src/components/renderers/sprite_renderer.c b/lib/gamacon/src/components/renderers/sprite_renderer.c new file mode 100644 index 0000000..7447380 --- /dev/null +++ b/lib/gamacon/src/components/renderers/sprite_renderer.c @@ -0,0 +1,56 @@ +/* +** EPITECH PROJECT, 2020 +** Twac +** File description: +** sprite_renderer +*/ + +#include "components/renderer.h" +#include "vector2.h" +#include "xml.h" +#include "sprite.h" +#include "sfml_renderer.h" +#include +#include + +void sprite_ctr(struct renderer *cmp, va_list args) +{ + sfVector2u size; + gc_sprite *sprite; + + sprite = malloc(sizeof(gc_sprite)); + if (!sprite) + return; + cmp->data = sprite; + sprite->texture = va_arg(args, sfTexture *); + sprite->rect = va_arg(args, gc_int_rect); + if (sprite->texture && sprite->rect.height < 0) { + size = sfTexture_getSize(sprite->texture); + sprite->rect.height = (float)size.y; + sprite->rect.width = (float)size.x; + } + sprite->scale = (gc_vector2){1, 1}; +} + +void sprite_fdctr(gc_scene *scene, struct renderer *cmp, node *n) +{ + node *rect = xml_getnode(n, "Rect"); + sfVector2u size; + gc_sprite *sprite = malloc(sizeof(gc_sprite)); + + cmp->data = sprite; + if (!cmp->data) + return; + sprite->texture = get_texture(scene, xml_gettempprop(n, "src")); + sprite->rect.height = xml_getfloatprop(rect, "height"); + sprite->rect.width = xml_getfloatprop(rect, "width"); + sprite->rect.top = xml_getfloatprop(rect, "top"); + sprite->rect.left = xml_getfloatprop(rect, "left"); + if (sprite->texture && sprite->rect.height < 0) { + size = sfTexture_getSize(sprite->texture); + sprite->rect.height = (float)size.y; + sprite->rect.width = (float)size.x; + } + sprite->scale = (gc_vector2){1, 1}; + cmp->type = GC_TEXTUREREND; +} diff --git a/lib/gamacon/src/components/renderers/text_renderer.c b/lib/gamacon/src/components/renderers/text_renderer.c new file mode 100644 index 0000000..c12c725 --- /dev/null +++ b/lib/gamacon/src/components/renderers/text_renderer.c @@ -0,0 +1,40 @@ +/* +** EPITECH PROJECT, 2020 +** Twac +** File description: +** sprite_renderer +*/ + +#include "components/renderer.h" +#include "vector2.h" +#include "xml.h" +#include "sprite.h" +#include "text.h" +#include +#include + +void text_ctr(struct renderer *cmp, va_list args) +{ + gc_text *gctext = malloc(sizeof(gc_text)); + + if (!gctext) + return; + cmp->data = gctext; + gctext->text = va_arg(args, char *); + gctext->font = va_arg(args, sfFont *); + cmp->type = GC_TXTREND; +} + +void text_fdctr(gc_scene *scene, struct renderer *cmp, node *n) +{ + gc_text *gctext = malloc(sizeof(gc_text)); + + if (!gctext) + return; + cmp->data = gctext; + gctext->text = xml_getproperty(n, "text"); + if (!gctext->text) + return; + gctext->font = scene->get_data(scene, "font", xml_getproperty(n, "src")); + cmp->type = GC_TXTREND; +} diff --git a/lib/gamacon/src/components/transform_component.c b/lib/gamacon/src/components/transform_component.c new file mode 100644 index 0000000..0ee67dc --- /dev/null +++ b/lib/gamacon/src/components/transform_component.c @@ -0,0 +1,65 @@ +/* +** 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 + +static void 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); +} + +static void fdctr(gc_entity *entity, gc_scene *scene, 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)scene; + (void)entity; +} + +static char *serialize(void *component) +{ + (void)component; + return (NULL); +} + +const struct transform_component transform_component = { + base: { + name: "transform_component", + size: sizeof(struct transform_component), + dependencies: (char *[]){NULL}, + ctr: &ctr, + fdctr: &fdctr, + dtr: NULL, + serialize: &serialize, + destroy: &component_destroy + }, + position: {0, 0}, + size: {0, 0} +}; \ No newline at end of file diff --git a/lib/gamacon/src/deserializer/deserialize_entity.c b/lib/gamacon/src/deserializer/deserialize_entity.c new file mode 100644 index 0000000..d4ce4d0 --- /dev/null +++ b/lib/gamacon/src/deserializer/deserialize_entity.c @@ -0,0 +1,48 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** deserializer +*/ + +#include +#include "xml.h" +#include "read_line.h" +#include "utility.h" +#include "entity.h" +#include "engine.h" + +gc_component *deserialize_component(gc_engine *engine, gc_entity *entity, \ +gc_scene *scene, node *n) +{ + const void *model = engine->get_component(engine, n->name); + gc_component *cmp = NULL; + + if (!model) { + my_printf("Couldn't find a component with the name: %s\n", n->name); + return (NULL); + } + cmp = new_component(model, 0, 0, 0, 0, 0, 0, 0); + if (cmp->fdctr) + cmp->fdctr(entity, scene, cmp, n); + return (cmp); +} + +gc_entity *deserialize_entity(gc_engine *engine, gc_scene *scene, node *n) +{ + gc_entity *entity = NULL; + gc_component *cmp = NULL; + int id = xml_getintprop(n, "id"); + + if (id > 0) + entity = entity_create_with_id(id); + else + entity = entity_create(); + if (!entity) + return (NULL); + for (node *cmp_n = n->child; cmp_n; cmp_n = cmp_n->next) { + cmp = deserialize_component(engine, entity, scene, cmp_n); + entity->add_component(entity, cmp); + } + return (entity); +} \ No newline at end of file diff --git a/lib/gamacon/src/deserializer/prefab.c b/lib/gamacon/src/deserializer/prefab.c new file mode 100644 index 0000000..a9b7c19 --- /dev/null +++ b/lib/gamacon/src/deserializer/prefab.c @@ -0,0 +1,44 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** prefab +*/ + +#include "xml.h" +#include "engine.h" +#include "entity.h" +#include "prefab.h" +#include "read_line.h" +#include "utility.h" +#include +#include + +int prefab_load(gc_engine *engine, const char *path) +{ + node *n; + + if (!engine || !engine->scene) + return (-1); + n = xml_parse(path); + if (!n || prefab_loadentities(n, engine, engine->scene) < 0) + return (-1); + xml_destroy(n); + return (0); +} + +int prefab_loadentities(node *n, gc_engine *engine, gc_scene *scene) +{ + gc_entity *entity; + + n = xml_getnode(n, "gc_entities"); + if (!n) + return (-1); + for (node *ent_n = n->child; ent_n; ent_n = ent_n->next) { + entity = deserialize_entity(engine, scene, ent_n); + if (!entity) + return (-1); + scene->add_entity(scene, entity); + } + return (0); +} \ No newline at end of file diff --git a/lib/gamacon/src/engine/discard_player.c b/lib/gamacon/src/engine/discard_player.c new file mode 100644 index 0000000..33dbecd --- /dev/null +++ b/lib/gamacon/src/engine/discard_player.c @@ -0,0 +1,18 @@ +/* +** EPITECH PROJECT, 2020 +** Twac +** File description: +** discard_player +*/ + +#include "engine.h" + +void engine_play_music(void *music) +{ + (void)music; +} + +void engine_stop_music(gc_engine *engine) +{ + (void)engine; +} \ No newline at end of file diff --git a/lib/gamacon/src/engine/engine.c b/lib/gamacon/src/engine/engine.c new file mode 100644 index 0000000..948a491 --- /dev/null +++ b/lib/gamacon/src/engine/engine.c @@ -0,0 +1,89 @@ +/* +** EPITECH PROJECT, 2019 +** Gamacon +** File description: +** engine +*/ + +#include "engine.h" +#include "system.h" +#include "utility.h" +#include +#include +#include + +void update_system(gc_engine *engine, gc_system *sys, float dtime) +{ + gc_scene *scene = engine->scene; + gc_list *entities; + + if (!scene) + return; + entities = scene->get_entity_by_cmp(scene, sys->component_name); + for (gc_list *entity = entities; entity; entity = entity->next) { + if (sys->check_dependencies(sys, entity->data)) + sys->update_entity(engine, sys, entity->data, dtime); + else + my_printf("Entity %d does not have all the required components \ +for the system %s\n", ((gc_entity *)entity->data)->id, sys->name); + } +} + +int game_loop(gc_engine *engine, float dtime) +{ + if (!engine->has_focus(engine)) + return (0); + engine->handle_events(engine); + for (gc_list *sys = engine->systems; sys; sys = sys->next) + update_system(engine, sys->data, dtime); + engine->draw(engine); + return (0); +} + +void engine_destroy(gc_engine *engine) +{ + gc_list *next; + + if (engine->scene) + engine->scene->destroy(engine->scene); + for (gc_list *dl = engine->dataloaders; dl; dl = next) { + next = dl->next; + free(dl->data); + free(dl); + } + for (gc_list *system = engine->systems; system; system = next) { + next = system->next; + ((gc_system *)system->data)->destroy(system->data); + free(system); + } + for (gc_list *cmp = engine->components; cmp; cmp = next) { + next = cmp->next; + free(cmp); + } + free(engine); +} + +gc_engine *engine_create(void) +{ + gc_engine *engine = malloc(sizeof(gc_engine)); + + if (!engine) + return (NULL); + engine->scene = NULL; + engine->should_close = false; + engine->is_open = &engine_is_open; + engine->has_focus = &engine_has_focus; + engine->is_keypressed = &engine_is_keypressed; + engine->handle_events = &handle_events; + engine->game_loop = &game_loop; + engine->draw = &engine_draw; + engine->change_scene = &change_scene; + engine->play_music = &engine_play_music; + engine->stop_music = &engine_stop_music; + engine->destroy = &engine_destroy; + engine_add_buildin_systems(engine); + engine_add_buildin_components(engine); + engine->dataloaders = NULL; + engine->add_dataloader = &engine_add_dataloader; + return (engine); +} \ No newline at end of file diff --git a/lib/gamacon/src/engine/engine_component_builder.c b/lib/gamacon/src/engine/engine_component_builder.c new file mode 100644 index 0000000..bc83c39 --- /dev/null +++ b/lib/gamacon/src/engine/engine_component_builder.c @@ -0,0 +1,44 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** engine_system_builder +*/ + +#include "engine.h" +#include "system.h" +#include "components/movable_component.h" +#include "components/parallax_component.h" +#include "components/fixed_to_cam_component.h" +#include "components/renderer.h" +#include "components/camerafollow_component.h" +#include "components/transform_component.h" +#include "components/controllable_component.h" +#include "components/controllers/keyboard_controller.h" +#include "components/friction_component.h" +#include "components/friction_giver.h" +#include "components/collision_component.h" +#include + +void engine_add_component(gc_engine *engine, const void *component) +{ + engine->components = list_add(engine->components, (void *)component); +} + +void engine_add_buildin_components(gc_engine *engine) +{ + engine->components = NULL; + engine->add_component = &engine_add_component; + engine->get_component = &engine_get_component; + engine->add_component(engine, &transform_component); + engine->add_component(engine, &movable_component); + engine->add_component(engine, &renderer_component); + engine->add_component(engine, &camerafollow_component); + engine->add_component(engine, &fixed_to_cam); + engine->add_component(engine, ¶llax_component); + engine->add_component(engine, &controllable_component); + engine->add_component(engine, &keyboard_controller); + engine->add_component(engine, &friction_giver); + engine->add_component(engine, &friction_component); + engine->add_component(engine, &collision_component); +} \ No newline at end of file diff --git a/lib/gamacon/src/engine/engine_dataloader.c b/lib/gamacon/src/engine/engine_dataloader.c new file mode 100644 index 0000000..dd9e86c --- /dev/null +++ b/lib/gamacon/src/engine/engine_dataloader.c @@ -0,0 +1,22 @@ +/* +** EPITECH PROJECT, 2020 +** Twac +** File description: +** engine_dataloader +*/ + +#include "engine.h" +#include "data.h" +#include "my.h" +#include + +void engine_add_dataloader(gc_engine *engine, char *type, gc_loader loader) +{ + gc_dataloader *dataloader = malloc(sizeof(*dataloader)); + + if (!dataloader) + return ((void)my_printf("Couldn't malloc dataloader for %s\n", type)); + dataloader->type = type; + dataloader->load = loader; + engine->dataloaders = list_add(engine->dataloaders, dataloader); +} \ No newline at end of file diff --git a/lib/gamacon/src/engine/engine_internal.c b/lib/gamacon/src/engine/engine_internal.c new file mode 100644 index 0000000..fbe0732 --- /dev/null +++ b/lib/gamacon/src/engine/engine_internal.c @@ -0,0 +1,37 @@ +/* +** EPITECH PROJECT, 2019 +** Gamacon +** File description: +** engine_internal +*/ + +#include +#include "engine.h" + +bool engine_is_open(gc_engine *engine) +{ + (void)engine; + return (false); +} + +bool engine_has_focus(gc_engine *engine) +{ + (void)engine; + return (true); +} + +void handle_events(gc_engine *engine) +{ + (void)engine; +} + +void engine_draw(gc_engine *engine) +{ + (void)engine; +} + +bool engine_is_keypressed(int key) +{ + (void)key; + return (false); +} \ No newline at end of file diff --git a/lib/gamacon/src/engine/engine_system_builder.c b/lib/gamacon/src/engine/engine_system_builder.c new file mode 100644 index 0000000..a659c77 --- /dev/null +++ b/lib/gamacon/src/engine/engine_system_builder.c @@ -0,0 +1,55 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** engine_system_builder +*/ + +#include "engine.h" +#include "system.h" +#include "systems/sfml_renderer_system.h" +#include "systems/movable_system.h" +#include "systems/parallax_system.h" +#include "systems/controllers/keyboard_controller_system.h" +#include "systems/friction_system.h" +#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) +{ + engine->systems = list_add(engine->systems, (void *)system); +} + +void engine_finish_physics(gc_engine *engine) +{ + engine->add_system(engine, new_system(&movable_system)); +} + +void engine_add_buildin_systems(gc_engine *engine) +{ + engine->systems = NULL; + engine->add_system = &engine_add_system; + engine->get_system = &engine_get_system; + engine->finish_physics = &engine_finish_physics; + engine->add_system(engine, ¶llax_system); + engine->add_system(engine, &keyboard_controller_system); + engine->add_system(engine, &friction_system); + engine->add_system(engine, new_system(&collision_system, engine->scene)); +} + +int engine_use_sfml(gc_engine *engine, const char *title, int framerate) +{ + void *renderer = new_system(&sfml_renderer, engine, title, framerate); + void *camfollow = new_system(&camerafollow_system); + + if (!renderer || !camfollow) + return (-1); + engine->add_system(engine, renderer); + engine->add_system(engine, camfollow); + engine->add_dataloader(engine, "music", &sfml_music_loader); + engine->add_dataloader(engine, "sprite", &sfml_sprite_loader); + engine->add_dataloader(engine, "font", &sfml_font_loader); + return (0); +} \ No newline at end of file diff --git a/lib/gamacon/src/entity/entity.c b/lib/gamacon/src/entity/entity.c new file mode 100644 index 0000000..18fac9a --- /dev/null +++ b/lib/gamacon/src/entity/entity.c @@ -0,0 +1,76 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** entity +*/ + +#include "entity.h" +#include "utility.h" +#include +#include + +gc_entity *entity_get(gc_scene *scene, int id) +{ + if (!scene) + return (NULL); + for (gc_list *ent = scene->entities; ent; ent = ent->next) { + if (((gc_entity *)ent->data)->id == id) + return (ent->data); + } + return (NULL); +} + +void *entity_get_component(const gc_entity *entity, const char *name) +{ + if (!entity || !name) + return (NULL); + for (gc_component *cmp = entity->components; cmp; cmp = cmp->next) { + if (!my_strcmp(cmp->name, name)) + return (cmp); + } + return (NULL); +} + +bool entity_has_component(const gc_entity *entity, const char *name) +{ + for (gc_component *cmp = entity->components; cmp; cmp = cmp->next) { + if (!my_strcmp(cmp->name, name)) + return (true); + } + return (false); +} + +char *entity_serialize(gc_entity *entity, int fd) +{ + char *id = tostr(entity->id); + + write(fd, id, my_strlen(id)); + write(fd, ": ", 2); + for (gc_component *cmp = entity->components; cmp; cmp = cmp->next) + cmp->serialize(cmp); + free(id); + return (NULL); +} + +static void destroy(gc_entity *entity) +{ + gc_component *next = NULL; + + for (gc_component *cmp = entity->components; cmp; cmp = next) { + next = cmp->next; + cmp->destroy(cmp); + } + free(entity); +} + +const gc_entity entity_prefab = { + id: 0, + components: NULL, + add_component: &entity_add_component, + get_component: &entity_get_component, + has_component: &entity_has_component, + remove_component: &entity_remove_component, + serialize: &entity_serialize, + destroy: &destroy +}; \ No newline at end of file diff --git a/lib/gamacon/src/entity/entity_factory.c b/lib/gamacon/src/entity/entity_factory.c new file mode 100644 index 0000000..74ff7d2 --- /dev/null +++ b/lib/gamacon/src/entity/entity_factory.c @@ -0,0 +1,87 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** entity_factory +*/ + +#include "entity.h" +#include "component.h" +#include "my.h" +#include + +static unsigned int next_id = 0; +const gc_entity entity_prefab; + +gc_entity *entity_create(void) +{ + gc_entity *entity = malloc(sizeof(gc_entity)); + + if (!entity) + return (NULL); + *entity = entity_prefab; + entity->id = next_id; + next_id++; + return (entity); +} + +gc_entity *entity_create_with_id(int id) +{ + gc_entity *entity = malloc(sizeof(gc_entity)); + + if (!entity) + return (NULL); + *entity = entity_prefab; + entity->id = id; + return (entity); +} + +int entity_add(gc_scene *scene, gc_entity *e) +{ + char *name; + + scene->entities = list_add(scene->entities, e); + if (!scene->entities) + return (-1); + for (gc_component *cmp = e->components; cmp; cmp = cmp->next) { + name = cmp->name; + scene->entities_by_cmp = tupple_add(scene->entities_by_cmp, name, e); + if (!scene->entities_by_cmp) + return (-1); + } + return (0); +} + +gc_entity *entity_add_component(gc_entity *entity, void *component) +{ + gc_component *components = entity->components; + + if (!component) + return (NULL); + if (!components) + entity->components = component; + else { + while (components->next) + components = components->next; + ((gc_component *)component)->prev = components; + components->next = component; + } + return (entity); +} + +void entity_remove_component(const gc_scene *scene, const gc_entity *entity, \ +const char *name) +{ + gc_component *cmp; + + if (!scene || !entity || !entity->components) + return; + cmp = component_remove(entity->components, name); + if (!cmp) + return; + for (gc_tupple *tup = scene->entities_by_cmp; tup; tup = tup->next) { + if (!my_strcmp(tup->name, name)) + tup_remove(tup, entity->id); + } + component_destroy(cmp); +} \ No newline at end of file diff --git a/lib/gamacon/src/scene/scene.c b/lib/gamacon/src/scene/scene.c new file mode 100644 index 0000000..9aa5c72 --- /dev/null +++ b/lib/gamacon/src/scene/scene.c @@ -0,0 +1,72 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** scene +*/ + +#include "engine.h" +#include "entity.h" +#include "utility.h" +#include "prefab.h" +#include + +gc_list *get_entity_by_cmp(gc_scene *scene, const char *cmp_name) +{ + for (gc_tupple *tup = scene->entities_by_cmp; tup; tup = tup->next) { + if (!my_strcmp(tup->name, cmp_name)) + return (tup->entities); + } + return (NULL); +} + +void *scene_get_data(gc_scene *scene, const char *type, const char *name) +{ + gc_data *data; + + if (!scene || !scene->data) + return (NULL); + for (gc_list *li = scene->data; li; li = li->next) { + data = (gc_data *)li->data; + if (type && my_strcmp(data->type, type)) + continue; + if (!name || !my_strcmp(data->name, name)) + return (data->custom); + } + return (NULL); +} + +gc_scene *scene_create(gc_engine *engine, const char *xmlpath) +{ + gc_scene *scene = malloc(sizeof(gc_scene)); + node *n = NULL; + + if (!scene) + return (NULL); + if (xmlpath && !(n = xml_parse(xmlpath))) + return (NULL); + scene_load_data(engine, scene, n); + scene->entities = NULL; + scene->entities_by_cmp = NULL; + scene->add_entity = &entity_add; + scene->get_entity = &entity_get; + scene->get_entity_by_cmp = &get_entity_by_cmp; + scene->destroy = &scene_destroy; + scene->get_data = &scene_get_data; + prefab_loadentities(n, engine, scene); + xml_destroy(n); + return (scene); +} + +int change_scene(gc_engine *engine, gc_scene *scene) +{ + void *music = scene->get_data(scene, "music", NULL); + + engine->stop_music(engine); + if (engine->scene) + engine->scene->destroy(engine->scene); + engine->scene = scene; + if (music) + engine->play_music(music); + return (0); +} \ No newline at end of file diff --git a/lib/gamacon/src/scene/scene_destroy.c b/lib/gamacon/src/scene/scene_destroy.c new file mode 100644 index 0000000..7ef7f9d --- /dev/null +++ b/lib/gamacon/src/scene/scene_destroy.c @@ -0,0 +1,49 @@ +/* +** EPITECH PROJECT, 2020 +** Twac +** File description: +** scene_destroy +*/ + +#include "scene.h" +#include "data.h" +#include "list.h" +#include "tupple.h" +#include + +static void free_data(gc_scene *scene) +{ + void *next = NULL; + + for (gc_list *data = scene->data; data; data = next) { + next = data->next; + if (((gc_data *)data->data)->destroy) + ((gc_data *)data->data)->destroy(data->data); + free(((gc_data *)data->data)->type); + free(data->data); + free(data); + } +} + +void scene_destroy(gc_scene *scene) +{ + void *next = NULL; + gc_tupple *tup = scene->entities_by_cmp; + + for (gc_list *entity = scene->entities; entity; entity = next) { + next = entity->next; + ((gc_entity *)entity->data)->destroy(entity->data); + free(entity); + } + free_data(scene); + for (gc_tupple *tupple = tup; tupple; tupple = tup) { + tup = tupple->next; + for (gc_list *li = tupple->entities; li; li = next) { + next = li->next; + free(li); + } + free(tupple->name); + free(tupple); + } + free(scene); +} \ No newline at end of file diff --git a/lib/gamacon/src/scene/scene_loader.c b/lib/gamacon/src/scene/scene_loader.c new file mode 100644 index 0000000..d71d1e9 --- /dev/null +++ b/lib/gamacon/src/scene/scene_loader.c @@ -0,0 +1,52 @@ +/* +** EPITECH PROJECT, 2020 +** Twac +** File description: +** scene_music +*/ + +#include "engine.h" +#include "scene.h" +#include "xml.h" +#include "my.h" +#include "data.h" +#include +#include +#include + +gc_dataloader *gc_dataloader_from_type(gc_engine *engine, const char *type) +{ + gc_dataloader *loader; + + for (gc_list *li = engine->dataloaders; li; li = li->next) { + loader = (gc_dataloader *)li->data; + if (!my_strcmp(loader->type, type)) + return (loader); + } + return (NULL); +} + +void scene_load_data(gc_engine *engine, gc_scene *scene, node *n) +{ + gc_data *data; + gc_dataloader *loader; + + scene->data = NULL; + if (!(n = xml_getnode(n, "data"))) + return; + for (n = n->child; n; n = n->next) { + if (!(data = malloc(sizeof(*data)))) + return; + data->type = my_strdup(n->name); + data->name = xml_getproperty(n, "name"); + data->destroy = NULL; + loader = gc_dataloader_from_type(engine, data->type); + if (!loader) + return ((void)my_printf("Couldn't find data loader for the type %s\ +\n", data->type)); + if (loader->load(data, n) < 0) + return ((void)my_printf("Error while loading data %s (type %s).\ +\n", data->name, data->type)); + scene->data = list_add(scene->data, data); + } +} \ No newline at end of file diff --git a/lib/gamacon/src/sfml_renderer/sfml_dataloaders.c b/lib/gamacon/src/sfml_renderer/sfml_dataloaders.c new file mode 100644 index 0000000..0c0a887 --- /dev/null +++ b/lib/gamacon/src/sfml_renderer/sfml_dataloaders.c @@ -0,0 +1,57 @@ +/* +** EPITECH PROJECT, 2020 +** Twac +** File description: +** sfml_dataloaders +*/ + +#include "data.h" +#include "xml.h" +#include "sfml_renderer.h" +#include "my.h" +#include +#include +#include + +int sfml_music_loader(gc_data *data, node *n) +{ + char *path = xml_getproperty(n, "src"); + + if (!path) + return (-1); + data->name = path; + data->custom = sfMusic_createFromFile(path); + data->destroy = &sfml_music_destroy; + if (!data->custom) + return (-1); + return (0); +} + +int sfml_sprite_loader(gc_data *data, node *n) +{ + char *path = xml_getproperty(n, "src"); + + if (!path) + return (-1); + data->name = path; + data->custom = sfTexture_createFromFile(path, NULL); + if (!data->custom || !data->name) + return (-1); + sfTexture_setRepeated(data->custom, sfTrue); + data->destroy = &sfml_texture_destroy; + return (0); +} + +int sfml_font_loader(gc_data *data, node *n) +{ + char *path = xml_getproperty(n, "src"); + + if (!path) + return (-1); + data->name = path; + data->custom = sfFont_createFromFile(path); + if (!data->custom || !data->name) + return (-1); + data->destroy = &sfml_font_destroy; + return (0); +} \ No newline at end of file diff --git a/lib/gamacon/src/sfml_renderer/sfml_drawer.c b/lib/gamacon/src/sfml_renderer/sfml_drawer.c new file mode 100644 index 0000000..7606763 --- /dev/null +++ b/lib/gamacon/src/sfml_renderer/sfml_drawer.c @@ -0,0 +1,69 @@ +/* +** EPITECH PROJECT, 2020 +** Twac +** File description: +** sfml_drawer +*/ + +#include "sfml_renderer.h" +#include "vector2.h" +#include "sprite.h" +#include "text.h" +#include "components/transform_component.h" +#include "systems/sfml_renderer_system.h" +#include + +void sfmlrenderer_draw_texture(struct sfml_renderer_system *renderer, \ +struct transform_component *tra, gc_sprite *sprite) +{ + sfVector2f pos = (sfVector2f){tra->position.x, -tra->position.y}; + sfVector2f scale = (sfVector2f){ + tra->size.x * sprite->scale.x / sprite->rect.width, + tra->size.y * sprite->scale.y / sprite->rect.height + }; + + sprite->pos = tra->position; + if (!sprite->texture) + return; + sfSprite_setTexture(renderer->sprite, sprite->texture, true); + sfSprite_setTextureRect(renderer->sprite, (sfIntRect){ + (int)sprite->rect.left, (int)sprite->rect.top, + (int)sprite->rect.width, (int)sprite->rect.height + }); + sfSprite_setPosition(renderer->sprite, pos); + sfSprite_setScale(renderer->sprite, scale); + sfSprite_setOrigin(renderer->sprite, (sfVector2f){ + scale.x < 0 ? sprite->rect.width : 0, + scale.y < 0 ? sprite->rect.height : 0 + }); + sfRenderWindow_drawSprite(renderer->window, renderer->sprite, NULL); +} + +void sfmlrenderer_draw_anim(struct sfml_renderer_system *renderer, \ +struct transform_component *tra, gc_animholder *holder, float dtime) +{ + gc_int_rect *rec = &holder->sprite->rect; + gc_anim *curr = holder->current; + + if (curr) + holder->timesince_up += dtime; + if (curr && holder->timesince_up > 1 / curr->frame_rate) { + rec->left += rec->width; + holder->timesince_up = 0; + if (rec->left > curr->rect.left + rec->width * (curr->frame_count - 1)) + rec->left = curr->rect.left; + } + sfmlrenderer_draw_texture(renderer, tra, holder->sprite); +} + +void sfmlrenderer_draw_txt(struct sfml_renderer_system *renderer, \ +struct transform_component *tra, gc_text *txt) +{ + sfText_setString(renderer->text, txt->text); + sfText_setFont(renderer->text, txt->font); + sfText_setPosition(renderer->text, (sfVector2f){ + tra->position.x, + -tra->position.y + }); + sfRenderWindow_drawText(renderer->window, renderer->text, NULL); +} \ No newline at end of file diff --git a/lib/gamacon/src/sfml_renderer/sfml_functions.c b/lib/gamacon/src/sfml_renderer/sfml_functions.c new file mode 100644 index 0000000..d558534 --- /dev/null +++ b/lib/gamacon/src/sfml_renderer/sfml_functions.c @@ -0,0 +1,72 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** sfml_functions +*/ + +#include "engine.h" +#include +#include "sfml_renderer.h" +#include "systems/sfml_renderer_system.h" +#include "systems/camerafollow_system.h" +#include "components/transform_component.h" + +bool sfml_is_open(gc_engine *engine) +{ + struct sfml_renderer_system *renderer = GETSYS(sfml_renderer_system); + + if (engine->should_close) + return (false); + return (sfRenderWindow_isOpen(renderer->window)); +} + +bool sfml_has_focus(gc_engine *engine) +{ + struct sfml_renderer_system *renderer = GETSYS(sfml_renderer_system); + + return (sfRenderWindow_hasFocus(renderer->window)); +} + +bool sfml_is_keypressed(int key) +{ + return (sfKeyboard_isKeyPressed(key)); +} + +void sfml_handle_events(gc_engine *engine) +{ + struct sfml_renderer_system *renderer = GETSYS(sfml_renderer_system); + sfEvent event; + + while (sfRenderWindow_pollEvent(renderer->window, &event)) { + if (event.type == sfEvtClosed) + sfRenderWindow_close(renderer->window); + if (event.type == sfEvtResized) { + sfView_setSize(renderer->view, (sfVector2f){ + event.size.width, + event.size.height + }); + entities_update_to_cam_size(engine->scene, (gc_vector2) { + event.size.width, + event.size.height + }); + } + } +} + +void sfml_draw(gc_engine *engine) +{ + struct sfml_renderer_system *renderer = GETSYS(sfml_renderer_system); + struct camerafollow_system *cam = GETSYS(camerafollow_system); + + if (cam) { + sfView_setCenter(renderer->view, (sfVector2f){ + cam->cam_pos.x, + -cam->cam_pos.y + }); + sfRenderWindow_setView(renderer->window, renderer->view); + entities_update_to_cam(engine->scene, renderer, cam); + } + sfRenderWindow_display(renderer->window); + sfRenderWindow_clear(renderer->window, sfBlack); +} \ No newline at end of file diff --git a/lib/gamacon/src/sfml_renderer/sfml_music_player.c b/lib/gamacon/src/sfml_renderer/sfml_music_player.c new file mode 100644 index 0000000..4e95716 --- /dev/null +++ b/lib/gamacon/src/sfml_renderer/sfml_music_player.c @@ -0,0 +1,25 @@ +/* +** EPITECH PROJECT, 2020 +** Twac +** File description: +** sfml_music_player +*/ + +#include "engine.h" +#include + +void sfml_play_music(void *music) +{ + sfMusic_setLoop(music, true); + sfMusic_play(music); +} + +void sfml_stop_music(gc_engine *engine) +{ + void *music; + + if (!engine->scene) + return; + music = engine->scene->get_data(engine->scene, "music", NULL); + sfMusic_stop(music); +} \ No newline at end of file diff --git a/lib/gamacon/src/sfml_renderer/texture_utility.c b/lib/gamacon/src/sfml_renderer/texture_utility.c new file mode 100644 index 0000000..4d9315f --- /dev/null +++ b/lib/gamacon/src/sfml_renderer/texture_utility.c @@ -0,0 +1,51 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** renderer_loader +*/ + +#include "engine.h" +#include "utility.h" +#include "sfml_renderer.h" +#include +#include +#include + + +sfTexture *get_texture(gc_scene *scene, char *name) +{ + sfTexture *texture; + + if (!scene) { + my_printf("No scene, can't load textures\n"); + return (NULL); + } + if (!name) { + my_printf("Texture name: null. How should I find a texture ?\n"); + return (NULL); + } + texture = scene->get_data(scene, "sprite", name); + if (texture) + return (texture); + my_printf("Gamacon: no texture found for the name \"%s\"\n", name); + return (NULL); +} + +void sfml_texture_destroy(gc_data *data) +{ + sfTexture_destroy(data->custom); + free(data->name); +} + +void sfml_music_destroy(gc_data *data) +{ + sfMusic_destroy(data->custom); + free(data->name); +} + +void sfml_font_destroy(gc_data *data) +{ + sfFont_destroy(data->custom); + free(data->name); +} \ No newline at end of file diff --git a/lib/gamacon/src/system.c b/lib/gamacon/src/system.c new file mode 100644 index 0000000..ca938fc --- /dev/null +++ b/lib/gamacon/src/system.c @@ -0,0 +1,61 @@ +/* +** EPITECH PROJECT, 2019 +** Gamacon +** File description: +** component +*/ + +#include "system.h" +#include "utility.h" +#include + +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 (!comp) + return (false); + for (int i = 0; comp->dependencies[i]; i++) { + if (!entity->has_component(entity, comp->dependencies[i])) { + my_printf("\tMissing component: %s\n\n", comp->dependencies[i]); + return (false); + } + } + return (true); +} + +void *engine_get_system(gc_engine *engine, const char *name) +{ + for (gc_list *sys = engine->systems; sys; sys = sys->next) { + if (!my_strcmp(((const gc_system *)sys->data)->name, name)) + return (sys->data); + } + return (NULL); +} + +void *new_system(const void *system, ...) +{ + gc_system *base = (gc_system *)system; + va_list args; + void *new_sys = malloc(base->size); + + if (!new_sys) + return (NULL); + *(gc_system *)new_sys = *base; + if (((gc_system *)new_sys)->ctr) { + va_start(args, system); + ((gc_system *)new_sys)->ctr(new_sys, args); + va_end(args); + } + return (new_sys); +} + +void system_destroy(void *system) +{ + gc_system *sys = (gc_system *)system; + + if (sys->dtr) + sys->dtr(system); + free(system); +} \ No newline at end of file diff --git a/lib/gamacon/src/systems/camera_follow_system.c b/lib/gamacon/src/systems/camera_follow_system.c new file mode 100644 index 0000000..2a8c883 --- /dev/null +++ b/lib/gamacon/src/systems/camera_follow_system.c @@ -0,0 +1,50 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** friction +*/ + +#include "entity.h" +#include "system.h" +#include "vector2.h" +#include "utility.h" +#include "components/transform_component.h" +#include "components/camerafollow_component.h" +#include "systems/camerafollow_system.h" +#include "math.h" +#include + +static void update_entity(gc_engine *engine, void *system, \ +gc_entity *entity, float dtime) +{ + struct camerafollow_system *sys = (struct camerafollow_system *)system; + struct transform_component *tra = GETCMP(transform_component); + + sys->cam_pos.x = (sys->cam_pos.x + tra->position.x) / 2; + sys->cam_pos.y = (sys->cam_pos.y + tra->position.y) / 2; + (void)engine; + (void)dtime; +} + +static void ctr(void *system, va_list args) +{ + struct camerafollow_system *sys = (struct camerafollow_system *)system; + + sys->cam_pos = (gc_vector2){0, 0}; + (void)args; +} + +const struct camerafollow_system camerafollow_system = { + base: { + name: "camerafollow_system", + component_name: "camerafollow_component", + size: sizeof(struct camerafollow_system), + ctr: &ctr, + dtr: NULL, + check_dependencies: &system_check_dependencies, + update_entity: &update_entity, + destroy: &system_destroy + }, + cam_pos: (gc_vector2){0, 0} +}; \ No newline at end of file diff --git a/lib/gamacon/src/systems/collision_system.c b/lib/gamacon/src/systems/collision_system.c new file mode 100644 index 0000000..7a3a4af --- /dev/null +++ b/lib/gamacon/src/systems/collision_system.c @@ -0,0 +1,71 @@ +/* +** EPITECH PROJECT, 2020 +** Twac +** File description: +** collision_system +*/ + +#include "entity.h" +#include "system.h" +#include "vector2.h" +#include "utility.h" +#include "components/collision_component.h" +#include "components/transform_component.h" +#include "components/renderer.h" +#include "systems/collision_system.h" +#include + +void col_update_entity(gc_engine *engine, \ +void *system, gc_entity *entity, float dtime __attribute__((unused))) +{ + gc_collision_system *sys = (gc_collision_system *)system; + struct collision_component *col = GETCMP(collision_component); + struct transform_component *pos = GETCMP(transform_component); + qt_object obj = (qt_object) { + entity->id, + {pos->position.x, pos->position.y, pos->size.y, pos->size.x}, + col->layer + }; + qt_update(sys->tree, obj); + qt_collision qtcol = collision_get_info(sys->tree, entity->id); + col->distance_down = qtcol.distance_down; + col->distance_top = qtcol.distance_top; + col->distance_left = qtcol.distance_left; + col->distance_right = qtcol.distance_right; + if (!col->on_collide) + return; + for (int i = 0; qtcol.collide_with[i] != -1; i++) { + for (size_t j = 0; j < col->collide_size / sizeof(void (*)()); j++) + col->on_collide[j](engine, entity, qtcol.collide_with[i]); + } +} + +static void ctr(void *system, va_list args) +{ + gc_collision_system *col = (gc_collision_system *)system; + + col->tree = qt_create((qt_intrect){-1000, -1000, 2000, 2000}, 25); + (void)args; +} + +static void dtr(void *system) +{ + gc_collision_system *mov = (gc_collision_system *)system; + + qt_destroy(mov->tree); + (void)system; +} + +const gc_collision_system collision_system = { + base: { + name: "collision_system", + component_name: "collision_component", + size: sizeof(gc_collision_system), + ctr: &ctr, + dtr: &dtr, + check_dependencies: &system_check_dependencies, + update_entity: &col_update_entity, + destroy: &system_destroy + }, + tree: NULL +}; \ No newline at end of file diff --git a/lib/gamacon/src/systems/controllers/keyboard_controller_system.c b/lib/gamacon/src/systems/controllers/keyboard_controller_system.c new file mode 100644 index 0000000..90e539d --- /dev/null +++ b/lib/gamacon/src/systems/controllers/keyboard_controller_system.c @@ -0,0 +1,44 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** movable_system +*/ + +#include "entity.h" +#include "system.h" +#include "vector2.h" +#include "components/movable_component.h" +#include "components/controllable_component.h" +#include "components/controllers/keyboard_controller.h" +#include "components/renderer.h" +#include + +void keyboard_update_entity(gc_engine *engine, void *system, \ +gc_entity *entity, float dtime) +{ + struct controllable_component *con = GETCMP(controllable_component); + struct keyboard_controller *key = GETCMP(keyboard_controller); + + con->moving_left = engine->is_keypressed(key->left_key); + con->moving_right = engine->is_keypressed(key->right_key); + con->jumping = engine->is_keypressed(key->jump_key); + (void)system; + (void)dtime; +} + +void keyboard_destroy(void *system) +{ + (void)system; +} + +const gc_system keyboard_controller_system = { + name: "keyboard_controller_system", + component_name: "keyboard_controller", + size: sizeof(gc_system), + ctr: NULL, + dtr: NULL, + check_dependencies: &system_check_dependencies, + update_entity: &keyboard_update_entity, + destroy: &keyboard_destroy +}; \ No newline at end of file diff --git a/lib/gamacon/src/systems/fixed_to_cam_pseudosystem.c b/lib/gamacon/src/systems/fixed_to_cam_pseudosystem.c new file mode 100644 index 0000000..a45c36a --- /dev/null +++ b/lib/gamacon/src/systems/fixed_to_cam_pseudosystem.c @@ -0,0 +1,59 @@ +/* +** EPITECH PROJECT, 2020 +** Twac +** File description: +** update_to_cam +*/ + +#include "entity.h" +#include "scene.h" +#include "systems/camerafollow_system.h" +#include "component.h" +#include "components/transform_component.h" +#include "components/fixed_to_cam_component.h" +#include "systems/sfml_renderer_system.h" +#include "my.h" +#include + +void entities_update_to_cam(gc_scene *scene, \ +struct sfml_renderer_system *renderer, struct camerafollow_system *cam) +{ + gc_list *list; + gc_entity *entity; + sfVector2f size; + gc_vector2 offset; + struct transform_component *tra; + + list = scene->get_entity_by_cmp(scene, "fixed_to_cam"); + for (gc_list *li = list; li; li = li->next) { + entity = (gc_entity *)li->data; + tra = GETCMP(transform_component); + if (!tra) + continue; + size = sfView_getSize(renderer->view); + offset = GETCMP(fixed_to_cam)->offset; + tra->position = (gc_vector2) { + cam->cam_pos.x - size.x / 2 + offset.x, + cam->cam_pos.y + size.y / 2 - offset.y, + }; + } +} + +void entities_update_to_cam_size(gc_scene *scene, gc_vector2 size) +{ + gc_list *list; + gc_entity *entity; + struct transform_component *tra; + + list = scene->get_entity_by_cmp(scene, "fixed_to_cam"); + for (gc_list *li = list; li; li = li->next) { + entity = (gc_entity *)li->data; + tra = GETCMP(transform_component); + if (!tra) + continue; + tra->size = (gc_vector2) { + size.x, + size.y + }; + } +} \ No newline at end of file diff --git a/lib/gamacon/src/systems/friction_system.c b/lib/gamacon/src/systems/friction_system.c new file mode 100644 index 0000000..14e7335 --- /dev/null +++ b/lib/gamacon/src/systems/friction_system.c @@ -0,0 +1,46 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** friction +*/ + +#include "entity.h" +#include "system.h" +#include "vector2.h" +#include "utility.h" +#include "components/movable_component.h" +#include "components/friction_component.h" +#include "components/collision_component.h" +#include "math.h" +#include + +void fric_update_entity(gc_engine *engine, void *system, \ +gc_entity *entity, float dtime) +{ + struct friction_component *fric = GETCMP(friction_component); + struct movable_component *mov = GETCMP(movable_component); + + mov->acceleration.x -= fric->value * mov->velocity.x; + mov->acceleration.y -= fric->value * mov->velocity.y; + fric->value = fric->default_value; + (void)system; + (void)engine; + (void)dtime; +} + +void fric_destroy(void *system) +{ + (void)system; +} + +const gc_system friction_system = { + name: "friction_system", + component_name: "friction_component", + size: sizeof(gc_system), + ctr: NULL, + dtr: NULL, + check_dependencies: &system_check_dependencies, + update_entity: &fric_update_entity, + destroy: &fric_destroy +}; \ No newline at end of file diff --git a/lib/gamacon/src/systems/movable_system.c b/lib/gamacon/src/systems/movable_system.c new file mode 100644 index 0000000..9583872 --- /dev/null +++ b/lib/gamacon/src/systems/movable_system.c @@ -0,0 +1,73 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** movable_system +*/ + +#include "entity.h" +#include "system.h" +#include "vector2.h" +#include "utility.h" +#include "components/movable_component.h" +#include "components/collision_component.h" +#include "components/transform_component.h" +#include "components/renderer.h" +#include "systems/movable_system.h" +#include + +#include "components/controllable_component.h" + +void clamp_vel(struct movable_component *mov, struct collision_component *col) +{ + if ((col->distance_left == 0 && mov->velocity.x < 0) || + (col->distance_right == 0 && mov->velocity.x > 0)) + mov->velocity.x = 0; + if ((col->distance_down == 0 && mov->velocity.y < 0) || + (col->distance_top == 0 && mov->velocity.y > 0)) + mov->velocity.y = 0; +} + +static void update_entity(gc_engine *engine __attribute__((unused)), \ +void *system __attribute__((unused)), gc_entity *entity, float dtime) +{ + struct movable_component *mov = GETCMP(movable_component); + struct transform_component *pos = GETCMP(transform_component); + struct collision_component *col = GETCMP(collision_component); + + if (mov->velocity.x < 0) + pos->position.x -= MIN(mov->velocity.x * -dtime, col->distance_left); + else + pos->position.x += MIN(mov->velocity.x * dtime, col->distance_right); + if (mov->velocity.y < 0) + pos->position.y -= MIN(mov->velocity.y * -dtime, col->distance_down); + else + pos->position.y += MIN(mov->velocity.y * dtime, col->distance_top); + clamp_vel(mov, col); + mov->velocity.x += mov->acceleration.x * dtime; + mov->velocity.y += mov->acceleration.y * dtime; + mov->acceleration.x = 0; + mov->acceleration.y = 0; +} + +static void ctr(void *system, va_list args) +{ + (void)system; + (void)args; +} + +static void dtr(void *system) +{ + (void)system; +} + +const gc_system movable_system = { + name: "movable_system", + component_name: "movable_component", + size: sizeof(gc_system), + ctr: &ctr, + dtr: &dtr, + check_dependencies: &system_check_dependencies, + update_entity: &update_entity, + destroy: &system_destroy +}; \ No newline at end of file diff --git a/lib/gamacon/src/systems/parallax_system.c b/lib/gamacon/src/systems/parallax_system.c new file mode 100644 index 0000000..3818e03 --- /dev/null +++ b/lib/gamacon/src/systems/parallax_system.c @@ -0,0 +1,49 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** parallax_system +*/ + +#include "entity.h" +#include "system.h" +#include "sprite.h" +#include "vector2.h" +#include "components/parallax_component.h" +#include "components/renderer.h" +#include "components/transform_component.h" +#include + +void parallax_update_entity(gc_engine *engine, void *system, \ +gc_entity *entity, float dtime) +{ + struct renderer *text = GETCMP(renderer); + struct parallax_component *par = GETCMP(parallax_component); + struct transform_component *tra = GETCMP(transform_component); + gc_sprite *sprite; + + if (text->type != GC_TEXTUREREND || !text->data) + return; + sprite = ((gc_sprite *)text->data); + sprite->rect.left += (tra->position.x - par->old_pos.x) * par->speed; + par->old_pos = tra->position; + (void)system; + (void)engine; + (void)dtime; +} + +void parallax_destroy(void *system) +{ + (void)system; +} + +const gc_system parallax_system = { + name: "parallax_system", + component_name: "parallax_component", + size: sizeof(gc_system), + ctr: NULL, + dtr: NULL, + check_dependencies: &system_check_dependencies, + update_entity: ¶llax_update_entity, + destroy: ¶llax_destroy +}; \ No newline at end of file diff --git a/lib/gamacon/src/systems/sfml_renderer_system.c b/lib/gamacon/src/systems/sfml_renderer_system.c new file mode 100644 index 0000000..8fe3c65 --- /dev/null +++ b/lib/gamacon/src/systems/sfml_renderer_system.c @@ -0,0 +1,104 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** texture_renderer_system +*/ + +#include "entity.h" +#include "system.h" +#include "vector2.h" +#include "sprite.h" +#include "systems/sfml_renderer_system.h" +#include "components/transform_component.h" +#include "components/renderer.h" +#include "my.h" +#include "sfml_renderer.h" +#include "text.h" +#include + +void sfml_update_entity(gc_engine *engine, void *system, \ +gc_entity *entity, float dtime) +{ + struct transform_component *pos = GETCMP(transform_component); + struct renderer *text = GETCMP(renderer); + struct sfml_renderer_system *rend = (struct sfml_renderer_system *)system; + + if (!text->data) + return; + switch (text->type) { + case GC_TEXTUREREND: + sfmlrenderer_draw_texture(rend, pos, (gc_sprite *)text->data); + break; + case GC_ANIMREND: + sfmlrenderer_draw_anim(rend, pos, (gc_animholder *)text->data, dtime); + break; + case GC_TXTREND: + sfmlrenderer_draw_txt(rend, pos, (gc_text *)text->data); + break; + default: + my_printf("Trying to render a texture with an unknown type.\n"); + break; + } + (void)engine; +} + +void sfml_setup_options(gc_engine *engine) +{ + engine->is_open = &sfml_is_open; + engine->has_focus = &sfml_has_focus; + engine->is_keypressed = &sfml_is_keypressed; + engine->handle_events = &sfml_handle_events; + engine->draw = &sfml_draw; + engine->play_music = &sfml_play_music; + engine->stop_music = &sfml_stop_music; +} + +void sfmlrend_ctr(void *rend, va_list list) +{ + struct sfml_renderer_system *renderer = (struct sfml_renderer_system *)rend; + sfVideoMode mode = {800, 600, 32}; + gc_engine *engine = va_arg(list, gc_engine *); + const char *title = va_arg(list, const char *); + int framerate = va_arg(list, int); + + renderer->window = sfRenderWindow_create(mode, title, sfDefaultStyle, NULL); + renderer->sprite = sfSprite_create(); + renderer->view = sfView_create(); + renderer->text = sfText_create(); + if (!renderer->window || !renderer->sprite || \ +!renderer->view || !renderer->text) + return; + sfRenderWindow_setFramerateLimit(renderer->window, framerate); + sfView_setSize(renderer->view, (sfVector2f){800, 600}); + sfView_setCenter(renderer->view, (sfVector2f){400, -300}); + sfRenderWindow_setView(renderer->window, renderer->view); + sfml_setup_options(engine); +} + +void sfmlrend_dtr(void *system) +{ + struct sfml_renderer_system *renderer = (struct sfml_renderer_system *)\ +system; + + sfRenderWindow_destroy(renderer->window); + sfSprite_destroy(renderer->sprite); + sfText_destroy(renderer->text); + sfView_destroy(renderer->view); +} + +const struct sfml_renderer_system sfml_renderer = { + { + name: "sfml_renderer_system", + component_name: "renderer", + size: sizeof(struct sfml_renderer_system), + ctr: &sfmlrend_ctr, + dtr: &sfmlrend_dtr, + check_dependencies: &system_check_dependencies, + update_entity: &sfml_update_entity, + destroy: &system_destroy + }, + window: NULL, + sprite: NULL, + view: NULL +}; \ No newline at end of file diff --git a/lib/gamacon/src/utility/arraylen.c b/lib/gamacon/src/utility/arraylen.c new file mode 100644 index 0000000..83aae12 --- /dev/null +++ b/lib/gamacon/src/utility/arraylen.c @@ -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); +} \ No newline at end of file diff --git a/lib/gamacon/src/utility/list.c b/lib/gamacon/src/utility/list.c new file mode 100644 index 0000000..da5154a --- /dev/null +++ b/lib/gamacon/src/utility/list.c @@ -0,0 +1,29 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** list +*/ + +#include "list.h" +#include + +gc_list *list_add(gc_list *list, void *obj) +{ + gc_list *listconst = list; + + if (!list) { + list = malloc(sizeof(gc_list)); + listconst = list; + } else { + while (list->next) + list = list->next; + list->next = malloc(sizeof(gc_list)); + list = list->next; + } + if (!list) + return (NULL); + list->data = obj; + list->next = NULL; + return (listconst); +} \ No newline at end of file diff --git a/lib/gamacon/src/utility/my_realloc.c b/lib/gamacon/src/utility/my_realloc.c new file mode 100644 index 0000000..481c696 --- /dev/null +++ b/lib/gamacon/src/utility/my_realloc.c @@ -0,0 +1,23 @@ +/* +** EPITECH PROJECT, 2020 +** Twac +** File description: +** my_realloc +*/ + +#include +#include + +void *my_realloc(void *oldptr, size_t oldsize, size_t newsize) +{ + void *new = malloc(newsize); + + if (oldptr && new) { + for (size_t i = 0; i < oldsize && i < newsize; i++) { + ((char *)new)[i] = ((char *)oldptr)[i]; + } + } + if (oldptr) + free(oldptr); + return (new); +} \ No newline at end of file diff --git a/lib/gamacon/src/utility/tupple.c b/lib/gamacon/src/utility/tupple.c new file mode 100644 index 0000000..ffdd560 --- /dev/null +++ b/lib/gamacon/src/utility/tupple.c @@ -0,0 +1,60 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** tupple +*/ + +#include "tupple.h" +#include "entity.h" +#include "utility.h" +#include + +static int tupple_add_existing(gc_tupple *li, const char *name, gc_entity *ent) +{ + while (li && my_strcmp(li->name, name)) + li = li->next; + if (!li) + return (-1); + li->entities = list_add(li->entities, ent); + return (0); +} + +gc_tupple *tupple_add(gc_tupple *list, const char *name, gc_entity *entity) +{ + gc_tupple *listconst = list; + + if (!list) { + list = malloc(sizeof(gc_tupple)); + listconst = list; + } else { + if (!tupple_add_existing(list, name, entity)) + return (listconst); + while (list->next) + list = list->next; + list->next = malloc(sizeof(gc_tupple)); + list = list->next; + } + if (!list) + return (NULL); + list->name = my_strdup(name); + list->entities = list_add(NULL, entity); + list->next = NULL; + return (listconst); +} + +void tup_remove(gc_tupple *tup, int id) +{ + gc_list *prev = NULL; + + for (gc_list *ent = tup->entities; ent; ent = ent->next) { + if (((gc_entity *)ent->data)->id == id) { + if (prev) + prev->next = ent->next; + else + tup->entities = ent->next; + return; + } + prev = ent; + } +} \ No newline at end of file diff --git a/lib/gamacon/src/utility/vector2.c b/lib/gamacon/src/utility/vector2.c new file mode 100644 index 0000000..bb61b51 --- /dev/null +++ b/lib/gamacon/src/utility/vector2.c @@ -0,0 +1,24 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** vector2 +*/ + +#include "vector2.h" +#include "utility.h" +#include "math.h" + +float gcvector_magnitude(gc_vector2 vec) +{ + return (sqrt(pow(vec.x, 2) + pow(vec.y, 2))); +} + +gc_vector2 gcvector2_normilize(gc_vector2 vec) +{ + float mag = gcvector_magnitude(vec); + + vec.x = vec.x / mag; + vec.y = vec.y / mag; + return (vec); +} \ No newline at end of file diff --git a/lib/gamacon/tests/deserializations.c b/lib/gamacon/tests/deserializations.c new file mode 100644 index 0000000..8131e99 --- /dev/null +++ b/lib/gamacon/tests/deserializations.c @@ -0,0 +1,28 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** deserializations +*/ + +#include +#include +#include "engine.h" +#include "prefab.h" + +Test(deserialization, deserialize_entity) +{ + gc_engine *engine = engine_create(); + gc_scene *scene = scene_create((const char **){ NULL }); + int ret; + + engine->change_scene(engine, scene); + ret = prefab_load(engine, "tests/player.gcprefab"); + cr_assert_eq(ret, 0); + cr_assert_neq(engine->scene, NULL); + cr_assert_eq(((gc_entity *)engine->scene->entities->data)->id, 0); + cr_assert_str_eq(((gc_entity *)engine->scene->entities->data)->components->name, "transform_component"); + cr_assert_str_eq(((gc_entity *)engine->scene->entities->data)->components->next->name, "renderer"); + cr_assert_str_eq(((gc_entity *)engine->scene->entities->data)->components->next->next->name, "movable_component"); + cr_assert_str_eq(((gc_entity *)engine->scene->entities->data)->components->next->next->next->name, "controllable_component"); +} \ No newline at end of file diff --git a/lib/gamacon/tests/game.gcprefab b/lib/gamacon/tests/game.gcprefab new file mode 100644 index 0000000..40aa56c --- /dev/null +++ b/lib/gamacon/tests/game.gcprefab @@ -0,0 +1,75 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/lib/gamacon/tests/game_loop.c b/lib/gamacon/tests/game_loop.c new file mode 100644 index 0000000..13953fe --- /dev/null +++ b/lib/gamacon/tests/game_loop.c @@ -0,0 +1,27 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** game_loop +*/ + +#include +#include "engine.h" +#include "prefab.h" + +Test(getentities, filter) +{ + gc_engine *engine = engine_create(); + gc_scene *scene = scene_create((const char **){ NULL }); + gc_list *list; + int count = 0; + + engine->change_scene(engine, scene); + prefab_load(engine, "tests/game.gcprefab"); + list = engine->scene->get_entity_by_cmp(engine->scene, "movable_component"); + while (list) { + count++; + list = list->next; + } + cr_assert_eq(count, 3); +} \ No newline at end of file diff --git a/lib/gamacon/tests/player.gcprefab b/lib/gamacon/tests/player.gcprefab new file mode 100644 index 0000000..99b741c --- /dev/null +++ b/lib/gamacon/tests/player.gcprefab @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/lib/my/Makefile b/lib/my/Makefile new file mode 100644 index 0000000..52460df --- /dev/null +++ b/lib/my/Makefile @@ -0,0 +1,62 @@ +## +## EPITECH PROJECT, 2019 +## PSU_my_printf_bootstrap_2019 +## File description: +## Makefile +## + +SRC = src/printf.c \ + src/printf_utility.c \ + src/my_putlonglong_base.c \ + src/formaters/string_formater.c \ + src/formaters/string_nonprintable_formater.c \ + src/formaters/char_formater.c \ + src/formaters/int_formater.c \ + src/formaters/uint_formater.c \ + src/formaters/ptr_formater.c \ + src/formaters/ubinary_formater.c \ + src/formaters/octal_formater.c \ + src/formaters/hexa_formater.c \ + src/formaters/big_hexa_formater.c \ + src/formaters/float_formater.c \ + src/formaters/no_format.c \ + src/get_nbr_size.c \ + my/*.c + +OBJ = *.o + +TESTS = tests/test_printf.c + +INCLUDE = -I ./include + +COVERAGE = --coverage -lcriterion + +CC = gcc + +CFLAGS = -Wall -Wshadow -Wextra -Wno-unused-parameter + +NAME = libmy.a + +UT = ut + +all: build + +build: + $(CC) -c $(SRC) $(INCLUDE) $(CFLAGS) + ar rc $(NAME) $(OBJ) + +tests_run: + $(CC) -o $(UT) $(SRC) $(TESTS) $(INCLUDE) $(COVERAGE) $(CFLAGS) + ./$(UT) + +clean: + rm -rf $(OBJ) + rm -rf *.gc* + +fclean: clean + rm -rf $(NAME) + rm -rf $(UT) + +re: fclean all + +.PHONY = all build clean fclean \ No newline at end of file diff --git a/lib/my/alphanum_helper.o b/lib/my/alphanum_helper.o new file mode 100644 index 0000000..f2fad4c Binary files /dev/null and b/lib/my/alphanum_helper.o differ diff --git a/lib/my/big_hexa_formater.o b/lib/my/big_hexa_formater.o new file mode 100644 index 0000000..1d19d19 Binary files /dev/null and b/lib/my/big_hexa_formater.o differ diff --git a/lib/my/char_formater.o b/lib/my/char_formater.o new file mode 100644 index 0000000..eb0e0da Binary files /dev/null and b/lib/my/char_formater.o differ diff --git a/lib/my/float_formater.o b/lib/my/float_formater.o new file mode 100644 index 0000000..3ef1c4e Binary files /dev/null and b/lib/my/float_formater.o differ diff --git a/lib/my/get_nbr_size.o b/lib/my/get_nbr_size.o new file mode 100644 index 0000000..e06d00a Binary files /dev/null and b/lib/my/get_nbr_size.o differ diff --git a/lib/my/hexa_formater.o b/lib/my/hexa_formater.o new file mode 100644 index 0000000..283485e Binary files /dev/null and b/lib/my/hexa_formater.o differ diff --git a/lib/my/include/formaters.h b/lib/my/include/formaters.h new file mode 100644 index 0000000..4d0d88e --- /dev/null +++ b/lib/my/include/formaters.h @@ -0,0 +1,37 @@ +/* +** EPITECH PROJECT, 2019 +** Formater struct +** File description: +** formaters +*/ + +#pragma once +#include + +#define MODIFIERS_SIZE 50 + +typedef struct formater +{ + int (*func)(va_list ap, char modifiers[MODIFIERS_SIZE]); + char flag; +} formater_t; + +int string_formater(va_list ap, char modifiers[MODIFIERS_SIZE]); +int string_nonprintable_formater(va_list ap, char modifiers[MODIFIERS_SIZE]); +int char_formater(va_list ap, char modifiers[MODIFIERS_SIZE]); +int int_formater(va_list ap, char modifiers[MODIFIERS_SIZE]); +int uint_formater(va_list ap, char modifiers[MODIFIERS_SIZE]); +int ptr_formater(va_list ap, char modifiers[MODIFIERS_SIZE]); +int ubinary_formater(va_list ap, char modifiers[MODIFIERS_SIZE]); +int octal_formater(va_list ap, char modifiers[MODIFIERS_SIZE]); +int hexa_formater(va_list ap, char modifiers[MODIFIERS_SIZE]); +int big_hexa_formater(va_list ap, char modifiers[MODIFIERS_SIZE]); +int float_formater(va_list ap, char modifiers[MODIFIERS_SIZE]); +int no_format(va_list ap, char modifiers[MODIFIERS_SIZE]); + +int count(char c, char *str); +int contains(char c, char *str); +int get_nbr_len(long long int n, const char *base); +int is_modifier(char c); +int last_mod(char *modifiers); +int my_putlonglong_base(long long int nbr, const char *base, int length); \ No newline at end of file diff --git a/lib/my/include/my.h b/lib/my/include/my.h new file mode 100644 index 0000000..3102a6f --- /dev/null +++ b/lib/my/include/my.h @@ -0,0 +1,127 @@ +/* +** EPITECH PROJECT, 2019 +** My lib +** File description: +** Header file +*/ +#pragma once + +int my_str_islower_or_num(const char *str); + +int my_printf(const char *str, ...); + +void print_ptr(void *ptr); + +void print_str_to(char *ptr, int max); + +int count_valid_queens_placements(int n); + +int my_compute_power_it(int n, int p); + +int my_compute_power_rec(int n, int p); + +int my_compute_factorial_it(int n); + +int my_compute_factorial_rec(int n); + +int my_sqrt(int nb); + +char *my_evil_str(char *str); + +int my_find_prime_sup(int n); + +int my_getnbr_base(const char *str, const char *base); + +int my_getnbr(const char *str); + +int my_isneg(int n); + +int my_is_prime(int n); + +void my_print_alpha(void); + +void my_print_comb2(void); + +void my_print_combn(int n); + +void my_print_comb(void); + +void my_print_digits(void); + +void my_print_revalpha(void); + +void my_putchar(char c); + +int my_putlong_base(long long nbr, const char *base); + +void my_putnbr_base(int n, const char *base); + +void my_put_nbr(int n); + +void my_putstr(const char *str); + +void my_revstr(char *str); + +int my_showmem(char *str, int size); + +void my_showstr(const char *str); + +void my_sort_int_array(int *array); + +int is_letter(char c); + +int is_alpha(char c); + +int is_num(char c); + +int is_digit(char c); + +char *my_strcapitalize(char *str); + +char *my_strcat(char *dest, const char *str); + +int my_strcmp(const char *s1, const char *s2); + +char *my_strcpy(char *dest, const char *src); + +int my_str_isalpha(const char *str); + +int is_lowercase(char c); + +int my_str_islower(const char *str); + +int my_str_isnum(const char *str); + +int is_printable(char c); +int my_str_isprintable(const char *str); + +int is_upper(char c); +int my_str_isupper(const char *str); + +int my_strlen(const char *str); + +int my_strlowcase(const char *str); + +char *my_strncat(char *dest, const char *str, int n); + +int my_strncmp(const char *s1, const char *s2, int n); + +char *my_strncpy(char *dest, const char *src, int n); + +char *my_strstr(const char *str, const char *to_find); + +int my_strupcase(const char *str); + +void my_swap(int *a, int *b); + +char *my_strdup(const char *str); + +char **my_str_to_word_array(const char *str); + +int my_show_word_array(const char **words); + +int is_alphanum(char c); + +int first_alphanum(const char *str); + +int index_of(const char *str, char c); \ No newline at end of file diff --git a/lib/my/index_of.o b/lib/my/index_of.o new file mode 100644 index 0000000..6cd4389 Binary files /dev/null and b/lib/my/index_of.o differ diff --git a/lib/my/int_formater.o b/lib/my/int_formater.o new file mode 100644 index 0000000..88ffe6a Binary files /dev/null and b/lib/my/int_formater.o differ diff --git a/lib/my/libmy.a b/lib/my/libmy.a new file mode 100644 index 0000000..ada5ea6 Binary files /dev/null and b/lib/my/libmy.a differ diff --git a/lib/my/my/alphanum_helper.c b/lib/my/my/alphanum_helper.c new file mode 100644 index 0000000..25a02b9 --- /dev/null +++ b/lib/my/my/alphanum_helper.c @@ -0,0 +1,24 @@ +/* +** EPITECH PROJECT, 2019 +** Helper for the task 4 +** File description: +** alphanum_helper +*/ + +int is_alpha(char c); + +int is_digit(char c); + +int is_alphanum(char c) +{ + return (is_alpha(c) || is_digit(c)); +} + +int first_alphanum(const char *str) +{ + for (int i = 0; str[i]; i++) { + if (is_alphanum(str[i])) + return i; + } + return (-1); +} diff --git a/lib/my/my/index_of.c b/lib/my/my/index_of.c new file mode 100644 index 0000000..733857c --- /dev/null +++ b/lib/my/my/index_of.c @@ -0,0 +1,15 @@ +/* +** EPITECH PROJECT, 2019 +** String helper +** File description: +** Index of char in a string +*/ + +int index_of(const char *str, const char c) +{ + for (int i = 0; str[i]; i++) { + if (str[i] == c) + return (i); + } + return (-1); +} \ No newline at end of file diff --git a/lib/my/my/my_compute_factorial_it.c b/lib/my/my/my_compute_factorial_it.c new file mode 100644 index 0000000..37f87bc --- /dev/null +++ b/lib/my/my/my_compute_factorial_it.c @@ -0,0 +1,22 @@ +/* +** EPITECH PROJECT, 2019 +** CPool_Day05_2019 +** File description: +** Factorials +*/ + +int my_compute_factorial_it(int nb) +{ + if (nb < 0) + return 0; + + int ret = 1; + while (nb > 0) { + ret *= nb; + nb--; + + if (ret < 0) + return 0; + } + return ret; +} diff --git a/lib/my/my/my_compute_factorial_rec.c b/lib/my/my/my_compute_factorial_rec.c new file mode 100644 index 0000000..de2d6e4 --- /dev/null +++ b/lib/my/my/my_compute_factorial_rec.c @@ -0,0 +1,25 @@ +/* +** EPITECH PROJECT, 2019 +** CPool_Day05_2019 +** File description: +** Factorials but in recursive mode +*/ + +int compute(int nb, int ret) +{ + if (ret < 0) + return (0); + + if (nb == 0) + return (ret); + + return (compute(nb - 1, ret * nb)); +} + +int my_compute_factorial_rec(int nb) +{ + if (nb == 0) + return (1); + + return (compute(nb, 1)); +} diff --git a/lib/my/my/my_compute_power_it.c b/lib/my/my/my_compute_power_it.c new file mode 100644 index 0000000..a9481bb --- /dev/null +++ b/lib/my/my/my_compute_power_it.c @@ -0,0 +1,29 @@ +/* +** EPITECH PROJECT, 2019 +** CPool_Day05_2019 +** File description: +** Power calculator +*/ + +int my_pow(int nb, int p) +{ + int ret = 1; + int mult = 1; + + if (p < 0) + return 0; + if (nb < 0) { + nb *= -1; + if (p % 2 == 1) + mult = -1; + } + while (p > 0) { + ret *= nb; + p--; + + if (ret < 0) + return (0); + } + + return (ret * mult); +} diff --git a/lib/my/my/my_compute_square_root.c b/lib/my/my/my_compute_square_root.c new file mode 100644 index 0000000..16e03e6 --- /dev/null +++ b/lib/my/my/my_compute_square_root.c @@ -0,0 +1,22 @@ +/* +** EPITECH PROJECT, 2019 +** CPool_Day05_2019 +** File description: +** Square root calculator +*/ + +int my_sqrt(int nb) +{ + if (nb < 0) + return 0; + + for (int i = 0; 1; i++) { + int power = i * i; + if (power == nb) + return i; + else if (power > nb) + return 0; + } + + return 0; +} diff --git a/lib/my/my/my_evil_str.c b/lib/my/my/my_evil_str.c new file mode 100644 index 0000000..3487c64 --- /dev/null +++ b/lib/my/my/my_evil_str.c @@ -0,0 +1,28 @@ +/* +** EPITECH PROJECT, 2019 +** CPool_Day04_2019 +** File description: +** Reverse string +*/ + +int my_putchar(char c); + +char *my_evil_str(char *str) +{ + int count = 0; + char buf[1598246]; + + while (1) { + buf[count] = str[count]; + if (*(str + count) != '\0') + count++; + else + break; + } + + for (int i = 0; i < count; i++) + str[i] = buf[count - i - 1]; + + str[count + 1] = '\0'; + return str; +} diff --git a/lib/my/my/my_find_prime_sup.c b/lib/my/my/my_find_prime_sup.c new file mode 100644 index 0000000..50ca6a8 --- /dev/null +++ b/lib/my/my/my_find_prime_sup.c @@ -0,0 +1,18 @@ +/* +** EPITECH PROJECT, 2019 +** CPool_Day05_2019 +** File description: +** Find a prime number greater than the given number +*/ + +int my_is_prime(int n); + +int my_find_prime_sup(int n) +{ + if (n <= 2) + return (2); + while (!my_is_prime(n)) + n++; + + return n; +} diff --git a/lib/my/my/my_getnbr.c b/lib/my/my/my_getnbr.c new file mode 100755 index 0000000..e5832e0 --- /dev/null +++ b/lib/my/my/my_getnbr.c @@ -0,0 +1,69 @@ +/* +** EPITECH PROJECT, 2019 +** CPool_Day04_2019 +** File description: +** Int parser +*/ + +int my_putchar(char c); + +static int parse(const char *str, int digit_count, int neg) +{ + int n = 0; + int add; + for (int power = 0; power < digit_count; power++) { + add = str[digit_count - power - 1] - '0'; + if (power == 10 && add > 2) + return 0; + + add *= neg; + for (int i = 0; i < power; i++) { + add *= 10; + if (add * neg < 0) + return 0; + } + n += add; + + if ((neg == -1 && n > 0) || (neg == 1 && n < 0)) + return 0; + } + return n; +} + +static int init_print(const char *str, int count, int s_index) +{ + int neg = 1; + + for (char c = str[s_index]; c == '-' || c == '+'; c = str[s_index]) { + if (c == '-') + neg *= -1; + s_index++; + count--; + } + + char buf[count]; + for (int i = 0; i < count; i++) + buf[i] = str[s_index + i]; + + + return parse(buf, count, neg); +} + +int my_getnbr(const char *str) +{ + int count = 0; + int start_index = -1; + char c; + + for (int i = 0; 1; i++) { + c = str[i]; + if ((c >= '0' && c <= '9') || c == '-' || c == '+') { + if (start_index == -1) + start_index = i; + count++; + } + else if (count > 0 || c == '\0') + break; + } + return init_print(str, count, start_index); +} diff --git a/lib/my/my/my_getnbr_base.c b/lib/my/my/my_getnbr_base.c new file mode 100644 index 0000000..e309364 --- /dev/null +++ b/lib/my/my/my_getnbr_base.c @@ -0,0 +1,75 @@ +/* +** EPITECH PROJECT, 2019 +** Get number with any base +** File description: +** Int library +*/ + +int my_strlen(const char *str); + +int my_pow(int i, int p); + +static int indexof(char c, const char *str) +{ + for (int i = 0; str[i] != '\0'; i++) { + if (str[i] == c) + return (i); + } + return (-1); +} + +static int parse(char *str, const char *base, long n, int i) +{ + int str_length = my_strlen(str); + int base_length = my_strlen(base); + int power; + int neg = 1; + + if (i < 0) { + neg = -1; + i *= -1; + } + power = my_pow(base_length, i - 1); + n += indexof(str[str_length - i], base) * power * neg; + if ((n > 0 && neg < 0) || (n < 0 && neg > 0)) + return (0); + if (str_length - i > 0) + return (parse(str, base, n, (i + 1) * neg)); + return (n); +} + +static int get_digit_count(const char *str, const char *base) +{ + int count = 0; + + for (int i = 0; str[i] != '\0'; i++) { + if (indexof(str[i], base) != -1) + count++; + else if (count > 0) + return (count); + } + return (count); +} + +int my_getnbr_base(const char *str, const char *base) +{ + int length = get_digit_count(str, base); + char num[length + 1]; + int start_index = 0; + int neg = 1; + + if (length == 0) + return 0; + for (int i = 0; str[i] != '\0'; i++) { + if (indexof(str[i], base) != -1) { + num[start_index] = str[i]; + start_index++; + } + else if (str[i] == '-') + neg *= -1; + else + break; + } + num[length] = '\0'; + return (parse(num, base, 0, neg)); +} diff --git a/lib/my/my/my_is_prime.c b/lib/my/my/my_is_prime.c new file mode 100644 index 0000000..18c5723 --- /dev/null +++ b/lib/my/my/my_is_prime.c @@ -0,0 +1,26 @@ +/* +** EPITECH PROJECT, 2019 +** CPool_Day05_2019 +** File description: +** Check if a number is a prime number +*/ + +int my_compute_square_root(int n); + +int my_is_prime(int n) +{ + int square; + + if (n <= 1) + return (0); + if (n == 2) + return (1); + for (int i = 0; i * i <= n; i++) + square = i; + for (int i = 2; i < square + 1; i++) { + if (n % i == 0) + return 0; + } + + return (1); +} diff --git a/lib/my/my/my_isneg.c b/lib/my/my/my_isneg.c new file mode 100644 index 0000000..9a91bd3 --- /dev/null +++ b/lib/my/my/my_isneg.c @@ -0,0 +1,17 @@ +/* +** EPITECH PROJECT, 2019 +** CPool_Day03_2019 +** File description: +** Task 4 +*/ + +int my_putchar(char c); + +int my_isneg(int n) +{ + if (n < 0) + my_putchar('N'); + else + my_putchar('P'); + return (0); +} diff --git a/lib/my/my/my_print_alpha.c b/lib/my/my/my_print_alpha.c new file mode 100644 index 0000000..9139faa --- /dev/null +++ b/lib/my/my/my_print_alpha.c @@ -0,0 +1,16 @@ +/* +** EPITECH PROJECT, 2019 +** CPool_Day03_2019 +** File description: +** First task +*/ + +int my_putchar(char c); + +int my_print_alpha(void) +{ + for (char i = 'a'; i <= 'z'; i++) { + my_putchar(i); + } + return (0); +} diff --git a/lib/my/my/my_print_comb.c b/lib/my/my/my_print_comb.c new file mode 100644 index 0000000..12b5280 --- /dev/null +++ b/lib/my/my/my_print_comb.c @@ -0,0 +1,46 @@ +/* +** EPITECH PROJECT, 2019 +** CPool_Day03_2019 +** File description: +** Task 5 +*/ + +int my_putchar(char c); + +static void print_c(char x, char y, char z) +{ + my_putchar(x); + my_putchar(y); + my_putchar(z); +} + +static void increment_y(char *x, char *y, char *z) +{ + *x = *x + 1; + *y = *x + 1; + *z = *y + 1; +} + +int my_print_comb(void) +{ + char x = '0'; + char y = '1'; + char z = '2'; + + while (1) { + print_c(x, y, z); + z++; + if (z > '9') { + y++; + z = y + 1; + } + if (y > '8') + increment_y(&x, &y, &z); + if (x <= '7') { + my_putchar(','); + my_putchar(' '); + } + else + return 0; + } +} diff --git a/lib/my/my/my_print_comb2.c b/lib/my/my/my_print_comb2.c new file mode 100644 index 0000000..fc60b16 --- /dev/null +++ b/lib/my/my/my_print_comb2.c @@ -0,0 +1,36 @@ +/* +** EPITECH PROJECT, 2019 +** CPool_Day03_2019 +** File description: +** Task 6 +*/ + +int my_putchar(char c); + +static void print_c2(int i, int j) +{ + my_putchar('0' + i / 10); + my_putchar('0' + i % 10); + my_putchar(' '); + my_putchar('0' + j / 10); + my_putchar('0' + j % 10); + + if (i != 98) { + my_putchar(','); + my_putchar(' '); + } +} + +static void get_group2(int i) +{ + for (int j = i + 1; j <= 99; j++) + print_c2(i, j); +} + +int my_print_comb2(void) +{ + for (int i = 0; i <= 98; i++) + get_group2(i); + + return (0); +} diff --git a/lib/my/my/my_print_combn.c b/lib/my/my/my_print_combn.c new file mode 100644 index 0000000..2907386 --- /dev/null +++ b/lib/my/my/my_print_combn.c @@ -0,0 +1,62 @@ +/* +** EPITECH PROJECT, 2019 +** CPool_Day03_2019 +** File description: +** Task 9 +*/ + +int my_putchar(char c); + +static int is_nbr_valid(int n, int i) +{ + if (n < 9) { + if (n >= i) + return (0); + else + return (1); + } + int j = n % 10; + + if (j >= i) + return (0); + return is_nbr_valid(n / 10, j); +} + +static void print_cn(int n) +{ + if (n > 9) + print_cn(n / 10); + + my_putchar('0' + n % 10); +} + +static void process_valid_number(int n, int starting, int last_valid) +{ + if (last_valid) { + my_putchar(','); + my_putchar(' '); + } + if (n < starting && starting != 1) + my_putchar('0'); + print_cn(n); +} + +int my_print_combn(int n) +{ + if (n > 10) + return 0; + + int starting = 1; + int last_valid = 0; + + for (int i = 0; i < n - 1; i++) + starting *= 10; + + for (int i = starting / 10; i < starting * 10; i++) { + if (is_nbr_valid(i, 10)) { + process_valid_number(i, starting, last_valid); + last_valid = 1; + } + } + return (0); +} diff --git a/lib/my/my/my_print_digits.c b/lib/my/my/my_print_digits.c new file mode 100644 index 0000000..69b9d48 --- /dev/null +++ b/lib/my/my/my_print_digits.c @@ -0,0 +1,16 @@ +/* +** EPITECH PROJECT, 2019 +** CPool_Day03_2019 +** File description: +** Task 3 +*/ + +int my_putchar(char n); + +int my_print_digits(void) +{ + for (char i = '0'; i <= '9'; i++) { + my_putchar(i); + } + return (0); +} diff --git a/lib/my/my/my_print_revalpha.c b/lib/my/my/my_print_revalpha.c new file mode 100644 index 0000000..c70547f --- /dev/null +++ b/lib/my/my/my_print_revalpha.c @@ -0,0 +1,16 @@ +/* +** EPITECH PROJECT, 2019 +** CPool_Day03_2019 +** File description: +** The second task +*/ + +int my_putchar(char c); + +int my_print_revalpha(void) +{ + for (char i = 'z'; i >= 'a'; i--) { + my_putchar(i); + } + return (0); +} diff --git a/lib/my/my/my_put_nbr.c b/lib/my/my/my_put_nbr.c new file mode 100644 index 0000000..6291012 --- /dev/null +++ b/lib/my/my/my_put_nbr.c @@ -0,0 +1,30 @@ +/* +** EPITECH PROJECT, 2019 +** CPool_Day03_2019 +** File description: +** Task 07 +*/ + +int my_putchar(char c); + +static void get_digit(long digit) +{ + long n = digit; + char c = '0' + n % 10; + if (n > 9) { + n /= 10; + get_digit(n); + } + my_putchar(c); +} + +int my_put_nbr(int n) +{ + long digit = n; + if (n < 0) { + my_putchar('-'); + digit *= -1; + } + get_digit(digit); + return (0); +} diff --git a/lib/my/my/my_putchar.c b/lib/my/my/my_putchar.c new file mode 100644 index 0000000..9e71ab6 --- /dev/null +++ b/lib/my/my/my_putchar.c @@ -0,0 +1,13 @@ +/* +** EPITECH PROJECT, 2019 +** Putchar +** File description: +** Yes +*/ + +#include + +void my_putchar(char c) +{ + write(1, &c, 1); +} diff --git a/lib/my/my/my_putlong_base.c b/lib/my/my/my_putlong_base.c new file mode 100644 index 0000000..afdcd5c --- /dev/null +++ b/lib/my/my/my_putlong_base.c @@ -0,0 +1,38 @@ +/* +** EPITECH PROJECT, 2019 +** Put nbr in a custom base +** File description: +** Might be useful later +*/ + +#include + +int my_strlen(const char *str); + +static void display_a_digit(char c) +{ + write(1, &c, 1); +} + +static void put_digit(long n, int base_length, const char *base) +{ + if (n > base_length - 1) + put_digit(n / base_length, base_length, base); + display_a_digit(base[n % base_length]); +} + +int my_putlong_base(long nbr, const char *base) +{ + int base_length = my_strlen(base); + + if (base_length < 2) { + display_a_digit('0'); + return (0); + } + if (nbr < 0) { + nbr *= -1; + display_a_digit('-'); + } + put_digit(nbr, base_length, base); + return (0); +} diff --git a/lib/my/my/my_putnbr_base.c b/lib/my/my/my_putnbr_base.c new file mode 100644 index 0000000..ac6ad41 --- /dev/null +++ b/lib/my/my/my_putnbr_base.c @@ -0,0 +1,38 @@ +/* +** EPITECH PROJECT, 2019 +** Put nbr in a custom base +** File description: +** Might be useful later +*/ + +#include + +int my_strlen(const char *str); + +static void display_a_digit(char c) +{ + write(1, &c, 1); +} + +static void put_digit(int n, int base_length, const char *base) +{ + if (n > base_length - 1) + put_digit(n / base_length, base_length, base); + display_a_digit(base[n % base_length]); +} + +int my_putnbr_base(int nbr, const char *base) +{ + int base_length = my_strlen(base); + + if (base_length < 2) { + display_a_digit('0'); + return (0); + } + if (nbr < 0) { + nbr *= -1; + display_a_digit('-'); + } + put_digit(nbr, base_length, base); + return (0); +} diff --git a/lib/my/my/my_putstr.c b/lib/my/my/my_putstr.c new file mode 100644 index 0000000..62611d5 --- /dev/null +++ b/lib/my/my/my_putstr.c @@ -0,0 +1,18 @@ +/* +** EPITECH PROJECT, 2019 +** CPool_Day04_2019 +** File description: +** Put string +*/ + +int my_putchar(char c); + +int my_putstr(const char *str) +{ + for (int i = 0; 1; i++) { + char c = *(str + i); + if (c == '\0') + return (0); + my_putchar(c); + } +} diff --git a/lib/my/my/my_revstr.c b/lib/my/my/my_revstr.c new file mode 100644 index 0000000..1de6af6 --- /dev/null +++ b/lib/my/my/my_revstr.c @@ -0,0 +1,21 @@ +/* +** EPITECH PROJECT, 2019 +** CPool_Day04_2019 +** File description: +** Reverse string +*/ + +int my_strlen(char *str); + +char *my_revstr(char *str) +{ + int length = my_strlen(str); + char dup[length + 1]; + + for (int i = 0; i < length; i++) + dup[i] = str[i]; + for (int i = 0; i < length; i++) + str[i] = dup[length - 1 - i]; + str[length] = '\0'; + return (str); +} diff --git a/lib/my/my/my_show_words_array.c b/lib/my/my/my_show_words_array.c new file mode 100644 index 0000000..f72f5a7 --- /dev/null +++ b/lib/my/my/my_show_words_array.c @@ -0,0 +1,19 @@ +/* +** EPITECH PROJECT, 2019 +** Show an array of words on the screen +** File description: +** Nothing to say +*/ + +void my_putstr(const char *str); + +void my_putchar(char c); + +int my_show_word_array(const char *words[]) +{ + for (int i = 0; words[i]; i++) { + my_putstr(words[i]); + my_putchar('\n'); + } + return (0); +} diff --git a/lib/my/my/my_showmem.c b/lib/my/my/my_showmem.c new file mode 100644 index 0000000..597ca1c --- /dev/null +++ b/lib/my/my/my_showmem.c @@ -0,0 +1,74 @@ +/* +** EPITECH PROJECT, 2019 +** Memory dump +** File description: +** Debug Library +*/ + +const char *hex = "0123456789abcdef"; + +#include "my.h" +#include + +int get_size(long n, const char *base); + +void my_putchar(char c); + +int is_printable(char c); + +void display_x_char(char c, int count) +{ + for (int i = 0; i < count; i++) + write(1, &c, 1); +} + +void print_ptr(void *ptr) +{ + write(1, "0x", 2); + my_putlong_base((long)ptr, hex); +} + +void print_hexa(char *ptr, int size) +{ + for (int i = 0; i < 16; i++) { + if (i >= size) { + display_x_char(' ', 40 - (i * 2 + i / 2)); + return; + } + if (ptr[i] < 16) + my_putchar('0'); + my_putlong_base((long)ptr[i], hex); + if (i % 2 == 1) + my_putchar(' '); + } +} + +void print_str_to(char *ptr, int max) +{ + int overflow = 0; + + for (int i = 0; i < max; i++) { + if (overflow || !is_printable(ptr[i])) + my_putchar('.'); + else + my_putchar(ptr[i]); + if (!overflow) + overflow = ptr[i] == '\0'; + } +} + +int my_showmem(char *str, int size) +{ + int padding = size % 16; + int line_count = padding == 0 ? size / 16 : size / 16 + 1; + char *ptr; + + for (int i = 0; i < line_count; i++) { + ptr = (str + i * 16); + print_ptr((void *)(ptr - str)); + print_hexa(ptr, size - i * 16); + print_str_to(ptr, 16); + my_putchar('\n'); + } + return (0); +} diff --git a/lib/my/my/my_showstr.c b/lib/my/my/my_showstr.c new file mode 100644 index 0000000..fdcecd2 --- /dev/null +++ b/lib/my/my/my_showstr.c @@ -0,0 +1,33 @@ +/* +** EPITECH PROJECT, 2019 +** Show Str With non printable values in hexa +** File description: +** Debugger Library +*/ + +#include + +int is_printable(char c); + +int my_putnbr_base(int i, const char *base); + +void puthex(char c) +{ + write(1, "\\", 1); + if (c < 8) + write(1, "0", 1); + if (c < 16) + write(1, "0", 1); + my_putnbr_base(c, "01234567"); +} + +int my_showstr(const char *str) +{ + for (int i = 0; str[i]; i++) { + if (is_printable(str[i])) + write(1, &(str[i]), 1); + else + puthex(str[i]); + } + return (0); +} diff --git a/lib/my/my/my_sort_int_array.c b/lib/my/my/my_sort_int_array.c new file mode 100644 index 0000000..643dc42 --- /dev/null +++ b/lib/my/my/my_sort_int_array.c @@ -0,0 +1,24 @@ +/* +** EPITECH PROJECT, 2019 +** CPool_Day04_2019 +** File description: +** Int array sorter +*/ + +static void sort_linear(int *array, int size) +{ + for (int i = 0; i < size - 1; i++) { + if (array[i] > array[i + 1]) { + int buf = array[i]; + array[i] = array[i + 1]; + array[i + 1] = buf; + } + } +} + +int my_sort_int_array(int *array, int size) +{ + for (int i = 0; i < size; i++) + sort_linear(array, size); + return (0); +} diff --git a/lib/my/my/my_str_isalpha.c b/lib/my/my/my_str_isalpha.c new file mode 100644 index 0000000..046c0fe --- /dev/null +++ b/lib/my/my/my_str_isalpha.c @@ -0,0 +1,24 @@ +/* +** EPITECH PROJECT, 2019 +** Str is alpha +** File description: +** Duplicate of the string.h (I think) +*/ + +int is_alpha(char c) +{ + if (c >= 'a' && c <= 'z') + return (1); + if (c >= 'A' && c <= 'Z') + return (1); + return (0); +} + +int my_str_isalpha(const char *str) +{ + for (int i = 0; str[i] != '\0'; i++) { + if (!is_alpha(str[i])) + return (0); + } + return (1); +} diff --git a/lib/my/my/my_str_islower.c b/lib/my/my/my_str_islower.c new file mode 100644 index 0000000..56d38c5 --- /dev/null +++ b/lib/my/my/my_str_islower.c @@ -0,0 +1,33 @@ +/* +** EPITECH PROJECT, 2019 +** Str is alpha +** File description: +** Duplicate of the string.h (I think) +*/ + +int is_digit(char c); + +int is_lowercase(char c) +{ + if (c >= 'a' && c <= 'z') + return (1); + return (0); +} + +int my_str_islower(const char *str) +{ + for (int i = 0; str[i] != '\0'; i++) { + if (!is_lowercase(str[i])) + return (0); + } + return (1); +} + +int my_str_islower_or_num(const char *str) +{ + for (int i = 0; str[i] != '\0'; i++) { + if (!is_lowercase(str[i]) && !is_digit(str[i])) + return (0); + } + return (1); +} diff --git a/lib/my/my/my_str_isnum.c b/lib/my/my/my_str_isnum.c new file mode 100644 index 0000000..2978430 --- /dev/null +++ b/lib/my/my/my_str_isnum.c @@ -0,0 +1,22 @@ +/* +** EPITECH PROJECT, 2019 +** Str is alpha +** File description: +** Duplicate of the string.h (I think) +*/ + +int is_digit(char c) +{ + if (c >= '0' && c <= '9') + return (1); + return (0); +} + +int my_str_isnum(const char *str) +{ + for (int i = 0; str[i] != '\0'; i++) { + if (!is_digit(str[i])) + return (0); + } + return (1); +} diff --git a/lib/my/my/my_str_isprintable.c b/lib/my/my/my_str_isprintable.c new file mode 100644 index 0000000..13a8f9e --- /dev/null +++ b/lib/my/my/my_str_isprintable.c @@ -0,0 +1,22 @@ +/* +** EPITECH PROJECT, 2019 +** Str is alpha +** File description: +** Duplicate of the string.h (I think) +*/ + +int is_printable(char c) +{ + if (c >= ' ' && c < 127) + return (1); + return (0); +} + +int my_str_isprintable(const char *str) +{ + for (int i = 0; str[i] != '\0'; i++) { + if (!is_printable(str[i])) + return (0); + } + return (1); +} diff --git a/lib/my/my/my_str_isupper.c b/lib/my/my/my_str_isupper.c new file mode 100644 index 0000000..ec4c8f7 --- /dev/null +++ b/lib/my/my/my_str_isupper.c @@ -0,0 +1,22 @@ +/* +** EPITECH PROJECT, 2019 +** Str is alpha +** File description: +** Duplicate of the string.h (I think) +*/ + +int is_upper(char c) +{ + if (c >= 'A' && c <= 'Z') + return (1); + return (0); +} + +int my_str_isupper(const char *str) +{ + for (int i = 0; str[i] != '\0'; i++) { + if (!is_upper(str[i])) + return (0); + } + return (1); +} diff --git a/lib/my/my/my_str_to_word_array.c b/lib/my/my/my_str_to_word_array.c new file mode 100644 index 0000000..97a191a --- /dev/null +++ b/lib/my/my/my_str_to_word_array.c @@ -0,0 +1,70 @@ +/* +** EPITECH PROJECT, 2019 +** Convert a string to a char ** +** File description: +** Split with every non alpha-numerical characters +*/ + +#include + +int is_alphanum(char c); + +int first_alphanum(const char *str); + +static int count_words(const char *str) +{ + int count = 1; + + if (first_alphanum(str) == -1) + return (0); + for (int i = first_alphanum(str); str[i + 1]; i++) { + if (!is_alphanum(str[i]) && is_alphanum(str[i + 1])) + count++; + } + return (count); +} + +static int get_word_length(const char *str, int n) +{ + int count = 0; + int word_length = 0; + + for (int i = first_alphanum(str); str[i]; i++) { + if (!is_alphanum(str[i]) && is_alphanum(str[i + 1])) + count++; + else if (count == n && is_alphanum(str[i])) + word_length++; + } + return (word_length); +} + +static char *get_word(const char *str, int n) +{ + int length = get_word_length(str, n); + char *ret = malloc(sizeof(*ret) * (length + 1)); + int count = 0; + int word_length = 0; + + for (int i = first_alphanum(str); word_length != length; i++) { + if (!is_alphanum(str[i]) && is_alphanum(str[i + 1])) + count++; + else if (count == n) { + ret[word_length] = str[i]; + word_length++; + } + } + ret[length] = '\0'; + return (ret); +} + +char **my_str_to_word_array(const char *str) +{ + int count = count_words(str); + char **ret = malloc(sizeof(*ret) * (count + 1)); + + for (int i = 0; i < count; i++) { + ret[i] = get_word(str, i); + } + ret[count] = 0; + return (ret); +} diff --git a/lib/my/my/my_strcapitalize.c b/lib/my/my/my_strcapitalize.c new file mode 100644 index 0000000..65b932f --- /dev/null +++ b/lib/my/my/my_strcapitalize.c @@ -0,0 +1,32 @@ +/* +** EPITECH PROJECT, 2019 +** ToUpercase +** File description: +** string.h +*/ + +int is_num(char c) +{ + return (c >= '0' && c <= '9'); +} + +int is_letter(char c) +{ + return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')); +} + +char *my_strcapitalize(char *str) +{ + int capitalize_next = 1; + + for (int i = 0; str[i] != '\0'; i++) { + if (capitalize_next && str[i] > 'a' && str[i] < 'z') + str[i] += 'A' - 'a'; + + if (is_num(str[i]) || is_letter(str[i])) + capitalize_next = 1; + else + capitalize_next = 0; + } + return (str); +} diff --git a/lib/my/my/my_strcat.c b/lib/my/my/my_strcat.c new file mode 100644 index 0000000..1bc8e8e --- /dev/null +++ b/lib/my/my/my_strcat.c @@ -0,0 +1,19 @@ +/* +** EPITECH PROJECT, 2019 +** String cat +** File description: +** The end of the string.h lib +*/ + +int my_strlen(const char *dest); + +char *my_strcat(char *dest, const char *src) +{ + int i; + int dest_length = my_strlen(dest); + + for (i = 0; src[i]; i++) + dest[dest_length + i] = src[i]; + dest[dest_length + i] = '\0'; + return (dest); +} diff --git a/lib/my/my/my_strchr.c b/lib/my/my/my_strchr.c new file mode 100644 index 0000000..8bd6717 --- /dev/null +++ b/lib/my/my/my_strchr.c @@ -0,0 +1,17 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** my_strchr +*/ + +#include "stddef.h" + +char *my_strchr(char *str, char c) +{ + for (int i = 0; str[i]; i++) { + if (str[i] == c) + return (&str[i]); + } + return (NULL); +} \ No newline at end of file diff --git a/lib/my/my/my_strcmp.c b/lib/my/my/my_strcmp.c new file mode 100644 index 0000000..e3a4972 --- /dev/null +++ b/lib/my/my/my_strcmp.c @@ -0,0 +1,25 @@ +/* +** EPITECH PROJECT, 2019 +** StrCmp +** File description: +** String.h duplicate +*/ + +int my_strcmp(const char *str1, const char *str2) +{ + int i; + int ret; + + for (i = 0; str1[i] != '\0'; i++) { + if (str2[i] == '\0') + return str1[i]; + ret = str1[i] - str2[i]; + if (ret != 0) + return (ret); + } + + if (str2[i] == '\0') + return (0); + else + return -str2[i]; +} diff --git a/lib/my/my/my_strcpy.c b/lib/my/my/my_strcpy.c new file mode 100644 index 0000000..a1bf550 --- /dev/null +++ b/lib/my/my/my_strcpy.c @@ -0,0 +1,16 @@ +/* +** EPITECH PROJECT, 2019 +** String copy +** File description: +** Copy an entire string +*/ + +char *my_strcpy(char *dest, const char *src) +{ + int i; + + for (i = 0; src[i] != '\0'; i++) + dest[i] = src[i]; + dest[i] = '\0'; + return (dest); +} diff --git a/lib/my/my/my_strdup.c b/lib/my/my/my_strdup.c new file mode 100644 index 0000000..1ae14cc --- /dev/null +++ b/lib/my/my/my_strdup.c @@ -0,0 +1,21 @@ +/* +** EPITECH PROJECT, 2019 +** Str dup replicate +** File description: +** MALLOC YES +*/ + +#include + +int my_strlen(const char *src); + +char *my_strdup(const char *src) +{ + int length = my_strlen(src); + char *ret = malloc(sizeof(char) * (length + 1)); + + for (int i = 0; i < length; i++) + ret[i] = src[i]; + ret[length] = '\0'; + return (ret); +} diff --git a/lib/my/my/my_strlen.c b/lib/my/my/my_strlen.c new file mode 100644 index 0000000..82c15a2 --- /dev/null +++ b/lib/my/my/my_strlen.c @@ -0,0 +1,14 @@ +/* +** EPITECH PROJECT, 2019 +** CPool_Day04_2019 +** File description: +** Strlen +*/ + +int my_strlen(const char *str) +{ + for (int i = 0; 1; i++) { + if (*(str + i) == '\0') + return i; + } +} diff --git a/lib/my/my/my_strlowcase.c b/lib/my/my/my_strlowcase.c new file mode 100644 index 0000000..54adc9b --- /dev/null +++ b/lib/my/my/my_strlowcase.c @@ -0,0 +1,15 @@ +/* +** EPITECH PROJECT, 2019 +** ToUpercase +** File description: +** string.h +*/ + +char *my_strlowcase(char *str) +{ + for (int i = 0; str[i] != '\0'; i++) { + if (str[i] > 'A' && str[i] < 'Z') + str[i] += 'a' - 'A'; + } + return (str); +} diff --git a/lib/my/my/my_strncat.c b/lib/my/my/my_strncat.c new file mode 100644 index 0000000..7e5622d --- /dev/null +++ b/lib/my/my/my_strncat.c @@ -0,0 +1,19 @@ +/* +** EPITECH PROJECT, 2019 +** String N Cat +** File description: +** End of the string.h lib +*/ + +int my_strlen(const char *str); + +char *my_strncat(char *dest, const char *src, int n) +{ + int i; + int dest_length = my_strlen(dest); + + for (i = 0; i < n && src[i]; i++) + dest[dest_length + i] = src[i]; + dest[dest_length + i] = '\0'; + return (dest); +} diff --git a/lib/my/my/my_strncmp.c b/lib/my/my/my_strncmp.c new file mode 100644 index 0000000..c5918a1 --- /dev/null +++ b/lib/my/my/my_strncmp.c @@ -0,0 +1,25 @@ +/* +** EPITECH PROJECT, 2019 +** StrNCmp +** File description: +** String.h duplicate +*/ + +int my_strncmp(const char *str1, const char *str2, int n) +{ + int i; + int ret; + + for (i = 0; i < n && str1[i] != '\0'; i++) { + if (str2[i] == '\0') + return str1[i]; + ret = str1[i] - str2[i]; + if (ret != 0) + return (ret); + } + + if (i == n || str2[i] == '\0') + return (0); + else + return -str2[i]; +} diff --git a/lib/my/my/my_strncpy.c b/lib/my/my/my_strncpy.c new file mode 100644 index 0000000..cd39de4 --- /dev/null +++ b/lib/my/my/my_strncpy.c @@ -0,0 +1,21 @@ +/* +** EPITECH PROJECT, 2019 +** String copy n first chars +** File description: +** Same but different +*/ + +char *my_strncpy(char *dest, const char* src, int n) +{ + int overflow = 0; + + for (int i = 0; i < n; i++) { + if (overflow) + dest[i] = '\0'; + else { + dest[i] = src[i]; + overflow = (src[i] == '\0'); + } + } + return (dest); +} diff --git a/lib/my/my/my_strstr.c b/lib/my/my/my_strstr.c new file mode 100644 index 0000000..b93e9a8 --- /dev/null +++ b/lib/my/my/my_strstr.c @@ -0,0 +1,26 @@ +/* +** EPITECH PROJECT, 2019 +** StrStr duplicate +** File description: +** A search in string function +*/ + +char *my_strstr(char *str, const char *to_find) +{ + int i; + int search_index = 0; + + for (i = 0; str[i] != '\0'; i++) { + if (to_find[search_index] == '\0') + return (str + i - search_index); + if (str[i] == to_find[search_index]) + search_index++; + else if (str[i] == to_find[0]) + search_index = 1; + else + search_index = 0; + } + if (to_find[search_index] == '\0') + return (str + i - search_index); + return 0; +} diff --git a/lib/my/my/my_strupcase.c b/lib/my/my/my_strupcase.c new file mode 100644 index 0000000..e2e87cc --- /dev/null +++ b/lib/my/my/my_strupcase.c @@ -0,0 +1,15 @@ +/* +** EPITECH PROJECT, 2019 +** ToUpercase +** File description: +** string.h +*/ + +char *my_strupcase(char *str) +{ + for (int i = 0; str[i] != '\0'; i++) { + if (str[i] > 'a' && str[i] < 'z') + str[i] += 'A' - 'a'; + } + return (str); +} diff --git a/lib/my/my/my_swap.c b/lib/my/my/my_swap.c new file mode 100644 index 0000000..84ff01c --- /dev/null +++ b/lib/my/my/my_swap.c @@ -0,0 +1,13 @@ +/* +** EPITECH PROJECT, 2019 +** CPool_Day04_2019 +** File description: +** Swap int +*/ + +void my_swap(int *a, int *b) +{ + int n = *a; + *a = *b; + *b = n; +} diff --git a/lib/my/my/tostr.c b/lib/my/my/tostr.c new file mode 100644 index 0000000..ce3e977 --- /dev/null +++ b/lib/my/my/tostr.c @@ -0,0 +1,60 @@ +/* +** EPITECH PROJECT, 2019 +** Convering Between bases +** File description: +** Moldavie-Land +*/ + +#include + +static void put_digit(int n, const char *base, char *ret, int i) +{ + int base_length = 10; + + if (n > base_length - 1) + put_digit(n / base_length, base, ret, i - 1); + ret[i] = base[n % base_length]; +} + +char *putnbr_in(int nbr, const char *base, char *ret, int ret_length) +{ + int base_length = 10; + int i = ret_length; + + if (base_length < 2) { + ret = "0"; + return (ret); + } + if (nbr < 0) { + nbr *= -1; + ret[0] = '-'; + } else { + i--; + ret[i] = '\0'; + } + put_digit(nbr, base, ret, i); + return (ret); +} + +int get_new_size(int n) +{ + int base_size = 10; + int i = 1; + + while (n >= base_size) { + n /= base_size; + i++; + } + return (i); +} + +char *tostr(int n) +{ + const char *base = "0123456789"; + int count = get_new_size(n); + char *ret = malloc(sizeof(char) * (count + 1)); + + putnbr_in(n, base, ret, count); + ret[count] = '\0'; + return (ret); +} diff --git a/lib/my/my/utility.c b/lib/my/my/utility.c new file mode 100644 index 0000000..291e646 --- /dev/null +++ b/lib/my/my/utility.c @@ -0,0 +1,20 @@ +/* +** EPITECH PROJECT, 2019 +** Utility for the showmem +** File description: +** utility +*/ + +#include "my.h" + +int get_size(long n, const char *base) +{ + int base_size = my_strlen(base); + int i = 1; + + while (n >= base_size) { + n /= base_size; + i++; + } + return (i); +} \ No newline at end of file diff --git a/lib/my/my_compute_factorial_it.o b/lib/my/my_compute_factorial_it.o new file mode 100644 index 0000000..16e0397 Binary files /dev/null and b/lib/my/my_compute_factorial_it.o differ diff --git a/lib/my/my_compute_factorial_rec.o b/lib/my/my_compute_factorial_rec.o new file mode 100644 index 0000000..f76995a Binary files /dev/null and b/lib/my/my_compute_factorial_rec.o differ diff --git a/lib/my/my_compute_power_it.o b/lib/my/my_compute_power_it.o new file mode 100644 index 0000000..7e95ece Binary files /dev/null and b/lib/my/my_compute_power_it.o differ diff --git a/lib/my/my_compute_square_root.o b/lib/my/my_compute_square_root.o new file mode 100644 index 0000000..d3a2ec4 Binary files /dev/null and b/lib/my/my_compute_square_root.o differ diff --git a/lib/my/my_evil_str.o b/lib/my/my_evil_str.o new file mode 100644 index 0000000..32bfa3c Binary files /dev/null and b/lib/my/my_evil_str.o differ diff --git a/lib/my/my_find_prime_sup.o b/lib/my/my_find_prime_sup.o new file mode 100644 index 0000000..bce4f76 Binary files /dev/null and b/lib/my/my_find_prime_sup.o differ diff --git a/lib/my/my_getnbr.o b/lib/my/my_getnbr.o new file mode 100644 index 0000000..2dffffe Binary files /dev/null and b/lib/my/my_getnbr.o differ diff --git a/lib/my/my_getnbr_base.o b/lib/my/my_getnbr_base.o new file mode 100644 index 0000000..291dcb3 Binary files /dev/null and b/lib/my/my_getnbr_base.o differ diff --git a/lib/my/my_is_prime.o b/lib/my/my_is_prime.o new file mode 100644 index 0000000..c0c04e1 Binary files /dev/null and b/lib/my/my_is_prime.o differ diff --git a/lib/my/my_isneg.o b/lib/my/my_isneg.o new file mode 100644 index 0000000..a20da3b Binary files /dev/null and b/lib/my/my_isneg.o differ diff --git a/lib/my/my_print_alpha.o b/lib/my/my_print_alpha.o new file mode 100644 index 0000000..f8d3c04 Binary files /dev/null and b/lib/my/my_print_alpha.o differ diff --git a/lib/my/my_print_comb.o b/lib/my/my_print_comb.o new file mode 100644 index 0000000..d89048d Binary files /dev/null and b/lib/my/my_print_comb.o differ diff --git a/lib/my/my_print_comb2.o b/lib/my/my_print_comb2.o new file mode 100644 index 0000000..0704ec5 Binary files /dev/null and b/lib/my/my_print_comb2.o differ diff --git a/lib/my/my_print_combn.o b/lib/my/my_print_combn.o new file mode 100644 index 0000000..e9ef18e Binary files /dev/null and b/lib/my/my_print_combn.o differ diff --git a/lib/my/my_print_digits.o b/lib/my/my_print_digits.o new file mode 100644 index 0000000..72bd134 Binary files /dev/null and b/lib/my/my_print_digits.o differ diff --git a/lib/my/my_print_revalpha.o b/lib/my/my_print_revalpha.o new file mode 100644 index 0000000..043859e Binary files /dev/null and b/lib/my/my_print_revalpha.o differ diff --git a/lib/my/my_put_nbr.o b/lib/my/my_put_nbr.o new file mode 100644 index 0000000..50a23cb Binary files /dev/null and b/lib/my/my_put_nbr.o differ diff --git a/lib/my/my_putchar.o b/lib/my/my_putchar.o new file mode 100644 index 0000000..0df36ea Binary files /dev/null and b/lib/my/my_putchar.o differ diff --git a/lib/my/my_putlong_base.o b/lib/my/my_putlong_base.o new file mode 100644 index 0000000..4729bfa Binary files /dev/null and b/lib/my/my_putlong_base.o differ diff --git a/lib/my/my_putlonglong_base.o b/lib/my/my_putlonglong_base.o new file mode 100644 index 0000000..0a5fef5 Binary files /dev/null and b/lib/my/my_putlonglong_base.o differ diff --git a/lib/my/my_putnbr_base.o b/lib/my/my_putnbr_base.o new file mode 100644 index 0000000..c71f599 Binary files /dev/null and b/lib/my/my_putnbr_base.o differ diff --git a/lib/my/my_putstr.o b/lib/my/my_putstr.o new file mode 100644 index 0000000..4618ed6 Binary files /dev/null and b/lib/my/my_putstr.o differ diff --git a/lib/my/my_revstr.o b/lib/my/my_revstr.o new file mode 100644 index 0000000..7929fba Binary files /dev/null and b/lib/my/my_revstr.o differ diff --git a/lib/my/my_show_words_array.o b/lib/my/my_show_words_array.o new file mode 100644 index 0000000..9a9bdae Binary files /dev/null and b/lib/my/my_show_words_array.o differ diff --git a/lib/my/my_showmem.o b/lib/my/my_showmem.o new file mode 100644 index 0000000..8f47066 Binary files /dev/null and b/lib/my/my_showmem.o differ diff --git a/lib/my/my_showstr.o b/lib/my/my_showstr.o new file mode 100644 index 0000000..cdef09c Binary files /dev/null and b/lib/my/my_showstr.o differ diff --git a/lib/my/my_sort_int_array.o b/lib/my/my_sort_int_array.o new file mode 100644 index 0000000..1cd5eea Binary files /dev/null and b/lib/my/my_sort_int_array.o differ diff --git a/lib/my/my_str_isalpha.o b/lib/my/my_str_isalpha.o new file mode 100644 index 0000000..d070a25 Binary files /dev/null and b/lib/my/my_str_isalpha.o differ diff --git a/lib/my/my_str_islower.o b/lib/my/my_str_islower.o new file mode 100644 index 0000000..4c1c20b Binary files /dev/null and b/lib/my/my_str_islower.o differ diff --git a/lib/my/my_str_isnum.o b/lib/my/my_str_isnum.o new file mode 100644 index 0000000..2406775 Binary files /dev/null and b/lib/my/my_str_isnum.o differ diff --git a/lib/my/my_str_isprintable.o b/lib/my/my_str_isprintable.o new file mode 100644 index 0000000..8088c05 Binary files /dev/null and b/lib/my/my_str_isprintable.o differ diff --git a/lib/my/my_str_isupper.o b/lib/my/my_str_isupper.o new file mode 100644 index 0000000..3e03ea0 Binary files /dev/null and b/lib/my/my_str_isupper.o differ diff --git a/lib/my/my_str_to_word_array.o b/lib/my/my_str_to_word_array.o new file mode 100644 index 0000000..29d95e9 Binary files /dev/null and b/lib/my/my_str_to_word_array.o differ diff --git a/lib/my/my_strcapitalize.o b/lib/my/my_strcapitalize.o new file mode 100644 index 0000000..f701c96 Binary files /dev/null and b/lib/my/my_strcapitalize.o differ diff --git a/lib/my/my_strcat.o b/lib/my/my_strcat.o new file mode 100644 index 0000000..0669e33 Binary files /dev/null and b/lib/my/my_strcat.o differ diff --git a/lib/my/my_strchr.o b/lib/my/my_strchr.o new file mode 100644 index 0000000..f15cd7d Binary files /dev/null and b/lib/my/my_strchr.o differ diff --git a/lib/my/my_strcmp.o b/lib/my/my_strcmp.o new file mode 100644 index 0000000..52532c7 Binary files /dev/null and b/lib/my/my_strcmp.o differ diff --git a/lib/my/my_strcpy.o b/lib/my/my_strcpy.o new file mode 100644 index 0000000..be94315 Binary files /dev/null and b/lib/my/my_strcpy.o differ diff --git a/lib/my/my_strdup.o b/lib/my/my_strdup.o new file mode 100644 index 0000000..d5aef81 Binary files /dev/null and b/lib/my/my_strdup.o differ diff --git a/lib/my/my_strlen.o b/lib/my/my_strlen.o new file mode 100644 index 0000000..0a81c68 Binary files /dev/null and b/lib/my/my_strlen.o differ diff --git a/lib/my/my_strlowcase.o b/lib/my/my_strlowcase.o new file mode 100644 index 0000000..e56d46c Binary files /dev/null and b/lib/my/my_strlowcase.o differ diff --git a/lib/my/my_strncat.o b/lib/my/my_strncat.o new file mode 100644 index 0000000..797e397 Binary files /dev/null and b/lib/my/my_strncat.o differ diff --git a/lib/my/my_strncmp.o b/lib/my/my_strncmp.o new file mode 100644 index 0000000..bcd08ca Binary files /dev/null and b/lib/my/my_strncmp.o differ diff --git a/lib/my/my_strncpy.o b/lib/my/my_strncpy.o new file mode 100644 index 0000000..c50335e Binary files /dev/null and b/lib/my/my_strncpy.o differ diff --git a/lib/my/my_strstr.o b/lib/my/my_strstr.o new file mode 100644 index 0000000..284454e Binary files /dev/null and b/lib/my/my_strstr.o differ diff --git a/lib/my/my_strupcase.o b/lib/my/my_strupcase.o new file mode 100644 index 0000000..3842a7d Binary files /dev/null and b/lib/my/my_strupcase.o differ diff --git a/lib/my/my_swap.o b/lib/my/my_swap.o new file mode 100644 index 0000000..d9205a8 Binary files /dev/null and b/lib/my/my_swap.o differ diff --git a/lib/my/no_format.o b/lib/my/no_format.o new file mode 100644 index 0000000..7fd4a34 Binary files /dev/null and b/lib/my/no_format.o differ diff --git a/lib/my/octal_formater.o b/lib/my/octal_formater.o new file mode 100644 index 0000000..4a61f4c Binary files /dev/null and b/lib/my/octal_formater.o differ diff --git a/lib/my/printf.o b/lib/my/printf.o new file mode 100644 index 0000000..8a7da15 Binary files /dev/null and b/lib/my/printf.o differ diff --git a/lib/my/printf_utility.o b/lib/my/printf_utility.o new file mode 100644 index 0000000..126c075 Binary files /dev/null and b/lib/my/printf_utility.o differ diff --git a/lib/my/ptr_formater.o b/lib/my/ptr_formater.o new file mode 100644 index 0000000..e35fdb5 Binary files /dev/null and b/lib/my/ptr_formater.o differ diff --git a/lib/my/src/#printf.c# b/lib/my/src/#printf.c# new file mode 100644 index 0000000..c8715a9 --- /dev/null +++ b/lib/my/src/#printf.c# @@ -0,0 +1,95 @@ +/* +** EPITECH PROJECT, 2019 +** Sum stdarg +** File description: +** sum_stdarg +*/ + +#include "formaters.h" +#include "my.h" +#include +#include + +formater_t formaters[] = {{string_formater, 's'}, + {string_nonprintable_formater, 'S'}, + {char_formater, 'c'}, + {int_formater, 'i'}, + {int_formater, 'd'}, + {octal_formater, 'o'}, + {hexa_formater, 'x'}, + {big_hexa_formater, 'X'}, + {uint_formater, 'u'}, + {ptr_formater, 'p'}, + {ubinary_formater, 'b'}, + {float_formater, 'f'}, + {float_formater, 'F'}, + {no_format, '%'}, + {0, 0}}; + +const char modifiersCst[] = "#0-+ hl"; + +int format(va_list ap, char flag, char modifiers[MODIFIERS_SIZE]) +{ + for (int i = 0; formaters[i].flag; i++) { + if (formaters[i].flag == flag) { + return (formaters[i].func(ap, modifiers)); + } + } + return (0); +} + +int is_flag(char c) +{ + for (int i = 0; formaters[i].flag; i++) { + if (formaters[i].flag == c) { + return (1); + } + } + return (0); +} + +int is_modifier(char c) +{ + for (int i = 0; modifiersCst[i]; i++) { + if (modifiersCst[i] == c) + return (1); + } + return (0); +} + +int get_modifiers(const char *str, char flags[MODIFIERS_SIZE]) +{ + int i; + + for (i = 0; !is_flag(str[i]); i++) { + if (!is_modifier(str[i]) && !is_num(str[i])) + return (-1); + flags[i] = str[i]; + } + flags[i] = '\0'; + return (i); +} + +int my_printf(const char *str, ...) +{ + int count = 0; + va_list ap; + char modifiers[MODIFIERS_SIZE]; + int next_is_flag; + + va_start(ap, str); + for (int i = 0; str[i]; i++) { + if (str[i] == '%') { + next_is_flag = 1; + i++; + i += get_modifiers(&str[i], modifiers); + } else + next_is_flag = 0; + if (next_is_flag && is_flag(str[i])) + count += format(ap, str[i], modifiers); + else + count += write(1, &str[i], 1); + } + va_end(ap); + return (count); +} \ No newline at end of file diff --git a/lib/my/src/formaters/big_hexa_formater.c b/lib/my/src/formaters/big_hexa_formater.c new file mode 100644 index 0000000..5782ad8 --- /dev/null +++ b/lib/my/src/formaters/big_hexa_formater.c @@ -0,0 +1,31 @@ +/* +** EPITECH PROJECT, 2019 +** int formater +** File description: +** int_formater +*/ + +#include "formaters.h" +#include "my.h" +#include +#include + +int big_hexa_formater(va_list ap, char mod[MODIFIERS_SIZE]) +{ + long long int var; + int nbrlen; + + if (count('l', mod) >= 2) + var = va_arg(ap, unsigned long long int); + else if (count('l', mod) == 1) + var = va_arg(ap, unsigned long); + else + var = va_arg(ap, unsigned int); + if (contains('0', mod)) + nbrlen = my_getnbr(&mod[last_mod(mod)]); + else + nbrlen = 0; + if (var != 0 && contains('#', mod)) + nbrlen -= write(1, "0X", 2); + return (my_putlonglong_base(var, "0123456789ABCDEF", nbrlen)); +} \ No newline at end of file diff --git a/lib/my/src/formaters/char_formater.c b/lib/my/src/formaters/char_formater.c new file mode 100644 index 0000000..8ab20c7 --- /dev/null +++ b/lib/my/src/formaters/char_formater.c @@ -0,0 +1,18 @@ +/* +** EPITECH PROJECT, 2019 +** Char formater +** File description: +** char_formater +*/ + +#include "formaters.h" +#include "my.h" +#include +#include + +int char_formater(va_list ap, char mod[MODIFIERS_SIZE]) +{ + char var = (char)va_arg(ap, int); + + return (write(1, &var, 1)); +} \ No newline at end of file diff --git a/lib/my/src/formaters/float_formater.c b/lib/my/src/formaters/float_formater.c new file mode 100644 index 0000000..debc171 --- /dev/null +++ b/lib/my/src/formaters/float_formater.c @@ -0,0 +1,24 @@ +/* +** EPITECH PROJECT, 2019 +** int formater +** File description: +** int_formater +*/ + +#include "formaters.h" +#include "my.h" +#include +#include + +int float_formater(va_list ap, char mod[MODIFIERS_SIZE]) +{ + double var = va_arg(ap, double); + int decimal = (var - (int)var) * 1000000 + 1; + + while (decimal % 10 == 0) + decimal /= 10; + my_put_nbr((int)var); + write(1, ".", 1); + my_put_nbr(decimal); + return (0); +} \ No newline at end of file diff --git a/lib/my/src/formaters/hexa_formater.c b/lib/my/src/formaters/hexa_formater.c new file mode 100644 index 0000000..8c384ee --- /dev/null +++ b/lib/my/src/formaters/hexa_formater.c @@ -0,0 +1,31 @@ +/* +** EPITECH PROJECT, 2019 +** int formater +** File description: +** int_formater +*/ + +#include "formaters.h" +#include "my.h" +#include +#include + +int hexa_formater(va_list ap, char mod[MODIFIERS_SIZE]) +{ + long long int var; + int nbrlen; + + if (count('l', mod) >= 2) + var = va_arg(ap, unsigned long long int); + else if (count('l', mod) == 1) + var = va_arg(ap, unsigned long); + else + var = va_arg(ap, unsigned int); + if (contains('0', mod)) + nbrlen = my_getnbr(&mod[last_mod(mod)]); + else + nbrlen = 0; + if (var != 0 && contains('#', mod)) + nbrlen -= write(1, "0x", 2); + return (my_putlonglong_base(var, "0123456789abcdef", nbrlen)); +} \ No newline at end of file diff --git a/lib/my/src/formaters/int_formater.c b/lib/my/src/formaters/int_formater.c new file mode 100644 index 0000000..dc307c9 --- /dev/null +++ b/lib/my/src/formaters/int_formater.c @@ -0,0 +1,33 @@ +/* +** EPITECH PROJECT, 2019 +** int formater +** File description: +** int_formater +*/ + +#include "formaters.h" +#include "my.h" +#include +#include + +int int_formater(va_list ap, char mod[MODIFIERS_SIZE]) +{ + long long int var; + int nbrlen; + + if (count('l', mod) >= 2) + var = va_arg(ap, long long int); + else if (count('l', mod) == 1) + var = va_arg(ap, long); + else + var = va_arg(ap, int); + if (contains('0', mod)) + nbrlen = my_getnbr(&mod[last_mod(mod)]); + else + nbrlen = 0; + if (var >= 0 && contains(' ', mod) && !contains('+', mod)) + nbrlen -= write(1, " ", 1); + if (var >= 0 && contains('+', mod)) + nbrlen -= write(1, "+", 1); + return (my_putlonglong_base(var, "0123456789", nbrlen)); +} \ No newline at end of file diff --git a/lib/my/src/formaters/no_format.c b/lib/my/src/formaters/no_format.c new file mode 100644 index 0000000..178c22b --- /dev/null +++ b/lib/my/src/formaters/no_format.c @@ -0,0 +1,16 @@ +/* +** EPITECH PROJECT, 2019 +** int formater +** File description: +** int_formater +*/ + +#include "formaters.h" +#include "my.h" +#include +#include + +int no_format(va_list _ap, char mod[MODIFIERS_SIZE]) +{ + return (write(1, "%", 1)); +} \ No newline at end of file diff --git a/lib/my/src/formaters/octal_formater.c b/lib/my/src/formaters/octal_formater.c new file mode 100644 index 0000000..da7c016 --- /dev/null +++ b/lib/my/src/formaters/octal_formater.c @@ -0,0 +1,31 @@ +/* +** EPITECH PROJECT, 2019 +** int formater +** File description: +** int_formater +*/ + +#include "formaters.h" +#include "my.h" +#include +#include + +int octal_formater(va_list ap, char mod[MODIFIERS_SIZE]) +{ + long long int var; + int nbrlen; + + if (count('l', mod) >= 2) + var = va_arg(ap, unsigned long long int); + else if (count('l', mod) == 1) + var = va_arg(ap, unsigned long); + else + var = va_arg(ap, unsigned int); + if (contains('0', mod)) + nbrlen = my_getnbr(&mod[last_mod(mod)]); + else + nbrlen = 0; + if (var != 0 && contains('#', mod)) + nbrlen -= write(1, "0", 2); + return (my_putlonglong_base(var, "01234567", nbrlen)); +} \ No newline at end of file diff --git a/lib/my/src/formaters/ptr_formater.c b/lib/my/src/formaters/ptr_formater.c new file mode 100644 index 0000000..bcd0c41 --- /dev/null +++ b/lib/my/src/formaters/ptr_formater.c @@ -0,0 +1,19 @@ +/* +** EPITECH PROJECT, 2019 +** Char formater +** File description: +** char_formater +*/ + +#include "formaters.h" +#include "my.h" +#include +#include + +int ptr_formater(va_list ap, char mod[MODIFIERS_SIZE]) +{ + void *var = va_arg(ap, void *); + + print_ptr(var); + return (0); +} \ No newline at end of file diff --git a/lib/my/src/formaters/string_formater.c b/lib/my/src/formaters/string_formater.c new file mode 100644 index 0000000..91bf943 --- /dev/null +++ b/lib/my/src/formaters/string_formater.c @@ -0,0 +1,23 @@ +/* +** EPITECH PROJECT, 2019 +** String formater +** File description: +** string_formater +*/ + +#include +#include +#include "formaters.h" +#include "my.h" + +int string_formater(va_list ap, char mod[MODIFIERS_SIZE]) +{ + char *var = va_arg(ap, char *); + int length; + + if (contains('0', mod)) + length = my_getnbr(&mod[last_mod(mod)]); + else + length = my_strlen(var); + return (write(1, var, length)); +} \ No newline at end of file diff --git a/lib/my/src/formaters/string_nonprintable_formater.c b/lib/my/src/formaters/string_nonprintable_formater.c new file mode 100644 index 0000000..8520e83 --- /dev/null +++ b/lib/my/src/formaters/string_nonprintable_formater.c @@ -0,0 +1,19 @@ +/* +** EPITECH PROJECT, 2019 +** String formater +** File description: +** string_formater +*/ + +#include +#include +#include "formaters.h" +#include "my.h" + +int string_nonprintable_formater(va_list ap, char mod[MODIFIERS_SIZE]) +{ + char *var = va_arg(ap, char *); + + my_showstr(var); + return (0); +} \ No newline at end of file diff --git a/lib/my/src/formaters/ubinary_formater.c b/lib/my/src/formaters/ubinary_formater.c new file mode 100644 index 0000000..53779aa --- /dev/null +++ b/lib/my/src/formaters/ubinary_formater.c @@ -0,0 +1,29 @@ +/* +** EPITECH PROJECT, 2019 +** int formater +** File description: +** int_formater +*/ + +#include "formaters.h" +#include "my.h" +#include +#include + +int ubinary_formater(va_list ap, char mod[MODIFIERS_SIZE]) +{ + long long int var; + int nbrlen; + + if (count('l', mod) >= 2) + var = va_arg(ap, unsigned long long int); + else if (count('l', mod) == 1) + var = va_arg(ap, unsigned long); + else + var = va_arg(ap, unsigned int); + if (contains('0', mod)) + nbrlen = my_getnbr(&mod[last_mod(mod)]); + else + nbrlen = 0; + return (my_putlonglong_base(var, "01", nbrlen)); +} \ No newline at end of file diff --git a/lib/my/src/formaters/uint_formater.c b/lib/my/src/formaters/uint_formater.c new file mode 100644 index 0000000..47705a9 --- /dev/null +++ b/lib/my/src/formaters/uint_formater.c @@ -0,0 +1,29 @@ +/* +** EPITECH PROJECT, 2019 +** int formater +** File description: +** int_formater +*/ + +#include "formaters.h" +#include "my.h" +#include +#include + +int uint_formater(va_list ap, char mod[MODIFIERS_SIZE]) +{ + long long int var; + int nbrlen; + + if (count('l', mod) >= 2) + var = va_arg(ap, unsigned long long int); + else if (count('l', mod) == 1) + var = va_arg(ap, unsigned long); + else + var = va_arg(ap, unsigned int); + if (contains('0', mod)) + nbrlen = my_getnbr(&mod[last_mod(mod)]); + else + nbrlen = 0; + return (my_putlonglong_base(var, "0123456789", nbrlen)); +} \ No newline at end of file diff --git a/lib/my/src/get_nbr_size.c b/lib/my/src/get_nbr_size.c new file mode 100644 index 0000000..6157162 --- /dev/null +++ b/lib/my/src/get_nbr_size.c @@ -0,0 +1,24 @@ +/* +** EPITECH PROJECT, 2019 +** Get nbr size +** File description: +** get_nbr_size +*/ + +#include "my.h" + +int get_nbr_len(long long int n, const char *base) +{ + int base_size = my_strlen(base); + int i = 1; + + if (n < 0) { + n *= -1; + i++; + } + while (n >= base_size) { + n /= base_size; + i++; + } + return (i); +} \ No newline at end of file diff --git a/lib/my/src/my_putlonglong_base.c b/lib/my/src/my_putlonglong_base.c new file mode 100644 index 0000000..4d1159b --- /dev/null +++ b/lib/my/src/my_putlonglong_base.c @@ -0,0 +1,42 @@ +/* +** EPITECH PROJECT, 2019 +** Put nbr in a custom base +** File description: +** Might be useful later +*/ + +#include +#include "formaters.h" + +int my_strlen(const char *str); + +static void display_a_digit(char c) +{ + write(1, &c, 1); +} + +static void put_digit(long long int n, int base_length, const char *base) +{ + if (n > base_length - 1) + put_digit(n / base_length, base_length, base); + display_a_digit(base[n % base_length]); +} + +int my_putlonglong_base(long long int nbr, const char *base, int nbrlen) +{ + int base_length = my_strlen(base); + + if (base_length < 2) { + display_a_digit(base[0]); + return (0); + } + if (nbr < 0) { + nbr *= -1; + display_a_digit('-'); + nbrlen--; + } + for (int i = get_nbr_len(nbr, base); i < nbrlen; i++) + write(1, base, 1); + put_digit(nbr, base_length, base); + return (nbrlen + nbr < 0 ? 1 : 0); +} diff --git a/lib/my/src/printf.c b/lib/my/src/printf.c new file mode 100644 index 0000000..451fc28 --- /dev/null +++ b/lib/my/src/printf.c @@ -0,0 +1,95 @@ +/* +** EPITECH PROJECT, 2019 +** Sum stdarg +** File description: +** sum_stdarg +*/ + +#include "formaters.h" +#include "my.h" +#include +#include + +formater_t formaters[] = {{string_formater, 's'}, + {string_nonprintable_formater, 'S'}, + {char_formater, 'c'}, + {int_formater, 'i'}, + {int_formater, 'd'}, + {octal_formater, 'o'}, + {hexa_formater, 'x'}, + {big_hexa_formater, 'X'}, + {uint_formater, 'u'}, + {ptr_formater, 'p'}, + {ubinary_formater, 'b'}, + {float_formater, 'f'}, + {float_formater, 'F'}, + {no_format, '%'}, + {0, 0}}; + +const char modifiersCst[] = "#0-+ hl"; + +int format(va_list ap, char flag, char modifiers[MODIFIERS_SIZE]) +{ + for (int i = 0; formaters[i].flag; i++) { + if (formaters[i].flag == flag) { + return (formaters[i].func(ap, modifiers)); + } + } + return (0); +} + +int is_flag(char c) +{ + for (int i = 0; formaters[i].flag; i++) { + if (formaters[i].flag == c) { + return (1); + } + } + return (0); +} + +int is_modifier(char c) +{ + for (int i = 0; modifiersCst[i]; i++) { + if (modifiersCst[i] == c) + return (1); + } + return (0); +} + +int get_modifiers(const char *str, char flags[MODIFIERS_SIZE]) +{ + int i; + + for (i = 0; !is_flag(str[i]); i++) { + if (!is_modifier(str[i]) && !is_num(str[i])) + return (-1); + flags[i] = str[i]; + } + flags[i] = '\0'; + return (i); +} + +int my_printf(const char *str, ...) +{ + int count = 0; + va_list ap; + char modifiers[MODIFIERS_SIZE]; + int next_is_flag; + + va_start(ap, str); + for (int i = 0; str[i]; i++) { + if (str[i] == '%') { + next_is_flag = 1; + i++; + i += get_modifiers(&str[i], modifiers); + } else + next_is_flag = 0; + if (next_is_flag && is_flag(str[i])) + count += format(ap, str[i], modifiers); + else + count += write(1, &str[i], 1); + } + va_end(ap); + return (count); +} \ No newline at end of file diff --git a/lib/my/src/printf_utility.c b/lib/my/src/printf_utility.c new file mode 100644 index 0000000..22baf41 --- /dev/null +++ b/lib/my/src/printf_utility.c @@ -0,0 +1,37 @@ +/* +** EPITECH PROJECT, 2019 +** Utility for the flags +** File description: +** utility +*/ + +#include "formaters.h" + +int count(char c, char *str) +{ + int count = 0; + + for (int i = 0; str[i]; i++) { + if (str[i] == c) + count++; + } + return (count); +} + +int contains(char c, char *str) +{ + for (int i = 0; str[i]; i++) { + if (str[i] == c) + return (1); + } + return (0); +} + +int last_mod(char *modifiers) +{ + for (int i = 0; modifiers[i]; i++) { + if (!is_modifier(modifiers[i])) + return (i); + } + return (0); +} \ No newline at end of file diff --git a/lib/my/string_formater.o b/lib/my/string_formater.o new file mode 100644 index 0000000..d759af2 Binary files /dev/null and b/lib/my/string_formater.o differ diff --git a/lib/my/string_nonprintable_formater.o b/lib/my/string_nonprintable_formater.o new file mode 100644 index 0000000..1dc5c7b Binary files /dev/null and b/lib/my/string_nonprintable_formater.o differ diff --git a/lib/my/tests/test_printf.c b/lib/my/tests/test_printf.c new file mode 100644 index 0000000..3f944e9 --- /dev/null +++ b/lib/my/tests/test_printf.c @@ -0,0 +1,176 @@ +/* +** EPITECH PROJECT, 2019 +** Tests +** File description: +** test_disp_stdarg +*/ + +#include +#include +#include "my.h" + +Test(disp, first, .init = cr_redirect_stdout) +{ + my_printf("Insane %s, %c, %d, %s\n", "Yes", '8', 15, "Nope"); + cr_assert_stdout_eq_str("Insane Yes, 8, 15, Nope\n"); +} + +Test(disp, faketag, .init = cr_redirect_stdout) +{ + my_printf("Insane %Y, %c, %d, %s\n", '8', 15, "Nope"); + cr_assert_stdout_eq_str("Insane %Y, 8, 15, Nope\n"); +} + +Test(disp, str, .init = cr_redirect_stdout) +{ + my_printf("Insane %s\n", "156615"); + cr_assert_stdout_eq_str("Insane 156615\n"); +} + +Test(disp, uints, .init = cr_redirect_stdout) +{ + my_printf("Insane %u\n", 156615); + cr_assert_stdout_eq_str("Insane 156615\n"); +} + +Test(disp, characters, .init = cr_redirect_stdout) +{ + my_printf("Insane %c\n", '^'); + cr_assert_stdout_eq_str("Insane ^\n"); +} + +Test(disp, ints, .init = cr_redirect_stdout) +{ + my_printf("Insane %d, %i\n", 15, -15); + cr_assert_stdout_eq_str("Insane 15, -15\n"); +} + +Test(disp, ptr, .init = cr_redirect_stdout) +{ + int ptr = 15; + char *str = malloc(sizeof(char) * 25); + + my_printf("Insane %p\n", &ptr); + sprintf(str, "Insane %p\n", &ptr); + cr_assert_stdout_eq_str(str); +} + +Test(disp, nonprintable, .init = cr_redirect_stdout) +{ + my_printf("Insane %S\n", "\aYes\a"); + cr_assert_stdout_eq_str("Insane \\007Yes\\007\n"); +} + +Test(disp, binary, .init = cr_redirect_stdout) +{ + my_printf("%b\n", 153152); + cr_assert_stdout_eq_str("100101011001000000\n"); +} + +Test(disp, octal, .init = cr_redirect_stdout) +{ + my_printf("%o\n", 153152); + cr_assert_stdout_eq_str("453100\n"); +} + +Test(disp, hexa, .init = cr_redirect_stdout) +{ + my_printf("%x\n", 1561356523); + cr_assert_stdout_eq_str("5d1068eb\n"); +} + +Test(disp, bighexa, .init = cr_redirect_stdout) +{ + my_printf("%X\n", 1561356523); + cr_assert_stdout_eq_str("5D1068EB\n"); +} + +Test(disp, uintneg, .init = cr_redirect_stdout) +{ + my_printf("%u\n", -500); + cr_assert_stdout_eq_str("4294966796\n"); +} + +Test(disp, noformat, .init = cr_redirect_stdout) +{ + my_printf("%%\n"); + cr_assert_stdout_eq_str("%\n"); +} + +Test(disp, floats, .init = cr_redirect_stdout) +{ + my_printf("%f\n", 13.684); + cr_assert_stdout_eq_str("13.684\n"); +} + +Test(disp, morefloat, .init = cr_redirect_stdout) +{ + my_printf("%f\n", 18.138613); + cr_assert_stdout_eq_str("18.138613\n"); +} + +Test(my_printf, mouliS, .init = cr_redirect_stdout) +{ + my_printf("%S", "mouline\atte\n"); + cr_assert_stdout_eq_str("mouline\\007tte\\012"); +} + +Test(my_printf, simplePtr, .init = cr_redirect_stdout) +{ + my_printf("%p", 3456789); + cr_assert_stdout_eq_str("0x34bf15"); +} + +Test(my_printf, intLength, .init = cr_redirect_stdout) +{ + my_printf("%05i", 15); + cr_assert_stdout_eq_str("00015"); +} + +Test(my_printf, intPlusSize, .init = cr_redirect_stdout) +{ + my_printf("%0+5i", 15); + cr_assert_stdout_eq_str("+0015"); +} + +Test(my_printf, intLengthSpace, .init = cr_redirect_stdout) +{ + my_printf("% 05i", 15); + cr_assert_stdout_eq_str(" 0015"); +} + +Test(my_printf, intLengthSpacePlus, .init = cr_redirect_stdout) +{ + my_printf("% 0+5i", -15); + cr_assert_stdout_eq_str("-0015"); +} + +Test(my_printf, xLength, .init = cr_redirect_stdout) +{ + my_printf("%05X", 15); + cr_assert_stdout_eq_str("0000F"); +} + +Test(my_printf, xPlusSize, .init = cr_redirect_stdout) +{ + my_printf("%0+5X", 15); + cr_assert_stdout_eq_str("0000F"); +} + +Test(my_printf, xLengthSpace, .init = cr_redirect_stdout) +{ + my_printf("%#X", -15); + cr_assert_stdout_eq_str("0XFFFFFFF1"); +} + +Test(my_printf, xLengthSpacePlus, .init = cr_redirect_stdout) +{ + my_printf("%# +X", -15); + cr_assert_stdout_eq_str("0XFFFFFFF1"); +} + +Test(my_printf, stringWithMax, .init = cr_redirect_stdout) +{ + my_printf("%# +03s", "ABCDEFG"); + cr_assert_stdout_eq_str("ABC"); +} \ No newline at end of file diff --git a/lib/my/tostr.o b/lib/my/tostr.o new file mode 100644 index 0000000..8d3041d Binary files /dev/null and b/lib/my/tostr.o differ diff --git a/lib/my/ubinary_formater.o b/lib/my/ubinary_formater.o new file mode 100644 index 0000000..88fca24 Binary files /dev/null and b/lib/my/ubinary_formater.o differ diff --git a/lib/my/uint_formater.o b/lib/my/uint_formater.o new file mode 100644 index 0000000..767b17e Binary files /dev/null and b/lib/my/uint_formater.o differ diff --git a/lib/my/utility.o b/lib/my/utility.o new file mode 100644 index 0000000..4334278 Binary files /dev/null and b/lib/my/utility.o differ diff --git a/lib/quadtree/.gitignore b/lib/quadtree/.gitignore new file mode 100644 index 0000000..eed063b --- /dev/null +++ b/lib/quadtree/.gitignore @@ -0,0 +1,3 @@ +*.gc* +*.o +libquadtree.a \ No newline at end of file diff --git a/lib/quadtree/LICENSE b/lib/quadtree/LICENSE new file mode 100644 index 0000000..0b98efa --- /dev/null +++ b/lib/quadtree/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2019 Anonymus Raccoon + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/lib/quadtree/Makefile b/lib/quadtree/Makefile new file mode 100644 index 0000000..8a3c226 --- /dev/null +++ b/lib/quadtree/Makefile @@ -0,0 +1,60 @@ +## +## EPITECH PROJECT, 2019 +## quadtree +## File description: +## Makefile +## + +SRC = src/quadtree.c \ + src/qt_split.c \ + src/qt_collide.c \ + src/qt_destroy.c \ + src/qt_layer.c \ + src/qt_position_overlap.c \ + src/utility/calloc.c \ + src/array.c + +OBJ = $(SRC:%.c=%.o) + +TESTS = tests/test_collide.c \ + tests/test_add.c + +COVERAGE = --coverage -lcriterion + +INCLUDE = -I ./include + +CFLAGS = $(INCLUDE) -Wall -Wextra -Wshadow + +LDFLAGS = + +NAME = libquadtree.a + +UT = ./ut + +CC = gcc + +AR = ar rc + +all: build + +build: $(OBJ) + $(AR) $(NAME) $(OBJ) + +tests_run: + $(CC) -o $(UT) $(SRC) $(TESTS) $(COVERAGE) $(CFLAGS) -g + $(UT) + +clean: + $(RM) $(OBJ) + $(RM) *.gc* + +fclean: clean + $(RM) $(NAME) + $(RM) $(UT) + +re: fclean all + +dbg: CFLAGS += -g +dbg: re + +.PHONY: all build clean fclean \ No newline at end of file diff --git a/lib/quadtree/include/array.h b/lib/quadtree/include/array.h new file mode 100644 index 0000000..959394e --- /dev/null +++ b/lib/quadtree/include/array.h @@ -0,0 +1,11 @@ +/* +** EPITECH PROJECT, 2020 +** Twac +** File description: +** array +*/ + +#pragma once + +void arr_add(int *arr, int n); +int arr_len(int *arr); \ No newline at end of file diff --git a/lib/quadtree/include/quadtree.h b/lib/quadtree/include/quadtree.h new file mode 100644 index 0000000..64d3d07 --- /dev/null +++ b/lib/quadtree/include/quadtree.h @@ -0,0 +1,54 @@ +/* +** EPITECH PROJECT, 2019 +** quadtree +** File description: +** quadtree +*/ + +#pragma once + +#include + +#define MAXCOL 1024 +#define MAX_ENTITY 20 + +typedef struct quadtree quadtree; + +typedef struct qt_intrect +{ + float x; + float y; + int h; + int w; +} qt_intrect; + +typedef struct qt_object +{ + int id; + qt_intrect rect; + int layer; +} qt_object; + +typedef struct qt_collision +{ + float distance_left; + float distance_right; + float distance_top; + float distance_down; + int *collide_with; +} qt_collision; + +struct quadtree +{ + qt_intrect rect; + int capacity; + void *objects; +}; + +quadtree *qt_create(qt_intrect rect, int capacity); +int qt_add(quadtree *tree, qt_object obj); +qt_collision collision_get_info(quadtree *tree, int entity_id); +bool qt_collide(qt_intrect r1, qt_intrect r2); +qt_object *qt_getobj(quadtree *tree, int id); +int qt_update(quadtree *tree, qt_object obj); +void qt_destroy(quadtree *tree); \ No newline at end of file diff --git a/lib/quadtree/include/quadtree_internal.h b/lib/quadtree/include/quadtree_internal.h new file mode 100644 index 0000000..22b4ab4 --- /dev/null +++ b/lib/quadtree/include/quadtree_internal.h @@ -0,0 +1,30 @@ +/* +** EPITECH PROJECT, 2019 +** quadtree +** File description: +** quadtree_internal +*/ + +#pragma once + +#include "quadtree.h" +#include + +void *my_calloc(int capacity, int size); + +quadtree *qt_split(quadtree *tree); + +bool collision_overlapx(qt_intrect r1, qt_intrect r2); +bool collision_overlapy(qt_intrect r1, qt_intrect r2); + +bool collision_can_see(int l1, int l2); + +#define MIN(x, y) ((x) < (y) ? (x) : (y)) +#define MAX(x, y) ((x) < (y) ? (y) : (x)) + +#define CAN_BE_SEEN 0b10 +#define CAN_SEE 0b01 + +#define TREEOBJ_AT(x) (&((qt_object *)tree->objects)[i]) + +#define COLLISION_MAX ((qt_collision){MAXCOL, MAXCOL, MAXCOL, MAXCOL, NULL}) \ No newline at end of file diff --git a/lib/quadtree/src/array.c b/lib/quadtree/src/array.c new file mode 100644 index 0000000..19ff0c8 --- /dev/null +++ b/lib/quadtree/src/array.c @@ -0,0 +1,27 @@ +/* +** EPITECH PROJECT, 2020 +** Twac +** File description: +** array +*/ + +#include "quadtree.h" + +int arr_len(int *arr) +{ + int i = 0; + + while (arr[i] != -1) + i++; + return (i); +} + +void arr_add(int *arr, int n) +{ + int new = arr_len(arr); + + if (new + 1 > MAX_ENTITY) + return; + arr[new] = n; + arr[new + 1] = -1; +} \ No newline at end of file diff --git a/lib/quadtree/src/qt_collide.c b/lib/quadtree/src/qt_collide.c new file mode 100644 index 0000000..f886eb1 --- /dev/null +++ b/lib/quadtree/src/qt_collide.c @@ -0,0 +1,79 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** qt_getcollide +*/ + +#include "quadtree.h" +#include "quadtree_internal.h" +#include "array.h" + +qt_collision check_collisions(qt_object *obj1, qt_object *obj2, int *colwith) +{ + qt_intrect r1 = obj1->rect; + qt_intrect r2 = obj2->rect; + qt_collision col = COLLISION_MAX; + + if (collision_overlapy(r1, r2)) { + if (r1.x <= r2.x) + col.distance_right = r2.x - (r1.x + r1.w); + if (r2.x <= r1.x) + col.distance_left = r1.x - (r2.x + r2.w); + } + if (collision_overlapx(r1, r2)) { + if (r1.y >= r2.y) + col.distance_down = r1.y - r2.y - r1.h; + if (r2.y >= r1.y) + col.distance_top = r2.y - r1.y - r2.h; + } + if (col.distance_right == 0 || col.distance_down == 0 || + col.distance_left == 0 || col.distance_right == 0) + arr_add(colwith, obj2->id); + return (col); +} + +qt_collision sum_col(qt_collision col1, qt_collision col2) +{ + col1.distance_down = MAX(0, MIN(col1.distance_down, col2.distance_down)); + col1.distance_top = MAX(0, MIN(col1.distance_top, col2.distance_top)); + col1.distance_left = MAX(0, MIN(col1.distance_left, col2.distance_left)); + col1.distance_right = MAX(0, MIN(col1.distance_right, col2.distance_right)); + return (col1); +} + +qt_collision collrec(quadtree *tree, int id, qt_collision col, int *collidewith) +{ + qt_object *obj; + qt_object *tmp; + quadtree *child; + + if (tree->capacity == -1) { + for (int i = 0; i < 4; i++) { + child = &((quadtree *)tree->objects)[i]; + col = collrec(child, id, col, collidewith); + } + return (col); + } + obj = qt_getobj(tree, id); + if (!obj) + return (col); + for (int i = 0; i < tree->capacity && TREEOBJ_AT(i)->id != -1; i++) { + tmp = TREEOBJ_AT(i); + if (tmp->id == id || !collision_can_see(obj->layer, tmp->layer)) + continue; + col = sum_col(col, check_collisions(obj, tmp, collidewith)); + } + return (col); +} + +qt_collision collision_get_info(quadtree *tree, int id) +{ + qt_collision col = COLLISION_MAX; + static int collidewith[MAX_ENTITY + 1]; + + collidewith[0] = -1; + col = collrec(tree, id, col, collidewith); + col.collide_with = collidewith; + return (col); +} \ No newline at end of file diff --git a/lib/quadtree/src/qt_destroy.c b/lib/quadtree/src/qt_destroy.c new file mode 100644 index 0000000..e0f6918 --- /dev/null +++ b/lib/quadtree/src/qt_destroy.c @@ -0,0 +1,25 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** qt_destroy +*/ + +#include "quadtree.h" +#include + +void qt_child_destroy(quadtree *tree) +{ + if (tree->capacity < 0) { + for (int i = 0; i < 4; i++) { + qt_child_destroy(&((quadtree *)tree->objects)[i]); + } + } + free(tree->objects); +} + +void qt_destroy(quadtree *tree) +{ + qt_child_destroy(tree); + free(tree); +} \ No newline at end of file diff --git a/lib/quadtree/src/qt_layer.c b/lib/quadtree/src/qt_layer.c new file mode 100644 index 0000000..73815aa --- /dev/null +++ b/lib/quadtree/src/qt_layer.c @@ -0,0 +1,23 @@ +/* +** EPITECH PROJECT, 2020 +** Twac +** File description: +** qt_layer +*/ + +#include "quadtree_internal.h" +#include + +bool collision_can_see(int l1, int l2) +{ + int can_see = CAN_SEE; + int can_be_seen = CAN_BE_SEEN; + + for (int i = 0; i < 4; i++) { + if (l1 & can_see && l2 & can_be_seen) + return (true); + can_see <<= 2; + can_be_seen <<= 2; + } + return (false); +} \ No newline at end of file diff --git a/lib/quadtree/src/qt_position_overlap.c b/lib/quadtree/src/qt_position_overlap.c new file mode 100644 index 0000000..9bfa9fc --- /dev/null +++ b/lib/quadtree/src/qt_position_overlap.c @@ -0,0 +1,35 @@ +/* +** EPITECH PROJECT, 2020 +** Twac +** File description: +** qt_position_overlap +*/ + +#include "quadtree.h" + +//X1 is on the line or X2 is on the line or both X are on each side of the map +bool collision_overlapx(qt_intrect r1, qt_intrect r2) +{ + if (r1.x > r2.x && r1.x < r2.x + r2.w) + return (true); + if (r1.x + r1.w > r2.x && r1.x + r1.w < r2.x + r2.w) + return (true); + if (r1.x < r2.x && r1.x + r1.w > r2.x + r2.w) + return (true); + if (r1.x == r2.x) + return (true); + return (false); +} + +bool collision_overlapy(qt_intrect r1, qt_intrect r2) +{ + if (r1.y - r1.h < r2.y && r1.y - r1.h > r2.y - r2.h) + return (true); + if (r1.y < r2.y && r1.y - r1.h > r2.y - r2.h) + return (true); + if (r1.y < r2.y && r1.y > r2.y - r2.h) + return (true); + if (r1.y == r2.y) + return (true); + return (false); +} \ No newline at end of file diff --git a/lib/quadtree/src/qt_split.c b/lib/quadtree/src/qt_split.c new file mode 100644 index 0000000..43ee036 --- /dev/null +++ b/lib/quadtree/src/qt_split.c @@ -0,0 +1,55 @@ +/* +** EPITECH PROJECT, 2019 +** quadtree +** File description: +** qt_split +*/ + +#include "quadtree.h" +#include "quadtree_internal.h" +#include + +bool qt_collide(qt_intrect r1, qt_intrect r2) +{ + if (r1.x + r1.w < r2.x || r2.x + r2.w < r1.x) + return (false); + if (r1.y + r1.h < r2.y || r2.y + r2.h < r1.y) + return (false); + return (true); +} + +qt_object *qt_filter_objects(quadtree *tree, qt_intrect rect) +{ + qt_object *objs = my_calloc(tree->capacity, sizeof(qt_object)); + int len = 0; + + for (int i = 0; i < tree->capacity; i++) { + if (qt_collide(((qt_object *)tree->objects)[i].rect, rect)) + objs[len++] = ((qt_object *)tree->objects)[i]; + } + return (objs); +} + +quadtree *qt_split(quadtree *tree) +{ + quadtree *ret = malloc(sizeof(quadtree) * 4); + + if (!ret) + return (NULL); + for (int i = 0; i < 4; i++) { + ret[i].capacity = tree->capacity; + ret[i].rect.h = tree->rect.h / 2; + ret[i].rect.w = tree->rect.w / 2; + ret[i].rect.x = tree->rect.x; + if (i % 2) + ret[i].rect.x += tree->rect.w / 2; + ret[i].rect.y = tree->rect.y; + if (i > 1) + ret[i].rect.y += tree->rect.h / 2; + ret[i].objects = (void *)qt_filter_objects(tree, ret[i].rect); + } + tree->capacity = -1; + free(tree->objects); + tree->objects = (void *)ret; + return (ret); +} \ No newline at end of file diff --git a/lib/quadtree/src/quadtree.c b/lib/quadtree/src/quadtree.c new file mode 100644 index 0000000..5ff60ed --- /dev/null +++ b/lib/quadtree/src/quadtree.c @@ -0,0 +1,98 @@ +/* +** EPITECH PROJECT, 2019 +** quadtree +** File description: +** quadtree +*/ + +#include "quadtree.h" +#include "quadtree_internal.h" +#include +#include + +quadtree *qt_create(qt_intrect rect, int capacity) +{ + quadtree *tree = malloc(sizeof(quadtree)); + + if (!tree) + return (NULL); + tree->rect = rect; + tree->capacity = capacity; + tree->objects = my_calloc(capacity, sizeof(qt_object)); + if (!tree->objects) { + free(tree); + return (NULL); + } + return (tree); +} + +int qt_add(quadtree *tree, qt_object obj) +{ + int i = 0; + + if (tree->capacity > 0) { + while (i < tree->capacity && ((qt_object *)tree->objects)[i].id != -1) + i++; + if (i < tree->capacity) + ((qt_object *)tree->objects)[i] = obj; + else { + qt_split(tree); + qt_add(tree, obj); + } + return (0); + } + for (i = 0; i < 4; i++) { + if (qt_collide(((quadtree *)tree->objects)[i].rect, obj.rect)) + qt_add(&((quadtree *)tree->objects)[i], obj); + } + return (0); +} + +qt_object *qt_getobj(quadtree *tree, int id) +{ + qt_object *obj; + int cap = tree->capacity; + + if (tree->capacity == -1) { + for (int i = 0; i < 4; i++) { + obj = qt_getobj(((quadtree **)tree->objects)[i], id); + if (obj) + return (obj); + } + return (NULL); + } + for (int i = 0; i < cap && ((qt_object *)tree->objects)[i].id != -1; i++) { + if (((qt_object *)tree->objects)[i].id == id) + return (&((qt_object *)tree->objects)[i]); + } + return (NULL); +} + +int qt_remove(quadtree *tree, int id) +{ + int last = tree->capacity - 1; + qt_object *objects; + + if (tree->capacity == -1) { + for (int i = 0; i < 4; i++) { + qt_remove(&((quadtree *)tree->objects)[i], id); + } + return (0); + } + objects = (qt_object *)tree->objects; + while (last > 0 && ((qt_object *)tree->objects)[last].id == -1) + last--; + for (int i = 0; i < tree->capacity && objects[i].id != -1; i++) { + if (objects[i].id == id) { + objects[i] = objects[last]; + objects[last] = (qt_object){-1, {-1, -1, -1, -1}, -1}; + } + } + return (0); +} + +int qt_update(quadtree *tree, qt_object obj) +{ + qt_remove(tree, obj.id); + return (qt_add(tree, obj)); +} \ No newline at end of file diff --git a/lib/quadtree/src/utility/calloc.c b/lib/quadtree/src/utility/calloc.c new file mode 100644 index 0000000..ba6962f --- /dev/null +++ b/lib/quadtree/src/utility/calloc.c @@ -0,0 +1,19 @@ +/* +** EPITECH PROJECT, 2019 +** quadtree +** File description: +** calloc +*/ + +#include +#include + +void *my_calloc(int capacity, int size) +{ + void *alloc = malloc(capacity * size); + + if (!alloc) + return (NULL); + memset(alloc, -1, capacity * size); + return (alloc); +} \ No newline at end of file diff --git a/lib/quadtree/tests/test_add.c b/lib/quadtree/tests/test_add.c new file mode 100644 index 0000000..4fdc47e --- /dev/null +++ b/lib/quadtree/tests/test_add.c @@ -0,0 +1,41 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** test_add +*/ + +#include "quadtree.h" +#include "quadtree_internal.h" +#include + +Test(qt_add, without_split) +{ + quadtree *tree = qt_create((qt_intrect){0, 0, 1000, 1000}, 3); + + qt_add(tree, (qt_object){0, {0, 0, 100, 100}}); + qt_add(tree, (qt_object){1, {0, 0, 100, 100}}); + qt_add(tree, (qt_object){2, {0, 0, 100, 100}}); + cr_assert_eq(tree->capacity, 3); + cr_assert_eq(((qt_object *)tree->objects)[0].id, 0); + cr_assert_eq(((qt_object *)tree->objects)[1].id, 1); + cr_assert_eq(((qt_object *)tree->objects)[2].id, 2); +} + +Test(qt_add, with_split) +{ + quadtree *tree = qt_create((qt_intrect){0, 0, 1000, 1000}, 3); + quadtree *child; + + qt_add(tree, (qt_object){0, {0, 0, 100, 100}}); + qt_add(tree, (qt_object){1, {0, 0, 100, 100}}); + qt_add(tree, (qt_object){2, {0, 0, 100, 100}}); + qt_add(tree, (qt_object){4, {550, 0, 100, 100}}); + cr_assert_eq(tree->capacity, -1); + child = &((quadtree *)tree->objects)[0]; + cr_assert_eq(((qt_object *)child->objects)[0].id, 0); + cr_assert_eq(((qt_object *)child->objects)[1].id, 1); + cr_assert_eq(((qt_object *)child->objects)[2].id, 2); + child = &((quadtree *)tree->objects)[1]; + cr_assert_eq(((qt_object *)child->objects)[0].id, 4); +} \ No newline at end of file diff --git a/lib/quadtree/tests/test_collide.c b/lib/quadtree/tests/test_collide.c new file mode 100644 index 0000000..ac4fdbe --- /dev/null +++ b/lib/quadtree/tests/test_collide.c @@ -0,0 +1,25 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** test_collide +*/ + +#include +#include "quadtree.h" + +Test(collide, yes) +{ + qt_intrect r1 = {0, 0, 10, 10}; + qt_intrect r2 = {0, 0, 100, 100}; + + cr_assert_eq(qt_collide(r1, r2), true); +} + +Test(collide, no) +{ + qt_intrect r1 = {0, 0, -10, -10}; + qt_intrect r2 = {0, 0, 100, 100}; + + cr_assert_eq(qt_collide(r1, r2), false); +} \ No newline at end of file diff --git a/lib/xmlparser/.gitignore b/lib/xmlparser/.gitignore new file mode 100644 index 0000000..8e5d36f --- /dev/null +++ b/lib/xmlparser/.gitignore @@ -0,0 +1,3 @@ +*.o +*.gc* +libxmlparser.a \ No newline at end of file diff --git a/lib/xmlparser/LICENSE b/lib/xmlparser/LICENSE new file mode 100644 index 0000000..23c2e93 --- /dev/null +++ b/lib/xmlparser/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2019 Tristan Roux + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/lib/xmlparser/Makefile b/lib/xmlparser/Makefile new file mode 100644 index 0000000..5076871 --- /dev/null +++ b/lib/xmlparser/Makefile @@ -0,0 +1,73 @@ +## +## EPITECH PROJECT, 2019 +## xmlparser +## File description: +## Makefile +## + +SRC = src/xmlparser.c \ + src/parsenode.c \ + src/list_utility.c \ + src/helper.c \ + src/xmlproperties.c \ + src/rawnode.c \ + src/xml_destroy.c \ + src/xmlget.c \ + src/strangeget.c \ + src/floatutils.c \ + src/child.c + +OBJ = $(SRC:%.c=%.o) + +TESTS = tests/test_basics.c \ + tests/tests_realxml.c + +TEST_MAIN = tests/test_main.c + +COVERAGE = --coverage -lcriterion + +INCLUDE = -I ./include + +CFLAGS = $(INCLUDE) -Wall -Wextra -Wshadow + +LDFLAGS = -lmy -L ../my + +CC = gcc + +AR = ar rc + +NAME = libxmlparser.a + +UT = ./ut + +FT = ./ft + +all: build + +build: $(OBJ) + $(AR) $(NAME) $(OBJ) + +tests_run: + $(CC) -o $(UT) $(SRC) $(TESTS) $(COVERAGE) $(CFLAGS) $(LDFLAGS) + $(UT) + +clean: + $(RM) $(OBJ) + $(RM) *.gc* + +fclean: clean + $(RM) $(NAME) + $(RM) $(UT) + $(RM) $(FT) + +re: fclean all + +dbg: CFLAGS += -g +dbg: re + +main-dbg: CFLAGS += -g +main-dbg: fclean +main-dbg: $(OBJ) + $(CC) -o $(FT) $(SRC) $(TEST_MAIN) $(CFLAGS) $(LDFLAGS) + +.PHONY: all build clean fclean diff --git a/lib/xmlparser/README.md b/lib/xmlparser/README.md new file mode 100644 index 0000000..b643cf6 --- /dev/null +++ b/lib/xmlparser/README.md @@ -0,0 +1,28 @@ +# XmlParser + +An xml parser in C. + +# Dependencies +To compile, you'll need gcc, ar rc and make. + +### System calls +Open, Close, Read, Stat + +## Build +Simple clone the repo and use the command ``make``. + +## Usage +Use ``xml_parse(path)`` to parse the whole xml. + +Use ``xml_getnode(xml, node_name)`` to get the first node with the name you inputed. + +Use ``xml_getproperty(key)`` to get an attribute of the xml. + +## Unsuported yet +Attributes with semi quotes (') instead of quotes (") + +Comments + +String containing multiples following spaces. + +String containing \n, \r or \t (theses specials chars are removed) diff --git a/lib/xmlparser/include/my.h b/lib/xmlparser/include/my.h new file mode 100644 index 0000000..dfd923f --- /dev/null +++ b/lib/xmlparser/include/my.h @@ -0,0 +1,123 @@ +/* +** EPITECH PROJECT, 2019 +** My lib +** File description: +** Header file +*/ +#pragma once + +int my_str_islower_or_num(const char *str); + +int count_valid_queens_placements(int n); + +char *my_strchr(char *str, char c); + +int my_compute_power_it(int n, int p); + +int my_compute_power_rec(int n, int p); + +int my_compute_factorial_it(int n); + +int my_compute_factorial_rec(int n); + +int my_compute_square_root(int n); + +char *my_evil_str(char *str); + +int my_find_prime_sup(int n); + +int my_getnbr_base(const char *str, const char *base); + +int my_getnbr(const char *str); + +int my_isneg(int n); + +int my_is_prime(int n); + +void my_print_alpha(void); + +void my_print_comb2(void); + +void my_print_combn(int n); + +void my_print_comb(void); + +void my_print_digits(void); + +void my_print_revalpha(void); + +void my_putchar(char c); + +void my_putlong_base(long n, const char *base); + +void my_putnbr_base(int n, const char *base); + +void my_put_nbr(int n); + +void my_putstr(const char *str); + +void my_revstr(char *str); + +void my_showmem(const char *str); + +void my_showstr(const char *str); + +void my_sort_int_array(int *array); + +int is_letter(char c); + +int is_alpha(char c); + +int is_num(char c); + +int is_digit(char c); + +char *my_strcapitalize(char *str); + +char *my_strcat(char *dest, const char *src); + +int my_strcmp(const char *s1, const char *s2); + +char *my_strcpy(const char *str); + +int my_str_isalpha(const char *str); + +int is_lowercase(char c); + +int my_str_islower(const char *str); + +int my_str_isnum(const char *str); + +int is_printable(char c); +int my_str_isprintable(const char *str); + +int is_upper(char c); +int my_str_isupper(const char *str); + +int my_strlen(const char *str); + +int my_strlowcase(const char *str); + +char *my_strncat(char *dest, const char *src, int n); + +int my_strncmp(const char *s1, const char *s2, int n); + +char *my_strncpy(char *dest, const char *src, int n); + +char *my_strstr(const char *str, const char *to_find); + +int my_strupcase(const char *str); + +void my_swap(int *a, int *b); + +char *my_strdup(const char *str); + +char **my_str_to_word_array(const char *str); + +int my_show_word_array(const char **words); + +int is_alphanum(char c); + +int first_alphanum(const char *str); + +int index_of(const char *str, char c); \ No newline at end of file diff --git a/lib/xmlparser/include/xml.h b/lib/xmlparser/include/xml.h new file mode 100644 index 0000000..389ddb3 --- /dev/null +++ b/lib/xmlparser/include/xml.h @@ -0,0 +1,44 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** xml +*/ + +typedef struct node node; +typedef struct dictionary dictionary; + +#pragma once + +#include +#include + +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); +bool xml_hasproperty(node *n, const char *key); +char *xml_getproperty(node *n, const char *key); +char *xml_gettempprop(node *n, const char *key); +int xml_getintprop(node *n, const char *key); +int xml_getbinaprop(node *n, const char *key); +int xml_gethexaprop(node *n, const char *key); +float xml_getfloatprop(node *n, const char *key); +int xml_getchildcount(node *n); +int xml_getchildcount_filtered(node *n, char *name); +void xml_destroy(node *n); \ No newline at end of file diff --git a/lib/xmlparser/include/xml_internal.h b/lib/xmlparser/include/xml_internal.h new file mode 100644 index 0000000..65ace57 --- /dev/null +++ b/lib/xmlparser/include/xml_internal.h @@ -0,0 +1,20 @@ +/* +** EPITECH PROJECT, 2019 +** xmlParser +** File description: +** xml_internal +*/ + +#pragma once + +#include + +void xml_fillclosing_br(char *buffer, const char *name); +char *xml_getname(char **nodestr, bool *has_parameters, bool *has_childs); +dictionary *xml_getproperties(char **nodestr, bool *can_have_child); +char *trimstr(char *str); +int xml_getstringdata(node *n, char **nodestr); +int xml_checkclosing(node *n, char **nodestr); +dictionary *property_add(dictionary *list, dictionary *property); +node *xml_parsenode(char **nodestr); +int get_int_size(int n); \ No newline at end of file diff --git a/lib/xmlparser/src/child.c b/lib/xmlparser/src/child.c new file mode 100644 index 0000000..99508f6 --- /dev/null +++ b/lib/xmlparser/src/child.c @@ -0,0 +1,38 @@ +/* +** EPITECH PROJECT, 2020 +** Twac +** File description: +** child +*/ + +#include "xml.h" +#include "my.h" + +int xml_getchildcount_filtered(node *n, char *name) +{ + int i = 0; + + if (!n || !n->child) + return (0); + n = n->child; + while (n->next) { + n = n->next; + if (!my_strcmp(n->name, name)) + i++; + } + return (i); +} + +int xml_getchildcount(node *n) +{ + int i = 1; + + if (!n || !n->child) + return (0); + n = n->child; + while (n->next) { + n = n->next; + i++; + } + return (i); +} \ No newline at end of file diff --git a/lib/xmlparser/src/floatutils.c b/lib/xmlparser/src/floatutils.c new file mode 100644 index 0000000..e4f59c3 --- /dev/null +++ b/lib/xmlparser/src/floatutils.c @@ -0,0 +1,20 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** floatutils +*/ + +#include "my.h" + +int get_int_size(int n) +{ + int base_size = my_strlen("0123456789"); + int i = 1; + + while (n >= base_size) { + n /= base_size; + i++; + } + return (i); +} \ No newline at end of file diff --git a/lib/xmlparser/src/helper.c b/lib/xmlparser/src/helper.c new file mode 100644 index 0000000..0a7dccb --- /dev/null +++ b/lib/xmlparser/src/helper.c @@ -0,0 +1,42 @@ +/* +** EPITECH PROJECT, 2019 +** xmlParser +** File description: +** helper +*/ + +#include "my.h" +#include + +void xml_fillclosing_br(char *buffer, const char *name) +{ + int i = my_strlen(name) + 2; + + buffer[0] = '<'; + buffer[1] = '/'; + buffer[2] = '\0'; + my_strcat(buffer, name); + buffer[i] = '>'; + buffer[i + 1] = '\0'; +} + +char *trimstr(char *str) +{ + int len = 0; + char *trimed; + + for (int i = 0; str[i]; i++) { + if (str[i] != '\t' && str[i] != '\n' && str[i] != '\r') + len++; + } + trimed = malloc(sizeof(char) * (len + 1)); + len = 0; + for (int i = 0; str[i]; i++) { + if (str[i] != '\t' && str[i] != '\n' && str[i] != '\r') { + trimed[len] = str[i]; + len++; + } + } + trimed[len] = '\0'; + return (trimed); +} \ No newline at end of file diff --git a/lib/xmlparser/src/list_utility.c b/lib/xmlparser/src/list_utility.c new file mode 100644 index 0000000..e6598e7 --- /dev/null +++ b/lib/xmlparser/src/list_utility.c @@ -0,0 +1,24 @@ +/* +** EPITECH PROJECT, 2019 +** xmlparser +** File description: +** list_utility +*/ + +#include "xml.h" +#include + +dictionary *property_add(dictionary *list, dictionary *property) +{ + dictionary *listconst = list; + + property->next = NULL; + if (!list) + return (property); + else { + while (list->next) + list = list->next; + list->next = property; + } + return (listconst); +} \ No newline at end of file diff --git a/lib/xmlparser/src/parsenode.c b/lib/xmlparser/src/parsenode.c new file mode 100644 index 0000000..5c6da93 --- /dev/null +++ b/lib/xmlparser/src/parsenode.c @@ -0,0 +1,100 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** parseline +*/ + +#include "xml.h" +#include "xml_internal.h" +#include "my.h" +#include +#include + +int xml_checkclosing(node *n, char **nodestr) +{ + if (!(*nodestr)[0] && (*nodestr)[1] == '/') { + if (my_strstr(*nodestr + 1, n->name) != *nodestr + 2) + return (-1); + *nodestr += 2 + my_strlen(n->name); + if ((*nodestr)[0] != '>') + return (-1); + *nodestr += 1; + return (0); + } + if (!(*nodestr)[0]) { + *nodestr += 1; + return (0); + } + return (-1); +} + +void xml_parsenext(node *n, char **nodestr, int depth) +{ + if (depth != 0) + n->next = xml_parsenode(nodestr); + else + n->next = NULL; +} + +int xml_parsechild(node *n, char **nodestr, bool has_child) +{ + char endname[my_strlen(n->name + 3)]; + static int depth = 0; + char *p; + + if (has_child) { + xml_fillclosing_br(endname, n->name); + p = my_strstr(*nodestr, endname); + if (!p) + return (-1); + *p = '\0'; + depth++; + n->child = xml_parsenode(nodestr); + if (xml_checkclosing(n, nodestr) < 0) + return (-1); + depth--; + } else + n->child = NULL; + xml_parsenext(n, nodestr, depth); + return (0); +} + +node *xml_parseproperties(node *n, char **str, bool has_params, bool has_childs) +{ + if (has_params) { + n->properties = xml_getproperties(str, &has_childs); + if (!n->properties) + return (NULL); + } + else + n->properties = NULL; + if (xml_parsechild(n, str, has_childs) < 0) + return (NULL); + return (n); +} + +node *xml_parsenode(char **nodestr) +{ + node *n = malloc(sizeof(node)); + bool has_param; + bool has_childs; + char *p = my_strchr(*nodestr, '>'); + + if (!n) + return (NULL); + if ((*nodestr)[0] == '<') { + if (p && (*nodestr)[1] != '/') { + *p = '\0'; + *nodestr += 1; + n->name = xml_getname(nodestr, &has_param, &has_childs); + if (n->name) + return (xml_parseproperties(n, nodestr, has_param, has_childs)); + } + } else if ((*nodestr)[1] != '/' && xml_getstringdata(n, nodestr) == 0) { + n->next = xml_parsenode(nodestr); + return (n); + } + free(n); + return (NULL); +} \ No newline at end of file diff --git a/lib/xmlparser/src/rawnode.c b/lib/xmlparser/src/rawnode.c new file mode 100644 index 0000000..6a53007 --- /dev/null +++ b/lib/xmlparser/src/rawnode.c @@ -0,0 +1,26 @@ +/* +** EPITECH PROJECT, 2019 +** xmlparser +** File description: +** rawnode +*/ + +#include "xml.h" +#include "xml_internal.h" +#include "my.h" +#include +#include + +int xml_getstringdata(node *n, char **nodestr) +{ + dictionary *prop = malloc(sizeof(dictionary)); + + prop->key = my_strdup("data"); + prop->value = my_strdup(*nodestr); + prop->next = NULL; + *nodestr += my_strlen(*nodestr); + n->name = my_strdup("data"); + n->child = NULL; + n->properties = prop; + return (0); +} \ No newline at end of file diff --git a/lib/xmlparser/src/strangeget.c b/lib/xmlparser/src/strangeget.c new file mode 100644 index 0000000..0e02eb6 --- /dev/null +++ b/lib/xmlparser/src/strangeget.c @@ -0,0 +1,41 @@ +/* +** EPITECH PROJECT, 2020 +** Twac +** File description: +** strangeget +*/ + +#include "xml.h" +#include "xml_internal.h" +#include "my.h" +#include + +int xml_gethexaprop(node *n, const char *key) +{ + char *prop = xml_gettempprop(n, key); + + if (!prop || my_strlen(prop) == 0) + return (0); + if (prop[0] == '0' && prop[1] == 'x') + prop += 2; + if (my_str_islower_or_num(prop)) + return (my_getnbr_base(prop, "0123456789abcdef")); + else + return (my_getnbr_base(prop, "0123456789ABCDEF")); +} + +int xml_getbinaprop(node *n, const char *key) +{ + char *prop = xml_gettempprop(n, key); + + if (!prop || my_strlen(prop) == 0) + return (0); + if (prop[0] == '0' && prop[1] == 'b') + prop += 2; + return (my_getnbr_base(prop, "01")); +} + +bool xml_hasproperty(node *n, const char *key) +{ + return (xml_gettempprop(n, key) != NULL); +} \ No newline at end of file diff --git a/lib/xmlparser/src/xml_destroy.c b/lib/xmlparser/src/xml_destroy.c new file mode 100644 index 0000000..137d4b3 --- /dev/null +++ b/lib/xmlparser/src/xml_destroy.c @@ -0,0 +1,33 @@ +/* +** EPITECH PROJECT, 2019 +** xmlparser +** File description: +** xml_destroy +*/ + +#include "xml.h" +#include + +void xml_free_dict(dictionary *dic) +{ + if (!dic) + return; + if (dic->next) + xml_free_dict(dic->next); + free(dic->key); + free(dic->value); + free(dic); +} + +void xml_destroy(node *n) +{ + if (!n) + return; + if (n->next) + xml_destroy(n->next); + if (n->child) + xml_destroy(n->child); + xml_free_dict(n->properties); + free(n->name); + free(n); +} \ No newline at end of file diff --git a/lib/xmlparser/src/xmlget.c b/lib/xmlparser/src/xmlget.c new file mode 100644 index 0000000..62b7bd5 --- /dev/null +++ b/lib/xmlparser/src/xmlget.c @@ -0,0 +1,76 @@ +/* +** EPITECH PROJECT, 2019 +** xmlparser +** File description: +** xmlget +*/ + +#include "xml.h" +#include "xml_internal.h" +#include "my.h" +#include "math.h" +#include + +char *xml_gettempprop(node *n, const char *key) +{ + if (!n) + return (NULL); + for (dictionary *prop = n->properties; prop; prop = prop->next) { + if (!my_strcmp(key, prop->key)) + return (prop->value); + } + return (NULL); +} + +char *xml_getproperty(node *n, const char *key) +{ + char *prop = xml_gettempprop(n, key); + + if (prop) + return (my_strdup(prop)); + return (NULL); +} + +int xml_getintprop(node *n, const char *key) +{ + char *prop = xml_gettempprop(n, key); + + if (!prop || my_strlen(prop) == 0) + return (0); + return (my_getnbr(prop)); +} + +float xml_getfloatprop(node *n, const char *key) +{ + char *prop = xml_gettempprop(n, key); + float nbr; + int deci; + + if (!prop) + return (0); + for (int i = 0; prop[i]; i++) { + if (!is_num(prop[i]) && prop[i] != '.') + return (-1); + } + nbr = (float)my_getnbr(prop); + prop += get_int_size(nbr); + if (*prop) { + deci = my_getnbr(prop + 1); + nbr += deci / pow(10, (float)get_int_size(deci)); + } + return (nbr); +} + +node *xml_getnode(node *parent, const char *name) +{ + node *tmp; + + if (!my_strcmp(parent->name, name)) + return (parent); + for (node *n = parent->child; n; n = n->next) { + tmp = xml_getnode(n, name); + if (tmp != NULL) + return (tmp); + } + return (NULL); +} \ No newline at end of file diff --git a/lib/xmlparser/src/xmlparser.c b/lib/xmlparser/src/xmlparser.c new file mode 100644 index 0000000..5bf3c62 --- /dev/null +++ b/lib/xmlparser/src/xmlparser.c @@ -0,0 +1,93 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** xmlparser +*/ + +#include "my.h" +#include "xml.h" +#include "xml_internal.h" +#include +#include +#include +#include +#include + +int xml_handle_prolog(char **nodestr) +{ + char *strconst = *nodestr; + + if (my_strstr(*nodestr, ""); + if (!*nodestr) { + free(strconst); + return (-1); + } + *nodestr += 2; + } + return (0); +} + +bool is_charvalid(char c) +{ + if (c == ' ' || c == '\t' || c == '\n' || c == '\r') + return (false); + if (c == '"' || c == '\'') + return (false); + if (c == '=') + return (false); + if (c == '>' || c == '<' || c == '/') + return (false); + return (true); +} + +bool is_space_usefull(char *nodestr, int i) +{ + if (i == 0 || (!is_charvalid(nodestr[i - 1]) && nodestr[i - 1] != '"')) + return (false); + if (i + 1 == my_strlen(nodestr) || !is_charvalid(nodestr[i + 1])) + return (false); + return (true); +} + +node *xml_parsestr(char *nodestr) +{ + node *n; + char *strconst = nodestr; + + if (xml_handle_prolog(&nodestr) < 0) + return (NULL); + for (int i = 0; nodestr[i]; i++) { + if (nodestr[i] == ' ' && !is_space_usefull(nodestr, i)) + nodestr[i] = '\t'; + } + nodestr = trimstr(nodestr); + free(strconst); + strconst = nodestr; + n = xml_parsenode(&nodestr); + free(strconst); + return (n); +} + +node *xml_parse(const char *path) +{ + node *n = NULL; + int fd = open(path, O_RDONLY); + struct stat stats; + char *nodestr; + int count; + + if (fd < 0) + return (NULL); + fstat(fd, &stats); + nodestr = malloc(stats.st_size + 1); + if (nodestr) { + count = read(fd, nodestr, stats.st_size); + nodestr[stats.st_size] = '\0'; + if (count == stats.st_size) + n = xml_parsestr(nodestr); + } + close(fd); + return (n); +} \ No newline at end of file diff --git a/lib/xmlparser/src/xmlproperties.c b/lib/xmlparser/src/xmlproperties.c new file mode 100644 index 0000000..5410260 --- /dev/null +++ b/lib/xmlparser/src/xmlproperties.c @@ -0,0 +1,85 @@ +/* +** EPITECH PROJECT, 2019 +** xmlparser +** File description: +** xmlproperties +*/ + +#include "my.h" +#include "xml.h" +#include "xml_internal.h" +#include +#include + +char *xml_getname(char **nodestr, bool *has_parameters, bool *has_childs) +{ + char *p = my_strchr(*nodestr, ' '); + char *name; + int i = 1; + + if (p) + *has_parameters = true; + else { + *has_parameters = false; + if (!(p = my_strchr(*nodestr, '/'))) { + p = my_strchr(*nodestr, '>'); + *has_childs = true; + } else { + i++; + *has_childs = false; + } if (!p) + p = *nodestr + my_strlen(*nodestr); + } + *p = '\0'; + name = my_strdup(*nodestr); + *nodestr = p + i; + return (name); +} + +dictionary *xml_parseproperty(char **nodestr) +{ + dictionary *property = malloc(sizeof(dictionary)); + char *p; + + if (!property) + return (NULL); + p = my_strchr(*nodestr, '='); + if (!p) + return (NULL); + *p = '\0'; + property->key = my_strdup(*nodestr); + *nodestr = p + 1; + if ((*nodestr)[0] != '"') + return (NULL); + *nodestr += 1; + p = my_strchr(*nodestr, '"'); + if (!p) + return (NULL); + *p = '\0'; + property->value = my_strdup(*nodestr); + *nodestr = p + 1; + return (property); +} + +dictionary *xml_getproperties(char **nodestr, bool *can_have_child) +{ + dictionary *properties = NULL; + dictionary *property = NULL; + + while ((*nodestr)[0] != '\0') { + property = xml_parseproperty(nodestr); + if (!property) + return (NULL); + properties = property_add(properties, property); + if ((*nodestr)[0] == ' ') + *nodestr += 1; + if ((*nodestr)[0] == '/') { + *nodestr += 1; + *can_have_child = false; + } + else + *can_have_child = true; + } + *nodestr += 1; + return (properties); +} \ No newline at end of file diff --git a/lib/xmlparser/tests/test_basics.c b/lib/xmlparser/tests/test_basics.c new file mode 100644 index 0000000..680bc0a --- /dev/null +++ b/lib/xmlparser/tests/test_basics.c @@ -0,0 +1,83 @@ +/* +** EPITECH PROJECT, 2019 +** xmlparser +** File description: +** test_basics +*/ + +#include "xml.h" +#include "xml_internal.h" +#include +#include + +Test(xml, simple) +{ + char *xml = strdup(""); + node *n = xml_parsenode(&xml); + + cr_assert_str_eq(n->name, "yes"); + cr_assert_eq(n->child, NULL); + cr_assert_eq(n->next, NULL); + cr_assert_eq(n->properties, NULL); +} + +Test(xml, withparam) +{ + char *xml = strdup(""); + node *n = xml_parsenode(&xml); + + cr_assert_str_eq(n->name, "yes"); + cr_assert_eq(n->child, NULL); + cr_assert_eq(n->next, NULL); + cr_assert_str_eq(n->properties->key, "params"); + cr_assert_str_eq(n->properties->value, "Test"); + cr_assert_eq(n->properties->next, NULL); +} + +Test(xml, withnext) +{ + char *xml = strdup(""); + node *n = xml_parsenode(&xml); + + cr_assert_str_eq(n->name, "yes"); + cr_assert_str_eq(n->child->name, "nop"); + cr_assert_eq(n->child->child, NULL); + cr_assert_eq(n->child->properties, NULL); + cr_assert_str_eq(n->child->next->name, "yep"); + cr_assert_eq(n->child->next->child, NULL); + cr_assert_eq(n->child->next->properties, NULL); + cr_assert_eq(n->child->next->next, NULL); + cr_assert_str_eq(n->properties->key, "params"); + cr_assert_str_eq(n->properties->value, "Test"); + cr_assert_eq(n->properties->next, NULL); +} + +Test(xml, withchild) +{ + char *xml = strdup(""); + node *n = xml_parsenode(&xml); + + cr_assert_str_eq(n->name, "yes"); + cr_expect_eq(n->next, NULL); + cr_assert_eq(n->properties, NULL); + cr_assert_str_eq(n->child->name, "nop"); + cr_assert_eq(n->child->child, NULL); + cr_assert_eq(n->child->properties, NULL); + cr_assert_eq(n->child->next, NULL); +} + +Test(xml, withchildwparams) +{ + char *xml = strdup(""); + node *n = xml_parsenode(&xml); + + cr_assert_str_eq(n->name, "yes"); + cr_assert_eq(n->next, NULL); + cr_assert_str_eq(n->properties->key, "params"); + cr_assert_str_eq(n->properties->value, "Test"); + cr_assert_eq(n->properties->next, NULL); + cr_assert_str_eq(n->child->name, "nop"); + cr_assert_eq(n->child->child, NULL); + cr_assert_eq(n->child->properties, NULL); + cr_assert_eq(n->child->next, NULL); +} \ No newline at end of file diff --git a/lib/xmlparser/tests/test_main.c b/lib/xmlparser/tests/test_main.c new file mode 100644 index 0000000..f1195cc --- /dev/null +++ b/lib/xmlparser/tests/test_main.c @@ -0,0 +1,19 @@ +/* +** EPITECH PROJECT, 2019 +** xmlparser +** File description: +** test_main +*/ + +#include "xml.h" +#include +#include + +int main(int argc, char **argv) +{ + if (argc != 2) + return (printf("Usage: %s xml_path\n", argv[0]), 0); + node *n = xml_parse(argv[1]); + xml_destroy(n); + return (0); +} \ No newline at end of file diff --git a/lib/xmlparser/tests/testprolog.txt b/lib/xmlparser/tests/testprolog.txt new file mode 100644 index 0000000..6c89782 --- /dev/null +++ b/lib/xmlparser/tests/testprolog.txt @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/lib/xmlparser/tests/tests_realxml.c b/lib/xmlparser/tests/tests_realxml.c new file mode 100644 index 0000000..3410c74 --- /dev/null +++ b/lib/xmlparser/tests/tests_realxml.c @@ -0,0 +1,47 @@ +/* +** EPITECH PROJECT, 2019 +** xmlparser +** File description: +** tests_realxml +*/ + +#include "xml.h" +#include +#include + +Test(xml, complete) +{ + node *n = xml_parse("tests/testprolog.txt"); + + cr_assert_str_eq(n->name, "yes"); + cr_assert_eq(n->next, NULL); + cr_assert_eq(n->properties, NULL); + cr_assert_str_eq(n->child->name, "nop"); + cr_assert_eq(n->child->child, NULL); + cr_assert_eq(n->child->properties, NULL); + cr_assert_str_eq(n->child->next->name, "Test"); + cr_assert_eq(n->child->next->properties, NULL); + cr_assert_eq(n->child->next->child, NULL); + cr_assert_eq(n->child->next->next, NULL); +} + +Test(xml, completewstring) +{ + node *n = xml_parse("tests/teststring.txt"); + + cr_assert_str_eq(n->name, "entity"); + cr_assert_eq(n->next, NULL); + cr_assert_str_eq(n->properties->key, "id"); + cr_assert_str_eq(n->properties->value, "0"); + cr_assert_eq(n->properties->next, NULL); + cr_assert_str_eq(n->child->name, "PositionComponent"); + cr_assert_eq(n->child->properties, NULL); + cr_assert_eq(n->child->next, NULL); + cr_assert_str_eq(n->child->child->name, "pos"); + cr_assert_str_eq(n->child->child->child->properties->value, "5,5"); + cr_assert_str_eq(n->child->child->next->name, "size"); + cr_assert_str_eq(n->child->child->next->properties->key, "x"); + cr_assert_str_eq(n->child->child->next->properties->value, "500"); + cr_assert_str_eq(n->child->child->next->properties->next->key, "y"); + cr_assert_str_eq(n->child->child->next->properties->next->value, "500"); +} \ No newline at end of file diff --git a/lib/xmlparser/tests/teststring.txt b/lib/xmlparser/tests/teststring.txt new file mode 100644 index 0000000..e368301 --- /dev/null +++ b/lib/xmlparser/tests/teststring.txt @@ -0,0 +1,6 @@ + + + 5, 5 + + + \ No newline at end of file