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 > How to get at a background's map contents?

#27359 - mr_square - Mon Oct 11, 2004 1:36 pm

My knowledge of pointers is a bit shakey, so bear with me :)

In my bg.h file I have this:

#define ScreenBaseBlock(n) (((n)*0x800)+0x6000000)

which is where the map data for the background is stored, right? Say my background 512x512 background is store at screenblock 28 - how do I get the contents of that memory address? Would this work:

Code:

u16 blah;
blah = &ScreenBaseBlock(28);


? And how would I get at an offset into that area of memory?

The reason for all this is that I'm toying around with collision detection at the moment and need to get at the map contents of my background at a particular location.

thanks :)

#27363 - yaustar - Mon Oct 11, 2004 4:04 pm

Unless you map data is created during runtime, wouldnt it be easier to get the information from the original map data rather then the memory?
_________________
[Blog] [Portfolio]

#27369 - getch - Mon Oct 11, 2004 6:52 pm

[quote="mr_square"]
Code:

u16 blah;
blah = &ScreenBaseBlock(28);

[quote]

Almost.
Try:
Code:

u16* blah;
blah = ScreenBaseBlock(28);

_________________
-pb

#27371 - keldon - Mon Oct 11, 2004 7:25 pm

Basically what getch has changed is that blah should be a pointer to store the address ScreenBaseBlock. When you write &value, you are returning the address of value; only ScreenBaseBlock is not a variable - and even if it was a pointer &ScreenBaseBlock would return the address of the pointer, not the address pointed to by the pointer.

#27376 - sgeos - Mon Oct 11, 2004 9:05 pm

The Pointer Story Chapter One- Where does x live?

Code:
int x = 7;
int y = 12345;


x is an integer. It contains the number 7. x exists in the physical memory location 0x020000F4. y is an integer. It contains the number 12345. It exists at the physical memory location 0x020000F8.

Code:
int *x_ptr = &x;


x_ptr is a pointer. It contains 0x020000F4. This is the address of x. x_ptr has an address of 0x020000F8.

Code:
x_ptr = &y;


x_ptr now contains 0x020000F8. This is the address of y.

Code:
*x_ptr = x;


*x_ptr is the value stored at the location of x_ptr. The variable y lives at 0x020000F8. It has a value of 12345. We just changed that value to the value of x. y now equals x. We used a poorly named pointer to get the job done. =P

Code:
int   x =      /* variable */
      7;   /* constant */
int *   x_ptr =      /* pointer */
      &x;   /* address of variable */
*x_ptr =      /* data at address */
      12345;   /* constant */


All pointers hold memory addresses. The only difference between a pointer to int and a pointer to float and a pointer to anything else is how they try to interpret the data stored at the address they contain. A void pointer just contains an address. There is no way to interpret the data at that address.

-Brendan


Last edited by sgeos on Mon Oct 11, 2004 9:10 pm; edited 1 time in total

#27377 - sajiimori - Mon Oct 11, 2004 9:07 pm

Hey, don't confuse people. :P You declared your "pointer" as a regular integer.

#27378 - sgeos - Mon Oct 11, 2004 9:11 pm

sajiimori wrote:
Hey, don't confuse people. :P You declared your "pointer" as a regular integer.

I hate it when I do that. Once last night, once today... =P Thanks for catching my mistake.

-Brendan