summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile2
-rw-r--r--include/so_long.h14
-rw-r--r--src/draw.c20
-rw-r--r--src/init.c15
-rw-r--r--src/input.c18
-rw-r--r--src/loop.c7
-rw-r--r--textures/player.pngbin0 -> 147 bytes
-rw-r--r--textures/player.xpm25
8 files changed, 39 insertions, 62 deletions
diff --git a/Makefile b/Makefile
index 7d5e6c2..d851d7b 100644
--- a/Makefile
+++ b/Makefile
@@ -14,7 +14,7 @@ OBJ := $(addprefix $(OBJ_DIR)/, $(SRC:%.c=%.o))
all: $(NAME)
-$(NAME): libmlx $(OBJ) | libft
+$(NAME): $(OBJ) | libft libmlx
@$(CC) $(CFLAGS) $(HEADERS) $^ -o $@ $(LIBS)
@echo "[$(NAME)] Created binary."
diff --git a/include/so_long.h b/include/so_long.h
index aea3bb8..e4e4f57 100644
--- a/include/so_long.h
+++ b/include/so_long.h
@@ -6,7 +6,7 @@
/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/08 14:14:02 by dkaiser #+# #+# */
-/* Updated: 2024/05/10 10:55:30 by dkaiser ### ########.fr */
+/* Updated: 2024/05/10 11:24:42 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
@@ -15,8 +15,8 @@
# define PLAYER_MOVE_SPEED 3
-# include "libft.h"
# include "../MLX42/include/MLX42/MLX42.h"
+# include "libft.h"
enum e_direction
{
@@ -36,23 +36,23 @@ typedef struct s_vector
typedef struct s_player
{
t_vector position;
- t_vector direction;
+ t_vector direction;
t_vector velocity;
- void *img;
+ mlx_image_t *img;
} t_player;
typedef struct s_game
{
- void *mlx;
+ mlx_t *mlx;
void *window;
t_player player;
int input_direction;
} t_game;
int init(t_game *game);
-int loop(t_game *game);
+void loop(void *game);
int draw(t_game *game);
-int on_key_down(int key, t_game *game);
+void on_key_down(mlx_key_data_t keydata, void *params);
int on_key_up(int keycode, t_game *game);
#endif // SO_LONG_H
diff --git a/src/draw.c b/src/draw.c
index 698e4b3..de4ba48 100644
--- a/src/draw.c
+++ b/src/draw.c
@@ -6,28 +6,16 @@
/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/09 17:58:23 by dkaiser #+# #+# */
-/* Updated: 2024/05/10 10:53:52 by dkaiser ### ########.fr */
+/* Updated: 2024/05/10 11:58:52 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
+#include "libft.h"
#include "so_long.h"
int draw(t_game *game)
{
- mlx_clear_window(game->mlx, game->window);
- if (game->input_direction & UP)
- mlx_string_put(game->mlx, game->window, 150, 100, 0x00FFFFFF, "^");
- if (game->input_direction & DOWN)
- mlx_string_put(game->mlx, game->window, 150, 200, 0x00FFFFFF, "v");
- if (game->input_direction & LEFT)
- mlx_string_put(game->mlx, game->window, 100, 150, 0x00FFFFFF, "<");
- if (game->input_direction & RIGHT)
- mlx_string_put(game->mlx, game->window, 200, 150, 0x00FFFFFF, ">");
- mlx_string_put(game->mlx, game->window, 500, 500, 0x00FFFFFF,
- ft_itoa(game->player.direction.x));
- mlx_string_put(game->mlx, game->window, 550, 500, 0x00FFFFFF,
- ft_itoa(game->player.direction.y));
- mlx_put_image_to_window(game->mlx, game->window, game->player.img,
- game->player.position.x, game->player.position.y);
+ game->player.img->instances[0].x = game->player.position.x;
+ game->player.img->instances[0].y = game->player.position.y;
return (0);
}
diff --git a/src/init.c b/src/init.c
index 55e8fe0..5efec32 100644
--- a/src/init.c
+++ b/src/init.c
@@ -6,7 +6,7 @@
/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/09 14:50:09 by dkaiser #+# #+# */
-/* Updated: 2024/05/10 10:53:44 by dkaiser ### ########.fr */
+/* Updated: 2024/05/10 11:49:11 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
@@ -17,23 +17,24 @@ static void init_player(t_player *player, int x, int y);
int init(t_game *game)
{
- game->mlx = mlx_init();
- game->window = mlx_new_window(game->mlx, 1920, 1080, "so_long");
+ game->mlx = mlx_init(1920, 1080, "so_long", false);
// TODO: make size and title dynamic
game->input_direction = ZERO;
init_hooks(game);
init_player(&game->player, 960, 540);
// TODO: make player spawn point dynamic
- int w, h;
- game->player.img = mlx_xpm_file_to_image(game->mlx, "textures/player.xpm", &w, &h);
+ mlx_texture_t *texture = mlx_load_png("textures/player.png");
+ game->player.img = mlx_texture_to_image(game->mlx, texture);
+ mlx_image_to_window(game->mlx, game->player.img, game->player.position.x, game->player.position.y);
return (0);
}
static void init_hooks(t_game *game)
{
mlx_loop_hook(game->mlx, loop, game);
- mlx_hook(game->window, 2, 0, on_key_down, game);
- mlx_hook(game->window, 3, 0, on_key_up, game);
+ mlx_key_hook(game->mlx, on_key_down, game);
+ /* mlx_hook(game->window, 2, 0, on_key_down, game); */
+ /* mlx_hook(game->window, 3, 0, on_key_up, game); */
}
static void init_player(t_player *player, int x, int y)
diff --git a/src/input.c b/src/input.c
index fe1f219..7582226 100644
--- a/src/input.c
+++ b/src/input.c
@@ -6,14 +6,27 @@
/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/09 15:37:40 by dkaiser #+# #+# */
-/* Updated: 2024/05/09 19:32:18 by dkaiser ### ########.fr */
+/* Updated: 2024/05/10 11:33:09 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
-int on_key_down(int keycode, t_game *game)
+void on_key_down(mlx_key_data_t keydata, void *params)
{
+ t_game *game;
+ int keycode;
+
+ keycode = keydata.os_key;
+
+ game = (t_game *) params;
+
+ if (keydata.action == MLX_RELEASE)
+ {
+ on_key_up(keycode, game);
+ return;
+ }
+
if (keycode == 13 || keycode == 126)
game->input_direction |= UP;
else if (keycode == 0 || keycode == 123)
@@ -22,7 +35,6 @@ int on_key_down(int keycode, t_game *game)
game->input_direction |= DOWN;
else if (keycode == 2 || keycode == 124)
game->input_direction |= RIGHT;
- return (0);
}
int on_key_up(int keycode, t_game *game)
diff --git a/src/loop.c b/src/loop.c
index 69c0712..e994bc0 100644
--- a/src/loop.c
+++ b/src/loop.c
@@ -6,16 +6,18 @@
/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/09 15:09:24 by dkaiser #+# #+# */
-/* Updated: 2024/05/10 10:53:59 by dkaiser ### ########.fr */
+/* Updated: 2024/05/10 11:17:33 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include "so_long.h"
-int loop(t_game *game)
+void loop(void *params)
{
+ t_game *game;
+ game = (t_game *) params;
game->player.direction.x = ((game->input_direction & RIGHT) != 0)
- ((game->input_direction & LEFT) != 0);
game->player.direction.y = ((game->input_direction & DOWN) != 0)
@@ -25,5 +27,4 @@ int loop(t_game *game)
game->player.position.y += game->player.direction.y * PLAYER_MOVE_SPEED;
draw(game);
- return (0);
}
diff --git a/textures/player.png b/textures/player.png
new file mode 100644
index 0000000..03f6517
--- /dev/null
+++ b/textures/player.png
Binary files differ
diff --git a/textures/player.xpm b/textures/player.xpm
deleted file mode 100644
index c2f06b1..0000000
--- a/textures/player.xpm
+++ /dev/null
@@ -1,25 +0,0 @@
-/* XPM */
-static char *fd469a8551244943fd42519ee02c19fdyQYtXZkzcWf5Mc13[] = {
-/* columns rows colors chars-per-pixel */
-"16 16 3 1 ",
-" c black",
-". c #FFF200",
-"X c white",
-/* pixels */
-"XXXXXXXXXXXXXXXX",
-"X..............X",
-"X..............X",
-"X.. .... ..X",
-"X.. .... ..X",
-"X.. .... ..X",
-"X..............X",
-"X..............X",
-"X..............X",
-"X.. ...... ..X",
-"X.. ...... ..X",
-"X.. ..X",
-"X.. ..X",
-"X..............X",
-"X..............X",
-"XXXXXXXXXXXXXXXX"
-};