Results 1 to 6 of 6

Thread: How do you use IMG_Load_RW...

  1. #1

    How do you use IMG_Load_RW...

    Hi all,

    I have data stored in a memory stream, i am trying to use SDL_Image to load the image.

    I currently have the following code:

    Code:
    var
      SDLStream: TSDL_RWops;
    
      SDLStream := StreamToRWops(_XPacks.Items[Index].GetStream(Filename).Stream);
      Surface := IMG_Load_RW(@SDLStream, 1);
      If Surface = Nil Then Exit;
    Code:
    function Seek(Context: PSDL_RWops; Offset: Integer; Whence: Integer): Integer;
    var
      Stream: TStream;
    begin
      Stream := TStream(Context^.unknown.data1);
      If Stream = Nil Then
      Begin
        Result := -1;
        Exit;
      End;
    
      Case Whence Of
           SEEK_SET: Stream.Position := Offset; 
           SEEK_CUR: Stream.Position := Stream.Position+Offset;
           SEEK_END: Stream.Position := Stream.Size+Offset;
      End;
      Result := Stream.Position; 
    end;
     
    function Read(Context: PSDL_RWops; Ptr: Pointer; Size: Integer; Maxnum: Integer ): Integer;
    var
      Stream: TStream;
    begin
      Stream := TStream(Context^.unknown.data1);
      If Stream = Nil Then
      Begin
        Result := -1;
        Exit;
      End;
      Result := Stream.Read(Ptr^, Size*Maxnum) Div Size;
    end;
    
    function Write(Context: PSDL_RWops; Ptr: Pointer; Size: Integer; Num: Integer ): Integer;
    var
      Stream: TStream;
    begin
      Stream := TStream(Context^.unknown.data1);
      If Stream = Nil Then
      Begin
        Result := -1;
        Exit;
      End;
      Result := Stream.Write(Ptr^, Size*Num) Div Size; 
    end;
    
    function Close(Context: PSDL_RWops): Integer;
    var
      Stream: TStream;
    begin
      Stream := TStream(Context^.unknown.data1); 
      if Stream = nil then
      begin
        Result := -1;
        Exit;
      end;
      Stream.Destroy; 
      context^.unknown.data1 := nil; 
      Result := 0;
    end;
     
    function StreamToRWops(Stream: TStream): TSDL_RWops;
    begin
      Result.type_ := 2;
      Result.unknown.data1 := Stream; 
      Result.seek := @Seek;
      Result.read := @Read;
      Result.write := @Write;
      Result.close := @Close;
    end;
    
    procedure RWopsToStream(RW: PSDL_RWops; Stream: TStream);
    var
      Buf: Pointer; 
      Read: Integer; 
    begin
      If Stream = Nil Then Exit; 
      If RW = Nil Then Exit;
      GetMem(Buf, 1024);
      Try
        Read := RW^.Read(RW, Buf, 1, 1024); 
        While Read > 0 Do
        Begin
          Stream.Write(Buf^, Read);
          Read := RW^.Read(RW, Buf, 1, 1024);
        End;
      Finally
        FreeMem(Buf);
      End;
    end;
    Everytime i get to the stage of loading the data to IMG_Load_RW i get an access violation.

    Does anyone know if im i missing something?

    Many Thanks
    M109uk
    <br />--------------------------------------------------------
    <br />www.pulse-soft.oneuk.com

  2. #2

    How do you use IMG_Load_RW...

    If it helps you, this is my code I use for this sort of thing:

    Code:
    Procedure TSpriteSource.LoadFromMem&#40;mem&#58; Pointer; size&#58; Integer&#41;;
    Var
        Surface &#58; PSDL_Surface;
        rw      &#58; PSDL_RWops;
    Begin
        If &#40;mem = Nil&#41; Or &#40;size <= 0&#41; Then Exit;
        rw &#58;= SDL_RWFromMem&#40;mem,size&#41;;
        If rw = Nil Then Exit;
        Surface &#58;= SDL_LoadBMP_RW&#40;rw,0&#41;;
        SDL_FreeRW&#40;rw&#41;;
        If Surface = Nil Then Exit;
        If FSurface <> Nil Then SDL_FreeSurface&#40;FSurface&#41;;
        FSurface &#58;= Surface;
        <SNIP>
    End;
    Of source as you are using SDL_Image, you would replace the SDL_LoadBMP_RW with IMG_Load_RW.
    In your case I am not sure if you should be freeing the memory with the 1 parameter though.

    cheers,
    Paul.

  3. #3

    How do you use IMG_Load_RW...

    Thanks paul_nicholls,

    Your code was a great help, just had to load the stream data into a pointer and it all seems to work great
    M109uk
    <br />--------------------------------------------------------
    <br />www.pulse-soft.oneuk.com

  4. #4

    How do you use IMG_Load_RW...

    Is there a problem with loading TGAs this way?

    All my textures work except for any that are in the tga format.

    Thanks

    Edit:
    Nevermind, it works ok if i use IMG_LoadTGA_RW..
    M109uk
    <br />--------------------------------------------------------
    <br />www.pulse-soft.oneuk.com

  5. #5

    How do you use IMG_Load_RW...

    JEDI-SDL v1.0 (from cvs) ships with an sdlstreams.pas unit. Which takes care of doing all the stream stuff for you.

    I use this succesfully with not only Img_Load_RW but also TTF_Load_RW for loading fonts. It works really well.
    <A HREF="http://www.myhpf.co.uk/banner.asp?friend=139328">
    <br /><IMG SRC="http://www.myhpf.co.uk/banners/60x468.gif" BORDER="0">
    <br /></A>

  6. #6

    How do you use IMG_Load_RW...

    Quote Originally Posted by M109uk
    Thanks paul_nicholls,

    Your code was a great help, just had to load the stream data into a pointer and it all seems to work great
    Glad to help!
    Just giving something back to the forum :-)
    cheers,
    Paul.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •