Login
Username:

Password:

Remember me



Lost Password?

Register now!

Sections

Who's Online
116 user(s) are online (109 user(s) are browsing Forums)

Members: 0
Guests: 116

more...

Support us!

Headlines

 
  Register To Post  

GCC, Switch( condition ) & Case
Quite a regular
Quite a regular


See User information
Hi,

I encounter an error on using SWITCH/CASE C/C++ commands under GCC ... I don't understand why because I think I use correctly the functions :

Here is the error :
setup.c:104:error: expected expression before 'default'

Here is the code :
/* Cr?ation de la fen?tre d'affichage principale */
void SetDisplayModeint Widthint Heightint Depth ){                    /* TO DO : MUST ADDED WITH TO InitDisplayMode */
  
BOOL Supported;
  
int DMode;
  
Supported CheckDisplayModeWidthHeightDepth );
  if ( 
Supported TRUE ){
    
/* Open the screen using windowed mode */
    
switch( Depth ){
      case 
32:
        
DMode GLUT_DOUBLE GLUT_RGB GLUT_DEPTH
      
case 24:
        
DMode GLUT_DOUBLE GLUT_RGB GLUT_DEPTH
      
case 16:
        
DMode GLUT_DOUBLE GLUT_RGB GLUT_DEPTH
      
case 8:
        
DMode GLUT_DOUBLE GL_COLOR_INDEX
      
case default:
        
DMode GLUT_DOUBLE GLUT_RGB GLUT_DEPTH
     
}
    
glutInitDisplayModeDMode );              /* Define display mode properties */
    
glutInitWindowSizeWidthHeight );
    
glutInitWindowPosition3232 );
    
WindowID glutCreateWindow"AmiDARK Basic application" ); /* Create the asked window */
    
if (FullSCREEN == TRUE ){
      
glutGameModeString"640x480:32" );
      
glutEnterGameMode();
     }
   }
 }


Strange. not ?
Or I am wrong again ?

EDIT :
Forget, I've found the error ... default does not need "case" before ... :p


Edited by freddix on 2009/2/22 23:53:06
All we have to decide is what to do with the time that is given to us.
Go to top
Re: GCC, Switch( condition ) & Case
Not too shy to talk
Not too shy to talk


See User information
@freddix

You need to put "break;" at the end of each case part, otherwise the code continues executing through the rest of the cases.

Go to top
Re: GCC, Switch( condition ) & Case
Quite a regular
Quite a regular


See User information
@xeron
I suspected that.
Thank you.

All we have to decide is what to do with the time that is given to us.
Go to top
Re: GCC, Switch( condition ) & Case
Just can't stay away
Just can't stay away


See User information
@freddix
I don't know about C++ but in C you will always end up with your DMode variable equal to the default case if you don't but "break" statements after each case like this:
switch( Depth ){
case 32:
DMode = GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH;
break;
case 24:
DMode = GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH;
break;
case 16:
DMode = GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH;
break;
case 8:
DMode = GLUT_DOUBLE | GL_COLOR_INDEX;
break;
case default:
DMode = GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH
}

Go to top
Re: GCC, Switch( condition ) & Case
Just popping in
Just popping in


See User information
@freddix

... and you should initialize DMode with some default value because you are using it.

... and you need a second "=" in:
if ( Supported TRUE ){

Your case could also be simplified to an if:
if ( Depth == ){
   
DMode GLUT_DOUBLE GL_COLOR_INDEX;
} else {
   
DMode GLUT_DOUBLE GLUT_RGB GLUT_DEPTH;
}

Go to top
Re: GCC, Switch( condition ) & Case
Quite a regular
Quite a regular


See User information
@Gazelle
Effectively, second = added :p

I must not simplify cos, if some more modes are added to glut to handle RGB32, RGB24, RGB16 and color index 8 bits, I want to have just to add the correct flag to make my display mode use them.

All we have to decide is what to do with the time that is given to us.
Go to top
Re: GCC, Switch( condition ) & Case
Just popping in
Just popping in


See User information
@freddix

Make it like this:
switch( Depth ){
      case 
8:
        
DMode GLUT_DOUBLE GL_COLOR_INDEX;
        break;
      case 
16/* for now fall through */
      
case 24:
      case 
32:
      default:
        
DMode GLUT_DOUBLE GLUT_RGB GLUT_DEPTH;
     }

Go to top
Re: GCC, Switch( condition ) & Case
Quite a regular
Quite a regular


See User information
@Gazelle
In fact, I've made it slightly different now:
switch( Depth ){
      case 
8:
        
DMode GLUT_DOUBLE GL_COLOR_INDEX;
        break;
      case 
16/* for now fall through */
      
case 24:
        
DMode GLUT_DOUBLE GLUT_RGB GLUT_DEPTH;
        break;
      case 
32:
        
DMode GLUT_DOUBLE GLUT_RGBA GLUT_DEPTH;
        break;
      default:
        
DMode GLUT_DOUBLE GLUT_RGB GLUT_DEPTH;
     }

All we have to decide is what to do with the time that is given to us.
Go to top
Re: GCC, Switch( condition ) & Case
Amigans Defender
Amigans Defender


See User information
@freddix
Quote:

BOOL Supported;
Supported = CheckDisplayMode( Width, Height, Depth );
if ( Supported = TRUE ){


The if() statement is wrong again and I'd do something more obvious like this:
BOOL IsSupported = CheckDisplayMode( Width, Height, Depth );
if ( IsSupported ) {
...

Again, a matter of style.

ExecSG Team Lead
Go to top
Re: GCC, Switch( condition ) & Case
Quite a regular
Quite a regular


See User information
@ssolie
It's not wrong ... but it's not perfect ;)

All we have to decide is what to do with the time that is given to us.
Go to top
Re: GCC, Switch( condition ) & Case
Home away from home
Home away from home


See User information
@freddix

if ( Supported = TRUE ){

Yes this line is wrong, should be:

if ( Supported == TRUE ){

'==' compare some to a value.
'=' set some thing to a value.

If you type "if ( Supported = TRUE ){" then result of this is always TRUE, because "TRUE" is what "Supported" is set as, in side your if().

(NutsAboutAmiga)

Basilisk II for AmigaOS4
AmigaInputAnywhere
Excalibur
and other tools and apps.
Go to top
Re: GCC, Switch( condition ) & Case
Quite a regular
Quite a regular


See User information
@LiveForIt
yes, it's what was explained before, I've understood, in the last statements, it was an error from me to not modify this in the post.

All we have to decide is what to do with the time that is given to us.
Go to top
Re: GCC, Switch( condition ) & Case
Quite a regular
Quite a regular


See User information
@freddix

In fact, you should never say "if (something == TRUE)" because that means it will only work for that one value (whatever "TRUE" is defined to be). "True" means any non-zero value.
The proper way to test for "truth" is: "if (something)..."

cheers
tony
Go to top
Re: GCC, Switch( condition ) & Case
Quite a regular
Quite a regular


See User information
@tonyw: I only compare boolean to TRUE or FALSE. ;) I never compare to TRUE/FALSE for integer and others data ...

All we have to decide is what to do with the time that is given to us.
Go to top
Re: GCC, Switch( condition ) & Case
Quite a regular
Quite a regular


See User information
@freddix

What Tonyw was trying to say is that under C there is no such thing as a boolean type, in fact any numeric value can be interpreted as a boolean : 0 is false anything else is true.
So the BOOLEAN you are using is in fact an alias to a long and TRUE and FALSE are in fact two #defines.
if( TRUE == myVal )
is equivalent to
if( myVal )
and
if( FALSE == myVal )
is equivalent to
if( !myVal )

However the first form has disadvantage in case of error in the equality test (that you did two times) of producing bugs (unless you are using the tip I told you to reverse the operands's order and I applied here) whereas the second has not (but for a beginner this form may seems a bit difficult to read)...

Back to a quiet home... At last
Go to top
Re: GCC, Switch( condition ) & Case
Just can't stay away
Just can't stay away


See User information
@abalaban

Quote:
if( TRUE == myVal )
is equivalent to
if( myVal )
No, it's not. It's only the same if myVal is 1, not if myVal is for example 2.
Especially on AmigaOS that can be a problem since dos.library functions usually return DOSFALSE (0) or DOSTRUE (-1), not FALSE (0) or TRUE (1), and using
if ( TRUE == IDOS->foobar() )
would be wrong but
if ( IDOS->foobar() )
works.

Go to top
Re: GCC, Switch( condition ) & Case
Home away from home
Home away from home


See User information
@abalaban

Quote:

if( TRUE == myVal )

is equivalent to

if( myVal )


No its not,

if( myVal)

is the same as typing:

if (myVal != 0)

any value not zero is true.

if( TRUE == myVal )

This line is only true if myVal is true, and as Joerg pointed out, TRUE is some times defined as -1 and some times as 1,
so its safer to write:

if (myVal)

Read about return values of OS functions in the OS4.X SDK and Linux man pages (clib/newlib) before you decide what to do.

(NutsAboutAmiga)

Basilisk II for AmigaOS4
AmigaInputAnywhere
Excalibur
and other tools and apps.
Go to top
Re: GCC, Switch( condition ) & Case
Amigans Defender
Amigans Defender


See User information
@abalaban
Quote:
What Tonyw was trying to say is that under C there is no such thing as a boolean type...

Since C99 there is a proper bool type in C (_Bool) but it has nothing to do with the fake boolean types that BOOL, BOOLEAN, etc. are. The stdbool.h header can be used to make the C _Bool type like the C++ bool type.

ExecSG Team Lead
Go to top
Re: GCC, Switch( condition ) & Case
Quite a regular
Quite a regular


See User information
@ssolie

oops yes you'are right, I guess I have forgotten this.

Back to a quiet home... At last
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