summaryrefslogtreecommitdiff
path: root/game.c
diff options
context:
space:
mode:
Diffstat (limited to 'game.c')
-rw-r--r--game.c125
1 files changed, 125 insertions, 0 deletions
diff --git a/game.c b/game.c
new file mode 100644
index 0000000..96864ab
--- /dev/null
+++ b/game.c
@@ -0,0 +1,125 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* game.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2022/10/09 07:30:46 by kdx #+# #+# */
+/* Updated: 2022/10/16 01:47:03 by kdx ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft/libft.h"
+#include "game.h"
+#include "sily.h"
+#include "sprite.h"
+#include "spritesheet.h"
+#include <stdbool.h>
+
+static t_err load_sprite(t_game *game, t_sprite **dst, char *path, int size)
+{
+ *dst = sprite_load(game->sily, path, size & 255, size / 256);
+ return (*dst == NULL);
+}
+
+static t_err load_tileset(t_game *game, const char *pattern, int size,
+ const char *tiles)
+{
+ int i;
+ char *path;
+ char *path_x;
+ unsigned char tile;
+
+ if (ft_strchr(pattern, 'X') == NULL)
+ return (1);
+ path = ft_strdup(pattern);
+ if (path == NULL)
+ return (1);
+ path_x = ft_strchr(path, 'X');
+ i = -1;
+ while (tiles[++i] != '\0')
+ {
+ tile = tiles[i];
+ *path_x = tile;
+ if (load_sprite(game, &game->s_tiles[tile], path, size))
+ {
+ ft_free(path);
+ return (1);
+ }
+ }
+ ft_free(path);
+ return (0);
+}
+
+static t_game *load_sprites(t_game *game)
+{
+ if (load_tileset(game, "tiles/X.xpm", TSIZE + TSIZE * 256,
+ "01ECabcdefghijklmnop"))
+ return (game_destroy(game));
+ if (load_sprite(game, &game->s_player, "player.xpm", TSIZE + TSIZE * 256))
+ return (game_destroy(game));
+ if (load_sprite(game, &game->s_badeline, "badeline.xpm",
+ TSIZE + TSIZE * 256))
+ return (game_destroy(game));
+ game->ss_exit = spritesheet_load(game->sily, "exit/X.xpm", 3);
+ if (game->ss_exit == NULL)
+ return (game_destroy(game));
+ return (game);
+}
+
+t_game *game_new(const char *map_path)
+{
+ t_game *game;
+ t_err err;
+
+ if (ft_alloc(&game, sizeof(t_game)))
+ return (NULL);
+ err = map_load(&game->map, map_path);
+ if (err)
+ ft_printf("Error\n%s\n", map_load_error(err));
+ if (err)
+ return (game_destroy(game));
+ game->collectibles = map_count(&game->map, TILE_COLLECTIBLE);
+ game->sily = ft_calloc(1, sizeof(t_sily));
+ if (game->sily == NULL)
+ return (game_destroy(game));
+ if (sily_init(game->sily, game->map.width * TSIZE, game->map.height * TSIZE,
+ "SO LONG"))
+ return (game_destroy(game));
+ game->sily->game = game;
+ ft_bzero(game->s_tiles, sizeof(game->s_tiles));
+ if (load_sprites(game) == NULL)
+ return (NULL);
+ map_spawn(game, &game->map);
+ game->badeline.follow = &game->player;
+ game->badeline.active = false;
+ return (game);
+}
+
+void *game_destroy(t_game *game)
+{
+ int i;
+
+ if (game != NULL)
+ {
+ i = 0;
+ spritesheet_destroy(game->sily, game->ss_exit);
+ while (i < 256)
+ {
+ sprite_destroy(game->sily, game->s_tiles[i]);
+ i++;
+ }
+ sprite_destroy(game->sily, game->s_badeline);
+ sprite_destroy(game->sily, game->s_player);
+ if (game->sily)
+ {
+ mlx_do_key_autorepeaton(game->sily->ctx);
+ sily_deinit(game->sily);
+ }
+ ft_free(game->sily);
+ map_destroy(&game->map);
+ ft_free(game);
+ }
+ return (NULL);
+}