DEADSOFTWARE

added license info
[d2df-sdl.git] / src / sheditor / xstreams_sdl.pas
1 (* Copyright (C) DooM 2D:Forever Developers
2 *
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.
7 *
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.
12 *
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/>.
15 *)
16 // special stream classes
17 {$MODE DELPHI}
18 {$R+}
19 unit xstreams_sdl;
21 interface
23 uses
24 SysUtils, Classes, xstreams;
27 type
28 // ïîòîê-îá¸ðòêà äëÿ SDL_RWops
29 TSFSSDLStream = class(TStream)
30 protected
31 fRW: PSDL_RWops; // SDL-íàÿ ïðîêëàäêà
32 fFreeSource: Boolean; // óáèâàòü èñõîäíèê ïðè ïîìèðàíèè?
34 public
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;
41 end;
44 implementation
47 { TSFSSDLStream }
48 constructor TSFSSDLStream.Create (aSrc: PSDL_RWops; aFreeSource: Boolean=true);
49 begin
50 inherited Create();
51 //ASSERT(aSrc <> nil);
52 fRW := aSrc;
53 fFreeSource := aFreeSource;
54 end;
56 destructor TSFSSDLStream.Destroy ();
57 begin
58 if fFreeSource and (fRW <> nil) then SDL_FreeRW(fRW);
59 inherited Destroy();
60 end;
62 function TSFSSDLStream.Read (var buffer; count: LongInt): LongInt;
63 begin
64 if (fRW = nil) or (count <= 0) then begin result := 0; exit; end;
65 result := SDL_RWread(fRW, @buffer, 1, count);
66 end;
68 function TSFSSDLStream.Write (const buffer; count: LongInt): LongInt;
69 begin
70 if (fRW = nil) or (count <= 0) then begin result := 0; exit; end;
71 result := SDL_RWwrite(fRW, @buffer, 1, count);
72 end;
74 function TSFSSDLStream.Seek (const offset: Int64; origin: TSeekOrigin): Int64;
75 var
76 ss: Integer;
77 begin
78 if fRW = nil then begin result := 0; exit; end;
79 case origin of
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 // äðóãèõ íå áûâàåò. à ó êîãî áûâàåò, òîìó ÿ íå äîêòîð.
85 end;
86 result := SDL_RWseek(fRW, offset, ss);
87 if result = -1 then raise XStreamError.Create('Seek() error');
88 end;
91 end.