Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Animation by XORing

DonotaloDonotalo Member Posts: 715
[purple]i need to animate a ball on a window. as an alternative to call the draw-erase-draw functions to make the ball animate, i want to keep a device context that will hold a bitmap on which the ball (a circle) will be drawn. afterwards, i'll simply need to copy the DC's contents to the original DC (that is linked with physical window) with SRCINVERT - once for draw, again for erase. here is my code:[code]
class Ball
{
int radius, dx, dy;
RECT position;
HBITMAP hbit;
HDC ball_dc;
...
}

//radius, dx, dy are initialized in the constructor (not shown)

void Ball::initialize(HWND hwnd, RECT b) {
int midx, width, height;
HDC hdc;

width = height = 2 * radius;
hdc = GetDC(hwnd);

ball_dc = CreateCompatibleDC(hdc);
hbit = CreateCompatibleBitmap(hdc, width, height);

SelectObject(ball_dc, hbit);
SelectObject(ball_dc, (HPEN) GetStockObject(BLACK_PEN));
SelectObject(ball_dc, (HBRUSH) GetStockObject(WHITE_BRUSH));
PatBlt(ball_dc, 0, 0, width, height, PATCOPY);

ReleaseDC(hwnd, hdc);

midx = (b.right + b.left) / 2;

position.left = midx - radius;
position.top = b.bottom - height;
position.right = midx + radius;
position.bottom = b.bottom;

Ellipse(ball_dc, position.left, position.top, position.right, position.bottom);
}

void Ball::draw(HDC hdc) const {
BitBlt(hdc, position.left, position.top,
2*radius, 2*radius, ball_dc, 0, 0, SRCINVERT);
}

void Ball::erase(HDC hdc) const {
draw(hdc);
}
[/code]
unfortunately, calling draw(), erase(), move to a new position, draw() shows a black rectangle instead of the circle! can anyone please check what i'm missing?
[/purple]
[hr][purple]~Donotalo()[/purple]

Comments

  • DonotaloDonotalo Member Posts: 715
    : [purple]i need to animate a ball on a window. as an alternative to call the draw-erase-draw functions to make the ball animate, i want to keep a device context that will hold a bitmap on which the ball (a circle) will be drawn. afterwards, i'll simply need to copy the DC's contents to the original DC (that is linked with physical window) with SRCINVERT - once for draw, again for erase. here is my code:[code]
    : class Ball
    : {
    : int radius, dx, dy;
    : RECT position;
    : HBITMAP hbit;
    : HDC ball_dc;
    : ...
    : }
    :
    : //radius, dx, dy are initialized in the constructor (not shown)
    :
    : void Ball::initialize(HWND hwnd, RECT b) {
    : int midx, width, height;
    : HDC hdc;
    :
    : width = height = 2 * radius;
    : hdc = GetDC(hwnd);
    :
    : ball_dc = CreateCompatibleDC(hdc);
    : hbit = CreateCompatibleBitmap(hdc, width, height);
    :
    : SelectObject(ball_dc, hbit);
    : SelectObject(ball_dc, (HPEN) GetStockObject(BLACK_PEN));
    : SelectObject(ball_dc, (HBRUSH) GetStockObject(WHITE_BRUSH));
    : PatBlt(ball_dc, 0, 0, width, height, PATCOPY);
    :
    : ReleaseDC(hwnd, hdc);
    :
    : midx = (b.right + b.left) / 2;
    :
    : position.left = midx - radius;
    : position.top = b.bottom - height;
    : position.right = midx + radius;
    : position.bottom = b.bottom;
    :
    : Ellipse(ball_dc, position.left, position.top, position.right, position.bottom);
    : }
    :
    : void Ball::draw(HDC hdc) const {
    : BitBlt(hdc, position.left, position.top,
    : 2*radius, 2*radius, ball_dc, 0, 0, SRCINVERT);
    : }
    :
    : void Ball::erase(HDC hdc) const {
    : draw(hdc);
    : }
    : [/code]
    : unfortunately, calling draw(), erase(), move to a new position, draw() shows a black rectangle instead of the circle! can anyone please check what i'm missing?
    : [/purple]
    : [hr][purple]~Donotalo()[/purple]
    :
    [purple]i've got the bug! the circle was not actually painted on the DC with that Ellipse call!
    [/purple]
    [hr][purple]~Donotalo()[/purple]

Sign In or Register to comment.