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++ > CopyScreen Function

#49252 - QuantumDoja - Wed Jul 27, 2005 9:28 pm

Hi, I have the following code, to take a palette and data, and then in mode4 plot the screen, I can get it to work by not using a function, but its these damn pointers Im stuggling with....heres where im up to:

Code:

void CopyScreen(u16 *data, u16 *palette) {
   u16 x,y;
   for(x = 0; x < 256; x++) {
      BGPaletteMem[x] = palette[x];
   }
   for(y = 0; y < 160; y++) {
      for(x = 0; x < 120; x++) {
         PlotPixel(x,y,data[y*120+x]);
      }
   }
}


Calling the function:

Code:

CopyScreen(&logoPalette,&logoData);


I keep getting the errors:
warning: Passing arg1 of "copyscreen" from incompatible pointer type
warning: Passing arg2 of "copyscreen" from incompatible pointer type
_________________
Chris Davis

#49255 - tepples - Wed Jul 27, 2005 9:40 pm

How are logoPalette and logoData declared?
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#49256 - QuantumDoja - Wed Jul 27, 2005 9:45 pm

Code:

u16 logoData[] = {
                    0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF,
                    0xFFFF, 0xFFFF, 0...........


u16 logoPalette[] = {
                    0x0000, 0x0000, 0x0000, 0x0001, 0x0001, 0x0021, 0x0022, 0x0022, 0x0022, 0x0022,
                    0x0023, 0x0042, 0x0043, 0x00....



before I had it like this....but still didnt work

Code:

const u16 logoData[] = {
                    0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF,
                    0xFFFF, 0xFFFF, 0...........


const u16 logoPalette[] = {
                    0x0000, 0x0000, 0x0000, 0x0001, 0x0001, 0x0021, 0x0022, 0x0022, 0x0022, 0x0022,
                    0x0023, 0x0042, 0x0043, 0x00....


_________________
Chris Davis

#49260 - ScottLininger - Wed Jul 27, 2005 10:03 pm

I believe that you want to change this...

Code:
CopyScreen(&logoPalette,&logoData);


Into this...

Code:
CopyScreen(logoPalette,logoData);


Array names are essentially pointers already. If I'm remembering correctly, you don't need the ampersand to pass it by reference.

-Scott

#49261 - QuantumDoja - Wed Jul 27, 2005 10:04 pm

Even tho i get the errors, a rom file is made, and the rom works...how strange.
_________________
Chris Davis

#49301 - Cearn - Thu Jul 28, 2005 9:35 am

If you take the address of an array with & you'll still get the, well, address of the array, which is why the code still works even though the compiler doesn't like it. This is not true for pointers though. Arrays-variables mark a given point in memory; pointer-variables are normal variables which have an address for their values.

Code:

const u8 array[16]= { ... }
// array occupies, say 0800:0000 - 0800:000F
// 'array' gives 0800:0000
// '&array' gives location of array, also 0800:0000

const u8 *ptr= array;
// ptr located at, say, 0300:0000 (IWRAM, where variables go)
// 'ptr' gives ptr's value, which is the address of array
// '&ptr' gives the address of ptr, which is 0300:0000

Just FYI, you can speed up the routine by between 8 and 30 times (depending on the implementation of PlotPixel) by using DMA. At the very least *sigh*, <broken record advice>use ints as counters instead of u16</broken record advise>