-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
149 lines (141 loc) · 3.7 KB
/
main.c
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hjabbour <hjabbour@student.1337.ma> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/26 15:28:53 by stamim #+# #+# */
/* Updated: 2023/02/05 13:08:30 by hjabbour ### ########.fr */
/* */
/* ************************************************************************** */
#include "include/enums.h"
#include "include/declarations.h"
#include "include/macros.h"
#include "include/types.h"
#ifndef PPM
#include "mlx.h"
#endif
#include <math.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/fcntl.h>
#include <unistd.h>
static void sample(const t_scene scn, t_buf *const buf)
{
volatile uint32_t rows;
volatile uint32_t cols;
t_ray ray;
t_color clr;
rows = 0;
while (rows < height)
{
cols = 0;
while (cols < width)
{
ray = ray_for_pixel(scn.cam, rows, cols);
clr = objects_coloring(ray, &scn);
(*buf)[rows][cols] = generate_color(clr);
cols += 1;
}
rows += 1;
}
}
static void init(t_scene *const scn, const int file)
{
rt_parse(scn, file, "");
if (!scn->is_amb || !scn->is_cam || !scn->is_light || !accepted_values(scn))
rt_exit(INVLD_SCN);
#ifndef PPM
scn->mlx = mlx_init();
if (!scn->mlx)
{
(rt_destroy_objs(scn), close(file), exit(EXIT_FAILURE));
}
scn->win = mlx_new_window(scn->mlx, width, height, TITLE);
if (!scn->win)
{
rt_destroy_objs(scn);
close(file);
free(scn->mlx);
exit(EXIT_FAILURE);
}
scn->img = mlx_new_image(scn->mlx, width, height);
if (!scn->img)
{
rt_destroy_objs(scn);
close(file);
mlx_destroy_window(scn->mlx, scn->win);
free(scn->mlx);
exit(EXIT_FAILURE);
}
#endif
}
#ifndef PPM
int destroy(t_scene *scn)
{
rt_destroy_objs(scn);
mlx_destroy_image(scn->mlx, scn->img);
mlx_destroy_window(scn->mlx, scn->win);
free(scn->mlx);
exit(EXIT_SUCCESS);
}
#endif
#ifndef PPM
static int on_keydown(t_keycode key, void *arg)
{
if (key == ESC)
{
destroy(arg);
}
return (1);
}
#endif
int main(const int argc, const char *argv[])
{
const int arg = rt_open(argc, argv[1]);
t_scene scn;
scn.objs = NULL;
scn.is_amb = false;
scn.is_cam = false;
scn.is_light = false;
init(&scn, arg);
#ifndef PPM
mlx_hook(scn.win, ON_DESTROY, 0, destroy, &scn);
mlx_hook(scn.win, ON_KEYDOWN, 0, on_keydown, &scn);
sample(scn,
(t_buf *)mlx_get_data_addr(scn.img, &scn.bpp, &scn.szl, &scn.end));
mlx_put_image_to_window(scn.mlx, scn.win, scn.img, 0, 0);
mlx_loop(scn.mlx);
#else
FILE *const file = fopen("image.ppm", "w");
if (!file)
{
perror("Error opening file.");
rt_destroy_objs(&scn);
close(arg);
exit(EXIT_FAILURE);
}
t_buf *const buf = malloc(sizeof(t_buf));
if (!buf)
{
rt_destroy_objs(&scn);
close(arg);
exit(EXIT_FAILURE);
}
sample(scn, buf);
fprintf(file, "P6\n%d %d\n255\n", width, height);
for (volatile uint32_t rows = 0; rows < height; rows++)
for (volatile uint32_t cols = 0; cols < width; cols++)
{
volatile uint32_t color = (*buf)[rows][cols];
volatile char rgb[3] = {color >> 16, color >> 8, color};
fwrite((void *)&rgb[0], sizeof(char), 1, file);
fwrite((void *)&rgb[1], sizeof(char), 1, file);
fwrite((void *)&rgb[2], sizeof(char), 1, file);
}
fclose(file);
#endif
}