Better check for 64-bit.
[AROS-Contrib.git] / Demo / Flamme / flamme.c
blobadce096fd49a5e149e3f0166352205744b7416c5
2 #include <dos/dos.h>
3 #include <intuition/intuition.h>
4 #include <graphics/gfx.h>
5 #include <cybergraphx/cybergraphics.h>
6 #include <proto/exec.h>
7 #include <proto/dos.h>
8 #include <proto/graphics.h>
9 #include <proto/cybergraphics.h>
10 #include <proto/intuition.h>
12 #include <math.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <memory.h>
19 /***********************************************************************************/
21 struct IntuitionBase *IntuitionBase;
22 struct GfxBase *GfxBase;
23 struct Library *CyberGfxBase;
24 struct Screen *scr;
25 struct Window *win;
26 struct RastPort *rp;
27 ULONG cgfx_coltab[256];
28 UBYTE remaptable[256];
29 UBYTE Keys[128];
30 WORD winx = -1, winy = -1;
31 char s[256];
32 BOOL forcescreen, forcewindow;
33 BOOL mustremap, truecolor, remapped, wbscreen = TRUE;
35 /***********************************************************************************/
37 #define W 320
38 #define H 200
40 /***********************************************************************************/
42 static unsigned char *scr1;
43 static unsigned char *scr2;
44 static unsigned char *scr_remapped;
47 void refresh () {
49 int bcl,tmp;
50 unsigned char *src;
51 unsigned char *dst;
52 unsigned char *swp;
54 for ( bcl=0 ; bcl<3*W ; bcl++ )
55 *(scr2+W*H+bcl) = 56 + rand()%40;
57 tmp = 30 + rand()%40;
58 for ( bcl=0 ; bcl<tmp ; bcl++ )
60 dst = scr2 + W*(H+1) + rand()%(W-3);
62 *dst =
63 *(dst+1) =
64 *(dst+2) =
65 *(dst+W) =
66 *(dst+W+1) =
67 *(dst+W+2) =
68 *(dst+2*W+1) =
69 *(dst+2*W+2) =
70 *(dst+2*W+3) = 149;
73 src = scr2 + 2*W;
74 dst = scr1 + W;
76 for ( bcl=0 ; bcl<W*(H+2)-2 ; bcl++ )
79 tmp = *(src+W)
80 + *(src+2*W-1)
81 + *(src+2*W)
82 + *(src+2*W+1);
84 tmp >>= 2;
86 if (tmp != 0)
87 *dst++ = tmp-1;
88 else
89 *dst++ = 0;
91 src++;
95 swp = scr1;
96 scr1 = scr2;
97 scr2 = swp;
99 if (truecolor)
101 WriteLUTPixelArray(scr1,
106 cgfx_coltab,
107 win->BorderLeft,
108 win->BorderTop,
111 CTABFMT_XRGB8);
113 else if (mustremap)
115 LONG i;
116 UBYTE *src = scr1;
117 UBYTE *dest = scr_remapped;
119 for(i = 0; i < W * H; i++)
121 *dest++ = remaptable[*src++];
123 WriteChunkyPixels(rp,
124 win->BorderLeft,
125 win->BorderTop,
126 win->BorderLeft + W - 1,
127 win->BorderTop + H - 1,
128 scr_remapped,
132 else
134 WriteChunkyPixels(rp,
135 win->BorderLeft,
136 win->BorderTop,
137 win->BorderLeft + W - 1,
138 win->BorderTop + H - 1,
139 scr1,
144 void init_colormap() {
146 int i;
147 int r,g,b;
149 for ( i=0 ; i<256 ; i++ )
151 r = g = b = 0 ;
153 if ( (i > 7) && (i < 32) )
154 r = 10 * ( i - 7 );
155 if ( i > 31 )
156 r = 255;
158 if ( (i > 32 ) && (i < 57 ) )
159 g = 10 * ( i - 32 );
160 if ( i > 56 )
161 g = 255;
163 if ( i < 8 )
164 b = 8 * i;
165 if ( (i > 7) && (i < 17) )
166 b = 8 * ( 16 - i );
167 if ( (i > 57) && (i < 82) )
168 b = 10 * ( i - 57 );
169 if ( i > 81 )
170 b = 255;
172 if (truecolor)
174 cgfx_coltab[i] = (r<<16) + (g<<8) + (b);
176 else if (mustremap)
178 ULONG red = r * 0x01010101;
179 ULONG green = g * 0x01010101;
180 ULONG blue = b * 0x01010101;
182 remaptable[i] = ObtainBestPen(scr->ViewPort.ColorMap,
183 red,
184 green,
185 blue,
186 OBP_Precision, PRECISION_IMAGE,
187 OBP_FailIfBad, FALSE,
188 TAG_DONE);
189 remapped = TRUE;
191 else
194 ULONG red = r * 0x01010101;
195 ULONG green = g * 0x01010101;
196 ULONG blue = b * 0x01010101;
198 SetRGB32(&scr->ViewPort, i, red, green, blue);
205 /***********************************************************************************/
207 static void cleanup(char *msg)
210 if (msg)
212 printf("Flamme: %s\n",msg);
215 if (win) CloseWindow(win);
217 if (remapped)
219 WORD i;
221 for(i = 0; i < 256; i++)
223 ReleasePen(scr->ViewPort.ColorMap, remaptable[i]);
227 if (scr)
229 if (wbscreen)
230 UnlockPubScreen(0, scr);
231 else
232 CloseScreen(scr);
235 if (CyberGfxBase) CloseLibrary(CyberGfxBase);
236 if (GfxBase) CloseLibrary((struct Library *)GfxBase);
237 if (IntuitionBase) CloseLibrary((struct Library *)IntuitionBase);
239 exit(0);
242 /***********************************************************************************/
244 #define ARG_TEMPLATE "WINPOSX=X/N/K,WINPOSY=Y/N/K,FORCESCREEN=SCR/S,FORCEWINDOW=WIN/S"
245 #define ARG_X 0
246 #define ARG_Y 1
247 #define ARG_SCR 2
248 #define ARG_WIN 3
249 #define NUM_ARGS 4
251 static IPTR args[NUM_ARGS];
253 static void getarguments(void)
255 struct RDArgs *myargs;
257 if ((myargs = ReadArgs(ARG_TEMPLATE, args, NULL)))
259 if (args[ARG_SCR])
260 forcescreen = TRUE;
261 else if (args[ARG_WIN])
262 forcewindow = TRUE;
264 if (args[ARG_X]) winx = *(IPTR *)args[ARG_X];
265 if (args[ARG_Y]) winy = *(IPTR *)args[ARG_Y];
267 FreeArgs(myargs);
271 /***********************************************************************************/
273 static void openlibs(void)
275 if (!(IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 39)))
277 cleanup("Can't open intuition.library V39!");
280 if (!(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 39)))
282 cleanup("Can't open graphics.library V39!");
285 if (!(CyberGfxBase = OpenLibrary("cybergraphics.library",0)))
287 cleanup("Can't open cybergraphics.library!");
292 /***********************************************************************************/
294 static void getvisual(void)
296 if (!(scr = LockPubScreen(NULL)))
298 cleanup("Can't lock pub screen!");
301 if (GetBitMapAttr(scr->RastPort.BitMap, BMA_DEPTH) <= 8)
303 if (!forcewindow)
305 wbscreen = FALSE;
307 else
309 mustremap = TRUE;
313 if (forcescreen) wbscreen = FALSE;
315 if (!wbscreen)
317 UnlockPubScreen(NULL, scr);
318 wbscreen = FALSE;
320 scr = OpenScreenTags(NULL, SA_Width , W ,
321 SA_Height , H ,
322 SA_Depth , 8 ,
323 TAG_DONE);
324 if (!scr) cleanup("Can't open screen!");
327 truecolor = (GetBitMapAttr(scr->RastPort.BitMap, BMA_DEPTH) >= 15) ? TRUE : FALSE;
330 /***********************************************************************************/
332 static void makewin(void)
334 struct TagItem winonwbtags[] =
336 {WA_DragBar , TRUE },
337 {WA_DepthGadget , TRUE },
338 {WA_CloseGadget , TRUE },
339 {WA_Title , (IPTR)"Flamme" },
340 {TAG_DONE }
343 struct TagItem winonscrtags[] =
345 {WA_Borderless, TRUE },
346 {TAG_DONE }
349 if (winx == -1) winx = (scr->Width - W - scr->WBorLeft - scr->WBorRight) / 2;
350 if (winy == -1) winy = (scr->Height - H - scr->WBorTop - scr->WBorTop - scr->Font->ta_YSize - 1) / 2;
352 win = OpenWindowTags(NULL, WA_CustomScreen , (IPTR)scr,
353 WA_Left , winx,
354 WA_Top , winy,
355 WA_InnerWidth , W,
356 WA_InnerHeight , H,
357 WA_AutoAdjust , TRUE,
358 WA_Activate , TRUE,
359 WA_IDCMP , IDCMP_CLOSEWINDOW |
360 IDCMP_RAWKEY,
361 TAG_MORE , wbscreen ? (IPTR) winonwbtags : (IPTR) winonscrtags);
364 if (!win) cleanup("Can't open window");
366 rp = win->RPort;
369 /***********************************************************************************/
371 #define KC_LEFT 0x4F
372 #define KC_RIGHT 0x4E
373 #define KC_UP 0x4C
374 #define KC_DOWN 0x4D
375 #define KC_ESC 0x45
377 /***********************************************************************************/
379 static void getevents(void)
381 struct IntuiMessage *msg;
383 while ((msg = (struct IntuiMessage *)GetMsg(win->UserPort)))
385 switch(msg->Class)
387 case IDCMP_CLOSEWINDOW:
388 Keys[KC_ESC] = 1;
389 break;
391 case IDCMP_RAWKEY:
393 WORD code = msg->Code & ~IECODE_UP_PREFIX;
395 Keys[code] = (code == msg->Code) ? 1 : 0;
398 break;
401 ReplyMsg((struct Message *)msg);
406 /***********************************************************************************/
408 static void action(void)
410 /* Init Scr */
412 scr1 = (unsigned char *) calloc ( W*(H+4) , 1 );
413 scr2 = (unsigned char *) calloc ( W*(H+4) , 1 );
415 if (!scr1 || !scr2) cleanup("Out of memory!");
417 if (mustremap)
419 scr_remapped = (unsigned char *) calloc (W * H, 1);
420 if (!scr_remapped) cleanup("Out of memory!");
422 /* Init FPS */
424 //init_fps ();
426 /* Boucle */
428 init_colormap();
430 while (!Keys[KC_ESC])
432 getevents();
434 refresh();
435 WaitTOF();
439 /* Aff FPS */
441 //aff_fps (NAME);
444 /***********************************************************************************/
446 int main(void)
448 getarguments();
449 openlibs();
450 getvisual();
451 makewin();
452 action();
453 cleanup(0);
455 return 0;
458 /***********************************************************************************/