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;
|
|
226 |
|
|
227 |
function SDL_RWFromFile(filename, mode: PChar): PSDL_RWops; cdecl; external SDLLibName;
|
|
228 |
function SDL_SaveBMP_RW(surface: PSDL_Surface; dst: PSDL_RWops; freedst: Integer): Integer; cdecl; external SDLLibName;
|
|
229 |
|
|
230 |
function SDL_GetKeyState(numkeys: PInteger): PByteArray; cdecl; external SDLLibName;
|
|
231 |
function SDL_GetMouseState(x, y: PInteger): Byte; cdecl; external SDLLibName;
|
|
232 |
function SDL_GetKeyName(key: Longword): PChar; cdecl; external SDLLibName;
|
|
233 |
procedure SDL_WarpMouse(x, y: Word); cdecl; external SDLLibName;
|
|
234 |
|
|
235 |
function SDL_PollEvent(event: PSDL_Event): Integer; cdecl; external SDLLibName;
|
|
236 |
|
|
237 |
function SDL_ShowCursor(toggle: Integer): Integer; cdecl; external SDLLibName;
|
|
238 |
|
|
239 |
procedure SDL_WM_SetCaption(title: PChar; icon: PChar); cdecl; external SDLLibName;
|
|
240 |
|
|
241 |
(* TTF *)
|
|
242 |
|
|
243 |
const {$IFDEF WIN32}
|
|
244 |
SDL_TTFLibName = 'SDL_ttf.dll';
|
|
245 |
{$ENDIF}
|
|
246 |
{$IFDEF UNIX}
|
|
247 |
SDL_TTFLibName = 'libSDL_ttf.so';
|
|
248 |
{$ENDIF}
|
|
249 |
|
|
250 |
|
|
251 |
type PTTF_Font = ^TTTF_font;
|
|
252 |
TTTF_Font = record
|
|
253 |
end;
|
|
254 |
|
|
255 |
function TTF_Init: integer; cdecl; external SDL_TTFLibName;
|
|
256 |
procedure TTF_Quit; cdecl; external SDL_TTFLibName;
|
|
257 |
|
|
258 |
|
|
259 |
function TTF_SizeText(font : PTTF_Font; const text: PChar; var w, h: integer): Integer; cdecl; external SDL_TTFLibName;
|
|
260 |
function TTF_RenderText_Solid(font : PTTF_Font; const text: PChar; fg: TSDL_Color): PSDL_Surface; cdecl; external SDL_TTFLibName;
|
|
261 |
function TTF_RenderText_Blended(font : PTTF_Font; const text: PChar; fg: TSDL_Color): PSDL_Surface; cdecl; external SDL_TTFLibName;
|
|
262 |
function TTF_OpenFont(const filename: Pchar; size: integer): PTTF_Font; cdecl; external SDL_TTFLibName;
|
|
263 |
|
|
264 |
(* SDL_mixer *)
|
|
265 |
|
|
266 |
const {$IFDEF WIN32}
|
|
267 |
SDL_MixerLibName = 'SDL_mixer.dll';
|
|
268 |
{$ENDIF}
|
|
269 |
{$IFDEF UNIX}
|
|
270 |
SDL_MixerLibName = 'libSDL_mixer.so';
|
|
271 |
{$ENDIF}
|
|
272 |
|
|
273 |
type PMixChunk = ^TMixChunk;
|
|
274 |
TMixChunk = record
|
|
275 |
allocated: Longword;
|
|
276 |
abuf : PByte;
|
|
277 |
alen : Longword;
|
|
278 |
volume : PByte;
|
|
279 |
end;
|
|
280 |
TMusic = (MUS_CMD, MUS_WAV, MUS_MOD, MUS_MID, MUS_OGG, MUS_MP3);
|
|
281 |
TMix_Fading = (MIX_NO_FADING, MIX_FADING_OUT, MIX_FADING_IN);
|
|
282 |
|
|
283 |
TMidiSong = record
|
|
284 |
samples : Integer;
|
|
285 |
events : pointer;
|
|
286 |
end;
|
|
287 |
|
|
288 |
TMusicUnion = record
|
|
289 |
case Byte of
|
|
290 |
0: ( midi : TMidiSong );
|
|
291 |
1: ( ogg : pointer);
|
|
292 |
end;
|
|
293 |
|
|
294 |
PMixMusic = ^TMixMusic;
|
|
295 |
TMixMusic = record
|
|
296 |
type_ : TMusic;
|
|
297 |
data : TMusicUnion;
|
|
298 |
fading : TMix_Fading;
|
|
299 |
fade_volume,
|
|
300 |
fade_step,
|
|
301 |
fade_steps,
|
|
302 |
error : integer;
|
|
303 |
end;
|
|
304 |
|
|
305 |
function Mix_OpenAudio(frequency: integer; format: Word; channels: integer; chunksize: integer): integer; cdecl; external SDL_MixerLibName;
|
|
306 |
procedure Mix_CloseAudio; cdecl; external SDL_MixerLibName;
|
|
307 |
|
|
308 |
function Mix_VolumeMusic(volume: integer): integer; cdecl; external SDL_MixerLibName;
|
|
309 |
|
11
|
310 |
function Mix_AllocateChannels(numchans: integer): integer; cdecl; external SDL_MixerLibName;
|
4
|
311 |
procedure Mix_FreeChunk(chunk: PMixChunk); cdecl; external SDL_MixerLibName;
|
|
312 |
procedure Mix_FreeMusic(music: PMixMusic); cdecl; external SDL_MixerLibName;
|
|
313 |
|
|
314 |
function Mix_LoadWAV_RW(src: PSDL_RWops; freesrc: integer): PMixChunk; cdecl; external SDL_MixerLibName;
|
|
315 |
function Mix_LoadMUS(const filename: PChar): PMixMusic; cdecl; external SDL_MixerLibName;
|
|
316 |
|
|
317 |
function Mix_Playing(channel: integer): integer; cdecl; external SDL_MixerLibName;
|
|
318 |
function Mix_PlayingMusic: integer; cdecl; external SDL_MixerLibName;
|
|
319 |
|
|
320 |
function Mix_PlayChannelTimed(channel: integer; chunk: PMixChunk; loops: integer; ticks: integer): integer; cdecl; external SDL_MixerLibName;
|
|
321 |
function Mix_PlayMusic(music: PMixMusic; loops: integer): integer; cdecl; external SDL_MixerLibName;
|
|
322 |
function Mix_HaltChannel(channel: integer): integer; cdecl; external SDL_MixerLibName;
|
|
323 |
|
|
324 |
(* SDL_image *)
|
|
325 |
|
|
326 |
const {$IFDEF WIN32}
|
|
327 |
SDL_ImageLibName = 'SDL_image.dll';
|
|
328 |
{$ENDIF}
|
|
329 |
{$IFDEF UNIX}
|
|
330 |
SDL_ImageLibName = 'libSDL_image.so';
|
|
331 |
{$ENDIF}
|
|
332 |
|
|
333 |
function IMG_Load(const _file: PChar): PSDL_Surface; cdecl; external SDL_ImageLibName;
|
|
334 |
|
|
335 |
(* SDL_net *)
|
|
336 |
|
|
337 |
const {$IFDEF WIN32}
|
|
338 |
SDL_NetLibName = 'SDL_net.dll';
|
|
339 |
{$ENDIF}
|
|
340 |
{$IFDEF UNIX}
|
|
341 |
SDL_NetLibName = 'libSDL_net.so';
|
|
342 |
{$ENDIF}
|
|
343 |
|
|
344 |
type TIPAddress = record
|
|
345 |
host: Longword;
|
|
346 |
port: Word;
|
|
347 |
end;
|
|
348 |
|
|
349 |
PTCPSocket = ^TTCPSocket;
|
|
350 |
TTCPSocket = record
|
|
351 |
ready,
|
|
352 |
channel: integer;
|
|
353 |
remoteAddress,
|
|
354 |
localAddress: TIPaddress;
|
|
355 |
sflag: integer;
|
|
356 |
end;
|
|
357 |
PSDLNet_SocketSet = ^TSDLNet_SocketSet;
|
|
358 |
TSDLNet_SocketSet = record
|
|
359 |
numsockets,
|
|
360 |
maxsockets: integer;
|
|
361 |
sockets: PTCPSocket;
|
|
362 |
end;
|
|
363 |
|
|
364 |
function SDLNet_Init: integer; cdecl; external SDL_NetLibName;
|
|
365 |
procedure SDLNet_Quit; cdecl; external SDL_NetLibName;
|
|
366 |
|
|
367 |
function SDLNet_AllocSocketSet(maxsockets: integer): PSDLNet_SocketSet; cdecl; external SDL_NetLibName;
|
|
368 |
function SDLNet_ResolveHost(var address: TIPaddress; host: PCHar; port: Word): integer; cdecl; external SDL_NetLibName;
|
|
369 |
function SDLNet_TCP_Accept(server: PTCPsocket): PTCPSocket; cdecl; external SDL_NetLibName;
|
|
370 |
function SDLNet_TCP_Open(var ip: TIPaddress): PTCPSocket; cdecl; external SDL_NetLibName;
|
|
371 |
function SDLNet_TCP_Send(sock: PTCPsocket; data: Pointer; len: integer): integer; cdecl; external SDL_NetLibName;
|
|
372 |
function SDLNet_TCP_Recv(sock: PTCPsocket; data: Pointer; len: integer): integer; cdecl; external SDL_NetLibName;
|
|
373 |
procedure SDLNet_TCP_Close(sock: PTCPsocket); cdecl; external SDL_NetLibName;
|
|
374 |
procedure SDLNet_FreeSocketSet(_set: PSDLNet_SocketSet); cdecl; external SDL_NetLibName;
|
|
375 |
function SDLNet_AddSocket(_set: PSDLNet_SocketSet; sock: PTCPSocket): integer; cdecl; external SDL_NetLibName;
|
|
376 |
function SDLNet_CheckSockets(_set: PSDLNet_SocketSet; timeout: integer): integer; cdecl; external SDL_NetLibName;
|
|
377 |
|
|
378 |
|
|
379 |
implementation
|
|
380 |
|
|
381 |
function SDL_MustLock(Surface: PSDL_Surface): Boolean;
|
|
382 |
begin
|
|
383 |
Result:= ( surface^.offset <> 0 )
|
|
384 |
or(( surface^.flags and (SDL_HWSURFACE or SDL_ASYNCBLIT or SDL_RLEACCEL)) <> 0)
|
|
385 |
end;
|
|
386 |
|
|
387 |
end.
|