SfmlLoaders.hpp
Go to the documentation of this file.
1 //
3 // Thor C++ Library
4 // Copyright (c) 2011-2015 Jan Haller
5 //
6 // This software is provided 'as-is', without any express or implied
7 // warranty. In no event will the authors be held liable for any damages
8 // arising from the use of this software.
9 //
10 // Permission is granted to anyone to use this software for any purpose,
11 // including commercial applications, and to alter it and redistribute it
12 // freely, subject to the following restrictions:
13 //
14 // 1. The origin of this software must not be misrepresented; you must not
15 // claim that you wrote the original software. If you use this software
16 // in a product, an acknowledgment in the product documentation would be
17 // appreciated but is not required.
18 //
19 // 2. Altered source versions must be plainly marked as such, and must not be
20 // misrepresented as being the original software.
21 //
22 // 3. This notice may not be removed or altered from any source distribution.
23 //
25 
28 
29 #ifndef THOR_SFMLLOADERS_HPP
30 #define THOR_SFMLLOADERS_HPP
31 
33 #include <Thor/Resources/Detail/ResourceLoaderHelpers.hpp>
34 
35 #include <Aurora/Meta/Templates.hpp>
36 
37 
38 namespace thor
39 {
40 
43 
46 namespace Resources
47 {
48  // Make _1, _2 etc. known within this namespace
49  using namespace std::placeholders;
50 
54  template <class R>
55  ResourceLoader<R> fromFile(const std::string& filename)
56  {
57  return detail::makeResourceLoader<R>(
58  [=] (R& resource) { return resource.loadFromFile(filename); },
59  detail::Tagger("File") << filename);
60  }
61 
66  template <class R, typename T>
67  ResourceLoader<R> fromFile(const std::string& filename, T arg1)
68  {
69  return detail::makeResourceLoader<R>(
70  [=] (R& resource) { return resource.loadFromFile(filename, arg1); },
71  detail::Tagger("File") << filename << arg1);
72  }
73 
78  template <class R, typename T, typename U>
79  ResourceLoader<R> fromMemory(T arg1, U arg2)
80  {
81  return detail::makeResourceLoader<R>(
82  [=] (R& resource) { return resource.loadFromMemory(arg1, arg2); },
83  detail::Tagger("Memory") << arg1 << arg2);
84  }
85 
91  template <class R, typename T, typename U, typename V>
92  ResourceLoader<R> fromMemory(T arg1, U arg2, V arg3)
93  {
94  return detail::makeResourceLoader<R>(
95  [=] (R& resource) { return resource.loadFromMemory(arg1, arg2, arg3); },
96  detail::Tagger("Memory") << arg1 << arg2 << arg3);
97  }
98 
102  template <class R>
103  ResourceLoader<R> fromStream(sf::InputStream& stream)
104  {
105  return detail::makeResourceLoader<R>(
106  [&] (R& resource) { return resource.loadFromStream(stream); },
107  detail::Tagger("Stream") << &stream);
108  }
109 
114  template <class R>
115  ResourceLoader<R> fromStream(sf::InputStream& vertexShaderStream, sf::InputStream& fragmentShaderStream)
116  {
117  return detail::makeResourceLoader<R>(
118  [&] (R& resource) { return resource.loadFromStream(vertexShaderStream, fragmentShaderStream); },
119  detail::Tagger("Stream") << &vertexShaderStream << &fragmentShaderStream);
120  }
121 
126  template <class R, typename T>
127  ResourceLoader<R> fromStream(sf::InputStream& stream, T arg1
128  AURORA_ENABLE_IF(!std::is_base_of<sf::InputStream, T>::value))
129  {
130  return detail::makeResourceLoader<R>(
131  [&stream, arg1] (R& resource) { return resource.loadFromStream(stream, arg1); },
132  detail::Tagger("Stream") << &stream << arg1);
133  }
134 
141  template <class R>
142  ResourceLoader<R> fromSamples(const sf::Int16* samples, std::size_t sampleCount, unsigned int channelCount, unsigned int sampleRate)
143  {
144  return detail::makeResourceLoader<R>(
145  [=] (R& resource) { return resource.loadFromSamples(samples, sampleCount, channelCount, sampleRate); },
146  detail::Tagger("Samples") << samples << sampleCount << channelCount << sampleRate);
147  }
148 
153  template <class R>
154  ResourceLoader<R> fromPixels(unsigned int width, unsigned int height, const sf::Uint8* pixels)
155  {
156  return detail::makeResourceLoader<R>(
157  [=] (R& resource) -> bool
158  {
159  resource.create(width, height, pixels);
160  return true;
161  },
162  detail::Tagger("Pixels") << width << height << pixels);
163  }
164 
169  template <class R>
170  ResourceLoader<R> fromColor(unsigned int width, unsigned int height, const sf::Color& color)
171  {
172  return detail::makeResourceLoader<R>(
173  [=] (R& resource) -> bool
174  {
175  resource.create(width, height, color);
176  return true;
177  },
178  detail::Tagger("Color") << width << height << color);
179  }
180 
185  template <class R>
186  ResourceLoader<R> fromImage(const sf::Image& image, const sf::IntRect area = sf::IntRect())
187  {
188  return detail::makeResourceLoader<R>(
189  [&image, area] (R& resource) { return resource.loadFromImage(image, area); },
190  detail::Tagger("Image") << &image << area);
191  }
192 
193 } // namespace Resources
194 
196 
197 } // namespace thor
198 
199 #endif // THOR_SFMLLOADERS_HPP
Definition: Animator.hpp:42
ResourceLoader< R > fromFile(const std::string &filename)
Load the resource from a file on hard disk.
Definition: SfmlLoaders.hpp:55
ResourceLoader< R > fromImage(const sf::Image &image, const sf::IntRect area=sf::IntRect())
Load the resource (usually sf::Texture) from a sf::Image.
Definition: SfmlLoaders.hpp:186
ResourceLoader< R > fromStream(sf::InputStream &stream)
Load the resource from an input stream.
Definition: SfmlLoaders.hpp:103
ResourceLoader< R > fromPixels(unsigned int width, unsigned int height, const sf::Uint8 *pixels)
Load resource (usually sf::Image) from array of pixels.
Definition: SfmlLoaders.hpp:154
ResourceLoader< R > fromSamples(const sf::Int16 *samples, std::size_t sampleCount, unsigned int channelCount, unsigned int sampleRate)
Load sf::SoundBuffer from array of audio samples in memory.
Definition: SfmlLoaders.hpp:142
Class storing loading information for resources.
Definition: ResourceLoader.hpp:48
Class template thor::ResourceLoader.
ResourceLoader< R > fromColor(unsigned int width, unsigned int height, const sf::Color &color)
Load resource (usually sf::Image) from size and fill color.
Definition: SfmlLoaders.hpp:170
ResourceLoader< R > fromMemory(T arg1, U arg2)
Load the resource from a file in memory.
Definition: SfmlLoaders.hpp:79