Login
Username:

Password:

Remember me



Lost Password?

Register now!

Sections

Who's Online
156 user(s) are online (141 user(s) are browsing Forums)

Members: 0
Guests: 156

more...

Support us!

Headlines

 
  Register To Post  

[Resolved] Solution to avoid if statement ?
Quite a regular
Quite a regular


See User information
Hi All,

i'd like to know if there is a way to avoid the use of if/elseif in a specific case.

under GLUT you can use the specialfunc that return an int with the key it.
glutspecialfuncint keyint xint y ){
...
}


I'd like to know if I can do this to turn to TRUE/FALSE 1/0 a register :

DBKeyboard.F1 = ( key == GLUT_KEY_F1 );
DBKeyboard.F2 = ( key == GLUT_KEY_F2 );
...


It compiles but does never return TRUE / 1 even if the key is pressed ..
Is there something similar to this to avoid if/elseif ... ?

Kindest Regards,
Freddix / AmiDARK


Edited by freddix on 2009/11/3 13:24:06
All we have to decide is what to do with the time that is given to us.
Go to top
Re: Solution to avoid if statement ?
Home away from home
Home away from home


See User information
@freddix

Most people use a switch statement, but I see no reason why your code wouldn't work. I am wondering what exactly you are trying to achieve with that code. If you're hoping to be able to detect multiple key presses then that code won't work; every time one key is pressed, all of the other fields in that structure will be cleared to false (e.g., push F2, and F1 is cleared).

Hans

Join Kea Campus' Amiga Corner and support Amiga content creation
https://keasigmadelta.com/ - see more of my work
Go to top
Re: Solution to avoid if statement ?
Quite a regular
Quite a regular


See User information
@Hans
in fact I do this for all keys handled so it will automatically set to 1 or 0 the key depending if it is pressed or not
without having to use a switch or if.

Re explaining :
glutspecialfuncint keyint xint y ){
  
DBKeyboard.F1 = ( key == GLUT_KEY_F1 );
  
DBKeyboard.F2 = ( key == GLUT_KEY_F2 );
  
// ...
 
}


After this user can use :

BOOL D = F1Pressed()
will return D = TRUE / 1 if key is pressed or FALSE / 0 if key is not pressed

I was thinking that ( key == GLUT_KEY_F1 ) = 1 if key F1 is pressed and = 0 if key is not pressed. But apparently not.


EDIT.
I've found the correct solution:
it work under glutKeyboardFunc( ... ) and not under glutSpecialFunc( ... ) :
void MyKeyboardFuncunsigned char kint xint y ){
  
UBYTE *kptr NULL;      kptr = &k;
  
UBYTE keyB = *kptr;
  
int key = (int)keyB;
  
DBKeyboard.Escape = ( key == 27 );
  
DBKeyboard.F1 = ( key == GLUT_KEY_F1 );
  
DBKeyboard.F2 = ( key == GLUT_KEY_F2 );
  ...
 }

I will store keys in my structure in the same order than scancode so they can be accessed easyli with commands I'll do like :
int KeyPressed = DEKeyPressed()
int KeyID = DEScanCode( int KeyID )

It now correctly work :)

Regards,
Fred


Edited by freddix on 2009/11/3 13:28:45
All we have to decide is what to do with the time that is given to us.
Go to top

  Register To Post

 




Currently Active Users Viewing This Thread: 1 ( 0 members and 1 Anonymous Users )




Powered by XOOPS 2.0 © 2001-2024 The XOOPS Project