Who's Online
189 user(s) are online (
169 user(s) are browsing
Forums )
Members: 1
Guests: 188
AmigaSociety ,
more...
Topic options
View mode
Newest First
GCC, Switch( condition ) & Case
Posted on:
2009/2/22 23:36
#1
Quite a regular
Joined: 2008/11/3 12:06Last Login
: 2023/8/2 22:18
From South France
Group:
Registered Users
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 SetDisplayMode ( int Width , int Height , int Depth ){ /* TO DO : MUST ADDED WITH TO InitDisplayMode */
BOOL Supported ;
int DMode ;
Supported = CheckDisplayMode ( Width , Height , Depth );
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
}
glutInitDisplayMode ( DMode ); /* Define display mode properties */
glutInitWindowSize ( Width , Height );
glutInitWindowPosition ( 32 , 32 );
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.
Re: GCC, Switch( condition ) & Case
Posted on:
2009/2/22 23:56
#2
Not too shy to talk
Joined: 2006/11/28 14:16Last Login
: 2014/4/18 13:26
From Weston-Super-Mare, Somerset, UK, Europe, Earth, Milky Way, The Universe
Group:
Registered Users
@freddix You need to put "break;" at the end of each case part, otherwise the code continues executing through the rest of the cases.
Re: GCC, Switch( condition ) & Case
Posted on:
2009/2/22 23:59
#3
Quite a regular
Joined: 2008/11/3 12:06Last Login
: 2023/8/2 22:18
From South France
Group:
Registered Users
@xeron I suspected that. Thank you.
All we have to decide is what to do with the time that is given to us.
Re: GCC, Switch( condition ) & Case
Posted on:
2009/2/23 0:03
#4
Just can't stay away
Joined: 2008/1/6 17:56Last Login
: 2023/4/18 20:37
From Pennsylvania, USA
Group:
Registered Users
@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 }
Re: GCC, Switch( condition ) & Case
Posted on:
2009/2/23 0:09
#5
Just popping in
Joined: 2007/5/15 13:42Last Login
: 2023/3/8 14:54
From Austria
Group:
Registered Users
@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 == 8 ){
DMode = GLUT_DOUBLE | GL_COLOR_INDEX ;
} else {
DMode = GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH ;
}
Re: GCC, Switch( condition ) & Case
Posted on:
2009/2/23 0:11
#6
Quite a regular
Joined: 2008/11/3 12:06Last Login
: 2023/8/2 22:18
From South France
Group:
Registered Users
@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.
Re: GCC, Switch( condition ) & Case
Posted on:
2009/2/23 0:22
#7
Just popping in
Joined: 2007/5/15 13:42Last Login
: 2023/3/8 14:54
From Austria
Group:
Registered Users
@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 ;
}
Re: GCC, Switch( condition ) & Case
Posted on:
2009/2/23 0:25
#8
Quite a regular
Joined: 2008/11/3 12:06Last Login
: 2023/8/2 22:18
From South France
Group:
Registered Users
@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.
Re: GCC, Switch( condition ) & Case
Posted on:
2009/2/23 3:11
#9
Amigans Defender
Joined: 2006/11/26 23:16Last Login
: 8/31 22:09
From Canada
Group:
Staff members Moderators
@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
Re: GCC, Switch( condition ) & Case
Posted on:
2009/2/23 3:16
#10
Quite a regular
Joined: 2008/11/3 12:06Last Login
: 2023/8/2 22:18
From South France
Group:
Registered Users
@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.
Re: GCC, Switch( condition ) & Case
Posted on:
2009/2/23 13:42
#11
Home away from home
Joined: 2006/11/20 16:26Last Login
: 11/3 19:40
From Norway
Group:
Registered Users
@freddixif ( 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.
Re: GCC, Switch( condition ) & Case
Posted on:
2009/2/23 18:17
#12
Quite a regular
Joined: 2008/11/3 12:06Last Login
: 2023/8/2 22:18
From South France
Group:
Registered Users
@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.
Re: GCC, Switch( condition ) & Case
Posted on:
2009/2/24 6:12
#13
Quite a regular
Joined: 2006/12/2 0:35Last Login
: 1/19 10:37
From Sydney
Group:
Registered Users
@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
Re: GCC, Switch( condition ) & Case
Posted on:
2009/2/24 17:57
#14
Quite a regular
Joined: 2008/11/3 12:06Last Login
: 2023/8/2 22:18
From South France
Group:
Registered Users
@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.
Re: GCC, Switch( condition ) & Case
Posted on:
2009/2/24 18:30
#15
Quite a regular
Joined: 2006/11/27 14:35Last Login
: Yesterday 22:14
From Nantes, France
Group:
Registered Users
@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
Re: GCC, Switch( condition ) & Case
Posted on:
2009/2/24 19:09
#16
Just can't stay away
Joined: 2006/12/1 19:07Last Login
: 11/16 10:57
From Germany
Group:
Registered Users
@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.
Re: GCC, Switch( condition ) & Case
Posted on:
2009/2/24 23:49
#17
Home away from home
Joined: 2006/11/20 16:26Last Login
: 11/3 19:40
From Norway
Group:
Registered Users
@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.
Re: GCC, Switch( condition ) & Case
Posted on:
2009/2/25 5:15
#18
Amigans Defender
Joined: 2006/11/26 23:16Last Login
: 8/31 22:09
From Canada
Group:
Staff members Moderators
@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
Re: GCC, Switch( condition ) & Case
Posted on:
2009/2/25 11:14
#19
Quite a regular
Joined: 2006/11/27 14:35Last Login
: Yesterday 22:14
From Nantes, France
Group:
Registered Users
@ssolie oops yes you'are right, I guess I have forgotten this.
Back to a quiet home... At last
Currently Active Users Viewing This Thread:
1
(
0 members
and 1 Anonymous Users
)