Weird things with very basic code
Posted: 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:
The tick functions:
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):
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?:
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,
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);
Also, declare the struct array as volatile doesn´t help
SGDK version is 1.65
Thanks in advance,