Click here to Skip to main content
Click here to Skip to main content

Create Simple Palette Animation

, 26 Dec 2009
Rate this:
Please Sign up or sign in to vote.
To show you how to create a easy animation with palette
PalAnimDemoSrc

Introduction

The article demonstrates how to create an animation with palette. As we all know, palette is outdated, but there are still many places where it is used. If you didn't know more about palette, go to search online to check out more about it. It's so cool that when you add some animation to your project, it could attract other's eyeballs. Of course, this code is very simple. You can change it in any way as per your wish to make it more useful and stronger.

Background

I had nothing to do, so I wrote this code. I love Codeproject.com, but I feel that I found it very late.

Using the Code

It's quite easy to use this code:

  1. Add the two class files to your project, "PalAnimClass.h" and "PalAnimClass.cpp".
  2. Call its public method Init() to finish initialization.
  3. Now, call its UpdateDate() frequently. I call this function once every 30ms. You can alter the value and adjust your computer. For this reason, no handle of window was passed into the class, so that we must use another timer to loop it. Let's take a look at the definition of the class:
class PalAnimClass 
{
public:
BOOL GetDirection();
BOOL SetType(int nType);
BOOL SetDirect(BOOL dir);
BOOL SetColor(int r,int g,int b);
BOOL SetSize(int iWidth,int iHeight);
void UpdateData(HDC hdc);
BOOL Init();
PalAnimClass();
virtual ~PalAnimClass();

private:
BOOL m_bDirection;
int m_nKind;
HBRUSH hBrush;
void DrawBackground(HDC hdc,int type);
void TimerRoutine(HPALETTE hPalette);
HPALETTE CreatePal();
RECT rt;
HPALETTE hPalette,hOldPal;
int m_nHeight;
int m_nWidth;
LOGPALETTE *plp;
int m_r,m_g,m_b; 
int i,j;
};

Only several public methods were found.

  • GetDirection() is used to get the direction of movement. Actually it's a BOOL date type, indacates the movement to left or to right, in or out. We use this method to get the value of movement and to use another method SetDirect() to set a new value.
  • SetType() is used to set the type of animations. The scope of animations' type is 0~6. Be sure that is not out of bound.
  • SetColor(): Three parameters should be passed into, they are RED, GREEN, BLUE colors. The scope of each of them is 0~255, just like the macro RGB.
  • SetSize() is used to set the size of animation. The default size is 300X300.
  • Init() is used for initialization.

Principle

The principle of palette animation is really very easy. There are only three steps in total.

  1. Create a palette. The palette is special, we will talk about it later.
  2. Draw shapes with the colors in the palette which we created just now.
  3. We change the value of paletteentrys.

So easy, isn't it? Let's start implementing it.

Create a special palette:

HPALETTE PalAnimClass::CreatePal()
{ 
HPALETTE hPalette;

if(hPalette) 
::DeleteObject (hPalette);

plp=(LOGPALETTE *)malloc(sizeof(LOGPALETTE)+sizeof(PALETTEENTRY)*64);
plp->palVersion =0x300; //Always 0x300
plp->palNumEntries =64;//We only use 64 so no need change to var


for(int i=0;i<64;i++)
{
plp->palPalEntry [i].peRed =m_r*sin(PI*i/64);
plp->palPalEntry[i].peGreen =m_g*sin(PI*i/64);
plp->palPalEntry [i].peBlue =m_b*sin(PI*i/64);
plp->palPalEntry [i].peFlags =PC_RESERVED; //This is very important!Must be 
		// PC_RESERVED. That is why I always say that it's a special palette.
}//If peFlasg is not this value, AinamatePalette() will not get the result we want!
hPalette=::CreatePalette(plp);
return hPalette;
}

Using the method we create a palette, now we draw shapes in the background. Remember to use the colors in the palette which we just created. This step is easy, look:

hBrush = CreateSolidBrush (PALETTEINDEX (min(64*sin(PI*j/64),64*sin(PI*i/64)))) ;

Use PALETTEINDEX or PALETTERGB to use the color in our new palette. Why are there so many sine functions here, that's just a math problem. You could understand it well. After painting our shapes in the background, now let's go to alter the value of palette. The snippets are as follows:

void PalAnimClass::TimerRoutine(HPALETTE hPalette)
{
 PALETTEENTRY temp;
  for(j=0;j<3;j++)
 {
   if(m_bDirection)  //m_bDirecton is a BOOL data structure to 
		//decide whether the palletteentry move to left or to right
   {              //here just a shift oporation,we take the last to first or 
		//take first to last
  temp=plp->palPalEntry [0];
  for(i=0;i<64;i++)
   plp->palPalEntry[i]=plp->palPalEntry [i+1];
  plp->palPalEntry [63]=temp;
   }
   else
   {
  temp=plp->palPalEntry [63];    
  for(i=63;i>0;i--)
   plp->palPalEntry[i]=plp->palPalEntry [i-1];
  plp->palPalEntry [0]=temp;
   }
 }

 AnimatePalette(hPalette,0,64,plp->palPalEntry); //here it is very important to
					// alter the physical device's data.
}

The AnimatePalette function replaces entries in the specified logical palette.

BOOL AnimatePalette(
  HPALETTE hpal,            // handle to logical color palette
  UINT iStartIndex,         // first entry in logical palette
  UINT cEntries,            // count of entries in logical palette
  CONST PALETTEENTRY *ppe   // pointer to first replacement
);

Remarks

An application can determine whether a device supports palette operations by calling the GetDeviceCaps function and specifying the RASTERCAPS constant.

The AnimatePalette function only changes entries with the PC_RESERVED flag set in the corresponding palPalEntry member of the LOGPALETTE structure.

If the given palette is associated with the active window, the colors in the palette are replaced immediately.

Getting to the start now. It's your turn, try to write more interesting code. Sorry about my shortcomings. Any questions, add me to your ICQ chat.

Points of Interest

I found that I became more and more patient and efficient. Thanks to everyone's help! Thanks again! CodeProject helped me improve!^.^

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Aric Green
Engineer
China China
Although I was a college studnet now as the same I was also a teacher of C/C++/3DS MAX/website for National Computer Rank Examination.I began to study computer when I was a child 8 year's old.I use BASIC/C/C++/VB/VF/VC/DELPHI/.NET to programming.When in college I was the chairman of Computer Association of our school and I take part in ACM Competition twice in two years.I also was the only person who take part in the National Mathematical Contest in Modeling twice in two years of our school.I like computer programming very much.Hope to be your friend.thx!

Comments and Discussions

 
GeneralCustomization of PaletteAnim PinmemberMember 189546128-Dec-09 11:30 
GeneralRe: Customization of PaletteAnim PinmemberAric Green28-Dec-09 15:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140709.1 | Last Updated 27 Dec 2009
Article Copyright 2009 by Aric Green
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid