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 > Need help with simple sprite program

#33585 - ymalik - Wed Jan 05, 2005 8:29 pm

The following program displays a messed up sprite (that's the best way I can describe it). The messed up sprite scrolls fine, though.
Code:
#include "stuff.h"
#include "mario.raw.c"
#include "mario.pal.c"

void wait_vblank();
void process_buttons(s16 *, s16 *);

int main(void)
{
   s16 x = 10, y = 10;
   int i;
   u16 char_number = 0;
   Sprite sprite[128];

   SetMode(2 | OBJ_ENABLE | OBJ_MAP_1D);

   // initialize sprites
//   for(i = 0; i < 128; i++)
//   {
//      sprite[0].attribute0 = 160;
//     sprite[0].attribute1 = 240;
//   }

   // copy sprite palette
   for(i = 0; i < 256; i++)
      SpritePal[i] = mario_Palette[i];
     
   // copy sprite data
   for(i = 0; i < 4096; i++)
      SpriteData[i] = mario_Bitmap[i];

   sprite[0].attribute0 = COLOR_16 | y;
   sprite[0].attribute1 = SIZE_64 | x;
   sprite[0].attribute2 = char_number;

   while(1)
   {
      process_buttons(&x, &y);

      sprite[0].attribute0 &= 0;
      sprite[0].attribute0 = COLOR_16 | y;
      sprite[0].attribute1 &= 0;
      sprite[0].attribute1 = SIZE_64 | x;

      wait_vblank();
     
      SpriteMem[0] = sprite[0].attribute0;
      SpriteMem[1] = sprite[0].attribute1;
      SpriteMem[2] = sprite[0].attribute2;
      SpriteMem[3] = sprite[0].attribute3;
   }
   
   return 0;
}

void process_buttons(s16 *x, s16 *y)
{
   if(!(*BUTTONS & BUTTON_LEFT))
      (*x)--;
   if(!(*BUTTONS & BUTTON_RIGHT))
      (*x)++;
   if(!(*BUTTONS & BUTTON_UP))
      (*y)--;
   if(!(*BUTTONS & BUTTON_DOWN))
      (*y)++;
}

void wait_vblank(void)
{
   while(!(REG_DISPSTAT & 1));
   while(REG_DISPSTAT & 1);
}


The sprite is a 4 bit 64x64. I created the sprites by using gba2gfx as follows:
gfx2gba -t8 -pmario.pal -fsrc mario.bmp

I can't use pcx2sprite because I don't know how to make PCX files.

Thanks,
Yasir

#33631 - dj-ceejay - Thu Jan 06, 2005 5:58 am

gfx2gba is used for creating backgrounds I believe.
pcx2sprite is used to create a sprite & palette header file.

You could use a bmp-pcx converter. I did a quick google search but I couldn't find a free one.
Here are some that look good:
http://www.convertzone.com/net/cz-abc%20amber%20image%20converter-bmp-pcx.htm
http://www.graphic-converter.net/
http://www.batchconverter.com/image-converter.html

Which graphics program are you using?
_________________
Fruit Machine Games:
http://www.fmsoftware.info/

#33649 - Fatnickc - Thu Jan 06, 2005 11:11 am

I thought the gfx2... came in gfx2gba and gfx2sprite. You'll use sprite. I used to use gfx2gba, but it never worked for me, so now I use PCX2SPRITE and PCX2GBA. Once you've told us which graphics program you are using, I could probably help you to get a PCX image. '
On the subject of gfx2gba, someone we all know is going to come round shouting 'frontend, frontend'.

#33660 - ymalik - Thu Jan 06, 2005 4:09 pm

I'm using MS Paint to create my images and saving them as BMP files. gfx2sprite can create tiles with the -t8 options and a palette as well. Isn't that all you need to create a sprite? An image broken up into tiles is an image broken up into tiles whether you are dealing with sprites or backgrounds, right?

#33672 - Fatnickc - Thu Jan 06, 2005 7:05 pm

OK. Well, with MS Paint I suppose you can only use that gfx2gba, as it doesn't like PCX and indexed colours. Yes, there isn't gfx2sprites. I can't really help you with this, but, if ever you get Photoshop Elements, you knwo where I am ;).

#33674 - DekuTree64 - Thu Jan 06, 2005 7:39 pm

You can use the same tile data on backgrounds and sprites, but most programs that convert images for backgrounds also remove any duplicate tiles, or ones that could be H/V flipped or palette swapped (if 4-bit). Since sprites can only use a straight strip of tiles with no per-tile flipping/palette, the data usually can't be used in both.
_________________
___________
The best optimization is to do nothing at all.
Therefore a fully optimized program doesn't exist.
-Deku

#33675 - yaustar - Thu Jan 06, 2005 7:54 pm

I found gif2sprite the easiest tool to use for sprite converting.
_________________
[Blog] [Portfolio]

#33703 - ymalik - Fri Jan 07, 2005 2:32 am

Thanks, yaustar, gifs2sprites works fine. But I have some questions about the output and my program:
1. If each value in the tiles array is supposed to be an index into the palette array, why do I see values like 0x4444 in the tiles array generated by gifs2sprite? The palette array only has 8 values.
2. Why does the tile array generated by gifs2sprite have 1024 and 2048 values in 16 color and 256 color images, respectively? Shouldn't it be 4096, which is the number of pixel in a 64x64 image?
3. When I scroll left to right, the image scrolls fine, i.e. the image wraps around. But when I go up close to the top of the screen, why does the image disappear?
4. In part of my code, I commented out the part that begins with // initialize sprites. Do I have to initialize all the 126 sprites or just the sprites that I'm going to use use (for example, just one)?

Thanks,
Yasir

#33709 - ymalik - Fri Jan 07, 2005 4:51 am

Ok, I figured out 1 and 2. But what about 3 and 4? Also, gifs2sprites does not support images with frames of animation (two 64x64 images next to each other).

#33711 - ymalik - Fri Jan 07, 2005 5:51 am

Ok, I answered question 3 and 4.

#33714 - tepples - Fri Jan 07, 2005 8:16 am

To convert multiple sprite cels in a 1D mapping mode, just stack the cels vertically.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#33732 - Quirky - Fri Jan 07, 2005 8:17 pm

I use Irfanview to convert between image formats. For my pixel pushing needs I use a combination of MS Paint (.bmp) and Deluxe Paint II (.pcx)

I use MS Paint to copy chunks between images, saving as 24-bit bitmaps (otherwise it randomly messes up the colours) then open the image in Irfan, downsample to 256 colour, which causes no quality loss as it already has less than 256 colours, then Save As a PCX file. I can then fix any palette indexing problems in Deluxe Paint, and it has a nicer interface for doodling.

The cel problem... gfx2gba (v0.13 by Markus, not the other gfx2gba) can generate "meta-tiles" that don't depend on the image's physical cel layout. I use a flag "-T16" to generate 16x16 cels, -T64 would generate 64x64 cels. This is also handy for avoiding font images that are really tall and skinny, something that other conversion programs force you to do.