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 > Plotting Tiles

#32755 - QuantumDoja - Sat Dec 25, 2004 10:07 pm

Hi, I have a 1D array that stores all of my tiles, is there a special way i am supposed to draw them to the screen, or can i simply go:

get map next tile
draw tile
loop

?????
_________________
Chris Davis

#32757 - Lord Graga - Sat Dec 25, 2004 11:25 pm

uhm...


there are several ways to load maps, but it's really just a standard datatransfer to the memory. Let us start off with the slowest, but most "understandable":

Code:
#define SCREENBASE    24

//Insert tile loading routine here

u16* mapmem = 0x6000000+0x800*SCREENBASE;
int x, y;
for(x = 0; x < 32; x++)
{
   for(y = 0; y < 32; y++)
   {
      mapmem[x+y*32] = map[x+y*32];
   }
}


Don't use the code about. It's slow and ugly.


Then we improve it a bit:

Code:
#define SCREENBASE    24

u16* mapmem = 0x6000000+0x800*SCREENBASE;
int i;
for(i=0;i<32*32;i++) mapmem[i] = map[i];


Looks a little better.


But, in the end, let us use a BIOS call (grab some headers from libgba):

Code:
#define SCREENBASE    24

CPUFastSet((void*)&map,(void*)0x6000000+0x800*SCREENBASE,512);


That is prolly one of the fastest methods that you can do, and it's easy to understand. Use CPUFastSet as much as you can :)

#32770 - Cearn - Mon Dec 27, 2004 5:11 pm

errr, yeah, what he said, CpuFastSet is great. Just make sure you understand what the arguments are first. From GBATek, BIOS routines:
Quote:

Memory copy/fill in units of 32 bytes. Memcopy is implemented as repeated LDMIA/STMIA [Rb]!,r2-r9 instructions. Memfill as single LDR followed by repeated STMIA [Rb]!,r2-r9.
The length must be a multiple of 32 bytes. The wordcount in r2 must be length/4, ie. length in word units rather than byte units.
r0 Source address (must be aligned by 4)
r1 Destination address (must be aligned by 4)
r2 Length/Mode
Bit 0-15 Wordcount (must be multiple of 8 WORDs, ie. 32 bytes)
Bit 24 Fixed Source Address (0=Copy, 1=Fill by WORD[r0])

I.e., first argument is source address, second arg is destination and third arg is the word-count, not the number of bytes. That's why it's 512 and not 32*32=1024.

Using DMA will work too, of course, in fact it's probably just a little bit faster. (In case you're interested, I did a little speed test of various copying methods here).