Creating and Drawing
The Plasma

This page shows the C++ code to create and display a Plasma.


I've had enough! I want to go home

Take me back to the CDib class


What is a Plasma?

A Plasma is a particular graphical effect where the screen seems to consist of pulsating blobs.

To construct the Plasma, I use a 2-dimensional graph of colour values, where each value is derived from the sum of a number of sine waves.

There are two vertical sine waves and three horizontal sine waves. The sine waves have different frequencies and ranges, and the starting angle of each wave varies for each phase of the plasma.

The colour values are mapped onto a graduated palette. Values near the middle of the range map to black, near the bottom of the range map to white, and near the top of the range map to green.

I derived the Plasma algorithm and the palette from a demo program in an old issue of PC Format.


Creating the Palette

I set up the palette in the 'OnCreate' method. The colours are hard-coded into an array. (A more sophisticated approach would be to somehow incorporate and use a palette resource).

CDC * pDC ;
int colourTable[256] ;
// set up RGB colour table // Use the palette from the PLASMA demo int i ;

for ( i = 0 ; i < 256 ; i++ ) colourTable[i] = 0 ; colourTable[10] = RGB (237,225,225) ; colourTable[11] = RGB (239,227,227); colourTable[12] = RGB (241,229,229) ; colourTable[13] = RGB (243,231,231) ; colourTable[14] = RGB (245,235,235) ; colourTable[15] = RGB (247,239,239) ; colourTable[16] = RGB (249,243,243) ; colourTable[17] = RGB (251,247,247) ; colourTable[18] = RGB (253,251,251) ; colourTable[19] = RGB (255,255,255) ; colourTable[20] = RGB (249,251,251) ; colourTable[21] = RGB (243,247,247); colourTable[22] = RGB (237,245,243) ; colourTable[23] = RGB (231,243,239); colourTable[24] = RGB (227,241,235) ; colourTable[25] = RGB (223,239,231); colourTable[26] = RGB (219,237,227) ; colourTable[27] = RGB (215,235,223) ; colourTable[28] = RGB (211,233,219) ; colourTable[29] = RGB (207,231,215) ; colourTable[30] = RGB (201,229,213) ; colourTable[31] = RGB (195,227,211); colourTable[32] = RGB (191,225,207) ; colourTable[33] = RGB (187,223,203); colourTable[34] = RGB (183,219,199) ; colourTable[35] = RGB (179,215,195); colourTable[36] = RGB (175,213,193) ; colourTable[37] = RGB (171,211,191); colourTable[38] = RGB (167,209,187) ; colourTable[39] = RGB (163,207,183); colourTable[40] = RGB (159,205,181) ; colourTable[41] = RGB (155,203,179); colourTable[42] = RGB (151,201,175) ; colourTable[43] = RGB (147,199,171); colourTable[44] = RGB (143,197,167) ; colourTable[45] = RGB (139,195,163); colourTable[46] = RGB (137,193,161) ; colourTable[47] = RGB (135,191,159); colourTable[48] = RGB (131,187,155) ; colourTable[49] = RGB (127,183,151); colourTable[50] = RGB (123,181,149) ; colourTable[51] = RGB (119,179,147); colourTable[52] = RGB (115,177,143) ; colourTable[53] = RGB (111,175,139); colourTable[54] = RGB (109,173,137) ; colourTable[55] = RGB (107,171,135); colourTable[56] = RGB (103,169,133) ; colourTable[57] = RGB ( 99,167,131); colourTable[58] = RGB ( 95,165,127) ; colourTable[59] = RGB ( 91,163,123); colourTable[60] = RGB ( 89,161,121) ; colourTable[61] = RGB ( 87,159,119); colourTable[62] = RGB ( 85,155,115) ; colourTable[63] = RGB ( 83,151,111); colourTable[64] = RGB ( 79,149,109) ; colourTable[65] = RGB ( 75,147,107); colourTable[66] = RGB ( 73,145,105) ; colourTable[67] = RGB ( 71,143,103); colourTable[68] = RGB ( 67,141,101) ; colourTable[69] = RGB ( 63,139, 99); colourTable[70] = RGB ( 61,137, 97) ; colourTable[71] = RGB ( 59,135, 95); colourTable[72] = RGB ( 57,133, 91) ; colourTable[73] = RGB ( 55,131, 87); colourTable[74] = RGB ( 53,129, 85) ; colourTable[75] = RGB ( 51,127, 83); colourTable[76] = RGB ( 49,123, 81) ; colourTable[77] = RGB ( 47,119, 79); colourTable[78] = RGB ( 45,117, 77) ; colourTable[79] = RGB ( 43,115, 75); colourTable[80] = RGB ( 41,113, 73) ; colourTable[81] = RGB ( 39,111, 71); colourTable[82] = RGB ( 37,109, 69) ; colourTable[83] = RGB ( 35,107, 67); colourTable[84] = RGB ( 33,105, 65) ; colourTable[85] = RGB ( 31,103, 63); colourTable[86] = RGB ( 29,101, 61) ; colourTable[87] = RGB ( 27, 99, 59); colourTable[88] = RGB ( 25, 97, 57) ; colourTable[89] = RGB ( 23, 95, 55); colourTable[90] = RGB ( 21, 91 ,53) ; colourTable[91] = RGB ( 19, 87, 51); colourTable[92] = RGB ( 17, 85, 49) ; colourTable[93] = RGB ( 15, 83, 47); colourTable[94] = RGB ( 15, 81, 45) ; colourTable[95] = RGB ( 15, 79, 43); colourTable[96] = RGB ( 13, 77, 41) ; colourTable[97] = RGB ( 11, 75, 39); colourTable[98] = RGB ( 11, 73, 37) ; colourTable[99] = RGB ( 11, 71, 35); colourTable[100] =RGB ( 9, 69 ,35) ; colourTable[101] =RGB ( 7, 67, 35); colourTable[102] =RGB ( 7, 65 ,33) ; colourTable[103] =RGB ( 7, 63, 31); colourTable[104] =RGB ( 7, 59 ,29) ; colourTable[105] =RGB ( 7, 55, 27); colourTable[106] =RGB ( 0, 53 ,25 ) ; colourTable[107] =RGB ( 0, 51, 23); colourTable[108] =RGB ( 0, 49 ,23 ) ; colourTable[109] =RGB ( 0, 47, 23); colourTable[110] =RGB ( 0, 45, 21 ) ; colourTable[111] =RGB ( 0, 43, 19); colourTable[112] =RGB ( 0, 41 ,17 ) ; colourTable[113] =RGB ( 0, 39, 15); colourTable[114] =RGB ( 0, 37 ,15 ) ; colourTable[115] =RGB ( 0, 35, 15); colourTable[116] =RGB ( 0, 35 ,15 ) ; colourTable[117] =RGB ( 0, 35, 15); colourTable[118] =RGB ( 0, 37, 15) ; colourTable[119] =RGB ( 0, 39, 15); colourTable[120] =RGB ( 0, 41 ,15 ) ; colourTable[121] =RGB ( 0, 43, 15); colourTable[122] =RGB ( 0, 45, 15) ; colourTable[123] =RGB ( 0, 47, 15); colourTable[124] =RGB ( 0, 49 ,17 ) ; colourTable[125] =RGB ( 0, 51, 19); colourTable[126] =RGB ( 0, 53, 19 ) ; colourTable[127] =RGB ( 0, 55, 19); colourTable[128] =RGB ( 0, 57, 19) ; colourTable[129] =RGB ( 0, 59, 19); colourTable[130] =RGB ( 3, 59 ,19 ) ; colourTable[131] =RGB ( 7, 59, 19); colourTable[132] =RGB ( 7, 61 ,19 ) ; colourTable[133] =RGB ( 7, 63, 19); colourTable[134] =RGB ( 7, 65, 19) ; colourTable[135] =RGB ( 7, 67, 19); colourTable[136] =RGB ( 7, 69, 19 ) ; colourTable[137] =RGB ( 7, 71, 19); colourTable[138] =RGB ( 9, 73, 19 ) ; colourTable[139] =RGB ( 11, 75, 19); colourTable[140] =RGB ( 11, 77 ,21 ) ; colourTable[141] =RGB ( 11, 79, 23); colourTable[142] =RGB ( 11, 81, 23 ) ; colourTable[143] =RGB ( 11, 83, 23); colourTable[144] =RGB ( 13, 85, 23 ) ; colourTable[145] =RGB ( 15, 87, 23); colourTable[146] =RGB ( 15, 87 ,23 ) ; colourTable[147] =RGB ( 15, 87, 23); colourTable[148] =RGB ( 17, 89 ,23 ) ; colourTable[149] =RGB ( 19, 91, 23); colourTable[150] =RGB ( 19, 93, 23) ; colourTable[151] =RGB ( 19, 95, 23); colourTable[152] =RGB ( 21, 97, 23 ) ; colourTable[153] =RGB ( 23, 99, 23); colourTable[154] =RGB ( 25,101, 23) ; colourTable[155] =RGB ( 27,103, 23); colourTable[156] =RGB ( 29,105 ,25 ) ; colourTable[157] =RGB ( 31,107, 27); colourTable[158] =RGB ( 33,109, 29) ; colourTable[159] =RGB ( 35,111, 31); colourTable[160] =RGB ( 37,111, 31) ; colourTable[161] =RGB ( 39,111, 31); colourTable[162] =RGB ( 41,113 ,33 ) ; colourTable[163] =RGB ( 43,115, 35); colourTable[164] =RGB ( 45,117, 37) ; colourTable[165] =RGB ( 47,119, 39); colourTable[166] =RGB ( 49,121, 39) ; colourTable[167] =RGB ( 51,123, 39); colourTable[168] =RGB ( 53,125, 41 ) ; colourTable[169] =RGB ( 55,127, 43); colourTable[170] =RGB ( 59,129 ,45 ) ; colourTable[171] =RGB ( 63,131, 47); colourTable[172] =RGB ( 65,133, 47) ; colourTable[173] =RGB ( 67,135, 47); colourTable[174] =RGB ( 69,135, 49 ) ; colourTable[175] =RGB ( 71,135, 51); colourTable[176] =RGB ( 73,137, 53) ; colourTable[177] =RGB ( 75,139, 55); colourTable[178] =RGB ( 79,141, 57) ; colourTable[179] =RGB ( 83,143, 59); colourTable[180] =RGB ( 85,145, 59) ; colourTable[181] =RGB ( 87,147, 59); colourTable[182] =RGB ( 89,149, 63 ) ; colourTable[183] =RGB ( 91,151, 67); colourTable[184] =RGB ( 95,153, 67) ; colourTable[185] =RGB ( 99,155, 67); colourTable[186] =RGB (101,157, 69 ) ; colourTable[187] =RGB (103,159, 71); colourTable[188] =RGB (105,159, 73 ) ; colourTable[189] =RGB (107,159, 75); colourTable[190] =RGB (111,161, 77 ) ; colourTable[191] =RGB (115,163, 79); colourTable[192] =RGB (117,165, 81 ) ; colourTable[193] =RGB (119,167, 83); colourTable[194] =RGB (123,169 ,85 ) ; colourTable[195] =RGB (127,171, 87); colourTable[196] =RGB (129,173 ,89 ) ; colourTable[197] =RGB (131,175, 91); colourTable[198] =RGB (133,177 ,93 ) ; colourTable[199] =RGB (135,179, 95); colourTable[200] =RGB (139,181 ,97 ) ; colourTable[201] =RGB (143,183, 99); colourTable[202] =RGB (145,183,101 ) ; colourTable[203] =RGB (147,183,103); colourTable[204] =RGB (151,185,105 ) ; colourTable[205] =RGB (155,187,107); colourTable[206] =RGB (157,189,109 ) ; colourTable[207] =RGB (159,191,111); colourTable[208] =RGB (163,193,115 ) ; colourTable[209] =RGB (167,195,119); colourTable[210] =RGB (169,197,121 ) ; colourTable[211] =RGB (171,199,123); colourTable[212] =RGB (173,201,125 ) ; colourTable[213] =RGB (175,203,127); colourTable[214] =RGB (179,205,129 ) ; colourTable[215] =RGB (183,207,131); colourTable[216] =RGB (185,207,133) ; colourTable[217] =RGB (187,207,135); colourTable[218] =RGB (191,209,139 ) ; colourTable[219] =RGB (195,211,143); colourTable[220] =RGB (197,213,145 ) ; colourTable[221] =RGB (199,215,147); colourTable[222] =RGB (201,217,149 ) ; colourTable[223] =RGB (203,219,151); colourTable[224] =RGB(205,221,153 );
pDC = this->GetDC() ; // work out screen size and colour depth screenWidth = pDC->GetDeviceCaps (HORZRES); screenHeight = pDC->GetDeviceCaps (VERTRES); screenBpp = pDC->GetDeviceCaps (BITSPIXEL); // create master palette CDib dummyDib ; int res ; res = dummyDib.makePicture ( 1 , 1 , colourTable ) ; res = dummyDib.makePalette ( &masterPalette) ;

The 'makePalette' method creates a palette based on a bitmap's colour table. The dummy bitmap is created just to contain the colour table with all the hard-coded colours included.

'masterPalette' is a CPalette object I set up as a class variable in the CSaverWnd class.


Create Identity Palette and DIB Section

After the palette is created, it gets converted to an identity palette (if the screen is in 256-colour mode), and a DIB section is created.

There is no need for an identity palette if the screen is in true-colour or high-colour mode, because in these modes there is no system palette. Instead, RGB colours are directly presented to the video card, instead of palette indices.

if ( screenBpp == 8 )
{
    // convert to an identity palette if in 256-colour mode

    for ( i = 0 ; i < 256 ; i++ )
        remap[i] = 0 ;

    osb.identityPalette ( &masterPalette , pDC ) ;

    for ( i = 0 ; i < 256 ; i++ )
        remap[i] = masterPalette.GetNearestPaletteIndex(colourTable[i]);
}
else
    for ( i = 0 ; i < 256 ; i++ ) 
        remap[i] = i ;

// set up the DIB section osb, same size as the full window

osb.makeDibSection ( screenWidth ,
                     screenHeight ,
                     8 , 
                     pDC ,
                     &masterPalette ,
                     (screenBpp == 8) // palettize if in 256-colour mode
                   ) ;

osb.setBgColour ( RGB(0,0,0) , 
                  &masterPalette) ;

osb.bgFill () ;

ReaseDC(pDC) ;

This code goes straight after the palette creation code, in 'OnCreate'.

Remember that 'palettize' is my name for the process of changing a bitmap to have a 1-to-1 colour table and having the pixels referring directly to a palette rather than the colour table.

'osb' is a CDib object I set up as a class variable.


Obtain the Configuration Values

frameRate  = theApp.GetProfileInt("Config","Frame Rate", 10) ;

maxPlasma  = theApp.GetProfileInt("Config" , "Max Plasma Size" , 200 ) ;

minPlasma  = theApp.GetProfileInt("Config" , "Min Plasma Size" , 100 );

drift      = (float)((int)theApp.GetProfileInt("Config" , "Drift Rate" , 2 ))/10.0 ;

plasmaSpeed = (float)(theApp.GetProfileInt("Config" , "Plasma Speed" , 41 )) / 10.0 ;

minPlasma = (minPlasma + 3 ) & ~3 ;
maxPlasma = (maxPlasma + 3 ) & ~3 ;

if ( minPlasma > maxPlasma ) 
{
    int save ;
save = maxPlasma ; maxPlasma = minPlasma ; minPlasma = save ; } if ( frameRate < 1 ) frameRate = 1 ; cycleNum = minPlasma ; cycleUp = true ;

This code is also in 'OnCreate'. I am reading the configuration values from out of the registry.

The Plasma image is created in a rectangle of a specified size. The rectangle then gets tiled across the screen. If it is a big rectangle, there won't be many copies. If it is a small rectangle, there will be many copies on the screen,

'frameRate' is how fast the animation will go (frames per second).

'minPlasma' is the smallest rectangle size to use for creating the Plasma.

'maxPlasma' is the largest rectangle size to use for creating the Plasma.

The Plasma varies in size from frame to frame, ranging between the 'minPlasma' and 'maxPlasma' values in steps of 4 pixels.

'drift' is the amount by which the Plasma pattern should appear to move diagonally up or down the screen.

'plasmaSpeed' is how much the Plasma pattern should change from one frame to the next.

Note that the 'minPlasma' and 'maxPlasma' values are forced to be multiples of 4. This makes it easier when accessing the pixels (there is no byte-padding on the end of each pixel row).

Also note that you need access to the application object in order to use the 'GetProfileInt' API. So, at the top of the screen saver full-screen window code, you need :

extern CPlasmaApp theApp ;

This links to the global application object that MFC App Wizard sets up for you in the automatically generated application source code.


Generate and Display the Plasma

I set up a new method to generate and draw the Plasma, called 'drawPlasma'. It relies on the starting angle for each of the five sine waves being saved from one frame to the next. I do this by setting them up as class variables.

Note that the C++ 'sin' function expects angles to be in radians rather than degrees. A full circle in radians is twice pi (6.284), rather than 360 degrees. This is why the code uses to 6.284 such a lot!

void CSaverWnd::drawPlasma ( CDC * pDC , int plasma_size)
{
int i , j , k ;
float value ;

int byteWidth ;

int * xbuffer  , * ybuffer ;

xbuffer = (int*)malloc(plasma_size * sizeof(int)) ;
ybuffer = (int*)malloc(plasma_size * sizeof(int)) ;

angle1bak = angle1 ;
angle2bak = angle2 ;
angle3bak = angle3 ;

// do horizontal graph

for ( i = 0 ; i < plasma_size ; i++ )
{
    value = 32.0*sin(angle1) + 16.0*sin(angle2) + 8.0*sin(angle3) ; // 16,8,4

    xbuffer[i] = (int)value ; // * 3)>>1;

    angle1 += 6.284/plasma_size;
    angle2 += 2*6.284/plasma_size;
    angle3 += 4*6.284/plasma_size ;
}

angle1 = angle1bak - ((1*6.284/320)*plasmaSpeed) ; // 1
angle2 = angle2bak + ((3*6.284/320)*plasmaSpeed) ; // 3
angle3 = angle3bak + ((2*6.284/320)*plasmaSpeed) ; // 2

angle4bak = angle4 ;
angle5bak = angle5 ;

for ( i = 0 ; i < plasma_size ; i++ )
{
    value = 32.0*sin(angle4) + 16.0*sin(angle5) ;

    ybuffer[i] = (int)value ;

    angle4 += 6.284/plasma_size;
    angle5 += 2*6.284/plasma_size ;
}

angle4 = angle1bak + ((3*6.284/320)*plasmaSpeed) ;  // 3
angle5 = angle2bak + ((5*6.284/320)*plasmaSpeed) ;  // 5

// fill in the plasma

::GdiFlush() ;
byteWidth = ( osb.width + 3 ) & ~3 ;
BYTE * pPix = osb.pPixels + (osb.height - plasma_size)*byteWidth ;

for ( j = 0 ; j < plasma_size ; j++ )
{
    k = ybuffer[j] ;

    if ( screenBpp == 8 )
        for ( i = 0 ; i < plasma_size ; i++ ) 
        {
            *(pPix + i + byteWidth*j) = remap[117 + k + xbuffer[i]] ;
        }
    else
        for ( i = 0 ; i < plasma_size ; i++ ) 
        {
            *(pPix + i + byteWidth*j) = 117 + k + xbuffer[i] ;
        }
}

free (xbuffer) ;
free (ybuffer) ;

// Copy plasma repeatedly in the osb, to tile it. 

int numAcross = int(osb.width / plasma_size) + 1 ;
int numDown   = int(osb.height / plasma_size) + 1 ;

int vert , horz ;

for ( vert = 0 ; vert < numDown ; vert++ ) 
    for ( horz = 0 ; horz < numAcross ; horz++ )
    {
	if ( vert > 0 || horz > 0 )
            osb.solidMerge ( horz*plasma_size , vert*plasma_size ,
                             osb.pBmpInfoHdr ,
                             osb.pPixels ,
                             0 , 0 , 
                             plasma_size , plasma_size , 
                             false ) ;
    }

CRect fullRect ;
fullRect.SetRect ( 0 , 0 , osb.width , 
                   osb.height ) ;
::GdiFlush() ;
osb.draw ( fullRect , fullRect , pDC , NULL ,
                PALOP_FOREGROUND , &masterPalette ) ;

angle1 -= drift ;
angle2 -= drift ;
angle3 -= drift ;
angle4 -= drift ;
angle5 -= drift ;

if ( angle1 < 0 ) 
    angle1 += 6.284 ;
else if ( angle1 > 6.283 )
    angle1 -= 6.284 ;
if ( angle2 < 0 ) 
    angle2 += 6.284 ;
else if ( angle2 > 6.283 )
    angle2 -= 6.284 ;
if ( angle3 < 0 ) 
    angle3 += 6.284 ;
else if ( angle3 > 6.283 )
    angle3 -= 6.284 ;
if ( angle4 < 0 ) 
    angle4 += 6.284 ;
else if ( angle4 > 6.283 )
    angle4 -= 6.284 ;
if ( angle5 < 0 ) 
    angle5 += 6.284 ;
else if ( angle5 > 6.283 )
    angle5 -= 6.284 ;
}


OnTimer()

The 'OnTimer()' method is where most of the action happens. Each time it is called, the next Plasma frame is generated and displayed.

void CSaverWnd::OnTimer(UINT nIDEvent) 
{
// fill in osb with next frame of plasma

if ( minPlasma == maxPlasma )
    cycleNum = maxPlasma ;
else if ( cycleUp ) 
{
    if ( cycleNum >= maxPlasma || cycleNum >= screenWidth )
    {
        cycleUp = false ;
        cycleNum -= 4 ;
    }
    else
        cycleNum += 4 ;
}
else 
{
    if ( cycleNum <= minPlasma )
    {
        cycleUp = true ;
        cycleNum += 4 ;
    }
    else
        cycleNum -=4 ;
}

CDC * pDC = GetDC() ;

drawPlasma ( pDC , cycleNum ) ;

ReleaseDC (pDC) ;

CWnd::OnTimer(nIDEvent);
}

Note that 'cycleNum' contains the size of the Plasma rectangle. This changes from one frame to the next (unless 'minPlasma' and 'maxPlasma' are the same). The 'cycleUp' variable indicates if the size is currently increasing or decreasing.


Erase Background Call

I put a call to 'drawPlasma' in the 'OnEraseBkgnd' method. This gets called whenever the screen saver starts up. Without the 'drawPlasma' call, the screen would be set to black by 'OnEraseBkgnd', and you would have to wait until the first timer tick before the plasma showed up. This would be visible as a slight flicker.

So, in 'OnEraseBkgnd', insert this code :

drawPlasma ( pDC , cycleNum ) ;


Repainting the Screen

There are occasions where the screen needs to be repainted, for example after the 'Enter Password' dialog box has obscured part of the screen or changed the palette.

Repainting the screen is easy to do when the screen image is stored in a CDib object. All you need to do is use the 'draw' method on the object.

My repainting code is :

OnPaletteChanged and OnQueryNewPalette

if ( i > 0 )
{
    // redraw the screen, so that colours are mapped properly
    CRect fullRect ;
    fullRect.SetRect ( 0 , 0 , osb.width , osb.height ) ;
    osb.draw ( fullRect , fullRect , pDC , NULL ,
               PALOP_FOREGROUND , &masterPalette ) ;
}

This code goes straight after the 'RealizePalette()' call.

OnPaint

CPaintDC dc(this); // device context for painting
	
CRect paintRect ( &dc.m_ps.rcPaint ) ;

osb.draw ( paintRect , paintRect , &dc , NULL ,
           PALOP_FOREGROUND , &masterPalette ) ;

Note that we only need to repaint the obscured area (the "invalid rectangle").


The Plasma Preview Window

The code for the Plasma preview window is very similar to the full-screen window, except on a smaller scale. Because it is so similar, I won't reproduce it here.


All The Source Code

You can download all the Plasma source code here. This includes the MSVC project files.


Previous | Home

1