--- /dev/null
+/*\r
+ * DEMO01 - Sprites, page flipping and palette rotation\r
+ * Copyright (c) 1994 Alessandro Scotti\r
+ */\r
+#include ../../x/modex.h\r
+\r
+#DEFINE MAX_SPRITE 100\r
+\r
+typedef unsigned char byte;\r
+typedef unsigned short word;\r
+typedef unsigned long dword;\r
+\r
+/* Sprite structure */\r
+typedef struct {\r
+ int X, Y; /* Sprite coordinates */\r
+ int DX,DY; /* Deltas for sprite movement */\r
+ int W, H; /* Sprite width and height */\r
+ byte Image[16,16]; /* Sprite image data */\r
+} TSprite;\r
+\r
+/* RGB color structure */\r
+typedef struct {\r
+ byte R, G, B;\r
+} TRgb;\r
+Tsprite S[MAX_SPRITE]; /* An array of sprites */\r
+TRgb Palette[ byte ]; /* Palette */\r
+word Page; /* Page offset */\r
+word i;\r
+\r
+/* Initializes a sprite structure */\r
+void sxInit(TSprite *S)\r
+{\r
+//word i;\r
+\r
+S->X = rand( 320 ); /* Initialize position with random values */\r
+S->Y = rand( 240 );\r
+S->DX = rand( 7 )-3; /* Initialize speed with random values */\r
+S->DY = rand( 7 )-3;\r
+S->W = 16; /* Size is fixed in this program */\r
+S->H = 16;\r
+/* The image is a square with a hole inside */\r
+FillChar( S->Image, SizeOf(S->Image), rand(15)+1 );\r
+ for(i=5; i<=12;i++)\r
+ {\r
+ FillChar( S->Image[ i, 5 ], 8, 0 );\r
+ }\r
+}\r
+\r
+/* Moves a sprite */\r
+void sxMove(TSprite *S)\r
+{\r
+//Inc( S.X, S.DX ); /* Get new position */\r
+//Inc( S.Y, S.DY );\r
+/* Check sprite position, change delta if needed */\r
+if( S->X > 320 ){\r
+ S->X = 320;\r
+S->DX = -S->DX;\r
+}\r
+if( S->X < -16 ){\r
+ S->X = -16;\r
+S->DX = -S->DX;\r
+}\r
+if( S->Y > 240 ){\r
+ S.Y = 240;\r
+ S->DY = -S->DY;\r
+}\r
+if( S->Y < -16 ){\r
+ S->Y = -16;\r
+ S->DY = -S->DY;\r
+}\r
+/* Draw the sprite, note the Page offset added to the */\r
+/* Y coordinate of the image */\r
+mxPutImage( S.Image, S->X, Page+S->Y, S->W, S->H, OP_TRANS );\r
+}\r
+\r
+void main()\r
+{\r
+ int i;\r
+ //TSprite S;\r
+ /* Initialize library */\r
+ mxInit();\r
+ \r
+ /* Enter graphics mode */\r
+ mxSetMode( MX_320x240 );\r
+ \r
+ /* Print initialization message */\r
+ mxSetTextColor( 15, OP_TRANS );\r
+ mxOutStr( 4, 4, 'Initializing...' );\r
+ \r
+ /* Initialize sprites */\r
+ for(i=1;i<=MAX_SPRITE;i++)\r
+ sxInit( &S[i] );\r
+ \r
+ /* Draw background */\r
+ for(i=1;i<=192;i++)\r
+ {\r
+ mxCircle( 160, 480+120, I, I+63 );\r
+ mxCircle( 161, 480+120, I, I+63 );\r
+ }\r
+ \r
+ /* Compute and set palette */\r
+ for(i=1;i<= 192;Palette[i+63])\r
+ {\r
+ short R = 0;\r
+ short G = 0;\r
+ short B = 0;\r
+ if( i < 64 )\r
+ R = i >> 1+31;\r
+ else if( i < 128 )\r
+ G = (i-64) >> 1+31;\r
+ else\r
+ B = (i-128) >> 1+31;\r
+ }\r
+ mxSetPalette( @Palette[64], 64, 192 );\r
+ \r
+ /* Main loop */\r
+ short Page = 240;\r
+ while(!kbhit())\r
+ {\r
+ /* Set clip region to current page */\r
+ mxSetClipRegion( 0, Page, 320, 240 );\r
+ mxSetClip( TRUE );\r
+ /* Restore background */\r
+ mxBitBlt( 0, 480, 320, 240, 0, Page );\r
+ /* Draw sprites */\r
+ for(i=1; i <= MAX_SPRITE; sxMove( S[i] ));\r
+ /* Print message */\r
+ mxOutStr( 4, Page+4, 'Some sprites moving...' );\r
+ /* Flip page */\r
+ mxStartLine( Page );\r
+ Page = 240-Page;\r
+ /* Animate palette */\r
+ mxSetPalette( @Palette[64], 64, 192 );\r
+ mxRotatePalette( @Palette[64], 192, 3 );\r
+ }\r
+ \r
+ mxSetMode( MX_TEXT );\r
+ mxTerm;\r
+ \r
+}\r