gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Game Design > many many sprites

#69887 - christosterone - Thu Feb 02, 2006 8:06 pm

I am designing the graphics engine structure for an RTS im making for the gba. there are many units and many projectiles and many other graphical entities which would be OAM screen sprites.

i quickly exceeded my 128 sprite limit.

i was thinking about only displaying the units which would be visible on the part of the map that my gba would be displaying. i would make rectangles and use somes algorithms to see which intersect a rectangle thats the screen etc.

Even if i got it to work, it seems like i would have to redefine all 128 sprites every drawing loop cycle.

does anybody think that will work or is there another way i should be doing this before i spend a lot of time coding.

chris

#69895 - DekuTree64 - Thu Feb 02, 2006 8:52 pm

Yeah, that's what I'd do. You're pretty much going to have to touch every OAM anyway to update their positions, so it's not much worse to put it in a different slot too.

Have each sprite store a pre-setup OAM entry. Then traverse the sprite list each frame, checking if each one is on the screen, and if so, claim the next real OAM and copy in your pre-setup one (and update the position).

For this kind of system, it's usually best to also keep a 'shadow' OAM array in normal RAM, and then DMA it to the hardware OAM during VBlank. That way you don't get any tearing or flickering as you re-populate the array.

If your units need to be sorted by Y coordinate for depth sorting, then you can just keep your sprite list sorted to begin with. As you render them out to sequential OAM entries, they will naturally be sorted by the hardware.
_________________
___________
The best optimization is to do nothing at all.
Therefore a fully optimized program doesn't exist.
-Deku

#70614 - christosterone - Tue Feb 07, 2006 8:12 pm

i used to write java games (sigh) but thank god ive moved on. the only issue is there was a Rectangle class in the javalib that had such functions as "bool intersects(Rectangle r1, Rectangle r2)", and "bool contains(...)".

are there any rectangle intersection algorithms anywhere or should i just write it myself?

-chris

ps thanks for the idea of making another sprite array then DMAing it.

#70662 - tepples - Wed Feb 08, 2006 1:40 am

Rectangle intersection is easy. Think of it as an analytic geometry problem, taking it one edge at a time. Untested code:
Code:
typedef struct Rectangle {
  int l, t, r, b;
} Rectangle;

boolean intersects(const Rectangle *a, const Rectangle *b) {

  /* Copy a into temporary variables */
  int l = a->l;
  int t = a->t;
  int r = a->r;
  int b = a->b;

  /* Clip the sides of the temporary rectangle to sit within b */
  if(l < b->l)
    l = b->l;
  if(t < b->t)
    t = b->t;
  if(r > b->r)
    r = b->r;
  if(b > b->b)
    b = b->b;

  /* If a is still positive in width and height, then the rectanges overlap */
  return (l < r) && (t < b);
}

_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#70974 - christosterone - Thu Feb 09, 2006 8:43 pm

ahh thankyou. your code seems much cleaner than mine because my Rectangle structs dont have top, bottom, left, and right values. they have:

Code:
typedef struct Rectangle
{
   u16 x, y;
   u8  w, h;
}Rectangle;


i'll have to modify your code a bit to fit my needs because the width and height of my rectangles will remain constant. i like that your way is so much simpler; my process was about 5x longer :-)

thanks, chris

#70986 - naleksiev - Thu Feb 09, 2006 9:47 pm

There is a smaller solution

Code:
boolean intersects(const Rectangle *a, const Rectangle *b)
{
   return (a->l < b->r) && (b->l < a->r) && (a->t < b->b) && (b->t < a->b);
}