Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 575495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:56:48+00:00 2026-05-13T13:56:48+00:00

I’m quite surprised that Google didn’t find a solution. I’m searching for a solution

  • 0

I’m quite surprised that Google didn’t find a solution. I’m searching for a solution that allows SDL_RWops to be used with std::istream. SDL_RWops is the alternative mechanism for reading/writing data in SDL.

Any links to sites that tackle the problem?

An obvious solution would be to pre-read enough data to memory and then use SDL_RWFromMem. However, that has the downside that I’d need to know the filesize beforehand.

Seems like the problem could somehow be solved by “overriding” SDL_RWops functions…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:56:48+00:00Added an answer on May 13, 2026 at 1:56 pm

    I feel bad answering my own question, but it preocupied me for some time, and this is the solution I came up with:

    int istream_seek( struct SDL_RWops *context, int offset, int whence)
    {
        std::istream* stream = (std::istream*) context->hidden.unknown.data1;
    
             if ( whence == SEEK_SET )
            stream->seekg ( offset, std::ios::beg );
        else if ( whence == SEEK_CUR )
            stream->seekg ( offset, std::ios::cur );
        else if ( whence == SEEK_END )
             stream->seekg ( offset, std::ios::end );
    
        return stream->fail() ? -1 : stream->tellg();
    }
    
    
    int istream_read(SDL_RWops *context, void *ptr, int size, int maxnum)
    {
        if ( size == 0 ) return -1;
        std::istream* stream = (std::istream*) context->hidden.unknown.data1;
        stream->read( (char*)ptr, size * maxnum );
    
        return stream->bad() ? -1 : stream->gcount() / size;
    }
    
    int istream_close( SDL_RWops *context )
    {
        if ( context ) {
            SDL_FreeRW( context );
        }
        return 0;
    }
    
    
    SDL_RWops *SDL_RWFromIStream( std::istream& stream )
    {
        SDL_RWops *rwops;
        rwops = SDL_AllocRW();
    
        if ( rwops != NULL ) 
        {
            rwops->seek = istream_seek;
            rwops->read = istream_read;
            rwops->write = NULL;
            rwops->close = istream_close;
            rwops->hidden.unknown.data1 = &stream;
        }
        return rwops;
    }
    

    Works under the assumptions that istream’s are never freed by SDL (and that they live through the operation). Also only istream support is in, a separate function would be done for ostream — I know I could pass iostream, but that would not allow passing an istream to the conversion function :/.

    Any tips on errors or upgrades welcome.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a simple page using Google Maps API 3. My first. One marker
I have a bunch of posts stored in text files formatted in yaml/textile (from
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.