1 /*
2 DSFML - The Simple and Fast Multimedia Library for D
3 
4 Copyright (c) 2013 - 2015 Jeremy DeHaan (dehaan.jeremiah@gmail.com)
5 
6 This software is provided 'as-is', without any express or implied warranty.
7 In no event will the authors be held liable for any damages arising from the use of this software.
8 
9 Permission is granted to anyone to use this software for any purpose, including commercial applications,
10 and to alter it and redistribute it freely, subject to the following restrictions:
11 
12 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software.
13 If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
14 
15 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
16 
17 3. This notice may not be removed or altered from any source distribution
18 */
19 
20 module dsfml.graphics.rendertarget;
21 
22 import dsfml.graphics.renderwindow;
23 import dsfml.graphics.rendertexture;
24 import dsfml.graphics.drawable;
25 import dsfml.graphics.renderstates;
26 
27 import dsfml.graphics.primitivetype;
28 import dsfml.graphics.vertex;
29 import dsfml.graphics.view;
30 import dsfml.graphics.color;
31 import dsfml.graphics.rect;
32 
33 import dsfml.system.vector2;
34 
35 /++
36  + Base class for all render targets (window, texture, ...)
37  + 
38  + RenderTarget defines the common behaviour of all the 2D render targets usable in the graphics module.
39  + 
40  + It makes it possible to draw 2D entities like sprites, shapes, text without using any OpenGL command directly.
41  + 
42  + A RenderTarget is also able to use views which are a kind of 2D cameras. With views you can globally scroll, rotate or zoom everything that is drawn, without having to transform every single entity.
43  + 
44  + On top of that, render targets are still able to render direct OpenGL stuff. It is even possible to mix together OpenGL calls and regular SFML drawing commands. When doing so, make sure that OpenGL states are not messed up by calling the pushGLStates/popGLStates functions.
45  + 
46  + Authors: Laurent Gomila, Jeremy DeHaan
47  + See_Also: http://sfml-dev.org/documentation/2.0/classsf_1_1RenderTarget.php#details
48  +/
49 interface RenderTarget
50 {
51 	/**
52 	 * Change the current active view.
53 	 * 
54 	 * The view is like a 2D camera, it controls which part of the 2D scene is visible, and how it is viewed in the render-target. The new view will affect everything that is drawn, until another view is set. 
55 	 * 
56 	 * The render target keeps its own copy of the view object, so it is not necessary to keep the original one alive after calling this function. To restore the original view of the target, you can pass the result of getDefaultView() to this function.
57 	 */
58 	@property
59 	{
60 		const(View) view(const(View) newView);
61 		const(View) view() const;
62 	}  
63 
64 	/**
65 	 * Get the default view of the render target.
66 	 * 
67 	 * The default view has the initial size of the render target, and never changes after the target has been created.
68 	 * 
69 	 * Returns: The default view of the render target.
70 	 */
71 	const(View) getDefaultView() const; // note: if refactored, change documentation of view property above
72 
73 	/**
74 	 * Return the size of the rendering region of the target.
75 	 * 
76 	 * Returns: Size in pixels
77 	 */
78 	Vector2u getSize() const;
79 
80 	/**
81 	 * Get the viewport of a view, applied to this render target.
82 	 * 
83 	 * The viewport is defined in the view as a ratio, this function simply applies this ratio to the current dimensions of the render target to calculate the pixels rectangle that the viewport actually covers in the target.
84 	 * 
85 	 * Params:
86 	 * 		view	= The view for which we want to compute the viewport
87 	 * 
88 	 * Returns: Viewport rectangle, expressed in pixels
89 	 */
90 	IntRect getViewport(const(View) view) const;
91 
92 	/**
93 	 * Clear the entire target with a single color.
94 	 * 
95 	 * This function is usually called once every frame, to clear the previous contents of the target.
96 	 * 
97 	 * Params:
98 	 * 		color	= Fill color to use to clear the render target
99 	 */
100 	void clear(Color color = Color.Black);
101 
102 	/**
103 	 * Draw a drawable object to the render target.
104 	 * 
105 	 * Params:
106 	 * 		drawable	= Object to draw
107 	 * 		states		= Render states to use for drawing
108 	 */
109 	void draw(Drawable drawable, RenderStates states = RenderStates.Default);
110 
111 	/**
112 	 * Draw primitives defined by an array of vertices.
113 	 * 
114 	 * Params:
115 	 * 		vertices	= Array of vertices to draw
116 	 * 		type		= Type of primitives to draw
117 	 * 		states		= Render states to use for drawing
118 	 */
119 	void draw(const(Vertex)[] vertices, PrimitiveType type, RenderStates states = RenderStates.Default);
120 
121 	/**
122 	 * Convert a point fom target coordinates to world coordinates, using the current view.
123 	 * 
124 	 * This function is an overload of the mapPixelToCoords function that implicitely uses the current view.
125 	 * 
126 	 * Params:
127 	 * 		point	= Pixel to convert
128 	 * 
129 	 * Returns: The converted point, in "world" coordinates.
130 	 */
131 	Vector2f mapPixelToCoords(Vector2i point) const;
132 
133 	/**
134 	 * Convert a point from target coordinates to world coordinates.
135 	 * 
136 	 * This function finds the 2D position that matches the given pixel of the render-target. In other words, it does the inverse of what the graphics card does, to find the initial position of a rendered pixel.
137 	 * 
138 	 * Initially, both coordinate systems (world units and target pixels) match perfectly. But if you define a custom view or resize your render-target, this assertion is not true anymore, ie. a point located at (10, 50) in your render-target may map to the point (150, 75) in your 2D world – if the view is translated by (140, 25).
139 	 * 
140 	 * For render-windows, this function is typically used to find which point (or object) is located below the mouse cursor.
141 	 * 
142 	 * This version uses a custom view for calculations, see the other overload of the function if you want to use the current view of the render-target.
143 	 * 
144 	 * Params:
145 	 * 		point	= Pixel to convert
146 	 * 		view	= The view to use for converting the point
147 	 * 
148 	 * Returns: The converted point, in "world" coordinates.
149 	 */
150 	Vector2f mapPixelToCoords(Vector2i point, const(View) view) const;
151 
152 	/**
153 	 * Convert a point from target coordinates to world coordinates, using the current view.
154 	 * 
155 	 * This function is an overload of the mapPixelToCoords function that implicitely uses the current view.
156 	 * 
157 	 * Params:
158 	 * 		point	= Point to convert
159 	 * 
160 	 * The converted point, in "world" coordinates
161 	 */
162 	Vector2i mapCoordsToPixel(Vector2f point) const;
163 
164 	/**
165 	 * Convert a point from world coordinates to target coordinates.
166 	 * 
167 	 * This function finds the pixel of the render-target that matches the given 2D point. In other words, it goes through the same process as the graphics card, to compute the final position of a rendered point.
168 	 * 
169 	 * Initially, both coordinate systems (world units and target pixels) match perfectly. But if you define a custom view or resize your render-target, this assertion is not true anymore, ie. a point located at (150, 75) in your 2D world may map to the pixel (10, 50) of your render-target – if the view is translated by (140, 25).
170 	 * 
171 	 * This version uses a custom view for calculations, see the other overload of the function if you want to use the current view of the render-target.
172 	 * 
173 	 * Params:
174 	 * 		point	= Point to convert
175 	 * 		view	= The view to use for converting the point
176 	 * 
177 	 * Returns: The converted point, in target coordinates (pixels)
178 	 */
179 	Vector2i mapCoordsToPixel(Vector2f point, const(View) view) const;
180 
181 	/**
182 	 * Restore the previously saved OpenGL render states and matrices.
183 	 * 
184 	 * See the description of pushGLStates to get a detailed description of these functions.
185 	 */
186 	void popGLStates();
187 
188 	/**
189 	 * Save the current OpenGL render states and matrices.
190 	 * 
191 	 * This function can be used when you mix SFML drawing and direct OpenGL rendering. Combined with PopGLStates, it ensures that:
192 	 * - SFML's internal states are not messed up by your OpenGL code
193 	 * - your OpenGL states are not modified by a call to an SFML function
194 	 * 
195 	 * More specifically, it must be used around the code that calls Draw functions.
196 	 * 
197 	 * Note that this function is quite expensive: it saves all the possible OpenGL states and matrices, even the ones you don't care about. Therefore it should be used wisely. It is provided for convenience, but the best results will be achieved if you handle OpenGL states yourself (because you know which states have really changed, and need to be saved and restored). Take a look at the ResetGLStates function if you do so.
198 	 */
199 	void pushGLStates();
200 
201 	/**
202 	 * Reset the internal OpenGL states so that the target is ready for drawing.
203 	 * 
204 	 * This function can be used when you mix SFML drawing and direct OpenGL rendering, if you choose not to use pushGLStates/popGLStates. It makes sure that all OpenGL states needed by SFML are set, so that subsequent draw() calls will work as expected.
205 	 */
206 	void resetGLStates();
207 }