#include "miniRT.h"
-int main(void)
+typedef struct s_img
{
+ void *img;
+ char *addr;
+ int bpp;
+ int bpl;
+ int endian;
+ int width;
+ int height;
+} t_img;
+
+typedef struct s_camera
+{
+ t_vec3 pos;
+ t_quaternion orientation;
+ float field_of_view;
+ t_img img;
+}
+
+typedef struct s_session
+{
+ void *mlx;
+ void *win;
+ t_cam camera;
+} t_session;
+
+#include <X11/keysym.h>
+int handle_key_press(int keycode, t_session *s)
+{
+ if (keycode == XK_Escape)
+ close_win(s);
+ else if (keycode == XK_Up || keycode == XK_w)
+ else if (keycode == XK_Left || keycode == XK_a)
+ else if (keycode == XK_Down || keycode == XK_s)
+ else if (keycode == XK_Right || keycode == XK_d)
+ else if (keycode == XK_KP_Add)
+ else if (keycode == XK_KP_Subtract)
+ if (keycode != XK_Escape)
+ draw(s);
+ return (0);
+}
+
+int handle_mouse_press(int button, int x, int y, t_session *s)
+{
+ if (button == Button4)
+ else if (button == Button5)
+ if (button == Button4 || button == Button5)
+ draw(s);
+ return (0);
+}
+
+int no_event_handle(t_session *s)
+{
+ return (0);
+}
+
+void *get_pixel(t_img *img, int x, int y)
+{
+ return (img->addr + y * img->bpl + x * img->bpp / CHAR_BIT);
+}
+
+void ft_putpx_img(t_img *img, int x, int y, t_color c)
+{
+ char *px_addr;
+
+ px_addr = get_pixel(img, x, y);
+ if (img->endian)
+ {
+ px_addr[0] = c.a;
+ px_addr[1] = c.r;
+ px_addr[2] = c.g;
+ px_addr[3] = c.b;
+ }
+ else
+ {
+ px_addr[3] = c.a;
+ px_addr[2] = c.r;
+ px_addr[1] = c.g;
+ px_addr[0] = c.b;
+ }
+ return ;
+}
+void draw(t_session *s)
+{
+ int x;
+ int y;
+ double param;
+
+ x = 0;
+ while (x < s->img.width)
+ {
+ y = 0;
+ while (y < s->img.height)
+ {
+ ft_putpx_img(&s->img, x, y, trace_ray(x, y, &s->cam));
+ ++y;
+ }
+ ++x;
+ }
+ mlx_put_image_to_window(s->mlx, s->win, s->cam.img.img, 0, 0);
+ return ;
+}
+
+void init_session(t_session *s)
+{
+ s->mlx = mlx_init();
+ s->win = mlx_new_window(s->mlx, s->img.width, s->img.height, "miniRT");
+ s->cam.pos = ;
+ s->cam.orientation = ;
+ s->cam.field_of_view = ;
+ s->cam.img.img = mlx_new_image(s->mlx, s->img.width, s->img.height);
+ s->cam.img.addr = mlx_get_data_addr(s->img.img,
+ &s->img.bpp, &s->img.bpl, &s->img.endian);
+}
+
+int close_win(t_session *s)
+{
+ mlx_destroy_window(s->mlx, s->win);
+ s->win = NULL;
+ return (0);
+}
+
+static void free_session(t_session *s)
+{
+ mlx_destroy_display(s->mlx);
+ free(s->mlx);
+ return ;
+}
+
+void cleanup(t_session *s)
+{
+ mlx_destroy_image(s->mlx, s->cam.img.img);
+ free_session(s);
+ return ;
+}
+
+void set_defaults(t_session *s)
+{
+ s->cam.img.width = 1920;
+ s->cam.img.height = 1011;
+
+int main(int argc, char **argv)
+{
+ t_session s;
+
+ set_defaults(&s);
+ if (parse_args(argc, argv, &s))
+ {
+ print_help();
+ return (1);
+ }
+ init_session(&s);
+ mlx_hook(s.win, KeyPress, KeyPressMask, handle_key_press, &s);
+ mlx_hook(s.win, ButtonPress, ButtonPressMask, handle_mouse_press, &s);
+ mlx_hook(s.win, DestroyNotify, NoEventMask, close_win, &s);
+ mlx_loop_hook(s.mlx, no_event_handle, &s);
+ draw(&s);
+ mlx_loop(s.mlx);
+ cleanup(&s);
return (0);
}