Showing posts with label GBA Programming. Show all posts
Showing posts with label GBA Programming. Show all posts

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

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

Well here part 2 of my hello intro I suppose...  Here I'm just going to revise the code I posted last night for the gba example that looked like crap.  It was really not that readable at all.  So this post I'm going to clean it up a bit and show you how nice you can make it.  Here the rearranged demo.

// Here is some simple type defs for the code
typedef unsigned short Uint16;
typedef unsigned int Uint32;

typedef Uint16 Color;

#define MEM_IO             0x04000000
#define MEM_VRAM         0X06000000

// 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
#define VID_BG1            0x0200
#define VID_BG2            0x0400
#define    VID_BG3            0x0800
// More mode later
#define VRAM            ((Color*)MEM_VRAM)

#define VID_CNT            *(Uint32*)(MEM_IO+0x0000)
#define RGB15(r, g, b)    (r | (g<<5) | (b<<10))


int main() {
    //*(unsigned int*)0x04000000 = 0x403;
   
    VID_CNT = VID_MODE3 | VID_BG2;
   
   
    //((unsigned short*)0x06000000)[120+80*240] = 0x001F;
   
    VRAM[120+80*240] = RGB15(31, 0, 0);
   
    while(1);
   
    return 0;
}

A lot more readable isn't it.  Its still quite simple though.  Like before it using video mode 3 on background 2.  And it still colors a pixel red at 120x80.  Still quite simple.  But it looks nicer.

Now as you can see the gba has 6 different vid modes.  The first 3 are tile mode.  These mode do hardware accelerated tile-mapping.  Most gba games use these 3 mode because they are extremely quick (NOTE: gba was built from the ground up for 2d games not 3d so the hardware was built specifically to speed up 2d apps.  Its possible to do 3d on the gba if you don't mind software rendering which really suck. )
 The mode I'll show here for now is bitmap mode which isn't really built for games unless you are doing a doom type game using ray casting.  But its easier than the tile modes when starting out.
 Enough chit chat more demo.  This demo is still boring and not real fun at all.  Its just a damn red dot in the middle of the screen. Lets try a simple yet kind of cool effect.

// Here is some simple type defs for the code
typedef unsigned short Uint16;
typedef unsigned int Uint32;

typedef Uint16 Color;

#define MEM_IO             0x04000000
#define MEM_VRAM         0X06000000

// 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
#define VID_BG1            0x0200
#define VID_BG2            0x0400
#define    VID_BG3            0x0800
// More mode later
#define VRAM            ((Color*)MEM_VRAM)

#define VID_CNT            *(Uint32*)(MEM_IO+0x0000)
#define RGB15(r, g, b)    (r | (g<<5) | (b<<10))
#define M3_WIDTH 240

int main() {
    int i, j, c = 0;
   
    VID_CNT = VID_MODE3 | VID_BG2;
   
    for(i = 0; i < 240; i++) {
        for(j = 0; j < 160; j++) {
            VRAM[i+j*M3_WIDTH] = RGB15(i, j, c);
        }
       
        c++;
    }
   
    while(1);
   
    return 0;
}

This code produces something like this...


I did this because it looks cool :D. Try rearranging the simple source code and see what type of crap you can load on the VRAM.  What the code is doing btw is it looping through the width and the height of the gba's screen then it changes the color of the pixel using the i, j and c variables.  i sets red, j sets green and c sets blue during the loops life time.  So it produces a really cool effect.  Oh well I'm getting tired need sleep, night.

Links for the demos
Demo 1
http://www.filehosting.org/file/details/218682/demo2.zip
Demo2
http://www.filehosting.org/file/details/218683/demo3.zip

Sunday, April 10, 2011

Hello and welcome to the nightmare. (Sortof)

Hello and welcome to at random blog about game deving (developing btw).  Its blog where I'll post random Ideas and concepts that isn't really base on any particular game project just ideas on how the develop these monster-as apps know as games.  I'm not going to candy coat any thing so if you are a beginner you might want to look else where for now.  I'm thinking of adding a simple set of tutorials on SDL and opengl later on but just random thought on projects I'm working on for my self which right now I'm working on game boy advance programming which is based off tonc tutorials which is a good place to start doing gba game development.  If you are a beginner than you might want to start with some a little more realistic like sdl or allegro. Or Microsoft XNA which uses C# while sdl and allegro use c or c++ ( they also have wrappers for these libraries too like python, ruby, lua, java or what not ).  Game Boy Advance programming or any sort of console development is quite low level.  Its just as low level as Operating System development (not as hard though ).  So do some thing like this...

int main() {
    *(unsigned int*)0x04000000 = 0x403;
    ((unsigned short*)0x06000000)[120+80*240] = 0x001F;
    while(1);
    return 0;
}

Is perfectly legal.  If you don't understand this source code its fine. 0x04000000 is the IO MEM location.  Its used to set info to the gba. 0x403 means display mode 3 which is bitmap mode 1 using background 2. 0x06000000 is the location of the video ram (VRAM) which alows you to display stuff the gba's display. This example display a red dot at [120x80] pixels.  The code is quite unreadable if I say my self, usually you would write a library to make this more manageable (I'm working on my own right now).  I"m just demonstrating how low-level console development is.  There are libraries out there to hide this like devkitpro's libgba or tonc's (This example is a simplified version of toncs hello world program).
Kind of boring but hell this is my first blog post after all.
Also note to compile this you need a compiler that compiles to arm and thumb code.  I'm using devkit pro's arm compile stack which is the default stack now a days here the link.

http://sourceforge.net/projects/devkitpro/files/devkitARM/

Here's the demo link if it disappears let me know I'll re-up it.

http://www.filehosting.org/file/details/218450/demo.zip

Oh well this was kind of long for a hello world post. Well later.