1 (* Copyright (C) DooM 2D:Forever Developers
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // special stream classes
24 SysUtils
, Classes
, xstreams
;
28 // ïîòîê-îá¸ðòêà äëÿ SDL_RWops
29 TSFSSDLStream
= class(TStream
)
31 fRW
: PSDL_RWops
; // SDL-íàÿ ïðîêëàäêà
32 fFreeSource
: Boolean; // óáèâàòü èñõîäíèê ïðè ïîìèðàíèè?
35 constructor Create (aSrc
: PSDL_RWops
; aFreeSource
: Boolean=true);
36 destructor Destroy (); override;
38 function Read (var buffer
; count
: LongInt): LongInt; override;
39 function Write (const buffer
; count
: LongInt): LongInt; override;
40 function Seek (const offset
: Int64; origin
: TSeekOrigin
): Int64; override;
48 constructor TSFSSDLStream
.Create (aSrc
: PSDL_RWops
; aFreeSource
: Boolean=true);
51 //ASSERT(aSrc <> nil);
53 fFreeSource
:= aFreeSource
;
56 destructor TSFSSDLStream
.Destroy ();
58 if fFreeSource
and (fRW
<> nil) then SDL_FreeRW(fRW
);
62 function TSFSSDLStream
.Read (var buffer
; count
: LongInt): LongInt;
64 if (fRW
= nil) or (count
<= 0) then begin result
:= 0; exit
; end;
65 result
:= SDL_RWread(fRW
, @buffer
, 1, count
);
68 function TSFSSDLStream
.Write (const buffer
; count
: LongInt): LongInt;
70 if (fRW
= nil) or (count
<= 0) then begin result
:= 0; exit
; end;
71 result
:= SDL_RWwrite(fRW
, @buffer
, 1, count
);
74 function TSFSSDLStream
.Seek (const offset
: Int64; origin
: TSeekOrigin
): Int64;
78 if fRW
= nil then begin result
:= 0; exit
; end;
80 soBeginning
: ss
:= RW_SEEK_SET
;
81 soCurrent
: ss
:= RW_SEEK_CUR
;
82 soEnd
: ss
:= RW_SEEK_END
;
83 else raise XStreamError
.Create('invalid Seek() call');
84 // äðóãèõ íå áûâàåò. à ó êîãî áûâàåò, òîìó ÿ íå äîêòîð.
86 result
:= SDL_RWseek(fRW
, offset
, ss
);
87 if result
= -1 then raise XStreamError
.Create('Seek() error');