miniRT
By Marcelo Magalhães and Ygor G. Sena, 2023.
canvas.h File Reference
#include "libft.h"
#include "mlx.h"
#include "camera.h"
#include "world.h"
Include dependency graph for canvas.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  s_canvas
 The struct of type t_canvas contains the necessary fields for graphic initialization and window rendering. More...
 
struct  s_scene
 The structure t_scene is how the user sees the ray tracing rendering. There is a camera from which the world of shapes and point lights are seen. More...
 

Macros

#define SCREEN_WIDTH   800
 
#define SCREEN_HEIGHT   400
 
#define K_ESCAPE   0xff1b
 
#define K_Q   0x0071
 
#define DESTROYNOTIFY   17
 
#define NOEVENTMASK   0L
 

Typedefs

typedef struct s_canvas t_canvas
 The struct of type t_canvas contains the necessary fields for graphic initialization and window rendering. More...
 
typedef struct s_scene t_scene
 The structure t_scene is how the user sees the ray tracing rendering. There is a camera from which the world of shapes and point lights are seen. More...
 

Functions

void init_mlx_connection (t_canvas *canvas)
 This function attempts to start a connection with Xserver using MLX library. More...
 
t_bool new_canvas (t_canvas *canvas, int width, int height)
 This function attempts to start a connection with Xserver using MLX library. After that, an attempt to create a new image on screen with the new mlx_ptr, width and height is done. Lastly, the function tries to write information on the new image with a given bits per pixel, line length and endianess. More...
 
t_bool put_on_window (t_canvas *canvas, char *title)
 The function tries to create a new window on screen with the established Xserver connection, width, height and window's title passed as parameter. More...
 
int show_window (t_canvas *canvas)
 This function inserts an image to window. To do that, it needs from the parameter canvas a pointer to an existing Xserver connection, a pointer an existing window, a pointer to an existing image and the x and y screen position. The default for x and y is 0. More...
 
int handle_keypress (int keysym, t_canvas *canvas)
 This function is to be used as the second parameter of the MLX library function mlx_key_hook(), which waits for an user event. If the user presses ESC or Q's key, the program is cleanly terminated with quit(). More...
 
int quit (t_canvas *canvas)
 quit() frees every data regarding graphic initialization and window rendering. It destroys the created image and window as well as the pointer to an established Xserver's connection, whose variable must be freed thereafter. More...
 
t_bool render (t_scene *scene, t_canvas *canvas)
 This functions renders a scene of ray casting objects. It attempts to initialize a connection to the graphic library MLX, if successfilly, it proceeds to write pixel by pixel to a image that will be rendered and seen by the user though a MLX's windows. More...
 
void write_pixel (const t_canvas *canvas, int x, int y, int color)
 The function write_pixel() writes a pixel at (x, y) coordinates of the image, if a given coordinate is within canvas width and height. More...
 

Typedef Documentation

◆ t_canvas

typedef struct s_canvas t_canvas

The struct of type t_canvas contains the necessary fields for graphic initialization and window rendering.

ABOUT THE HEADER CANVAS.H

This header contains all functions that works directly or indirectly with graphic information and MLX's library manipulation.

Parameters
mlx_ptrContains the pointer to a connection with the Xserver.
win_ptrContains the pointer to a window created with MLX.
img_ptrContains the pointer to a image created with MLX.
addrStores information about how to write data on pixels from an instance of a MLX's windows and image.
bppContains the bits per pixel value from MLX.
line_lenContains the line length value from MLX.
endianStores the most significant endianess order from MLX.
widthStores the width of the window and the image created with MLX.
heightStores the height of the window and the image created with MLX.

◆ t_scene

typedef struct s_scene t_scene

The structure t_scene is how the user sees the ray tracing rendering. There is a camera from which the world of shapes and point lights are seen.

Parameters
worldStores the world objects to be displayed.
cameraStores the information about the point of view from which the world is observed.

Function Documentation

◆ handle_keypress()

int handle_keypress ( int  keysym,
t_canvas canvas 
)

This function is to be used as the second parameter of the MLX library function mlx_key_hook(), which waits for an user event. If the user presses ESC or Q's key, the program is cleanly terminated with quit().

Parameters
keysymStores the ID of the key pressed by the user. To see all possible values of keysym, refer to the header X11/keysymdef.h.
canvasA struct of type t_canvas containing the necessary fields for graphic initialization and window rendering.
Returns
If the keysym passed as parameter is equal to ESC or Q's key, the program successfully exits. Otherwise, the function cleanly exits with status 0, i.e., EXIT_SUCCESS identifier.

◆ init_mlx_connection()

void init_mlx_connection ( t_canvas canvas)

This function attempts to start a connection with Xserver using MLX library.

Parameters
canvasA struct of type t_canvas containing the necessary fields for graphic initialization and window rendering.
Returns
If connection with Xserver is established succesfully, the function returns nothing. Otherwise, it prints a message to stderr and exits the program.

◆ new_canvas()

t_bool new_canvas ( t_canvas canvas,
int  width,
int  height 
)

This function attempts to start a connection with Xserver using MLX library. After that, an attempt to create a new image on screen with the new mlx_ptr, width and height is done. Lastly, the function tries to write information on the new image with a given bits per pixel, line length and endianess.

Parameters
canvasA struct of type t_canvas containing the necessary fields for graphic initialization and window rendering.
widthThe desired width of the window to be created.
heightThe desired height of the window to be created.
Returns
If the function successfully establishes a Xserver connection, create a new image on screen and write information on it, the function returns TRUE. Otherwise, if one of the aforementioned steps fail, the function returns FALSE.

◆ put_on_window()

t_bool put_on_window ( t_canvas canvas,
char *  title 
)

The function tries to create a new window on screen with the established Xserver connection, width, height and window's title passed as parameter.

Parameters
canvasA struct of type t_canvas containing the necessary fields for graphic initialization and window rendering.
titleThe desired title to describe the window to be created.
Returns
Returns TRUE if new window is created succesfully. Otherwise, it returns FALSE.

◆ quit()

int quit ( t_canvas canvas)

quit() frees every data regarding graphic initialization and window rendering. It destroys the created image and window as well as the pointer to an established Xserver's connection, whose variable must be freed thereafter.

Parameters
canvasA struct of type t_canvas containing the necessary fields for graphic initialization and window rendering.
Returns
After free routine, the function successfully exits with status 0, i.e., EXIT_SUCCESS identifier.

◆ render()

t_bool render ( t_scene scene,
t_canvas canvas 
)

This functions renders a scene of ray casting objects. It attempts to initialize a connection to the graphic library MLX, if successfilly, it proceeds to write pixel by pixel to a image that will be rendered and seen by the user though a MLX's windows.

Parameters
sceneA struct of type t_scene that stores information about the how the world will be seen and which objects of a given world will be observed.
canvasA struct of type t_canvas containing the necessary fields for graphic initialization and window rendering.
Returns
Returns TRUE if a scene is rendered successfully. Otherwise, if the canvas cannot be created, it returns FALSE.

◆ show_window()

int show_window ( t_canvas canvas)

This function inserts an image to window. To do that, it needs from the parameter canvas a pointer to an existing Xserver connection, a pointer an existing window, a pointer to an existing image and the x and y screen position. The default for x and y is 0.

Parameters
canvasA struct of type t_canvas containing the necessary fields for graphic initialization and window rendering.
Returns
The function successfully exits after image is inserted to window.

◆ write_pixel()

void write_pixel ( const t_canvas canvas,
int  x,
int  y,
int  color 
)

The function write_pixel() writes a pixel at (x, y) coordinates of the image, if a given coordinate is within canvas width and height.

Parameters
canvasA struct of type t_canvas containing the necessary fields for graphic initialization and window rendering.
xThe x value of the pixel to be inserted.
yThe y value of the pixel to be inserted.
colorStores the color of the pixel.