forked from 42Paris/minilibx-linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mlx_new_image.c
159 lines (137 loc) · 3.7 KB
/
mlx_new_image.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
150
151
152
153
154
155
156
157
158
159
/*
** mlx_new_image.c for MiniLibX in raytraceur
**
** Made by Charlie Root
** Login <[email protected]>
**
** Started on Mon Aug 14 15:29:14 2000 Charlie Root
** Last update Wed May 25 16:46:31 2011 Olivier Crouzet
*/
#include "mlx_int.h"
/*
** To handle X errors
*/
#define X_ShmAttach 1
int mlx_X_error;
int shm_att_pb(Display *d,XErrorEvent *ev)
{
if (ev->request_code==146 && ev->minor_code==X_ShmAttach)
write(2,WARN_SHM_ATTACH,strlen(WARN_SHM_ATTACH));
mlx_X_error = 1;
}
/*
** Data malloc : width+32 ( bitmap_pad=32 ), *4 = *32 / 8bit
*/
void *mlx_int_new_xshm_image(t_xvar *xvar,int width,int height,int format)
{
t_img *img;
int (*save_handler)();
if (!(img = malloc(sizeof(*img))))
return ((void *)0);
bzero(img,sizeof(*img));
img->data = 0;
img->image = XShmCreateImage(xvar->display,xvar->visual,xvar->depth,
format,img->data,&(img->shm),width,height);
if (!img->image)
{
free(img);
return ((void *)0);
}
img->width = width;
img->height = height;
img->size_line = img->image->bytes_per_line;
img->bpp = img->image->bits_per_pixel;
img->format = format;
img->shm.shmid = shmget(IPC_PRIVATE,(width+32)*height*4,IPC_CREAT|0777);
if (img->shm.shmid==-1)
{
XDestroyImage(img->image);
free(img);
return ((void *)0);
}
img->data = img->shm.shmaddr = img->image->data = shmat(img->shm.shmid,0,0);
if (img->data==(void *)-1)
{
shmctl(img->shm.shmid,IPC_RMID,0);
XDestroyImage(img->image);
free(img);
return ((void *)0);
}
img->shm.readOnly = False;
mlx_X_error = 0;
save_handler = XSetErrorHandler(shm_att_pb);
if (!XShmAttach(xvar->display,&(img->shm)) ||
0&XSync(xvar->display,False) || mlx_X_error)
{
XSetErrorHandler(save_handler);
shmdt(img->data);
shmctl(img->shm.shmid,IPC_RMID,0);
XDestroyImage(img->image);
free(img);
return ((void *)0);
}
XSetErrorHandler(save_handler);
shmctl(img->shm.shmid,IPC_RMID,0);
if (xvar->pshm_format==format)
{
img->pix = XShmCreatePixmap(xvar->display,xvar->root,img->shm.shmaddr,
&(img->shm),width,height,xvar->depth);
img->type = MLX_TYPE_SHM_PIXMAP;
}
else
{
img->pix = XCreatePixmap(xvar->display,xvar->root,
width,height,xvar->depth);
img->type = MLX_TYPE_SHM;
}
if (xvar->do_flush)
XFlush(xvar->display);
return (img);
}
void *mlx_int_new_image(t_xvar *xvar,int width, int height,int format)
{
t_img *img;
if (!(img = malloc(sizeof(*img))))
return ((void *)0);
if (!(img->data = malloc((width+32)*height*4)))
{
free(img);
return ((void *)0);
}
bzero(img->data,(width+32)*height*4);
img->image = XCreateImage(xvar->display,xvar->visual,xvar->depth,format,0,
img->data,width,height,32,0);
if (!img->image)
{
free(img->data);
free(img);
return ((void *)0);
}
img->gc = 0;
img->size_line = img->image->bytes_per_line;
img->bpp = img->image->bits_per_pixel;
img->width = width;
img->height = height;
img->pix = XCreatePixmap(xvar->display,xvar->root,width,height,xvar->depth);
img->format = format;
img->type = MLX_TYPE_XIMAGE;
if (xvar->do_flush)
XFlush(xvar->display);
return (img);
}
void *mlx_new_image(t_xvar *xvar,int width, int height)
{
t_img *img;
if (xvar->use_xshm)
if (img = mlx_int_new_xshm_image(xvar,width,height,ZPixmap))
return (img);
return (mlx_int_new_image(xvar,width,height,ZPixmap));
}
void *mlx_new_image2(t_xvar *xvar,int width, int height)
{
t_img *img;
if (xvar->use_xshm)
if (img = mlx_int_new_xshm_image(xvar,width,height,XYPixmap))
return (img);
return (mlx_int_new_image(xvar,width,height,XYPixmap));
}