ge211
ge211_resource.cpp
1 #include "ge211_resource.h"
2 #include "ge211_error.h"
3 
4 #include <SDL.h>
5 #include <SDL_ttf.h>
6 
7 #include <string>
8 
9 namespace ge211 {
10 
11 using namespace detail;
12 
13 static const char* search_prefixes[] = {
14  "Resources/",
15  "../Resources/",
16  GE211_RESOURCES
17 };
18 
19 namespace detail {
20 
21 std::vector<const char*> get_search_prefixes()
22 {
23  using namespace std;
24  return vector<const char*>(begin(search_prefixes),
25  end(search_prefixes));
26 }
27 
28 static void close_rwops(SDL_RWops* rwops)
29 {
30  SDL_RWclose(rwops);
31 }
32 
33 delete_ptr<SDL_RWops> File_resource::open_rwops_(const std::string& filename)
34 {
35  for (auto prefix : search_prefixes) {
36  std::string path;
37  path += prefix;
38  path += filename;
39  SDL_RWops* rwops = SDL_RWFromFile(path.c_str(), "rb");
40  if (rwops) return {rwops, &close_rwops};
41  }
42 
43  throw File_error::could_not_open(filename);
44 }
45 
46 File_resource::File_resource(const std::string& filename)
47  : ptr_{open_rwops_(filename)}
48 { }
49 
50 } // end namespace detail
51 
52 delete_ptr<TTF_Font> Font::load_(const std::string& filename,
53  File_resource&& file,
54  int size)
55 {
56  TTF_Font* result = TTF_OpenFontRW(std::move(file).release(), 1, size);
57  if (result) return {result, &TTF_CloseFont};
58 
59  throw Font_error::could_not_load(filename);
60 }
61 
62 Font::Font(const std::string& filename, int size)
63  : ptr_{nullptr, &no_op_deleter}
64 {
65  File_resource fr(filename);
66  ptr_ = load_(filename, std::move(fr), size);
67 }
68 
69 }
The game engine namespace.
Definition: ge211.h:17
Font(const std::string &filename, int size)
Loads a font from the specified TrueType font file, at the specified size.