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.

C/C++ > Having more than 6 ships in collection causes crash

#83010 - Lini - Fri May 12, 2006 12:27 am

I can't seem to figure this out myself.

I have 2 arrays for ship placement, one for horizontal and one for vertical:
Code:

const u8 IN_ROW[21] = {0,0,0,0,0,0,0,1,1,1,1,1,1,1,2,2,2,2,2,2,2}; //The row each ship is in
const u8 IN_COL[21] = {0,1,2,3,4,5,6,0,1,2,3,4,5,6,0,1,2,3,4,5,6}; //The column each ship is in


I use ham_CreateObj() to create the ships at the correct x/y position:
Code:

   while(i < MAX_IN_COLLECTION)
   {
      if(ships_type[i] != NO_SHIP)
      {
         ships[i] = ham_CreateObj((void*)&shipspr_Bitmap,
OBJ_SIZE_16X16,OBJ_MODE_NORMAL,1,0,0,0,0,0,0,((IN_COL[i])*19)+4,((IN_ROW[i])*19)+103);

         ham_UpdateObjGfx(ships[i],(void*)&shipspr_Bitmap[256*ships_type[i]]);
      }

      i++;
   }


I also update the ship graphic according to ships_type (which is the ship type).
Yes, I know I could use a 'for' loop, I just forgot about i when I coded this. It was about 2AM, and I was tired.

But when I get more than 6 ships in collection, it causes the graphics to bug out, and it crashes when you try to go to flight-mode after that.

Other related code:
Code:

#define NO_SHIP            150
const u8 MAX_IN_COLLECTION = 21;


The interface background (I'm using a 256 color bitmap, had to convert to gif to upload/show):
[Images not permitted - Click here to view it]

#83038 - ScottLininger - Fri May 12, 2006 6:29 am

I'm not familiar enough with HAM to say for certain, but it sounds like you're overflowing your sprite tile memory.

If you're using VisualBoy for emulation, you can use the Tile viewer to peek into your sprite tile memory and see how full it is.

If I'm right about the problem, your solution might be to more carefully share tiles between ships so that you're minimizing the # of tiles. Your other solution might be to go to 16-color tiles rather than 256.

-scott

#83056 - sgeos - Fri May 12, 2006 1:02 pm

Lini wrote:
Yes, I know I could use a 'for' loop, I just forgot about i when I coded this. It was about 2AM, and I was tired.

You should promptly fix things that seem silly instead of trying to excuse coding behavior. I'll let someone else bring up the counter argument =)

I don't know enough about HAM to help you. Read over other parts of your code. The problem might be somewhere else. I've had times where I know the problem is *here*... then after six hours and a nights sleep, I find the problem *there*. =P

Good luck!
-Brendan

#83693 - ProblemBaby - Wed May 17, 2006 10:00 pm

a buffer with 21 u8's would cause misaligned data I think.
change to 24 to be safe.