Tricks of the Windows Game Programming Gurus:Advanced DirectDraw and Bitmapped Graphics

All ITKnowledge Source Code Search Tips Advanced Search |

| Tricks of the Windows Game Programming Gurus by Andre LaMothe Sams, Macmillan Computer Publishing ISBN: 0672313618 Pub Date: 09/27/99

Search this book:

Previous | Table of Contents | Next

Basically, you fill in the “user” fields and then call the function with a `BLINKER_ADD` command. Anyway, the general operation is as follows: You call the function at any time to add, delete, or update, but only once per frame with the run command. Here’s the code for the function:

    int Blink_Colors(int command, BLINKER_PTR new_light, int id)

    {

    // this function blinks a set of lights

    static BLINKER lights[256]; // supports up to 256 blinking lights

    static int initialized = 0; // tracks if function has initialized

    // test if this is the first time function has run

    if (!initialized)

       {

       // set initialized

       initialized = 1;

       // clear out all structures

       memset((void *)lights,0, sizeof(lights));

       } // end if

    // now test what command user is sending

    switch (command)

           {

           case BLINKER_ADD: // add a light to the database

                {

                // run thru database and find an open light

                for (int index=0; index < 256; index++)

                    {

                    // is this light available?

                    if (lights[index].state == 0)

                       {

                       // set light up

                       lights[index] = *new_light;

                       // set internal fields up

                       lights[index].counter =  0;

                       lights[index].state   = -1; // off

                       // update palette entry

                       lpddpal->SetEntries(0,lights[index].color_index,

                                           1,&lights[index].off_color);

                       // return id to caller

                       return(index);

                       } // end if

                    } // end for index

                } break;

           case BLINKER_DELETE: // delete the light indicated by id

                {

                // delete the light sent in id

                if (lights[id].state != 0)

                   {

                   // kill the light

                   memset((void *)&lights[id],0,sizeof(BLINKER));

                   // return id

                   return(id);

                   } // end if

                else

                    return(-1); // problem

                } break;

           case BLINKER_UPDATE: // update the light indicated by id

                {

                // make sure light is active

                if (lights[id].state != 0)

                   {

                   // update on/off parms only

                   lights[id].on_color  = new_light->on_color;

                   lights[id].off_color = new_light->off_color;

                   lights[id].on_time   = new_light->on_time;

                   lights[id].off_time  = new_light->off_time;

                   // update palette entry

                   if (lights[id].state == -1)

                      lpddpal->SetEntries(0,lights[id].color_index,

                                          1,&lights[id].off_color);

                   else

                      lpddpal->SetEntries(0,lights[id].color_index,

                                          1,&lights[id].on_color);

                   // return id

                   return(id);

                   } // end if

                else

                    return(-1); // problem

                } break;

           case BLINKER_RUN: // run the algorithm

                {

                // run thru database and process each light

                for (int index=0; index < 256; index++)

                    {

                    // is this active?

                    if (lights[index].state == -1)

                       {

                       // update counter

                       if (++lights[index].counter >= lights[index].off_time)

                          {

                          // reset counter

                          lights[index].counter = 0;

                          // change states

                          lights[index].state = -lights[index].state;

                          // update color

                          lpddpal->SetEntries(0,lights[index].color_index,

                                              1,&lights[index].on_color);

                          } // end if

                       } // end if

                    else

                    if (lights[index].state == 1)

                       {

                       // update counter

                       if (++lights[index].counter >= lights[index].on_time)

                          {

                          // reset counter

                          lights[index].counter = 0;

                          // change states

                          lights[index].state = -lights[index].state;

                          // update color

                          lpddpal->SetEntries(0,lights[index].color_index,

                                              1,&lights[index].off_color);

                          } // end if

                       } // end else if

                    } // end for index

                } break;

           default: break;

           } // end switch

    // return success

    return(1);

    } // end Blink_Colors

> Note: I’ve boldfaced the sections that update the DirectDraw palette entries. I assume that there’s a global palette interface `lpddpal`.

The function has three main sections: initialization, updating, and run logic. When the function is called for the first time, it initializes itself. Then the next code segment tests for updating commands or the run command. If an update-type command is requested, logic is performed to add, delete, or update a blinking light. If the run mode is requested, the lights are all processed through one cycle. In general, you would use the function after first adding one or more lights, which you’d do by setting up a generic `BLINKER` structure and then passing the structure to the function with the `BLINKER_ADD` command. The function would then return the ID of your blinking light, which you’d save—you’ll need the ID if you want to delete or update a blinking light.

Previous | Table of Contents | Next

Copyright © Macmillan Computer Publishing, Inc.

Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.