aboutsummaryrefslogtreecommitdiff
path: root/state.c
blob: 139904b79407bf3443e51bdaf00cdd52df738a35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   graphics.c                                         :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: cacharle <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2019/11/19 16:39:57 by cacharle          #+#    #+#             */
/*   Updated: 2020/01/11 10:24:53 by cacharle         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "cub3d.h"

/*
** Initialize the state attributes that weren't already filled by the parsing
*/

t_state	*state_new(t_state *state)
{
	int	i;

	state->running = TRUE;
	if ((state->mlx_ptr = mlx_init()) == NULL)
		return (state_destroy(state));
	if ((state->window_ptr = mlx_new_window(state->mlx_ptr,
			state->window.width, state->window.height, WINDOW_TITLE)) == NULL)
		return (state_destroy(state));
	i = -1;
	while (++i < TEXTURES_NUM)
	{
		load_texture(state->mlx_ptr, &state->textures[i], state->textures_path[i]);
		if (state->textures[i].id == NULL)
			return (state_destroy(state));
	}
	if ((state->window.id = mlx_new_image(state->mlx_ptr,
			state->window.width, state->window.height)) == NULL)
		return (state_destroy(state));
	state->window.data = mlx_get_data_addr(state->window.id,
			&state->window.depth, &state->window.size_line, &state->window.endian);
	state_init_player(state);
	return (state);
}

void	state_init_player(t_state *state)
{
	int	i;
	int j;

	i = -1;
	while (++i < state->map_height)
	{
		j = -1;
		while (++j < state->map_width)
			if (helper_is_player_cell(state->map[i][j]))
			{
				state->pos.x = (double)j + 0.5;
				state->pos.y = (double)i + 0.5;
				// break 2nd loop?
				break;
			}
	}
	/* need to be normalized */
	if (state->map[(int)state->pos.y][(int)state->pos.x] == CELL_LOOK_NORTH)
		state->dir.y = 1.0;
	else if (state->map[(int)state->pos.y][(int)state->pos.x] == CELL_LOOK_SOUTH)
		state->dir.y = -1.0;
	else if (state->map[(int)state->pos.y][(int)state->pos.x] == CELL_LOOK_WEST)
		state->dir.x = -1.0;
	else if (state->map[(int)state->pos.y][(int)state->pos.x] == CELL_LOOK_EAST)
		state->dir.x = 1.0;
	state->plane.x = 0.0;
	state->plane.y = 0.66;
}

t_state	*state_new_empty(void)
{
	int		i;
	t_state	*state;

	if ((state = (t_state*)malloc(sizeof(t_state))) == NULL)
		return (NULL);
	state->mlx_ptr = NULL;
	state->window_ptr = NULL;
	i = -1;
	while (++i < TEXTURES_NUM)
	{
		state->textures_path[i] = NULL;
		state->textures[i].id = NULL;
	}
	state->dir.x = 0.0;
	state->dir.y = 0.0;
	state->map = NULL;
	state->ceilling_color.hexcode = 0x0;
	state->floor_color.hexcode = 0x0;
	return (state);
}

void	*state_destroy(t_state *state)
{
	int	i;

	if (state == NULL)
		return (NULL);
	i = -1;
	while (++i < TEXTURES_NUM)
	{
		free(state->textures_path[i]);
		mlx_destroy_image(state->mlx_ptr, state->textures[i].id);
	}
	if (state->mlx_ptr && state->window_ptr)
		mlx_destroy_window(state->mlx_ptr, state->window_ptr);
	if (state->map != NULL)
		while (state->map_height-- > 0 && state->map[state->map_height] != NULL)
			free(state->map[state->map_height]);
	free(state->map);
	free(state);
	return (NULL);
}

void	load_texture(void *mlx_ptr, t_image *image, char *path)
{
	if ((image->id = mlx_xpm_file_to_image(
			mlx_ptr, path, &image->width, &image->height)) == NULL)
		return ;
	image->data = mlx_get_data_addr(image->id, &image->depth,
									&image->size_line, &image->endian);
}