[A83] Re: fast clipped sprite routine


[Prev][Next][Index][Thread]

[A83] Re: fast clipped sprite routine



Joe Pemberton writes:
> I thought of that, but how much is the actual speed gain?  In this
> case, not much - the reason being because my game uses a screen that
> is 64 pixels wide and 100 pixels high (it scrolls vertically).

I don't know what the gain would be.  You know your application.  Always
know, not guess, what needs to be optimized before attempting to optimizing
it.  The obligatory Knuth quote here is ``Premature optimization is the root
of all evil''.

> Because of this, usually about 1/3 to 1/2 of the sprites that need to
> be drawn are offscreen, and you actually lose speed by not clipping
> them and drawing them to a large buffer. And the buffer would be
> large - around about 1500 bytes for a buffer that would allow me to
> draw all of my sprites without clipping them (some of them are around
> 18 pixels high).

You can clip the sprites that are completely off screen easily, before
calling the sprite routine.  Partial clipping in the sprite routine is much
slower.  Determine how many sprites you are drawing per frame.  Determine
the speed gain to move to a non clipped sprite routine.  That is your gain.

Also remember that horizontal clipping is a slower than vertical clipping.
Just eliminating that may result in significant gains.

> So the question is - how can I speed this up?  How can I reduce the
> number of times I must loop through the collision detection?

You essentially have an O(n^2) algorithm that you want to make faster.
While there are ways of doing this, the cure might be worse than the
disease.  Sorting, sectors, etc. can speed things up, but could end up
slower than an optimized check loop.  There are some articles about this:

http://www.ddj.com/documents/s=983/ddj9513a/9513a.htm

> The problem is that it is possible that enemies and bullets
> could get out of order relative to each other's coordinates, so I
> would have to manually sort the arrays each time they were updated.

Yep.  And doing so would take a lot of time, possibly more time than it
would save.

--
David Phillips <david@acz.org>
http://david.acz.org/




References: