Animation using GDI

Nov 20, 2011 at 1:49pm
Hi.
I'm trying to animate a number of squares (number is defined by a constant) using GDI features. So i created a class Square :


#pragma once
#include <windows.h>

class Square {

1
2
3
4
5
6
7
8
9
10
public:
	 int right_x,right_y;
	 int left_x,left_y;

	 void DrawSquare(HDC,int,int,int,int);
	 void MoveSquare(int,int);

	 Square(void);
	~Square(void);
};


and it's implementation file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include "Square.h"
#define Step 5

static int x =0,y=0;

// Create a square
void Square::DrawSquare(HDC hdc,int rx,int ry, int lx, int ly) {
	Rectangle(hdc,rx,ry,lx,ly);
}

/* Refresh path to move for square
x and y keep coordinates of resized window
*/
void Square::MoveSquare(int x_,int y_) {

	x=x_+Step;
	y=y_+Step;

}

Square::Square(void) {
}

Square::~Square(void) {
}


I'm using a timer to animate these squares . I will post just code snippets from my main.cpp :

1
2
3
4
5
6
7
8
9
10
int WINAPI WinMain (HINSTANCE hInt, HINSTANCE,LPSTR,int) {

  //...

  SetTimer (hwnd, 1, 40, NULL) ; // create a timer 
 
  Square square[NUMARUL_DE_FIGURI]; // create a Square array with a given number       
of elements 

}


When a Timer message occurs squares must move

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
case WM_TIMER:

	for (int i=0;i<NUMARUL_DE_FIGURI;i++) {
	
		square[0].MoveSquare(x,y);
	
	}
            
            InvalidateRect (hwnd, NULL, TRUE) ;
            
    /*        if ( x >= mod_x_Size - 30) // put the -50 else it'll let you to up to x point
            {
                x= mod_x_Size - 30 ;
                moveWith_x =  -moveWith_x++;// put ++ so its less predictable
            }
            if (y >= mod_y_Size -30)
            {
                y=mod_y_Size - 30 ;
                moveWith_y= -moveWith_y++;
            }
            if (x < 30)
            {
                x=30;
                moveWith_x= - moveWith_x++;
            }
            if (y <30)
            {
                y=50;
                moveWith_y= - moveWith_x++;	
            } */


Create device context where to draw squares with white color

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
case WM_PAINT: 

	// Permite desenarea pe dispozitivul de desenare
    hdc = BeginPaint(hwnd, &ps);
	
	// Crearea patratelor
   for (int i=0;i<NUMARUL_DE_FIGURI;i++) {

	    shapeColor = (HBRUSH)SelectObject(hdc, CreateSolidBrush(RGB(255,255,255)));

		square[i].DrawSquare(hdc,mod_x_Size-((i*200)+50),mod_y_Size-((i*200)+50),mod_x_Size-((i*20)+100),mod_y_Size-((i*20)+100)); 

	}

	// Finiseaza procesul de desenare pe dispozitiv
    EndPaint(hwnd, &ps);
 
	break;


But when running the program it's shows just static objects without moving them.
I need some advices , i DONT NEED THE CODE , just tell me what i'm doing wrong.

Nov 20, 2011 at 1:57pm
Shouldn't your Square::DrawSquare routine be using the coords the Square instance holds, rather than those passed to is??? Otherwise, the class is a bit pointless (from an encapsulation point of view).

And I don't get the global, static x and y used in Square::MoveSquare, for similar reasons.

Actually, I am a bit confused about x and y. Are there more than one at different scopes?
(There are variables whose definition are not visible.)

Also, you usually call SetTimer in the WM_CREATE handler (or a suitable WM_COMMAND handler). You might want to random position your squares at the same time. And KillTimer in WM_DESTROY

And your move loop only ever moves the first element in the array.

And you should move CreateSolidBrush and SelectObject out of the for loop. And restore the old brush before you call EndPaint (what you're calling shapeColor could be called hBrushOld?) and delete the (single) brush you created. Currently you're leaking NUMARUL_DE_FIGURI brushes every time you process WM_PAINT.

And I see that the square array variable is local to WinMain. How do the message handlers get at it?

Andy
Last edited on Nov 20, 2011 at 2:16pm
Topic archived. No new replies allowed.