SDL2-for-Pascal icon indicating copy to clipboard operation
SDL2-for-Pascal copied to clipboard

TSDL_Bool not cbool

Open sechshelme opened this issue 2 years ago • 3 comments

SetColorKey requires an integer as the second parameter. (SDL_surface.h; line: 446)

extern DECLSPEC int SDLCALL SDL_SetColorKey(SDL_Surface * surface, int flag, Uint32 key);

And SDL_FALSE & SDL_TRUE are also declared as integers. (SDL_stdinc.h; line: 184)

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

So, in my opinion, TSDL_Bool should also be declared as an integer. I modified the following in "sdlstdinc.inc":

type
   TSDL_Bool = cint;
// TSDL_Bool = cbool;

Do you also think that this is more the case?

I am using Lazarus 2.2.4

sechshelme avatar Jan 22 '23 16:01 sechshelme

Hi @sechshelme,

we had a longer discussion about handling of SDL_bool in this issue: https://github.com/PascalGameDevelopment/SDL2-for-Pascal/issues/30

Since the boolean data type is not the same as the integer data type (although they can be implemented via integer types) they should be distinguished. In contrast to C this is strongly done in Pascal, hence the problem is not with TSDL_Bool but rather with the flawed data type if the flag argument in SDL_SetColorKey. The flag should be of SDL_bool type instead of int type.

You should go with a cint type-cast instead of changing the TSDL_Bool data type. Use for example cint(SDL_TRUE) or Integer(SDL_TRUE) for the flag argument.

Best regards Matthias

And who would change it like that?

type
  TSDL_Bool = cbool;

const
  SDL_FALSE = 0;
  SDL_TRUE  = 1;
//  SDL_FALSE = TSDL_Bool(0);
//  SDL_TRUE  = TSDL_Bool(1);   

or

const
  SDL_FALSE = cint(0);
  SDL_TRUE  = cint(1);

sechshelme avatar Jan 23 '23 16:01 sechshelme

@sechshelme

I see your point, but please consider these two functions which will just return the arguments bool value:

(1) function BoolArgument(ABool: TSDL_Bool); TSDL_Bool; (2) function IntArgument(ABoolFlag: cint): TSDL_Bool;

Case 1: SDL_TRUE/SDL_FALSE is declared as is in our headers:

BoolArgument(SDL_True);  // returns True
IntArgument(SDL_True);  // compiling error: argument must be of integer type

Case 2: SDL_TRUE/SDL_FALSE is declared as you propose as integers:

BoolArgument(SDL_True);  // compiling error: argument must be of bool type
IntArgument(SDL_True);  // returns True (if casted within the function body)

As Pascal strongly distinguishes the bool type from the integer type there cannot be ONE declaration for SDL_TRUE/SDL_FALSE to fit both cases. In contrast to this in C the bool type is a simple integer type which is interpreted as a bool type, hence you can use SDL_TRUE in both cases without compiler errors. In Pascal at some point there has to be a type-cast.

We decided to go for a bool type representation by SDL_TRUE and SDL_FALSE as this is their actual meaning. - In C there is no other way as to represent them as integers, but nobody would actually do something like SDL_FALSE + 3 * SDL_TRUE; you see what I mean.

Best regards Matthias