- Online Community Forums » Programming Questions » trail

This thread is locked; no one can reply to it. rss feed Print
neil dwyer
Member #7,237
May 2006

In my pong game. I want to have the ball leave a trail of putpixels. The only problem with this is that I am clearing the buffer every loop so the putpixel from the previous loop does not stay. Anyone have an idea to get around this.

Member #1,548
September 2001

Make a particle class and use it to leave a trail, that way it gets treated like every other object and is redrawn.

neil dwyer
Member #7,237
May 2006

The old pixels would still be cleared from the buffer, no?

Edit: is there a way to clear certain objects from a bitmap without clearing the whole bitmap.

Member #6,239
September 2005

Yes, the "old" pixels will be cleared when you clear your frame buffer, but you will be redrawing. What you could do is record the last 10 frames' (x,y) coordinates for the ball. Then, in your drawing, you would redraw all 10 frames using the ball's bitmap, but you could also add alpha blending. Here is an example of what I'm talking about.

Our point structure, so we can easily manage 2D points

typedef struct Point2D
  int x;
  int y;

Declare our array of trails

  #define MAX_TRAILS     10
  Point2D BallTrail[MAX_TRAILS+1];

Our drawing method for ball

1void Ball::Draw(BITMAP *dest)
3 for(int i = 0; i < MAX_TRAILS; i++)
4 {
5 int per = ( 128 * ((float)i/MAX_TRAILS) ); // calculate the percentage
6 // which is used in the alpha
7 set_trans_blender(per, per, per, per);
8 draw_trans_sprite(dest, sprBall, BallTrail<i>.x, BallTrail<i>.y);
9 }
11 // turn off trans blending, draw our REAL ball
12 set_trans_blender(255, 255, 255, 255);
13 draw_sprite(dest, sprBall, x, y);
15 // update our trails
16 BallTrail[MAX_TRAILS].x = x;
17 BallTrail[MAX_TRAILS].y = y;
18 for(int i = 0; i < MAX_TRAILS; i++) {
19 BallTrail<i>.x = BallTrail[i+1].x;
20 BallTrail<i>.y = BallTrail[i+1].y; }

You will need to go through and pound out a few of the minor things (such as fully initialising the array), and this code is untested, but at least it gives you the idea.

SolarStrike Software - MicroMacro home - Automation software.

Member #907
January 2001

I don't know if either explaination makes it clear or not, but the goal is to keep in memory enough information to redraw the trail.


is there a way to clear certain objects from a bitmap without clearing the whole bitmap.

The dirty rectangle system (DRS) is a programming technique to do it. The Allegro demo game has an implementation.
It works by making a small copy of the background BITMAP, before putting any sprite there. So you can later "remove" the sprite by overwriting it with the stored copy.
It's mostly intended for sprites and such, so in your case (pixel-size particles), it should work but is probably quite inefficient.

neil dwyer
Member #7,237
May 2006

Ok since my physics are non-existent(ball_x adds until it hits the paddle, then it subtracts;ball_y adds until it hits the wall then it subtracts) I declared 10 variables(5 for the five pixel's x values and 5 for the y's). It would of made sense to make a tail[5][2] but I wasn't thinking about clean code. Tail1 is always equal to the middle of the ball. If the ball_x is adding, then the next tail's x value is ball_x - 5.

I had the idea to make the program "remember" where the ball has been from 5 frames before but I didn't know how to do it. I'll look in to elverion's idea.

Go to: