aboutsummaryrefslogtreecommitdiff
path: root/state.c
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-01-10 11:44:44 +0100
committerCharles <sircharlesaze@gmail.com>2020-01-10 17:53:02 +0100
commit988e058680280e25d345b17d840c3c6d40e30a76 (patch)
tree0cdb0fe6bbf2b3a55edb5347d35aadf6576876c1 /state.c
parent2875f205e24f19964d44ddce2470659d21fe902d (diff)
downloadcub3d-988e058680280e25d345b17d840c3c6d40e30a76.tar.gz
cub3d-988e058680280e25d345b17d840c3c6d40e30a76.tar.bz2
cub3d-988e058680280e25d345b17d840c3c6d40e30a76.zip
refactoring
Diffstat (limited to 'state.c')
-rw-r--r--state.c95
1 files changed, 95 insertions, 0 deletions
diff --git a/state.c b/state.c
new file mode 100644
index 0000000..eb9b3eb
--- /dev/null
+++ b/state.c
@@ -0,0 +1,95 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* graphics.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/11/19 16:39:57 by cacharle #+# #+# */
+/* Updated: 2020/01/10 11:44:24 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "cub3d.h"
+
+t_state *state_new(void *mlx_ptr, void *window_ptr, t_parsing *parsing)
+{
+ t_state *state;
+
+ if ((state = (t_state*)malloc(sizeof(t_state))) == NULL)
+ return (NULL);
+ if ((state->north_texture = load_texture(parsing->north_texture_path)) == NULL)
+ return (NULL);
+ if ((state->south_texture = load_texture(parsing->south_texture_path)) == NULL)
+ return (NULL);
+ if ((state->west_texture = load_texture(parsing->west_texture_path)) == NULL)
+ return (NULL);
+ if ((state->east_texture = load_texture(parsing->east_texture_path)) == NULL)
+ return (NULL);
+
+ state->window_img.id = mlx_new_image(mlx_ptr, parsing->resolution_width, parsing->resolution_height);
+ state->window_img.width = parsing->resolution_width;
+ state->window_img.height = parsing->resolution_height;
+ state->window_img.data = mlx_get_data_addr(state->window_img.id,
+ &state->window_img.depth, &state->window_img.size_line, &state->window_img.endian);
+
+ state->running = TRUE;
+ state->mlx_ptr = mlx_ptr;
+ state->window_ptr = window_ptr;
+ state->window_width = parsing->resolution_width;
+ state->window_height = parsing->resolution_height;
+ state->map = parsing->map;
+ state->map_width = parsing->map_width;
+ state->map_height = parsing->map_height;
+ state->ceilling_color = parsing->ceilling_color;
+ state->floor_color = parsing->floor_color;
+
+ state_new_player(state, parsing);
+
+ return (state);
+}
+
+void state_new_player(t_state *state, t_parsing *parsing)
+{
+ int i;
+ int j;
+
+ i = -1;
+ while (++i < parsing->map_height)
+ {
+ j = -1;
+ while (++j < parsing->map_width)
+ if (parsing->map[i][j] & (CELL_LOOK_NORTH | CELL_LOOK_SOUTH
+ | CELL_LOOK_WEST | CELL_LOOK_EAST)
+ {
+ state->pos.x = (double)j + 0.5;
+ state->pos.y = (double)i + 0.5;
+ // break 2nd loop?
+ break;
+ }
+ }
+ /* need to be normalized */
+ state->dir.x = 0.0;
+ state->dir.y = 0.0;
+ if (parsing->map[(int)state.y][(int)state.x] & CELL_LOOK_NORTH)
+ state->dir.y = 1.0;
+ else if (parsing->map[(int)state.y][(int)state.x] & CELL_LOOK_SOUTH)
+ state->dir.y = -1.0;
+ else if (parsing->map[(int)state.y][(int)state.x] & CELL_LOOK_WEST)
+ state->dir.x = -1.0;
+ else if (parsing->map[(int)state.y][(int)state.x] & CELL_LOOK_EAST)
+ state->dir.x = 1.0;
+
+ state->plane.x = 0.0;
+ state->plane.y = 0.66;
+}
+
+void state_destroy(t_state *state)
+{
+ mlx_destroy_window(state->mlx_ptr, state->window_ptr);
+ free(state->north_texture);
+ free(state->south_texture);
+ free(state->west_texture);
+ free(state->east_texture);
+ free(state);
+}