SFML logo
  • Main Page
  • Namespaces
  • Classes
  • Files
  • File List

RenderTarget.cpp

00001 
00002 //
00003 // SFML - Simple and Fast Multimedia Library
00004 // Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
00005 //
00006 // This software is provided 'as-is', without any express or implied warranty.
00007 // In no event will the authors be held liable for any damages arising from the use of this software.
00008 //
00009 // Permission is granted to anyone to use this software for any purpose,
00010 // including commercial applications, and to alter it and redistribute it freely,
00011 // subject to the following restrictions:
00012 //
00013 // 1. The origin of this software must not be misrepresented;
00014 //    you must not claim that you wrote the original software.
00015 //    If you use this software in a product, an acknowledgment
00016 //    in the product documentation would be appreciated but is not required.
00017 //
00018 // 2. Altered source versions must be plainly marked as such,
00019 //    and must not be misrepresented as being the original software.
00020 //
00021 // 3. This notice may not be removed or altered from any source distribution.
00022 //
00024 
00026 // Headers
00028 #include <SFML/Graphics/RenderTarget.hpp>
00029 #include <SFML/Graphics/Drawable.hpp>
00030 #include <iostream>
00031 
00032 #ifdef _MSC_VER
00033     #pragma warning(disable : 4355) // "'this' : used in base member initializer list"
00034 #endif
00035 
00036 
00037 namespace sf
00038 {
00040 RenderTarget::RenderTarget() :
00041 myRenderer      (*this),
00042 myStatesSaved   (false),
00043 myViewHasChanged(false)
00044 {
00045 
00046 }
00047 
00048 
00050 RenderTarget::~RenderTarget()
00051 {
00052     // Nothing to do
00053 }
00054 
00055 
00057 void RenderTarget::Clear(const Color& color)
00058 {
00059     if (Activate(true))
00060         myRenderer.Clear(color);
00061 }
00062 
00063 
00065 void RenderTarget::Draw(const Drawable& object)
00066 {
00067     if (Activate(true))
00068     {
00069         // Update the projection matrix and viewport if the current view has changed
00070         // Note: we do the changes here and not directly in SetView in order to gather
00071         // rendering commands, which is safer in multithreaded environments
00072         if (myViewHasChanged)
00073         {
00074             myRenderer.SetProjection(myCurrentView.GetMatrix());
00075             myRenderer.SetViewport(GetViewport(myCurrentView));
00076             myViewHasChanged = false;
00077         }
00078 
00079         // Save the current render states
00080         myRenderer.PushStates();
00081 
00082         // Setup the shader
00083         myRenderer.SetShader(NULL);
00084 
00085         // Let the object draw itself
00086         object.Draw(*this, myRenderer);
00087 
00088         // Restore the previous render states
00089         myRenderer.PopStates();
00090     }
00091 }
00092 
00093 
00095 void RenderTarget::Draw(const Drawable& object, const Shader& shader)
00096 {
00097     if (Activate(true))
00098     {
00099         // Update the projection matrix and viewport if the current view has changed
00100         // Note: we do the changes here and not directly in SetView in order to gather
00101         // rendering commands, which is safer in multithreaded environments
00102         if (myViewHasChanged)
00103         {
00104             myRenderer.SetProjection(myCurrentView.GetMatrix());
00105             myRenderer.SetViewport(GetViewport(myCurrentView));
00106             myViewHasChanged = false;
00107         }
00108 
00109         // Save the current render states
00110         myRenderer.PushStates();
00111 
00112         // Setup the shader
00113         myRenderer.SetShader(&shader);
00114 
00115         // Let the object draw itself
00116         object.Draw(*this, myRenderer);
00117 
00118         // Restore the previous render states
00119         myRenderer.PopStates();
00120     }
00121 }
00122 
00123 
00125 void RenderTarget::SetView(const View& view)
00126 {
00127     // Save it for later use
00128     myCurrentView = view;
00129     myViewHasChanged = true;
00130 }
00131 
00132 
00134 const View& RenderTarget::GetView() const
00135 {
00136     return myCurrentView;
00137 }
00138 
00139 
00141 const View& RenderTarget::GetDefaultView() const
00142 {
00143     return myDefaultView;
00144 }
00145 
00146 
00148 IntRect RenderTarget::GetViewport(const View& view) const
00149 {
00150     float width  = static_cast<float>(GetWidth());
00151     float height = static_cast<float>(GetHeight());
00152     const FloatRect& viewport = view.GetViewport();
00153 
00154     return IntRect(static_cast<int>(0.5f + width  * viewport.Left),
00155                    static_cast<int>(0.5f + height * viewport.Top),
00156                    static_cast<int>(0.5f + width  * viewport.Right),
00157                    static_cast<int>(0.5f + height * viewport.Bottom));
00158 }
00159 
00160 
00162 Vector2f RenderTarget::ConvertCoords(unsigned int x, unsigned int y) const
00163 {
00164     return ConvertCoords(x, y, GetView());
00165 }
00166 
00167 
00169 Vector2f RenderTarget::ConvertCoords(unsigned int x, unsigned int y, const View& view) const
00170 {
00171     // First, convert from viewport coordinates to homogeneous coordinates
00172     Vector2f coords;
00173     IntRect viewport = GetViewport(view);
00174     coords.x = -1.f + 2.f * (static_cast<int>(x) - viewport.Left) / viewport.GetSize().x;
00175     coords.y = 1.f  - 2.f * (static_cast<int>(y) - viewport.Top)  / viewport.GetSize().y;
00176 
00177     // Then transform by the inverse of the view matrix
00178     return view.GetInverseMatrix().Transform(coords);
00179 }
00180 
00181 
00183 void RenderTarget::SaveGLStates()
00184 {
00185     if (Activate(true))
00186     {
00187         myRenderer.SaveGLStates();
00188         myStatesSaved = true;
00189 
00190         // Restore the render states and the current view, for SFML rendering
00191         myRenderer.Initialize();
00192         SetView(GetView());
00193     }
00194 }
00195 
00196 
00198 void RenderTarget::RestoreGLStates()
00199 {
00200     if (myStatesSaved)
00201     {
00202         if (Activate(true))
00203         {
00204             myRenderer.RestoreGLStates();
00205             myStatesSaved = false;
00206         }
00207     }
00208 }
00209 
00210 
00212 void RenderTarget::Initialize()
00213 {
00214     // Setup the default view
00215     myDefaultView.Reset(FloatRect(0, 0, static_cast<float>(GetWidth()), static_cast<float>(GetHeight())));
00216     SetView(myDefaultView);
00217 
00218     // Initialize the renderer
00219     if (Activate(true))
00220         myRenderer.Initialize();
00221 }
00222 
00223 } // namespace sf

 ::  Copyright © 2007-2008 Laurent Gomila, all rights reserved  ::  Documentation generated by doxygen 1.5.2  ::