diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..036c111 --- /dev/null +++ b/Makefile @@ -0,0 +1,77 @@ +#!/usr/bin/make -f +# --- +# Project: my_runner +# Author: yann.magnin@epitech.eu +# --- +include global.mk + + +# function +# $1 module name +# $2 file +define rule-module +build/$1_$(patsubst %.c,%,$2).o: src/$1/$2 + $(cc) $(cflags) -c $$< -o $$@ $(header) +endef + +config = gcc.cfg +name = my_runner +header = -Iinclude/ +module = core draw game memory menu text kinematic +build-dir = build +$(foreach mod,$(module), $(eval \ + mod-$(mod)-src = $(notdir $(wildcard src/$(mod)/*.c)) $n\ + mod-$(mod)-obj = $$(patsubst %.c,build/$(mod)_%.o,$$(mod-$(mod)-src)) \ +)) +target-obj = $(foreach mod, $(module), $(mod-$(mod)-obj)) + + + + +# Chek configuration file +ifeq ($(wildcard $(config)),) +$(warning "Configuration file missing. skipping some compilation flags.") +cflags += -g0 +else +cflags += @$(config) +endif + + + + +all: check_lib $(name) +$(target-obj): | $(build-dir) + +$(name): $(target-obj) + @ printf 'link binary $@\n' + $(cc) $(cflags) -o $@ $(target-obj) $(header) $(lib-link) -lgcc + +$(build-dir): + @ printf 'create build folder:\033[1;35m $@\033[0m\n' + mkdir -p $@ + +check_lib: + make -C lib/ + +$(foreach mod, $(module), \ + $(foreach source,$(mod-$(mod)-src), $(eval \ + $(call rule-module,$(mod),$(source)))) \ +) + + + + + +## +## Clean rule +## +clean: + rm -rf $(name) + make clean -C lib/ +fclean: clean + rm -rf build + make fclean -C lib/ +re: fclean all + + +.PHONY: clean re fclean diff --git a/configure b/configure new file mode 100755 index 0000000..7c5efb1 --- /dev/null +++ b/configure @@ -0,0 +1,111 @@ +#! /bin/bash + +# output file +file_gcc="gcc.cfg" + + +declare -A config +config[FILE_DEBUG]= +config[NO_WALL_MOVE]= +config[VALGRIND]= +config[WALL_SPEED]=5 +config[CAMERA_SPEED]=32 +config[FRAME_RATE]=64 +config[PLAYER_JUMP_MAX]=7 +config[PLAYER_SPEED]=6 +config[CAMERA_SHAKY]=15 +config[THROWER_STONE_SLEEP]=35 +config[THROWER_BULLET_SLEEP]=100 +config[BULLET_SPEED]=32 +config[NB_STONE]=64 +config[THROWER_DX_SLEEP]=20 + +function help() +{ + echo -e "Configuration script for my_runner." + echo -e "\nOption that will print some debug log:" + echo -e "\t\033[1;32m--file-debug [default: false]\033[0m" + echo -e "\tEnable some indication when map file are loading." + echo -e "\t\033[1;32m--valgrind-debug [default: false]\033[0m" + echo -e "\tAdd some flags for debugging with valgrind." + echo -e "\t\033[1;32m--help | -h\033[0m" + echo -e "\tPrint this text." + echo -e "\nOption that will affect the game:" + echo -e "\t\033[1;32m--wall-speed= [default: 5]\033[0m" + echo -e "\tAjuste wall speed." + echo -e "\t\033[1;32m--camera-speed= [default: 32]\033[0m" + echo -e "\tAjuste camera speed." + echo -e "\t\033[1;32m--camera-shaky= [default: 15]\033[0m" + echo -e "\tAjuste camera when player's dead." + echo -e "\t\033[1;32m--player-jump-time= [default: 7]\033[0m" + echo -e "\tAjuste player jump time." + echo -e "\t\033[1;32m--frame-rate= [default: 64]\033[0m" + echo -e "\tAjust frame rate." + echo -e "\t\033[1;32m--stone-dx= [default: 20]\033[0m" + echo -e "\tAjust X axis of thrower object." + echo -e "\t\033[1;32m--stone-nb= [default:64]\033[0m" + echo -e "\tAjust number of thrower object." + echo -e "\t\033[1;32m--thrower-stone-sleep= [default: 35]\033[0m" + echo -e "\tAjust the \"sleep\" of thrower. (stone)" + echo -e "\t\033[1;32m--thrower-bullet-sleep= [default: 100]\033[0m" + echo -e "\tAjust the \"sleep\" of thrower. (bullet)" + echo -e "\t\033[1;32m--no-wall [default: false]\033[0m" + echo -e "\tDisable moving wall." + exit 0 +} + +function option_update() +{ + if [[ "$1" -gt "0" ]]; then config[$2]=$1 + else errror; fi +} + +function error() +{ + echo -e "\033[1;33merror\033[0m need positive interger value." + exit 1 +} + +function out_gcc() +{ + if [ "${config[VALGRIND]}" != "" ]; then echo "-g3" + else echo "-g0"; fi + [ "${config[FILE_DEBUG]}" != "" ] && echo "-D DEBUG_LOAD_FILE" + [ "${config[NO_WALL_MOVE]}" != "" ] && echo "-D NO_WALL_MOVE" + echo "-D WALL_SPEED=${config[WALL_SPEED]}" + echo "-D CAMERA_SPEED=${config[CAMERA_SPEED]}" + echo "-D FRAME_RATE=${config[FRAME_RATE]}" + echo "-D PLAYER_MAX_JUMP=${config[PLAYER_JUMP_MAX]}" + echo "-D PLAYER_SPEED=${config[PLAYER_SPEED]}" + echo "-D CAMERA_SHAKY=${config[CAMERA_SHAKY]}" + echo "-D THROWER_STONE_SLEEP=${config[THROWER_STONE_SLEEP]}" + echo "-D THROWER_BULLET_SLEEP=${config[THROWER_BULLET_SLEEP]}" + echo "-D THROWER_DX_SLEEP=${config[THROWER_DX_SLEEP]}" + echo "-D NB_OBJECT=${config[NB_STONE]}" +} + +for arg; do case "$arg" in + --help | -h) help;; + --file-debug) config[FILE_DEBUG]=true;; + --no-wall) config[NO_WALL_MOVE]=true;; + --valgrind-debug) config[VALGRIND]=true;; + --wall-speed=*) option_update ${arg#*=} WALL_SPEED;; + --camera-speed=*) option_update ${arg#*=} CAMERA_SPEED;; + --camera-shaky=*) option_update ${arg#*=} CAMERA_SHAKY;; + --player-jump-time=*) option_update ${arg#*=} PLAYER_JUMP_MAX;; + --player-speed=*) option_update ${arg#*=} PLAYER_SPEED;; + --frame-rate=*) option_update ${arg#*=} FRAME_RATE;; + --object-speed=*) option_update ${arg#*=} OBJECT_SPEED;; + --thrower-stone-sleep=*) option_update ${arg#*=} THROWER_STONE_SLEEP;; + --thrower-bullet-sleep=*) option_update ${arg#*=} THROWER_BULLET_SLEEP;; + --stone-dx=*) option_update ${arg#*=} THROWER_DX_SLEEP;; + --stone-nb=*) option_update ${arg#*=} NB_STONE;; + *) + echo -e "\033[1;33merror\033[0m unreconized argument '$arg'" + exit 1 +esac; done + +echo "Configuration saved in $file_gcc." +out_gcc > $file_gcc +cat $file_gcc +exit 0 diff --git a/global.mk b/global.mk new file mode 100644 index 0000000..e513fad --- /dev/null +++ b/global.mk @@ -0,0 +1,13 @@ +# Global option use in each Makefile + +cc = gcc +ar = $(cc)-ar +lmystdio = lib/libmycsfml.a +lib-link = -Llib/ -lmycsfml -Llib/ -lmystdio -Llib/ -lmystring -Llib/ -lmystdlib +cflags = -W -Werror -Wextra -Wunused-value -fno-builtin -std=c99 -pedantic \ + -Os -lcsfml-graphics -lcsfml-system -lcsfml-window -lcsfml-audio + +define n +# Force newline character + +endef diff --git a/include/game/ascii.h b/include/game/ascii.h new file mode 100644 index 0000000..829d9b5 --- /dev/null +++ b/include/game/ascii.h @@ -0,0 +1,44 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ + +#ifndef __ASCII_H__ +# define __ASCII_H__ + +#define ASCII_DEFAULT_HEIGHT 5 +#define ASCII_DEFAULT_WIDTH 4 +#define ASCII_CENTER_X 0x01 +#define ASCII_CENTER_Y 0x02 +#define ASCII_ALPHA 0x04 +#define ASCII_REVERSE 0x08 + +#include "lib/my_stddef.h" +#include "lib/s_sfml.h" + +typedef struct ascii_s +{ + int width; + int height; + int offset; + int nb_line; + char *str; + int size_str; + int x; + int y; +} ascii_t; + +void my_print(sfml_t *sfml, int position[2], const char *str, uint32_t *option); +uint32_t *my_print_set_option(int size, uint32_t color_char, +uint32_t color_alpha, uint8_t action); + +void my_print_set_position(int position[2], int x, int y); +void reverse_update(void *pass[2], int position[2], uint32_t *option); +void message_set_start(sfml_t *sfml, ascii_t *ascii, uint32_t *option, int size_str); +ascii_t *init_set_ascii(const char *str, uint32_t *option); +int get_line_size(const char *str, int pos); + +void my_print_nbr(sfml_t *sfml, int nb, uint32_t *option, int pos[2]); +#endif diff --git a/include/game/ascii_table.h b/include/game/ascii_table.h new file mode 100644 index 0000000..1036c77 --- /dev/null +++ b/include/game/ascii_table.h @@ -0,0 +1,52 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ + +#ifndef __ASCII_TABLE_H__ +# define __ASCII_TABLE_H__ + +#include "lib/my_stddef.h" +static const uint16_t ascii_tab[256] = { + 32319, 32319, 32319, 32319, 32319, 32319, 32319, + 32319, 32319, 32319, 32319, 32319, 32319, 32319, + 32319, 32319, 32319, 32319, 32319, 32319, 32319, + 32319, 32319, 32319, 32319, 32319, 32319, 32319, + 32319, 32319, 32319, 32319, 0, 928, 24600, + 32095, 12269, 9362, 11946, 768, 17856, 465, + 20756, 4548, 65, 4228, 32, 24707, 31279, + 2025, 9907, 10929, 31900, 19133, 24239, 25235, + 32447, 31421, 320, 321, 17732, 10570, 4433, + 25264, 14963, 16015, 10943, 17966, 14911, 18111, + 17055, 20014, 31903, 18417, 30754, 27807, 1087, + 32031, 32223, 14894, 8863, 15918, 11935, 19113, + 17392, 31807, 30782, 31839, 27803, 24824, 26291, + 18400, 3224, 1009, 8712, 1057, 272, 16034, + 2239, 9510, 31906, 13998, 20964, 31404, 3231, + 1765, 22562, 9439, 2033, 15759, 7439, 6438, + 4431, 15684, 5455, 19113, 10184, 15407, 14382, + 15471, 9417, 14504, 13803, 18276, 992, 4977, + 4290, 32767, 22101, 21077, 21925, 5557, 27083, + 4970, 9192, 3042, 4575, 32196, 4416, 324, + 4356, 4164, 18738, 9801, 704, 12828, 20756, + 9096, 8456, 30728, 31326, 3018, 10962, 31698, + 30876, 21194, 21070, 31256, 31454, 31452, 10153, + 19186, 9637, 22197, 22517, 30910, 32437, 740, + 16833, 31812, 0, 0, 0, 0, 0, + 0, 352, 3207, 5189, 2274, 2114, 15362, + 15663, 1509, 5481, 15849, 15438, 10597, 10535, + 15628, 15727, 15726, 7495, 10926, 25119, 15523, + 32287, 18107, 15214, 13933, 11426, 24793, 22822, + 18094, 15022, 3475, 24735, 7303, 8847, 4964, + 11418, 0, 0, 0, 27972, 4443, 21956, + 4565, 10564, 4426, 22179, 14005, 17966, 14005, + 7603, 23971, 16043, 24243, 23731, 7843, 27947, + 19054, 5494, 21862, 14190, 21878, 368, 16736, + 8808, 8296, 26922, 20119, 2482, 18850, 10922, + 19122, 18610, 4772, 10378, 7479, 23847, 11819, + 23607, 18722, 26986, 20596 +}; + +#endif /* ASCII_TABLE*/ diff --git a/include/game/core.h b/include/game/core.h new file mode 100644 index 0000000..59c834a --- /dev/null +++ b/include/game/core.h @@ -0,0 +1,143 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ + +#ifndef __CORE_H__ +# define __CORE_H__ + +#define sgn(x) (x < 0 ? -1 : 1) + +#define SCREEN_WIDTH 1920 +#define SCREEN_HEIGHT 1080 +#define MAP_WIDTH_MIN (SCREEN_WIDTH >> 6) + 1 +#define MAP_HEIGHT_MIN (SCREEN_HEIGHT >> 6) + 1 +#define BLOCK_LOAD_X (SCREEN_WIDTH >> 6) +#define BLOCK_LOAD_Y (SCREEN_HEIGHT >> 6) + 1 + +#ifndef CAMERA_SPEED +# define CAMERA_SPEED 32 +#endif +#ifndef CAMERA_SHAKY +# define CAMERA_SHAKY 15 +#endif + +#ifndef NB_OBJECT +# define NB_OBJECT 32 +#endif +#ifndef THROWER_STONE_SLEEP +# define THROWER_STONE_SLEEP 35 +#endif +#ifndef THROWER_BULLET_SLEEP +# define THROWER_BULLET_SLEEP 100 +#endif +#ifndef THROWER_DX_SLEEP +# define THROWER_DX_SLEEP 10 +#endif +#ifndef BULLET_SPEED +# define BULLET_SPEED 32 +#endif +#define BULLET_STOP 0xffff + +#define PLAYER_IDLE 0x01 +#define PLAYER_RUN 0x02 +#define PLAYER_JUMP 0x04 +#define PLAYER_WALL 0x08 +#define PLAYER_LEFT 0x80 +#define PLAYER_DEAD 0x40 +#define PLAYER_WIN 0x20 +#define PLAYER_CANT_JUMP 0x10 +#define PLAYER_FALL_MAX 64 +#ifndef PLAYER_SPEED +# define PLAYER_SPEED 6 +#endif +#define PLAYER_DEAD_TIME 10 +#ifndef PLAYER_MAX_JUMP +# define PLAYER_MAX_JUMP 7 +#endif + +#define ID_PLAYER 'A' +#define ID_PRINCESS 'B' +#define ID_FAKE_PRINCESS 'C' +#define ID_GROUND '1' +#define ID_FAKE_GROUND '2' +#define ID_PIK_DOWN '3' +#define ID_PIK_UP ID_PIK_DOWN + 1 +#define ID_PIK_LEFT ID_PIK_DOWN + 2 +#define ID_PIK_RIGHT ID_PIK_DOWN + 3 +#define ID_THROWER_STONE 'V' +#define ID_THROWER_UP ID_THROWER_STONE + 1 +#define ID_THROWER_DOWN ID_THROWER_STONE + 2 +#define ID_THROWER_LEFT ID_THROWER_STONE + 3 +#define ID_THROWER_RIGHT ID_THROWER_STONE + 4 + +#define WALL_SIZE 128 +#ifndef WALL_SPEED +# define WALL_SPEED 5 +#endif + +#define KEY_LSHIFT 0x00000001 +#define KEY_RSHIFT 0x00000002 +#define KEY_RIGHT 0x00000004 +#define KEY_LEFT 0x00000008 +#define KEY_UP 0x00000010 +#define KEY_SPACE 0x00000020 +#define KEY_A 0x00000040 +#define KEY_ESC 0x00000080 +#define KEY_DOWN 0x00000100 +#define KEY_ENTER 0x00000200 + +#define PRES_LSHIFT 0x00010000 +#define PRES_RSHIFT 0x00020000 +#define PRES_RIGHT 0x00040000 +#define PRES_LEFT 0x00080000 +#define PRES_UP 0x00100000 +#define PRES_SPACE 0x00200000 +#define PRES_A 0x00400000 +#define PRES_ESC 0x00800000 +#define PRES_DOWN 0x01000000 +#define PRES_ENTER 0x02000000 + + +#define NB_MESSAGE 20 +#define MESSAGE_GAME 0 +#define MESSAGE_INTRO 1 +#define MESSAGE_X_TRIGGER 0x01 +#define MESSAGE_BEGIN 0x02 +#define MESSAGE_DEAD 0x04 +#define MESSAGE_FAKE_GROUND 0x08 +#define MESSAGE_FAKE_PRINCESS 0x10 +#define MESSAGE_X_PRINCESS 0x20 +#ifndef MESSAGE_DEFAULT_TIME +# define MESSAGE_DEFAULT_TIME 200 +#endif + +#define FADE_SPEED 8 +#define FADE_CLOSE 0x00 +#define FADE_OPEN 0x01 + +#define BMP_WIDTH 16 +#define BMP_HEIGHT 56 +#define BMP_SPEED 4 + +#define SOUND_ON 0x01 + +#define SCENE_LOAD_PLAYER 0x01 +#define SCENE_LOAD_THROWER 0x02 +#define SCENE_LOAD_MESSAGE_INTRO 0x04 +#define SCENE_LOAD_MESSAGE_GAME 0x08 +#define SCENE_LOAD_WALL 0x10 +#define SCENE_LOAD_ALL_GAME (SCENE_LOAD_PLAYER | SCENE_LOAD_THROWER | \ +SCENE_LOAD_WALL | SCENE_LOAD_MESSAGE_GAME) + +#define NOT_SAVE_WINDOW 0 +#define SAVE_WINDOW 1 + +#include "lib/s_sfml.h" +void map_rendering(sfml_t *sfml); +void set_exit(sfml_t *sfml); +void get_key(sfml_t *sfml); + +#endif diff --git a/include/game/draw.h b/include/game/draw.h new file mode 100644 index 0000000..f5cfb37 --- /dev/null +++ b/include/game/draw.h @@ -0,0 +1,24 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ + +#ifndef __DRAW_H__ +# define __DRAW_H__ + +#include "lib/s_sfml.h" +void player_rendering(sfml_t *sfml); +void wall_rendering(sfml_t *sfml); +void draw_monochrome(sfml_t *sfml, const uint8_t *bmp, int draw[4]); +void draw_princess(sfml_t *sfml, int x, int y); +void draw_pik(sfml_t *sfml, int x, int y, uint8_t direction); +void draw_thrower(sfml_t *sfml, int x, int y, uint8_t direction); +void draw_ground(sfml_t *sfml, int x, int y, uint8_t grass_id); +void set_background(sfml_t *sfml); + +void draw_thrower_object(sfml_t *sfml); +void draw_endtext_bmp(sfml_t *sfml); +void draw_logo_bmp(sfml_t *sfml, int y); +#endif diff --git a/include/game/end_data.h b/include/game/end_data.h new file mode 100644 index 0000000..26ef4f6 --- /dev/null +++ b/include/game/end_data.h @@ -0,0 +1,31 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ + +#ifndef __END_DATA_H__ +# define __END_DATA_H__ + +static const char *dialog[] ={ + "Hellooo !", + "Why are you following\nme like this, sir ?", + "You're ugly, your head is\nshaped like a square.", + "Sorry, my heart goes\nto circle heads !", + " ... ", + "Hellooo !", + "Be my wife !", + " ... ", + "Yeah, ok. Wathever." +}; + +static uint32_t dialog_pos_x[] ={ + 736, 768, 504, 760, 724, 736, 678, 914, 770 +}; + +static uint32_t dialog_pos_y[] = { + 900, 860, 860, 860, 900, 900, 900, 900, 900 +}; + +#endif diff --git a/include/game/endtext_bmp.h b/include/game/endtext_bmp.h new file mode 100644 index 0000000..09d12ff --- /dev/null +++ b/include/game/endtext_bmp.h @@ -0,0 +1,38 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ + +#ifndef __BMP_THE_END_H__ +# define __BMP_THE_END_H__ + +#include "lib/my_stddef.h" +#define BMP_END_WIDTH 16 +#define BMP_END_HEIGHT 18 +static const uint8_t endtext_bmp[] = { + 255, 255, 255, 255, 255, 255, 255, 255, 254, 0, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 254, 0, 127, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 252, 239, 255, 255, 254, 0, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 192, 71, 255, 255, + 254, 63, 255, 255, 207, 255, 255, 255, 255, 255, 255, 252, 0, 71, 255, + 255, 254, 63, 255, 255, 207, 255, 255, 255, 255, 255, 255, 240, 1, + 231, 254, 31, 254, 63, 255, 255, 207, 255, 255, 255, 255, 255, 255, + 240, 15, 230, 60, 15, 254, 1, 137, 255, 159, 255, 255, 255, 255, 255, + 255, 249, 143, 224, 24, 7, 254, 0, 128, 240, 159, 255, 255, 255, 255, + 255, 255, 255, 199, 224, 16, 199, 254, 0, 132, 224, 31, 255, 255, 255, + 255, 255, 255, 255, 199, 225, 17, 143, 254, 63, 140, 192, 31, 255, + 255, 255, 255, 255, 255, 255, 199, 225, 16, 31, 254, 63, 140, 199, 31, + 255, 255, 255, 255, 255, 255, 255, 199, 227, 16, 127, 254, 63, 156, + 143, 191, 255, 255, 255, 255, 255, 255, 255, 231, 243, 152, 199, 254, + 1, 156, 143, 63, 255, 255, 255, 255, 255, 255, 255, 227, 241, 152, 7, + 254, 0, 156, 134, 63, 255, 255, 255, 255, 255, 255, 255, 227, 241, + 156, 15, 255, 0, 156, 192, 63, 255, 255, 255, 255, 255, 255, 255, 227, + 241, 255, 255, 255, 255, 255, 224, 127, 255, 255, 255, 255, 255, 255, + 255, 243, 255, 255, 255, 255, 255, 255, 248, 127, 255, 255, 255, 255, + 255, 255, 255, 243, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255 +}; + +#endif diff --git a/include/game/game.h b/include/game/game.h new file mode 100644 index 0000000..1bf983c --- /dev/null +++ b/include/game/game.h @@ -0,0 +1,25 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ + +#ifndef __GAME_H__ +# define __GAME_H__ + +#include "lib/s_sfml.h" +void game_main(sfml_t *sfml, char *map_name); +void update_player(sfml_t *sfml); +void update_wall(sfml_t *sfml); +void update_thrower(sfml_t *sfml, thrower_t **thrower, level_t *level); +void collision_show(sfml_t *sfml); +void collision_pik(sfml_t *sfml); +void collision_princess(sfml_t *sfml); +void collision_player_run(scene_t *scene); +void collision_player_jump(scene_t *scene); +void collision_wall(sfml_t *sfml); +void collision_stone(sfml_t *sfml); +void game_camera_update(scene_t *scene, int width, int height); + +#endif diff --git a/include/game/help.h b/include/game/help.h new file mode 100644 index 0000000..d163700 --- /dev/null +++ b/include/game/help.h @@ -0,0 +1,13 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ + +#ifndef __HELP_H__ +# define __HELP_H__ + +void man_help(const char *bin_name); + +#endif diff --git a/include/game/kinematic.h b/include/game/kinematic.h new file mode 100644 index 0000000..bd68c18 --- /dev/null +++ b/include/game/kinematic.h @@ -0,0 +1,18 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ + +#ifndef __KINEMATIC_H__ +# define __KINEMATIC_H__ + +#include "lib/s_sfml.h" +void intro(sfml_t *sfml); +void fade(sfml_t *sfml, uint8_t action); +void end(sfml_t *sfml, int nb_dead); +void suicide(sfml_t *sfml, int nb_dead); +int text_end(sfml_t *sfml, int nb_dead); + +#endif diff --git a/include/game/logo_bmp.h b/include/game/logo_bmp.h new file mode 100644 index 0000000..aecfcb9 --- /dev/null +++ b/include/game/logo_bmp.h @@ -0,0 +1,98 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ + +#ifndef __COMPRESED_BMP_H__ +# define __COMPRESED_BMP_H__ + +#include "lib/my_stddef.h" +static const uint8_t title_bmp[] = { + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 143, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 224, 255, 255, 143, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 128, 63, 255, 135, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 0, 63, 198, 1, 195, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 254, 14, 24, 2, 1, 129, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 254, 31, 24, 3, + 3, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 252, 63, 8, 35, 142, 24, 227, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 252, 63, 140, 115, 142, 60, 224, 127, + 255, 255, 255, 255, 255, 255, 255, 255, 252, 127, 140, + 127, 142, 124, 192, 63, 255, 255, 255, 255, 255, 255, + 255, 255, 252, 127, 140, 127, 142, 120, 194, 63, 255, + 255, 255, 255, 255, 255, 255, 255, 252, 63, 140, 127, + 142, 56, 198, 127, 255, 255, 255, 255, 255, 255, 255, + 255, 254, 63, 142, 127, 142, 17, 140, 127, 255, 255, 255, + 255, 255, 255, 255, 255, 254, 63, 30, 127, 143, 3, 140, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 31, + 30, 127, 255, 135, 153, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 14, 31, 255, 255, 255, 153, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 128, 63, + 255, 255, 255, 251, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 192, 127, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 241, 255, 254, + 127, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 254, 63, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 252, 63, + 255, 243, 255, 247, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 253, 60, 191, 243, 255, 247, 252, 63, 255, + 255, 255, 255, 255, 255, 255, 255, 253, 188, 31, 247, + 255, 243, 248, 159, 255, 255, 255, 255, 255, 255, 255, + 255, 249, 188, 92, 7, 252, 243, 57, 191, 255, 255, 255, + 255, 255, 255, 255, 255, 248, 28, 217, 135, 252, 114, 27, + 63, 255, 255, 255, 255, 255, 255, 255, 255, 248, 28, 219, + 207, 248, 48, 152, 127, 255, 255, 255, 255, 255, 255, 255, + 255, 241, 157, 219, 207, 248, 57, 200, 207, 255, 255, 255, + 255, 255, 255, 255, 255, 243, 205, 155, 207, 252, 249, 204, + 31, 255, 255, 255, 255, 255, 255, 255, 255, 243, 253, 153, + 143, 252, 249, 206, 127, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 252, 31, 252, 249, 231, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 159, 252, 249, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 252, 249, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 227, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 192, 255, 255, 255, 255, 255, 255, 255, + 255, 15, 255, 255, 255, 255, 255, 255, 192, 127, 255, 255, + 255, 255, 255, 255, 254, 7, 255, 255, 255, 255, 255, 255, + 204, 63, 254, 127, 255, 255, 255, 255, 252, 39, 255, 255, + 255, 255, 255, 255, 142, 63, 252, 63, 255, 255, 255, 254, 56, + 255, 255, 255, 255, 255, 255, 255, 143, 63, 254, 127, 255, + 255, 255, 248, 24, 255, 255, 255, 255, 255, 255, 255, 143, 63, + 255, 255, 255, 255, 240, 240, 28, 3, 255, 255, 255, 255, 255, 255, + 4, 57, 255, 255, 255, 255, 192, 115, 255, 225, 255, 255, 255, 255, + 255, 255, 0, 120, 15, 249, 159, 7, 128, 113, 255, 241, 255, 255, + 255, 255, 255, 254, 0, 240, 6, 113, 14, 3, 140, 120, 30, 67, 255, + 255, 255, 255, 255, 254, 63, 240, 70, 112, 14, 51, 24, 255, 142, + 3, 255, 255, 255, 255, 255, 254, 63, 241, 198, 112, 204, 127, 1, + 255, 207, 15, 255, 255, 255, 255, 255, 252, 63, 241, 238, 112, 204, + 127, 7, 251, 15, 255, 255, 255, 255, 255, 255, 252, 127, 227, 252, + 113, 204, 255, 31, 56, 15, 255, 255, 255, 255, 255, 255, 254, 127, + 227, 252, 113, 204, 255, 12, 60, 63, 255, 255, 255, 255, 255, 255, + 255, 255, 227, 252, 113, 204, 49, 128, 127, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 227, 252, 113, 206, 1, 192, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 252, 113, 207, 7, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 249, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 253, 223, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 241, 191, 255, 237, 223, + 252, 230, 207, 255, 255, 255, 255, 255, 255, 255, 255, 246, 243, 119, + 101, 223, 255, 84, 175, 255, 255, 255, 255, 255, 255, 255, 255, 241, + 162, 170, 173, 223, 30, 214, 175, 255, 255, 255, 255, 255, 255, 255, + 255, 247, 174, 238, 173, 223, 253, 214, 175, 255, 255, 255, 255, 255, + 255, 255, 255, 247, 178, 239, 116, 71, 252, 78, 159, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255 +}; + +#endif diff --git a/include/game/memory.h b/include/game/memory.h new file mode 100644 index 0000000..00ad9f4 --- /dev/null +++ b/include/game/memory.h @@ -0,0 +1,24 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ + +#ifndef __MEMORY__ +# define __MEMORY__ + +#include "lib/s_sfml.h" +int check_map(scene_t *scene); +void free_scene(scene_t *scene); +camera_t *init_camera(scene_t *scene); +level_t *load_map(const char *file); +wall_t *init_wall(player_t *player); +player_t *init_player(level_t *level); +thrower_t **init_thrower(level_t *level); +object_t *init_object(thrower_t *thrower); +message_t **load_message(scene_t *scene, uint8_t type); +scene_t *new_scene(char *map_name, uint8_t object); +sound_t *init_music(char *music_name); + +#endif diff --git a/include/game/menu.h b/include/game/menu.h new file mode 100644 index 0000000..325c812 --- /dev/null +++ b/include/game/menu.h @@ -0,0 +1,17 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ + +#ifndef __MENU_H__ +# define __MENU_H__ + +#include "lib/s_sfml.h" +int check_pause(sfml_t *sfml); +int menu_pause(sfml_t *sfml); +int menu_start(sfml_t *sfml, int *y); +int menu_settings(sfml_t *sfml); + +#endif diff --git a/include/game/message.h b/include/game/message.h new file mode 100644 index 0000000..1143e2d --- /dev/null +++ b/include/game/message.h @@ -0,0 +1,14 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ + +#ifndef __MESSAGE_H__ +# define __MESSAGE_H__ + +#include "lib/s_sfml.h" +void check_message(sfml_t *sfml, message_t **message); + +#endif diff --git a/include/game/message_data.h b/include/game/message_data.h new file mode 100644 index 0000000..ae30fc2 --- /dev/null +++ b/include/game/message_data.h @@ -0,0 +1,49 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ + +#ifndef __MESAGE_DATA_H__ +# define __MESAGE_DATA_H__ + +#include "game/core.h" +static const char *default_message[NB_MESSAGE] = { + "Use the arrow keys to move.", + "Press SPACE or UP to jump\nlike a plumber.", + "Yay ! Now, go kiss\nthat princess !", + "In this game you can\nwalk through spikes\nwithout getting killed !", + "I lie.", + "And... you failed.", + "Come on...\nThis is not that difficult.", + "This is fun, isn't it?", + "That was fun.\nDo it again, please !", + "Three tries ? Really ?", + "You shouldn't drink while\nplaying this game, you know...", + "Don't you have a life ?", + "Nah, I won't say a thing.\nI'll juste watch.", + "Some walls aren't what\nthey appear to be...", + "Oops, I forgot to tell you\nabout the traps...", + "This was a fake princess !\nYou've been fooled, kiddo.", + "That was the stupidest\nway you could die.", + "Are you sure this\nis the right way ?", + "Run rabbit, run !", + "Yep, definitely not\nthe right way." +}; + +static const uint8_t default_action[NB_MESSAGE] = { + MESSAGE_BEGIN, MESSAGE_X_TRIGGER, MESSAGE_X_TRIGGER, MESSAGE_BEGIN, + MESSAGE_DEAD, MESSAGE_DEAD, MESSAGE_DEAD, MESSAGE_DEAD, + MESSAGE_DEAD, MESSAGE_DEAD, MESSAGE_DEAD, MESSAGE_DEAD, + MESSAGE_DEAD, MESSAGE_FAKE_GROUND, MESSAGE_DEAD, + MESSAGE_FAKE_PRINCESS, MESSAGE_DEAD, MESSAGE_DEAD, + MESSAGE_DEAD, MESSAGE_DEAD +}; + +static const uint16_t default_trigger_start[NB_MESSAGE] = { + 0, 740, 1408, 0, 1, 12, 8, 15, 10, 3, 20, 50, + 60, 22, 5, 45, 30, 40, 32, 42 +}; + +#endif diff --git a/include/game/s_game.h b/include/game/s_game.h new file mode 100644 index 0000000..b5c5ac4 --- /dev/null +++ b/include/game/s_game.h @@ -0,0 +1,81 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ + +#ifndef __S_GAME_H__ +# define __S_GAME_H__ + +#include "lib/my_stddef.h" +typedef struct player_s +{ + intmax_t x; + intmax_t y; + intmax_t start_x; + intmax_t start_y; + int8_t dx; + int8_t dy; + uint16_t key; + uint8_t action; + int counter_jump; + int frame_counter; + int dead_counter; + int nb_dead; +} player_t; + +typedef struct object_s +{ + int sleep; + int dx; + int dy; + int x; + int y; +} object_t; + +typedef struct thrower_s +{ + intmax_t x; + intmax_t y; + uint8_t action; + object_t **object; +} thrower_t; + +typedef struct wall_s +{ + intmax_t x; + intmax_t start_x; + intmax_t start_y; +} wall_t; + +typedef struct camera_s +{ + intmax_t x; + intmax_t y; +} camera_t; + +typedef struct message_s +{ + int x; + int y; + uint8_t action; + int trigger_data; + void *message; + uint32_t timer_after; + uint32_t timer_message; +} message_t; + +typedef struct level_s +{ + uint8_t *map; + int file_width; + int file_height; + int height; + int width; + int thrower_counter; + int timer_stone; + int timer_bullet; +} level_t; + +#endif diff --git a/include/lib/my_graphics.h b/include/lib/my_graphics.h new file mode 100644 index 0000000..fbf759f --- /dev/null +++ b/include/lib/my_graphics.h @@ -0,0 +1,29 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ + +#ifndef __MY_GRAPHICS_H__ +# define __MY_GRAPHICS_H__ + +#include "s_sfml.h" +void my_display_vram(sfml_t *sfml); +void my_clear_vram(sfml_t *sfml, uint32_t color); +void my_line(sfml_t *sfml, int line[4], uint32_t color); +void my_pixel(sfml_t *sfml, int x, int y, uint32_t color); +void my_horizontal_line(sfml_t *sfml, int h_line[3], uint32_t color); +void my_filled_rectangle(sfml_t *sfml, int rectangle[4], uint32_t color); +void my_vram_zoom(sfml_t *sfml, double zoom_width, double zoom_height); +void my_filled_circle(sfml_t *sfml, int circle[3], uint32_t color); +void my_filled_polygon(sfml_t *sfml, polygon_t polygon); +void copy_vram(sfml_t *sfml, uint32_t *src); +void my_draw_bmp(sfml_t *sfml, sprite_t *sprite, intmax_t x, intmax_t y); +sprite_t *my_load_sprite(const char *file); +void my_draw_sheet(sfml_t *sfml, sprite_t *sprite, int sheet[6]); +void my_rectangle(sfml_t *sfml, int rect[5], +uint32_t border_color, uint32_t fill_color); + +void get_drawline(polygon_t *polygon, int *xmax, int *xmin, char *empty); +#endif diff --git a/include/lib/my_memory.h b/include/lib/my_memory.h new file mode 100644 index 0000000..a677643 --- /dev/null +++ b/include/lib/my_memory.h @@ -0,0 +1,19 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ + +#ifndef __MY_MEMORY_H__ +# define __MY_MEMORY_H__ + +#ifndef FRAME_RATE +# define FRAME_RATE 64 +#endif + +#include "lib/s_sfml.h" +sfml_t *init_sfml(int width, int height); +void free_sfml(sfml_t *sfml); + +#endif diff --git a/include/lib/my_stddef.h b/include/lib/my_stddef.h new file mode 100644 index 0000000..fe2f45f --- /dev/null +++ b/include/lib/my_stddef.h @@ -0,0 +1,118 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ + +#ifndef __MY_STDDEF_H__ +# define __MY_STDDEF_H__ + +#ifndef __WORDSIZE +# if defined __x86_x64__ && !defined __ILP32__ +# define __WORDSIZE 64 +# else +# define __WORDSIZE 32 +# define __WORDSIZE32_SIZE_ULONG 0 +# define __WORDSIZE32_PTRDIFF_ULONG 0 +# endif +#endif + +#ifndef NULL +# define NULL (void*)0 +#endif + +#define MY_CHAR_BITS 8 +#define MY_UCHAR_MIN 0 +#define MY_UCHAR_MAX 0xff +#define MY_SCHAR_MIN (-0x7f - 1) +#define MY_SCHAR_MAX 0x7f + +#define MY_SHORT_BITS 16 +#define MY_USHORT_MIN 0 +#define MY_USHORT_MAX 0xffff +#define MY_SHORT_MIN (-0x7fff - 1) +#define MY_SHORT_MAX 0x7fff + +#define MY_INT_BITS 32 +#define MY_UINT_MIN 0 +#define MY_UINT_MAX 0xffffffff +#define MY_INT_MIN (-0x7fffffff - 1) +#define MY_INT_MAX 0x7fffffff + +#if __WORDSIZE == 64 +#define MY_LONG_BITS 64 +#define MY_ULONG_MIN 0 +#define MY_ULONG_MAX 0xffffffffffffffff +#define MY_LONG_MIN (-0x7fffffffffffffff - 1) +#define MY_LONG_MAX 0x7fffffffffffffff +#define MY_LLONG_BITS 64 +#define MY_ULLONG_MIN 0 +#define MY_ULLONG_MAX 0xffffffffffffffff +#define MY_LLONG_MIN (-0x7fffffffffffffff - 1) +#define MY_LLONG_MAX 0x7fffffffffffffff +#define MY_PTRDIFF_BITS 64 +#define MY_PTRDIFF_MIN 0 +#define MY_PRTDIFF_MAX 0xffffffffffffffff +#else +#define MY_LONG_BITS 32 +#define MY_ULONG_MIN 0 +#define MY_ULONG_MAX 0xffffffff +#define MY_LONG_MIN (-0x7fffffff - 1) +#define MY_LONG_MAX 0x7fffffff +#define MY_LLONG_BITS 32 +#define MY_ULLONG_MIN 0 +#define MY_ULLONG_MAX 0xffffffff +#define MY_LLONG_MIN (-0x7fffffff - 1) +#define MY_LLONG_MAX 0x7fffffff +#define MY_PTRDIFF_BITS 32 +#define MY_PTRDIFF_MIN 0 +#define MY_PTRDIFF_MAX 0xffffffff +#endif + + +#ifndef _BITS_STDINT_INTN_H +# define _BITS_STDINT_INTN_H +#include +typedef signed char int8_t; +typedef unsigned char uint8_t; +typedef signed short int16_t; +typedef unsigned short uint16_t; +typedef signed int int32_t; +typedef unsigned int uint32_t; +# if __WORDSIZE == 64 +typedef signed long long int int64_t; +typedef unsigned long long int uint64_t; +# else +__extension__ typedef signed long long int int64_t; +__extension__ typedef unsigned long long int uint64_t; +# endif +#endif + +#ifndef _STDINT_H +typedef signed long long int intmax_t; +typedef unsigned long long int uintmax_t; +#endif + +#ifndef __PTRDIFF_T +# define __PTRDIFF_T +typedef unsigned long ptrdiff_t; +#endif + +#ifndef __FILE_defined +# define _FILE_defined 1 +struct _IO_FILE; +typedef struct _IO_FILE FILE; +#endif + +#ifndef __SIZE_T +# define __SIZE_T +typedef unsigned int size_t; +#endif + +#ifndef __ssize_t_defined +# define __ssize_t_defined +typedef signed int ssize_t; +#endif + +#endif diff --git a/include/lib/my_stdio.h b/include/lib/my_stdio.h new file mode 100644 index 0000000..b3f408d --- /dev/null +++ b/include/lib/my_stdio.h @@ -0,0 +1,54 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ + +#ifndef __MY_STDIO_H__ +# define __MY_STDIO_H__ + +#include +#include "lib/my_stddef.h" + +#define PRINT_MODE_RESET 0x00 +#define PRINT_MODE_DIEZ 0x01 +#define PRINT_MODE_PLUS 0x02 +#define PRINT_MODE_MINUS 0x04 +#define PRINT_MODE_SPACE 0x08 +#define PRINT_MODE_MAJ 0x10 +#define PRINT_MODE_HEXA 0x20 + +#define PRINT_TYPE_RESET 0x00 +#define PRINT_TYPE_RESET 0x00 +#define PRINT_TYPE_LLONG 0x01 +#define PRINT_TYPE_LONG 0x02 +#define PRINT_TYPE_CHAR 0x04 +#define PRINT_TYPE_PTRD 0x08 +#define PRINT_TYPE_INTM 0x10 +#define PRINT_TYPE_SIZE 0x20 +#define PRINT_TYPE_SINT 0x40 + +typedef struct printf_s +{ + uint8_t mode_flags; + uintmax_t value; + int precision; + int str_width; + int nbr_width; + uint8_t base; + uint8_t type; + int cursor; + va_list ap; + int stream; + char sign; +} printf_t; + +int my_putchar(int n); +void my_printf(char const *format, ...); +void my_dprintf(int fd, const char *format, ...); +void my_fprintf(FILE *stream, const char *format, ...); +void my_vdprintf(int fd, const char *format, va_list ap); +void my_vfprintf(int _stdout, char const *format, va_list ap); + +#endif /*MY_STDIO*/ diff --git a/include/lib/my_stdlib.h b/include/lib/my_stdlib.h new file mode 100644 index 0000000..7dada54 --- /dev/null +++ b/include/lib/my_stdlib.h @@ -0,0 +1,24 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ + +#ifndef __MY_STDLIB_H__ +# define __MY_STDLIB_H__ + +#include "lib/my_stddef.h" +int my_abs(int j); +long int my_labs(long int j); +int my_atoi(const char *nptr); +long my_atol(const char *nptr); +int my_strnb_i(const char **str); +long long my_atoll(const char *nptr); +long int my_strnb_l(const char **str); +long long int my_llabs(long long int j); +long long int my_strnb_ll(const char **str); +long int my_strtol(char *nptr, char **endptr, int base); +long long int my_strtoll(char *nptr, char **endptr, int base); + +#endif diff --git a/include/lib/my_string.h b/include/lib/my_string.h new file mode 100644 index 0000000..6cc2dee --- /dev/null +++ b/include/lib/my_string.h @@ -0,0 +1,29 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ + +#ifndef __MY_STRING_H__ +# define __MY_STRING_H__ + +#include "lib/my_stddef.h" +char *my_strdup(char const *s); +size_t my_strlen(const char *s); +char *my_strdupa(char const *s); +char *my_strndup(char const *s, size_t n); +char *my_strndupa(char const *s, size_t n); +char *my_strcpy(char *dest, char const *src); +char *my_strcat(char *dest, char const *src); +int my_strcmp(const char *s1, const char *s2); +int my_strcoll(const char *s1, const char *s2); +size_t my_strnlen(const char *s, size_t maxlen); +int my_strcoll_l(const char *s1, const char *s2); +char *my_strncpy(char *dest, char const *src, size_t n); +char *my_strncat(char *dest, char const *src, size_t n); +int my_strncmp(const char *s1, const char *s2, size_t n); +char *my_strstr(const char *haystack, const char *needle); +char *my_strcasestr(const char *haystack, const char *needle); + +#endif /* MY_STRING */ diff --git a/include/lib/s_sfml.h b/include/lib/s_sfml.h new file mode 100644 index 0000000..46ccea4 --- /dev/null +++ b/include/lib/s_sfml.h @@ -0,0 +1,74 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ + +#ifndef __S_SFML_H__ +# define __S_SFML_H__ + +#include +#include +#include +#include +#include +#include +#include "game/s_game.h" +#include "lib/my_stddef.h" + +typedef struct sprite_s +{ + int width; + int height; + uint8_t *data; +} sprite_t; + +typedef struct polygon_s +{ + int nb_vertices; + uint32_t color; + int *x; + int *y; + int ymin; + int ymax; + int xmax; + int xmin; + double z_buffer; +} polygon_t; + +typedef struct scene_s +{ + int nb_message; + uint8_t message_active; + wall_t *wall; + thrower_t **thrower; + message_t **message; + player_t *player; + camera_t *camera; + level_t *level; +} scene_t; + +typedef struct sound_s +{ + uint8_t active; + uint8_t volume; + sfMusic *music; +} sound_t; + +typedef struct sfml_s +{ + sfRenderWindow *window; + sfSprite *vram_sprite; + sfTexture *texture; + sfEvent event; + int width; + int height; + uint32_t *vram; + uint32_t key; + uint8_t debug; + scene_t *scene; + sound_t *sound; +} sfml_t; + +#endif diff --git a/lib/Makefile b/lib/Makefile new file mode 100644 index 0000000..f4e212f --- /dev/null +++ b/lib/Makefile @@ -0,0 +1,52 @@ +#!/usr/bin/make -f +# --- +# Project: my_libc +# Author: yann.magnin@epitech.eu +# --- +include ../global.mk + + + +build-dir = ../build +header = -I../include +target-module = my_stdio my_stdlib my_string my_csfml + +$(foreach mod, $(target-module), $(eval \ + target-$(mod)-src = $(wildcard $(mod)/*.c)$n\ + target-$(mod)-obj = $$(patsubst %.c,$(build-dir)/lib_%.o, $$(subst \ + $(mod)/,l$(subst _,,$(mod))_,$$(target-$(mod)-src))) $n\ + target-$(mod)-out = $(patsubst %,lib%.a,$(subst _,,$(mod)))\ +)) +target-lib = $(foreach mod, $(target-module), $(target-$(mod)-out)) + + +define rule-target +$(patsubst %.c,$(build-dir)/lib_%.o,\ +$(subst $2/,l$(subst _,,$2)_,$1)): $1 | $(build-dir) + $$(cc) $(cflags) -c $$< -o $$@ $(header) +endef + +define rule-link +$(patsubst %,lib%.a,$(subst _,,$1)): $2 + @ printf 'link lib \033[1;35m$$@\033[0m\n' + $(ar) crs $$@ $$^ +endef + +all: $(target-lib) +$(build-dir): + @ printf 'create build folder:\033[1;35m $@\033[0m\n' + mkdir -p $@ + + +$(foreach mod,$(target-module), \ + $(foreach source,$(target-$(mod)-src), $(eval \ + $(call rule-target,$(source),$(mod))))\ + $(eval $(call rule-link,$(mod),$(target-$(mod)-obj)))\ +) + +clean: + rm -rf $(build-dir) +fclean: clean + rm -rf $(target-lib) +re: fclean all +.PHONY: re clean fclean all diff --git a/lib/my_csfml/my_circle.c b/lib/my_csfml/my_circle.c new file mode 100644 index 0000000..6503dfd --- /dev/null +++ b/lib/my_csfml/my_circle.c @@ -0,0 +1,66 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include "../../include/lib/s_sfml.h" +#include "../../include/lib/my_graphics.h" + +static void my_circle_line(sfml_t *sfml, +int circle[3], int var[4], uint32_t color) +{ + int h_line[3]; + + if (var[1] >= var[0]){ + h_line[0] = circle[1] + var[0]; + h_line[1] = circle[0] - var[1]; + h_line[2] = circle[0] + var[1]; + my_horizontal_line(sfml, h_line, color); + h_line[0] = circle[1] - var[0]; + my_horizontal_line(sfml, h_line, color); + } +} + +static void my_var_init(int var[], int circle[3]) +{ + var[0] = 0; + var[1] = circle[2]; +} + +static void draw_line_circle(sfml_t *sfml, +int circle[3], int var[4], uint32_t color) +{ + int h_line[3]; + + h_line[0] = circle[1] + var[1] + 1; + h_line[1] = circle[0] - var[0]; + h_line[2] = circle[0] + var[0]; + my_horizontal_line(sfml, h_line, color); + h_line[0] = circle[1] - var[1] - 1; + my_horizontal_line(sfml, h_line, color); +} + +void my_filled_circle(sfml_t *sfml, int circle[3], uint32_t color) +{ + int h_line[4] = {circle[1], circle[0] - circle[2], + circle[0] + circle[2]}; + int tmp = 1 - circle[2]; + int var[4]; + + if (circle[2] < 0) + return; + my_var_init(var, circle); + my_horizontal_line(sfml, h_line, color); + while (var[1] > var[0]){ + if (tmp < 0) + tmp += (var[0] << 1) + 3; + else { + tmp += ((var[0]-var[1]) << 1) + 5; + var[1]--; + draw_line_circle(sfml, circle, var, color); + } + var[0]++; + my_circle_line(sfml, circle, var, color); + } +} diff --git a/lib/my_csfml/my_graphics.c b/lib/my_csfml/my_graphics.c new file mode 100644 index 0000000..44f4c11 --- /dev/null +++ b/lib/my_csfml/my_graphics.c @@ -0,0 +1,57 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include "../../include/lib/s_sfml.h" +#include "../../include/lib/my_graphics.h" + +void my_clear_vram(sfml_t *sfml, uint32_t color) +{ + uint8_t red = (color >> 24) & 0xff; + uint8_t green = (color >> 16) & 0xff; + uint8_t blue = (color >> 8) & 0xff; + uint8_t alpha = color & 0xff; + uint8_t *vram = (uint8_t*)sfml->vram; + int i; + + i = -1; + while (++i < sfml->width * sfml->height){ + vram[i << 2] = red; + vram[(i << 2) + 1] = green; + vram[(i << 2) + 2] = blue; + vram[(i << 2) + 3] = alpha; + } +} + +void my_display_vram(sfml_t *sfml) +{ + sfTexture_updateFromPixels(sfml->texture, (sfUint8*)sfml->vram, sfml->width, + sfml->height, 0, 0); + sfSprite_setTexture(sfml->vram_sprite, sfml->texture, 0); + sfRenderWindow_drawSprite(sfml->window, sfml->vram_sprite, NULL); + sfRenderWindow_display(sfml->window); +} + +void my_pixel(sfml_t *sfml, int x, int y, uint32_t color) +{ + uint8_t *vram = (uint8_t*)sfml->vram; + if (x < 0 || x >= sfml->width || y < 0 || y >= sfml->height) + return; + vram[(y * sfml->width + x) << 2] = (color >> 24) & 0xff; + vram[((y * sfml->width + x) << 2) + 1] = (color >> 16) & 0xff; + vram[((y * sfml->width + x) << 2) + 2] = (color >> 8) & 0xff; + vram[((y * sfml->width + x) << 2) + 3] = color & 0xff; +} + +void my_horizontal_line(sfml_t *sfml, int h_line[3], uint32_t color) +{ + int start; + int end; + + end = (h_line[1] < h_line[2]) ? h_line[2] : h_line[1]; + start = (h_line[1] < h_line[2]) ? h_line[1] - 1 : h_line[2] - 1; + while (++start < end) + my_pixel(sfml, start, h_line[0], color); +} diff --git a/lib/my_csfml/my_line.c b/lib/my_csfml/my_line.c new file mode 100644 index 0000000..fbeae39 --- /dev/null +++ b/lib/my_csfml/my_line.c @@ -0,0 +1,63 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include "../../include/lib/s_sfml.h" +#include "../../include/lib/my_graphics.h" + +static void my_line_dx(sfml_t *sfml, int var[7], uint32_t color) +{ + int cumul; + int i; + + i = 0; + cumul = var[2] / 2; + while (++i < var[2]){ + var[0] += var[4]; + cumul += var[3]; + if (cumul > var[2]){ + cumul -= var[2]; + var[1] += var[5]; + } + my_pixel(sfml, var[0], var[1], color); + } +} + +static void my_line_dy(sfml_t *sfml, int var[7], uint32_t color) +{ + int cumul; + int i; + + i = 0; + cumul = var[3] / 2; + while (++i < var[3]){ + var[1] += var[5]; + cumul += var[2]; + if (cumul > var[3]){ + cumul -= var[3]; + var[0] += var[4]; + } + my_pixel(sfml, var[0], var[1], color); + } +} + +void my_line(sfml_t *sfml, int line[4], uint32_t color) +{ + int var[6]; + + var[0] = line[0]; + var[1] = line[1]; + var[2] = line[2] - line[0]; + var[3] = line[3] - line[1]; + var[4] = (var[2] > 0) ? 1 : -1; + var[5] = (var[3] > 0) ? 1 : -1; + var[2] = (var[2] > 0) ? var[2] : -var[2]; + var[3] = (var[3] > 0) ? var[3] : -var[3]; + my_pixel(sfml, var[0], var[1], color); + if (var[2] > var[3]) + my_line_dx(sfml, var, color); + else + my_line_dy(sfml, var, color); +} diff --git a/lib/my_csfml/my_memory.c b/lib/my_csfml/my_memory.c new file mode 100644 index 0000000..68aaecf --- /dev/null +++ b/lib/my_csfml/my_memory.c @@ -0,0 +1,43 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include +#include "lib/s_sfml.h" +#include "lib/my_memory.h" +#include "game/memory.h" + +sfml_t *init_sfml(int width, int height) +{ + sfml_t *sfml; + sfVideoMode mode = {width, height, 32}; + + sfml = (sfml_t*)malloc(sizeof(sfml_t)); + if (sfml == NULL) + return (NULL); + sfml->width = width; + sfml->height = height; + sfml->window = sfRenderWindow_create(mode, "runner", + sfFullscreen | sfClose, NULL); + sfRenderWindow_setMouseCursorVisible(sfml->window, 0); + sfRenderWindow_setFramerateLimit(sfml->window, FRAME_RATE); + sfml->vram = (uint32_t*)malloc(sizeof(uint32_t) * (width * height)); + sfml->texture = sfTexture_create(width, height); + sfml->vram_sprite = sfSprite_create(); + sfml->sound = init_music("music/sikmu.ogg"); + sfml->debug = 0x00; + sfml->key = 0x0000; + return (sfml); +} + +void free_sfml(sfml_t *sfml) +{ + sfRenderWindow_destroy(sfml->window); + sfTexture_destroy(sfml->texture); + sfSprite_destroy(sfml->vram_sprite); + sfMusic_destroy(sfml->sound->music); + free(sfml->vram); + free(sfml); +} diff --git a/lib/my_csfml/my_polygon.c b/lib/my_csfml/my_polygon.c new file mode 100644 index 0000000..74b9c5c --- /dev/null +++ b/lib/my_csfml/my_polygon.c @@ -0,0 +1,60 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include +#include "../../include/lib/s_sfml.h" +#include "../../include/lib/my_graphics.h" + +static void draw_filled_polygon(sfml_t *sfml, +polygon_t *polygon, int *xmax, int *xmin) +{ + int h_line[3]; + int i; + + i = -1; + while (++i < polygon->ymax - polygon->ymin + 1){ + h_line[0] = polygon->ymin + i; + h_line[1] = xmin[i]; + h_line[2] = xmax[i]; + my_horizontal_line(sfml, h_line, polygon->color); + } +} + +static void polygon_get_info(polygon_t *polygon) +{ + int i; + + i = 0; + polygon->ymin = polygon->y[0]; + polygon->ymax = polygon->y[0]; + while (++i < polygon->nb_vertices){ + if (polygon->y[i] < polygon->ymin) + polygon->ymin = polygon->y[i]; + if (polygon->y[i] > polygon->ymax) + polygon->ymax = polygon->y[i]; + } +} + +void my_filled_polygon(sfml_t *sfml, polygon_t polygon) +{ + char *empty; + int *xmin; + int *xmax; + int i; + + i = -1; + polygon_get_info(&polygon); + xmin = (int*)malloc(sizeof(int) * (polygon.ymax - polygon.ymin + 1)); + xmax = (int*)malloc(sizeof(int) * (polygon.ymax - polygon.ymin + 1)); + empty = (char*)malloc(polygon.ymax - polygon.ymin + 1); + while (++i < polygon.ymax - polygon.ymin + 1) + empty[i] = 1; + get_drawline(&polygon, xmin, xmax, empty); + draw_filled_polygon(sfml, &polygon, xmax, xmin); + free(empty); + free(xmin); + free(xmax); +} diff --git a/lib/my_csfml/my_polygon_getline.c b/lib/my_csfml/my_polygon_getline.c new file mode 100644 index 0000000..7b5059f --- /dev/null +++ b/lib/my_csfml/my_polygon_getline.c @@ -0,0 +1,92 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include "../../include/lib/s_sfml.h" +#include "../../include/lib/my_graphics.h" + + +static void set_line_dy(polygon_t *polygon, void *ptr[], int var[6]) +{ + char *empty = (char*)ptr[2]; + int *xmin = (int*)ptr[1]; + int *xmax = (int*)ptr[0]; + int cumul; + int i; + + i = 0; + cumul = var[3] >> 1; + while (++i < var[3]){ + var[1] += var[5]; + cumul += var[2]; + if (cumul > var[3]){ + cumul -= var[3]; + var[0] += var[4]; + } + xmax[var[1] - polygon->ymin] = var[0]; + if (empty[var[1] - polygon->ymin]){ + xmin[var[1] - polygon->ymin] = var[0]; + empty[var[1] - polygon->ymin] = 0; + } + } +} + +static void set_line_dx(polygon_t *polygon, void *ptr[], int var[6]) +{ + char *empty = (char*)ptr[2]; + int *xmin = (int*)ptr[1]; + int *xmax = (int*)ptr[0]; + int cumul; + int i; + + i = 0; + cumul = var[2] >> 1; + while (++i < var[2]){ + var[0] += var[4]; + cumul += var[3]; + if (cumul > var[2]){ + cumul -= var[2]; + var[1] += var[5]; + } + xmax[var[1] - polygon->ymin] = var[0]; + if (empty[var[1] - polygon->ymin]){ + xmin[var[1] - polygon->ymin] = var[0]; + empty[var[1] - polygon->ymin] = 0; + } + } +} + +static void polygon_init_var(polygon_t *polygon, int var[6], int i) +{ + var[0] = polygon->x[i]; + var[1] = polygon->y[i]; + var[2] = polygon->x[(i + 1) % polygon->nb_vertices] - var[0]; + var[3] = polygon->y[(i + 1) % polygon->nb_vertices] - var[1]; + var[4] = (var[2] > 0) ? 1 : -1; + var[5] = (var[3] > 0) ? 1 : -1; + var[2] = (var[2] > 0) ? var[2] : -var[2]; + var[3] = (var[3] > 0) ? var[3] : -var[3]; +} + +void get_drawline(polygon_t *polygon, int *xmax, int *xmin, char *empty) +{ + void *ptr[]= {(void*)xmax, (void*)xmin, (void*)empty}; + int var[6]; + int i; + + i = -1; + while (++i < polygon->nb_vertices){ + polygon_init_var(polygon, var, i); + xmax[var[1] - polygon->ymin] = var[0]; + if (empty[var[1] - polygon->ymin]){ + xmin[var[1] - polygon->ymin] = var[0]; + empty[var[1] - polygon->ymin] = 0; + } + if (var[2] > var[3]) + set_line_dx(polygon, ptr, var); + else + set_line_dy(polygon, ptr, var); + } +} diff --git a/lib/my_csfml/my_rectangle.c b/lib/my_csfml/my_rectangle.c new file mode 100644 index 0000000..cd50ef7 --- /dev/null +++ b/lib/my_csfml/my_rectangle.c @@ -0,0 +1,92 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include "../include/lib/s_sfml.h" +#include "../include/lib/my_graphics.h" +#include "../include/lib/my_stdlib.h" + +static int check_position(sfml_t *sfml, int rectangle[]) +{ + if ((rectangle[0] < 0 && rectangle[2] < 0) || + ((rectangle[0] > sfml->width) && (rectangle[2] > sfml->width)) + || (rectangle[1] < 0 && rectangle[3] < 0) + || ((rectangle[1] > sfml->height) + && (rectangle[3] > sfml->height))) + return (1); + return (0); +} + +void my_filled_rectangle(sfml_t *sfml, int rectangle[4], uint32_t color) +{ + int h_line[3]; + int var[4]; + int i; + + if (check_position(sfml, rectangle)) + return; + i = -1; + var[0] = (rectangle[2] < rectangle[0]) ? rectangle[2] : rectangle[0]; + var[0] = (var[0] < 0) ? 0 : var[0]; + var[1] = (rectangle[3] < rectangle[1]) ? rectangle[3] : rectangle[1]; + var[1] = (var[1] < 0) ? 0 : var[1]; + var[2] = (rectangle[2] < rectangle[0]) ? rectangle[0] : rectangle[2]; + var[2] = (var[2] > sfml->width) ? sfml->width : var[2]; + var[3] = my_abs(rectangle[3] - rectangle[1]); + var[3] = (var[3] > sfml->height) ? sfml->height : var[3]; + while (++i < var[3]){ + h_line[0] = var[1] + i; + h_line[1] = var[0]; + h_line[2] = var[2]; + my_horizontal_line(sfml, h_line, color); + } +} + +static void set_h_line(int h_line[3], int y, int x1, int x2) +{ + h_line[0] = y; + h_line[1] = x1; + h_line[2] = x2; +} + +static void draw_border(sfml_t *sfml, int rect[5], uint32_t border_color) +{ + int h_line[3]; + int i; + + i = -1; + while (++i < rect[4] - 1){ + set_h_line(h_line, rect[1] + i, rect[0], rect[2]); + my_horizontal_line(sfml, h_line, border_color); + set_h_line(h_line, rect[3] - i, rect[0], rect[2]); + my_horizontal_line(sfml, h_line, border_color); + } + i = rect[1] + rect[4] - 2; + while (++i <= rect[3] - rect[4] + 1){ + set_h_line(h_line, i, rect[0], rect[0] + rect[4] - 1); + my_horizontal_line(sfml, h_line, border_color); + set_h_line(h_line, i, rect[2] - rect[4] + 1, rect[2]); + my_horizontal_line(sfml, h_line, border_color); + } +} + +void my_rectangle(sfml_t *sfml, int rect[5], +uint32_t border_color, uint32_t fill_color) +{ + int h_line[3]; + int i; + + if (check_position(sfml, rect)) + return; + if (border_color & 0xff && rect[4] > 0) + draw_border(sfml, rect, border_color); + if (fill_color & 0xff){ + i = rect[1] + rect[4] - 1; + while (++i <= rect[3] - rect[4]){ + set_h_line(h_line, i, rect[0] + rect[4], rect[2] - rect[4]); + my_horizontal_line(sfml, h_line, fill_color); + } + } +} diff --git a/lib/my_csfml/my_vram_zoom.c b/lib/my_csfml/my_vram_zoom.c new file mode 100644 index 0000000..aa334e9 --- /dev/null +++ b/lib/my_csfml/my_vram_zoom.c @@ -0,0 +1,45 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include +#include "../../include/lib/s_sfml.h" +#include "../../include/lib/my_graphics.h" + +void copy_vram(sfml_t *sfml, uint32_t *src) +{ + int i; + int end; + + i = -1; + end = sfml->height * sfml->width; + while (++i < end) + sfml->vram[i] = src[i]; +} + +void my_vram_zoom(sfml_t *sfml, double zoom_width, double zoom_height) +{ + uint32_t *new_vram; + int copy_x; + int copy_y; + int i = -1; + int j; + + zoom_width = (zoom_width < 0) ? 0 : zoom_width; + zoom_height = (zoom_height < 0) ? 0 : zoom_height; + new_vram = (uint32_t*)malloc(sizeof(uint32_t) * + sfml->width * sfml->height); + while (++i < sfml->width){ + j = -1; + copy_x = i / zoom_width + 0.5; + while (++j < sfml->height){ + copy_y = j / zoom_height + 0.5; + new_vram[i + j * sfml->width] = + sfml->vram[copy_x + copy_y * sfml->width]; + } + } + copy_vram(sfml, new_vram); + free(new_vram); +} diff --git a/lib/my_stdio/my_dprintf.c b/lib/my_stdio/my_dprintf.c new file mode 100644 index 0000000..3d6c713 --- /dev/null +++ b/lib/my_stdio/my_dprintf.c @@ -0,0 +1,17 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include "lib/my_stdio.h" + +void my_dprintf(int fd, const char *format, ...) +{ + va_list ap; + int size; + + va_start(ap, format); + my_vfprintf(fd, format, ap); + va_end(ap); +} diff --git a/lib/my_stdio/my_fprintf.c b/lib/my_stdio/my_fprintf.c new file mode 100644 index 0000000..413ef20 --- /dev/null +++ b/lib/my_stdio/my_fprintf.c @@ -0,0 +1,12 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include "lib/my_stdio.h" + +void my_fprintf(FILE *stream, const char *format, ...) +{ + return; +} diff --git a/lib/my_stdio/my_printf.c b/lib/my_stdio/my_printf.c new file mode 100644 index 0000000..833fc24 --- /dev/null +++ b/lib/my_stdio/my_printf.c @@ -0,0 +1,16 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include "lib/my_stdio.h" + +void my_printf(char const *format, ...) +{ + va_list ap; + + va_start(ap, format); + my_vfprintf(1, format, ap); + va_end(ap); +} diff --git a/lib/my_stdio/my_putchar.c b/lib/my_stdio/my_putchar.c new file mode 100644 index 0000000..c576a1b --- /dev/null +++ b/lib/my_stdio/my_putchar.c @@ -0,0 +1,14 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include +#include "lib/my_stdio.h" + +int my_putchar(int c) +{ + write(1, &c, 1); + return (c); +} diff --git a/lib/my_stdio/my_vdprintf.c b/lib/my_stdio/my_vdprintf.c new file mode 100644 index 0000000..b18bf0e --- /dev/null +++ b/lib/my_stdio/my_vdprintf.c @@ -0,0 +1,12 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include "lib/my_stdio.h" + +void my_vdprintf(int fd, const char *format, va_list ap) +{ + my_vfprintf(fd, format, ap); +} diff --git a/lib/my_stdio/my_vfprintf.c b/lib/my_stdio/my_vfprintf.c new file mode 100644 index 0000000..cb656e0 --- /dev/null +++ b/lib/my_stdio/my_vfprintf.c @@ -0,0 +1,69 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include +#include "lib/my_stdio.h" + +extern void vfprintf_get_arg(printf_t *print, char type); +extern void vfprintf_get_base(printf_t *print, char type); +extern void vfprintf_print_arg(printf_t *print, int size); +extern int vfprintf_get_size(printf_t *print, char type); +extern int vfprintf_get_ouput_type(printf_t *print, char type); +extern int vfprintf_get_type(printf_t *print, char const *format); +extern int vfprintf_get_flags(printf_t *print, char const *format); +extern int vfprintf_get_precision(printf_t *print, char const *format); + +static void vfprintf_setup_print(printf_t *print) +{ + print->type = 0; + print->base = 10; + print->value = 0; + print->cursor = 0; + print->sign = '\0'; + print->str_width = 0; + print->nbr_width = 0; + print->precision = 0; + print->type = PRINT_TYPE_RESET; + print->mode_flags = PRINT_MODE_RESET; +} + +static int vfprintf_action(printf_t *print, char const *format) +{ + int read_size; + int error; + + if (*format != '%') + return (0); + vfprintf_setup_print(print); + read_size = vfprintf_get_flags(print, format); + read_size += vfprintf_get_precision(print, format + read_size); + read_size += vfprintf_get_type(print, format + read_size); + vfprintf_get_base(print, *(format + read_size)); + error = vfprintf_get_ouput_type(print, *(format + read_size)); + if (error < 0) + return (read_size + 1); + if (error == 0) + return (0); + vfprintf_get_arg(print, *(format + read_size)); + vfprintf_get_size(print, *(format + read_size)); + vfprintf_print_arg(print, print->str_width); + return (read_size + 1); +} + +void my_vfprintf(int fd, char const *format, va_list ap) +{ + int read_flags; + printf_t print; + + print.stream = fd; + va_copy(print.ap, ap); + while (*format != '\0'){ + read_flags = vfprintf_action(&print, format); + if (!read_flags) + write(fd, format++, 1); + format += read_flags; + } +} diff --git a/lib/my_stdio/my_vprintf.c b/lib/my_stdio/my_vprintf.c new file mode 100644 index 0000000..cc3abc8 --- /dev/null +++ b/lib/my_stdio/my_vprintf.c @@ -0,0 +1,13 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include +#include "lib/my_stdio.h" + +void my_vprintf(const char *format, va_list ap) +{ + my_vfprintf(1, format, ap); +} diff --git a/lib/my_stdio/vfprintf_get_arg.c b/lib/my_stdio/vfprintf_get_arg.c new file mode 100644 index 0000000..8cf0b02 --- /dev/null +++ b/lib/my_stdio/vfprintf_get_arg.c @@ -0,0 +1,55 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include "lib/my_stdio.h" + +/* */ +/* it's a bit ugly but it's the best */ +/* maniere to get exacly what we want */ +/* */ +static void get_signed_arg(printf_t *print) +{ + if (print->type & PRINT_TYPE_CHAR) + print->value = (uintmax_t)(char)print->value; + if (!print->type) + print->value = (uintmax_t)(int)print->value; + if (print->type & PRINT_TYPE_INTM) + print->value = (uintmax_t)(intmax_t)print->value; + if (print->type & PRINT_TYPE_SINT || print->type & PRINT_TYPE_SIZE) + print->value = (uintmax_t)(short int)print->value; + if (print->type & PRINT_TYPE_LLONG) + print->value = (uintmax_t)(long long int)print->value; + if (print->type & PRINT_TYPE_LONG) + print->value = (uintmax_t)(long int)print->value; + if (print->type & PRINT_TYPE_PTRD) + print->value = (uintmax_t)(ptrdiff_t)print->value; +} + +static void get_unsigned_arg(printf_t *print) +{ + if (print->type & PRINT_TYPE_CHAR) + print->value = (uintmax_t)(unsigned char)print->value; + if (!print->type) + print->value = (uintmax_t)(unsigned int)print->value; + if (print->type & PRINT_TYPE_SINT || print->type & PRINT_TYPE_SIZE) + print->value = (uintmax_t)(unsigned short int)print->value; + if (print->type & PRINT_TYPE_LLONG) + print->value = (uintmax_t)(unsigned long long int)print->value; + if (print->type & PRINT_TYPE_LONG) + print->value = (intmax_t)(unsigned long int)print->value; + if (print->type & PRINT_TYPE_PTRD) + print->value = (uintmax_t)(ptrdiff_t)print->value; +} + +void vfprintf_get_arg(printf_t *print, char type) +{ + print->value = va_arg(print->ap, uintmax_t); + if (type == 'o' || type == 'x' || type == 'X' + || type == 'p' || type == 'b' || type == 'u') + get_unsigned_arg(print); + else + get_signed_arg(print); +} diff --git a/lib/my_stdio/vfprintf_get_option.c b/lib/my_stdio/vfprintf_get_option.c new file mode 100644 index 0000000..9cad039 --- /dev/null +++ b/lib/my_stdio/vfprintf_get_option.c @@ -0,0 +1,97 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include +#include "lib/my_stdio.h" + +int vfprintf_get_flags(printf_t *print, char const *format) +{ + int read_size; + + read_size = 0; + while (format[++read_size] == '+' || format[read_size] == '#' + || format[read_size] == ' ' || format[read_size] == '-'){ + if (format[read_size] == '#') + print->mode_flags |= PRINT_MODE_DIEZ; + if (format[read_size] == '+') + print->mode_flags |= PRINT_MODE_PLUS; + if (format[read_size] == '-') + print->mode_flags |= PRINT_MODE_MINUS; + if (format[read_size] == ' ') + print->mode_flags |= PRINT_MODE_SPACE; + } + return (read_size); +} + +int vfprintf_get_type(printf_t *print, char const *format) +{ + if (*format == 'l' && format[1] == 'l'){ + print->type |= PRINT_TYPE_LLONG; + return (2); + } + if (*format == 'h' && format[1] == 'h'){ + print->type |= PRINT_TYPE_SINT; + return (2); + } + print->type |= (*format == 'l') ? PRINT_TYPE_LONG : 0; + print->type |= (*format == 'h') ? PRINT_TYPE_CHAR : 0; + print->type |= (*format == 't') ? PRINT_TYPE_PTRD : 0; + print->type |= (*format == 'j') ? PRINT_TYPE_INTM : 0; + print->type |= (*format == 'z') ? PRINT_TYPE_SIZE : 0; + print->type |= (*format == 'Z') ? PRINT_TYPE_SIZE : 0; + print->type |= (*format == 'q') ? PRINT_TYPE_LLONG : 0; + return ((print->type) ? 1 : 0); +} + +int vfprintf_get_precision(printf_t *print, char const *format) +{ + int read_size; + + if (*format != '.') + return (0); + read_size = -1; + while (format[++read_size] >= '0' && format[++read_size] <= '9'){ + print->precision *= 10; + print->precision += format[read_size] - '0'; + } + return (read_size); +} + +void vfprintf_get_base(printf_t *print, char type) +{ + if (type == 'x' || type == 'X' || type == 'p') + print->base = 16; + if (type == 'b') + print->base = 2; + if (type == 'o') + print->base = 8; + if (type == 'X') + print->mode_flags |= PRINT_MODE_MAJ; +} + +int vfprintf_get_ouput_type(printf_t *print, char type) +{ + int len = -1; + char buffer; + char *str; + + if (type == 'c'){ + buffer = (char)va_arg(print->ap, int); + write(1, &buffer, 1); + } + if (type == 's'){ + str = va_arg(print->ap, char*); + while (str[++len] != '\0'); + write(1, str, len); + } + if (type == '%') + write(1, "%", 1); + if (type == '%' || type == 's' || type == 'c') + return (-1); + return ((!(type == 'o' || type == 'x' || type == 'X' + || type == 'p' || type == 'b' || type == 'i' + || type == 'd' || type == 'u')) ? 0 : 1); +} diff --git a/lib/my_stdio/vfprintf_get_size.c b/lib/my_stdio/vfprintf_get_size.c new file mode 100644 index 0000000..e8c2ab6 --- /dev/null +++ b/lib/my_stdio/vfprintf_get_size.c @@ -0,0 +1,83 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include "lib/my_stdio.h" + +static void vfprintf_get_sign(printf_t *print) +{ + intmax_t value; + + value = (intmax_t)print->value; + if (value < 0){ + print->value = (uintmax_t)(-value); + print->str_width++; + print->sign = '-'; + return; + } + if (print->mode_flags & PRINT_MODE_PLUS){ + print->str_width++; + print->sign = '+'; + return; + } + if (print->mode_flags & PRINT_MODE_SPACE){ + print->str_width++; + print->sign = ' '; + } +} + +static void vfprintf_get_size_unsigned(printf_t *print) +{ + uintmax_t value; + + if (print->type & PRINT_TYPE_CHAR) + value = MY_UCHAR_MAX; + if (print->type & PRINT_TYPE_SINT) + value = MY_USHORT_MAX; + if (print->type & PRINT_TYPE_LONG) + value = MY_ULONG_MAX; + if (print->type & PRINT_TYPE_PTRD) + value = MY_PTRDIFF_MAX; + if (print->type & PRINT_TYPE_LLONG) + value = MY_ULLONG_MAX; + if (!print->type) + value = MY_UINT_MAX; + while (value){ + value /= print->base; + print->nbr_width++; + print->str_width++; + } +} + +static void vfprintf_get_size_signed(printf_t *print) +{ + intmax_t value; + + value = (intmax_t)print->value; + if (!value){ + print->nbr_width = 1; + print->str_width++; + return; + } + while (value){ + value /= print->base; + print->nbr_width++; + print->str_width++; + } +} + +void vfprintf_get_size(printf_t *print, char type) +{ + vfprintf_get_sign(print); + if (type == 'x' || type == 'X' || type == 'p' || type == 'o' + || type == 'b' || type == 'u'){ + vfprintf_get_size_unsigned(print); + print->str_width += 2; + } + else + vfprintf_get_size_signed(print); + if (print->str_width < print->precision) + print->str_width = print->precision; +} diff --git a/lib/my_stdio/vfprintf_print_arg.c b/lib/my_stdio/vfprintf_print_arg.c new file mode 100644 index 0000000..3bf32f1 --- /dev/null +++ b/lib/my_stdio/vfprintf_print_arg.c @@ -0,0 +1,87 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include +#include "lib/my_stdio.h" + +static const char base[2][16] = { + "0123456789abcdef", + "0123456789ABCDEF" +}; + +static void vfprintf_set_output(printf_t *print, char str[], int maj) +{ + int locate; + int size; + + locate = (maj) ? 1 : 0; + size = print->str_width; + while (++print->cursor < print->nbr_width){ + str[size - 1 - print->cursor] = + base[locate][print->value % print->base]; + print->value /= print->base; + print->precision--; + } +} + +static void vfprintf_add_sign(printf_t *print, char str[]) +{ + int size; + + size = print->str_width; + if (print->mode_flags & PRINT_MODE_DIEZ){ + str[size - 1 - print->cursor++] = '0'; + print->precision--; + } + if (print->precision > 0) + while (print->precision > 0){ + str[size - 1 - print->cursor++] = '0'; + print->precision--; + } + if (print->mode_flags & PRINT_MODE_HEXA){ + if (print->mode_flags & PRINT_MODE_MAJ) + str[size - 1 - print->cursor++] = 'X'; + else + str[size - 1 - print->cursor++] = 'x'; + str[size - 1 - print->cursor++] = '0'; + } + if (print->sign != '\0') + str[size - 1 - print->cursor++] = print->sign; +} + +static void vfprintf_reverse_padding(char str[], int size) +{ + char tmp[size + 1]; + int len; + int i; + + i = 0; + len = -1; + tmp[size] = '\0'; + while (str[i++] == ' '); + while (str[i++]) + tmp[++len] = str[i]; + while (++len < size) + tmp[len] = ' '; + i = -1; + while (++i < size) + str[i] = tmp[i]; +} + +void vfprintf_print_arg(printf_t *print, int size) +{ + char str[size + 1]; + + str[size] = '\0'; + print->cursor = -1; + vfprintf_set_output(print, str, print->mode_flags & PRINT_MODE_MAJ); + vfprintf_add_sign(print, str); + while (print->cursor < size) + str[size - 1 - print->cursor++] = ' '; + if (print->mode_flags & PRINT_MODE_MINUS) + vfprintf_reverse_padding(str, print->str_width); + write(print->stream, str, sizeof(str) - 1); +} diff --git a/lib/my_stdlib/my_abs.c b/lib/my_stdlib/my_abs.c new file mode 100644 index 0000000..1f92d47 --- /dev/null +++ b/lib/my_stdlib/my_abs.c @@ -0,0 +1,22 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include "lib/my_stdlib.h" + +int my_abs(int j) +{ + return ((j < 0) ? -j : j); +} + +long int my_labs(long int j) +{ + return ((j < 0) ? -j : j); +} + +long long int my_llabs(long long int j) +{ + return ((j < 0) ? -j : j); +} diff --git a/lib/my_stdlib/my_atoi.c b/lib/my_stdlib/my_atoi.c new file mode 100644 index 0000000..24a984c --- /dev/null +++ b/lib/my_stdlib/my_atoi.c @@ -0,0 +1,61 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include "lib/my_stdlib.h" + +int my_atoi(const char *nptr) +{ + size_t neg; + int result; + + nptr--; + neg = 0; + result = 0; + while (*(++nptr) == '-' || *nptr == '+' + || *nptr == '\t' || *nptr == ' '){ + neg = (*nptr == '-') ? 1 : neg; + neg = (*nptr == '+') ? 0 : neg; + } + while (*(++nptr) >= '0' && *nptr <= '9') + result = (result * 10) + *nptr - '0'; + return (neg ? -result : result); +} + +long my_atol(const char *nptr) +{ + long result; + size_t neg; + + nptr--; + neg = 0; + result = 0; + while (*(++nptr) == '-' || *nptr == '+' + || *nptr == '\t' || *nptr == ' '){ + neg = (*nptr == '-') ? 1 : neg; + neg = (*nptr == '+') ? 0 : neg; + } + while (*(++nptr) >= '0' && *nptr <= '9') + result = (result * 10) + *nptr - '0'; + return (neg ? -result : result); +} + +long long my_atoll(const char *nptr) +{ + long long result; + size_t neg; + + nptr--; + neg = 0; + result = 0; + while (*(++nptr) == '-' || *nptr == '+' + || *nptr == '\t' || *nptr == ' '){ + neg = (*nptr == '-') ? 1 : neg; + neg = (*nptr == '+') ? 0 : neg; + } + while (*(++nptr) >= '0' && *nptr <= '9') + result = (result * 10) + *nptr - '0'; + return (neg ? -result : result); +} diff --git a/lib/my_stdlib/my_strnb.c b/lib/my_stdlib/my_strnb.c new file mode 100644 index 0000000..8816642 --- /dev/null +++ b/lib/my_stdlib/my_strnb.c @@ -0,0 +1,67 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include "lib/my_stdlib.h" + +int my_strnb_i(const char **str) +{ + size_t neg; + int result; + + neg = 0; + result = 0; + while (**str == '-' || **str == '+' + || **str == '\t' || **str == ' '){ + neg = (**str == '-') ? 1 : neg; + neg = (**str == '+') ? 0 : neg; + (*str)++; + } + while (**str >= '0' && **str <= '9'){ + result = (result * 10) + **str - '0'; + (*str)++; + } + return (neg ? -result : result); +} + +long int my_strnb_l(const char **str) +{ + long int result; + size_t neg; + + neg = 0; + result = 0; + while (**str == '-' || **str == '+' + || **str == '\t' || **str == ' '){ + neg = (**str == '-') ? 1 : neg; + neg = (**str == '+') ? 0 : neg; + (*str)++; + } + while (**str >= '0' && **str <= '9'){ + result = (result * 10) + **str - '0'; + (*str)++; + } + return (neg ? -result : result); +} + +long long int my_strnb_ll(const char **str) +{ + long long int result; + size_t neg; + + neg = 0; + result = 0; + while (**str == '-' || **str == '+' + || **str == '\t' || **str == ' '){ + neg = (**str == '-') ? 1 : neg; + neg = (**str == '+') ? 0 : neg; + (*str)++; + } + while (**str >= '0' && **str <= '9'){ + result = (result * 10) + **str - '0'; + (*str)++; + } + return (neg ? -result : result); +} diff --git a/lib/my_stdlib/my_strto.c b/lib/my_stdlib/my_strto.c new file mode 100644 index 0000000..c4915c5 --- /dev/null +++ b/lib/my_stdlib/my_strto.c @@ -0,0 +1,69 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include "lib/my_stdlib.h" + +static int check_base(char n, int base) +{ + char btab[] = + "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; + ssize_t exit; + ssize_t i; + + i = -1; + exit = 1; + while (++i < 62 && btab[i] == n); + i -= (i > 36) ? 26 : 0; + return ((--i > base || exit) ? -1 : i); +} + +long int strtol(char *nptr, char **endptr, int base) +{ + long int result; + ssize_t value; + ssize_t neg; + ssize_t i; + + i = 0; + neg = 1; + result = 0; + while (nptr[i++] == ' '); + while (nptr[i] == '-' || nptr[i] == '+') + neg = (nptr[i++] == '-') ? -1 : 1; + i = -1; + value = 0; + while (nptr[++i] != '\0' && value >= 0){ + value = check_base(nptr[i], base); + if (value >= 0) + result = result * 10 + value; + } + *endptr = nptr + i; + return (result * neg); +} + +long long int strtoll(char *nptr, char **endptr, int base) +{ + long long int result; + ssize_t value; + ssize_t neg; + ssize_t i; + + i = 0; + neg = 1; + result = 0; + while (nptr[i++] == ' '); + while (nptr[i] == '-' || nptr[i] == '+') + neg = (nptr[i++] == '-') ? -1 : 1; + i = -1; + value = 0; + while (nptr[++i] != '\0' && value >= 0){ + value = check_base(nptr[i], base); + if (value >= 0) + result = result * 10 + value; + } + *endptr = nptr + i; + return (result * neg); +} diff --git a/lib/my_string/my_strcat.c b/lib/my_string/my_strcat.c new file mode 100644 index 0000000..44ad9f1 --- /dev/null +++ b/lib/my_string/my_strcat.c @@ -0,0 +1,35 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include "lib/my_string.h" + +char *my_strcat(char *dest, char const *src) +{ + ssize_t start; + ssize_t i; + + i = -1; + start = -1; + while (dest[++start] != '\0'); + while (src[++i] != '\0') + dest[start + i] = src[i]; + dest[start + i] = '\0'; + return (dest); +} + +char *my_strncat(char *dest, char const *src, size_t n) +{ + ssize_t start; + ssize_t i; + + i = -1; + start = -1; + while (dest[++start] != '\0'); + while (++i < (ssize_t)n && src[i] != '\0') + dest[start + i] = src[i]; + dest[start + i] = '\0'; + return (dest); +} diff --git a/lib/my_string/my_strcmp.c b/lib/my_string/my_strcmp.c new file mode 100644 index 0000000..c9e7e4d --- /dev/null +++ b/lib/my_string/my_strcmp.c @@ -0,0 +1,25 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include "lib/my_string.h" + +int my_strcmp(const char *s1, const char *s2) +{ + ssize_t i; + + i = -1; + while (s1[++i] != '\0' && s1[i] != '\0' && s1[i] == s2[i]); + return (s1[i] - s2[i]); +} + +int my_strncmp(const char *s1, const char *s2, size_t n) +{ + size_t i; + + i = 0; + while (i < n && s1[i] != '\0' && s1[i] != '\0' && s1[i] == s2[i++]); + return (s1[i] - s2[i]); +} diff --git a/lib/my_string/my_strcoll.c b/lib/my_string/my_strcoll.c new file mode 100644 index 0000000..f095c44 --- /dev/null +++ b/lib/my_string/my_strcoll.c @@ -0,0 +1,26 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include "lib/my_string.h" + +int my_strcoll(const char *s1, const char *s2) +{ + ssize_t i; + + i = -1; + while (s1[++i] != '\0' && s1[i] != '\0' && s1[i] == s2[i]); + return (s1[i] - s2[i]); +} + +int my_strcoll_l(const char *s1, const char *s2) +{ + ssize_t i; + + i = -1; + while (s1[++i] != '\0' && s1[i] != '\0' && s1[i] == s2[i]); + return (s1[i] - s2[i]); + +} diff --git a/lib/my_string/my_strcpy.c b/lib/my_string/my_strcpy.c new file mode 100644 index 0000000..70c0003 --- /dev/null +++ b/lib/my_string/my_strcpy.c @@ -0,0 +1,28 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include "lib/my_string.h" + +char *my_strcpy(char *dest, char const *src) +{ + size_t i; + + i = -1; + while (src[++i] != '\0') + dest[i] = src[i]; + dest[i] = '\0'; + return (dest); +} + +char *my_strncpy(char *dest, char const *src, size_t n) +{ + size_t i; + + i = 0; + while (i < n && src[i] != '\0') + dest[i] = src[i++]; + return (dest); +} diff --git a/lib/my_string/my_strdup.c b/lib/my_string/my_strdup.c new file mode 100644 index 0000000..32e744e --- /dev/null +++ b/lib/my_string/my_strdup.c @@ -0,0 +1,69 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include +#include +#include "lib/my_string.h" + +char *my_strdup(char const *s) +{ + char *str; + ssize_t len; + + len = -1; + while (s[++len] != '\0'); + str = (char*)malloc(len + 1); + len = -1; + while (s[++len] != '\0') + str[len] = s[len]; + str[len] = '\0'; + return (str); +} + +char *my_strndup(char const *s, size_t n) +{ + char *str; + ssize_t i; + + i = -1; + while (++i < (ssize_t)n && s[i] != '\0'); + str = (char*)malloc(i + 1); + i = -1; + while (++i < (ssize_t)n && s[i] != '\0') + str[i] = s[i]; + str[i] = '\0'; + return (str); +} + +char *my_strdupa(char const *s) +{ + char *str; + ssize_t len; + + len = -1; + while (s[++len] != '\0'); + str = (char*)alloca(len + 1); + len = -1; + while (s[++len] != '\0') + str[len] = s[len]; + str[len] = '\0'; + return (str); +} + +char *my_strndupa(char const *s, size_t n) +{ + char *str; + ssize_t i; + + i = -1; + while (++i < (ssize_t)n && s[i] != '\0'); + str = (char*)alloca(i + 1); + i = -1; + while (++i < (ssize_t)n && s[i] != '\0') + str[i] = s[i]; + str[i] = '\0'; + return (str); +} diff --git a/lib/my_string/my_strlen.c b/lib/my_string/my_strlen.c new file mode 100644 index 0000000..96070db --- /dev/null +++ b/lib/my_string/my_strlen.c @@ -0,0 +1,25 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include "lib/my_string.h" + +size_t my_strlen(const char *s) +{ + ssize_t len; + + len = -1; + while (s[++len] != '\0'); + return (len); +} + +size_t my_strnlen(const char *s, size_t maxlen) +{ + ssize_t len; + + len = -1; + while (++len < (ssize_t)maxlen && s[len] != '\0'); + return (len); +} diff --git a/lib/my_string/my_strstr.c b/lib/my_string/my_strstr.c new file mode 100644 index 0000000..2af1dd8 --- /dev/null +++ b/lib/my_string/my_strstr.c @@ -0,0 +1,37 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include "lib/my_string.h" + +char *my_strstr(const char *haystack, const char *needle) +{ + ssize_t needle_len; + size_t len; + + len = 0; + haystack--; + needle_len = -1; + while (needle[++needle_len] != '\0'); + while (*(++haystack) != '\0' && needle[len] != '\0') + len = (*haystack == needle[len]) ? len + 1 : 0; + return ((needle[len] == '\0') ? (char*)haystack - needle_len : NULL); +} + +char *my_strcasestr(const char *haystack, const char *needle) +{ + size_t needle_len; + ssize_t len; + + len = 0; + haystack--; + needle_len = -1; + while (needle[++needle_len] != '\0'); + while (*(++haystack) != '\0' && needle[len] != '\0') + len = (*haystack == needle[len] + || *haystack - 'A' + 'a' == needle[len] + || *haystack == needle[len] - 'A' + 'a') ? len + 1 : 0; + return ((needle[len] == '\0') ? (char*)haystack - needle_len : NULL); +} diff --git a/map/level_1.txt b/map/level_1.txt new file mode 100644 index 0000000..152d3b7 --- /dev/null +++ b/map/level_1.txt @@ -0,0 +1,26 @@ +1 1 +1 1 +1 1 +1 1 +1 1 +1 1 +1 1 +1 1 +1 1 +1 1 +1 1 +1 1 +1 1 1 +1 V 1 +1 1 1 +1 1X 1 +1 1 1 +1 1 1 +1 1 1 1 +16 4 C 1 1 +1Y 1 Z111111111 1 +16 A 1111111111111111111 1 +11211111133311111111111111111111116 51 +11211111111111111111111111111111116 51 +1122222222222222222222222222222222 33 B51 +1111111111111111111111111111111111111111111111 diff --git a/map/level_2.txt b/map/level_2.txt new file mode 100644 index 0000000..36187dd --- /dev/null +++ b/map/level_2.txt @@ -0,0 +1,17 @@ +11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 +11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 +111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 1 + 1 + 111111111 + 3 3 3 111111111 + 51111111111111111111111111111111111111111111111111111111111111111111111 + X X XX 511111 + 1 511111 + 1 3 3 3 511111 + 111111111111111111111111111111111111 Z111111111111111111111111 511111 + 4 X 1 1111111111111111111111111111111111111111 1 + 1 1 1111111111111111111111111111111111111111 1 + 1 3 3 3 3 W 1 Z111111111111111111111111111111 1 + A 111111111111111111111111111111111111111111 444 1 B 1 +1111111133113311111111111111111111111111111111111111111111111 11111111 +11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 diff --git a/map/level_3.txt b/map/level_3.txt new file mode 100644 index 0000000..4fb08d1 --- /dev/null +++ b/map/level_3.txt @@ -0,0 +1,24 @@ +1 V V V V V +1 V V V V +1 V V V V +1 A V V V V +1 1 V V V V +1 V V V V +1 V V V V +1 111 +1 111 +1 111 +1 111 +1 111 +1 111 +1 111 +1 111 +1 111 +1 111 +1 11111 B 111 +1Y 22221 C 2 2212 222111 +1 3 22221 52111 C 2 Z2212 222111 +1111111111111Y 322221 222 11211111112112212 111 +111111111111112211111121112211122222 111 111 2 12 111 +111111111111133313313323313313322222 11133331113333233331233333111 +1111111111111111111111111111111111111111111111111111111111111111111 diff --git a/map/level_impossible.txt b/map/level_impossible.txt new file mode 100644 index 0000000..13bf5f6 --- /dev/null +++ b/map/level_impossible.txt @@ -0,0 +1,17 @@ + V V V VV + V V V VV + + + + + + + + + + + + + Z +A 3 3 3 3 3 B Z1111111111111 +1111111111111111111111111111111111111111111111 diff --git a/music/sikmu.ogg b/music/sikmu.ogg new file mode 100644 index 0000000..cfb2aef Binary files /dev/null and b/music/sikmu.ogg differ diff --git a/src/core/engine.c b/src/core/engine.c new file mode 100644 index 0000000..4d89b54 --- /dev/null +++ b/src/core/engine.c @@ -0,0 +1,63 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include "game/core.h" +#include "game/draw.h" + +static int update_counter(sfml_t *sfml, int *x, int *y, int end_x) +{ + int tmp; + + if (++(*x) == end_x){ + tmp = *x; + *x = 0; + (*y)++; + return (sfml->scene->level->width - tmp); + } + return (0); +} + +static void check_id(sfml_t *sfml, int x, int y, int i) +{ + uint8_t id; + + id = sfml->scene->level->map[i]; + if (id == ID_GROUND || id == ID_FAKE_GROUND) + draw_ground(sfml, x, y, + (i < sfml->scene->level->width) ? ' ' + : sfml->scene->level->map[i - sfml->scene->level->width]); + if (id == ID_PRINCESS || id == ID_FAKE_PRINCESS) + draw_princess(sfml, x, y); + if (id >= ID_PIK_DOWN && id <= ID_PIK_RIGHT) + draw_pik(sfml, x, y, id - ID_PIK_DOWN); + if (id >= ID_THROWER_STONE && id <= ID_THROWER_RIGHT) + draw_thrower(sfml, x, y, id - ID_THROWER_STONE); +} + +void map_rendering(sfml_t *sfml) +{ + int offset_x = sfml->scene->camera->x & 0x3f; + int offset_y = sfml->scene->camera->y & 0x3f; + int load_x = (!offset_x) ? BLOCK_LOAD_X : BLOCK_LOAD_X + 1; + int load_y = (!offset_y) ? BLOCK_LOAD_Y : BLOCK_LOAD_Y + 1; + int end_y = ((sfml->scene->camera->y >> 6) + load_y) + * sfml->scene->level->width; + int end_x = (sfml->scene->camera->x >> 6) + load_x; + int i = (sfml->scene->camera->x >> 6) + + (sfml->scene->camera->y >> 6) * sfml->scene->level->width - 1; + int x = -1; + int y = 0; + + end_y = (end_y > sfml->scene->level->height * + sfml->scene->level->width) + ? sfml->scene->level->height * sfml->scene->level->width : end_y; + end_x = (end_x > sfml->scene->level->width) ? +sfml->scene->level->width : end_x; + while (++i < end_y){ + i += update_counter(sfml, &x, &y, end_x); + check_id(sfml, (x << 6) - offset_x, (y << 6) - offset_y, i); + } +} diff --git a/src/core/event.c b/src/core/event.c new file mode 100644 index 0000000..29270f0 --- /dev/null +++ b/src/core/event.c @@ -0,0 +1,14 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include "game/core.h" + +void set_exit(sfml_t *sfml) +{ + while (sfRenderWindow_pollEvent(sfml->window, &sfml->event)) + if (sfml->event.type == sfEvtClosed) + sfRenderWindow_close(sfml->window); +} diff --git a/src/core/get_key.c b/src/core/get_key.c new file mode 100644 index 0000000..cef3241 --- /dev/null +++ b/src/core/get_key.c @@ -0,0 +1,26 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include "game/core.h" +#include +#include + +static const uint8_t key_code[] = {sfKeyLShift, sfKeyRShift, sfKeyLeft, + sfKeyRight, sfKeyUp, sfKeySpace, sfKeyA, sfKeyEscape, + sfKeyDown, sfKeyReturn}; + +void get_key(sfml_t *sfml) +{ + int i; + + i = -1; + while (++i < 10){ + if (sfKeyboard_isKeyPressed(key_code[i])) + sfml->key |= (0x10000 << i) | (0x01 << i); + else + sfml->key &= ~(0x10000 << i); + } +} diff --git a/src/core/help.c b/src/core/help.c new file mode 100644 index 0000000..0f9cd5b --- /dev/null +++ b/src/core/help.c @@ -0,0 +1,29 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include "lib/my_stdio.h" + +void man_help(const char *bin_name) +{ + my_printf("NAME\n"); + my_printf("\t%s - a framebuffer runner game.\n\n", bin_name); + my_printf("SYNOPSIS\n"); + my_printf("\t%s [--version] [FILE]\n\n", bin_name); + my_printf("DESCRIPTION\n"); + my_printf("\t%s is a runner game inspired by Orton and the", bin_name); + my_printf(" princess.\n\n"); + my_printf("KEY\n"); + my_printf("\tLeft:\tMove player to left.\n"); + my_printf("\tRight:\tMove player to right.\n"); + my_printf("\tSpace:\tPlayer jump.\n"); + my_printf("\tUp:\tPlayer jump.\n"); + my_printf("\tEchap:\tPause menu.\n"); + my_printf("OPTIONS\n"); + my_printf("\t--version\n"); + my_printf("\t\tPrint the current version.\n"); + my_printf("\t--help or -h\n"); + my_printf("\t\tPrint this page.\n"); +} diff --git a/src/core/main.c b/src/core/main.c new file mode 100644 index 0000000..e31dd91 --- /dev/null +++ b/src/core/main.c @@ -0,0 +1,80 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include +#include +#include "lib/my_graphics.h" +#include "lib/my_memory.h" +#include "lib/my_stdio.h" +#include "game/kinematic.h" +#include "game/memory.h" +#include "game/core.h" +#include "game/help.h" +#include "game/game.h" +#include "game/menu.h" + +static int check_flags(const char *bin_name, const char *str) +{ + if (*str == '-' && (*(str + 1) == 'h' && *(str + 2) == '\0') + || (str[1] == '-' && str[2] == 'h' && str[3] == 'e' + && str[4] == 'l' && str[5] == 'p' && str[6] == '\0')){ + man_help(bin_name); + return (1); + } + if (*str == '-' && str[1] == '-' && str[2] == 'v' && str[3] == 'e' + && str[4] == 'r' && str[5] == 's' && str[6] == 'i' && str[7] == 'o' + && str[8] == 'n' && str[9] == '\0'){ + my_printf("%s version 1.0.\n", bin_name); + return (1); + } + return (0); +} + +static void main_loop(sfml_t *sfml, char *map_name) +{ + void *window; + int exit; + int y; + + exit = 1; + y = -(BMP_HEIGHT << 3); + sfMusic_play(sfml->sound->music); + sfMusic_setVolume(sfml->sound->music, sfml->sound->volume); + while (sfml != NULL && exit != 2 + && sfRenderWindow_isOpen(sfml->window)){ + while (sfml->key & PRES_ENTER) + get_key(sfml); + exit = menu_start(sfml, &y); + if (!exit){ + intro(sfml); + game_main(sfml, map_name); + } + } +} + +int main(int argc, char **argv) +{ + sfml_t *sfml; + int error = 0; + int exit; + + if (argc != 2 || check_flags(argv[0], argv[1])) + return ((argc != 2) ? 84 : 0); + sfml = init_sfml(SCREEN_WIDTH, SCREEN_HEIGHT); + if (sfml != NULL) + sfml->scene = new_scene(argv[1], SCENE_LOAD_ALL_GAME); + if (sfml == NULL || sfml->scene == NULL){ + if (sfml != NULL) + free_scene(sfml->scene); + free_sfml(sfml); + return (84); + } + free_scene(sfml->scene); + srand(time(NULL)); + main_loop(sfml, argv[1]); + free_sfml(sfml); + return (0); +} diff --git a/src/draw/draw.c b/src/draw/draw.c new file mode 100644 index 0000000..53061eb --- /dev/null +++ b/src/draw/draw.c @@ -0,0 +1,108 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include "lib/my_graphics.h" +#include "game/draw.h" +#include "game/core.h" + +static const uint8_t wall_pik[] = {128, 192, 128, 0, 128, 192, 128, 0}; +static const uint8_t player_run_a_r[] = {0, 126, 66, 86, 66, 66, 126, 40}; +static const uint8_t player_run_b_r[] = {0, 126, 66, 86, 66, 66, 126, 34}; +static const uint8_t player_run_c_r[] = {126, 66, 86, 66, 66, 127, 128, 0}; +static const uint8_t player_run_d_r[] = {126, 66, 86, 66, 66, 254, 1, 0}; +static const uint8_t player_idle_r[] = {0, 126, 66, 86, 66, 66, 126, 66}; +static const uint8_t player_jump_r[] = {126, 66, 86, 66, 66, 255, 0, 0}; +static const uint8_t player_run_a_l[] = {0, 126, 66, 106, 66, 66, 126, 20}; +static const uint8_t player_run_b_l[] = {0, 126, 66, 106, 66, 66, 126, 68}; +static const uint8_t player_run_c_l[] = {126, 66, 106, 66, 66, 254, 1, 0}; +static const uint8_t player_run_d_l[] = {126, 66, 106, 66, 66, 127, 128, 0}; +static const uint8_t player_idle_l[] = {0, 126, 66, 106, 66, 66, 126, 66}; +static const uint8_t player_jump_l[] = {126, 66, 106, 66, 66, 255, 0, 0}; +static const uint8_t explosion_a[] = {0, 44, 82, 74, 129, 110, 48, 0}; +static const uint8_t explosion_b[] = {6, 105, 66, 136, 2, 33, 132, 99}; +static const uint8_t explosion_c[] = {8, 65, 16, 132, 0, 0, 17, 130}; +static const uint8_t* player[]={ + player_run_a_l, player_run_b_l, player_run_c_l, player_run_d_l, + player_run_a_r, player_run_b_r, player_run_c_r, player_run_d_r, + player_jump_l, player_jump_r, + player_idle_l, player_idle_r, + explosion_a, explosion_b, explosion_c +}; + +static void draw_line(sfml_t *sfml, const uint8_t *bmp, int draw[4], int y) +{ + int end; + int x; + + x = (draw[0] < 0) ? -draw[0] - 1 : -1; + end = (x + (8 << draw[2]) > sfml->width) ? +sfml->width - x : (8 << draw[2]); + while (++x < end){ + if (bmp[y >> draw[2]] & 0x80 >> (x >> draw[2])) + my_pixel(sfml, draw[0] + x, draw[1] + y, draw[3]); + } +} + +void draw_monochrome(sfml_t *sfml, const uint8_t *bmp, int draw[4]) +{ + int end; + int y; + + y = (draw[1] < 0) ? -draw[1] - 1 : -1; + end = (y + (8 << draw[2]) > sfml->height) ? +sfml->height - y : (8 << draw[2]); + while (++y < end) + if (bmp[y >> draw[2]]) + draw_line(sfml, bmp, draw, y); +} + +void player_rendering(sfml_t *sfml) +{ + int draw[4]; + int locate; + + locate = 0; + if (sfml->scene->player->action & PLAYER_RUN) + locate = sfml->scene->player->frame_counter / 5 + + ((sfml->scene->player->action & PLAYER_LEFT) ? 4 : 0); + if (sfml->scene->player->action & PLAYER_JUMP) + locate = (sfml->scene->player->action & PLAYER_LEFT) ? 9 : 8; + if (sfml->scene->player->action & PLAYER_IDLE) + locate = (sfml->scene->player->action & PLAYER_LEFT) ? 11 : 10; + if (sfml->scene->player->action & PLAYER_DEAD) + locate = sfml->scene->player->dead_counter / + PLAYER_DEAD_TIME + 12; + draw[0] = sfml->scene->player->x - sfml->scene->camera->x; + draw[1] = sfml->scene->player->y - sfml->scene->camera->y; + draw[2] = 3; + draw[3] = 0x000000ff; + draw_monochrome(sfml, player[locate], draw); +} + +void wall_rendering(sfml_t *sfml) +{ + int offset_y = sfml->scene->camera->y & 0x1f; + int sprite_load_y; + int rectangle[4]; + int draw[4]; + int i = -1; + + rectangle[0] = sfml->scene->wall->x - sfml->scene->camera->x; + rectangle[1] = 0; + rectangle[2] = sfml->scene->wall->x + WALL_SIZE - + sfml->scene->camera->x; + rectangle[3] = sfml->height; + my_filled_rectangle(sfml, rectangle, 0x000000ff); + sprite_load_y = (offset_y) ? BLOCK_LOAD_Y : BLOCK_LOAD_Y + 1; + while (++i < sprite_load_y){ + draw[0] = sfml->scene->wall->x + WALL_SIZE - + sfml->scene->camera->x; + draw[1] = (i << 6) - offset_y; + draw[2] = 3; + draw[3] = 0x000000ff; + draw_monochrome(sfml, wall_pik, draw); + } +} diff --git a/src/draw/draw_compresed_bmp.c b/src/draw/draw_compresed_bmp.c new file mode 100644 index 0000000..6eb9de8 --- /dev/null +++ b/src/draw/draw_compresed_bmp.c @@ -0,0 +1,66 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include "game/endtext_bmp.h" +#include "lib/my_graphics.h" +#include "game/logo_bmp.h" +#include "game/draw.h" +#include "game/core.h" + + +static void set_8px_bmp(sfml_t *sfml, int type, int *x, int y) +{ + int i; + + i = -1; + if (type == 0xff){ + *x += (8 << 3); + return; + } + while (++i < (8 << 3)){ + if (!(type & (0x80 >> (i >> 3)))) + my_pixel(sfml, *x, y, 0x000000ff); + else + my_pixel(sfml, *x, y, 0xffffffff); + (*x)++; + } +} + +void draw_logo_bmp(sfml_t *sfml, int y) +{ + int j; + int i; + int x; + + j = -1; + while (++j < BMP_HEIGHT << 3){ + i = -1; + x = (sfml->width >> 1) - (BMP_WIDTH << 6 >> 1); + while (++i < BMP_WIDTH) + set_8px_bmp(sfml, title_bmp[(j >> 3 << 4) + i], + &x, y); + y++; + } +} + +void draw_endtext_bmp(sfml_t *sfml) +{ + int j; + int i; + int x; + int y; + + j = -1; + y = (SCREEN_HEIGHT >> 2) - (BMP_END_HEIGHT << 3 >> 1); + while (++j < BMP_END_HEIGHT << 3){ + i = -1; + x = (sfml->width >> 1) - (BMP_END_WIDTH << 6 >> 1); + while (++i < BMP_END_WIDTH) + set_8px_bmp(sfml, ~endtext_bmp[(j >> 3 << 4) + i], + &x, y); + y++; + } +} diff --git a/src/draw/draw_id.c b/src/draw/draw_id.c new file mode 100644 index 0000000..d2343a6 --- /dev/null +++ b/src/draw/draw_id.c @@ -0,0 +1,73 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include "lib/my_graphics.h" +#include "game/draw.h" +#include "game/core.h" + +static const uint8_t princess_r[] = {0, 126, 66, 86, 66, 66, 126, 66}; +static const uint8_t princess_l[] = {0, 126, 66, 106, 66, 66, 126, 66}; +static const uint8_t grass[] = {255, 221, 187, 255, 255, 255, 255, 255}; +static const uint8_t pik_right[] = {1, 3, 1, 0, 1, 3, 1, 0}; +static const uint8_t pik_down[] = {0, 0, 0, 0, 0, 0, 68, 238}; +static const uint8_t pik_up[] = {238, 68, 0, 0, 0, 0, 0, 0}; +static const uint8_t pik_left[] = {128, 192, 128, 0, 128, 192, 128, 0}; +static const uint8_t *pik[] = { + pik_down, pik_up, pik_right, pik_left +}; + +void draw_ground(sfml_t *sfml, int x, int y, uint8_t grass_id) +{ + int rectangle[4]; + int draw[4]; + + if (grass_id == ID_GROUND || grass_id == ID_FAKE_GROUND){ + rectangle[0] = x; + rectangle[1] = y; + rectangle[2] = 64 + x; + rectangle[3] = 64 + y; + my_filled_rectangle(sfml, rectangle, 0x000000ff); + } else { + draw[0] = x; + draw[1] = y; + draw[2] = 3; + draw[3] = 0x000000ff; + draw_monochrome(sfml, grass, draw); + } +} + +void draw_pik(sfml_t *sfml, int x, int y, uint8_t direction) +{ + int draw[4]; + + draw[0] = x; + draw[1] = y; + draw[2] = 3; + draw[3] = 0x000000ff; + draw_monochrome(sfml, pik[direction], draw); +} + +void draw_princess(sfml_t *sfml, int x, int y) +{ + int rect[5]; + int draw[4]; + + draw[0] = x; + draw[1] = y; + draw[2] = 3; + draw[3] = 0x000000ff; + draw_monochrome(sfml, (x > sfml->scene->player->x - + sfml->scene->camera->x) + ? princess_l : princess_r, draw); + if (sfml->debug){ + rect[0] = x + 8; + rect[1] = y + 8; + rect[2] = x + 56; + rect[3] = y + 64; + rect[4] = 2; + my_rectangle(sfml, rect, 0xff0000ff, 0x00000000); + } +} diff --git a/src/draw/draw_thrower.c b/src/draw/draw_thrower.c new file mode 100644 index 0000000..eb4327a --- /dev/null +++ b/src/draw/draw_thrower.c @@ -0,0 +1,85 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include "game/draw.h" +#include "game/core.h" + +static const uint8_t bullet_left[] = {64, 192, 64, 0, 0, 0, 0, 0}; +static const uint8_t bullet_right[] = {128, 192, 128, 0, 0, 0, 0, 0}; +static const uint8_t bullet_up[] = {64, 224, 0, 0, 0, 0, 0, 0}; +static const uint8_t bullet_down[] = {224, 64, 0, 0, 0, 0, 0, 0}; +static const uint8_t stone[] = {224, 160, 224, 0, 0, 0, 0, 0}; +static const uint8_t thrower_up[] = {0, 0, 0, 0, 0, 60, 126, 126}; +static const uint8_t thrower_down[] = {126, 126, 60, 0, 0, 0, 0, 0}; +static const uint8_t thrower_right[] = {0, 192, 224, 224, 224, 224, 192, 0}; +static const uint8_t thrower_left[] = {0, 3, 7, 7, 7, 7, 3, 0}; +static const uint8_t *thrower[] = { + thrower_down, thrower_up, thrower_down, thrower_right, thrower_left +}; +static const uint8_t *bullet[] = { + bullet_up, bullet_down, bullet_right, bullet_left +}; + +void draw_thrower(sfml_t *sfml, int x, int y, uint8_t direction) +{ + int draw[4]; + + draw[0] = x; + draw[1] = y; + draw[2] = 3; + draw[3] = 0x000000ff; + draw_monochrome(sfml, thrower[direction], draw); +} + +static void draw_stone(sfml_t *sfml, object_t *object) +{ + int draw[4]; + + if (object == NULL) + return; + draw[0] = object->x - sfml->scene->camera->x; + draw[1] = object->y - sfml->scene->camera->y; + draw[3] = 0x000000ff; + draw[2] = 3; + draw_monochrome(sfml, stone, draw); +} + +static void draw_bullet(sfml_t *sfml, object_t *object, uint8_t locate) +{ + int draw[4]; + + draw[0] = object->x - sfml->scene->camera->x; + draw[1] = object->y - sfml->scene->camera->y; + draw[2] = 3; + draw[3] = 0x000000ff; + draw_monochrome(sfml, bullet[locate], draw); +} + +static void draw_setup(sfml_t *sfml, thrower_t *thrower, int j) +{ + if (thrower->action == ID_THROWER_STONE){ + draw_stone(sfml, thrower->object[j]); + return; + } + if (thrower->object[j] == NULL + || thrower->object[j]->sleep == BULLET_STOP) + return; + draw_bullet(sfml, thrower->object[j], + thrower->action - ID_THROWER_STONE - 1); +} + +void draw_thrower_object(sfml_t *sfml) +{ + int i; + int j; + + i = -1; + while (++i < sfml->scene->level->thrower_counter){ + j = -1; + while (++j < NB_OBJECT) + draw_setup(sfml, sfml->scene->thrower[i], j); + } +} diff --git a/src/game/camera.c b/src/game/camera.c new file mode 100644 index 0000000..29eb6af --- /dev/null +++ b/src/game/camera.c @@ -0,0 +1,59 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include +#include "lib/my_stdlib.h" +#include "game/game.h" +#include "game/core.h" + +static void update_camera_dead(scene_t *scene) +{ + scene->camera->x += rand() % CAMERA_SHAKY; + scene->camera->y += rand() % CAMERA_SHAKY; + scene->player->dead_counter++; + if (scene->player->dead_counter >= PLAYER_DEAD_TIME * 3){ + scene->player->x = scene->player->start_x; + scene->player->y = scene->player->start_y; + if (scene->wall != NULL) + scene->wall->x = scene->player->x - (WALL_SIZE << 2); + scene->player->action = PLAYER_IDLE | PLAYER_LEFT; + scene->player->dead_counter = 0; + scene->player->frame_counter = 0; + scene->player->dx = 0; + scene->player->dy = 0; + scene->player->nb_dead++; + } +} + +static void fix_camera(scene_t *scene, int width, int height) +{ + if (scene->camera->x < 0) + scene->camera->x = 0; + if (scene->camera->y < 0) + scene->camera->y = 0; + if (scene->camera->x + width > scene->level->width << 6) + scene->camera->x = (scene->level->width << 6) - width; + if (scene->camera->y + height > scene->level->height << 6) + scene->camera->y = (scene->level->height << 6) - height; +} + +void game_camera_update(scene_t *scene, int width, int height) +{ + int dx; + int dy; + + dx = scene->player->x - (width >> 1) - scene->camera->x; + dy = scene->player->y - (height >> 1) - scene->camera->y; + scene->camera->x = (my_abs(dx) > CAMERA_SPEED) + ? scene->camera->x + CAMERA_SPEED * ((dx < 0) ? -1 : 1) +: scene->player->x - (width >> 1); + scene->camera->y = (my_abs(dy) > CAMERA_SPEED) + ? scene->camera->y + CAMERA_SPEED * ((dy < 0) ? -1 : 1) +: scene->player->y - (height >> 1); + fix_camera(scene, width, height); + if (scene->player->action & PLAYER_DEAD) + update_camera_dead(scene); +} diff --git a/src/game/collision.c b/src/game/collision.c new file mode 100644 index 0000000..65e4ec6 --- /dev/null +++ b/src/game/collision.c @@ -0,0 +1,87 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include "game/game.h" +#include "game/core.h" + +void collision_pik(sfml_t *sfml) +{ + uint8_t *map = sfml->scene->level->map; + int x = sfml->scene->player->x; + int y = sfml->scene->player->y; + int width = sfml->scene->level->width; + + if (map[(x + 8 >> 6) + (y >> 6) * width] == ID_PIK_LEFT + || map[(x + 8 >> 6) + (y + 64 >> 6) * width] == ID_PIK_LEFT + || map[(x + 56 >> 6) + (y >> 6) * width] == ID_PIK_RIGHT + || map[(x + 56 >> 6) + (y + 64 >> 6) * width] == ID_PIK_RIGHT + || map[(x + 8 >> 6) + (y + 16 >> 6) * width] == ID_PIK_DOWN + || map[(x + 56 >> 6) + (y + 16 >> 6) * width] == ID_PIK_DOWN + || map[(x + 8 >> 6) + (y + 48 >> 6) * width] == ID_PIK_UP + || map[(x + 56 >> 6) + (y + 48 >> 6) * width] == ID_PIK_UP){ + sfml->scene->player->action = PLAYER_DEAD; + sfml->scene->player->dead_counter = 0; + } +} + +void collision_player_run(scene_t *scene) +{ + uint8_t *map = scene->level->map; + int x = scene->player->x; + int y = scene->player->y; + + if (map[(x + 8 >> 6) + (y + 1 >> 6) * scene->level->width] == ID_GROUND + || map[(x + 8 >> 6) + (y + 63 >> 6) * + scene->level->width] == ID_GROUND){ + scene->player->x = ((scene->player->x - 8) >> 6 << 6) + 55; + scene->player->action &= ~(PLAYER_RUN | PLAYER_JUMP); + scene->player->action |= PLAYER_IDLE; + scene->player->dx = 0; + return; + } + if (map[(x + 54 >> 6) + (y + 1 >> 6) * scene->level->width] == ID_GROUND + || map[(x + 54 >> 6) + + (y + 63 >> 6) * scene->level->width] == ID_GROUND){ + scene->player->x = ((scene->player->x - 8) >> 6 << 6) + 9; + scene->player->action &= ~(PLAYER_RUN | PLAYER_JUMP); + scene->player->action |= PLAYER_IDLE; + scene->player->dx = 0; + } +} + +void collision_player_jump(scene_t *scene) +{ + uint8_t *map = scene->level->map; + int x = scene->player->x; + int y = scene->player->y; + + if (map[(x + 9 >> 6) + (y >> 6) * scene->level->width] == ID_GROUND + || map[(x + 53 >> 6) + (y >> 6) * scene->level->width] == ID_GROUND){ + scene->player->y = (scene->player->y >> 6 << 6) + 64; + scene->player->counter_jump = PLAYER_MAX_JUMP; + scene->player->dy = 1; + } + if (map[(x + 53 >> 6) + (y + 64 >> 6) * + scene->level->width] == ID_GROUND + || map[(x + 9 >> 6) + (y + 64 >> 6) * + scene->level->width] == ID_GROUND){ + scene->player->y = ((scene->player->y - 64) >> 6 << 6) + 64; + scene->player->action &= ~(PLAYER_CANT_JUMP | PLAYER_JUMP); + scene->player->counter_jump = 0; + scene->player->dy = 1; + } +} + +void collision_wall(sfml_t *sfml) +{ + if (sfml->scene->wall->x + WALL_SIZE + 16 > sfml->scene->player->x){ + sfml->scene->player->action = PLAYER_DEAD; + sfml->scene->player->dead_counter = 0; + } + if (sfml->scene->wall->x + WALL_SIZE > sfml->scene->level->width << 6) + sfml->scene->wall->x = + (sfml->scene->level->width << 6) - WALL_SIZE; +} diff --git a/src/game/collision_princess.c b/src/game/collision_princess.c new file mode 100644 index 0000000..4864a7f --- /dev/null +++ b/src/game/collision_princess.c @@ -0,0 +1,41 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include "game/core.h" +#include "game/game.h" + +static int collision_id_princess(sfml_t *sfml, int id_princess) +{ + uint8_t *map = sfml->scene->level->map; + int x = sfml->scene->player->x; + int y = sfml->scene->player->y; + int width = sfml->scene->level->width; + + if (map[(x + 8 >> 6) + (y + 8 >> 6) * width] == id_princess) + return ((x + 8 >> 6) + (y + 8 >> 6) * width); + if (map[(x + 8 >> 6) + (y + 64 >> 6) * width] == id_princess) + return ((x + 8 >> 6) + (y + 64 >> 6) * width); + if (map[(x + 56 >> 6) + (y + 8 >> 6) * width] == id_princess) + return ((x + 54 >> 6) + (y + 8 >> 6) * width); + if (map[(x + 56 >> 6) + (y + 64 >> 6) * width] == id_princess) + return ((x + 54 >> 6) + (y + 64 >> 6) * width); + return (-1); +} + +void collision_princess(sfml_t *sfml) +{ + int locate; + + locate = collision_id_princess(sfml, ID_FAKE_PRINCESS); + if (locate >= 0){ + sfml->scene->level->map[locate] = ' '; + sfml->scene->player->action = PLAYER_DEAD; + sfml->scene->player->dead_counter = 0; + sfml->scene->message_active |= MESSAGE_FAKE_PRINCESS; + } + if (collision_id_princess(sfml, ID_PRINCESS) >= 0) + sfml->scene->player->action |= PLAYER_WIN; +} diff --git a/src/game/collision_show.c b/src/game/collision_show.c new file mode 100644 index 0000000..56e09fa --- /dev/null +++ b/src/game/collision_show.c @@ -0,0 +1,61 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include "lib/my_graphics.h" +#include "game/game.h" +#include "game/core.h" + +static void collision_show_thrower(sfml_t *sfml, +object_t *object, camera_t *camera, uint8_t action) +{ + int rect[5]; + int i; + int j; + + if (object == NULL || + (action != ID_THROWER_STONE && object->sleep == BULLET_STOP)) + return; + rect[0] = object->x - camera->x; + rect[1] = object->y - camera->y; + rect[2] = object->x - camera->x + 24; + rect[3] = object->y - camera->y + 24; + rect[4] = 2; + my_rectangle(sfml, rect, 0xff0000ff, 0x00000000); +} + +static void thrower_show(sfml_t *sfml) +{ + int i; + int j; + + i = -1; + while (++i < sfml->scene->level->thrower_counter){ + j = -1; + while (++j < NB_OBJECT) + collision_show_thrower(sfml, + sfml->scene->thrower[i]->object[j], sfml->scene->camera, + sfml->scene->thrower[i]->action); + } +} + +void collision_show(sfml_t *sfml) +{ + int rect[5]; + + if ((!(sfml->key & PRES_A) && sfml->key & KEY_A)){ + sfml->key &= ~KEY_A; + sfml->debug ^= 0x01; + } + if (!(sfml->debug & 0x01)) + return; + rect[0] = sfml->scene->player->x - sfml->scene->camera->x + 8; + rect[1] = sfml->scene->player->y - sfml->scene->camera->y; + rect[2] = sfml->scene->player->x - sfml->scene->camera->x + 56; + rect[3] = sfml->scene->player->y - sfml->scene->camera->y + 64; + rect[4] = 2; + my_rectangle(sfml, rect, 0xff0000ff, 0x00000000); + thrower_show(sfml); +} diff --git a/src/game/collision_thrower.c b/src/game/collision_thrower.c new file mode 100644 index 0000000..c56a7ce --- /dev/null +++ b/src/game/collision_thrower.c @@ -0,0 +1,57 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include "game/game.h" +#include "game/core.h" + +static void collision_player_stone(sfml_t *sfml, +player_t *player, object_t *object) +{ + if (object == NULL) + return; + if (object->x + 24 >= player->x + 8 && object->x <= player->x + 56 + && object->y + 24 >= player->y && object->y <= player->y + 32){ + object->y = player->y - 16; + object->dy = -2; + player->counter_jump = PLAYER_MAX_JUMP; + sfml->debug |= 0x02; + player->dy = 6; + } +} + +static void collision_player_bullet(sfml_t *sfml, +player_t *player, object_t *object) +{ + if (object == NULL || object->sleep == BULLET_STOP) + return; + if (object->x + 24 >= player->x + 8 && object->x <= player->x + 56 + && object->y + 24 >= player->y + 8 && object->y <= player->y + 64){ + player->action = PLAYER_DEAD; + player->dead_counter = 0; + } +} + +void collision_stone(sfml_t *sfml) +{ + void (*fun[2])(sfml_t*, player_t*, object_t*); + int i; + int j; + + if (sfml->scene->thrower == NULL) + return; + i = -1; + fun[1] = &collision_player_stone; + fun[0] = &collision_player_bullet; + while (++i < sfml->scene->level->thrower_counter){ + j = -1; + while (++j < NB_OBJECT) + fun[sfml->scene->thrower[i]->action == ID_THROWER_STONE] + (sfml, sfml->scene->player, + sfml->scene->thrower[i]->object[j]); + } + sfml->scene->player->y += (sfml->debug & 0x02) ? 4 : 0; + sfml->debug &= ~0x02; +} diff --git a/src/game/game.c b/src/game/game.c new file mode 100644 index 0000000..c741208 --- /dev/null +++ b/src/game/game.c @@ -0,0 +1,86 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include "lib/my_graphics.h" +#include "game/kinematic.h" +#include "game/message.h" +#include "game/ascii.h" +#include "game/game.h" +#include "game/core.h" +#include "game/draw.h" +#include "game/menu.h" +#include "game/memory.h" +#include + +static void show_hud(sfml_t *sfml, int nb_dead) +{ + uint32_t *option; + int pos[2]; + + option = my_print_set_option(3, 0x000000ff, 0xffffffff, ASCII_REVERSE); + pos[0] = 8; + pos[1] = 8; + my_print(sfml, pos, "Dead:", option); + pos[0] = 168; + pos[1] = 8; + if (!nb_dead) + my_print(sfml, pos, "0", option); + else + my_print_nbr(sfml, nb_dead, option, pos); + free(option); +} + +static void rendering(sfml_t *sfml, int *fade_active) +{ + my_clear_vram(sfml, 0xffffffff); + map_rendering(sfml); + player_rendering(sfml); + wall_rendering(sfml); + draw_thrower_object(sfml); + collision_show(sfml); + check_message(sfml, sfml->scene->message); + show_hud(sfml, sfml->scene->player->nb_dead); + if (!*fade_active){ + fade(sfml, FADE_OPEN); + *fade_active = 1; + } + else + my_display_vram(sfml); +} + +static void game_update_object(sfml_t *sfml) +{ + get_key(sfml); + if (!(sfml->scene->player->action & PLAYER_DEAD)) + update_player(sfml); + #ifndef NO_WALL_MOVE + update_wall(sfml); + #endif + update_thrower(sfml, sfml->scene->thrower, sfml->scene->level); +} + +void game_main(sfml_t *sfml, char *map_name) +{ + int fade_active; + int exit = 1; + + fade_active = 0; + sfml->scene = new_scene(map_name, SCENE_LOAD_ALL_GAME); + if (sfml->scene == NULL) + return; + while (sfRenderWindow_isOpen(sfml->window) + && !(sfml->scene->player->action & PLAYER_WIN) && exit){ + game_camera_update(sfml->scene, sfml->width, sfml->height); + rendering(sfml, &fade_active); + game_update_object(sfml); + exit = check_pause(sfml); + set_exit(sfml); + } + free_scene(sfml->scene); + fade(sfml, FADE_CLOSE); + if (exit) + end(sfml, sfml->scene->player->nb_dead); +} diff --git a/src/game/update_obj.c b/src/game/update_obj.c new file mode 100644 index 0000000..06503a7 --- /dev/null +++ b/src/game/update_obj.c @@ -0,0 +1,78 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include "game/game.h" +#include "game/core.h" + +static void player_jump(sfml_t *sfml) +{ + if (sfml->key & (PRES_SPACE | PRES_UP) + && (sfml->scene->player->dy <= 0 + || !(sfml->scene->player->action & PLAYER_CANT_JUMP)) + && sfml->scene->player->counter_jump < PLAYER_MAX_JUMP){ + if (!(sfml->scene->player->action & PLAYER_CANT_JUMP)){ + sfml->scene->player->action |= PLAYER_CANT_JUMP; + sfml->scene->player->dy = -8; + } + else + sfml->scene->player->dy -= 2; + sfml->scene->player->counter_jump++; + } + if (sfml->scene->player->dy < PLAYER_FALL_MAX) + sfml->scene->player->dy++; + sfml->scene->player->y += sfml->scene->player->dy; + collision_player_jump(sfml->scene); + if (sfml->scene->player->action & PLAYER_CANT_JUMP){ + sfml->scene->player->action &= ~(PLAYER_IDLE | PLAYER_RUN); + sfml->scene->player->action |= PLAYER_JUMP; + } +} + +static void move_player(sfml_t *sfml) +{ + if (sfml->key & PRES_LEFT){ + if (sfml->scene->player->dx < PLAYER_SPEED) + sfml->scene->player->dx++; + sfml->scene->player->action &= ~(PLAYER_JUMP | PLAYER_IDLE); + sfml->scene->player->action |= PLAYER_LEFT | PLAYER_RUN; + } + if (sfml->key & PRES_RIGHT){ + if (sfml->scene->player->dx > -PLAYER_SPEED) + sfml->scene->player->dx--; + sfml->scene->player->action &= + ~(PLAYER_LEFT | PLAYER_JUMP | PLAYER_IDLE); + sfml->scene->player->action |= PLAYER_RUN; + } + if (!(sfml->key & PRES_LEFT) && !(sfml->key & PRES_RIGHT)){ + sfml->scene->player->action &= ~(PLAYER_RUN | PLAYER_RUN); + sfml->scene->player->action |= PLAYER_IDLE; + sfml->scene->player->frame_counter = 0; + sfml->scene->player->dx /= 2; + } +} + +void update_player(sfml_t *sfml) +{ + sfml->scene->player->frame_counter = + (sfml->scene->player->frame_counter + 1) % 20; + move_player(sfml); + sfml->scene->player->x += sfml->scene->player->dx; + collision_player_run(sfml->scene); + collision_pik(sfml); + collision_princess(sfml); + collision_stone(sfml); + player_jump(sfml); +} + +void update_wall(sfml_t *sfml) +{ + if (sfml->scene->player->x == sfml->scene->wall->start_x + && sfml->scene->player->y == sfml->scene->wall->start_y) + return; + sfml->scene->wall->x += WALL_SPEED; + if (!(sfml->scene->player->action & PLAYER_DEAD)) + collision_wall(sfml); +} diff --git a/src/game/update_thrower.c b/src/game/update_thrower.c new file mode 100644 index 0000000..537ab28 --- /dev/null +++ b/src/game/update_thrower.c @@ -0,0 +1,94 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include +#include "lib/my_stdlib.h" +#include "game/memory.h" +#include "game/game.h" +#include "game/core.h" + +static int check_object_map(uint8_t *map, int x, int y, int width) +{ + return ((map[(x >> 6) + (y >> 6) * width] == ID_GROUND + || map[(x + 24 >> 6) + (y >> 6) * width] == ID_GROUND + || map[(x + 24 >> 6) + (y + 24 >> 6) * width] == ID_GROUND + || map[(x >> 6) + (y + 24 >> 6) * width] == ID_GROUND) ? 0 : 1); +} + +static void check_collision(sfml_t *sfml, object_t *object) +{ + if (sfml->scene->level->map[(object->x >> 6) + + (object->y >> 6) * sfml->scene->level->width] == ID_GROUND + || sfml->scene->level->map[(object->x + 24 >> 6) + + (object->y >> 6) * sfml->scene->level->width] == ID_GROUND + || sfml->scene->level->map[(object->x >> 6) + + (object->y + 24 >> 6) * sfml->scene->level->width] == ID_GROUND + || sfml->scene->level->map[(object->x + 24>> 6) + + (object->y + 24 >> 6) * sfml->scene->level->width] == ID_GROUND){ + object->y = (object->y >> 6 << 6) + 40; + object->dy = 0; + object->dx = 0; + } +} + +static void update_object(sfml_t *sfml, object_t *object, uint8_t action) +{ + if (object == NULL) + return; + if (action != ID_THROWER_STONE){ + if (!check_object_map(sfml->scene->level->map, + object->x, object->y, sfml->scene->level->width)) + object->sleep = BULLET_STOP; + object->x += object->dx; + object->y += object->dy; + return; + } + object->sleep = (object->sleep + 1) % THROWER_DX_SLEEP; + if (object->dx + && !(object->sleep % (THROWER_DX_SLEEP / my_llabs(object->dx))) + && check_object_map(sfml->scene->level->map, + object->x + sgn(object->dx), object->y, + sfml->scene->level->width)) + object->x += sgn(object->dx); + object->y += object->dy / 4; + object->dy += (object->dy < PLAYER_FALL_MAX) ? 1 : 0; + check_collision(sfml, object); +} + +static void add_new_object(thrower_t *thrower) +{ + int i; + + i = -1; + if (thrower->object[0]) + free(thrower->object[0]); + while (++i < NB_OBJECT - 1) + thrower->object[i] = thrower->object[i + 1]; + thrower->object[NB_OBJECT - 1] = init_object(thrower); +} + +void update_thrower(sfml_t *sfml, thrower_t **thrower, level_t *level) +{ + int i; + int j; + + if (!thrower || level->thrower_counter <= 0) + return; + i = -1; + while (++i < level->thrower_counter){ + if ((thrower[i]->action == ID_THROWER_STONE + && !level->timer_stone) + || (thrower[i]->action != ID_THROWER_STONE + && !level->timer_bullet)) + add_new_object(thrower[i]); + j = -1; + while (++j < NB_OBJECT) + update_object(sfml, thrower[i]->object[j], + thrower[i]->action); + } + level->timer_stone = (level->timer_stone + 1) % THROWER_STONE_SLEEP; + level->timer_bullet = (level->timer_bullet + 1) % THROWER_BULLET_SLEEP; +} diff --git a/src/kinematic/background.c b/src/kinematic/background.c new file mode 100644 index 0000000..ce7d810 --- /dev/null +++ b/src/kinematic/background.c @@ -0,0 +1,32 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include "lib/my_graphics.h" +#include "game/kinematic.h" +#include "game/core.h" +#include "game/draw.h" + +static const uint8_t grass[] = {255, 221, 187, 255, 255, 255, 255, 255}; +void set_background(sfml_t *sfml) +{ + int offset_x = (sfml->scene->camera->x >> 1) & 0x3f; + int offset_y = (sfml->scene->camera->y >> 1) & 0x3f; + int rectangle[4]; + int i; + int j; + + i = -1; + while (++i < BLOCK_LOAD_Y + 1){ + j = -1; + while (++j < BLOCK_LOAD_X + 1){ + rectangle[0] = -offset_x + (j << 6) + 4; + rectangle[1] = -offset_y + (i << 6) + 4; + rectangle[2] = -offset_x + (j << 6) + 60; + rectangle[3] = -offset_y + (i << 6) + 60; + my_filled_rectangle(sfml, rectangle, 0xaaaaaaff); + } + } +} diff --git a/src/kinematic/end.c b/src/kinematic/end.c new file mode 100644 index 0000000..1ef06c3 --- /dev/null +++ b/src/kinematic/end.c @@ -0,0 +1,81 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include +#include "lib/my_graphics.h" +#include "game/kinematic.h" +#include "game/end_data.h" +#include "lib/my_memory.h" +#include "game/message.h" +#include "game/memory.h" +#include "game/ascii.h" +#include "game/draw.h" +#include "game/menu.h" +#include "game/core.h" +#include "game/game.h" + +static void end_dialog(sfml_t *sfml, int *dialog_cursor) +{ + uint32_t *option; + int pos[2]; + + option = my_print_set_option(3, 0x000000ff, 0xffffffff, ASCII_REVERSE); + my_print_set_position(pos, dialog_pos_x[*dialog_cursor], + dialog_pos_y[*dialog_cursor]); + my_print(sfml, pos, dialog[*dialog_cursor], option); + free(option); + if ((!(sfml->key & PRES_LSHIFT) && sfml->key & KEY_LSHIFT) + || (!(sfml->key & PRES_RSHIFT) && sfml->key & KEY_RSHIFT) + || (!(sfml->key & PRES_RIGHT) && sfml->key & KEY_RIGHT) + || (!(sfml->key & PRES_ENTER) && sfml->key & KEY_ENTER) + || (!(sfml->key & PRES_LEFT) && sfml->key & KEY_LEFT) + || (!(sfml->key & PRES_ESC) && sfml->key & KEY_ESC) + || (!(sfml->key & PRES_UP) && sfml->key & KEY_UP) + || (!(sfml->key & PRES_A) && sfml->key & KEY_A) + || (!(sfml->key & PRES_SPACE) && sfml->key & KEY_SPACE)){ + (*dialog_cursor)++; + sfml->key &= ~(KEY_LSHIFT | KEY_RSHIFT | KEY_RIGHT | KEY_LEFT | + KEY_UP | KEY_A | KEY_SPACE | KEY_ESC | KEY_ENTER); + } +} + +static void end_main(sfml_t *sfml, int nb_dead) +{ + int fade_active = 0; + int dialog_cursor = (nb_dead) ? 0 : 5; + int dialog_end = (nb_dead) ? 5 : 9; + + while (sfRenderWindow_isOpen(sfml->window) + && dialog_cursor < dialog_end){ + get_key(sfml); + game_camera_update(sfml->scene, sfml->width, sfml->height); + my_clear_vram(sfml, 0xffffffff); + map_rendering(sfml); + player_rendering(sfml); + end_dialog(sfml, &dialog_cursor); + if (!fade_active) + fade(sfml, FADE_OPEN); + else + my_display_vram(sfml); + fade_active = 1; + set_exit(sfml); + } +} + +void end(sfml_t *sfml, int nb_dead) +{ + sfml->scene = new_scene("src/scene_map/map_end.txt", SCENE_LOAD_PLAYER); + if (sfml == NULL) + return; + sfml->key = 0x00; + end_main(sfml, nb_dead); + fade(sfml, FADE_CLOSE); + free_scene(sfml->scene); + if (nb_dead) + suicide(sfml, nb_dead); + else + text_end(sfml, nb_dead); +} diff --git a/src/kinematic/fade.c b/src/kinematic/fade.c new file mode 100644 index 0000000..74b1d39 --- /dev/null +++ b/src/kinematic/fade.c @@ -0,0 +1,33 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include "lib/my_graphics.h" +#include "game/kinematic.h" +#include "game/core.h" + +void fade(sfml_t *sfml, uint8_t action) +{ + uint8_t *vram = (uint8_t*)sfml->vram; + int32_t fade_alpha; + uint8_t fade_color; + int i; + + fade_alpha = 0xfe; + while (fade_alpha >= 0){ + i = -1; + fade_color = (action & FADE_OPEN) ? 0xff - + fade_alpha : fade_alpha; + while (++i < sfml->width * sfml->height){ + vram[(i << 2)] = (vram[(i << 2)]) ? fade_color : 0; + vram[(i << 2) + 1] = (vram[(i << 2) + 1]) + ? fade_color : 0; + vram[(i << 2) + 2] = (vram[(i << 2) + 2]) + ? fade_color : 0; + } + my_display_vram(sfml); + fade_alpha -= FADE_SPEED; + } +} diff --git a/src/kinematic/intro.c b/src/kinematic/intro.c new file mode 100644 index 0000000..9d658e1 --- /dev/null +++ b/src/kinematic/intro.c @@ -0,0 +1,73 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include +#include "lib/my_graphics.h" +#include "lib/my_memory.h" +#include "game/kinematic.h" +#include "game/message.h" +#include "game/memory.h" +#include "game/draw.h" +#include "game/menu.h" +#include "game/core.h" +#include "game/game.h" +#include "game/ascii.h" + +static void check_skip(sfml_t *sfml, int *exit) +{ + int pos[2] = {0, sfml->height - (ASCII_DEFAULT_HEIGHT << 3) - 16}; + uint32_t *option; + + option = my_print_set_option(3, 0x000000ff, 0xffffffff, + ASCII_REVERSE | ASCII_CENTER_X); + my_print(sfml, pos, "Press A to skip intro.", option); + *exit = check_pause(sfml); + if (sfml->key & KEY_A && !(sfml->key & PRES_A)){ + sfml->key &= ~KEY_A; + *exit = 0; + } + free(option); +} + +static void intro_main(sfml_t *sfml, int *fade_active) +{ + int exit = 1; + + while (sfRenderWindow_isOpen(sfml->window) + && !(sfml->scene->player->action & PLAYER_WIN) && exit){ + get_key(sfml); + game_camera_update(sfml->scene, sfml->width, sfml->height); + my_clear_vram(sfml, 0xffffffff); + map_rendering(sfml); + player_rendering(sfml); + check_skip(sfml, &exit); + check_message(sfml, sfml->scene->message); + if (!*fade_active){ + fade(sfml, FADE_OPEN); + *fade_active = 1; + } + else + my_display_vram(sfml); + if (!(sfml->scene->player->action & PLAYER_DEAD)) + update_player(sfml); + set_exit(sfml); + } +} + +void intro(sfml_t *sfml) +{ + int fade_active; + + sfml->key = 0x00; + sfml->scene = new_scene("src/scene_map/map_intro.txt", + SCENE_LOAD_PLAYER | SCENE_LOAD_MESSAGE_INTRO); + if (sfml->scene == NULL) + return; + fade_active = 0; + intro_main(sfml, &fade_active); + fade(sfml, FADE_CLOSE); + free_scene(sfml->scene); +} diff --git a/src/kinematic/suicide.c b/src/kinematic/suicide.c new file mode 100644 index 0000000..51d5076 --- /dev/null +++ b/src/kinematic/suicide.c @@ -0,0 +1,81 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include +#include "lib/my_graphics.h" +#include "game/kinematic.h" +#include "game/message.h" +#include "game/ascii.h" +#include "game/game.h" +#include "game/core.h" +#include "game/draw.h" +#include "game/menu.h" +#include "game/memory.h" + +static void text_rendering(sfml_t *sfml) +{ + int pos[2] = {0, 16}; + uint32_t *option; + + option = my_print_set_option(3, 0x000000ff, 0xffffffff, + ASCII_CENTER_X | ASCII_REVERSE); + my_print(sfml, pos, "I think that the\nlast thing to do.", option); + free(option); +} + +static void rendering(sfml_t *sfml) +{ + get_key(sfml); + update_thrower(sfml, sfml->scene->thrower, sfml->scene->level); + game_camera_update(sfml->scene, sfml->width, sfml->height); + my_clear_vram(sfml, 0xffffffff); + map_rendering(sfml); + player_rendering(sfml); + draw_thrower_object(sfml); + collision_show(sfml); + text_rendering(sfml); +} + +static int suicide_main(sfml_t *sfml, int *fade_active) +{ + sfml->scene->player->nb_dead = 0; + while (sfRenderWindow_isOpen(sfml->window) + && !(sfml->scene->player->action & PLAYER_WIN) + && !sfml->scene->player->nb_dead){ + rendering(sfml); + if (!*fade_active) + fade(sfml, FADE_OPEN); + else + my_display_vram(sfml); + *fade_active = 1; + if (!(sfml->scene->player->action & PLAYER_DEAD)) + update_player(sfml); + set_exit(sfml); + } + return ((!sfml->scene->player->nb_dead) ? 2 : 1); +} + +void suicide(sfml_t *sfml, int nb_dead) +{ + int fade_active = 0; + int exit = 1; + + sfml->scene = new_scene("src/scene_map/map_suicide.txt", + SCENE_LOAD_PLAYER | SCENE_LOAD_THROWER); + if (sfml->scene == NULL) + return; + while (exit > 0 && sfRenderWindow_isOpen(sfml->window)){ + sfml->key = 0x00; + exit = suicide_main(sfml, &fade_active); + if (exit == 2){ + free_scene(sfml->scene); + end(sfml, 0); + return; + } + exit = text_end(sfml, ++nb_dead); + } + free_scene(sfml->scene); +} diff --git a/src/kinematic/textend.c b/src/kinematic/textend.c new file mode 100644 index 0000000..acd05bf --- /dev/null +++ b/src/kinematic/textend.c @@ -0,0 +1,59 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include +#include "lib/my_graphics.h" +#include "game/kinematic.h" +#include "game/message.h" +#include "game/ascii.h" +#include "game/game.h" +#include "game/core.h" +#include "game/draw.h" +#include "game/menu.h" +#include "game/memory.h" + +static void set_text_suicide(sfml_t *sfml, int nb_dead, uint32_t *option) +{ + int pos[] = {0, sfml->height >> 3}; + + my_print(sfml, pos, "Doh ! Looks like you missed the secret" + " ending...\nPress [A] if you want to find it.\nOr [SPACE] key to" + " return to main menu.\n\nNumber of dead: 0", option); + pos[1] = (sfml->height >> 1) + 250; + pos[0] = 258; + my_print_nbr(sfml, nb_dead, option, pos); +} + +static void set_text_win(sfml_t *sfml, int nb_dead, uint32_t *option) +{ + int pos[] = {0, sfml->height >> 3}; + + my_print(sfml, pos, "...and they lived happily" + " ever after.\nWell, almost.\n\nPress [A] or" + " [SPACE] to return to main menu.", option); +} + +int text_end(sfml_t *sfml, int nb_dead) +{ + void (*set_text[2])(sfml_t*, int, uint32_t*) = { + &set_text_suicide, &set_text_win}; + uint32_t *option; + + sfml->key = 0x00; + option = my_print_set_option(3, 0x000000ff, 0xffffffff, + ASCII_CENTER_X | ASCII_REVERSE | ASCII_CENTER_Y); + my_clear_vram(sfml, 0x000000ff); + draw_endtext_bmp(sfml); + set_text[(nb_dead) ? 0 : 1](sfml, nb_dead, option); + free(option); + fade(sfml, FADE_OPEN); + while (sfRenderWindow_isOpen(sfml->window) + && !((!(sfml->key & PRES_A) && sfml->key & KEY_A) + || (!(sfml->key & PRES_SPACE) && sfml->key & KEY_SPACE))) + get_key(sfml); + fade(sfml, FADE_CLOSE); + return ((!(sfml->key & PRES_A) && sfml->key & KEY_A) ? 1 : 0); +} diff --git a/src/memory/check_map.c b/src/memory/check_map.c new file mode 100644 index 0000000..dea9840 --- /dev/null +++ b/src/memory/check_map.c @@ -0,0 +1,55 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include +#include "game/memory.h" +#include "game/core.h" +#include "lib/my_stdio.h" + +static void draw_debug_map(level_t *level) +{ + int i; + + i = -1; + my_printf("level width: %d - level height: %d\n", level->width, + level->height); + while (++i < level->height){ + write(1, &level->map[i * level->width], level->width); + write(1, "$\n", 2); + } +} + +static int check_entity(scene_t *scene) +{ + int counter_princess = 0; + int counter_player = 0; + int i = -1; + + while (++i < scene->level->width * scene->level->height){ + counter_player += (scene->level->map[i] == ID_PLAYER) ? 1 : 0; + if (scene->level->map[i] == ID_PRINCESS) + counter_princess++; + } + if (counter_player != 1) + write(2, "Player error.\n", 14); + if (counter_princess <= 0) + write(2, "Princess error.\n", 16); + return ((counter_player != 1 || counter_princess <= 0) ? 1 : 0); +} + +int check_map(scene_t *scene) +{ + int i = -1; + + #ifdef DEBUG_LOAD_FILE + draw_debug_map(scene->level); + #endif + if (scene->level->width <= 0 || scene->level->height <= 0){ + write(2, "Map size not correct.\n", 22); + return (1); + } + return (check_entity(scene)); +} diff --git a/src/memory/file.c b/src/memory/file.c new file mode 100644 index 0000000..a88875a --- /dev/null +++ b/src/memory/file.c @@ -0,0 +1,89 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include "game/memory.h" +#include "game/core.h" +#include +#include +#include +#include + +static void fill_line(level_t *level, int x, int y) +{ + while (x < level->width) + level->map[x+++ y * level->width] = (uint8_t)' '; +} + +static void map_cpy(level_t *level, uint8_t *tmp, int size) +{ + int i = 0; + int j = 0; + int x = 0; + int y = 0; + + while (level->file_height +++i < MAP_HEIGHT_MIN) + fill_line(level, x, y++); + i = -1; + while (++i < size){ + if (tmp[i] == '\n'){ + fill_line(level, x, y); + x = 0; + y++; + } + else + level->map[x+++ y * level->width] = (uint8_t)tmp[i]; + } +} + +static void get_info_map(level_t *level, uint8_t *tmp, int size) +{ + int i = -1; + int x = 0; + int y = 0; + + level->width = 0; + level->height = 0; + while (++i < size){ + x++; + x = (tmp[i] == '\n') ? 0 : x; + level->height += (tmp[i] == '\n') ? 1 : 0; + level->width = (level->width > x) ? level->width : x; + } + level->file_width = level->width; + level->file_height = level->height; + if (level->width < MAP_WIDTH_MIN) + level->width = MAP_WIDTH_MIN; + if (level->height < MAP_HEIGHT_MIN) + level->height = MAP_HEIGHT_MIN; +} + +/* Ugly but my getnextline is not stable so */ +/* I use a huge buffer to read all the file */ +/* and malloc "properly". */ +level_t *load_map(const char *file) +{ + uint8_t tmp[1000000]; + level_t *level; + int handle = open(file, O_RDONLY); + int size; + + if (handle < 0){ + write(2, "Error when open map file.\n", 26); + return (NULL); + } + size = read(handle, tmp, 1000000); + if (!size || read(handle, tmp, 1000000)){ + write(2, "Error when read map file.\n", 26); + close(handle); + return (NULL); + } + level = (level_t*)malloc(sizeof(level_t)); + get_info_map(level, tmp, size); + level->map = (uint8_t*)malloc((level->width * level->height) << 2); + map_cpy(level, tmp, size); + close(handle); + return (level); +} diff --git a/src/memory/free_game.c b/src/memory/free_game.c new file mode 100644 index 0000000..05b8778 --- /dev/null +++ b/src/memory/free_game.c @@ -0,0 +1,59 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include +#include "lib/my_memory.h" +#include "game/memory.h" +#include "game/core.h" + +static void free_thrower(thrower_t **thrower, int thrower_counter) +{ + int i; + + if (thrower == NULL) + return; + i = -1; + while (++i < thrower_counter){ + if (thrower[i]->object) + free(thrower[i]->object); + free(thrower[i]); + } + free(thrower); +} + +static void free_message(message_t **message, int nb_message) +{ + int i; + + if (message == NULL) + return; + i = -1; + while (++i < nb_message) + if (message[i]) + free(message[i]); + free(message); +} + +void free_scene(scene_t *scene) +{ + if (scene == NULL) + return; + if (scene->thrower) + free_thrower(scene->thrower, scene->level->thrower_counter); + if (scene->level){ + if (scene->level->map) + free(scene->level->map); + free(scene->level); + } + if (scene->message) + free_message(scene->message, scene->nb_message); + if (scene->player) + free(scene->player); + if (scene->camera) + free(scene->camera); + if (scene->wall) + free(scene->wall); +} diff --git a/src/memory/load_scene.c b/src/memory/load_scene.c new file mode 100644 index 0000000..b77f0d6 --- /dev/null +++ b/src/memory/load_scene.c @@ -0,0 +1,57 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include +#include "lib/my_memory.h" +#include "game/memory.h" +#include "game/game.h" +#include "game/core.h" + +static void active_object(scene_t *scene, uint8_t object) +{ + if (object & SCENE_LOAD_PLAYER) + scene->player = init_player(scene->level); + if (object & SCENE_LOAD_THROWER) + scene->thrower = init_thrower(scene->level); + if (object & SCENE_LOAD_MESSAGE_INTRO) + scene->message = load_message(scene, MESSAGE_INTRO); + if (object & SCENE_LOAD_MESSAGE_GAME) + scene->message = load_message(scene, MESSAGE_GAME); + if (object & SCENE_LOAD_WALL) + scene->wall = init_wall(scene->player); + scene->camera = init_camera(scene); +} + +static void scene_reset(scene_t *scene) +{ + scene->wall = NULL; + scene->player = NULL; + scene->camera = NULL; + scene->message = NULL; + scene->thrower = NULL; +} + +scene_t *new_scene(char *map_name, uint8_t object) +{ + scene_t *scene; + + scene = (scene_t*)malloc(sizeof(scene_t)); + if (scene == NULL) + return (NULL); + scene_reset(scene); + scene->level = load_map(map_name); + if (scene->level != NULL && !check_map(scene)) + active_object(scene, object); + if ((scene->player == NULL && object & SCENE_LOAD_PLAYER) + || (scene->wall == NULL && object & SCENE_LOAD_WALL) + || scene->camera == NULL){ + free_scene(scene); + return (NULL); + } + if (scene->player != NULL) + game_camera_update(scene, SCREEN_WIDTH, SCREEN_HEIGHT); + return (scene); +} diff --git a/src/memory/memory.c b/src/memory/memory.c new file mode 100644 index 0000000..4d8edce --- /dev/null +++ b/src/memory/memory.c @@ -0,0 +1,65 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include +#include "game/memory.h" +#include "game/core.h" + +camera_t *init_camera(scene_t *scene) +{ + camera_t *camera; + + camera = (camera_t*)malloc(sizeof(camera_t)); + camera->x = scene->player->x - (SCREEN_WIDTH >> 1); + camera->y = scene->player->y - (SCREEN_HEIGHT >> 1); + return (camera); +} + +player_t *init_player(level_t *level) +{ + player_t *player; + int i; + + i = -1; + while (++i < level->width * level->height + && level->map[i] != ID_PLAYER); + if (level->map[i] != ID_PLAYER) + return (NULL); + level->map[i] = 0x00; + player = (player_t*)malloc(sizeof(player_t)); + player->x = (i - ((i / level->width) * level->width)) << 6; + player->y = (i / level->width) << 6; + player->start_x = player->x; + player->start_y = player->y; + player->action = PLAYER_IDLE | PLAYER_LEFT; + player->nb_dead = 0; + player->key = 0x00; + player->dx = 0; + player->dy = 0; + return (player); +} + +wall_t *init_wall(player_t *player) +{ + wall_t *wall; + + wall = (wall_t*)malloc(sizeof(wall_t)); + wall->x = player->x - (WALL_SIZE << 2); + wall->start_x = player->x; + wall->start_y = player->y; + return (wall); +} + +sound_t *init_music(char *music_name) +{ + sound_t *sound; + + sound = (sound_t*)malloc(sizeof(sound_t)); + sound->music = sfMusic_createFromFile(music_name); + sound->active = SOUND_ON; + sound->volume = 30; + return (sound); +} diff --git a/src/memory/message.c b/src/memory/message.c new file mode 100644 index 0000000..cc82017 --- /dev/null +++ b/src/memory/message.c @@ -0,0 +1,37 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include +#include "game/message_data.h" +#include "game/memory.h" +#include "game/ascii.h" +#include "game/core.h" + + +message_t **load_message(scene_t *scene, uint8_t type) +{ + message_t **message; + int offset; + int i; + + i = -1; + offset = (type == MESSAGE_GAME) ? 3 : 0; + scene->message_active = 0x00; + scene->nb_message = (type == MESSAGE_GAME) ? 17 : 3; + message = (message_t**)malloc(sizeof(message_t*) + * scene->nb_message); + while (++i < scene->nb_message){ + message[i] = (message_t*)malloc(sizeof(message_t)); + message[i]->trigger_data = default_trigger_start[i + offset]; + message[i]->message = (void*)default_message[i + offset]; + message[i]->action = default_action[i + offset]; + message[i]->timer_message = MESSAGE_DEFAULT_TIME - + ((message[i]->action == MESSAGE_BEGIN) ? 1 : 0); + message[i]->x = 0; + message[i]->y = 0; + } + return (message); +} diff --git a/src/memory/thrower.c b/src/memory/thrower.c new file mode 100644 index 0000000..722b0fc --- /dev/null +++ b/src/memory/thrower.c @@ -0,0 +1,89 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include +#include "game/memory.h" +#include "game/core.h" + +static void init_bullet(object_t *object, thrower_t *thrower) +{ + object->dx = (thrower->action == ID_THROWER_LEFT) ? BULLET_SPEED : 0; + if (thrower->action == ID_THROWER_RIGHT) + object->dx = -BULLET_SPEED; + object->dy = (thrower->action == ID_THROWER_DOWN) ? BULLET_SPEED : 0; + if (thrower->action == ID_THROWER_UP) + object->dy = -BULLET_SPEED; + object->x = thrower->x + 16; + object->y = thrower->y + 16; + object->sleep = 0; +} + +object_t *init_object(thrower_t *thrower) +{ + object_t *object; + + object = (object_t*)malloc(sizeof(object_t)); + if (thrower->action == ID_THROWER_STONE){ + object->dx = (rand() % 9) - 4; + object->dy = 0; + object->x = thrower->x + 16; + object->y = thrower->y + 16; + object->sleep = 0; + return (object); + } + init_bullet(object, thrower); + return (object); +} + +static thrower_t *setup_thrower(int i, int width, uint8_t action) +{ + thrower_t *thrower; + int j; + + j = -1; + thrower = (thrower_t*)malloc(sizeof(thrower_t)); + thrower->object = (object_t**)malloc(sizeof(object_t*) * NB_OBJECT); + while (++j < NB_OBJECT) + thrower->object[j] = NULL; + thrower->x = (i - ((i / width) * width)) << 6; + thrower->y = (i / width) << 6; + thrower->action = action; + return (thrower); +} + +static thrower_t **get_thrower(uint8_t *map, int width, +int height, int thrower_counter) +{ + thrower_t **thrower; + int counter; + int i; + + i = -1; + counter = 0; + thrower = (thrower_t**)malloc(sizeof(thrower_t*) * thrower_counter); + while (++i < width * height && counter < thrower_counter) + if (map[i] >= ID_THROWER_STONE && map[i] <= ID_THROWER_RIGHT) + thrower[counter++] = setup_thrower(i, width, map[i]); + return (thrower); +} + +thrower_t **init_thrower(level_t *level) +{ + int i; + + i = -1; + level->thrower_counter = 0; + while (++i < level->width * level->height) + if (level->map[i] >= ID_THROWER_STONE + && level->map[i] <= ID_THROWER_RIGHT) + level->thrower_counter++; + if (!level->thrower_counter) + return (NULL); + level->timer_stone = 0; + level->timer_bullet = 0; + return (get_thrower(level->map, level->width, + level->height, level->thrower_counter)); +} diff --git a/src/menu/menu.c b/src/menu/menu.c new file mode 100644 index 0000000..8395fab --- /dev/null +++ b/src/menu/menu.c @@ -0,0 +1,17 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include "game/menu.h" +#include "game/core.h" + +int check_pause(sfml_t *sfml) +{ + if (sfml->key & KEY_ESC && !(sfml->key & PRES_ESC)){ + sfml->key &= ~KEY_ESC; + return (menu_pause(sfml)); + } + return (1); +} diff --git a/src/menu/pause.c b/src/menu/pause.c new file mode 100644 index 0000000..cfa8674 --- /dev/null +++ b/src/menu/pause.c @@ -0,0 +1,94 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include +#include "lib/my_graphics.h" +#include "game/menu.h" +#include "game/core.h" +#include "game/ascii.h" + +static void clear_menu(sfml_t *sfml) +{ + int rectangle[5]; + + rectangle[0] = (sfml->width >> 1) - (sfml->width >> 2); + rectangle[1] = (sfml->height >> 1) - (sfml->height >> 2); + rectangle[2] = (sfml->width >> 1) + (sfml->width >> 2); + rectangle[3] = (sfml->height >> 1) + (sfml->height >> 2); + rectangle[4] = 4; + my_rectangle(sfml, rectangle, 0x000000ff, 0xffffffff); +} + +static void text_management(sfml_t *sfml, int *locate) +{ + uint32_t *option; + int pos[2]; + + option = my_print_set_option(3, 0x000000ff, 0xffffffff, 0); + option[3] = (!*locate) ? ASCII_CENTER_X | +ASCII_REVERSE : ASCII_CENTER_X; + my_print_set_position(pos, -(sfml->width >> 3) - 50, + (sfml->height >> 1) + (sfml->height >> 4)); + my_print(sfml, pos, "Exit", option); + my_print_set_position(pos, 0, (sfml->height >> 1) + + (sfml->height >> 4)); + option[3] = (*locate == 1) ? ASCII_CENTER_X | +ASCII_REVERSE : ASCII_CENTER_X; + my_print(sfml, pos, "Return", option); + my_print_set_position(pos, (sfml->width >> 3) + 56, + (sfml->height >> 1) + (sfml->height >> 4)); + option[3] = (*locate == 2) ? ASCII_CENTER_X | +ASCII_REVERSE : ASCII_CENTER_X; + my_print(sfml, pos, "Settings", option); + my_display_vram(sfml); + free(option); +} + +static void key_management(sfml_t *sfml, int *locate, int *exit) +{ + get_key(sfml); + if (sfml->key & KEY_LEFT && !(sfml->key & PRES_LEFT)){ + *locate = (*locate + 1) % 3; + sfml->key &= ~KEY_LEFT; + } + if (sfml->key & KEY_RIGHT && !(sfml->key & PRES_RIGHT)){ + *locate = (*locate + 2) % 3; + sfml->key &= ~KEY_RIGHT; + } + if (sfml->key & KEY_ENTER && !(sfml->key & PRES_ENTER)){ + sfml->key &= ~KEY_ENTER; + *exit = 0; + } + if (sfml->key & KEY_ESC && !(sfml->key & PRES_ESC)){ + sfml->key &= ~KEY_ESC; + *exit = -1; + } +} + +int menu_pause(sfml_t *sfml) +{ + int position[2]; + uint32_t *option; + int locate = 1; + int exit = 1; + + sfml->key &= ~(KEY_ENTER | KEY_LEFT | KEY_RIGHT); + while (exit > 0 && sfRenderWindow_isOpen(sfml->window)){ + clear_menu(sfml); + option = my_print_set_option(4, 0x000000ff, + 0xffffffff, ASCII_CENTER_X | ASCII_CENTER_Y); + my_print_set_position(position, 0, -50); + my_print(sfml, position, "Pause", option); + text_management(sfml, &locate); + key_management(sfml, &locate, &exit); + set_exit(sfml); + free(option); + if (!exit && locate == 2) + menu_settings(sfml); + exit = (!exit && locate == 2) ? 1 : exit; + } + return ((exit == -1) ? 1 : locate); +} diff --git a/src/menu/settings.c b/src/menu/settings.c new file mode 100644 index 0000000..f8cccf1 --- /dev/null +++ b/src/menu/settings.c @@ -0,0 +1,116 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include +#include "lib/my_graphics.h" +#include "game/menu.h" +#include "game/core.h" +#include "game/ascii.h" + +static void (*music_action[2])(sfMusic*) = {&sfMusic_pause, &sfMusic_play}; + +static void clear_menu(sfml_t *sfml) +{ + int rectangle[5]; + + rectangle[0] = (sfml->width >> 1) - (sfml->width >> 2); + rectangle[1] = (sfml->height >> 1) - (sfml->height >> 2); + rectangle[2] = (sfml->width >> 1) + (sfml->width >> 2); + rectangle[3] = (sfml->height >> 1) + (sfml->height >> 2); + rectangle[4] = 4; + my_rectangle(sfml, rectangle, 0x000000ff, 0xffffffff); +} + +static void text_management(sfml_t *sfml, int *locate) +{ + uint32_t *option = my_print_set_option(3, 0x000000ff, 0xffffffff, 0); + int pos[2]; + + option[3] = (!*locate) ? ASCII_CENTER_X | ASCII_REVERSE +: ASCII_CENTER_X; + my_print_set_position(pos, 0, + (sfml->height >> 1) - (sfml->height >> 4)); + my_print(sfml, pos, (sfml->sound->active & SOUND_ON) + ? "Sound: ON" : "Sound:OFF", option); + my_print_set_position(pos, 0, (sfml->height >> 1)); + option[3] = (*locate == 1) ? ASCII_CENTER_X | ASCII_REVERSE +: ASCII_CENTER_X; + my_print(sfml, pos, "Volume: 0", option); + my_print_set_position(pos, 128, (sfml->height >> 1)); + my_print_nbr(sfml, sfml->sound->volume, option, pos); + my_print_set_position(pos, 0, + (sfml->height >> 1) + (sfml->height >> 4)); + option[3] = (*locate == 2) ? ASCII_CENTER_X | ASCII_REVERSE +: ASCII_CENTER_X; + my_print(sfml, pos, "Exit", option); + free(option); +} + +static void key_action(sfml_t *sfml, int *locate, int *exit) +{ + if (sfml->key & KEY_LEFT && !(sfml->key & PRES_LEFT) && *locate != 2){ + if (*locate == 0){ + sfml->sound->active ^= SOUND_ON; + music_action[sfml->sound->active + & SOUND_ON](sfml->sound->music); + } else if (*locate == 1 && sfml->sound->volume + 5 <= 100) + sfml->sound->volume += 5; + sfml->key &= ~KEY_LEFT; + } + if (sfml->key & KEY_RIGHT && !(sfml->key & PRES_RIGHT)){ + if (*locate == 0){ + sfml->sound->active ^= SOUND_ON; + music_action[sfml->sound->active + & SOUND_ON](sfml->sound->music); + } else if (*locate == 1 && sfml->sound->volume - 5 >= 0) + sfml->sound->volume -= 5; + sfml->key &= ~KEY_RIGHT; + } + sfMusic_setVolume(sfml->sound->music, sfml->sound->volume); +} + +static void key_management(sfml_t *sfml, int *locate, int *exit) +{ + if (sfml->key & KEY_DOWN && !(sfml->key & PRES_DOWN)){ + *locate = (*locate + 1) % 3; + sfml->key &= ~KEY_DOWN; + } + if (sfml->key & KEY_UP && !(sfml->key & PRES_UP)){ + *locate = (*locate + 2) % 3; + sfml->key &= ~KEY_UP; + } + if ((sfml->key & KEY_ENTER && !(sfml->key & PRES_ENTER)) + || (sfml->key & KEY_ESC && !(sfml->key & PRES_ESC))){ + sfml->key &= ~(KEY_ENTER | KEY_ESC); + *exit = 0; + } + key_action(sfml, locate, exit); +} + +int menu_settings(sfml_t *sfml) +{ + int position[2]; + uint32_t *option; + int locate = 1; + int exit = 1; + + sfml->key &= ~(KEY_ENTER | KEY_LEFT | KEY_RIGHT); + while (exit > 0 && sfRenderWindow_isOpen(sfml->window)){ + clear_menu(sfml); + option = my_print_set_option(4, 0x000000ff, + 0xffffffff, ASCII_CENTER_X | ASCII_CENTER_Y); + my_print_set_position(position, 0, -(sfml->height >> 3)); + my_print(sfml, position, "Settings", option); + text_management(sfml, &locate); + my_display_vram(sfml); + key_management(sfml, &locate, &exit); + get_key(sfml); + set_exit(sfml); + free(option); + } + locate = (exit == -1) ? 1 : locate; + return (locate); +} diff --git a/src/menu/start.c b/src/menu/start.c new file mode 100644 index 0000000..7acb078 --- /dev/null +++ b/src/menu/start.c @@ -0,0 +1,110 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include +#include "lib/my_graphics.h" +#include "lib/s_sfml.h" +#include "lib/my_stdio.h" +#include "lib/my_memory.h" +#include "game/s_game.h" +#include "game/menu.h" +#include "game/memory.h" +#include "game/game.h" +#include "game/draw.h" +#include "game/core.h" +#include "game/ascii.h" +#include "game/kinematic.h" + +static void key_management(sfml_t *sfml, int *locate, int *exit) +{ + get_key(sfml); + if (sfml->key & KEY_DOWN && !(sfml->key & PRES_DOWN)){ + *locate = (*locate + 1) % 3; + sfml->key &= ~KEY_DOWN; + } + if (sfml->key & KEY_UP && !(sfml->key & PRES_UP)){ + *locate = (*locate + 2) % 3; + sfml->key &= ~KEY_UP; + } + if (sfml->key & KEY_ENTER && !(sfml->key & PRES_ENTER)){ + sfml->key &= ~KEY_ENTER; + if (*locate == 1) + menu_settings(sfml); + else + *exit = 0; + + } +} + +static void choice_management(sfml_t *sfml, uint32_t *option, +int *locate, int *exit) +{ + int pos[2]; + + my_print_set_position(pos, 0, + (sfml->height >> 1) + (sfml->height >> 4)); + option[3] = (!*locate) ? ASCII_CENTER_X | +ASCII_REVERSE : ASCII_CENTER_X; + my_print(sfml, pos, "Play", option); + my_print_set_position(pos, 0, + (sfml->height >> 1) + 2 * (sfml->height >> 4)); + option[3] = (*locate == 1) ? ASCII_CENTER_X | +ASCII_REVERSE : ASCII_CENTER_X; + my_print(sfml, pos, "Settings", option); + my_print_set_position(pos, 0, + (sfml->height >> 1) + 3 * (sfml->height >> 4)); + option[3] = (*locate == 2) ? ASCII_CENTER_X | +ASCII_REVERSE : ASCII_CENTER_X; + my_print(sfml, pos, "Exit", option); + key_management(sfml, locate, exit); +} + +static void display_scene(sfml_t *sfml, int y, int *fade_active) +{ + draw_logo_bmp(sfml, y); + if (!*fade_active){ + fade(sfml, FADE_OPEN); + *fade_active = 1; + } + else + my_display_vram(sfml); + +} + +static void rendering(sfml_t *sfml, uint32_t *option, int *locate, int *exit) +{ + my_clear_vram(sfml, 0xffffffff); + map_rendering(sfml); + player_rendering(sfml); + draw_thrower_object(sfml); + update_thrower(sfml, sfml->scene->thrower, sfml->scene->level); + choice_management(sfml, option, locate, exit); +} + +int menu_start(sfml_t *sfml, int *y) +{ + uint32_t *option = my_print_set_option(3, 0x000000ff, 0xffffffff, 0); + int end = (SCREEN_HEIGHT >> 2) - (BMP_HEIGHT << 3 >> 1); + int fade_active = 0; + int locate = 0; + int exit = 1; + + sfml->key = 0x00; + sfml->scene = new_scene("src/scene_map/map_menu_start.txt", + SCENE_LOAD_PLAYER | SCENE_LOAD_THROWER); + if (sfml->scene == NULL) + return (0); + while (sfRenderWindow_isOpen(sfml->window) && exit){ + rendering(sfml, option, &locate, &exit); + display_scene(sfml, *y, &fade_active); + *y += (*y < end) ? BMP_SPEED : 0; + set_exit(sfml); + } + fade(sfml, FADE_CLOSE); + free(option); + free_scene(sfml->scene); + return (locate); +} diff --git a/src/scene_map/map_end.txt b/src/scene_map/map_end.txt new file mode 100644 index 0000000..dc238f4 --- /dev/null +++ b/src/scene_map/map_end.txt @@ -0,0 +1,18 @@ +111111111111111111111111111111 +111 1111 11111 +1111 1111 +1111 1111 +111 111 +1111 111 +111 11111 +11111 111111 +11111 111111 +11111 111111 +11111 111111 +111111 11111 +111111 11111 +111111 11111 +11111 111111 +11111 111111 +111111 A B 111111 +111111111111111111111111111111 diff --git a/src/scene_map/map_intro.txt b/src/scene_map/map_intro.txt new file mode 100644 index 0000000..047fe36 --- /dev/null +++ b/src/scene_map/map_intro.txt @@ -0,0 +1,18 @@ +11111111111111111111111111111111111111 +11111111111111111111111111111111111111 +1111 1111 11 1111 +111 111 111 +11 11 11 +11 11 +11 11 +11 11 +11 11 +11 11 +11 11 +11 11 +11 11 +11 1 11 +11 1 11 +1111 A 1 1 B 1111 +11111111111111111111111111111111111111 +11111111111111111111111111111111111111 diff --git a/src/scene_map/map_menu_start.txt b/src/scene_map/map_menu_start.txt new file mode 100644 index 0000000..e56f463 --- /dev/null +++ b/src/scene_map/map_menu_start.txt @@ -0,0 +1,18 @@ + + + + + + + + + + + + +11111111 + V + +1 Z11 +1111A 333 B11111 +1111111111111111111111111111111 diff --git a/src/scene_map/map_suicide.txt b/src/scene_map/map_suicide.txt new file mode 100644 index 0000000..4e217b0 --- /dev/null +++ b/src/scene_map/map_suicide.txt @@ -0,0 +1,37 @@ +11111111111111111111111111111111111111111111111111 +11111111111111111111111111111111111111111111111111 +11111111111111111111111111111111111111111111111111 +11111111111111111111111111111111111111111111111111 +11111111111111111111111111111111111111111111111111 +11111111111111111111111111111111111111111111111111 +11111111111111111111111111111111111111111111111111 +11111111111111111111111111111111111111111111111111 +11111111111111111111111111111111111111111111111111 +11111111111111111111111111111111111111111111111111 +11111111111111111111111111111111111111111111111111 +11111111111111111111111111111111111111111111111111 +111111111111111111111 11 +111111111111111111111 11 +111111111111111111111 11 +111111111111111111111 A 11 +11111111111111111111111111111111 11 +11111111111111111111111111111111 11 +11111111111111111111111111111111 11 +11111111111111111111111111111111 11 +11111111111111111111111111111111 11 +11111111111111111111111111111111 11 +1 222222222222222222222222222 11 +1B 222222222222222222222222222 11 +11111111111111111111111111111111 11 +11111111111111111111111111111111 11 +11111111111111111111111111111111 11 +11111111111111111111111111111111 11 +11111111111111111111111111111111 11 +11111111111111111111111111111111 11 +11111111111111111111111111111111 11 +11111111111111111111111111111111 11 +11111111111111111111111111111111Y Z11 +11111111111111111111111111111111Y Z11 +11111111111111111111111111111111 11 +11111111111111111111111111111111333333333333333311 +11111111111111111111111111111111111111111111111111 diff --git a/src/text/ascii.c b/src/text/ascii.c new file mode 100644 index 0000000..bb8d602 --- /dev/null +++ b/src/text/ascii.c @@ -0,0 +1,87 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include +#include "game/ascii_table.h" +#include "lib/my_graphics.h" +#include "lib/my_string.h" +#include "game/ascii.h" + +static void draw_ascii_pixel(sfml_t *sfml, int pos[2], +uint8_t bit, uint32_t *option) +{ + bit ^= (option[3] & ASCII_REVERSE) ? 0xff : 0x00; + if (option[3] & ASCII_ALPHA){ + if (!(bit & 0x01)) + return; + } + my_pixel(sfml, pos[0], pos[1], (bit & 0x01) ? option[1] : option[2]); +} + +static void set_ascii(sfml_t *sfml, int position[2], +uint16_t buffer, uint32_t *option) +{ + int pos[2]; + int i; + int j; + + i = -1; + while (++i < ASCII_DEFAULT_WIDTH << option[0]){ + j = -1; + while (++j < ASCII_DEFAULT_HEIGHT << option[0]){ + pos[0] = position[0] + i; + pos[1] = position[1] + + (ASCII_DEFAULT_WIDTH << option[0]) - j; + draw_ascii_pixel(sfml, pos, ((buffer >> + ((j >> option[0]) + ((i >> option[0])) + * ASCII_DEFAULT_HEIGHT)) & 0x01), option); + } + } +} + +void my_print_set_position(int position[2], int x, int y) +{ + position[0] = x; + position[1] = y; +} + +uint32_t *my_print_set_option(int size, uint32_t color_char, +uint32_t color_alpha, uint8_t action) +{ + uint32_t *option; + + option = (uint32_t*)malloc(4); + option[0] = (uint32_t)size; + option[1] = color_char; + option[2] = color_alpha; + option[3] = (uint32_t)action; + return (option); +} + +void my_print(sfml_t *sfml, int pos1[2], const char *str, uint32_t *option) +{ + ascii_t *ascii = init_set_ascii(str, option); + void *pass[2] = {ascii, sfml}; + int i = -1; + int pos2[2]; + + message_set_start(sfml, ascii, option, ascii->size_str); + my_print_set_position(pos2, pos1[0] + ascii->x, pos1[1] + ascii->y); + reverse_update(pass, pos2, option); + message_set_start(sfml, ascii, option, get_line_size(str, 0)); + while (str[++i] != '\0'){ + pos2[0] = pos1[0] + ascii->x; + ascii->x += ascii->width; + if (str[i] == '\n'){ + pos2[1] += ascii->height + (ascii->offset << 1); + message_set_start(sfml, ascii, option, + get_line_size(str, i + 1)); + } + else + set_ascii(sfml, pos2, + ascii_tab[(uint8_t)str[i]], option); + } +} diff --git a/src/text/message_position.c b/src/text/message_position.c new file mode 100644 index 0000000..8677238 --- /dev/null +++ b/src/text/message_position.c @@ -0,0 +1,75 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include +#include "lib/my_graphics.h" +#include "game/memory.h" +#include "game/ascii.h" + +int get_line_size(const char *str, int pos) +{ + int i; + + i = pos - 1; + while (str[++i] != '\n' && str[i] != '\0'); + return (i - pos); +} + +ascii_t *init_set_ascii(const char *str, uint32_t *option) +{ + ascii_t *ascii; + int size_max; + int i; + + i = -1; + size_max = 0; + ascii = (ascii_t*)malloc(sizeof(ascii_t)); + ascii->nb_line = 1; + ascii->size_str = 0; + while (str[++i] != '\0'){ + ascii->nb_line += (str[i] == '\n') ? 1 : 0; + size_max = (str[i] == '\n') ? 0 : size_max + 1; + ascii->size_str += (size_max > ascii->size_str) ? 1 : 0; + } + ascii->height = (ASCII_DEFAULT_HEIGHT - 1) << option[0]; + ascii->width = ASCII_DEFAULT_WIDTH << option[0]; + ascii->offset = 1 << option[0]; + return (ascii); +} + +void message_set_start(sfml_t *sfml, ascii_t *ascii, +uint32_t *option, int size_str) +{ + ascii->x = 0; + ascii->y = 0; + if (option[3] & ASCII_CENTER_X) + ascii->x = (sfml->width >> 1) - + ((size_str * ascii->width) >> 1); + if (option[3] & ASCII_CENTER_Y){ + ascii->y = (sfml->height >> 1) - + ((ascii->nb_line * ascii->height) >> 1); + option[3] ^= ASCII_CENTER_Y; + } +} + +void reverse_update(void *pass[2], int position[2], uint32_t *option) +{ + ascii_t *ascii; + sfml_t *sfml; + int rect[4]; + + if (!(option[3] & ASCII_REVERSE)) + return; + sfml = (sfml_t*)pass[1]; + ascii = (ascii_t*)pass[0]; + option[3] &= ~ASCII_ALPHA; + rect[0] = position[0] - ascii->offset; + rect[1] = position[1] - (2 << option[0]) + 2; + rect[2] = position[0] + (ascii->size_str * ascii->width); + rect[3] = position[1] + (ascii->nb_line * ascii->height) - 1 + + (ascii->nb_line * (ascii->offset << 1)); + my_filled_rectangle(sfml, rect, option[1]); +} diff --git a/src/text/print_message.c b/src/text/print_message.c new file mode 100644 index 0000000..f265c0e --- /dev/null +++ b/src/text/print_message.c @@ -0,0 +1,61 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include +#include "game/message.h" +#include "game/ascii.h" +#include "game/core.h" + +static void print_message(sfml_t *sfml, message_t *message) +{ + uint32_t *option; + int pos[2]; + + option = my_print_set_option(3, 0x000000ff, + 0xffffffff, ASCII_CENTER_X | ASCII_REVERSE); + pos[0] = message->x; + pos[1] = message->y + 16; + my_print(sfml, pos, message->message, option); + message->timer_message--; + free(option); +} + +static void update_active(sfml_t *sfml) +{ + uint8_t *map = sfml->scene->level->map; + intmax_t width = sfml->scene->level->width; + intmax_t x = sfml->scene->player->x; + intmax_t y = sfml->scene->player->y; + + if (map[(x + 9 >> 6) + (y >> 6) * width] == ID_FAKE_GROUND + || map[(x + 53 >> 6) + (y >> 6) * width] == ID_FAKE_GROUND + || map[(x + 53 >> 6) + (y + 64 >> 6) * width] == ID_FAKE_GROUND + || map[(x + 9 >> 6) + (y + 64 >> 6) * width] == ID_FAKE_GROUND) + sfml->scene->message_active |= MESSAGE_FAKE_GROUND; +} + +void check_message(sfml_t *sfml, message_t **message) +{ + int counter = 0; + int exit = 1; + int i = -1; + + update_active(sfml); + while (++i < sfml->scene->nb_message) + if (message[i]->timer_message > 0 + && (message[i]->timer_message != MESSAGE_DEFAULT_TIME + || (message[i]->action == MESSAGE_DEAD + && message[i]->trigger_data == + sfml->scene->player->nb_dead) + || (message[i]->action == MESSAGE_X_TRIGGER + && sfml->scene->player->x >= message[i]->trigger_data) + || (message[i]->action == MESSAGE_FAKE_GROUND + && sfml->scene->message_active & MESSAGE_FAKE_GROUND) + || (message[i]->action == MESSAGE_FAKE_PRINCESS + && sfml->scene->message_active + & MESSAGE_FAKE_PRINCESS))) + print_message(sfml, message[i]); +} diff --git a/src/text/print_nbr.c b/src/text/print_nbr.c new file mode 100644 index 0000000..e529f96 --- /dev/null +++ b/src/text/print_nbr.c @@ -0,0 +1,19 @@ +/* +** EPITECH PROJECT, 2018 +** task01 +** File description: +** I do task +*/ +#include "game/ascii.h" + +void my_print_nbr(sfml_t *sfml, int nb, uint32_t *option, int pos[2]) +{ + uint8_t buffer; + + if (!nb) + return; + my_print_nbr(sfml, nb / 10, option, pos); + buffer = '0' + nb % 10; + my_print(sfml, pos, (const char*)&buffer, option); + pos[0] += ASCII_DEFAULT_WIDTH << option[0]; +}