Multiple sprites?

SGDK only sub forum

Moderator: Stef

Post Reply
MrKsoft
Newbie
Posts: 4
Joined: Fri Feb 28, 2014 2:50 pm

Multiple sprites?

Post by MrKsoft »

So I'm trying to get multiple sprites going for a pong clone but I'm not sure I've figured it out. I'm cobbling this together from multiple code sources. Based on what I've read this SHOULD work, but upon compiling I get an invalid initializer error for the sprites after the SpriteList. Can someone give me a hand?

All it should do is draw a "fish" background and then draw two separate "sprPaddle" sprites next to each other, for testing (identical for now, will use swapped palettes later)

Code: Select all

/* Required files from SGDK */
#include <genesis.h>
/* Load resources files (generated from .res files in /res/) */
#include <tiles.h>
#include <sprites.h>
 
Sprite SpritesList[MAX_SPRITE];

Sprite objPaddle = &SpritesList[0];
Sprite objPaddle2 = &SpritesList[1];

redPaddle_X = FIX16(8);
redPaddle_Y = FIX16(16);
bluePaddle_X = FIX16(24);
bluePaddle_Y = FIX16(16);

int main()
{
		void GameField_init();

		/* This is the initialization block.  Everything before the while loop will only happen once when the game boots up. */
		SPR_init(256);
		/* Draw fish image to PLANE B */
		u16 tileIndex = TILE_USERINDEX;
		VDP_drawImageEx(VDP_PLAN_B, &fish, TILE_ATTR_FULL(PAL1, FALSE, FALSE, FALSE, tileIndex), 0, 0, TRUE, TRUE);
		tileIndex += fish.tileset->numTile;

		/* Print some text (text is plane A so this will be over the fish) */
		VDP_drawText("This is a status line, I think", 0, 0);
		
		/*Initialization code for GAME FIELD */
		GameField_init();
	
        while(1)
        {
    SPR_update(&SpritesList, 2); 
	VDP_updateSprites();
            VDP_waitVSync();
        }
        return 0;
}
void GameField_init()
{
	/* Paddle sprite init! */
    SPR_initSprite(&objPaddle, &sprPaddle, fix16ToInt(redPaddle_X), fix16ToInt(redPaddle_Y), TILE_ATTR(PAL2, TRUE, FALSE, FALSE)); 
	/* Copy the palette! */
	VDP_setPalette(PAL2, sprPaddle.palette->data);
	
	SPR_initSprite(&objPaddle2, &sprPaddle, fix16ToInt(bluePaddle_X), fix16ToInt(bluePaddle_Y), TILE_ATTR(PAL2, TRUE, FALSE, FALSE));
}
Mask of Destiny
Very interested
Posts: 628
Joined: Thu Nov 30, 2006 6:30 am

Post by Mask of Destiny »

You probably want objPaddle and objPaddle2 to be Sprite pointers rather than Sprite structs. So:

Code: Select all

Sprite *objPaddle = &SpritesList[0];
Sprite *objPaddle2 = &SpritesList[1];
and then ditch the address-of operator in the calls to SPR_initSprite
MrKsoft
Newbie
Posts: 4
Joined: Fri Feb 28, 2014 2:50 pm

Post by MrKsoft »

...Yep, that did it. I'm admittedly still a little new to C and pointers and stuff. Thanks!
Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Post by Stef »

When you use the SPR_xxx method you don't need to call VDP_updateSprites() method (which is for low level sprites manipulation).

If you look at the Sonic sprite sample :
http://code.google.com/p/sgdk/source/br ... src/main.c

You can see i only call SPR_udpate(..) when i'm done with my sprites modifications :)
Mills
Interested
Posts: 34
Joined: Fri Apr 11, 2014 11:09 pm

Load two sprites SPR_

Post by Mills »

I could not get two sprites to move at the same time. I tried to copy MrKsoft code... it did not work, so i started with a basic code.

This code loads two sprites.

It works. But the two sprites can't be drawn at the same time.. how can i do it?

Code: Select all

#include <genesis.h>
#include "gfx.h"

Sprite bird;
Sprite bird2;

int main(){

        SPR_initSprite(&bird, &sprite1, fix16ToInt(80), fix16ToInt(100), TILE_ATTR(PAL1,1,0,0));
		SPR_initSprite(&bird2, &sprite2, fix16ToInt(40), fix16ToInt(60), TILE_ATTR(PAL2,1,0,0));

	memcpy(&palette[16], sprite1.palette->data, 16 * 2);
        memcpy(&palette[32], sprite2.palette->data, 16 * 2);

   while(1){
        
        SPR_update(&bird, 1);
        SPR_update(&bird2, 1);

   }

}

If I update bird and bird1... screen goes crazy.. If i update one of them, and not the other, it works.

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

Re: Load two sprites SPR_

Post by Stef »

Actually you were close to it ;)
To display severals sprites you have to sue an array of sprites as the SPR_Update(..) method should be called once per frame !

So here is how your code should be :

Code: Select all

#include <genesis.h>
#include "gfx.h"

Sprite birds[2];

int main(){

        SPR_initSprite(&birds[0], &sprite1, fix16ToInt(80), fix16ToInt(100), TILE_ATTR(PAL1,1,0,0));
		SPR_initSprite(&birds[1], &sprite2, fix16ToInt(40), fix16ToInt(60), TILE_ATTR(PAL2,1,0,0));

	memcpy(&palette[16], sprite1.palette->data, 16 * 2);
        memcpy(&palette[32], sprite2.palette->data, 16 * 2);

   while(1){
        SPR_update(birds, 2);

   }

}

And that is it !
Post Reply