ge211  2021.5.1
A student game engine
ge211_resource.cxx
1 #include "ge211_resource.hxx"
2 #include "ge211_error.hxx"
3 #include "ge211_session.hxx"
4 
5 #include <SDL.h>
6 #include <SDL_ttf.h>
7 
8 #include <ios>
9 #include <string>
10 
11 namespace ge211 {
12 
13 using namespace detail;
14 
15 namespace {
16 
17 static const char* search_prefixes[] = {
18  GE211_RESOURCES,
19  "Resources/",
20  "../Resources/",
21 };
22 
23 template <class OPENER>
24 typename OPENER::result_t
25 open_resource_(std::string const& filename)
26 {
27  std::string path;
28 
29  for (auto prefix : search_prefixes) {
30  path.clear();
31  path += prefix;
32  path += filename;
33 
34  auto result = OPENER::open(path);
35  if (result) return result;
36  }
37 
38  return OPENER::fail(filename);
39 }
40 
41 } // end anonymous namespace
42 
43 namespace detail {
44 
45 template <bool BinaryMode>
46 struct ifstream_opener
47 {
48  using result_t = std::ifstream;
49 
50  static constexpr std::ios_base::openmode
51  mode = BinaryMode
52  ? std::ios_base::in | std::ios_base::binary
53  : std::ios_base::in;
54 
55  static result_t open(std::string const& path)
56  {
57  return result_t(path, mode);
58  }
59 
60  static result_t fail(std::string const& filename)
61  {
62  throw ge211::File_error::could_not_open(filename);
63  }
64 };
65 
66 } // end namespace detail
67 
70 {
71  return open_resource_<ifstream_opener<false>>(filename);
72 }
73 
76 {
77  return open_resource_<ifstream_opener<true>>(filename);
78 }
79 
80 namespace detail {
81 
82 static Owned<SDL_RWops> open_rwops_(const std::string& filename)
83 {
84  struct Opener
85  {
86  using result_t = Owned<SDL_RWops>;
87 
88  static result_t open(std::string const& path)
89  {
90  return SDL_RWFromFile(path.c_str(), "rb");
91  }
92 
93  static result_t fail(std::string const&)
94  {
95  return nullptr;
96  }
97  };
98 
99  return open_resource_<Opener>(filename);
100 }
101 
102 File_resource::File_resource(const std::string& filename)
103  : ptr_(open_rwops_(filename))
104 {
105  if (!ptr_)
106  throw File_error::could_not_open(filename);
107 }
108 
109 void File_resource::close_rwops_(Owned<SDL_RWops> ptr)
110 {
111  SDL_RWclose(ptr);
112 }
113 
114 } // end namespace detail
115 
116 static Owned<TTF_Font> open_ttf_(const std::string& filename, int size)
117 {
118  return TTF_OpenFontRW(File_resource(filename).release(), 1, size);
119 }
120 
121 Font::Font(const std::string& filename, int size)
122  : ptr_(open_ttf_(filename, size))
123 {
124  Session::check_session("Font loading");
125 
126  if (!ptr_)
127  throw Font_error::could_not_load(filename);
128 }
129 
130 }
std::string
ge211::open_binary_resource_file
std::ifstream open_binary_resource_file(std::string const &filename)
Opens a file in the Resources/ directory for input in binary mode.
Definition: ge211_resource.cxx:75
std::string::clear
T clear(T... args)
ge211
The game engine namespace.
Definition: ge211.hxx:4
ge211::open_resource_file
std::ifstream open_resource_file(std::string const &filename)
Opens a file in the Resources/ directory for input in text mode.
Definition: ge211_resource.cxx:69
std::string::c_str
T c_str(T... args)
std::ifstream