2017-04-04 17:28:01 +00:00
|
|
|
/** @addtogroup Extensions
|
2016-04-27 00:08:01 +00:00
|
|
|
*
|
2017-04-06 18:14:52 +00:00
|
|
|
* @brief Various extensions including integration of libostd with other libs.
|
2017-04-04 17:28:01 +00:00
|
|
|
*
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @file sdl_rwops.hh
|
|
|
|
*
|
2017-04-06 18:14:52 +00:00
|
|
|
* @brief Integration of libostd streams with SDL RWops.
|
2017-04-04 17:28:01 +00:00
|
|
|
*
|
2017-04-06 18:14:52 +00:00
|
|
|
* This provides integration of libostd streams with SDL RWops so that
|
2017-04-04 17:28:01 +00:00
|
|
|
* various APIs that provide a generic RWops interface to deal with
|
2017-04-06 18:14:52 +00:00
|
|
|
* files can use libostd streams.
|
2017-04-04 17:28:01 +00:00
|
|
|
*
|
|
|
|
* Supports both SDL1 and SDL2, with SDL2 being default. If you want to
|
|
|
|
* use SDL1 compatibility, define `OSTD_EXT_SDL_USE_SDL1` at build time
|
|
|
|
* or before including this header.
|
|
|
|
*
|
|
|
|
* @copyright See COPYING.md in the project tree for further information.
|
2016-04-27 00:08:01 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef OSTD_EXT_SDL_RWOPS_HH
|
|
|
|
#define OSTD_EXT_SDL_RWOPS_HH
|
|
|
|
|
|
|
|
#ifdef OSTD_EXT_SDL_USE_SDL1
|
|
|
|
#include <SDL/SDL.h>
|
|
|
|
#else
|
|
|
|
#include <SDL2/SDL.h>
|
|
|
|
#endif
|
|
|
|
|
2017-06-19 14:59:36 +00:00
|
|
|
#include <ostd/stream.hh>
|
2016-04-27 00:08:01 +00:00
|
|
|
|
|
|
|
namespace ostd {
|
|
|
|
namespace sdl {
|
|
|
|
|
2017-04-04 17:28:01 +00:00
|
|
|
/** @addtogroup Extensions
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2017-04-06 18:14:52 +00:00
|
|
|
/** @brief Create an `SDL_RWops` using an libostd stream.
|
2017-04-04 17:28:01 +00:00
|
|
|
*
|
|
|
|
* The resulting RWops object is created using `SDL_AllocRW()`.
|
|
|
|
*
|
|
|
|
* The `size`, `seek`, `read`, `write` and `close` callbacks are set up,
|
|
|
|
* but `close` will not actually close the stream, as the RWops object
|
|
|
|
* does not take ownership.
|
|
|
|
*
|
|
|
|
* @returns The RWops object or `nullptr` on failure.
|
|
|
|
*/
|
|
|
|
inline SDL_RWops *stream_to_rwops(stream &s) noexcept {
|
2016-04-27 00:17:26 +00:00
|
|
|
#ifdef OSTD_EXT_SDL_USE_SDL1
|
2017-04-04 17:28:01 +00:00
|
|
|
using sdl_rwops_off_t = int;
|
2016-04-27 00:17:26 +00:00
|
|
|
#else
|
2017-04-04 17:28:01 +00:00
|
|
|
using sdl_rwops_off_t = int64_t;
|
2016-04-27 00:17:26 +00:00
|
|
|
#endif
|
|
|
|
|
2016-04-27 00:08:01 +00:00
|
|
|
SDL_RWops *rwr = SDL_AllocRW();
|
|
|
|
if (!rwr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-07-31 19:40:25 +00:00
|
|
|
|
2016-04-27 00:08:01 +00:00
|
|
|
rwr->hidden.unknown.data1 = &s;
|
2016-07-31 19:40:25 +00:00
|
|
|
|
2017-04-03 23:06:55 +00:00
|
|
|
rwr->size = [](SDL_RWops *rw) -> sdl_rwops_off_t {
|
|
|
|
auto &is = *static_cast<stream *>(rw->hidden.unknown.data1);
|
|
|
|
try {
|
|
|
|
return static_cast<sdl_rwops_off_t>(is.size());
|
|
|
|
} catch (stream_error const &) {
|
|
|
|
return -1;
|
|
|
|
}
|
2016-04-27 00:17:26 +00:00
|
|
|
};
|
2016-07-31 19:40:25 +00:00
|
|
|
|
2017-04-03 23:06:55 +00:00
|
|
|
rwr->seek = [](SDL_RWops *rw, sdl_rwops_off_t pos, int whence) ->
|
|
|
|
sdl_rwops_off_t
|
2016-07-31 19:40:25 +00:00
|
|
|
{
|
2017-04-03 23:06:55 +00:00
|
|
|
auto &is = *static_cast<stream *>(rw->hidden.unknown.data1);
|
|
|
|
try {
|
|
|
|
if (!pos && whence == SEEK_CUR) {
|
2017-04-04 17:28:01 +00:00
|
|
|
return static_cast<sdl_rwops_off_t>(is.tell());
|
2017-04-03 23:06:55 +00:00
|
|
|
}
|
|
|
|
if (is->seek((stream_off_t(pos), stream_seek(whence)))) {
|
|
|
|
return static_cast<sdl_rwops_off_t>(is.tell());
|
|
|
|
}
|
|
|
|
} catch (stream_error const &) {
|
|
|
|
return -1;
|
2016-07-31 19:40:25 +00:00
|
|
|
}
|
2016-04-27 00:08:01 +00:00
|
|
|
return -1;
|
2016-04-27 00:17:26 +00:00
|
|
|
};
|
2016-07-31 19:40:25 +00:00
|
|
|
|
2017-04-03 23:06:55 +00:00
|
|
|
rwr->read = [](SDL_RWops *rw, void *buf, size_t size, size_t nb) ->
|
|
|
|
size_t
|
|
|
|
{
|
|
|
|
auto &is = *static_cast<stream *>(rw->hidden.unknown.data1);
|
|
|
|
try {
|
|
|
|
return is.read_bytes(buf, size * nb) / size;
|
|
|
|
} catch (stream_error const &) {
|
|
|
|
return 0;
|
|
|
|
}
|
2016-04-27 00:17:26 +00:00
|
|
|
};
|
2016-07-31 19:40:25 +00:00
|
|
|
|
2017-04-03 23:06:55 +00:00
|
|
|
rwr->write = [](SDL_RWops *rw, void const *buf, size_t size, size_t nb) ->
|
|
|
|
size_t
|
|
|
|
{
|
|
|
|
stream &is = *static_cast<stream *>(rw->hidden.unknown.data1);
|
|
|
|
try {
|
|
|
|
return is->write_bytes(buf, size * nb) / size;
|
|
|
|
} catch (stream_error const &) {
|
|
|
|
return 0;
|
|
|
|
}
|
2016-04-27 00:17:26 +00:00
|
|
|
};
|
2016-07-31 19:40:25 +00:00
|
|
|
|
|
|
|
rwr->close = [](SDL_RWops *) -> int {
|
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
2016-04-27 00:10:16 +00:00
|
|
|
return rwr;
|
2016-04-27 00:08:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-04 17:28:01 +00:00
|
|
|
/** @} */
|
|
|
|
|
2016-04-27 00:08:01 +00:00
|
|
|
#endif
|
2017-04-04 17:28:01 +00:00
|
|
|
|
|
|
|
/** @} */
|