Hello World plugin: update()
Jump to navigation
Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
<cpp>
... if ( mFirstTime ) { // Clear previous pixel array
delete [] mBackgroundPixels;
// Create array of pixels
mBackgroundPixels = new unsigned char[ getSurfaceSize() ];
mFirstTime = false;
// Square beginning color squareR = 0; squareG = 0x88; squareB = 0x88; squareWidth = 0x80; squareHeight = 0x80; // Start position of square is 0,0 squareXpos = 0; squareYpos = 0; // Determine the square's initial movement direction randomizeDirection(); }; // Check for browser stop button input if ( mStopAction ) return; if ( time( NULL ) > mLastUpdateTime + 1 ) { // Draw black background int bkgnd_r = 0; int bkgnd_g = 0; int bkgnd_b = 0; for (int bkgnd_pix = 0; bkgnd_pix < getSurfaceSize(); bkgnd_pix = bkgnd_pix+mDepth) { mBackgroundPixels [bkgnd_pix + 0] = bkgnd_r; mBackgroundPixels [bkgnd_pix + 1] = bkgnd_g; mBackgroundPixels [bkgnd_pix + 2] = bkgnd_b; } // Set the color of the moving square squareR = (squareR+1) % (0xFF-0x20) + 0x20; squareG = (squareG+7) % (0xFF-0x20) + 0x20; squareB = (squareB+7) % (0xFF-0x20) + 0x20; time( &mLastUpdateTime ); } memcpy( mPixels, mBackgroundPixels, getSurfaceSize() );
// Black out last position of square for ( int xcnt = 0; xcnt < squareWidth; ++xcnt ) { for ( int ycnt = 0; ycnt < squareHeight; ++ycnt ) { mPixels [ (squareXpos + xcnt)*mDepth + (squareYpos + ycnt)*mWidth*mDepth + 0 ] = 0; mPixels [ (squareXpos + xcnt)*mDepth + (squareYpos + ycnt)*mWidth*mDepth + 1 ] = 0; mPixels [ (squareXpos + xcnt)*mDepth + (squareYpos + ycnt)*mWidth*mDepth + 2 ] = 0; } }
// Calculate the new position of the moving square. Note: (0,0) is the top left corner. // if (rand() % 400 == 0) // only change direction once in a while { randomizeDirection(); } if ((squareXpos + squareXinc < 0) || (squareXpos + squareXinc >= mWidth - squareWidth)) squareXinc = -squareXinc; if ((squareYpos + squareYinc < 0) || (squareYpos + squareYinc >= mHeight - squareHeight)) squareYinc = -squareYinc; squareXpos += squareXinc; squareYpos += squareYinc; // Draw square for ( int xcnt = 0; xcnt < squareWidth; ++xcnt ) { for ( int ycnt = 0; ycnt < squareHeight; ++ycnt ) { mPixels [ (squareXpos + xcnt)*mDepth + (squareYpos + ycnt)*mWidth*mDepth + 0 ] = squareR; mPixels [ (squareXpos + xcnt)*mDepth + (squareYpos + ycnt)*mWidth*mDepth + 1 ] = squareG; mPixels [ (squareXpos + xcnt)*mDepth + (squareYpos + ycnt)*mWidth*mDepth + 2 ] = squareB; } } setDirty( 0, 0, mWidth, mHeight ); ...
</cpp>