adjust to match the expanded name.
[AROS-Contrib.git] / Demo / Kitty / main.c
blob60432aabb28bc3c7c8ff61329be0f11e08ebc0bf
1 #include <intuition/intuition.h>
2 #include <cybergraphx/cybergraphics.h>
4 #include <proto/exec.h>
5 #include <proto/graphics.h>
6 #include <proto/intuition.h>
7 #include <proto/cybergraphics.h>
8 #include <proto/alib.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
14 #include "kittysprite_image.c"
16 static struct Screen *scr;
17 static struct Window *win;
18 static struct Region *shape;
19 static Object *draggad;
20 static BOOL remapped, truecolor, shape_active = TRUE;
21 static UBYTE chunky[KITTYSPRITE_WIDTH * KITTYSPRITE_HEIGHT];
22 static UBYTE remaptable[KITTYSPRITE_COLORS];
23 static ULONG cgfx_coltab[KITTYSPRITE_COLORS];
25 static void cleanup(char *msg)
27 if (msg)
29 printf("Kitty: %s\n", msg);
32 if (remapped)
34 WORD i;
36 for(i = 0; i < KITTYSPRITE_COLORS; i++)
38 ReleasePen(scr->ViewPort.ColorMap, remaptable[i]);
42 if (scr) UnlockPubScreen(NULL, scr);
43 if (draggad) DisposeObject(draggad);
44 if (win) CloseWindow(win);
46 exit(0);
49 static void getvisual(void)
51 if (!(scr = LockPubScreen(NULL)))
53 cleanup("Can't lock pub screen!");
56 if (GetBitMapAttr(scr->RastPort.BitMap, BMA_DEPTH) > 8)
58 truecolor = TRUE;
62 #if KITTYSPRITE_PACKED
63 static BOOL unpack_byterun1(const UBYTE *source, UBYTE *dest, LONG unpackedsize)
65 UBYTE r;
66 BYTE c;
68 for(;;)
70 c = (BYTE)(*source++);
71 if (c >= 0)
73 while(c-- >= 0)
75 *dest++ = *source++;
76 if (--unpackedsize <= 0) return FALSE;
79 else if (c != -128)
81 c = -c;
82 r = *source++;
84 while(c-- >= 0)
86 *dest++ = r;
87 if (--unpackedsize <= 0) return FALSE;
92 return TRUE;
94 #endif
96 static void makegfx(void)
98 LONG i;
100 #if KITTYSPRITE_PACKED
101 unpack_byterun1(kittysprite_data, chunky, KITTYSPRITE_WIDTH * KITTYSPRITE_HEIGHT);
102 #else
103 memcpy(chunky, kittysprite_data, KITTYSPRITE_WIDTH * KITTYSPRITE_HEIGHT);
104 #endif
106 shape = NewRegion();
107 if (shape)
109 WORD x, y;
111 for(y = 0; y < KITTYSPRITE_HEIGHT; y++)
113 struct Rectangle rect = {0, y, 0, y};
114 BOOL transp, transpstate = TRUE;
116 for(x = 0; x < KITTYSPRITE_WIDTH; x++)
118 transp = chunky[y * KITTYSPRITE_WIDTH + x] == 0;
120 if (transpstate)
122 if (!transp)
124 rect.MaxX = rect.MinX = x;
125 transpstate = FALSE;
128 else
130 if (transp)
132 OrRectRegion(shape, &rect);
133 transpstate = TRUE;
135 else
137 rect.MaxX++;
142 if (!transpstate) OrRectRegion(shape, &rect);
146 for(i = 0; i < KITTYSPRITE_COLORS; i++)
148 if (truecolor)
150 cgfx_coltab[i] = kittysprite_pal[i];
152 else
154 ULONG r = (kittysprite_pal[i] >> 16) & 0xFF;
155 ULONG g = (kittysprite_pal[i] >> 8) & 0xFF;
156 ULONG b = (kittysprite_pal[i]) & 0xFF;
158 remaptable[i] = ObtainBestPen(scr->ViewPort.ColorMap,
159 r * 0x01010101,
160 g * 0x01010101,
161 b * 0x01010101,
162 OBP_Precision, PRECISION_IMAGE,
163 OBP_FailIfBad, FALSE,
164 TAG_DONE);
169 if (!truecolor)
171 remapped = TRUE;
173 for(i = 0; i < KITTYSPRITE_WIDTH * KITTYSPRITE_HEIGHT; i++)
175 chunky[i] = remaptable[chunky[i]];
181 static void makedraggad(void)
183 draggad = NewObject(NULL, GADGETCLASS, GA_Left, 0,
184 GA_Top, 0,
185 GA_RelWidth, 0,
186 GA_RelHeight, 0,
187 GA_SysGType, GTYP_WDRAGGING,
188 TAG_DONE);
193 static void makewin(void)
195 win = OpenWindowTags(NULL, WA_CustomScreen, (IPTR)scr,
196 WA_Left, 0,
197 WA_Top, 0,
198 WA_Width, KITTYSPRITE_WIDTH,
199 WA_Height, KITTYSPRITE_HEIGHT,
200 WA_Borderless, TRUE,
201 WA_Activate, TRUE,
202 WA_IDCMP, IDCMP_VANILLAKEY,
203 WA_Gadgets, (IPTR)draggad,
204 WA_ShapeRegion, (IPTR)shape,
205 TAG_DONE);
207 if (!win) cleanup("Can't open window!");
210 static void paintwin(void)
212 if (truecolor)
214 WriteLUTPixelArray(chunky,
217 KITTYSPRITE_WIDTH,
218 win->RPort,
219 cgfx_coltab,
222 KITTYSPRITE_WIDTH,
223 KITTYSPRITE_HEIGHT,
224 CTABFMT_XRGB8);
226 else
228 WriteChunkyPixels(win->RPort,
231 KITTYSPRITE_WIDTH - 1,
232 KITTYSPRITE_HEIGHT - 1,
233 chunky,
234 KITTYSPRITE_WIDTH);
238 static void handleall(void)
240 struct IntuiMessage *msg;
241 BOOL quitme = FALSE;
243 while(!quitme)
245 WaitPort(win->UserPort);
246 while((msg = (struct IntuiMessage *)GetMsg(win->UserPort)))
248 switch(msg->Class)
250 case IDCMP_VANILLAKEY:
251 switch(msg->Code)
253 case 'Q':
254 case 'q':
255 case 'X':
256 case 'x':
257 case 27:
258 quitme = TRUE;
259 break;
261 case 'F':
262 case 'f':
263 case 13:
264 WindowToFront(win);
265 break;
267 case 'B':
268 case 'b':
269 case 8:
270 WindowToBack(win);
271 break;
273 case 'S':
274 case 's':
275 case 'M':
276 case 'm':
277 case 32:
278 ChangeWindowShape(win, (shape_active ? NULL: shape), NULL);
279 shape_active = !shape_active;
280 break;
281 } /* switch(msg->Code) */
282 break;
284 } /* switch(msg->Class) */
286 ReplyMsg((struct Message *)msg);
288 } /* while((msg = (struct IntuiMessage *)GetMsg(win->UserPort))) */
290 } /* while(!quitme) */
293 int main(void)
295 getvisual();
296 makegfx();
297 makedraggad();
298 makewin();
299 paintwin();
300 handleall();
301 cleanup(0);
303 return 0;