RenderWindow.cpp
1 //
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/RenderWindow.hpp>
29 #include <SFML/Graphics/Drawable.hpp>
30 #include <SFML/Graphics/Image.hpp>
31 #include <SFML/Graphics/GraphicsContext.hpp>
32 #include <iostream>
33 
34 
35 namespace sf
36 {
41 {
42  // Nothing to do
43 }
44 
45 
49 RenderWindow::RenderWindow(VideoMode Mode, const std::string& Title, unsigned long WindowStyle, const WindowSettings& Params)
50 {
51  Create(Mode, Title, WindowStyle, Params);
52 }
53 
54 
58 RenderWindow::RenderWindow(WindowHandle Handle, const WindowSettings& Params)
59 {
60  Create(Handle, Params);
61 }
62 
63 
68 {
69  // Nothing to do
70 }
71 
72 
76 bool RenderWindow::Activate(bool Active)
77 {
78  // For performances and consistency reasons, we only handle activation
79  if (Active)
80  return SetActive();
81  else
82  return true;
83 }
84 
85 
89 unsigned int RenderWindow::GetWidth() const
90 {
91  return sf::Window::GetWidth();
92 }
93 
94 
98 unsigned int RenderWindow::GetHeight() const
99 {
100  return sf::Window::GetHeight();
101 }
102 
103 
108 {
109  // Get the window dimensions
110  const unsigned int Width = GetWidth();
111  const unsigned int Height = GetHeight();
112 
113  // Set our window as the current target for rendering
114  if (SetActive())
115  {
116  // Make sure we have a valid context
117  priv::GraphicsContext Ctx;
118 
119  // Get pixels from the backbuffer
120  std::vector<Uint8> Pixels(Width * Height * 4);
121  Uint8* PixelsPtr = &Pixels[0];
122  GLCheck(glReadPixels(0, 0, Width, Height, GL_RGBA, GL_UNSIGNED_BYTE, PixelsPtr));
123 
124  // Flip the pixels
125  unsigned int Pitch = Width * 4;
126  for (unsigned int y = 0; y < Height / 2; ++y)
127  std::swap_ranges(PixelsPtr + y * Pitch, PixelsPtr + (y + 1) * Pitch, PixelsPtr + (Height - y - 1) * Pitch);
128 
129  // Create an image from the pixel buffer and return it
130  return Image(Width, Height, PixelsPtr);
131  }
132  else
133  {
134  return Image(Width, Height, Color::White);
135  }
136 }
137 
138 
142 sf::Vector2f RenderWindow::ConvertCoords(unsigned int WindowX, unsigned int WindowY, const View* TargetView) const
143 {
144  // Use the current view if none has been passed
145  if (!TargetView)
146  TargetView = &GetView();
147 
148  float Left = TargetView->GetCenter().x - TargetView->GetHalfSize().x;
149  float Top = TargetView->GetCenter().y - TargetView->GetHalfSize().y;
150  float Right = TargetView->GetCenter().x + TargetView->GetHalfSize().x;
151  float Bottom = TargetView->GetCenter().y + TargetView->GetHalfSize().y;
152 
153  return sf::Vector2f(Left + WindowX * (Right - Left) / GetWidth(),
154  Top + WindowY * (Bottom - Top) / GetHeight());
155 }
156 
157 
161 void RenderWindow::OnCreate()
162 {
163  // We can now initialize the render target part
165 }
166 
167 } // namespace sf
virtual unsigned int GetWidth() const
Get the width of the rendering region of the window.
This class defines a view (position, size, etc.) ; you can consider it as a 2D camera.
Definition: View.hpp:45
Structure defining the creation settings of windows.
unsigned int GetWidth() const
Get the width of the rendering region of the window.
Definition: Window.cpp:191
T x
X coordinate of the vector.
Definition: Vector2.hpp:59
Image Capture() const
Save the content of the window to an image.
sf::Vector2f ConvertCoords(unsigned int WindowX, unsigned int WindowY, const View *TargetView=NULL) const
Convert a point in window coordinates into view coordinates.
RenderWindow()
Default constructor.
const sf::Vector2f & GetCenter() const
Get the center of the view.
Definition: View.cpp:108
const View & GetView() const
Get the current view.
VideoMode defines a video mode (width, height, bpp, frequency) and provides static functions for gett...
Definition: VideoMode.hpp:42
const sf::Vector2f & GetHalfSize() const
Get the half-size of the view.
Definition: View.cpp:117
Image is the low-level class for loading and manipulating images.
Definition: Image.hpp:46
bool SetActive(bool Active=true) const
Activate of deactivate the window as the current target for rendering.
Definition: Window.cpp:338
static const Color White
White predefined color.
Definition: Color.hpp:105
unsigned int GetHeight() const
Get the height of the rendering region of the window.
Definition: Window.cpp:200
void Initialize()
Called by the derived class when it's ready to be initialized.
T y
Y coordinate of the vector.
Definition: Vector2.hpp:60
void Create(VideoMode Mode, const std::string &Title, unsigned long WindowStyle=Style::Resize|Style::Close, const WindowSettings &Params=WindowSettings())
Create (or recreate) the window.
Definition: Window.cpp:104
virtual ~RenderWindow()
Destructor.
virtual unsigned int GetHeight() const
Get the height of the rendering region of the window.