Skip to content

Commit 065752e

Browse files
Merge pull request #29 from Free-Pascal-meets-SDL-Website/usectypes
Use ctypes unit for c-type definitions if FPC is used for compilation 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.
2 parents 05d6d7a + 8104b96 commit 065752e

38 files changed

+1157
-1091
lines changed

units/ctypes.inc

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// C data types
2+
3+
{
4+
Simple DirectMedia Layer
5+
Copyright (C) 1997-2013 Sam Lantinga <[email protected]>
6+
7+
Pascal-Header-Conversion
8+
Copyright (C) 2012-2020 Tim Blume aka End/EV1313
9+
10+
SDL2-for-Pascal
11+
Copyright (C) 2020-2021 PGD Community
12+
13+
This file is part of the project above. It has solely the purpose
14+
to map common C data types correctly by Pascal compilers.
15+
16+
FPC: Most C data types are found in the native ctypes unit.
17+
Delphi + others: Relies on this file for C data type mapping.
18+
19+
These native C types should be strictly separated from
20+
types defined by SDL which can be found in sdlstdinc.inc.
21+
}
22+
23+
{$IFNDEF FPC}
24+
type
25+
DWord = LongWord;
26+
27+
pcint = ^cint;
28+
cint = Integer;
29+
{$EXTERNALSYM cint}
30+
31+
pcint8 = ^cint8;
32+
cint8 = ShortInt;
33+
{$EXTERNALSYM cint8}
34+
35+
pcuint8 = ^cuint8;
36+
cuint8 = Byte;
37+
{$EXTERNALSYM cuint8}
38+
39+
pcint16 = ^cint16;
40+
cint16 = SmallInt;
41+
{$EXTERNALSYM cint16}
42+
43+
pcuint16 = ^cuint16;
44+
cuint16 = Word;
45+
{$EXTERNALSYM cuint16}
46+
47+
pcint32 = ^cint32;
48+
cint32 = LongInt;
49+
{$EXTERNALSYM cint32}
50+
51+
pcuint32 = ^cuint32;
52+
cuint32 = LongWord;
53+
{$EXTERNALSYM cuint32}
54+
55+
{$IFNDEF Has_Int64}
56+
pcint64 = ^cint64;
57+
cint64 = record
58+
hi: cuint32;
59+
lo: cuint32;
60+
end;
61+
{$EXTERNALSYM cint64}
62+
63+
pcuint64 = ^cuint64;
64+
cuint64 = record
65+
hi: cuint32;
66+
lo: cuint32;
67+
end;
68+
{$EXTERNALSYM cuint64}
69+
{$ELSE}
70+
pcint64 = ^cint64;
71+
cint64 = Int64;
72+
{$EXTERNALSYM cint64}
73+
74+
pcuint64 = ^cuint64;
75+
cuint64 = UInt64;
76+
{$EXTERNALSYM cuint64}
77+
{$ENDIF}
78+
79+
{$IFNDEF WIN64}
80+
csize_t = cuint32;
81+
{$ELSE}
82+
csize_t = cuint64;
83+
{$ENDIF}
84+
{$EXTERNALSYM SIZE_T}
85+
86+
pcfloat = ^cfloat;
87+
cfloat = Single;
88+
{$EXTERNALSYM cfloat}
89+
{$ENDIF}
90+
91+
{ Data types for all compilers }
92+
type
93+
PUInt8Array = ^TUInt8Array;
94+
TUInt8Array = array [0..MAXINT shr 1] of cuint8;
95+
96+
ppcuint8 = ^pcuint8;
97+

units/sdl.inc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//from "sdl.h"
22

33
type
4-
TSDL_Init = type UInt32;
4+
TSDL_Init = type cuint32;
55

66
const
77
SDL_INIT_TIMER = TSDL_Init($00000001);
@@ -40,13 +40,13 @@ const
4040
* signal handlers for some commonly ignored fatal signals (like SIGSEGV).
4141
*}
4242

43-
function SDL_Init(flags: TSDL_Init): SInt32 cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_Init' {$ENDIF} {$ENDIF};
43+
function SDL_Init(flags: TSDL_Init): cint32 cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_Init' {$ENDIF} {$ENDIF};
4444

4545
{**
4646
* This function initializes specific SDL subsystems
4747
*}
4848

49-
function SDL_InitSubSystem(flags: TSDL_Init): SInt32 cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_InitSubSystem' {$ENDIF} {$ENDIF};
49+
function SDL_InitSubSystem(flags: TSDL_Init): cint32 cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_InitSubSystem' {$ENDIF} {$ENDIF};
5050

5151
{**
5252
* This function cleans up specific SDL subsystems
@@ -61,7 +61,7 @@ procedure SDL_QuitSubSystem(flags: TSDL_Init) cdecl; external SDL_LibName {$IFDE
6161
* If flags is 0, it returns a mask of all initialized subsystems.
6262
*}
6363

64-
function SDL_WasInit(flags: TSDL_Init): UInt32 cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_WasInit' {$ENDIF} {$ENDIF};
64+
function SDL_WasInit(flags: TSDL_Init): cuint32 cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_WasInit' {$ENDIF} {$ENDIF};
6565

6666
{**
6767
* This function cleans up all initialized subsystems. You should

units/sdl2.pas

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,17 @@ interface
9797

9898
{$IFDEF WINDOWS}
9999
uses
100+
{$IFDEF FPC}
101+
ctypes,
102+
{$ENDIF}
100103
Windows;
101104
{$ENDIF}
102105

103106
{$IF DEFINED(UNIX) AND NOT DEFINED(ANDROID)}
104107
uses
108+
{$IFDEF FPC}
109+
ctypes,
110+
{$ENDIF}
105111
{$IFDEF DARWIN}
106112
CocoaAll;
107113
{$ELSE}
@@ -135,6 +141,8 @@ interface
135141
{$ENDIF}
136142
{$ENDIF}
137143

144+
145+
{$I ctypes.inc} // C data types
138146
{$I sdlstdinc.inc}
139147
{$I sdlversion.inc}
140148
{$I sdlerror.inc}
@@ -183,7 +191,7 @@ procedure SDL_VERSION(out x: TSDL_Version);
183191
x.patch := SDL_PATCHLEVEL;
184192
end;
185193

186-
function SDL_VERSIONNUM(X,Y,Z: UInt32): Cardinal;
194+
function SDL_VERSIONNUM(X,Y,Z: cuint32): Cardinal;
187195
begin
188196
Result := X*1000 + Y*100 + Z;
189197
end;
@@ -201,7 +209,7 @@ function SDL_VERSION_ATLEAST(X,Y,Z: Cardinal): Boolean;
201209
end;
202210

203211
//from "sdl_mouse.h"
204-
function SDL_Button(button: SInt32): SInt32;
212+
function SDL_Button(button: cint32): cint32;
205213
begin
206214
Result := 1 shl (button - 1);
207215
end;
@@ -237,39 +245,39 @@ function SDL_PointInRect(const p: PSDL_Point; const r: PSDL_Rect): Boolean;
237245

238246
//from "sdl_rwops.h"
239247

240-
function SDL_RWsize(ctx: PSDL_RWops): SInt64;
248+
function SDL_RWsize(ctx: PSDL_RWops): cint64;
241249
begin
242250
Result := ctx^.size(ctx);
243251
end;
244252

245-
function SDL_RWseek(ctx: PSDL_RWops; offset: SInt64; whence: SInt32): SInt64;
253+
function SDL_RWseek(ctx: PSDL_RWops; offset: cint64; whence: cint32): cint64;
246254
begin
247255
Result := ctx^.seek(ctx,offset,whence);
248256
end;
249257

250-
function SDL_RWtell(ctx: PSDL_RWops): SInt64;
258+
function SDL_RWtell(ctx: PSDL_RWops): cint64;
251259
begin
252260
Result := ctx^.seek(ctx, 0, RW_SEEK_CUR);
253261
end;
254262

255-
function SDL_RWread(ctx: PSDL_RWops; ptr: Pointer; size: size_t; n: size_t): size_t;
263+
function SDL_RWread(ctx: PSDL_RWops; ptr: Pointer; size: csize_t; n: csize_t): csize_t;
256264
begin
257265
Result := ctx^.read(ctx, ptr, size, n);
258266
end;
259267

260-
function SDL_RWwrite(ctx: PSDL_RWops; ptr: Pointer; size: size_t; n: size_t): size_t;
268+
function SDL_RWwrite(ctx: PSDL_RWops; ptr: Pointer; size: csize_t; n: csize_t): csize_t;
261269
begin
262270
Result := ctx^.write(ctx, ptr, size, n);
263271
end;
264272

265-
function SDL_RWclose(ctx: PSDL_RWops): SInt32;
273+
function SDL_RWclose(ctx: PSDL_RWops): cint32;
266274
begin
267275
Result := ctx^.close(ctx);
268276
end;
269277

270278
//from "sdl_audio.h"
271279

272-
function SDL_LoadWAV(_file: PAnsiChar; spec: PSDL_AudioSpec; audio_buf: PPUInt8; audio_len: PUInt32): PSDL_AudioSpec;
280+
function SDL_LoadWAV(_file: PAnsiChar; spec: PSDL_AudioSpec; audio_buf: ppcuint8; audio_len: pcuint32): PSDL_AudioSpec;
273281
begin
274282
Result := SDL_LoadWAV_RW(SDL_RWFromFile(_file, 'rb'), 1, spec, audio_buf, audio_len);
275283
end;
@@ -349,7 +357,7 @@ function SDL_LoadBMP(_file: PAnsiChar): PSDL_Surface;
349357
end;
350358

351359
function SDL_SaveBMP(const surface: PSDL_Surface; const filename: AnsiString
352-
): sInt32;
360+
): cint32;
353361
begin
354362
Result := SDL_SaveBMP_RW(surface, SDL_RWFromFile(PAnsiChar(filename), 'wb'), 1)
355363
end;
@@ -393,23 +401,23 @@ function SDL_WINDOWPOS_ISCENTERED(X: Variant): Variant;
393401

394402
//from "sdl_events.h"
395403

396-
function SDL_GetEventState(type_: TSDL_EventType): UInt8;
404+
function SDL_GetEventState(type_: TSDL_EventType): cuint8;
397405
begin
398406
Result := SDL_EventState(type_, SDL_QUERY);
399407
end;
400408

401409
// from "sdl_timer.h"
402-
function SDL_TICKS_PASSED(const A, B: UInt32): Boolean;
410+
function SDL_TICKS_PASSED(const A, B: cuint32): Boolean;
403411
begin
404-
Result := ((Int64(B) - Int64(A)) <= 0)
412+
Result := ((cint64(B) - cint64(A)) <= 0)
405413
end;
406414

407415
// from "sdl_gamecontroller.h"
408416
{**
409417
* Load a set of mappings from a file, filtered by the current SDL_GetPlatform()
410418
*}
411419
function SDL_GameControllerAddMappingsFromFile(const FilePath: PAnsiChar
412-
): SInt32;
420+
): cint32;
413421
begin
414422
Result := SDL_GameControllerAddMappingsFromRW(SDL_RWFromFile(FilePath, 'rb'), 1)
415423
end;

0 commit comments

Comments
 (0)