summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/draw.c21
-rw-r--r--src/init.c19
-rw-r--r--src/input.c49
-rw-r--r--src/loop.c16
-rw-r--r--src/main.c3
5 files changed, 49 insertions, 59 deletions
diff --git a/src/draw.c b/src/draw.c
index ce23842..de4ba48 100644
--- a/src/draw.c
+++ b/src/draw.c
@@ -6,29 +6,16 @@
/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/09 17:58:23 by dkaiser #+# #+# */
-/* Updated: 2024/05/09 19:24:37 by dkaiser ### ########.fr */
+/* Updated: 2024/05/10 11:58:52 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
-#include "mlx.h"
+#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 0aa6664..bd6b48f 100644
--- a/src/init.c
+++ b/src/init.c
@@ -6,11 +6,10 @@
/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/09 14:50:09 by dkaiser #+# #+# */
-/* Updated: 2024/05/09 18:37:18 by dkaiser ### ########.fr */
+/* Updated: 2024/05/10 12:13:33 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
-#include "mlx.h"
#include "so_long.h"
static void init_hooks(t_game *game);
@@ -18,23 +17,27 @@ 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");
+ mlx_texture_t *texture;
+
+ 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);
+ 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_input, 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..a6cb8d4 100644
--- a/src/input.c
+++ b/src/input.c
@@ -6,34 +6,37 @@
/* 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 12:13:22 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
-int on_key_down(int keycode, t_game *game)
+void on_key_input(mlx_key_data_t event, void *params)
{
- if (keycode == 13 || keycode == 126)
- game->input_direction |= UP;
- else if (keycode == 0 || keycode == 123)
- game->input_direction |= LEFT;
- else if (keycode == 1 || keycode == 125)
- game->input_direction |= DOWN;
- else if (keycode == 2 || keycode == 124)
- game->input_direction |= RIGHT;
- return (0);
-}
+ t_game *game;
-int on_key_up(int keycode, t_game *game)
-{
- if (keycode == 13 || keycode == 126)
- game->input_direction &= ~UP;
- else if (keycode == 0 || keycode == 123)
- game->input_direction &= ~LEFT;
- else if (keycode == 1 || keycode == 125)
- game->input_direction &= ~DOWN;
- else if (keycode == 2 || keycode == 124)
- game->input_direction &= ~RIGHT;
- return (0);
+ game = (t_game *)params;
+ if (event.action == MLX_PRESS)
+ {
+ if (event.key == MLX_KEY_W || event.key == MLX_KEY_UP)
+ game->input_direction |= UP;
+ else if (event.key == MLX_KEY_A || event.key == MLX_KEY_LEFT)
+ game->input_direction |= LEFT;
+ else if (event.key == MLX_KEY_S || event.key == MLX_KEY_DOWN)
+ game->input_direction |= DOWN;
+ else if (event.key == MLX_KEY_D || event.key == MLX_KEY_RIGHT)
+ game->input_direction |= RIGHT;
+ }
+ else if (event.action == MLX_RELEASE)
+ {
+ if (event.key == MLX_KEY_W || event.key == MLX_KEY_UP)
+ game->input_direction &= ~UP;
+ else if (event.key == MLX_KEY_A || event.key == MLX_KEY_LEFT)
+ game->input_direction &= ~LEFT;
+ else if (event.key == MLX_KEY_S || event.key == MLX_KEY_DOWN)
+ game->input_direction &= ~DOWN;
+ else if (event.key == MLX_KEY_D || event.key == MLX_KEY_RIGHT)
+ game->input_direction &= ~RIGHT;
+ }
}
diff --git a/src/loop.c b/src/loop.c
index d0d9fe5..6162935 100644
--- a/src/loop.c
+++ b/src/loop.c
@@ -6,25 +6,23 @@
/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/09 15:09:24 by dkaiser #+# #+# */
-/* Updated: 2024/05/09 19:08:51 by dkaiser ### ########.fr */
+/* Updated: 2024/05/10 12:17:03 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
-#include "mlx.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)
- ((game->input_direction & UP) != 0);
-
- game->player.position.x += game->player.direction.x * PLAYER_MOVE_SPEED;
- game->player.position.y += game->player.direction.y * PLAYER_MOVE_SPEED;
-
- draw(game);
- return (0);
+ game->player.position.x += game->player.direction.x * PLAYER_MOVE_SPEED;
+ game->player.position.y += game->player.direction.y * PLAYER_MOVE_SPEED;
+ draw(game);
}
diff --git a/src/main.c b/src/main.c
index db6b524..cf517b6 100644
--- a/src/main.c
+++ b/src/main.c
@@ -6,11 +6,10 @@
/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/08 14:14:13 by dkaiser #+# #+# */
-/* Updated: 2024/05/09 15:15:29 by dkaiser ### ########.fr */
+/* Updated: 2024/05/10 10:53:37 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
-#include "mlx.h"
#include "so_long.h"
int main(void)