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

Use ctypes unit for c-type definitions if FPC is used for compilation #29

Conversation

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

@Free-Pascal-meets-SDL-Website Free-Pascal-meets-SDL-Website commented Sep 30, 2021

Compare issue #26 for discussion and details about this PR.

Main features:

  • map all variables/parameters/return values consistently against native C types by using ctypes unit (FPC) or our new ctypes.inc (Delphi + others)
  • use one naming style (style from ctypes unit) throughout the whole project (cint instead of a mixture of Integer/Int/SInt32/UInt32/...)
  • prevent access violations or unpredictable behaviour by using/expecting accidently wrong memory size of data types (see e.g. C's int on different platforms)
  • makes the code much more comprehensible and tracking of memory related bugs much easier

Necessity

As a Pascal developer you need to know the exact C variable type of every parameter of the parameter list of a function, instead of what we believe the C function is asking for. E.g. prior to this update C's int was often translated to SInt32, which will work well most often, but as a Pascal developer it is hidden to you, that the platform dependent int is actually expected. This may raise bugs when developing for a system where int does not correspond to SInt32.

- use unit ctypes if FPC is used
- else (Delphi) use our own type definitions
@suve
Copy link
Collaborator

suve commented Sep 30, 2021

Looks good, though I wonder if we should add a definition for int / unsigned int as well. Right now we use either SInt32 or Pascal's Integer in these places - which are fixed as 32-bit, whereas C's int can theoretically be anything 16 bits or larger.

Alternatively, if we don't mind doing some larger changes to the code, we could use a different approach:

  • convert all the code to use the definitions from ctypes - cint, cfloat, etc.
  • use an {$IFDEF} to check if we're compiling using FPC
  • if FPC, simply do uses ctypes and be done with it
  • otherwise, have a massive Type block that holds its own definition for each type (cint, cfloat, etc.)

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

Looks good, though I wonder if we should add a definition for int / unsigned int as well. Right now we use either SInt32 or Pascal's Integer in these places - which are fixed as 32-bit, whereas C's int can theoretically be anything 16 bits or larger.

Good idea and totally agree!

Alternatively, if we don't mind doing some larger changes to the code, we could use a different approach:

* convert all the code to use the definitions from `ctypes` - `cint`, `cfloat`, etc.

* use an `{$IFDEF}` to check if we're compiling using FPC

* if FPC, simply do `uses ctypes` and be done with it

* otherwise, have a massive `Type` block that holds its own definition for each type (`cint`, `cfloat`, etc.)

I like the idea. Otherwise this would mean to convert all files to the new type names in one large go.

@Free-Pascal-meets-SDL-Website Free-Pascal-meets-SDL-Website merged commit 065752e into PascalGameDevelopment:master Oct 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants