#include #include "Graphics.h" #include "Arena1.h" void GameLoop(); void DrawPlasma(); void CyclePalette(); void InitLines(); void DrawLines(); //// variables GsIMAGE ArenaImage; GsSPRITE ArenaSprite; #define NUM_LINES 255 #define SCREEN_WIDTH 256 #define SCREEN_HEIGHT 256 GsGLINE Lines[NUM_LINES]; int nXSpeed[2][NUM_LINES]; int nYSpeed[2][NUM_LINES]; int nCounter; void main() { // Initialise the graphics system InitGraphics( SCREEN_WIDTH, SCREEN_HEIGHT ); SetupSprite( &ArenaSprite, &ArenaImage, (u_long *) &Arena1[4] ); DrawPlasma(); SetupSprite( &ArenaSprite, &ArenaImage, (u_long *) &Arena1[4] ); DrawSync(0); InitLines(); printf("Entering the game loop\n"); GameLoop(); } void GameLoop() // Runs the actual game loop { GsOT * pOrderingTable; while ( TRUE ) { pOrderingTable = NewFrame( TRUE ); CyclePalette(); DrawLines(); GsSortSprite( &ArenaSprite, pOrderingTable, 0); EndFrame(); } } void DrawPlasma() // Draws the initial plasma, over the loaded image { double CosTable[360]; int CosP[4]; int nRow, nColumn, nIndex; u_short * ColourAdd; u_char * Pixel; u_short ColPart; // First build the cosine table for ( nIndex = 0 ; nIndex < 360 ; nIndex ++ ) CosTable[nIndex] = cos((nIndex * 3.1412)/180); // Next, build up the first palette ColourAdd = (unsigned short *) ArenaImage.clut; for ( nIndex = 0 ; nIndex < 2 ; nIndex ++ ) { for ( ColPart = 0 ; ColPart < 32 ; ColPart ++ ) { if ( nIndex == 0 ) *ColourAdd = (u_short) ColPart<<10; else *ColourAdd = (u_short) ColPart << 10 | ColPart ; ColourAdd++; } } // Now build your image CosP[2] = CosP[3] = 150; Pixel = (u_char *) ArenaImage.pixel; for ( nRow = 0 ; nRow < 256 ; nRow ++ ) { CosP[0] = CosP[1] = 32; for ( nColumn = 0 ; nColumn < 320 ; nColumn ++ ) { // Work out your colour *Pixel = ( (u_char) ( (int) (fabs(CosTable[CosP[0]] + CosTable[CosP[1]] + CosTable[CosP[2]] + CosTable[CosP[3]]) * 64)) % 64) & 0xFF; Pixel++; CosP[0]=(CosP[0] + 2) % 360; CosP[1]=(CosP[1] + 3) % 360; } CosP[2]= (CosP[2] + 1) %360; CosP[3]=(CosP[3] + 0) % 360; } } void CyclePalette() { // Cycle only the first 32 colours unsigned short * Col1, *Col2; unsigned short TCol; int nIndex; RECT FrameDestination; Col1= (unsigned short *) ArenaImage.clut; TCol = *Col1; Col2 = Col1++; for ( nIndex = 1 ; nIndex < 64 ; nIndex ++) { *Col2 = *Col1; Col2++; Col1++; } *Col2 = TCol; FrameDestination.x = ArenaImage.cx; FrameDestination.y = ArenaImage.cy; FrameDestination.w = 64 /*ArenaImage.cw*/; FrameDestination.h = ArenaImage.ch; DrawSync(0); LoadImage( &FrameDestination, ArenaImage.clut ); } void InitLines() { int nBlue = 255; int nIndex; int nXS[2], nYS[2]; // Init the counter nCounter = NUM_LINES; srand(3423); for ( nIndex = 0 ; nIndex < 2 ; nIndex ++ ) { nXS[nIndex] = 0; while ( nXS[nIndex] == 0 ) nXS[nIndex] = (rand() % 6) - 3; nYS[nIndex] = 0; while ( nYS[nIndex] == 0) nYS[nIndex] = (rand() % 6) - 3; } // Init the lines for (nIndex = 0 ; nIndex < NUM_LINES ; nIndex ++ ) { Lines[nIndex].x0 = Lines[nIndex].x1 = Lines[nIndex].y0 = Lines[nIndex].y1 = 100; // Speeds; nXSpeed[0][nIndex] = nXS[0]; nYSpeed[0][nIndex] = nYS[0]; nXSpeed[1][nIndex] = nXS[1]; nYSpeed[1][nIndex] = nYS[1]; Lines[nIndex].r0 = Lines[nIndex].g0 = 0; Lines[nIndex].b0 = nBlue; Lines[nIndex].b1 = Lines[nIndex].g1 = 0; Lines[nIndex].r1 = nBlue; Lines[nIndex].attribute = 1<<28 | 1<<30; nBlue -= (255 / NUM_LINES); } } void DrawLines() { int nIndex; GsOT * pOrderingTable; pOrderingTable = CurrentOT(); if ( nCounter > 0 ) nCounter--; for ( nIndex = 0 ; nIndex < NUM_LINES - nCounter ; nIndex ++ ) { if ( Lines[nIndex].x0 + nXSpeed[0][nIndex] < 0 || Lines[nIndex].x0 + nXSpeed[0][nIndex] >= SCREEN_WIDTH ) nXSpeed[0][nIndex] = 0 - nXSpeed[0][nIndex]; if ( Lines[nIndex].x1 + nXSpeed[1][nIndex] < 0 || Lines[nIndex].x1 + nXSpeed[1][nIndex] >= SCREEN_WIDTH ) nXSpeed[1][nIndex] = 0 - nXSpeed[1][nIndex]; if ( Lines[nIndex].y0 + nYSpeed[0][nIndex] < 0 || Lines[nIndex].y0 + nYSpeed[0][nIndex] >= SCREEN_HEIGHT ) nYSpeed[0][nIndex] = 0 - nYSpeed[0][nIndex]; if ( Lines[nIndex].y1 + nYSpeed[1][nIndex] < 0 || Lines[nIndex].y1 + nYSpeed[1][nIndex] >= SCREEN_HEIGHT ) nYSpeed[1][nIndex] = 0 - nYSpeed[1][nIndex]; Lines[nIndex].x0 += nXSpeed[0][nIndex]; Lines[nIndex].x1 += nXSpeed[1][nIndex]; Lines[nIndex].y0 += nYSpeed[0][nIndex]; Lines[nIndex].y1 += nYSpeed[1][nIndex]; GsSortGLine( &Lines[nIndex], pOrderingTable, 0); } }