Back to index

libsfml  1.6+dfsg2
RenderWindow.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/RenderWindow.hpp>
00029 #include <SFML/Graphics/Drawable.hpp>
00030 #include <SFML/Graphics/Image.hpp>
00031 #include <SFML/Graphics/GraphicsContext.hpp>
00032 #include <iostream>
00033 
00034 
00035 namespace sf
00036 {
00040 RenderWindow::RenderWindow()
00041 {
00042     // Nothing to do
00043 }
00044 
00045 
00049 RenderWindow::RenderWindow(VideoMode Mode, const std::string& Title, unsigned long WindowStyle, const WindowSettings& Params)
00050 {
00051     Create(Mode, Title, WindowStyle, Params);
00052 }
00053 
00054 
00058 RenderWindow::RenderWindow(WindowHandle Handle, const WindowSettings& Params)
00059 {
00060     Create(Handle, Params);
00061 }
00062 
00063 
00067 RenderWindow::~RenderWindow()
00068 {
00069     // Nothing to do
00070 }
00071 
00072 
00076 bool RenderWindow::Activate(bool Active)
00077 {
00078     // For performances and consistency reasons, we only handle activation
00079     if (Active)
00080         return SetActive();
00081     else
00082         return true;
00083 }
00084 
00085 
00089 unsigned int RenderWindow::GetWidth() const
00090 {
00091     return sf::Window::GetWidth();
00092 }
00093 
00094 
00098 unsigned int RenderWindow::GetHeight() const
00099 {
00100     return sf::Window::GetHeight();
00101 }
00102 
00103 
00107 Image RenderWindow::Capture() const
00108 {
00109     // Get the window dimensions
00110     const unsigned int Width  = GetWidth();
00111     const unsigned int Height = GetHeight();
00112 
00113     // Set our window as the current target for rendering
00114     if (SetActive())
00115     {
00116         // Make sure we have a valid context
00117         priv::GraphicsContext Ctx;
00118 
00119         // Get pixels from the backbuffer
00120         std::vector<Uint8> Pixels(Width * Height * 4);
00121         Uint8* PixelsPtr = &Pixels[0];
00122         GLCheck(glReadPixels(0, 0, Width, Height, GL_RGBA, GL_UNSIGNED_BYTE, PixelsPtr));
00123 
00124         // Flip the pixels
00125         unsigned int Pitch = Width * 4;
00126         for (unsigned int y = 0; y < Height / 2; ++y)
00127             std::swap_ranges(PixelsPtr + y * Pitch, PixelsPtr + (y + 1) * Pitch, PixelsPtr + (Height - y - 1) * Pitch);
00128 
00129         // Create an image from the pixel buffer and return it
00130         return Image(Width, Height, PixelsPtr);
00131     }
00132     else
00133     {
00134         return Image(Width, Height, Color::White);
00135     }
00136 }
00137 
00138 
00142 sf::Vector2f RenderWindow::ConvertCoords(unsigned int WindowX, unsigned int WindowY, const View* TargetView) const
00143 {
00144     // Use the current view if none has been passed
00145     if (!TargetView)
00146         TargetView = &GetView();
00147 
00148     float Left   = TargetView->GetCenter().x - TargetView->GetHalfSize().x;
00149     float Top    = TargetView->GetCenter().y - TargetView->GetHalfSize().y;
00150     float Right  = TargetView->GetCenter().x + TargetView->GetHalfSize().x;
00151     float Bottom = TargetView->GetCenter().y + TargetView->GetHalfSize().y;
00152 
00153     return sf::Vector2f(Left + WindowX * (Right - Left) / GetWidth(),
00154                         Top  + WindowY * (Bottom - Top) / GetHeight());
00155 }
00156 
00157 
00161 void RenderWindow::OnCreate()
00162 {
00163     // We can now initialize the render target part
00164     RenderTarget::Initialize();
00165 }
00166 
00167 } // namespace sf