aboutsummaryrefslogtreecommitdiffstats
path: root/minilibx_linux/test/main.c
blob: ea132b4913353453d7d172f5f2d03996f5bd5f24 (plain)
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284

#include	"mlx.h"
#include	"mlx_int.h"

#define	WIN1_SX		242
#define	WIN1_SY		242
#define	IM1_SX		42
#define	IM1_SY		42
#define	IM3_SX		242
#define	IM3_SY		242

void	*mlx;
void	*win1;
void    *win2;
void    *win3;
void    *im1;
void	*im2;
void	*im3;
void	*im4;
int	bpp1;
int	bpp2;
int	bpp3;
int	bpp4;
int	sl1;
int	sl2;
int	sl3;
int	sl4;
int	endian1;
int	endian2;
int	endian3;
int	endian4;
char	*data1;
char	*data2;
char	*data3;
char	*data4;
int	xpm1_x;
int	xpm1_y;

int	local_endian;

int	expose_win1(void *p)
{
  mlx_put_image_to_window(mlx,win1,im3,0,0);
}

int	expose_win2(void *p)
{
  mlx_put_image_to_window(mlx,win2,im4,0,0);
  mlx_put_image_to_window(mlx,win2,im2,0,0);
}

int	key_win1(int key,void *p)
{
  printf("Key in Win1 : %d\n",key);
  if (key==0xFF1B)
    exit(0);
}

int	key_win2(int key,void *p)
{
  printf("Key in Win2 : %d\n",key);
  if (key==0xFF1B)
    exit(0);
}

int	key_win3(int key,void *p)
{
  printf("Key in Win3 : %d\n",key);
  if (key==0xFF1B)
    mlx_destroy_window(mlx,win3);
}

int	mouse_win1(int button,int x,int y, void *p)
{
  printf("Mouse in Win1, button %d at %dx%d.\n",button,x,y);
}

int	mouse_win2(int button,int x,int y, void *p)
{
  printf("Mouse in Win2, button %d at %dx%d.\n",button,x,y);
}

int	mouse_win3(int x,int y, void *p)
{
  printf("Mouse moving in Win3, at %dx%d.\n",x,y);
}


int	main()
{
  int	a;

  printf("MinilibX Test Program\n");
  a = 0x11223344;
  if (((unsigned char *)&a)[0] == 0x11)
    local_endian = 1;
  else
    local_endian = 0;
  printf(" => Local Endian : %d\n",local_endian);

  printf(" => Connection ...");
  if (!(mlx = mlx_init()))
    {
      printf(" !! KO !!\n");
      exit(1);
    }
  printf("OK (use_xshm %d pshm_format %d)\n",((t_xvar *)mlx)->use_xshm,((t_xvar *)mlx)->pshm_format);

  printf(" => Window1 %dx%d \"Title 1\" ...",WIN1_SX,WIN1_SY);
  if (!(win1 = mlx_new_window(mlx,WIN1_SX,WIN1_SY,"Title1")))
    {
      printf(" !! KO !!\n");
      exit(1);
    }
  printf("OK\n");

  printf(" => Colormap sans event ...");
  color_map_1(win1,WIN1_SX,WIN1_SY);
  printf("OK\n");
  sleep(2);

  printf(" => Clear Window ...");
  mlx_clear_window(mlx,win1);
  printf("OK\n");
  sleep(2);

  printf(" => Image1 ZPixmap %dx%d ...",IM1_SX,IM1_SY);
  if (!(im1 = mlx_new_image(mlx,IM1_SX,IM1_SY)))
    {
      printf(" !! KO !!\n");
      exit(1);
    }
  data1 = mlx_get_data_addr(im1,&bpp1,&sl1,&endian1);
  printf("OK (bpp1: %d, sizeline1: %d endian: %d type: %d)\n",bpp1,sl1,endian1,
	 ((t_img *)im1)->type);

  printf(" => Fill Image1 ...");
  color_map_2(data1,bpp1,sl1,IM1_SX,IM1_SY,endian1, 1);
  printf("OK (pixmap : %d)\n",(int)((t_img *)im1)->pix);

  printf(" => Put Image1 ...");
  mlx_put_image_to_window(mlx,win1,im1,20,20);
  printf("OK\n");
  sleep(2);

  printf(" => Destroy Image1 ... ");
  mlx_destroy_image(mlx, im1);
  printf("OK\n");
  sleep(2);

  printf(" => Image3 ZPixmap %dx%d ...",IM3_SX,IM3_SY);
  if (!(im3 = mlx_new_image(mlx,IM3_SX,IM3_SY)))
    {
      printf(" !! KO !!\n");
      exit(1);
    }
  data3 = mlx_get_data_addr(im3,&bpp3,&sl3,&endian3);
  printf("OK (bpp3 %d, sizeline3 %d endian3 %d type %d)\n",bpp3,sl3,endian3,
	 ((t_img *)im3)->type);

  printf(" => Fill Image3 ...");
  color_map_2(data3,bpp3,sl3,IM3_SX,IM3_SY,endian3, 1);
  printf("OK (pixmap : %d)\n",(int)((t_img *)im3)->pix);

  printf(" => Put Image3 ...");
  mlx_put_image_to_window(mlx,win1,im3,20,20);
  printf("OK\n");
  sleep(2);

  printf(" => String ...");
  mlx_string_put(mlx,win1,5,WIN1_SY/2,0xFF99FF,"String output");
  mlx_string_put(mlx,win1,15,WIN1_SY/2+20,0x00FFFF,"MinilibX test");
  printf("OK\n");
  sleep(2);

  printf(" => Xpm from file ...");
  if (!(im2 = mlx_xpm_file_to_image(mlx,"open.xpm",&xpm1_x,&xpm1_y)))
    {
      printf(" !! KO !!\n");
      exit(1);
    }
  data2 = mlx_get_data_addr(im2,&bpp2,&sl2,&endian2);
  printf("OK (xpm %dx%d)(img bpp2: %d, sizeline2: %d endian: %d type: %d)\n",
	 xpm1_x,xpm1_y,bpp2,sl2,endian2,((t_img *)im2)->type);
  sleep(2);

  printf(" => Put xpm ...");
  mlx_put_image_to_window(mlx,win1,im2,0,0);
  mlx_put_image_to_window(mlx,win1,im2,100,100);
  printf("OK\n");
  sleep(2);

  printf(" => 2nd window,");
  win2 = mlx_new_window(mlx,WIN1_SX,WIN1_SY,"Title2");
  if (!(im4 = mlx_new_image(mlx,IM3_SX, IM3_SY)))
    {
      printf(" !! KO !!\n");
      exit(1);
    }
  data4 = mlx_get_data_addr(im4,&bpp4,&sl4,&endian4);
  color_map_2(data4,bpp4,sl4,IM3_SX,IM3_SY,endian4, 2);

  printf(" 3rd window, Installing hooks ...");
  win3 = mlx_new_window(mlx,WIN1_SX,WIN1_SY,"Title3");
  mlx_expose_hook(win1,expose_win1,0);
  mlx_mouse_hook(win1,mouse_win1,0);
  mlx_key_hook(win1,key_win1,0);
  mlx_expose_hook(win2,expose_win2,0);
  mlx_mouse_hook(win2,mouse_win2,0);
  mlx_key_hook(win2,key_win2,0);
  mlx_key_hook(win3,key_win3,0);

  mlx_hook(win3, MotionNotify, PointerMotionMask, mouse_win3, 0);

  printf("OK\nNow in Loop. Just play. Esc in 3 to destroy, 1&2 to quit.\n");
  
  mlx_loop(mlx);
}


int	color_map_1(void *win,int w,int h)
{
  int	x;
  int	y;
  int	color;

  x = w;
  while (x--)
    {
      y = h;
      while (y--)
        {
          color = (x*255)/w+((((w-x)*255)/w)<<16)+(((y*255)/h)<<8);
	  mlx_pixel_put(mlx,win,x,y,color);
        }
    }
}


int	color_map_2(unsigned char *data,int bpp,int sl,int w,int h,int endian, int type)
{
  int	x;
  int	y;
  int	opp;
  int	dec;
  int	color;
  int	color2;
  unsigned char *ptr;

  opp = bpp/8;
  printf("(opp : %d) ",opp);
  y = h;
  while (y--)
    {
      ptr = data+y*sl;
      x = w;
      while (x--)
        {
	  if (type==2)
	    color = (y*255)/w+((((w-x)*255)/w)<<16)
	      +(((y*255)/h)<<8);
	  else
	    color = (x*255)/w+((((w-x)*255)/w)<<16)+(((y*255)/h)<<8);
          color2 = mlx_get_color_value(mlx,color);
	  dec = opp;
	  while (dec--)
	    if (endian==local_endian)
	      {
		if (endian)
		  *(ptr+x*opp+dec) = ((unsigned char *)(&color2))[4-opp+dec];
		else
		  *(ptr+x*opp+dec) = ((unsigned char *)(&color2))[dec];
	      }
	    else
	      {
		if (endian)
		  *(ptr+x*opp+dec) = ((unsigned char *)(&color2))[opp-1-dec];
		else
		  *(ptr+x*opp+dec) = ((unsigned char *)(&color2))[3-dec];
	      }
        }
    }

}