SGDK load sprite file

SGDK only sub forum

Moderator: Stef

Post Reply
orlanrod
Very interested
Posts: 99
Joined: Fri Sep 25, 2015 7:46 pm

SGDK load sprite file

Post by orlanrod » Sat Sep 26, 2015 9:36 pm

So i got it installed and running, and managed to draw the simple text and variables. Now i am looking at the sprite tutorial.

I am using Genesis Sprite Studio (if there is something better let me know,) and got the file outputed. How would i load this?

Thanks.

matteus
Very interested
Posts: 336
Joined: Mon Feb 04, 2008 1:41 pm

Re: SGDK load sprite file

Post by matteus » Sun Sep 27, 2015 7:25 am

Are you using a res file to declare the sprite?

Grind
Very interested
Posts: 69
Joined: Fri Jun 13, 2014 1:26 pm
Location: US
Contact:

Re: SGDK load sprite file

Post by Grind » Sun Sep 27, 2015 11:34 pm

I have not used Genesis Sprite Studio so I'm not sure how much of this post applies to you. Hopefully it helps.

Firstly, check out this exmaple: https://github.com/Stephane-D/SGDK/tree ... ple/sprite

This is what is involved in making it work.

1. Create a PNG file with indexed color (16 color palette) like this one, and put it in the "res" folder:
Image
Each row is an "animation", and each column is a "frame" within those animations. rescomp can detect duplicate frames, so you don't have to worry about loading the same one in memory twice. It also detects blank frames and does not show those either.

2. In that same folder should be a resources.res file, if not create one (filename doesn't matter as long as it ends in .res):

Code: Select all

SPRITE sonic_sprite "sonic.png" 6 6 -1 5
Based on the documentation for the rescomp tool (https://raw.githubusercontent.com/Steph ... escomp.txt) this line will do the following:
  • SPRITE - create a sprite
  • sonic_sprite - This is the name of the sprite in your code
  • "sonic.png" - The filename/path to the png image to convert to an SGDK sprite
  • 6 - Number of columns (there are up to 6 frames per animation)
  • 6 - Number of rows (there are 6 animations, one for standing, walking, etc)
  • -1 - Use whichever compression it wants
  • 5 - Speed for frame advance, the sprite will animate once every 5 frames
3. When the game is build this will generate a header file for you to include that contains a pointer to sonic_sprite. In the code you want to:

Code: Select all

#include <genesis.h>
// Include the generated resource file containing the sprite (whatever_you_named_the_res_file.h)
#include "resources.h"
// Declare an array for your sprites (must be actual sprites and not pointers)
Sprite sprites[2];

int main() {
	VDP_init();
	// Initialize the sprite engine
	SPR_init(256);
	// If you want get the palette from the sprite
	VDP_setPalette(PAL2, sonic_sprite.palette->data);
	// Or from a palette you defined yourself if you want to share the same palette among many sprites
	//VDP_setPalette(PAL2, MY_PAL.data);
	// Initialize the sprite itself
	SPR_initSprite(&sprites[0], &sonic_sprite, 4, 4, TILE_ATTR(PAL2, TRUE, FALSE, FALSE));
	while(1) {
		// Update the sprites in the array, since there is only 1 sprite update 1
		SPR_update(sprites, 1);
		VDP_waitVBlank();
	}
}
I didn't actually test the code I wrote there but it is based on the sprite example with the extra stuff removed. If you compile the sprite example I think it lets you move around and show different animations.

Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Re: SGDK load sprite file

Post by Stef » Mon Sep 28, 2015 8:30 am

Grind wrote:I have not used Genesis Sprite Studio so I'm not sure how much of this post applies to you. Hopefully it helps.

Firstly, check out this exmaple: https://github.com/Stephane-D/SGDK/tree ... ple/sprite

...

Based on the documentation for the rescomp tool (https://raw.githubusercontent.com/Steph ... escomp.txt) this line will do the following:
  • SPRITE - create a sprite
  • sonic_sprite - This is the name of the sprite in your code
  • "sonic.png" - The filename/path to the png image to convert to an SGDK sprite
  • 6 - Number of columns (there are up to 6 frames per animation)
  • 6 - Number of rows (there are 6 animations, one for standing, walking, etc)
  • -1 - Use whichever compression it wants
  • 5 - Speed for frame advance, the sprite will animate once every 5 frames
Almost perfect ;) except for "6 6" parameters which simply represents the size in tile of the sprite so here for instance the sonic sprite is 6x6 tiles sized (48x48 pixels). The number of frame and number of animation is then automatically computed from the total image size and the basic sprite size =)

orlanrod
Very interested
Posts: 99
Joined: Fri Sep 25, 2015 7:46 pm

Re: SGDK load sprite file

Post by orlanrod » Mon Sep 28, 2015 10:10 pm

Thanks guys, i got it working. I just modified the example in the sgdk for my sprites.

Post Reply