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.

Coding > Screen control problems

#4823 - Unciaa - Fri Apr 11, 2003 12:36 am

I've written a simple tile-based display engine [32x32 VRAM area, actual map area of undefined sizes, yadayada...] and have started implementing a primitive collision engine [for now assuming my main sprite will always be in a certain position on screen, will see if I need more later].
Now while testing for tile data at coordinates works perfectly, I hit some problems when I try to do them per-direction. This is the relevant code...

Code:

void charcontrol(int direction, int scroll){
   int terminator=1;
   u16 x;
   CopyOAM();
   while(terminator){
      direction=GetInput(direction);
      WaitForVsync();
      if(direction==1){
         x=fullmapX/8+14+(fullmapY/8+9)*mapsizeX;
         if((map[x] == 0x16))
            scrollscreen(direction, scroll);
         }
      direction=0;
      }
   }

int main(void){
   REG_BG0CNT = CHAR_BASE(1) | BG_MOSAIC_ENABLE | BG_COLOR_256;
   SetMode(MODE_1 | BG0_ENABLE | OBJ_MAP_1D| OBJ_ENABLE );
   int i, direction=0, scroll=2;
   
   map=map1;
   MakeSPRITE();
   MakeBKG();
   
   InitializeSprite1();
   while (1){
      charcontrol(direction, scroll);
      }
   }


What the program so far is supposed to do is point a pointer to our current map data, fill the VRAM 32x32 area with appropritate tiles given the starting coordinates [in our case 16x16, so the upper left corner is two tiles right and two tiles down], then just run a character control module.

The character control module creates a 16x16 sprite roughly in the middle [7x5th tile], then runs a loop in which it
-fetches the key direction [1=up 2=down 3=left 4=right 0=else], waits for sync, then if the direction is 1 checks if the tile above us is the designated blank tile. If it is we scroll the screen in the given direction for the [scroll] amount of tiles, reset the direction, then rinse and repeat.
[I'm just using this method until I get around to implementing double buffering... So no excessive flaming on it ;)]

The problem is, if I use
if(direction==1){}
for some reason the screen [at boot up] displays at the very upper left corner, then jumps to the correct position once I move. If I replace that with
if(1){}
the screen displays at the given coordinates correctly.

What the heck? Am I overwriting one memory location with another? I'm pretty sure I'm not [there are 6 integer global variables, one const u8 pointer and the OAM stuff]... Perhaps I'm not taking in account GBA's lag with certain things, but I fail to see what the problem is. Especially since testing for something produces different results from just saying "always", even though we start off the same and that if loop isn't even ran at that time. Help?

#4829 - pollier - Fri Apr 11, 2003 6:43 am

Seems to me your program should run exactly as you said...

scrollscreen() will never be called until direction is 1, and direction will never be 1 until the up key is pressed, so the screen stays at the default position until you press that key.

You need to call "x=fullmapX/8+14+(fullmapY/8+9)*mapsizeX;" and "scrollscreen()" before the loop in order to be sure your screen position is initialised at the start.

Also, may I suggest using a bit shift ">>3" rather than a divide "/8"?

Hope this helps.
_________________
(Works for me!)

#4838 - Unciaa - Fri Apr 11, 2003 3:07 pm

D'oh! Much thanks, didn't realize the display behaved that way. :)
Also, thanks for the bit shift hint; I'm too used to just using C for school assignements where details like that don't represent enough of a speed difference to matter. *g*
*goes replace all the gazillion '*32's with '<<5's*

#4842 - pollier - Fri Apr 11, 2003 11:22 pm

No problem :)

While we're on the topic, does anyone know if GCC will optimize out power of 2 divides, or multiplications, for that matter, and make them into shifts? Maybe one of the higher -O levels...?
_________________
(Works for me!)