make.tmpl: add missing compiler attribute to build_progs
[AROS.git] / test / intuition / gamma.c
blob24bfdc3d17661be38ec463d8d77c46f2c3f956af
1 /*
2 Copyright © 2000-2013, The AROS Development Team. All rights reserved.
3 $Id$
5 */
7 /*****************************************************************************************/
9 #include <dos/dos.h>
10 #include <intuition/intuition.h>
11 #include <intuition/gadgetclass.h>
12 #include <intuition/icclass.h>
13 #include <intuition/imageclass.h>
14 #include <intuition/screens.h>
15 #include <gadgets/gradientslider.h>
16 #include <gadgets/colorwheel.h>
17 #include <proto/dos.h>
18 #include <proto/exec.h>
19 #include <proto/intuition.h>
20 #include <proto/colorwheel.h>
21 #include <proto/graphics.h>
22 #include <proto/utility.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
29 * We won't allow to decrease gamma more than down to 128.
30 * Otherwise the playing user might end up in a completely
31 * black screen with zeroed out gamma.
33 #define BOTTOM_LIMIT 128
35 struct Library *GradientSliderBase;
36 struct Library *ColorWheelBase;
38 static struct RDArgs *rda;
39 static struct Screen *scr;
40 static struct DrawInfo *dri;
41 static struct ViewPort *vp;
42 static struct ColorMap *cm;
43 static struct Window *win;
44 static WORD penr = -1, peng = -1, penb = -1;
45 static struct Gadget *rgad, *ggad, *bgad, *wheelgad;
47 static WORD pensr[] = {2, 1, ~0};
48 static WORD pensg[] = {2, 1, ~0};
49 static WORD pensb[] = {2, 1, ~0};
51 struct myargs
53 STRPTR mode;
54 IPTR nogamma;
57 static struct myargs args =
59 NULL,
60 FALSE
63 static UBYTE gamma_r[256];
64 static UBYTE gamma_b[256];
65 static UBYTE gamma_g[256];
67 static void gen_gamma(UBYTE r, UBYTE b, UBYTE g)
69 UWORD i;
71 printf("Generating gamma: %02X %02X %02X\n", r, g, b);
73 for (i = 0; i < 256; i++)
75 gamma_r[i] = (i < r) ? i : r;
76 gamma_b[i] = (i < b) ? i : b;
77 gamma_g[i] = (i < g) ? i : g;
81 /*****************************************************************************************/
83 static void cleanup(char *msg)
85 if (msg) printf("colorwheel: %s\n", msg);
87 if (win)
89 if (wheelgad) RemoveGadget(win, wheelgad);
90 if (rgad) RemoveGadget(win, rgad);
91 if (ggad) RemoveGadget(win, ggad);
92 if (bgad) RemoveGadget(win, bgad);
93 CloseWindow(win);
96 if (wheelgad) DisposeObject((Object *)wheelgad);
97 if (rgad) DisposeObject((Object *)rgad);
98 if (ggad) DisposeObject((Object *)ggad);
99 if (bgad) DisposeObject((Object *)bgad);
101 if (penr != -1) ReleasePen(cm, penr);
102 if (peng != -1) ReleasePen(cm, peng);
103 if (penb != -1) ReleasePen(cm, penb);
105 if (dri) FreeScreenDrawInfo(scr, dri);
106 if (scr) CloseScreen(scr);
108 if (rda) FreeArgs(rda);
110 if (ColorWheelBase) CloseLibrary(ColorWheelBase);
111 if (GradientSliderBase) CloseLibrary(GradientSliderBase);
113 exit(0);
116 /*****************************************************************************************/
118 static void openlibs(void)
120 if (!(GradientSliderBase = OpenLibrary("Gadgets/gradientslider.gadget", 0)))
122 cleanup("Can't open gradientslider.gadget!");
125 if (!(ColorWheelBase = OpenLibrary("Gadgets/colorwheel.gadget", 0)))
127 cleanup("Can't open colorwheel.gadget!");
131 /*****************************************************************************************/
133 static void getvisual(void)
135 struct TagItem tags[] =
137 {SA_LikeWorkbench, TRUE },
138 {SA_GammaControl , TRUE },
139 {SA_GammaRed , (IPTR)gamma_r },
140 {SA_GammaBlue , (IPTR)gamma_b },
141 {SA_GammaGreen , (IPTR)gamma_g },
142 {SA_Title , (IPTR)"Gamma test"},
143 {TAG_DONE , 0 }
146 rda = ReadArgs("MODEID/K,NOGAMMA/S", (IPTR *)&args, NULL);
147 if (!rda)
148 cleanup("Failed to parse arguments!");
150 gen_gamma(255, 255, 255);
152 if (args.mode)
154 tags[0].ti_Tag = SA_DisplayID;
155 tags[0].ti_Data = strtoul(args.mode, NULL, 16);
157 tags[1].ti_Data = !args.nogamma;
159 scr = OpenScreenTagList(NULL, tags);
160 if (!scr)
162 cleanup("Can't open screen!");
165 if (!(dri = GetScreenDrawInfo(scr)))
167 cleanup("Can't get screen drawinfo!");
170 vp = &scr->ViewPort;
171 cm = vp->ColorMap;
173 penr = ObtainBestPen(cm, 0xFFFFFFFF, 0, 0, OBP_Precision, PRECISION_GUI);
174 peng = ObtainBestPen(cm, 0, 0xFFFFFFFF, 0, OBP_Precision, PRECISION_GUI);
175 penb = ObtainBestPen(cm, 0, 0, 0xFFFFFFFF, OBP_Precision, PRECISION_GUI);
177 if (penr != -1) pensr[0] = penr;
178 if (peng != -1) pensg[0] = peng;
179 if (penb != -1) pensb[0] = penb;
182 /*****************************************************************************************/
184 #define BORDERX 4
185 #define BORDERY 4
186 #define SPACINGX 4
187 #define SPACINGY 4
188 #define GRADWIDTH 20
190 static void makegads(void)
192 Object *im;
193 WORD sizeheight = 14;
194 WORD gradx, grady, gradw, gradh;
195 WORD wheelx, wheely, wheelw, wheelh;
197 im = NewObject(NULL, SYSICLASS, SYSIA_DrawInfo, (IPTR)dri, SYSIA_Which, SIZEIMAGE, TAG_DONE);
198 if (im)
200 sizeheight = ((struct Image *)im)->Height;
201 DisposeObject(im);
204 wheelx = scr->WBorLeft + BORDERX;
205 wheely = scr->WBorTop + scr->Font->ta_YSize + 1 + BORDERY;
206 wheelw = -(scr->WBorLeft + scr->WBorRight + BORDERX * 2 + SPACINGX + GRADWIDTH * 3);
207 wheelh = -(scr->WBorTop + scr->Font->ta_YSize + 1 + sizeheight + BORDERY * 2);
209 gradx = -(scr->WBorRight + BORDERX + GRADWIDTH) + 1;
210 grady = scr->WBorTop + scr->Font->ta_YSize + 1 + BORDERY;
211 gradw = GRADWIDTH;
212 gradh = -(scr->WBorTop + scr->Font->ta_YSize + 1 + sizeheight + BORDERY * 2);
214 rgad = (struct Gadget *)NewObject(0, "gradientslider.gadget",
215 GA_RelRight , gradx - gradw * 2,
216 GA_Top , grady,
217 GA_Width , gradw,
218 GA_RelHeight , gradh,
219 GRAD_PenArray , (IPTR)pensr,
220 GRAD_KnobPixels, 10,
221 GRAD_MaxVal , BOTTOM_LIMIT,
222 PGA_Freedom , LORIENT_VERT,
223 ICA_TARGET , ICTARGET_IDCMP,
224 TAG_DONE);
226 if (!rgad) cleanup("Can't create red slider!");
228 ggad = (struct Gadget *)NewObject(0, "gradientslider.gadget",
229 GA_RelRight , gradx - gradw,
230 GA_Top , grady,
231 GA_Width , gradw,
232 GA_RelHeight , gradh,
233 GRAD_PenArray , (IPTR)pensg,
234 GRAD_KnobPixels, 10,
235 GRAD_MaxVal , BOTTOM_LIMIT,
236 PGA_Freedom , LORIENT_VERT,
237 ICA_TARGET , ICTARGET_IDCMP,
238 GA_Previous , rgad,
239 TAG_DONE);
241 if (!ggad) cleanup("Can't create green slider!");
243 bgad = (struct Gadget *)NewObject(0, "gradientslider.gadget",
244 GA_RelRight , gradx,
245 GA_Top , grady,
246 GA_Width , gradw,
247 GA_RelHeight , gradh,
248 GRAD_PenArray , (IPTR)pensb,
249 GRAD_KnobPixels, 10,
250 GRAD_MaxVal , BOTTOM_LIMIT,
251 PGA_Freedom , LORIENT_VERT,
252 ICA_TARGET , ICTARGET_IDCMP,
253 GA_Previous , ggad,
254 TAG_DONE);
256 if (!bgad) cleanup("Can't create blue slider!");
259 wheelgad = (struct Gadget *)NewObject(0, "colorwheel.gadget",
260 GA_Left , wheelx,
261 GA_Top , wheely,
262 GA_RelWidth , wheelw,
263 GA_RelHeight , wheelh,
264 GA_RelVerify , TRUE,
265 WHEEL_Screen , scr,
266 WHEEL_BevelBox, TRUE,
267 GA_Previous , bgad,
268 TAG_DONE);
270 if (!wheelgad) cleanup("Can't create colorwheel gadget!");
274 /*****************************************************************************************/
276 static void makewin(void)
278 win = OpenWindowTags(0,
279 WA_CustomScreen, scr,
280 WA_Left , 10,
281 WA_Top , 20,
282 WA_Width , 220,
283 WA_Height , 190,
284 WA_MinWidth , 50,
285 WA_MinHeight , 50,
286 WA_MaxWidth , 4000,
287 WA_MaxHeight , 4000,
288 WA_AutoAdjust , TRUE,
289 WA_Title , "Gamma test",
290 WA_CloseGadget , TRUE,
291 WA_DragBar , TRUE,
292 WA_DepthGadget , TRUE,
293 WA_SizeGadget , TRUE,
294 WA_SizeBBottom , TRUE,
295 WA_Activate , TRUE,
296 WA_ReportMouse , TRUE,
297 WA_IDCMP , IDCMP_CLOSEWINDOW | IDCMP_IDCMPUPDATE,
298 WA_Gadgets , rgad,
299 TAG_DONE);
301 if (!win) cleanup("Can't open window!");
304 /*****************************************************************************************/
306 static void handleall(void)
308 struct IntuiMessage *msg;
309 BOOL quitme = FALSE;
310 IPTR r, g, b;
312 while(!quitme)
314 WaitPort(win->UserPort);
316 while ((msg = (struct IntuiMessage *)GetMsg(win->UserPort)))
318 switch(msg->Class)
320 case IDCMP_CLOSEWINDOW:
321 quitme = TRUE;
322 break;
324 case IDCMP_IDCMPUPDATE:
325 GetAttr(GRAD_CurVal, (Object *)rgad, &r);
326 GetAttr(GRAD_CurVal, (Object *)ggad, &g);
327 GetAttr(GRAD_CurVal, (Object *)bgad, &b);
329 /* Values are reversed (largest down, smallest up) */
330 gen_gamma(255 - r, 255 - g, 255 - b);
332 SetAttrs(scr,
333 SA_GammaRed , gamma_r,
334 SA_GammaBlue , gamma_b,
335 SA_GammaGreen, gamma_g,
336 TAG_DONE);
337 break;
338 } /* switch(msg->Class) */
340 ReplyMsg((struct Message *)msg);
342 } /* while ((msg = (struct IntuiMessage *)GetMsg(win->UserPort))) */
344 } /* while(!quitme) */
348 /*****************************************************************************************/
350 int main(void)
352 openlibs();
353 getvisual();
354 makegads();
355 makewin();
356 handleall();
357 cleanup(0);
359 return 0;
362 /*****************************************************************************************/