-
Notifications
You must be signed in to change notification settings - Fork 4
/
example.c
69 lines (66 loc) · 3.18 KB
/
example.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* example.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lmarques <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/04/18 03:51:42 by lmarques #+# #+# */
/* Updated: 2017/04/18 23:27:10 by lmarques ### ########.fr */
/* */
/* ************************************************************************** */
#include "example.h"
int main(void)
{
t_mlx mlx; //Here I first create my struct that will contains all the "MLX stuff"
int count_w;
int count_h;
count_h = -1;
//First you need to call mlx_init and store its return value.
mlx.mlx_ptr = mlx_init();
//Now do the same with mlx_new_window
mlx.win = mlx_new_window(mlx.mlx_ptr, WIN_WIDTH, WIN_HEIGHT, "A simple example");
//One more time with mlx_new_image
mlx.img.img_ptr = mlx_new_image(mlx.mlx_ptr, WIN_WIDTH, WIN_HEIGHT);
/*
Now the important part :
mlx_get_data_addr will return a char* that is 4 times the (width * height) of your image.
Why so ? Let me explain : This char* will represent your image, pixel by pixel,
and the values of this array are the colors. That's why the array is 4 times bigger :
you need 4 char to code the color of each pixels (one for Red, Green and Blue) and one for the alpha.
But... it's not very convenient, right ? So here is my little trick : you cast
mlx_get_data_addr as an int* and store it in an int*.
This way, the array will have the exact same size as your window, and each index
will represent one complete color of a pixel !
*/
mlx.img.data = (int *)mlx_get_data_addr(mlx.img.img_ptr, &mlx.img.bpp, &mlx.img.size_l,
&mlx.img.endian);
/*
Now just a little example : here is a loop that will draw each pixels that
have an odd width in white and the ones that have an even width in black.
*/
while (++count_h < WIN_HEIGHT)
{
count_w = -1;
while (++count_w < WIN_WIDTH)
{
if (count_w % 2)
/*
As you can see here instead of using the mlx_put_pixel function
I just assign a color to each pixel one by one in the image,
and the image will be printed in one time at the end of the loop.
Now one thing to understand here is that you're working on a 1-dimensional
array, while your window is (obviously) 2-dimensional.
So, instead of having data[height][width] here you'll have the following
formula : [current height * max width + current width] (as you can see below)
*/
mlx.img.data[count_h * WIN_WIDTH + count_w] = 0xFFFFFF;
else
mlx.img.data[count_h * WIN_WIDTH + count_w] = 0;
}
}
//Now you just have to print the image using mlx_put_image_to_window !
mlx_put_image_to_window(mlx.mlx_ptr, mlx.win, mlx.img.img_ptr, 0, 0);
mlx_loop(mlx.mlx_ptr);
return (0);
}