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.

Coding > Volatile unsigned char to ints typecasting

#177231 - blessingta@hotmail.co.uk - Tue Jan 31, 2012 7:54 pm

Is it possible to typecast "volatile unsigned char" to "int"?
Would I have to change my "vcuSnake_X" and "vcuSnake_Y" to ints?


Code:
void Render_Game()
{
Print_Fonts("UP",0,10);
redcircle_sprites((int)vcuSnake_X,(int)vcuSnake_Y,1);
}

#177232 - blessingta@hotmail.co.uk - Tue Jan 31, 2012 8:01 pm

somemore info:
Below is my Logic which influences the volatile data
Code:
#include "_gamestate_Logic.h"

enum E_SNAKE_DIRECTION{UP=1,DOWN=2,LEFT=3,RIGHT=4};

volatile unsigned char vucSNAKE_DIR =RIGHT;//stores requested direction
volatile unsigned char vucSNAKE_MOVE =RIGHT;//Stores current snake direction

volatile unsigned char vcuSnake_X = 125;//230max X
volatile unsigned char vcuSnake_Y = 75;//150max Y
void SNAKE_DIR_func ();
void MOVE_SNAKE();

void Run_Game ()
{
//Control snake direction
SNAKE_DIR_func();

//Move snake
MOVE_SNAKE();

}




//SNAKE direction
void SNAKE_DIR_func ()
{
      switch (vucKEYS_STATES){
      case KEY_UPpressed:      Print_Fonts("UP",0,10); vucSNAKE_DIR = UP;    break;
      case KEY_LEFTpressed:  Print_Fonts("LEFT",0,10);vucSNAKE_DIR = LEFT;  break;
      case KEY_RIGHTpressed: Print_Fonts("RIGHT",0,10);vucSNAKE_DIR = RIGHT; break;
      case KEY_DOWNpressed:  Print_Fonts("DOWN",0,10);vucSNAKE_DIR = DOWN;  break;
      default: break;
      }
}

void MOVE_SNAKE()
{
switch (vucSNAKE_DIR)//Requested Direction:
{
case UP:
switch(vucSNAKE_MOVE)//Accept change in direct if snake moving left or right
{case LEFT:/**/ vcuSnake_Y += 1;vucSNAKE_MOVE = UP; /**/break;
case RIGHT:/**/ vcuSnake_Y += 1;vucSNAKE_MOVE = UP; /**/break;
} break;

case LEFT: 
switch(vucSNAKE_MOVE)//Accept change in direct if snake moving up or down
{case   UP:/**/ vcuSnake_X -= 1; vucSNAKE_MOVE = LEFT; /**/break;
 case DOWN:/**/ vcuSnake_X -= 1;vucSNAKE_MOVE = LEFT; /**/break;
}break;

 case RIGHT:
 switch(vucSNAKE_MOVE)//Accept change in direct if snake moving up or down
{case   UP:/**/ vcuSnake_X += 1; vucSNAKE_MOVE = RIGHT; /**/break;
 case DOWN:/**/ vcuSnake_X += 1;vucSNAKE_MOVE = RIGHT; /**/break;
}break;

 case DOWN: 
 switch(vucSNAKE_MOVE)//Accept change in direct if snake moving left or right
{case LEFT:/**/ vcuSnake_Y -= 1;vucSNAKE_MOVE = DOWN; /**/break;
case RIGHT:/**/ vcuSnake_Y -= 1;vucSNAKE_MOVE = DOWN; /**/break;
} break;
}

}

#177233 - elhobbs - Tue Jan 31, 2012 9:08 pm

I am guessing that you are asking because you are experiencing a problem - but, you never say what the problems is.

yes, it is possible to cast these values as you have shown. you need to consider sign extension though. chances are you are better off using int all around.

are you reading/modifying these values in an interrupt? if not then you can drop volatile.

#177236 - sverx - Wed Feb 01, 2012 11:40 am

probably you don't need 'volatile' at all... and unsigned char should implicitly cast to int if required...
_________________
libXM7|NDS programming tutorial (Italiano)|Waimanu DS / GBA|A DS Homebrewer's Diary

#177237 - blessingta@hotmail.co.uk - Wed Feb 01, 2012 4:07 pm

Thanks, I just followed your advice and it turns out the problem seems to be my sprite function. If you could check out the "bta_game_sprites" and "btaLoadSprites" that would be appreciated, I reckon that's the source of my problems.

Below is my project:
http://dl.dropbox.com/u/42518274/SNAKE3.rar