Public Group

# Beginning win32 animation.

This topic is 4952 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi guys i was just starting win32 animation and seein if i could figure it out. This is what i've come up with so far.
class CAnimation{
private:
HBITMAP bitmaps[2];
int current;
DWORD start_time;
public:
CAnimation(char* file1, char* file2){
current = 0;
start_time = GetTickCount();
}
void Animate(HWND hwnd){
if((GetTickCount() - start_time) < 30){
HDC hdc = GetDC(hwnd);
HDC imagedc = CreateCompatibleDC(hdc);
SelectObject(imagedc,bitmaps[current]);
BitBlt(hdc,0,0,100,100,imagedc,0,0,SRCCOPY);
DeleteDC(imagedc);
start_time = GetTickCount();
if(current == 1){
current = 0;
}
else{
current++;
}
}
}
};


Would this work or is there a better way. Also ( i will add double buffer when i get a chance if this is the right way) Thanks guys,

##### Share on other sites
Well I know that my source works but i couldnt see the animation unless i put the framrate to 1000 instead of 30. ANy help explaining it?

##### Share on other sites
Well I noticed that in your code - if the time is not ready for a redraw - you do nothing. Try this:

[source lang = "cpp"]class CAnimation{private:   HBITMAP bitmaps[2];   int current;   DWORD start_time;public:   CAnimation(char* file1, char* file2){      HBITMAP bitmaps[0] = (HBITMAP)LoadImage(NULL,file1,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);      HBITMAP bitmaps[1] = (HBITMAP)LoadImage(NULL,file2,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);      current = 0;      start_time = GetTickCount();   }   void Animate(HWND hwnd)   {         HDC hdc = GetDC(hwnd);         HDC imagedc = CreateCompatibleDC(hdc);         SelectObject(imagedc,bitmaps[current]);         BitBlt(hdc,0,0,100,100,imagedc,0,0,SRCCOPY);         DeleteDC(imagedc);         start_time = GetTickCount();      if((GetTickCount() - start_time) < 30)      {         if(current == 1){            current = 0;         }         else{            current++;         }      }   }};

Basically you only want to change the animation when the time is right, but you still want to draw the image! Try this out and let me know if it works correctly now - it *should*.

- Drew

##### Share on other sites
Um isn't urs exactly like mine?

##### Share on other sites
O nvrmnd i see what u r saying
EDIT: Sorry for double post. BTW THANKS BRO it works a lot smoother now. :-D
Is there anything else i should do. I know i am thinking about making the class have a vector of HBITMAP and let the constructor take a vector of char* so that it can load as many images into the animation as needed. Good idea?
EDIT2: Also i will make it take in an x and y for the animation :-D
EDIT3: Also i will make it take a framerate too :-D that way they control the speed of animation :-D Man i'm just bursting with ideas.

##### Share on other sites
Quote:
 Original post by GotenRulezUO nvrmnd i see what u r sayingEDIT: Sorry for double post. BTW THANKS BRO it works a lot smoother now. :-D Is there anything else i should do. I know i am thinking about making the class have a vector of HBITMAP and let the constructor take a vector of char* so that it can load as many images into the animation as needed. Good idea?EDIT2: Also i will make it take in an x and y for the animation :-DEDIT3: Also i will make it take a framerate too :-D that way they control the speed of animation :-D Man i'm just bursting with ideas.

No problem! Glad you are on a programming spree [wink], once one good idea comes, the rest follow. As for the vector idea, absolutly! It will make adding whatever you want a lot easier and more efficiently. Now here is an idea of what I would do to make that function much faster:
[source lang ="cpp"]class CAnimation{private:   vector <HBITMAP> bitmaps;   int current;   DWORD start_time;   HDC hdc;   HDC imagedc;   HWND hwnd;public:   CAnimation()   {hwnd = 0;hdc = 0;imagedc = 0;      current = 0;      start_time = GetTickCount();   }   void Init(HWND _hw)   {      hwnd = _hw;      hdc = GetDC(hwnd);      imagedc = CreateCompatibleDC(hdc);   }     void Add(char* file1, int w, int h)   {W = w;H = h;      bitmaps.PushBack( (HBITMAP)LoadImage(NULL,file1,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);   }   void Animate( int X, int Y)   {      SelectObject(imagedc,bitmaps[current]);      BitBlt(hdc,X,Y,W,H,imagedc,0,0,SRCCOPY);      start_time = GetTickCount();      if((GetTickCount() - start_time) < 30)      {         if(current == 1){            current = 0;         }         else{            current++;         }      }   }void Cleanup(){   DeleteDC(imagedc);}};

I think this should work as well - I do not know much about Win32 drawing functions but after looking at MSDN I think this will work, if not try this:
...void Animate( int X, int Y)   {      imagedc = CreateCompatibleDC(hdc);            SelectObject(imagedc,bitmaps[current]);      ...      DeleteDC(imagedc);    }...

and remove DeleteDC(imagedc); from cleanup.

Just a few ideas I had. I hope this helps some more - let your imagination run wild [smile].

- Drew

##### Share on other sites
Thanks for being so supportive :-D Eventually I also have an idea to make it so u put in the frame rate per frame if u want so that u can have diff speeds to dif frames. I think i will have a mybitmap class and a Animation class to handle the bitmaps. ( i want to have a mybitmap class to add more useability to the HBITMAPS)

##### Share on other sites
Drew posted this link in another post and I found it very helpful. I think it will help you get your own Bitmap and Sprite class up and going:

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 10
• 14
• 30
• 13
• 11
• ### Forum Statistics

• Total Topics
631790
• Total Posts
3002365
×