sdlstreams.pas 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. unit sdlstreams;
  2. {
  3. $Id: sdlstreams.pas,v 1.1 2004/02/05 00:08:20 savage Exp $
  4. }
  5. {******************************************************************}
  6. { }
  7. { SDL - Simple DirectMedia Layer }
  8. { Copyright (C) 1997, 1998, 1999, 2000, 2001 Sam Lantinga }
  9. { }
  10. { Portions created by Chris Bruner are }
  11. { Copyright (C) 2002 Chris Bruner. }
  12. { }
  13. { Contributor(s) }
  14. { -------------- }
  15. { }
  16. { }
  17. { Obtained through: }
  18. { Joint Endeavour of Delphi Innovators ( Project JEDI ) }
  19. { }
  20. { You may retrieve the latest version of this file at the Project }
  21. { JEDI home page, located at http://delphi-jedi.org }
  22. { }
  23. { The contents of this file are used with permission, subject to }
  24. { the Mozilla Public License Version 1.1 (the "License"); you may }
  25. { not use this file except in compliance with the License. You may }
  26. { obtain a copy of the License at }
  27. { http://www.mozilla.org/NPL/NPL-1_1Final.html }
  28. { }
  29. { Software distributed under the License is distributed on an }
  30. { "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or }
  31. { implied. See the License for the specific language governing }
  32. { rights and limitations under the License. }
  33. { }
  34. { Description }
  35. { ----------- }
  36. { Shows how to use OpenGL to do 2D and 3D with the SDL libraries }
  37. { }
  38. { }
  39. { Requires }
  40. { -------- }
  41. { SDL runtime libary somewhere in your path }
  42. { The Latest SDL runtime can be found on http://www.libsdl.org }
  43. { }
  44. { Programming Notes }
  45. { ----------------- }
  46. { }
  47. { }
  48. { }
  49. { }
  50. { }
  51. { Revision History }
  52. { ---------------- }
  53. { January 11 2002 - CB : Software embraced and extended by }
  54. { Chris Bruner of Crystal Software }
  55. { (Canada) Inc. }
  56. { }
  57. { February 11 2002 - DL : Added FreePascal support as suggested }
  58. { by "QuePasha Pepe" <[email protected]> }
  59. { }
  60. {******************************************************************}
  61. {
  62. $Log: sdlstreams.pas,v $
  63. Revision 1.1 2004/02/05 00:08:20 savage
  64. Module 1.0 release
  65. }
  66. {$i jedi-sdl.inc}
  67. interface
  68. uses
  69. Classes,
  70. SysUtils,
  71. sdl,
  72. sdlutils;
  73. {$IFDEF FPC}
  74. type
  75. EinvalidContainer=class(Exception);
  76. {$ENDIF}
  77. function LoadSDLBMPFromStream( Stream : TStream ) : PSDL_Surface;
  78. procedure SaveSDLBMPToStream( SDL_Surface : PSDL_Surface; stream : TStream );
  79. function SDL_Swap16( D : UInt16 ) : Uint16;
  80. function SDL_Swap32( D : UInt32 ) : Uint32;
  81. function SDLStreamSetup( stream : TStream ) : PSDL_RWops;
  82. // this only closes the SDL_RWops part of the stream, not the stream itself
  83. procedure SDLStreamCloseRWops( SDL_RWops : PSDL_RWops );
  84. implementation
  85. function SDL_Swap16( D : UInt16 ) : Uint16;
  86. begin
  87. Result := ( D shl 8 ) or ( D shr 8 );
  88. end;
  89. function SDL_Swap32( D : UInt32 ) : Uint32;
  90. begin
  91. Result := ( ( D shl 24 ) or ( ( D shl 8 ) and $00FF0000 ) or ( ( D shr 8 ) and $0000FF00 ) or ( D shr 24 ) );
  92. end;
  93. (*function SDL_Swap64(D : UInt64) : Uint64;
  94. var hi,lo : Uint32;
  95. begin
  96. // Separate into high and low 32-bit resultues and swap them
  97. lo := Uint32(D and $0FFFFFFFF); // bloody pascal is too tight in it's type checking!
  98. D := D shr 32;
  99. hi = Uint32((D and $FFFFFFFF));
  100. result = SDL_Swap32(lo);
  101. result := result shl 32;
  102. result := result or SDL_Swap32(hi);
  103. end;
  104. *)
  105. function SdlStreamSeek( context : PSDL_RWops; offset : Integer; whence : Integer ) : integer; cdecl;
  106. var
  107. stream : TStream;
  108. origin : Word;
  109. begin
  110. stream := TStream( context.unknown );
  111. if ( stream = nil ) then
  112. raise EInvalidContainer.Create( 'SDLStreamSeek on nil' );
  113. case whence of
  114. 0 : origin := soFromBeginning; // Offset is from the beginning of the resource. Seek moves to the position Offset. Offset must be >= 0.
  115. 1 : origin := soFromCurrent; // Offset is from the current position in the resource. Seek moves to Position + Offset.
  116. 2 : origin := soFromEnd;
  117. else
  118. origin := soFromBeginning; // just in case
  119. end;
  120. Result := stream.Seek( offset, origin );
  121. end;
  122. function SDLStreamWrite( context : PSDL_RWops; Ptr : Pointer;
  123. size : Integer; num : Integer ) : Integer; cdecl;
  124. var
  125. stream : TStream;
  126. begin
  127. stream := TStream( context.unknown );
  128. if ( stream = nil ) then
  129. raise EInvalidContainer.Create( 'SDLStreamWrite on nil' );
  130. try
  131. Result := stream.Write( Ptr^, Size * num ) div size;
  132. except
  133. Result := -1;
  134. end;
  135. end;
  136. function SdlStreamRead( context : PSDL_RWops; Ptr : Pointer; size : Integer; maxnum
  137. : Integer ) : Integer; cdecl;
  138. var
  139. stream : TStream;
  140. begin
  141. stream := TStream( context.unknown );
  142. if ( stream = nil ) then
  143. raise EInvalidContainer.Create( 'SDLStreamRead on nil' );
  144. try
  145. Result := stream.read( Ptr^, Size * maxnum ) div size;
  146. except
  147. Result := -1;
  148. end;
  149. end;
  150. function SDLStreamClose( context : PSDL_RWops ) : Integer; cdecl;
  151. var
  152. stream : TStream;
  153. begin
  154. stream := TStream( context.unknown );
  155. if ( stream = nil ) then
  156. raise EInvalidContainer.Create( 'SDLStreamClose on nil' );
  157. stream.Free;
  158. Result := 1;
  159. end;
  160. function SDLStreamSetup( stream : TStream ) : PSDL_RWops;
  161. begin
  162. result := SDL_AllocRW;
  163. if ( result = nil ) then
  164. raise EInvalidContainer.Create( 'could not create SDLStream on nil' );
  165. result.unknown := TUnknown( stream );
  166. result.seek := SDLStreamSeek;
  167. result.read := SDLStreamRead;
  168. result.write := SDLStreamWrite;
  169. result.close := SDLStreamClose;
  170. Result.type_ := 2; // TUnknown
  171. end;
  172. // this only closes the SDL part of the stream, not the context
  173. procedure SDLStreamCloseRWops( SDL_RWops : PSDL_RWops );
  174. begin
  175. SDL_FreeRW( SDL_RWops );
  176. end;
  177. function LoadSDLBMPFromStream( stream : TStream ) : PSDL_Surface;
  178. var
  179. SDL_RWops : PSDL_RWops;
  180. begin
  181. SDL_RWops := SDLStreamSetup( stream );
  182. result := SDL_LoadBMP_RW( SDL_RWops, 0 );
  183. SDLStreamCloseRWops( SDL_RWops );
  184. end;
  185. procedure SaveSDLBMPToStream( SDL_Surface : PSDL_Surface; stream : TStream );
  186. var
  187. SDL_RWops : PSDL_RWops;
  188. begin
  189. SDL_RWops := SDLStreamSetup( stream );
  190. SDL_SaveBMP_RW( SDL_Surface, SDL_RWops, 0 );
  191. SDLStreamCloseRWops( SDL_RWops );
  192. end;
  193. end.