Jump to content


Multithreading for sprite explosions


4 replies to this topic

#1 Wade Berkn

    New Member

  • Members
  • PipPip
  • 14 posts

Posted 11 June 2007 - 02:46 AM

Hi, I'm using Allegro to do my graphics and was wondering if there was a way to draw explosions on the screen without halting other processes. Right now I'm trying to figure out how to use threads as defined in the process.h header for c++. But it seems to only except functions with "void*" as a parameter, but the function I created to draw explosions to the screen takes two parameters, an X and Y coordinate. Thanks for any help!

#2 monjardin

    Senior Member

  • Members
  • PipPipPipPip
  • 1033 posts

Posted 11 June 2007 - 05:35 AM

You can use the void* parameter to pass whatever data you want to the thread. However, it doesn't sound like you really need a thread for this. Instead of blocking the whole program while you draw an explosion, just poll a clock and do meaningful work in between advancing the animation.
monjardin's JwN Meter (1,2,3,4,5,6):
|----|----|----|----|----|----|----|----|----|----|
*

#3 Wade Berkn

    New Member

  • Members
  • PipPip
  • 14 posts

Posted 11 June 2007 - 10:03 AM

Sorry I dont understand what you mean by polling the clock. Ty for th void* though, I didnt know that.

#4 geon

    Senior Member

  • Members
  • PipPipPipPip
  • 893 posts

Posted 11 June 2007 - 01:27 PM

It all depends on how you organize your main loop.

It sounds like you went for a event-based approach. (A loop is waiting until the player press a button and draw to the screen accordingly.) That can make sense for some situations, but in a realtime game, you should generally do it the other way around. (Draw the screen all the time and update the player position etc. depending on pressed buttons.)

#5 monjardin

    Senior Member

  • Members
  • PipPipPipPip
  • 1033 posts

Posted 11 June 2007 - 02:37 PM

By polling, I mean instead of doing this:

explosion.setFrame(0)

explosion.draw()

sleep(DELAY)

explosion.setFrame(1)

explosion.draw()

sleep(DELAY)

// ...

poll the time inside the draw routine of your explosion class:

void Explosion::draw()

{

  if (SomeTimeFunction() - lastTime > DELAY)

    setFrame(getFrame() + 1);

  if (getFrame() > maxFrames)

    // delete the sprite


  // draw the frame

}


monjardin's JwN Meter (1,2,3,4,5,6):
|----|----|----|----|----|----|----|----|----|----|
*





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users