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 > question about mode5

#46414 - funkaster - Fri Jun 24, 2005 11:48 am

If I remember correcly, mode5 is 160x128... so I'm trying to display a 160x128 image (a TGA file, wich is loaded fine). But for some reason, the file is displayed like if the screen where 240x160 (its in the upper left corner), this is the code I'm using:

Code:

    if ((img = load_tga (foot, &ll)) != NULL)
    {
        SetMode(MODE_5 | BG2_ENABLE);
        if (ll == 0)
        {
            int x, y;
            for (y = SCREEN_HEIGHT-1; y >= 0; y--)
                for (x=0; x < SCREEN_WIDTH; x++)
                {
                    /* TGA is 24 bit */
                    unsigned char r = *img++;
                    unsigned char g = *img++;
                    unsigned char b = *img++;
                    VB[x+y*SCREEN_WIDTH] = RGB8(r,g,b);
                }
        }
        else
        {
            for (i=0; i < SCREEN_WIDTH*SCREEN_HEIGHT; i++)
            {
                /* 16 bits tga: GGGBBBBB ARRRRRGG */
                unsigned char r = *img++;
                unsigned char g = *img++;
                unsigned char b = *img++;
                VB[i] = RGB8(r,g,b);
            }
        }
        while (1) {}
    }
    return 0;

Any idea why this could be happening?

#46416 - tepples - Fri Jun 24, 2005 12:40 pm

Because you're not scaling or moving the image. Use background 2's affine registers to do that. Common uses include centering the image, or rotating it by 90 degrees and stretching it by a factor of 2.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#46420 - strager - Fri Jun 24, 2005 3:39 pm

funkaster wrote:
Code:
for (i=0; i < SCREEN_WIDTH*SCREEN_HEIGHT; i++)


Maybe changing the dementions should help. Replace SCREEN_WIDTH*SCREEN_HEIGHT with 192*128. That should help.

#46423 - funkaster - Fri Jun 24, 2005 4:21 pm

tepples wrote:
Because you're not scaling or moving the image. Use background 2's affine registers to do that. Common uses include centering the image, or rotating it by 90 degrees and stretching it by a factor of 2.


Yep, that showed the image *almost* at full screen. But I'm still using a 160x128 image... should I use different size? (this is how it looks now):
http://www.rolando.cl/gba/vba.png
Also, where's info about what this does:
Code:

        REG_DISPCNT = 5+((page&1)<<4)+(1<<10); /* mode 5, page, bg2 enable */
        (&REG_DISPCNT)[0x10] = 0;
        (&REG_DISPCNT)[0x11] = 256;
        (&REG_DISPCNT)[0x12] = 128;
        (&REG_DISPCNT)[0x13] = 0;

(adapted from the code that tepples suggested)
thanks![/url]

#46425 - tepples - Fri Jun 24, 2005 4:38 pm

The code sets up BG2's affine matrix to stretch the background and swap the X and Y axes. Here's a clearer version of the code:
Code:
/* this goes in your header file */

typedef signed   short s16;
typedef unsigned int   u32;

struct BGAFFINEREC
{
  s16 pa;  /* map_x increment per pixel */
  s16 pb;  /* map_x increment per scanline */
  s16 pc;  /* map_y increment per pixel */
  s16 pd;  /* map_y increment per scanline */
  u32 x_origin, y_origin;
};

#define BGAFFINE ((volatile struct BGAFFINEREC *)0x04000000)



/* this goes somewhere in your code */

BGAFFINE[2].pa = 0;
BGAFFINE[2].pb = 256;
BGAFFINE[2].pc = 128;
BGAFFINE[2].pd = 0;

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