rw_ops.hpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#pragma once


#include <planet/sdl/handle.hpp>
#include <planet/asset_manager.hpp>

#include <felspar/exceptions.hpp>

#include <SDL.h>
#undef main


namespace planet::sdl {

SDL SDL_RWops wrapper

Stores a vector of bytes that can be used by SDL's file loading through its SDL_RWops abstraction.

21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
    class rw_ops_const_memory final {
        std::vector<std::byte> memory;
        SDL_RWops *prw;

      public:
        rw_ops_const_memory(
                std::vector<std::byte> m,
                felspar::source_location const &loc =
                        felspar::source_location::current())
        : memory{std::move(m)},
          prw{SDL_RWFromConstMem(memory.data(), memory.size())} {
            if (not prw) {
                throw felspar::stdexcept::runtime_error{
                        "SDL_RWFromConstMem returned nullptr", loc};
            }
        }
        rw_ops_const_memory(
                asset_manager const &am,
                char const *fn,
                felspar::source_location const &loc =
                        felspar::source_location::current())
        : rw_ops_const_memory{am.file_data(fn, loc), loc} {}
        ~rw_ops_const_memory() { SDL_RWclose(prw); }

        SDL_RWops *get() const noexcept { return prw; }
    };


}