Upcoming Events
Unite 2010
11/10 - 11/12 @ Montréal, Canada

GDC China
12/5 - 12/7 @ Shanghai, China

Asia Game Show 2010
12/24 - 12/27  

GDC 2011
2/28 - 3/4 @ San Francisco, CA

More events...
Quick Stats
64 people currently visiting GDNet.
2406 articles in the reference section.

Help us fight cancer!
Join SETI Team GDNet!
Link to us Events 4 Gamers
Intel sponsors gamedev.net search:

Contents
 Getting the Compiler
 Working

 Screen modes
 Drawing a
 pre-made image


 Printable version
 Discuss this article

The Series
 Volume I
 Volume II
 Volume III

Drawing a Pre-Made Image

Drawing an image made in another program is remarkably easy - I've practically given you all the information already.

First, get an image converter/creation program. www.gbadev.org is a great place to get one. With an image converter, take the .bmp or .gif or .pcx or whatever (depending on what the program uses), and convert the image to a standard C .h file. If your image uses a palette, this .h file should have both palette information and image information stored in separate arrays.

Now, all drawing the image consists of is reading from the arrays. Read from the palette array (if there is any) and put the data into the palette memory. To draw the image, simply run a loop that goes through your array and puts the pixels defined in the array at the desired location.

Naturally, these steps will be slightly different depending on what program you're using. However, being the nice guy that I am, I'm going to provide you with a quick example.

The program: Gfx2Gba v1.03 by Darren (can be found at www.gbadev.org, read the readme for instructions)

The image: A 240*160, 8 bit image named gbatest.bmp

The command line: gfx2gba gbatest.bmp gbatest.h -8 -w 240

The code:

#include "gba.h"
#include "screenmodes.h"
#include "gbatest.h"
 
u16* theVideoBuffer = (u16*)VideoBuffer;
u16* theScreenPalette = (u16*)BGPaletteMem;
 
#define RGB(r,g,b) (r+(g<<5)+(b<<10))   //Macro to build a color from its parts
 
int main()
{
  SetMode(SCREENMODE4|BG2ENABLE);

  //Copy the palette
  u16 i;
  for ( i = 0; i < 256; i++ )
    theScreenPalette[ i ] = gbatestPalette[ i ];

  //Cast a 16 bit pointer to our data so we can read/write 16 bits at a time easily
  u16* tempData = (u16*)gbatest;

  //Write the data
  //Note we're using 120 instead of 240 because we're writing 16  bits
  //(2 colors) at a time.
  u16 x, y;
  for ( x = 0; x < 120; x++ )
    for ( y = 0; y < 160; y++ )
      theVideoBuffer[ y * 120 + x ] = tempData[ y * 120 + x ];

  return 0;
} 

And that's all! I didn't use the backbuffer, because I was just trying to make a point. I used Mode 4 so that I could press the fact that you MUST write 16 bits at a time. If you were using Mode 3/16 bit color, you wouldn't need the tempData pointer. You would also change the 120s back to 240s.

In fact, that's the end of this article. By now, you should have bitmapped modes mastered, or at least you should have a relatively firm grasp on them.

Next Article

In the next article, I'm going to give you information on a rather easy topic - input. If you're lucky, I might even make a game demo to show off.

Acknowledgements

I would like to thank dovoto, as his tutorials have been my biggest source of information on GBA development since I started. Check out his site at www.thepernproject.com. I'd also like to thank the guys in #gamedev and #gbadev on EFNet in IRC for all their help, and all the help they'll be giving me as I write these articles. Furthermore, I would like to thank www.gbadev.org/. The site is a great resource, and it is definitely worth your time.

If you have any questions or comments, you can always e-mail me at genesisgenocide@yahoo.com or nairb@usa.com. I can't promise I'll answer your questions, like your comments, or listen to your suggestions, but I'll at least read the e-mail (as long as it doesn't look like spam).