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 > First experience with graphic

#43655 - Dude? - Wed May 25, 2005 2:02 pm

Hello!
I wrote this little code

int x, y;

//switch to video mode 3 (240x160 16-bit)
REG_DISPCNT = (MODE_3 | BG2_ENABLE);

//fill the screen
for (x = 0; x < 239; x++)
{
for (y = 0; y < 159; y++)
{
DrawPixel3(x, y, RGB(x*2,0,(x+1)));
}
}

to fill the screen with a violet/pink shading, but I noticed it does not cover the entire screen, it misses the last row and the last column of the sceen which is black. Why? Is not the screen 240*160?
If I change obviously the code with 240 and 160 instead of 239 and 159 it works, but this mean I am supposing the scren is 241*161...
Could you help me please?

#43659 - Kyoufu Kawa - Wed May 25, 2005 2:16 pm

Code:

   for (x = 0; x < 239; x++)
      {
      for (y = 0; y < 159; y++)
      {
      DrawPixel3(x, y, RGB(x*2,0,(x+1)));
      }
   }


Off by one. The screen -is- 240 by 160. Your code however checks for X to be -lower- than 239, same for Y. Try changing both to <= instead of just <. That'll include the last row and column. It's an easy mistake to make.

/guilty

#43660 - tepples - Wed May 25, 2005 2:16 pm

This line
Code:
for (x = 0; x < 239; x++)

will loop from 0 to 238 inclusive. Change the 239 to 240 and the 159 to 160 to properly accommodate a 240x160 screen.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#43661 - Dude? - Wed May 25, 2005 2:27 pm

Ops, I feel so stupid! Thank you!