Back to index

libsfml  1.6+dfsg2
RenderTarget.cpp
Go to the documentation of this file.
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 <SFML/Graphics/GraphicsContext.hpp>
00031 #include <iostream>
00032 
00033 
00034 namespace sf
00035 {
00039 RenderTarget::RenderTarget() :
00040 myCurrentView   (&myDefaultView),
00041 myPreserveStates(false),
00042 myIsDrawing     (false)
00043 {
00044 
00045 }
00046 
00047 
00051 RenderTarget::~RenderTarget()
00052 {
00053     // Nothing to do
00054 }
00055 
00056 
00060 void RenderTarget::Clear(const Color& FillColor)
00061 {
00062     if (Activate(true))
00063     {
00064         // Clear the frame buffer
00065         GLCheck(glClearColor(FillColor.r / 255.f, FillColor.g / 255.f, FillColor.b / 255.f, FillColor.a / 255.f));
00066         GLCheck(glClear(GL_COLOR_BUFFER_BIT));
00067 
00068         Activate(false);
00069     }
00070 }
00071 
00072 
00076 void RenderTarget::Draw(const Drawable& Object)
00077 {
00078     // Check whether we are called from the outside or from a previous call to Draw
00079     if (!myIsDrawing)
00080     {
00081         myIsDrawing = true;
00082 
00083         // Set our target as the current target for rendering
00084         if (Activate(true))
00085         {
00086             // Save the current render states and set the SFML ones
00087             if (myPreserveStates)
00088             {
00089                 GLCheck(glPushAttrib(GL_COLOR_BUFFER_BIT | GL_CURRENT_BIT   | GL_ENABLE_BIT  |
00090                                      GL_TEXTURE_BIT      | GL_TRANSFORM_BIT | GL_VIEWPORT_BIT));
00091                 GLCheck(glMatrixMode(GL_MODELVIEW));  GLCheck(glPushMatrix());
00092                 GLCheck(glMatrixMode(GL_PROJECTION)); GLCheck(glPushMatrix());
00093                 SetRenderStates();
00094             }
00095 
00096             // Set the window viewport and transform matrices
00097             GLCheck(glViewport(0, 0, GetWidth(), GetHeight()));
00098             GLCheck(glMatrixMode(GL_PROJECTION)); GLCheck(glLoadMatrixf(myCurrentView->GetMatrix().Get4x4Elements()));
00099             GLCheck(glMatrixMode(GL_MODELVIEW));  GLCheck(glLoadIdentity());
00100 
00101             // Let the object draw itself
00102             Object.Draw(*this);
00103 
00104             // Restore render states
00105             if (myPreserveStates)
00106             {
00107                 GLCheck(glMatrixMode(GL_PROJECTION)); GLCheck(glPopMatrix());
00108                 GLCheck(glMatrixMode(GL_MODELVIEW));  GLCheck(glPopMatrix());
00109                 GLCheck(glPopAttrib());
00110             }
00111 
00112             // Deactivate rendering on this target
00113             Activate(false);
00114         }
00115 
00116         myIsDrawing = false;
00117     }
00118     else
00119     {
00120         // We are already called from a previous Draw : we don't need to set the states again, just draw the object
00121         Object.Draw(*this);
00122     }
00123 }
00124 
00125 
00129 void RenderTarget::SetView(const View& NewView)
00130 {
00131     myCurrentView = &NewView;
00132 }
00133 
00134 
00138 const View& RenderTarget::GetView() const
00139 {
00140     return *myCurrentView;
00141 }
00142 
00143 
00147 View& RenderTarget::GetDefaultView()
00148 {
00149     return myDefaultView;
00150 }
00151 
00152 
00161 void RenderTarget::PreserveOpenGLStates(bool Preserve)
00162 {
00163     myPreserveStates = Preserve;
00164 }
00165 
00166 
00170 void RenderTarget::Initialize()
00171 {
00172     // Set the default rendering states
00173     SetRenderStates();
00174 
00175     // Setup the default view
00176     myDefaultView.SetFromRect(FloatRect(0, 0, static_cast<float>(GetWidth()), static_cast<float>(GetHeight())));
00177     SetView(myDefaultView);
00178 }
00179 
00180 
00184 void RenderTarget::SetRenderStates()
00185 {
00186     GLCheck(glDisable(GL_ALPHA_TEST));
00187     GLCheck(glDisable(GL_DEPTH_TEST));
00188     GLCheck(glDisable(GL_LIGHTING)); 
00189 }
00190 
00191 } // namespace sf