Skip to content

Commit 411bf4e

Browse files
Merge branch 'add-video-render-and-others' and conflicting main
2 parents 751af5b + e944d98 commit 411bf4e

15 files changed

+10309
-115
lines changed

examples/primitives.pas

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
{
2+
This file is part of:
3+
4+
SDL3 for Pascal
5+
(https://github.com/PascalGameDevelopment/SDL3-for-Pascal)
6+
SPDX-License-Identifier: Zlib
7+
}
8+
9+
{ Example is based on examples/renderer/02-primitives/primitives.c }
10+
11+
program primitives;
12+
13+
uses
14+
SDL3, SysUtils;
15+
16+
var
17+
Window: PSDL_Window = nil;
18+
Renderer: PSDL_Renderer = nil;
19+
Points: array[0..499] of TSDL_FPoint;
20+
Rect: TSDL_FRect;
21+
I: Integer;
22+
23+
begin
24+
if not SDL_Init(SDL_INIT_VIDEO) then
25+
begin
26+
27+
SDL_Log(PChar(Format('Couldn''t initialize SDL: %s', [SDL_GetError])));
28+
Exit;
29+
end;
30+
31+
if not SDL_CreateWindowAndRenderer('primitives', 640, 480, 0, @Window, @Renderer) then
32+
begin
33+
SDL_Log(PChar(Format('Couldn''t create window/renderer: %s', [SDL_GetError])));
34+
Exit;
35+
end;
36+
37+
{ Set up some random points }
38+
Randomize;
39+
for i := 0 to High(Points)-1 do
40+
begin
41+
Points[I].x:=(Random(10000)/100)*4.4 + 100.0;
42+
Points[I].y:=(Random(10000)/100)*2.8 + 100.0;
43+
end;
44+
45+
{ as you can see from this, rendering draws over whatever was drawn before it. }
46+
SDL_SetRenderDrawColor(Renderer, 33, 33, 33, SDL_ALPHA_OPAQUE); { dark gray, full alpha }
47+
SDL_RenderClear(Renderer); { start with a blank canvas. }
48+
49+
{ draw a filled rectangle in the middle of the canvas. }
50+
SDL_SetRenderDrawColor(Renderer, 0, 0, 255, SDL_ALPHA_OPAQUE); { blue, full alpha }
51+
Rect.x := 100;
52+
Rect.y := 100;
53+
Rect.w := 440;
54+
Rect.h := 280;
55+
SDL_RenderFillRect(Renderer, @Rect);
56+
57+
{ draw some points across the canvas. }
58+
SDL_SetRenderDrawColor(Renderer, 255, 0, 0, SDL_ALPHA_OPAQUE); { red, full alpha }
59+
SDL_RenderPoints(Renderer, @Points[0], High(Points)-1);
60+
61+
{ draw a unfilled Rectangle in-set a little bit. }
62+
SDL_SetRenderDrawColor(Renderer, 0, 255, 0, SDL_ALPHA_OPAQUE); { green, full alpha }
63+
Rect.x := Rect.x+30;
64+
Rect.y := Rect.y+30;
65+
Rect.w := Rect.w-60;
66+
Rect.h := Rect.h-60;
67+
SDL_RenderRect(Renderer, @Rect);
68+
69+
{ draw two lines in an X across the whole canvas. }
70+
SDL_SetRenderDrawColor(Renderer, 255, 255, 0, SDL_ALPHA_OPAQUE); { yellow, full alpha }
71+
SDL_RenderLine(Renderer, 0, 0, 640, 480);
72+
SDL_RenderLine(Renderer, 0, 480, 640, 0);
73+
74+
SDL_RenderPresent(Renderer); { put it all on the screen! }
75+
76+
SDL_Delay(2000);
77+
78+
SDL_Quit();
79+
end.
80+

units/SDL3.pas

Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
unit SDL3;
2+
3+
{
4+
SDL3-for-Pascal
5+
=================
6+
Pascal units for SDL3 - Simple Direct MediaLayer, Version 3
7+
8+
(to be extended - todo)
9+
10+
}
11+
12+
{$I sdl.inc}
13+
14+
interface
15+
16+
{$IFDEF WINDOWS}
17+
uses
18+
{$IFDEF FPC}
19+
ctypes,
20+
{$ENDIF}
21+
Windows;
22+
{$ENDIF}
23+
24+
{$IF DEFINED(UNIX) AND NOT DEFINED(ANDROID)}
25+
uses
26+
{$IFDEF FPC}
27+
ctypes,
28+
UnixType,
29+
{$ENDIF}
30+
{$IFDEF DARWIN}
31+
CocoaAll;
32+
{$ELSE}
33+
X,
34+
XLib;
35+
{$ENDIF}
36+
{$ENDIF}
37+
38+
{$IF DEFINED(UNIX) AND DEFINED(ANDROID) AND DEFINED(FPC)}
39+
uses
40+
ctypes,
41+
UnixType;
42+
{$ENDIF}
43+
44+
const
45+
46+
{$IFDEF WINDOWS}
47+
SDL_LibName = 'SDL3.dll';
48+
{$ENDIF}
49+
50+
{$IFDEF UNIX}
51+
{$IFDEF DARWIN}
52+
SDL_LibName = 'libSDL3.dylib';
53+
{$IFDEF FPC}
54+
{$LINKLIB libSDL2}
55+
{$ENDIF}
56+
{$ELSE}
57+
{$IFDEF FPC}
58+
SDL_LibName = 'libSDL3.so';
59+
{$ELSE}
60+
SDL_LibName = 'libSDL3.so.0';
61+
{$ENDIF}
62+
{$ENDIF}
63+
{$ENDIF}
64+
65+
{$IFDEF MACOS}
66+
SDL_LibName = 'SDL3';
67+
{$IFDEF FPC}
68+
{$linklib libSDL3}
69+
{$ENDIF}
70+
{$ENDIF}
71+
72+
{$I ctypes.inc} // C data types
73+
74+
{ The include file translates
75+
corresponding C header file.
76+
Inc file was updated against
77+
SDL_init.inc --> SDL_init.h this version of the header file: }
78+
{$I SDL_init.inc} // 3.1.6-prev
79+
{$I SDL_log.inc} // 3.1.6-prev
80+
{$I SDL_version.inc} // 3.1.6-prev
81+
{$I SDL_revision.inc} // 3.1.6-prev
82+
{$I SDL_stdinc.inc} // 3.1.6-prev (unfinished)
83+
{$I SDL_rect.inc} // 3.1.6-prev
84+
{$I SDL_properties.inc} // 3.1.6-prev
85+
{$I SDL_pixels.inc} // 3.1.6-prev
86+
{$I SDL_blendmode.inc} // 3.1.6-prev
87+
{$I SDL_iostream.inc} // 3.1.6-prev (unfinished)
88+
{$I SDL_surface.inc} // 3.1.6-prev
89+
{$I SDL_video.inc} // 3.1.6-prev
90+
{$I SDL_render.inc} // 3.1.6-prev
91+
{$I SDL_timer.inc} // 3.1.6-prev
92+
{$I SDL_error.inc} // 3.1.6-prev
93+
94+
95+
implementation
96+
97+
{ Macros from SDL_version.h }
98+
function SDL_VERSIONNUM(major, minor, patch: Integer): Integer;
99+
begin
100+
Result:=(major*1000000)+(minor*1000)+patch;
101+
end;
102+
103+
function SDL_VERSIONNUM_MAJOR(version: Integer): Integer;
104+
begin
105+
Result:=version div 1000000;
106+
end;
107+
108+
function SDL_VERSIONNUM_MINOR(version: Integer): Integer;
109+
begin
110+
Result:=(version div 1000) mod 1000;
111+
end;
112+
113+
function SDL_VERSIONNUM_MICRO(version: Integer): Integer;
114+
begin
115+
Result:=version mod 1000;
116+
end;
117+
118+
function SDL_VERSION: Integer;
119+
begin
120+
Result:=SDL_VERSIONNUM(SDL_MAJOR_VERSION, SDL_MINOR_VERSION, SDL_MICRO_VERSION);
121+
end;
122+
123+
function SDL_VERSION_ATLEAST(X, Y, Z: Integer): Boolean;
124+
begin
125+
if (SDL_VERSION >= SDL_VERSIONNUM(X, Y, Z)) then
126+
Result:=True
127+
else
128+
Result:=False;
129+
end;
130+
131+
{ Macros from SDL_rect.h }
132+
procedure SDL_RectToFRect(const rect: PSDL_Rect; frect: PSDL_FRect);
133+
begin
134+
frect^.x:=cfloat(rect^.x);
135+
frect^.y:=cfloat(rect^.y);
136+
frect^.w:=cfloat(rect^.w);
137+
frect^.h:=cfloat(rect^.h);
138+
end;
139+
140+
function SDL_PointInRect(const p: PSDL_Point; const r: PSDL_Rect): cbool;
141+
begin
142+
Result :=
143+
(p <> nil) and (r <> nil) and (p^.x >= r^.x) and (p^.x < (r^.x + r^.w)) and
144+
(p^.y >= r^.y) and (p^.y < (r^.y + r^.h));
145+
end;
146+
147+
function SDL_RectEmpty(const r: PSDL_Rect): cbool;
148+
begin
149+
Result := (r = nil) or (r^.w <= 0) or (r^.h <= 0);
150+
end;
151+
152+
function SDL_RectsEqual(const a: PSDL_Rect; const b: PSDL_Rect): cbool;
153+
begin
154+
Result := (a <> nil) and (b <> nil) and (a^.x = b^.x) and (a^.y = b^.y) and
155+
(a^.w = b^.w) and (a^.h = b^.h);
156+
end;
157+
158+
function SDL_PointInRectFloat(const p: PSDL_FPoint; const r: PSDL_FRect): cbool;
159+
begin
160+
Result :=
161+
(p <> nil) and (r <> nil) and (p^.x >= r^.x) and (p^.x <= (r^.x + r^.w)) and
162+
(p^.y >= r^.y) and (p^.y <= (r^.y + r^.h));
163+
end;
164+
165+
function SDL_RectEmptyFloat(const r: PSDL_FRect): cbool;
166+
begin
167+
Result := (r = nil) or (r^.w < cfloat(0.0)) or (r^.h < cfloat(0.0));
168+
end;
169+
170+
function SDL_RectsEqualEpsilon(const a: PSDL_Frect; const b: PSDL_FRect;
171+
const epsilon: cfloat): cbool;
172+
begin
173+
Result :=
174+
(a <> nil) and (b <> nil) and ((a = b) or
175+
((SDL_fabsf(a^.x - b^.x) <= epsilon) and
176+
(SDL_fabsf(a^.y - b^.y) <= epsilon) and
177+
(SDL_fabsf(a^.w - b^.w) <= epsilon) and
178+
(SDL_fabsf(a^.h - b^.h) <= epsilon)));
179+
end;
180+
181+
function SDL_RectsEqualFloat(const a: PSDL_FRect; b: PSDL_FRect): cbool;
182+
begin
183+
Result := SDL_RectsEqualEpsilon(a, b, SDL_FLT_EPSILON);
184+
end;
185+
186+
{ Macros from SDL_timer.h }
187+
function SDL_SECONDS_TO_NS(S: Integer): Integer;
188+
begin
189+
SDL_SECONDS_TO_NS:=(cuint64(S))*SDL_NS_PER_SECOND;
190+
end;
191+
192+
function SDL_NS_TO_SECONDS(NS: Integer): Integer;
193+
begin
194+
SDL_NS_TO_SECONDS:=NS div SDL_NS_PER_SECOND;
195+
end;
196+
197+
function SDL_MS_TO_NS(MS: Integer): Integer;
198+
begin
199+
SDL_MS_TO_NS:=(cuint64(MS))*SDL_NS_PER_MS;
200+
end;
201+
202+
function SDL_NS_TO_MS(NS: Integer): Integer;
203+
begin
204+
SDL_NS_TO_MS:=NS div SDL_NS_PER_MS;
205+
end;
206+
207+
function SDL_US_TO_NS(US: Integer): Integer;
208+
begin
209+
SDL_US_TO_NS:=(cuint64(US))*SDL_NS_PER_US;
210+
end;
211+
212+
function SDL_NS_TO_US(NS: Integer): Integer;
213+
begin
214+
SDL_NS_TO_US:=NS div SDL_NS_PER_US;
215+
end;
216+
217+
{ Macros from SDL_video.h }
218+
function SDL_WINDOWPOS_UNDEFINED_DISPLAY(X: Integer): Integer;
219+
begin
220+
Result := (SDL_WINDOWPOS_CENTERED_MASK or X);
221+
end;
222+
223+
function SDL_WINDOWPOS_ISUNDEFINED(X: Integer): Boolean;
224+
begin
225+
Result := (X and $FFFF0000) = SDL_WINDOWPOS_UNDEFINED_MASK;
226+
end;
227+
228+
function SDL_WINDOWPOS_CENTERED_DISPLAY(X: Integer): Integer;
229+
begin
230+
Result := (SDL_WINDOWPOS_CENTERED_MASK or X);
231+
end;
232+
233+
function SDL_WINDOWPOS_ISCENTERED(X: Integer): Boolean;
234+
begin
235+
Result := (X and $FFFF0000) = SDL_WINDOWPOS_CENTERED_MASK;
236+
end;
237+
238+
end.
239+

0 commit comments

Comments
 (0)