Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we use std::fstream instead of SDL_RWops in SDL2?

Tags:

c++

fstream

sdl-2

As the title, does SDL_RWops have any advantages over std::fstream in dealing with I/O file? Can I use std::fstream instead because I am more familiar with it?

like image 519
Stoatman Avatar asked Feb 09 '23 04:02

Stoatman


2 Answers

By reading their documentation, you can find that std::fstream is an:

Input/output stream class to operate on files.

On the other side, SDL_RWops is something more:

SDL_RWops is an abstraction over I/O. It provides interfaces to read, write and seek data in a stream, without the caller needing to know where the data is coming from.

For example, a RWops might be fed by a memory buffer, or a file on disk, or a connection to a web server, without any changes to how the caller consumes the data.

Quite stronger an abstraction.

So, can you use std::fstream in place of SDL_RWops for your files? Absolutely, if you feel more confident, go with it. The latter is an useful abstraction over any sort of stream in your game, so the advantage is something beyond reading a file.

like image 107
skypjack Avatar answered Feb 10 '23 18:02

skypjack


SDL_RWops may be implemented for many types of data streams. Standard SDL provides SDL_RWFromFile and SDL_RWFromMem, while other libraries like physfs provides implementation of RWops for many of its supported archive types.

Main benefit of RWops is that all of SDL-family libraries (SDL_image, SDL_mixer, ...) supports loading from RWops so you can easily feed your own specific data source (e.g. your archive format, or maybe even network source) to them. Aside from that, it may or may not be good for your code, depending on your needs.

like image 27
keltar Avatar answered Feb 10 '23 18:02

keltar