1 #include "ge211_resource.hxx"
2 #include "ge211_error.hxx"
3 #include "ge211_session.hxx"
13 using namespace detail;
17 static const char* search_prefixes[] = {
23 template <
class OPENER>
24 typename OPENER::result_t
29 for (
auto prefix : search_prefixes) {
34 auto result = OPENER::open(path);
35 if (result)
return result;
38 return OPENER::fail(filename);
45 template <
bool BinaryMode>
46 struct ifstream_opener
50 static constexpr std::ios_base::openmode
52 ? std::ios_base::in | std::ios_base::binary
57 return result_t(path, mode);
62 throw ge211::File_error::could_not_open(filename);
71 return open_resource_<ifstream_opener<false>>(filename);
77 return open_resource_<ifstream_opener<true>>(filename);
82 static Owned<SDL_RWops> open_rwops_(
const std::string& filename)
86 using result_t = Owned<SDL_RWops>;
90 return SDL_RWFromFile(path.
c_str(),
"rb");
99 return open_resource_<Opener>(filename);
102 File_resource::File_resource(
const std::string& filename)
103 : ptr_(open_rwops_(filename))
106 throw File_error::could_not_open(filename);
109 void File_resource::close_rwops_(Owned<SDL_RWops> ptr)
116 static Owned<TTF_Font> open_ttf_(
const std::string& filename,
int size)
118 return TTF_OpenFontRW(File_resource(filename).release(), 1, size);
122 : ptr_(open_ttf_(filename, size))
124 Session::check_session(
"Font loading");
127 throw Font_error::could_not_load(filename);