Wrap SDL_RWops* in a unique_ptr

This commit is contained in:
Alexander van Gessel 2017-09-16 11:36:19 +02:00 committed by Jyrki Vesterinen
parent 958b4ce676
commit af1e8f17d8
5 changed files with 19 additions and 16 deletions

View file

@ -37,7 +37,9 @@ namespace filesystem {
using scoped_istream = std::unique_ptr<std::istream>;
using scoped_ostream = std::unique_ptr<std::ostream>;
SDL_RWops* load_RWops(const std::string &path);
typedef std::unique_ptr<SDL_RWops, void(*)(SDL_RWops*)> rwops_ptr;
rwops_ptr load_RWops(const std::string &path);
/** An exception object used when an IO error occurs */
struct io_exception : public game::error {

View file

@ -28,8 +28,8 @@ static size_t SDLCALL ifs_read(struct SDL_RWops *context, void *ptr, size_t size
static size_t SDLCALL ifs_write(struct SDL_RWops *context, const void *ptr, size_t size, size_t num);
static int SDLCALL ifs_close(struct SDL_RWops *context);
SDL_RWops* load_RWops(const std::string &path) {
std::unique_ptr<SDL_RWops, decltype(&SDL_FreeRW)> rw(SDL_AllocRW(), &SDL_FreeRW);
rwops_ptr load_RWops(const std::string &path) {
rwops_ptr rw(SDL_AllocRW(), &SDL_FreeRW);
rw->size = &ifs_size;
rw->seek = &ifs_seek;
@ -42,12 +42,13 @@ SDL_RWops* load_RWops(const std::string &path) {
scoped_istream ifs = istream_file(path);
if(!ifs) {
ERR_FS << "load_RWops: istream_file returned NULL on " << path << '\n';
return nullptr;
rw.reset();
return rw;
}
rw->hidden.unknown.data1 = ifs.release();
return rw.release();
return rw;
}

View file

@ -184,8 +184,8 @@ static TTF_Font* open_font_impl(const std::string & fname, int size) {
}
}
SDL_RWops *rwops = filesystem::load_RWops(name);
TTF_Font* font = TTF_OpenFontRW(rwops, true, size); // SDL takes ownership of rwops
filesystem::rwops_ptr rwops = filesystem::load_RWops(name);
TTF_Font* font = TTF_OpenFontRW(rwops.release(), true, size); // SDL takes ownership of rwops
if(font == nullptr) {
ERR_FT << "Failed opening font: '" << fname << "'\n";
ERR_FT << "TTF_OpenFont: " << TTF_GetError() << std::endl;

View file

@ -513,8 +513,8 @@ static std::string get_localized_path(const std::string& file, const std::string
// Load overlay image and compose it with the original surface.
static void add_localized_overlay(const std::string& ovr_file, surface& orig_surf)
{
SDL_RWops* rwops = filesystem::load_RWops(ovr_file);
surface ovr_surf = IMG_Load_RW(rwops, true); // SDL takes ownership of rwops
filesystem::rwops_ptr rwops = filesystem::load_RWops(ovr_file);
surface ovr_surf = IMG_Load_RW(rwops.release(), true); // SDL takes ownership of rwops
if(ovr_surf.null()) {
return;
}
@ -537,8 +537,8 @@ static surface load_image_file(const image::locator& loc)
if(!loc_location.empty()) {
location = loc_location;
}
SDL_RWops* rwops = filesystem::load_RWops(location);
res = IMG_Load_RW(rwops, true); // SDL takes ownership of rwops
filesystem::rwops_ptr rwops = filesystem::load_RWops(location);
res = IMG_Load_RW(rwops.release(), true); // SDL takes ownership of rwops
// If there was no standalone localized image, check if there is an overlay.
if(!res.null() && loc_location.empty()) {
const std::string ovr_location = get_localized_path(location, "--overlay");
@ -1217,7 +1217,7 @@ bool save_image(const surface& surf, const std::string& filename)
LOG_DP << "Writing a png image to " << filename << std::endl;
surface tmp = SDL_PNGFormatAlpha(surf.get());
// SDL_SavePNG_RW(tmp, filesystem::load_RWops(filename), 1); //1 means to close the file (RWops) when we finish
// SDL_SavePNG_RW(tmp, filesystem::load_RWops(filename).release(), 1); //1 means to close the file (RWops) when we finish
//^ This doesn't work, load_RWops is only for reading not writing
return SDL_SavePNG(tmp, filename.c_str()) == 0;
}

View file

@ -601,9 +601,9 @@ static void play_new_music()
if(itor == music_cache.end()) {
LOG_AUDIO << "attempting to insert track '" << filename << "' into cache\n";
SDL_RWops* rwops = filesystem::load_RWops(filename);
filesystem::rwops_ptr rwops = filesystem::load_RWops(filename);
// SDL takes ownership of rwops
const std::shared_ptr<Mix_Music> music(Mix_LoadMUSType_RW(rwops, MUS_NONE, true), &Mix_FreeMusic);
const std::shared_ptr<Mix_Music> music(Mix_LoadMUSType_RW(rwops.release(), MUS_NONE, true), &Mix_FreeMusic);
if(music == nullptr) {
ERR_AUDIO << "Could not load music file '" << filename << "': " << Mix_GetError() << "\n";
@ -882,8 +882,8 @@ static Mix_Chunk* load_chunk(const std::string& file, channel_group group)
const std::string& filename = filesystem::get_binary_file_location("sounds", file);
if(!filename.empty()) {
SDL_RWops* rwops = filesystem::load_RWops(filename);
temp_chunk.set_data(Mix_LoadWAV_RW(rwops, true)); // SDL takes ownership of rwops
filesystem::rwops_ptr rwops = filesystem::load_RWops(filename);
temp_chunk.set_data(Mix_LoadWAV_RW(rwops.release(), true)); // SDL takes ownership of rwops
} else {
ERR_AUDIO << "Could not load sound file '" << file << "'." << std::endl;
throw chunk_load_exception();