Gamedev Framework (gf)  0.6.0
A C++11 framework for 2D games
RenderTexture.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2017 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  *
21  * Part of this file comes from SFML, with the same license:
22  * Copyright (C) 2007-2015 Laurent Gomila (laurent@sfml-dev.org)
23  */
24 #ifndef GF_RENDER_TEXTURE_H
25 #define GF_RENDER_TEXTURE_H
26 
27 #include "Portability.h"
28 #include "RenderTarget.h"
29 
30 namespace gf {
31 #ifndef DOXYGEN_SHOULD_SKIP_THIS
32 inline namespace v1 {
33 #endif
34 
35  /**
36  * @ingroup graphics
37  * @brief Target for off-screen 2D rendering into a texture
38  *
39  * gf::RenderTexture is the little brother of gf::RenderWindow.
40  * It implements the same 2D drawing and OpenGL-related functions
41  * (see their base class gf::RenderTarget for more details),
42  * the difference is that the result is stored in an off-screen
43  * texture rather than being show in a window.
44  *
45  * Rendering to a texture can be useful in a variety of situations:
46  *
47  * - precomputing a complex static texture (like a level's background
48  * from multiple tiles)
49  * - applying post-effects to the whole scene with shaders (See
50  * gf::PostProcessing)
51  * - creating a sprite from a 3D object rendered with OpenGL
52  * - etc.
53  *
54  * Usage example:
55  *
56  * ~~~{.cc}
57  * // Create a new render-window
58  * gf::RenderWindow renderer(window);
59  *
60  * // Create a new render-texture
61  * gf::RenderTexture textureRenderer;
62  *
63  * if (!textureRenderer.create(500, 500)) {
64  * return -1;
65  * }
66  *
67  * // The main loop
68  * while (window.isOpen()) {
69  * // Event processing
70  * // ...
71  *
72  * // Activate the texture
73  * textureRenderer.setActive();
74  *
75  * // Clear the whole texture with red color
76  * textureRenderer.clear(gf::Color::Red);
77  *
78  * // Draw stuff to the texture
79  * textureRenderer.draw(sprite); // sprite is a gf::Sprite
80  * textureRenderer.draw(shape); // shape is a gf::Shape
81  * textureRenderer.draw(text); // text is a gf::Text
82  *
83  * // We're done drawing to the texture
84  * textureRenderer.display();
85  *
86  * // Now we start rendering to the window
87  * // Activate it first and then clear it
88  * renderer.setActive();
89  * renderer.clear();
90  *
91  * // Draw the texture
92  * gf::Sprite sprite(textureRenderer.getTexture());
93  * renderer.draw(sprite);
94  *
95  * // End the current frame and display its contents on screen
96  * renderer.display();
97  * }
98  * ~~~
99  *
100  * @sa gf::RenderTarget, gf::RenderWindow, gf::View, gf::Texture
101  */
103  public:
104  /**
105  * @brief Default constructor
106  *
107  * Constructs an empty, invalid render-texture. You must
108  * call `create()` to have a valid render-texture.
109  *
110  * @sa create()
111  */
112  RenderTexture();
113 
114  /**
115  * @brief Destructor
116  */
117  ~RenderTexture();
118 
119  /**
120  * @brief Create the render-texture
121  *
122  * Before calling this function, the render-texture is in
123  * an invalid state, thus it is mandatory to call it before
124  * doing anything with the render-texture.
125  *
126  * @param size Size of the render-texture
127  *
128  * @return True if creation has been successful
129  */
130  bool create(Vector2u size);
131 
132  /**
133  * @brief Enable or disable texture smoothing
134  *
135  * This function is similar to `Texture::setSmooth()`.
136  * This parameter is disabled by default.
137  *
138  * @param smooth True to enable smoothing, false to disable it
139  *
140  * @sa isSmooth()
141  */
142  void setSmooth(bool smooth = true) {
143  m_texture.setSmooth(smooth);
144  }
145 
146  /**
147  * @brief Check if the smooth filtering is enabled or not
148  *
149  * @return True if texture smoothing is enabled
150  *
151  * @sa setSmooth()
152  */
153  bool isSmooth() const {
154  return m_texture.isSmooth();
155  }
156 
157  /**
158  * @brief Enable or disable texture repeating
159  *
160  * This function is similar to `Texture::setRepeated()`.
161  * This parameter is disabled by default.
162  *
163  * @param repeated True to enable repeating, false to disable it
164  *
165  * @sa isRepeated()
166  */
167  void setRepeated(bool repeated = true) {
168  m_texture.setRepeated(repeated);
169  }
170 
171  /**
172  * @brief Check if the texture is repeated or not
173  *
174  * @return True if texture is repeated
175  *
176  * @sa setRepeated()
177  */
178  bool isRepeated() const {
179  return m_texture.isRepeated();
180  }
181 
182  virtual Vector2u getSize() const override;
183 
184  /**
185  * @brief Activate the render-texture for rendering
186  *
187  * This function activates the render-texture so that all draw calls
188  * are targeted to the texture. You should call this function before
189  * you want to draw something to the target.
190  */
191  void setActive();
192 
193  /**
194  * @brief Update the contents of the target texture
195  *
196  * This function updates the target texture with what
197  * has been drawn so far. Like for windows, calling this
198  * function is mandatory at the end of rendering. Not calling
199  * it may leave the texture in an undefined state.
200  */
201  void display();
202 
203  /**
204  * @brief Copy the current contents of the render texture to an image
205  *
206  * This is a slow operation, whose main purpose is to make screenshots of
207  * the application.
208  */
209  Image capture() const;
210 
211  /**
212  * @brief Get a read-only reference to the target texture
213  *
214  * After drawing to the render-texture and calling `display()`,
215  * you can retrieve the updated texture using this function,
216  * and draw it using a sprite (for example).
217  *
218  * The internal sf::Texture of a render-texture is always the
219  * same instance, so that it is possible to call this function
220  * once and keep a reference to the texture even after it is
221  * modified.
222  *
223  * @return Const reference to the texture
224  */
225  const Texture& getTexture() const {
226  return m_texture;
227  }
228 
229  private:
230  unsigned m_name;
231  Texture m_texture;
232  };
233 
234 #ifndef DOXYGEN_SHOULD_SKIP_THIS
235 }
236 #endif
237 }
238 
239 #endif // GF_RENDER_TEXTURE_H
Target for off-screen 2D rendering into a texture.
Definition: RenderTexture.h:102
bool create(Vector2u size)
Create the render-texture.
void display()
Update the contents of the target texture.
Base class for all render targets (window, texture, ...)
Definition: RenderTarget.h:66
virtual Vector2u getSize() const override
Return the size of the rendering region of the target.
bool isRepeated() const
Check if the texture is repeated or not.
Definition: RenderTexture.h:178
void setSmooth(bool smooth=true)
Enable or disable texture smoothing.
Definition: RenderTexture.h:142
void setRepeated(bool repeated=true)
Enable or disable texture repeating.
Definition: RenderTexture.h:167
A texture for colored images.
Definition: Texture.h:339
Class for loading, manipulating and saving images.
Definition: Image.h:92
The namespace for gf classes.
Definition: Action.h:34
bool isSmooth() const
Check if the smooth filtering is enabled or not.
Definition: RenderTexture.h:153
Image capture() const
Copy the current contents of the render texture to an image.
const Texture & getTexture() const
Get a read-only reference to the target texture.
Definition: RenderTexture.h:225
~RenderTexture()
Destructor.
#define GF_API
Definition: Portability.h:35
void setActive()
Activate the render-texture for rendering.
RenderTexture()
Default constructor.