Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle SDL_bool like a real boolean #30

Closed
Free-Pascal-meets-SDL-Website opened this issue Oct 30, 2021 · 6 comments
Closed

Handle SDL_bool like a real boolean #30

Free-Pascal-meets-SDL-Website opened this issue Oct 30, 2021 · 6 comments
Labels
discussion Discuss an idea/feature/PR/issue...

Comments

@Free-Pascal-meets-SDL-Website
Copy link
Collaborator

Actually we should translate SDL_bool by TSDL_Bool. This will generate compiler errors in situations as shown below:

(from sdl2.pas)

function SDL_VERSION_ATLEAST(X,Y,Z: cuint8): Boolean;
begin
  Result := SDL_COMPILEDVERSION >= SDL_VERSIONNUM(X,Y,Z);
end;

How should we handle or fix this?

@Free-Pascal-meets-SDL-Website
Copy link
Collaborator Author

C and Pascal handle booleans differently. In C they are basically integer values which are interpreted but in Pascal they form a special, separate type (or are at least treated as such).

So, what follows from this:

  • Macros which have a Boolean type result should be written as function with Pascal native Boolean type or cbool type depending on the further use of the macro.
  • SDL functions which have a SDL_Bool return value should be converted to return TSDL_Bool
  • SDL functions which need SDL_Bool value as argument will treat them correctly obviously
  • Pascal functions which rely on a TSDL_Bool value as argument should evaluate the integer values or typecast (see next point)
  • Attention: Typecast Boolean(TSDL_Bool) should be safe as TSDL_Bool can be 0 or 1 only, but in general care has to be taken with this cast, see http://www.delphigroups.info/2/10/12833.html
  • A logical expression can be returned into a TSDL_Bool value by typecasting, see example code below
program SDLBoolTest;

uses SDL2, ctypes, SysUtils;

var
  a, b: Integer;

function BoolTest(a, b: Integer): TSDL_Bool;
begin
  // works
  Result := TSDL_Bool(a > b);

  // does not work - Error: got "Boolean" expected "TSDL_Bool"
  //Result := (a > b);
end;

begin
  writeln('Bool Test a > b');
  for a:= 0 to 3 do
    for b := 0 to 3 do
      begin
        write('a = ' + IntToStr(a) + '; b = ' + IntToStr(b) +';    Result = ' + IntToStr(BoolTest(a, b)) + ' ');
        writeln(Boolean(BoolTest(a, b)));
      end;

  readln;
end.

So, for the example in the initial post it is correct to use the native Boolean, because a macro without a defined SDL_Bool return value should not adopt a fictional one.

@Free-Pascal-meets-SDL-Website Free-Pascal-meets-SDL-Website added the discussion Discuss an idea/feature/PR/issue... label Jun 20, 2022
@furious-programming
Copy link
Contributor

furious-programming commented Aug 3, 2022

For me, just change the declaration of TSDL_Bool to this:

type
  TSDL_Bool = System.Boolean;

and adopt all the code to the new type. Casting to native booleans should be avoided — the code should be adopted to the Pascal language, not to the C language. Additionally, some constant values should also be redeclared (eg. SDL_TRUE or SDL_ENABLE), since they also contains value 0 or 1 and they act like a booleans to toggle various options.

What do you think about it?

@suve
Copy link
Collaborator

suve commented Aug 3, 2022

The problem is that we need to maintain binary compatibility with the C library. According to the FPC Language Reference, Boolean has a size of 1 (the same as char), whereas the C definition is typedef int SDL_bool, i.e. the same size as an int (which is usually 2 or 4).

We could consider using LongBool, or ctypes.cbool, though.

@furious-programming
Copy link
Contributor

furious-programming commented Aug 3, 2022

We could consider using LongBool, or ctypes.cbool, though.

Exactly. After all, the same goes for the functions of the Win32 API, wherever the multibyte booleans are required.

Free-Pascal-meets-SDL-Website added a commit to Free-Pascal-meets-SDL-Website/SDL2-for-Pascal that referenced this issue Aug 9, 2022
@Free-Pascal-meets-SDL-Website
Copy link
Collaborator Author

I added a PR (#70) to solve this issue according to you proposal using cbool.

The code below works well, too now.

program SDLBoolTest;

uses SDL2, ctypes, SysUtils;

var
  a, b: Integer;

function BoolTest(a, b: Integer): TSDL_Bool;
begin
  // works
  //Result := TSDL_Bool(a > b);

  // works, too
  Result := (a > b);
end;

begin
  SetHeapTraceOutput('heaptrc.txt');

  writeln('Bool Test a > b');
  for a:= 0 to 3 do
    for b := 0 to 3 do
      begin
        write('a = ' + IntToStr(a) + '; b = ' + IntToStr(b) +';    Result = ');
        writeln(BoolTest(a, b));
      end;

  readln;
end.

Free-Pascal-meets-SDL-Website added a commit that referenced this issue Sep 24, 2022
Change TSDL_Bool to equal cbool
Closes issue #30
@Free-Pascal-meets-SDL-Website
Copy link
Collaborator Author

Closed by PR #70

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion Discuss an idea/feature/PR/issue...
Projects
None yet
Development

No branches or pull requests

3 participants