aboutsummaryrefslogtreecommitdiffstats
path: root/src/ft_draw_scene.c
blob: 4c7879b2120a815cfaaaa1a09195491b77a50d7f (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
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_draw_scene.c                                    :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: rbousset <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/02/14 17:28:46 by rbousset          #+#    #+#             */
/*   Updated: 2020/02/14 17:28:46 by rbousset         ###   ########lyon.fr   */
/*                                                                            */
/* ************************************************************************** */

#include <libft.h>
#include <cub3d.h>
#include <mlx.h>
#include <stdlib.h>
#include <stdint.h>

static void
	ft_put_fps(t_cub *clist)
{
	mlx_string_put(clist->wlist.wlx,
				   clist->wlist.winptr,
				   10, 20,
				   0x00eeeeee,
				   clist->fps_count);
}

static int8_t
	ft_put_stage(t_cub *clist)
{
	uint8_t		len;
	char		*str;
	float		x;
	uint32_t	y;

	x = 0.5 * clist->mlist.scale;
	y = clist->wlist.y_size - (clist->mlist.map_h * clist->mlist.scale) - 25;
	len = 6 + ft_uintlen(clist->currlvl);
	if (!(str = (char*)malloc((len + 1) * sizeof(char))))
		return (-1);
	ft_sprintf(str, "Stage %hd", clist->currlvl);
	mlx_string_put(clist->wlist.wlx,
					clist->wlist.winptr,
					(uint32_t)x, y,
					0x002288da,
					str);
	ft_memdel((void*)&str);
	return (0);
}

void
	ft_draw_scene(t_cub *clist)
{
	if (FT_OS == 2)
	{
		clist->img.img = mlx_new_image(clist->wlist.wlx,
			clist->wlist.x_size, clist->wlist.y_size);
		clist->img.ptr = mlx_get_data_addr(clist->img.img, &clist->img.bpp,
			&clist->img.sizeline, &clist->img.endian);
	}
	if (clist->doicast)
		ft_castray(clist);
	else
		clist->doicast = 1;
	if (clist->ishud)
		ft_draw_hud(clist);
	mlx_put_image_to_window(clist->wlist.wlx,
		clist->wlist.winptr, clist->img.img, 0, 0);
	if (FT_OS == 2)
		mlx_destroy_image(clist->wlist.wlx, clist->img.img);
	if (clist->ishud)
	{
		ft_put_fps(clist);
		if (clist->mlist.isnlvl && ft_put_stage(clist) < 0)
			ft_alloc_error(clist);
	}
}

void
	ft_draw_scene_bmp(t_cub *clist)
{
	clist->img.img = mlx_new_image(clist->wlist.wlx,
				clist->wlist.x_size, clist->wlist.y_size);
	clist->img.ptr = mlx_get_data_addr(clist->img.img, &clist->img.bpp,
				&clist->img.sizeline, &clist->img.endian);
	ft_castray(clist);
	if (ft_save_to_bmp(clist) < 0)
		ft_error(FT_RET_BMP_ERR, FT_ERR_WR_BMP, clist);
	mlx_destroy_image(clist->wlist.wlx, clist->img.img);
}