Weird things with very basic code

SGDK only sub forum

Moderator: Stef

Post Reply
masteries
Very interested
Posts: 53
Joined: Thu Jul 30, 2020 3:33 pm

Weird things with very basic code

Post by masteries » Thu Oct 21, 2021 2:55 pm

While testing some very basics, such modify struct based variables from one function, to be used at a later one...

A strange behaviour have been found; despite the code shown here is not optimized, pointers are not taken...
but generally speaken is very rare; such example this do not happen using the same sample code when compiling for other vintage platforms:

Here the code examples; there are an array of struct variables declared, some of the variables are modified by an sprite tick function, to be used at another one; but the another one can´t see the modifications:

Code: Select all


At top:

void tick_player(unsigned short id, unsigned short idother);
void tick_enemy(unsigned short id, unsigned short idother);
void tick_world(unsigned short id, unsigned short idother);


Later in the main:


//Some sprites, in order to test very basic sort of things

    VDP_setPalette(PAL3, sonic_sprite.palette->data);

	entidad[1].spr = SPR_addSprite(&sonic_sprite, 160, 160, TILE_ATTR(PAL3, TRUE, FALSE, FALSE));
	SPR_setVRAMTileIndex(entidad[1].spr,1632);//Colocar el sprite donde deseas - Sobrescribir los tiles de las letras
	entidad[1].id = 1;
	entidad[1].tickfcn = tick_player;
	
	
	entidad[2].spr = SPR_addSprite(&sonic_sprite, 400, 100, TILE_ATTR(PAL3, TRUE, FALSE, FALSE));
	SPR_setVRAMTileIndex(entidad[2].spr,1632+19);//Colocar el sprite donde deseas - Sobrescribir los tiles de las letras
	entidad[2].id = 2;
	entidad[2].tickfcn = tick_enemy;
	entidad[2].xw = 400;
	entidad[2].yw = 100;
	
	entidad[3].spr = SPR_addSprite(&sonic_sprite, 280, 140, TILE_ATTR(PAL3, TRUE, FALSE, FALSE));
	
	SPR_setVRAMTileIndex(entidad[3].spr,1632+38);//Esto funciona bien
	entidad[3].id = 3;
	entidad[3].create_body = 0;
	entidad[3].tickfcn = tick_world;
	entidad[3].xw = 280;
	entidad[3].yw = 140;
	
	entidad[2].idother = 3;
	

	
	


The tick functions:

Code: Select all


void tick_player(unsigned short id, unsigned short idother)
{

    if (Xorder > 0)         //PULSO DERECHA
    {
		if( (offset + 320) < map_width)
		{
			offset++; cuentaPixels++; cuentaPixelsB++; offsetAcumulado++; 
			offsetB = (Current_Bcol*8) + (offsetAcumulado / 2);
		}
        SPR_setAnim(entidad[id].spr, ANIM_RUN);
        SPR_setHFlip(entidad[id].spr, FALSE);
		
    }
    else if (Xorder < 0)    //PULSO IZQUIERDA
    {
        SPR_setAnim(entidad[id].spr, ANIM_RUN);
        SPR_setHFlip(entidad[id].spr, TRUE);
    }
    else SPR_setAnim(entidad[id].spr, ANIM_STAND);
    
}



void tick_enemy(unsigned short id, unsigned short idother)
{

    SPR_setAnim(entidad[id].spr, ANIM_RUN);
	
	if(entidad[id].yw > 99)
	{
		SPR_setHFlip(entidad[id].spr, TRUE);
		entidad[id].xw--;
		entidad[idother].xw--;
		if(entidad[id].xw < 128)
			entidad[id].yw = 99;
	}
	else//Cambio de sentido
	{
		SPR_setHFlip(entidad[id].spr, FALSE);
		entidad[id].xw++;
		entidad[idother].xw++;
		if(entidad[id].xw > 512)
			entidad[id].yw = 100;
	}

	//Reubicar el enemigo en coordenadas pantalla
	entidad[id].spr->x = entidad[id].xw - offset;

}



//Very basic tick function, in order to positionate a sprite in screen coordinates
void tick_world(unsigned short id, unsigned short idother) 
{
	entidad[id].spr->x = entidad[id].xw - offset;	
}




The order of execution for these tick functions, tick_world cannot view the modifications performed by the previous call of tick_enemy(2,3).
Sprite is displayed static, what is a non sense, due to its "xw" variable is being modified by tick_enemy(2,3):

Code: Select all

			
			tick_player(1,0);
			tick_enemy(2,3);
			//tick_enemy(3,0);
			tick_world(3,0);


If the order of execution is this one, tick_enemy(3,0) can view the modifications, its obvious due to the Sonic sprite moves with double speed, due to its "xw" variable is modified byt tick_enemy(2,3) and tick_enemy(3,0). But why tick_world can´t see this change at all?:

Code: Select all

			
			tick_player(1,0);
			tick_enemy(2,3);
			tick_enemy(3,0);
			//tick_world(3,0);

I changed the compiler optimization to O1, but there are no effects.
Also, declare the struct array as volatile doesn´t help

SGDK version is 1.65


Thanks in advance,

masteries
Very interested
Posts: 53
Joined: Thu Jul 30, 2020 3:33 pm

Re: Weird things with very basic code

Post by masteries » Thu Oct 21, 2021 6:54 pm

If I put at top of a tick function:

Code: Select all


SPR_setAnim(entidad[id].spr, ANIM_RUN);

The sprite can be relocated in the screen, or world.


But, I will use frame by frame animation instead use SPR_setAnim()...


Will I face problems moving the sprites around?

masteries
Very interested
Posts: 53
Joined: Thu Jul 30, 2020 3:33 pm

Re: Weird things with very basic code

Post by masteries » Fri Oct 22, 2021 10:01 am

Solved using SPR_setFrame() and SPR_setPosition()

Its much better to use functions than directly modify pointer values.

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

Re: Weird things with very basic code

Post by Stef » Sun Oct 24, 2021 2:38 pm

Hi,

Just seeing your post sorry, indeed the sprite engine is meant to be used through the SPR_xx methods, don't directly modify internal fields (maybe except spr->timer in some situation) or you may screw some internal states of the sprite.
Just use SPR_xx for all sprite manipulations and it should work as expected =)

Post Reply