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 > I'm just plain stuck.

#168367 - penjuin - Sat Apr 25, 2009 1:21 pm

Hey all,

This is my first foray into GBA programming, but it is not my first time at C (or embedded-style C either). I am trying to write code to display a sprite; I have read a multitude of tutorials and I just cannot get this to work. According to VBA, my palette is loaded fine, my tiles are loaded as well and the OAM viewer says that my object is visible at 5,5. The sprite in the OAM viewer is of the correct dimensions (8x32) but it shows up as a black square (not my sprite). Any help would be very much appreciated.

Code:


#include <gba.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pcx.h>

// include sprites
#include "black_pcx.h"
#include "blue_pcx.h"
#include "brown_pcx.h"
#include "green_pcx.h"
#include "purple_pcx.h"
#include "red_pcx.h"
#include "resistor_pcx.h"

OBJATTR sprites[128];      // Object Attribute memory, for manipulating the sprites

u16 PaletteBuffer[256];

//---------------------------------------------------------------------------------
// Program entry point
//---------------------------------------------------------------------------------
int main(void) {
   //---------------------------------------------------------------------------------
   
   
   // the vblank interrupt must be enabled for VBlankIntrWait() to work
   // since the default dispatcher handles the bios flags no vblank handler
   // is required
   irqInit();
   irqEnable(IRQ_VBLANK);
   REG_IME = 1; // Interrupt enable
      
   // set up sprites
   DecodePCX(black_pcx, (u16*)VRAM , PaletteBuffer);
   
   u16 *palette_pointer;
   palette_pointer = BG_COLORS;
   u32 i=0;
   for (i=0; i<255; i++)
      *palette_pointer++ = PaletteBuffer[i];
   
    sprites[0].attr0 = ATTR0_COLOR_256 | (1<<15) | 5 ; // 8 width
    sprites[0].attr1 = (1<<14) | 5; // 32 long
    sprites[0].attr2 = 0;
   
   //Copy OAM
   u16 *temp;
   temp = (u16*)sprites;
   u16 *temp2;
   temp2 = (u16 *)OAM;
   for (i=0;i<4;i++)
      *temp2++ = temp[i];
    // screen mode & background to display
   SetMode( MODE_4 | BG0_ON | OBJ_ENABLE | 0x40);

   
   while (1) {
      VBlankIntrWait();
   }
}

#168368 - DekuTree64 - Sat Apr 25, 2009 1:26 pm

Try using mode 0. Mode 4 uses half of sprite VRAM for the BG, since the usual 64KB of BG VRAM isn't enough for 2 full screens of bitmap data. So in mode 4, you can only use sprite tiles 512 and above.
_________________
___________
The best optimization is to do nothing at all.
Therefore a fully optimized program doesn't exist.
-Deku

#168369 - penjuin - Sat Apr 25, 2009 1:40 pm

Thanks DekuTree64, that definitely changed something!

Now I get a weird pattern of basically my first tile spread all over the screen. I took a look at the VRAM and OAM and there is nothing unusual going on there either, but I guess I wouldn't really know what usual looks like because I have yet to get it working :P . The OAM viewer still can't detect my sprite. Do I just set attribute 2 to the first tile and it works out where the rest are?

#168370 - eKid - Sat Apr 25, 2009 4:58 pm

The array of tiles you are seeing is from the BG0 you enabled in the screen mode. You copied the sprite graphics into BG video ram, you want to decompress to the sprite vram which starts 64KB after the bg vram (i think the pointer is called SPRITE_GFX (or maybe thats only for libnds))
You can remove BG0_ON since you're not using that layer.

#168377 - penjuin - Sun Apr 26, 2009 4:52 am

Awesome! I had the sprites and palettes going into the BG locations instead of the sprite ones. thanks.

#168379 - sgeos - Sun Apr 26, 2009 8:30 am

Visual Boy Advance allows you to view tiles, sprites and palettes. This is very useful.

#168383 - Dwedit - Sun Apr 26, 2009 2:51 pm

It's not just very useful, it's extremely useful. NO$GBA commercial version also has a similar feature, but it's not as good, since it forces you to use the palettes on the screen, and automatically assigns palettes based on what is displayed. So if the graphics aren't displayed correctly, you might see black areas in vram, which are just areas that you're not displaying right now.
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."