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

自闭症网瘾萝莉.ら 提交于 2019-12-12 13:29:18

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/34553858/can-we-use-stdfstream-instead-of-sdl-rwops-in-sdl2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!