SDL_bool

Hi all!

I’m wondering why SDL uses simple

typedef enum SDL_bool {
SDL_FALSE = 0,
SDL_TRUE = 1
} SDL_bool;

Maybe for better C++ interface it could use something like

#ifdef __cplusplus
typedef SDL_bool bool;
typedef SDL_FALSE false;
typedef SDL_TRUE true;
#else
typedef enum SDL_bool {
SDL_FALSE = 0,
SDL_TRUE = 1
} SDL_bool;
#endif

But I dont know, maybe there will be some binary incompatabilities
then (on some compilers on some platforms)?–
Roman Kyrylych (??? ???)

false/true are not a type as far as I know the code you suggest is invalid.On 7/11/06, Roman Kyrylych <roman.kyrylych at gmail.com> wrote:

Hi all!

I’m wondering why SDL uses simple

typedef enum SDL_bool {
SDL_FALSE = 0,
SDL_TRUE = 1
} SDL_bool;

Maybe for better C++ interface it could use something like

#ifdef __cplusplus
typedef SDL_bool bool;
typedef SDL_FALSE false;
typedef SDL_TRUE true;
#else
typedef enum SDL_bool {
SDL_FALSE = 0,
SDL_TRUE = 1
} SDL_bool;
#endif

But I dont know, maybe there will be some binary incompatabilities
then (on some compilers on some platforms)?


Roman Kyrylych (??? ???)


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Roman Kyrylych wrote:

[…]

Maybe for better C++ interface it could use something like

I suppose that since SDL is written in C, they don’t bother about
complete C++ compatibility…

2006/7/11, Olivier Delannoy <olivier.delannoy at gmail.com>:

false/true are not a type as far as I know the code you suggest is invalid.

Oh, I made a mistake. Correct code should be
#ifdef __cplusplus
typedef SDL_bool bool;
#define SDL_FALSE false
#define SDL_TRUE true
#else
typedef enum SDL_bool {
SDL_FALSE = 0,
SDL_TRUE = 1
} SDL_bool;
#endif

Is this OK?–
Roman Kyrylych (??? ???)

Hi all!

I’m wondering why SDL uses simple

typedef enum SDL_bool {
SDL_FALSE = 0,
SDL_TRUE = 1
} SDL_bool;

Maybe for better C++ interface it could use something like

#ifdef __cplusplus
typedef SDL_bool bool;
typedef SDL_FALSE false;
typedef SDL_TRUE true;
#else
typedef enum SDL_bool {
SDL_FALSE = 0,
SDL_TRUE = 1
} SDL_bool;
#endif

But I dont know, maybe there will be some binary incompatabilities
then (on some compilers on some platforms)?


Roman Kyrylych (??? ???)


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

i would suggest you use an int for that. if you want it to be readable then #define MY_TRUE = -1 and #define MY_FALSE 0. then to compare just use it like:

int my_bool;

my_bool = MY_TRUE;

if(my_bool) printf(“true\n”);

this can be faster since its only testing if its zero or non zero, if you compiler is not optimizing or smart enough then it is comparing (subtracting) then testing. you could write some short code and look at the machine code from the compiler and see what it will do and how that works.

matt

----- Original Message -----
From: Roman Kyrylych <roman.kyrylych at gmail.com>
Sent: Jul 11, 2006 3:48 AM
To: “A list for developers using the SDL library. (includes SDL-announce)”
Subject: [SDL] SDL_bool

Roman Kyrylych schrieb:

Maybe for better C++ interface it could use something like

#ifdef __cplusplus
typedef SDL_bool bool;
typedef SDL_FALSE false;
typedef SDL_TRUE true;
#else
typedef enum SDL_bool {
SDL_FALSE = 0,
SDL_TRUE = 1
} SDL_bool;
#endif

I fail to see the whole point of redefining SDL_bool. It works
perfectly, doesn’t it? As opposed to, when you change the define in the
header file, you would also need to provide SDL binaries that use bool
instead of SDL_bool. Seems a bit messy, and I don’t think it would make
that many C++ programmers that much happier that it would be worth it.

-Sebastian

mattmatteh at earthlink.net wrote:

i would suggest you use an int for that. if you want it to be readable then #define MY_TRUE = -1 and #define MY_FALSE 0. then to compare just use it like:

int my_bool;

my_bool = MY_TRUE;

if(my_bool) printf(“true\n”);

this can be faster since its only testing if its zero or non zero, if you compiler is not optimizing or smart enough then it is comparing (subtracting) then testing. you could write some short code and look at the machine code from the compiler and see what it will do and how that works.

enums are int’s on pretty much every compiler.

Using -1 wouldn’t make it any faster as the compiler would still test
for zero/nonzero. Furthermore, if you cast C++'s true to an integer
type, the result is 1, not -1, so it is more compatible to use 1.

It is not a good idea to change between using an int and using a real
bool, as the size is different. On a lot of compilers, bool is a byte,
on some others it gets bit packed.

Pete.

2006/7/11, Sebastian Beschke <sebastian.beschke at student.uni-tuebingen.de>:

I fail to see the whole point of redefining SDL_bool. It works
perfectly, doesn’t it? As opposed to, when you change the define in the
header file, you would also need to provide SDL binaries that use bool
instead of SDL_bool. Seems a bit messy, and I don’t think it would make
that many C++ programmers that much happier that it would be worth it.

OK, I just asked. I use mostly pure C too and very rarely C++.
Recently I played with SDL_Config, this library uses int for C
interface and bool for C++ so I was wondering if this can be in SDL,
and if not - why.–
Roman Kyrylych (??? ???)

Nope. SDL is a library written in C. Think of what happens when, for
example, your C++ game calls a C library function returning or taking an
SDL_bool as an argument. If it even links at all, you’ll end up with
undefined behaviour. Which is bad.

  • GerryOn Tue, 11 Jul 2006 13:33:55 +0200, Roman Kyrylych <roman.kyrylych at gmail.com> wrote:

Is this OK?