hedgewars/SDLh.pas
changeset 1 30f2d1037d5d
child 4 bcbd7adb4e4b
equal deleted inserted replaced
0:475c0f2f9d17 1:30f2d1037d5d
       
     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;
       
    78 type PSDL_Rect = ^TSDL_Rect;
       
    79      TSDL_Rect = record
       
    80                  x, y: SmallInt;
       
    81                  w, h: Word;
       
    82                  end;
       
    83 
       
    84      TPoint = record
       
    85               x: Integer;
       
    86               y: Integer;
       
    87               end;
       
    88 
       
    89      PSDL_PixelFormat = ^TSDL_PixelFormat;
       
    90      TSDL_PixelFormat = record
       
    91                         palette: Pointer;
       
    92                         BitsPerPixel : Byte;
       
    93                         BytesPerPixel: Byte;
       
    94                         Rloss : Byte;
       
    95                         Gloss : Byte;
       
    96                         Bloss : Byte;
       
    97                         Aloss : Byte;
       
    98                         Rshift: Byte;
       
    99                         Gshift: Byte;
       
   100                         Bshift: Byte;
       
   101                         Ashift: Byte;
       
   102                         RMask : Longword;
       
   103                         GMask : Longword;
       
   104                         BMask : Longword;
       
   105                         AMask : Longword;
       
   106                         colorkey: Longword;
       
   107                         alpha : Byte;
       
   108                         end;
       
   109 
       
   110 
       
   111      PSDL_Surface = ^TSDL_Surface;
       
   112      TSDL_Surface = record
       
   113                     flags : Longword;
       
   114                     format: PSDL_PixelFormat;
       
   115                     w, h  : Integer;
       
   116                     pitch : Word;
       
   117                     pixels: Pointer;
       
   118                     offset: Integer;
       
   119                     hwdata: Pointer;
       
   120                     clip_rect: TSDL_Rect;
       
   121                     unused1,
       
   122                     locked   : Longword;
       
   123                     Blitmap  : Pointer;
       
   124                     format_version: Longword;
       
   125                     refcount : Integer;
       
   126                     end;
       
   127 
       
   128      PSDL_Color = ^TSDL_Color;
       
   129      TSDL_Color = record
       
   130                   r: Byte;
       
   131                   g: Byte;
       
   132                   b: Byte;
       
   133                   a: Byte;
       
   134                   end;
       
   135 
       
   136      PSDL_RWops = ^TSDL_RWops;
       
   137      TSeek = function( context: PSDL_RWops; offset: Integer; whence: Integer ): Integer; cdecl;
       
   138      TRead = function( context: PSDL_RWops; Ptr: Pointer; size: Integer; maxnum : Integer ): Integer;  cdecl;
       
   139      TWrite = function( context: PSDL_RWops; Ptr: Pointer; size: Integer; num: Integer ): Integer; cdecl;
       
   140      TClose = function( context: PSDL_RWops ): Integer; cdecl;
       
   141 
       
   142      TStdio = record
       
   143               autoclose: Integer;
       
   144               fp: pointer;
       
   145               end;
       
   146 
       
   147      TMem = record
       
   148             base: PByte;
       
   149             here: PByte;
       
   150             stop: PByte;
       
   151             end;
       
   152 
       
   153      TUnknown = record
       
   154                 data1: Pointer;
       
   155                 end;
       
   156 
       
   157      TSDL_RWops = record
       
   158                   seek: TSeek;
       
   159                   read: TRead;
       
   160                   write: TWrite;
       
   161                   close: TClose;
       
   162                   type_: Longword;
       
   163                   case Byte of
       
   164                        0: (stdio: TStdio);
       
   165                        1: (mem: TMem);
       
   166                        2: (unknown: TUnknown);
       
   167                        end;
       
   168 
       
   169      TSDL_KeySym = record
       
   170                    scancode: Byte;
       
   171                    sym,
       
   172                    modifier: Longword;
       
   173                    unicode: Word;
       
   174                    end;
       
   175 
       
   176      TSDL_KeyboardEvent = record
       
   177                           type_: Byte;
       
   178                           which: Byte;
       
   179                           state: Byte;
       
   180                           keysym: TSDL_KeySym;
       
   181                           end;
       
   182 
       
   183      TSDL_QuitEvent = record
       
   184                       type_: Byte;
       
   185                       end;
       
   186      PSDL_Event = ^TSDL_Event;
       
   187      TSDL_Event = record
       
   188                   case Byte of
       
   189                        SDL_NOEVENT: (type_: byte);
       
   190                        SDL_KEYDOWN, SDL_KEYUP: (key: TSDL_KeyboardEvent);
       
   191                        SDL_QUITEV: (quit: TSDL_QuitEvent);
       
   192                        end;
       
   193 
       
   194      PByteArray = ^TByteArray;
       
   195      TByteArray = array[0..32767] of Byte;
       
   196 
       
   197 function  SDL_Init(flags: Longword): Integer; cdecl; external SDLLibName;
       
   198 procedure SDL_Quit; cdecl; external SDLLibName;
       
   199 
       
   200 procedure SDL_Delay(msec: Longword); cdecl; external SDLLibName;
       
   201 function  SDL_GetTicks: Longword; cdecl; external SDLLibName;
       
   202 
       
   203 function  SDL_MustLock(Surface: PSDL_Surface): Boolean;
       
   204 function  SDL_LockSurface(Surface: PSDL_Surface): Integer; cdecl; external SDLLibName;
       
   205 procedure SDL_UnlockSurface(Surface: PSDL_Surface); cdecl; external SDLLibName;
       
   206 
       
   207 function  SDL_GetError: PChar; cdecl; external SDLLibName;
       
   208 
       
   209 function  SDL_SetVideoMode(width, height, bpp: Integer; flags: Longword): PSDL_Surface; cdecl; external SDLLibName;
       
   210 function  SDL_CreateRGBSurface(flags: Longword; Width, Height, Depth: Integer; RMask, GMask, BMask, AMask: Longword): PSDL_Surface; cdecl; external SDLLibName;
       
   211 function  SDL_CreateRGBSurfaceFrom(pixels: Pointer; width, height, depth, pitch: Integer; RMask, GMask, BMask, AMask: Longword): PSDL_Surface; cdecl; external SDLLibName;
       
   212 procedure SDL_FreeSurface(Surface: PSDL_Surface); cdecl; external SDLLibName;
       
   213 function  SDL_SetColorKey(surface: PSDL_Surface; flag, key: Longword): Integer; cdecl; external SDLLibName;
       
   214 
       
   215 function  SDL_UpperBlit(src: PSDL_Surface; srcrect: PSDL_Rect; dst: PSDL_Surface; dstrect: PSDL_Rect): Integer; cdecl; external SDLLibName;
       
   216 function  SDL_FillRect(dst: PSDL_Surface; dstrect: PSDL_Rect; color: Longword): Integer; cdecl; external SDLLibName;
       
   217 procedure SDL_UpdateRect(Screen: PSDL_Surface; x, y: Integer; w, h: Longword); cdecl; external SDLLibName;
       
   218 function  SDL_Flip(Screen: PSDL_Surface): Integer; cdecl; external SDLLibName;
       
   219 
       
   220 procedure SDL_GetRGB(pixel: Longword; fmt: PSDL_PixelFormat; r, g, b: PByte); cdecl; external SDLLibName;
       
   221 function  SDL_MapRGB(format: PSDL_PixelFormat; r, g, b: Byte): Integer; cdecl; external SDLLibName;
       
   222 
       
   223 function  SDL_DisplayFormat(Surface: PSDL_Surface): PSDL_Surface; cdecl; external SDLLibName;
       
   224 
       
   225 function  SDL_RWFromFile(filename, mode: PChar): PSDL_RWops; cdecl; external SDLLibName;
       
   226 function  SDL_SaveBMP_RW(surface: PSDL_Surface; dst: PSDL_RWops; freedst: Integer): Integer; cdecl; external SDLLibName;
       
   227 
       
   228 function  SDL_GetKeyState(numkeys: PInteger): PByteArray; cdecl; external SDLLibName;
       
   229 function  SDL_GetMouseState(x, y: PInteger): Byte; cdecl; external SDLLibName;
       
   230 function  SDL_GetKeyName(key: Longword): PChar; cdecl; external SDLLibName;
       
   231 procedure SDL_WarpMouse(x, y: Word); cdecl; external SDLLibName;
       
   232 
       
   233 function  SDL_PollEvent(event: PSDL_Event): Integer; cdecl; external SDLLibName;
       
   234 
       
   235 function  SDL_ShowCursor(toggle: Integer): Integer; cdecl; external SDLLibName;
       
   236 
       
   237 procedure SDL_WM_SetCaption(title: PChar; icon: PChar); cdecl; external SDLLibName;
       
   238 
       
   239 (*  TTF  *)
       
   240 
       
   241 const {$IFDEF WIN32}
       
   242       SDL_TTFLibName = 'SDL_ttf.dll';
       
   243       {$ENDIF}
       
   244       {$IFDEF UNIX}
       
   245       SDL_TTFLibName = 'libSDL_ttf.so';
       
   246       {$ENDIF}
       
   247 
       
   248 
       
   249 type PTTF_Font = ^TTTF_font;
       
   250      TTTF_Font = record
       
   251                  end;
       
   252 
       
   253 function TTF_Init: integer; cdecl; external SDL_TTFLibName;
       
   254 procedure TTF_Quit; cdecl; external SDL_TTFLibName;
       
   255 
       
   256 
       
   257 function TTF_SizeText(font : PTTF_Font; const text: PChar; var w, h: integer): Integer; cdecl; external SDL_TTFLibName;
       
   258 function TTF_RenderText_Solid(font : PTTF_Font; const text: PChar; fg: TSDL_Color): PSDL_Surface; cdecl; external SDL_TTFLibName;
       
   259 function TTF_RenderText_Blended(font : PTTF_Font; const text: PChar; fg: TSDL_Color): PSDL_Surface; cdecl; external SDL_TTFLibName;
       
   260 function TTF_OpenFont(const filename: Pchar; size: integer): PTTF_Font; cdecl; external SDL_TTFLibName;
       
   261 
       
   262 (*  SDL_mixer *)
       
   263 
       
   264 const {$IFDEF WIN32}
       
   265       SDL_MixerLibName = 'SDL_mixer.dll';
       
   266       {$ENDIF}
       
   267       {$IFDEF UNIX}
       
   268       SDL_MixerLibName = 'libSDL_mixer.so';
       
   269       {$ENDIF}
       
   270 
       
   271 type PMixChunk = ^TMixChunk;
       
   272      TMixChunk = record
       
   273                  allocated: Longword;
       
   274                  abuf     : PByte;
       
   275                  alen     : Longword;
       
   276                  volume   : PByte;
       
   277                   end;
       
   278      TMusic = (MUS_CMD, MUS_WAV, MUS_MOD, MUS_MID, MUS_OGG, MUS_MP3);
       
   279      TMix_Fading = (MIX_NO_FADING, MIX_FADING_OUT, MIX_FADING_IN);
       
   280 
       
   281      TMidiSong = record
       
   282                samples : Integer;
       
   283                events  : pointer;
       
   284                end;
       
   285 
       
   286      TMusicUnion = record
       
   287         case Byte of
       
   288              0: ( midi : TMidiSong );
       
   289              1: ( ogg  : pointer);
       
   290              end;
       
   291 
       
   292      PMixMusic = ^TMixMusic;
       
   293      TMixMusic = record
       
   294                  type_  : TMusic;
       
   295                  data   : TMusicUnion;
       
   296                  fading : TMix_Fading;
       
   297                  fade_volume,
       
   298                  fade_step,
       
   299                  fade_steps,
       
   300                  error  : integer;
       
   301                  end;
       
   302 
       
   303 function  Mix_OpenAudio(frequency: integer; format: Word; channels: integer; chunksize: integer): integer; cdecl; external SDL_MixerLibName;
       
   304 procedure Mix_CloseAudio; cdecl; external SDL_MixerLibName;
       
   305 
       
   306 function  Mix_VolumeMusic(volume: integer): integer; cdecl; external SDL_MixerLibName;
       
   307 
       
   308 procedure Mix_FreeChunk(chunk: PMixChunk); cdecl; external SDL_MixerLibName;
       
   309 procedure Mix_FreeMusic(music: PMixMusic); cdecl; external SDL_MixerLibName;
       
   310 
       
   311 function  Mix_LoadWAV_RW(src: PSDL_RWops; freesrc: integer): PMixChunk; cdecl; external SDL_MixerLibName;
       
   312 function  Mix_LoadMUS(const filename: PChar): PMixMusic; cdecl; external SDL_MixerLibName;
       
   313 
       
   314 function  Mix_Playing(channel: integer): integer; cdecl; external SDL_MixerLibName;
       
   315 function  Mix_PlayingMusic: integer; cdecl; external SDL_MixerLibName;
       
   316 
       
   317 function  Mix_PlayChannelTimed(channel: integer; chunk: PMixChunk; loops: integer; ticks: integer): integer; cdecl; external SDL_MixerLibName;
       
   318 function  Mix_PlayMusic(music: PMixMusic; loops: integer): integer; cdecl; external SDL_MixerLibName;
       
   319 function  Mix_HaltChannel(channel: integer): integer; cdecl; external SDL_MixerLibName;
       
   320 
       
   321 (*  SDL_image *)
       
   322 
       
   323 const {$IFDEF WIN32}
       
   324       SDL_ImageLibName = 'SDL_image.dll';
       
   325       {$ENDIF}
       
   326       {$IFDEF UNIX}
       
   327       SDL_ImageLibName = 'libSDL_image.so';
       
   328       {$ENDIF}
       
   329 
       
   330 function IMG_Load(const _file: PChar): PSDL_Surface; cdecl; external SDL_ImageLibName;
       
   331 
       
   332 (*  SDL_net *)
       
   333 
       
   334 const {$IFDEF WIN32}
       
   335       SDL_NetLibName = 'SDL_net.dll';
       
   336       {$ENDIF}
       
   337       {$IFDEF UNIX}
       
   338       SDL_NetLibName = 'libSDL_net.so';
       
   339       {$ENDIF}
       
   340 
       
   341 type TIPAddress = record
       
   342                   host: Longword;
       
   343                   port: Word;
       
   344                   end;
       
   345 
       
   346      PTCPSocket = ^TTCPSocket;
       
   347      TTCPSocket = record
       
   348                   ready,
       
   349                   channel: integer;
       
   350                   remoteAddress,
       
   351                   localAddress: TIPaddress;
       
   352                   sflag: integer;
       
   353                   end;
       
   354      PSDLNet_SocketSet = ^TSDLNet_SocketSet;
       
   355      TSDLNet_SocketSet = record
       
   356                          numsockets,
       
   357                          maxsockets: integer;
       
   358                          sockets: PTCPSocket;
       
   359                          end;
       
   360 
       
   361 function SDLNet_Init: integer; cdecl; external SDL_NetLibName;
       
   362 procedure SDLNet_Quit; cdecl; external SDL_NetLibName;
       
   363 
       
   364 function SDLNet_AllocSocketSet(maxsockets: integer): PSDLNet_SocketSet; cdecl; external SDL_NetLibName;
       
   365 function SDLNet_ResolveHost(var address: TIPaddress; host: PCHar; port: Word): integer; cdecl; external SDL_NetLibName;
       
   366 function SDLNet_TCP_Accept(server: PTCPsocket): PTCPSocket; cdecl; external SDL_NetLibName;
       
   367 function SDLNet_TCP_Open(var ip: TIPaddress): PTCPSocket; cdecl; external SDL_NetLibName;
       
   368 function SDLNet_TCP_Send(sock: PTCPsocket; data: Pointer; len: integer): integer; cdecl; external SDL_NetLibName;
       
   369 function SDLNet_TCP_Recv(sock: PTCPsocket; data: Pointer; len: integer): integer; cdecl; external SDL_NetLibName;
       
   370 procedure SDLNet_TCP_Close(sock: PTCPsocket); cdecl; external SDL_NetLibName;
       
   371 procedure SDLNet_FreeSocketSet(_set: PSDLNet_SocketSet); cdecl; external SDL_NetLibName;
       
   372 function SDLNet_AddSocket(_set: PSDLNet_SocketSet; sock: PTCPSocket): integer; cdecl; external SDL_NetLibName;
       
   373 function SDLNet_CheckSockets(_set: PSDLNet_SocketSet; timeout: integer): integer; cdecl; external SDL_NetLibName;
       
   374 
       
   375 
       
   376 implementation
       
   377 
       
   378 function SDL_MustLock(Surface: PSDL_Surface): Boolean;
       
   379 begin
       
   380 Result:= ( surface^.offset <> 0 )
       
   381        or(( surface^.flags and (SDL_HWSURFACE or SDL_ASYNCBLIT or SDL_RLEACCEL)) <> 0)
       
   382 end;
       
   383 
       
   384 end.