summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/draw.c20
-rw-r--r--src/init.c15
-rw-r--r--src/input.c18
-rw-r--r--src/loop.c7
4 files changed, 31 insertions, 29 deletions
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);
}