Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: skid marks  (Read 4371 times)

0 Members and 1 Guest are viewing this topic.

Bernd

  • Newbie
  • *
  • Posts: 11
    • View Profile
skid marks
« on: October 20, 2010, 10:44:22 pm »
Hi,
I'm working on a topdown racing game (GTA like).
I want to draw skid marks of my cars. What would be a fast way to do this?

I tried sf::Shapes in a list, which obviously doesn't work for very long ;-)
Then I tried to have a initially transparent image (same size as the screen) and then use setpixel to draw the skid marks. But even with only one pixel drawn per frame, there is a huge framerate drop.

Spidyy

  • Sr. Member
  • ****
  • Posts: 493
    • View Profile
skid marks
« Reply #1 on: October 20, 2010, 10:46:59 pm »

Bernd

  • Newbie
  • *
  • Posts: 11
    • View Profile
skid marks
« Reply #2 on: October 21, 2010, 08:36:41 am »
Nice project (though a bit overkill for me), but I want to draw the skid marks permanently on the screen.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
skid marks
« Reply #3 on: October 21, 2010, 08:54:28 am »
Quote
I tried sf::Shapes in a list, which obviously doesn't work for very long ;-)

Why? How many marks do you have?
Laurent Gomila - SFML developer

Bernd

  • Newbie
  • *
  • Posts: 11
    • View Profile
skid marks
« Reply #4 on: October 21, 2010, 09:00:47 am »
Everytime a car slides (in a curve for example), I want to draw permanent skidmarks for every tire that slips.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
skid marks
« Reply #5 on: October 21, 2010, 09:20:18 am »
Ok, but you didn't exactly answer my questions:
- why do you say that sf::Shape doesn't work? did you try it?
- how many marks maximum (approximately) can you have? tens? thousands? millions? all visible on screen?
Laurent Gomila - SFML developer

Bernd

  • Newbie
  • *
  • Posts: 11
    • View Profile
skid marks
« Reply #6 on: October 21, 2010, 10:09:38 am »
I tried shapes. I added a shape to a list every frame (when the car slides), and then drew the hole list. The framerate droppes at a couple hundred shapes.

I don't use a 'camera', so the hole map is always visible, and so are the skidmarks, which probably could easily reach 10000. That's why I wanted to use pixels.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
skid marks
« Reply #7 on: October 21, 2010, 10:14:09 am »
Ok, understood.

To use an image efficiently, you need to work on your own array of pixels (a std::vector<sf::Uint8> for example, or even a sf::Uint8[] if size is fixed), work on it with direct access, and update the sf::Image in one call with LoadFromPixels. This should be fast enough. SetPixel is not efficient at all, it is made for convenient access to pixels, not for real-time update.
Laurent Gomila - SFML developer

Bernd

  • Newbie
  • *
  • Posts: 11
    • View Profile
skid marks
« Reply #8 on: October 21, 2010, 10:33:29 am »
Thanks Laurent, will try that.

Bernd

  • Newbie
  • *
  • Posts: 11
    • View Profile
skid marks
« Reply #9 on: October 21, 2010, 01:11:00 pm »
I got it working now, the code is not complete yet (and contains errors), but the performance is bad :-(

initialization
Code: [Select]
Color c(0,0,0,0);
imgSkids = new Image(SCREEN_WIDTH, SCREEN_HEIGHT, c);
sprtSkids = Sprite(*imgSkids);
int dim = imgSkids->GetWidth() * imgSkids->GetHeight() * 4;
skidmarks = new Uint8[dim];
for (int i = 0; i < dim; i++)
i % 4 != 0 ? skidmarks[i] = 0 : skidmarks[i] = 255;


called once per frame
Code: [Select]

sprtCarPos = sprtCar.GetPosition();
Uint8 *pixel = &skidmarks[(int)((sprtCarPos.x + (sprtCarPos.y * imgSkids->GetWidth())) * 4)];

if (ABS(lateralforce_f.y) > ABS(MAX_GRIP))
{
*pixel = 0;
*(pixel+1) = 0;
*(pixel+2) = 0;
*(pixel+3) = 255;
imgSkids->LoadFromPixels(imgSkids->GetWidth(), imgSkids->GetHeight(), skidmarks);
w.Draw(sprtSkids);
}

 

anything