Monday, April 11, 2011

Hello and welcome to the nightmare. (Sortof) part 3

Lets do something funner than just create pixels on the fly.  How about loading an really crappy image.




 Oh God thats crappy,  oh who cares I'm no artist, just a bored programmer who has nothing better to (btw I drew this in about a minute :D).  Here is what I'm going to demo today.  How to load images on the lil old gba.  Note there are some strange issues, like I said before gba programming or any console in general is very low level.  Just as low-level as OS developing, so there isn't any file system you can use.  Here come the tool grit.
 Grit is a simple tool used trance forms your sexy artwork and my non-so-sexy artwork into an array that the gba can read.  Instead of loading via file you compile it with your binary, cool huh.  One suggestion though make sure you compile your gfx data to a separate library because it will make your *.gba files smaller.  Just a suggestion that's all.

grit crappy.png -gb -gB32 -p! -ftc

The grit tool contains many and I do mean many option to get your artwork workable.  -gb basically tells grit that it is a bitmap if you do -gt then it would be a tile instead (I'll get to that later).  -gB32 Produces a Uint32 (unsigned int btw) array of image data.  -p! this options is for pal memory more on that later. -ftc creates a c file that contains the array, if you don't specify this option then it will be in ARMS or THUMBS asm instruction instead ( I haven't done much asm stuff yet but I'll do it later data ).  Grit come with devkitarm as one of it standard tool and m'yes it a great tool.
 Ok more source code and less talking.

// Here is some simple type defs for the code
#include <string.h>
#include "crappy.h"
typedef unsigned short Uint16;
typedef unsigned int Uint32;

typedef Uint16 Color;

// Memory locations
#define MEM_IO             0x04000000 // IO Memory Location
#define MEM_VRAM         0X06000000 // VRam Memory Locations

// Video Modes
// These are tiled modes
#define VID_MODE0        0
#define VID_MODE1        0x0001
#define VID_MODE2        0x0002
// These are bitmap modes
#define VID_MODE3        0x0003 // 240x160 16 color no page flipping
#define VID_MODE4        0x0004 // 240x160 8 color page flipping
#define VID_MODE5        0x0005 // 160x128 16 color page flipping
// These are additional mode for background stuff
#define VID_BG0            0x0100 // Background 0
#define VID_BG1            0x0200 // Background 1
#define VID_BG2            0x0400 // Background 2
#define    VID_BG3            0x0800 // Background 3
// More mode later
#define VRAM            ((Color*)MEM_VRAM) // Video Ram accessor Macro

#define VID_CNT            *(Uint32*)(MEM_IO+0x0000) // Video Controle
#define RGB15(r, g, b)    (r | (g<<5) | (b<<10)) // Simple Color Macro
#define M3_WIDTH 240 // Mode 3 width

int main() {
    VID_CNT = VID_MODE3 | VID_BG2;
   
    memcpy(VRAM, crappyBitmap, crappyBitmapLen);
  
    while(1);
   
    return 0;
}

As you can see there are 3 changes to the source, first one is that I'm including string.h and if any one knows c this file contains functions for c string functionality and it contains a very useful function for copying memory or memcpy.  Its not perfect for gba because of data alignment issues but it still gets the job done for simple examples.  crappy.h perfect name isn't it, this is the header file that was generated from the tool grit.  It contains the both crappyBitmap array and the crappyBitmapLen which is the size of the image data. The array data its self is being housed in the crappy.c file which is just a huge array and nothing else.  As you can see in the main function I'm copying the crappy bitmap into the VRAM.  And that is pretty much it.
If you are see this then congrats, you're gba need anger management.  Well more laters.

Demo 4 files

http://www.filehosting.org/file/details/218842/demo4.zip

No comments:

Post a Comment