RenderTarget.cpp
1
2//
3// SFML - Simple and Fast Multimedia Library
4// Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@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,
10// including commercial applications, and to alter it and redistribute it freely,
11// subject to the following restrictions:
12//
13// 1. The origin of this software must not be misrepresented;
14// you must not claim that you wrote the original software.
15// If you use this software in a product, an acknowledgment
16// in the product documentation would be appreciated but is not required.
17//
18// 2. Altered source versions must be plainly marked as such,
19// and must not be misrepresented as being the original software.
20//
21// 3. This notice may not be removed or altered from any source distribution.
22//
24
26// Headers
28#include <SFML/Graphics/RenderTarget.hpp>
29#include <SFML/Graphics/Drawable.hpp>
30#include <SFML/Graphics/GraphicsContext.hpp>
31#include <iostream>
32
33
34namespace sf
35{
40myCurrentView (&myDefaultView),
41myPreserveStates(false),
42myIsDrawing (false)
43{
44
45}
46
47
52{
53 // Nothing to do
54}
55
56
60void RenderTarget::Clear(const Color& FillColor)
61{
62 if (Activate(true))
63 {
64 // Clear the frame buffer
65 GLCheck(glClearColor(FillColor.r / 255.f, FillColor.g / 255.f, FillColor.b / 255.f, FillColor.a / 255.f));
66 GLCheck(glClear(GL_COLOR_BUFFER_BIT));
67
68 Activate(false);
69 }
70}
71
72
76void RenderTarget::Draw(const Drawable& Object)
77{
78 // Check whether we are called from the outside or from a previous call to Draw
79 if (!myIsDrawing)
80 {
81 myIsDrawing = true;
82
83 // Set our target as the current target for rendering
84 if (Activate(true))
85 {
86 // Save the current render states and set the SFML ones
87 if (myPreserveStates)
88 {
89 GLCheck(glPushAttrib(GL_COLOR_BUFFER_BIT | GL_CURRENT_BIT | GL_ENABLE_BIT |
90 GL_TEXTURE_BIT | GL_TRANSFORM_BIT | GL_VIEWPORT_BIT));
91 GLCheck(glMatrixMode(GL_MODELVIEW)); GLCheck(glPushMatrix());
92 GLCheck(glMatrixMode(GL_PROJECTION)); GLCheck(glPushMatrix());
93 SetRenderStates();
94 }
95
96 // Set the window viewport and transform matrices
97 GLCheck(glViewport(0, 0, GetWidth(), GetHeight()));
98 GLCheck(glMatrixMode(GL_PROJECTION)); GLCheck(glLoadMatrixf(myCurrentView->GetMatrix().Get4x4Elements()));
99 GLCheck(glMatrixMode(GL_MODELVIEW)); GLCheck(glLoadIdentity());
100
101 // Let the object draw itself
102 Object.Draw(*this);
103
104 // Restore render states
105 if (myPreserveStates)
106 {
107 GLCheck(glMatrixMode(GL_PROJECTION)); GLCheck(glPopMatrix());
108 GLCheck(glMatrixMode(GL_MODELVIEW)); GLCheck(glPopMatrix());
109 GLCheck(glPopAttrib());
110 }
111
112 // Deactivate rendering on this target
113 Activate(false);
114 }
115
116 myIsDrawing = false;
117 }
118 else
119 {
120 // We are already called from a previous Draw : we don't need to set the states again, just draw the object
121 Object.Draw(*this);
122 }
123}
124
125
129void RenderTarget::SetView(const View& NewView)
130{
131 myCurrentView = &NewView;
132}
133
134
139{
140 return *myCurrentView;
141}
142
143
148{
149 return myDefaultView;
150}
151
152
162{
163 myPreserveStates = Preserve;
164}
165
166
171{
172 // Set the default rendering states
173 SetRenderStates();
174
175 // Setup the default view
176 myDefaultView.SetFromRect(FloatRect(0, 0, static_cast<float>(GetWidth()), static_cast<float>(GetHeight())));
177 SetView(myDefaultView);
178}
179
180
184void RenderTarget::SetRenderStates()
185{
186 GLCheck(glDisable(GL_ALPHA_TEST));
187 GLCheck(glDisable(GL_DEPTH_TEST));
188 GLCheck(glDisable(GL_LIGHTING));
189}
190
191} // namespace sf
Color is an utility class for manipulating 32-bits RGBA colors.
Definition Color.hpp:41
Uint8 a
Alpha (transparency) component.
Definition Color.hpp:119
Uint8 g
Green component.
Definition Color.hpp:117
Uint8 b
Blue component.
Definition Color.hpp:118
Uint8 r
Red component.
Definition Color.hpp:116
Abstract base class for every object that can be drawn into a render window.
Definition Drawable.hpp:59
void Clear(const Color &FillColor=Color(0, 0, 0))
Clear the entire target with a single color.
View & GetDefaultView()
Get the default view of the window for read / write.
virtual void Draw(const Drawable &Object)
Draw something into the target.
const View & GetView() const
Get the current view.
RenderTarget()
Default constructor.
void Initialize()
Called by the derived class when it's ready to be initialized.
virtual ~RenderTarget()
Destructor.
virtual unsigned int GetWidth() const =0
Get the width of the rendering region of the target.
void SetView(const View &NewView)
Change the current active view.
virtual unsigned int GetHeight() const =0
Get the height of the rendering region of the target.
void PreserveOpenGLStates(bool Preserve)
Tell SFML to preserve external OpenGL states, at the expense of more CPU charge.
This class defines a view (position, size, etc.) ; you can consider it as a 2D camera.
Definition View.hpp:46