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.

Beginners > Dynamic Map Loading Woes

#8938 - XeroxBoy - Thu Jul 24, 2003 11:24 am

OK, basically, I've been trying for quite a good chunk of time getting dynamic map loading (i.e. swapping in rows/columns as you scroll) to work. I perfectly understand the concept, and, from what I can tell, nearly have it working correctly, but I still seem to be having some troubles. The issue is complicated by the fact that, as a method of compressing my maps, I store them in 16x16 tile format. So, I would greatly appreciate it if anyone could take a look at my code and point out exactly what I'm doing wrong. And, yes, I'm well aware that the code is actually rather sloppy and unoptimized - I subscribe to the school that optimizations should be done after the code actually works right. Also note that this comes from my map editor (ideally, I design the map on the GBA and save it to SRAM), so there's no smooth scrolling and things like variable names make more sense coming from that context.

Code:

u16 MapScrollVert = 0; // Keeps track of, in pixels, how far the screen has scrolled
u16 MapScrollHoriz = 0;
u8 CursorY = 0; // Keeps track of where exactly the cursor is in the world (in 16x16 tile notation)
u8 CursorX = 0;
u8 TempScrollY; // Used for jamming into the scroll registers
u8 TempScrollX;
u8 CounterDown=9; // Where the bottom of the screen is (in 16x16 tile notation)
u8 CounterUp=0;
u8 CounterRight=14;
u8 CounterLeft=0;
u8 CounterX; // How many times we've scrolled an entire screen
u8 CounterY;

int main()
{
   // Random Crap
   DisplayMap(); //Loads the first section of the map into VRAM (works perfectly)
   do
   {
      if(!( (*KEYS) & KEY_DOWN))
      {
         if(MapScrollVert == 864) // Cursor is near the end of the screen
         {
            if(CursorY!=63) // If cursor ISN'T at the end of the screen
            {
               CursorY++;
            }
         }
         else
         {
            CursorY++;
            LoadRowDown();
            TempScrollY+=16;
            REG_BG0VOFS = TempScrollY;
            MapScrollVert+=16;
         }
      }

// more crap, including the other directions, the end of the loop, etc. If no one can find the problem in just this, I'll post the rest

void LoadRowDown()
{
   u8 x;
   u8 tempx = 0;
   u8 tempy;
   u8 y;

   CounterDown++;
   CounterUp++;

   if(CounterDown==16)
      CounterDown=0;
   if(CounterUp==16)
   {   
      CounterUp=0;
      CounterY++;
   }


   tempy = CounterDown*2;

   y = CursorY;

   for (x=CounterX*16;x<CounterX*16+16;x++)
   {
      m0[tempy*32+tempx]=Map[y*32+x];
      m0[tempy*32+tempx+1]=(Map[y*32+x]+1);
      m0[tempy*32+tempx+32]=(Map[y*32+x]+2);
      m0[tempy*32+tempx+33]=(Map[y*32+x]+3);
      tempx += 2;
   }
}