4
|
1 |
(*
|
|
2 |
* Hedgewars, a worms-like game
|
|
3 |
* Copyright (c) 2004, 2005 Andrey Korotaev <unC0Rr@gmail.com>
|
|
4 |
*
|
|
5 |
* Distributed under the terms of the BSD-modified licence:
|
|
6 |
*
|
|
7 |
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8 |
* of this software and associated documentation files (the "Software"), to deal
|
|
9 |
* with the Software without restriction, including without limitation the
|
|
10 |
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
11 |
* sell copies of the Software, and to permit persons to whom the Software is
|
|
12 |
* furnished to do so, subject to the following conditions:
|
|
13 |
*
|
|
14 |
* 1. Redistributions of source code must retain the above copyright notice,
|
|
15 |
* this list of conditions and the following disclaimer.
|
|
16 |
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
17 |
* this list of conditions and the following disclaimer in the documentation
|
|
18 |
* and/or other materials provided with the distribution.
|
|
19 |
* 3. The name of the author may not be used to endorse or promote products
|
|
20 |
* derived from this software without specific prior written permission.
|
|
21 |
*
|
|
22 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
|
23 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
24 |
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
|
25 |
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
26 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
27 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
|
28 |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
29 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
30 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
|
31 |
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
32 |
*)
|
|
33 |
|
|
34 |
unit SDLh;
|
|
35 |
interface
|
|
36 |
{$IFDEF LINUX}
|
|
37 |
{$DEFINE UNIX}
|
|
38 |
{$ENDIF}
|
|
39 |
{$IFDEF FREEBSD}
|
|
40 |
{$DEFINE UNIX}
|
|
41 |
{$ENDIF}
|
|
42 |
|
|
43 |
{$IFDEF UNIX}
|
|
44 |
{$linklib c}
|
|
45 |
{$linklib pthread} // кажется, это только для FreeBSD, не уверен
|
|
46 |
{$ENDIF}
|
|
47 |
|
|
48 |
{$IFDEF FPC}
|
|
49 |
{$MODE Delphi}
|
|
50 |
{$PACKRECORDS 4}
|
|
51 |
{$ENDIF}
|
|
52 |
|
|
53 |
(* SDL *)
|
|
54 |
const {$IFDEF WIN32}
|
|
55 |
SDLLibName = 'SDL.dll';
|
|
56 |
{$ENDIF}
|
|
57 |
{$IFDEF UNIX}
|
|
58 |
SDLLibName = 'libSDL.so';
|
|
59 |
{$ENDIF}
|
|
60 |
SDL_SWSURFACE = $00000000;
|
|
61 |
SDL_HWSURFACE = $00000001;
|
|
62 |
SDL_ASYNCBLIT = $00000004;
|
|
63 |
SDL_ANYFORMAT = $10000000;
|
|
64 |
SDL_HWPALETTE = $20000000;
|
|
65 |
SDL_DOUBLEBUF = $40000000;
|
|
66 |
SDL_FULLSCREEN = $80000000;
|
|
67 |
SDL_NOFRAME = $00000020;
|
|
68 |
SDL_HWACCEL = $00000100;
|
|
69 |
SDL_SRCCOLORKEY = $00001000;
|
|
70 |
SDL_RLEACCEL = $00004000;
|
|
71 |
|
|
72 |
SDL_NOEVENT = 0;
|
|
73 |
SDL_KEYDOWN = 2;
|
|
74 |
SDL_KEYUP = 3;
|
|
75 |
SDL_QUITEV = 12;
|
|
76 |
|
|
77 |
SDL_INIT_VIDEO = $00000020;
|
11
|
78 |
SDL_INIT_AUDIO = $00000010;
|
|
79 |
|
4
|
80 |
type PSDL_Rect = ^TSDL_Rect;
|
|
81 |
TSDL_Rect = record
|
|
82 |
x, y: SmallInt;
|
|
83 |
w, h: Word;
|
|
84 |
end;
|
|
85 |
|
|
86 |
TPoint = record
|
|
87 |
x: Integer;
|
|
88 |
y: Integer;
|
|
89 |
end;
|
|
90 |
|
|
91 |
PSDL_PixelFormat = ^TSDL_PixelFormat;
|
|
92 |
TSDL_PixelFormat = record
|
|
93 |
palette: Pointer;
|
|
94 |
BitsPerPixel : Byte;
|
|
95 |
BytesPerPixel: Byte;
|
|
96 |
Rloss : Byte;
|
|
97 |
Gloss : Byte;
|
|
98 |
Bloss : Byte;
|
|
99 |
Aloss : Byte;
|
|
100 |
Rshift: Byte;
|
|
101 |
Gshift: Byte;
|
|
102 |
Bshift: Byte;
|
|
103 |
Ashift: Byte;
|
|
104 |
RMask : Longword;
|
|
105 |
GMask : Longword;
|
|
106 |
BMask : Longword;
|
|
107 |
AMask : Longword;
|
|
108 |
colorkey: Longword;
|
|
109 |
alpha : Byte;
|
|
110 |
end;
|
|
111 |
|
|
112 |
|
|
113 |
PSDL_Surface = ^TSDL_Surface;
|
|
114 |
TSDL_Surface = record
|
|
115 |
flags : Longword;
|
|
116 |
format: PSDL_PixelFormat;
|
|
117 |
w, h : Integer;
|
|
118 |
pitch : Word;
|
|
119 |
pixels: Pointer;
|
|
120 |
offset: Integer;
|
|
121 |
hwdata: Pointer;
|
|
122 |
clip_rect: TSDL_Rect;
|
|
123 |
unused1,
|
|
124 |
locked : Longword;
|
|
125 |
Blitmap : Pointer;
|
|
126 |
format_version: Longword;
|
|
127 |
refcount : Integer;
|
|
128 |
end;
|
|
129 |
|
|
130 |
PSDL_Color = ^TSDL_Color;
|
|
131 |
TSDL_Color = record
|
|
132 |
r: Byte;
|
|
133 |
g: Byte;
|
|
134 |
b: Byte;
|
|
135 |
a: Byte;
|
|
136 |
end;
|
|
137 |
|
|
138 |
PSDL_RWops = ^TSDL_RWops;
|
|
139 |
TSeek = function( context: PSDL_RWops; offset: Integer; whence: Integer ): Integer; cdecl;
|
|
140 |
TRead = function( context: PSDL_RWops; Ptr: Pointer; size: Integer; maxnum : Integer ): Integer; cdecl;
|
|
141 |
TWrite = function( context: PSDL_RWops; Ptr: Pointer; size: Integer; num: Integer ): Integer; cdecl;
|
|
142 |
TClose = function( context: PSDL_RWops ): Integer; cdecl;
|
|
143 |
|
|
144 |
TStdio = record
|
|
145 |
autoclose: Integer;
|
|
146 |
fp: pointer;
|
|
147 |
end;
|
|
148 |
|
|
149 |
TMem = record
|
|
150 |
base: PByte;
|
|
151 |
here: PByte;
|
|
152 |
stop: PByte;
|
|
153 |
end;
|
|
154 |
|
|
155 |
TUnknown = record
|
|
156 |
data1: Pointer;
|
|
157 |
end;
|
|
158 |
|
|
159 |
TSDL_RWops = record
|
|
160 |
seek: TSeek;
|
|
161 |
read: TRead;
|
|
162 |
write: TWrite;
|
|
163 |
close: TClose;
|
|
164 |
type_: Longword;
|
|
165 |
case Byte of
|
|
166 |
0: (stdio: TStdio);
|
|
167 |
1: (mem: TMem);
|
|
168 |
2: (unknown: TUnknown);
|
|
169 |
end;
|
|
170 |
|
|
171 |
TSDL_KeySym = record
|
|
172 |
scancode: Byte;
|
|
173 |
sym,
|
|
174 |
modifier: Longword;
|
|
175 |
unicode: Word;
|
|
176 |
end;
|
|
177 |
|
|
178 |
TSDL_KeyboardEvent = record
|
|
179 |
type_: Byte;
|
|
180 |
which: Byte;
|
|
181 |
state: Byte;
|
|
182 |
keysym: TSDL_KeySym;
|
|
183 |
end;
|
|
184 |
|
|
185 |
TSDL_QuitEvent = record
|
|
186 |
type_: Byte;
|
|
187 |
end;
|
|
188 |
PSDL_Event = ^TSDL_Event;
|
|
189 |
TSDL_Event = record
|
|
190 |
case Byte of
|
|
191 |
SDL_NOEVENT: (type_: byte);
|
|
192 |
SDL_KEYDOWN, SDL_KEYUP: (key: TSDL_KeyboardEvent);
|
|
193 |
SDL_QUITEV: (quit: TSDL_QuitEvent);
|
|
194 |
end;
|
|
195 |
|
|
196 |
PByteArray = ^TByteArray;
|
|
197 |
TByteArray = array[0..32767] of Byte;
|
|
198 |
|
|
199 |
function SDL_Init(flags: Longword): Integer; cdecl; external SDLLibName;
|
|
200 |
procedure SDL_Quit; cdecl; external SDLLibName;
|
|
201 |
|
|
202 |
procedure SDL_Delay(msec: Longword); cdecl; external SDLLibName;
|
|
203 |
function SDL_GetTicks: Longword; cdecl; external SDLLibName;
|
|
204 |
|
|
205 |
function SDL_MustLock(Surface: PSDL_Surface): Boolean;
|
|
206 |
function SDL_LockSurface(Surface: PSDL_Surface): Integer; cdecl; external SDLLibName;
|
|
207 |
procedure SDL_UnlockSurface(Surface: PSDL_Surface); cdecl; external SDLLibName;
|
|
208 |
|
|
209 |
function SDL_GetError: PChar; cdecl; external SDLLibName;
|
|
210 |
|
|
211 |
function SDL_SetVideoMode(width, height, bpp: Integer; flags: Longword): PSDL_Surface; cdecl; external SDLLibName;
|
|
212 |
function SDL_CreateRGBSurface(flags: Longword; Width, Height, Depth: Integer; RMask, GMask, BMask, AMask: Longword): PSDL_Surface; cdecl; external SDLLibName;
|
|
213 |
function SDL_CreateRGBSurfaceFrom(pixels: Pointer; width, height, depth, pitch: Integer; RMask, GMask, BMask, AMask: Longword): PSDL_Surface; cdecl; external SDLLibName;
|
|
214 |
procedure SDL_FreeSurface(Surface: PSDL_Surface); cdecl; external SDLLibName;
|
|
215 |
function SDL_SetColorKey(surface: PSDL_Surface; flag, key: Longword): Integer; cdecl; external SDLLibName;
|
|
216 |
|
|
217 |
function SDL_UpperBlit(src: PSDL_Surface; srcrect: PSDL_Rect; dst: PSDL_Surface; dstrect: PSDL_Rect): Integer; cdecl; external SDLLibName;
|
|
218 |
function SDL_FillRect(dst: PSDL_Surface; dstrect: PSDL_Rect; color: Longword): Integer; cdecl; external SDLLibName;
|
|
219 |
procedure SDL_UpdateRect(Screen: PSDL_Surface; x, y: Integer; w, h: Longword); cdecl; external SDLLibName;
|
|
220 |
function SDL_Flip(Screen: PSDL_Surface): Integer; cdecl; external SDLLibName;
|
|
221 |
|
|
222 |
procedure SDL_GetRGB(pixel: Longword; fmt: PSDL_PixelFormat; r, g, b: PByte); cdecl; external SDLLibName;
|
|
223 |
function SDL_MapRGB(format: PSDL_PixelFormat; r, g, b: Byte): Integer; cdecl; external SDLLibName;
|
|
224 |
|
|
225 |
function SDL_DisplayFormat(Surface: PSDL_Surface): PSDL_Surface; cdecl; external SDLLibName;
|
35
|
226 |
function SDL_DisplayFormatAlpha(Surface: PSDL_Surface): PSDL_Surface; cdecl; external SDLLibName;
|
4
|
227 |
|
|
228 |
function SDL_RWFromFile(filename, mode: PChar): PSDL_RWops; cdecl; external SDLLibName;
|
|
229 |
function SDL_SaveBMP_RW(surface: PSDL_Surface; dst: PSDL_RWops; freedst: Integer): Integer; cdecl; external SDLLibName;
|
|
230 |
|
|
231 |
function SDL_GetKeyState(numkeys: PInteger): PByteArray; cdecl; external SDLLibName;
|
|
232 |
function SDL_GetMouseState(x, y: PInteger): Byte; cdecl; external SDLLibName;
|
|
233 |
function SDL_GetKeyName(key: Longword): PChar; cdecl; external SDLLibName;
|
|
234 |
procedure SDL_WarpMouse(x, y: Word); cdecl; external SDLLibName;
|
|
235 |
|
|
236 |
function SDL_PollEvent(event: PSDL_Event): Integer; cdecl; external SDLLibName;
|
|
237 |
|
|
238 |
function SDL_ShowCursor(toggle: Integer): Integer; cdecl; external SDLLibName;
|
|
239 |
|
|
240 |
procedure SDL_WM_SetCaption(title: PChar; icon: PChar); cdecl; external SDLLibName;
|
|
241 |
|
|
242 |
(* TTF *)
|
|
243 |
|
|
244 |
const {$IFDEF WIN32}
|
|
245 |
SDL_TTFLibName = 'SDL_ttf.dll';
|
|
246 |
{$ENDIF}
|
|
247 |
{$IFDEF UNIX}
|
|
248 |
SDL_TTFLibName = 'libSDL_ttf.so';
|
|
249 |
{$ENDIF}
|
|
250 |
|
|
251 |
|
|
252 |
type PTTF_Font = ^TTTF_font;
|
|
253 |
TTTF_Font = record
|
|
254 |
end;
|
|
255 |
|
|
256 |
function TTF_Init: integer; cdecl; external SDL_TTFLibName;
|
|
257 |
procedure TTF_Quit; cdecl; external SDL_TTFLibName;
|
|
258 |
|
|
259 |
|
|
260 |
function TTF_SizeText(font : PTTF_Font; const text: PChar; var w, h: integer): Integer; cdecl; external SDL_TTFLibName;
|
|
261 |
function TTF_RenderText_Solid(font : PTTF_Font; const text: PChar; fg: TSDL_Color): PSDL_Surface; cdecl; external SDL_TTFLibName;
|
|
262 |
function TTF_RenderText_Blended(font : PTTF_Font; const text: PChar; fg: TSDL_Color): PSDL_Surface; cdecl; external SDL_TTFLibName;
|
|
263 |
function TTF_OpenFont(const filename: Pchar; size: integer): PTTF_Font; cdecl; external SDL_TTFLibName;
|
|
264 |
|
|
265 |
(* SDL_mixer *)
|
|
266 |
|
|
267 |
const {$IFDEF WIN32}
|
|
268 |
SDL_MixerLibName = 'SDL_mixer.dll';
|
|
269 |
{$ENDIF}
|
|
270 |
{$IFDEF UNIX}
|
|
271 |
SDL_MixerLibName = 'libSDL_mixer.so';
|
|
272 |
{$ENDIF}
|
|
273 |
|
|
274 |
type PMixChunk = ^TMixChunk;
|
|
275 |
TMixChunk = record
|
|
276 |
allocated: Longword;
|
|
277 |
abuf : PByte;
|
|
278 |
alen : Longword;
|
|
279 |
volume : PByte;
|
|
280 |
end;
|
|
281 |
TMusic = (MUS_CMD, MUS_WAV, MUS_MOD, MUS_MID, MUS_OGG, MUS_MP3);
|
|
282 |
TMix_Fading = (MIX_NO_FADING, MIX_FADING_OUT, MIX_FADING_IN);
|
|
283 |
|
|
284 |
TMidiSong = record
|
|
285 |
samples : Integer;
|
|
286 |
events : pointer;
|
|
287 |
end;
|
|
288 |
|
|
289 |
TMusicUnion = record
|
|
290 |
case Byte of
|
|
291 |
0: ( midi : TMidiSong );
|
|
292 |
1: ( ogg : pointer);
|
|
293 |
end;
|
|
294 |
|
|
295 |
PMixMusic = ^TMixMusic;
|
|
296 |
TMixMusic = record
|
|
297 |
type_ : TMusic;
|
|
298 |
data : TMusicUnion;
|
|
299 |
fading : TMix_Fading;
|
|
300 |
fade_volume,
|
|
301 |
fade_step,
|
|
302 |
fade_steps,
|
|
303 |
error : integer;
|
|
304 |
end;
|
|
305 |
|
|
306 |
function Mix_OpenAudio(frequency: integer; format: Word; channels: integer; chunksize: integer): integer; cdecl; external SDL_MixerLibName;
|
|
307 |
procedure Mix_CloseAudio; cdecl; external SDL_MixerLibName;
|
|
308 |
|
|
309 |
function Mix_VolumeMusic(volume: integer): integer; cdecl; external SDL_MixerLibName;
|
|
310 |
|
11
|
311 |
function Mix_AllocateChannels(numchans: integer): integer; cdecl; external SDL_MixerLibName;
|
4
|
312 |
procedure Mix_FreeChunk(chunk: PMixChunk); cdecl; external SDL_MixerLibName;
|
|
313 |
procedure Mix_FreeMusic(music: PMixMusic); cdecl; external SDL_MixerLibName;
|
|
314 |
|
|
315 |
function Mix_LoadWAV_RW(src: PSDL_RWops; freesrc: integer): PMixChunk; cdecl; external SDL_MixerLibName;
|
|
316 |
function Mix_LoadMUS(const filename: PChar): PMixMusic; cdecl; external SDL_MixerLibName;
|
|
317 |
|
|
318 |
function Mix_Playing(channel: integer): integer; cdecl; external SDL_MixerLibName;
|
|
319 |
function Mix_PlayingMusic: integer; cdecl; external SDL_MixerLibName;
|
|
320 |
|
|
321 |
function Mix_PlayChannelTimed(channel: integer; chunk: PMixChunk; loops: integer; ticks: integer): integer; cdecl; external SDL_MixerLibName;
|
|
322 |
function Mix_PlayMusic(music: PMixMusic; loops: integer): integer; cdecl; external SDL_MixerLibName;
|
|
323 |
function Mix_HaltChannel(channel: integer): integer; cdecl; external SDL_MixerLibName;
|
|
324 |
|
|
325 |
(* SDL_image *)
|
|
326 |
|
|
327 |
const {$IFDEF WIN32}
|
|
328 |
SDL_ImageLibName = 'SDL_image.dll';
|
|
329 |
{$ENDIF}
|
|
330 |
{$IFDEF UNIX}
|
|
331 |
SDL_ImageLibName = 'libSDL_image.so';
|
|
332 |
{$ENDIF}
|
|
333 |
|
|
334 |
function IMG_Load(const _file: PChar): PSDL_Surface; cdecl; external SDL_ImageLibName;
|
|
335 |
|
|
336 |
(* SDL_net *)
|
|
337 |
|
|
338 |
const {$IFDEF WIN32}
|
|
339 |
SDL_NetLibName = 'SDL_net.dll';
|
|
340 |
{$ENDIF}
|
|
341 |
{$IFDEF UNIX}
|
|
342 |
SDL_NetLibName = 'libSDL_net.so';
|
|
343 |
{$ENDIF}
|
|
344 |
|
|
345 |
type TIPAddress = record
|
|
346 |
host: Longword;
|
|
347 |
port: Word;
|
|
348 |
end;
|
|
349 |
|
|
350 |
PTCPSocket = ^TTCPSocket;
|
|
351 |
TTCPSocket = record
|
|
352 |
ready,
|
|
353 |
channel: integer;
|
|
354 |
remoteAddress,
|
|
355 |
localAddress: TIPaddress;
|
|
356 |
sflag: integer;
|
|
357 |
end;
|
|
358 |
PSDLNet_SocketSet = ^TSDLNet_SocketSet;
|
|
359 |
TSDLNet_SocketSet = record
|
|
360 |
numsockets,
|
|
361 |
maxsockets: integer;
|
|
362 |
sockets: PTCPSocket;
|
|
363 |
end;
|
|
364 |
|
|
365 |
function SDLNet_Init: integer; cdecl; external SDL_NetLibName;
|
|
366 |
procedure SDLNet_Quit; cdecl; external SDL_NetLibName;
|
|
367 |
|
|
368 |
function SDLNet_AllocSocketSet(maxsockets: integer): PSDLNet_SocketSet; cdecl; external SDL_NetLibName;
|
|
369 |
function SDLNet_ResolveHost(var address: TIPaddress; host: PCHar; port: Word): integer; cdecl; external SDL_NetLibName;
|
|
370 |
function SDLNet_TCP_Accept(server: PTCPsocket): PTCPSocket; cdecl; external SDL_NetLibName;
|
|
371 |
function SDLNet_TCP_Open(var ip: TIPaddress): PTCPSocket; cdecl; external SDL_NetLibName;
|
|
372 |
function SDLNet_TCP_Send(sock: PTCPsocket; data: Pointer; len: integer): integer; cdecl; external SDL_NetLibName;
|
|
373 |
function SDLNet_TCP_Recv(sock: PTCPsocket; data: Pointer; len: integer): integer; cdecl; external SDL_NetLibName;
|
|
374 |
procedure SDLNet_TCP_Close(sock: PTCPsocket); cdecl; external SDL_NetLibName;
|
|
375 |
procedure SDLNet_FreeSocketSet(_set: PSDLNet_SocketSet); cdecl; external SDL_NetLibName;
|
|
376 |
function SDLNet_AddSocket(_set: PSDLNet_SocketSet; sock: PTCPSocket): integer; cdecl; external SDL_NetLibName;
|
|
377 |
function SDLNet_CheckSockets(_set: PSDLNet_SocketSet; timeout: integer): integer; cdecl; external SDL_NetLibName;
|
|
378 |
|
|
379 |
|
|
380 |
implementation
|
|
381 |
|
|
382 |
function SDL_MustLock(Surface: PSDL_Surface): Boolean;
|
|
383 |
begin
|
|
384 |
Result:= ( surface^.offset <> 0 )
|
|
385 |
or(( surface^.flags and (SDL_HWSURFACE or SDL_ASYNCBLIT or SDL_RLEACCEL)) <> 0)
|
|
386 |
end;
|
|
387 |
|
|
388 |
end.
|