Window.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/Window/Window.hpp>
29 #include <SFML/Window/Context.hpp>
30 #include <SFML/Window/WindowImpl.hpp>
31 #include <SFML/System/Sleep.hpp>
32 #include <iostream>
33 
34 
36 // Private data
38 namespace
39 {
40  const sf::Window* FullscreenWindow = NULL;
41 }
42 
43 
44 namespace sf
45 {
50 myWindow (NULL),
51 myLastFrameTime (0.f),
52 myIsExternal (false),
53 myFramerateLimit(0),
54 mySetCursorPosX (0xFFFF),
55 mySetCursorPosY (0xFFFF)
56 {
57 
58 }
59 
60 
64 Window::Window(VideoMode Mode, const std::string& Title, unsigned long WindowStyle, const WindowSettings& Params) :
65 myWindow (NULL),
66 myLastFrameTime (0.f),
67 myIsExternal (false),
68 myFramerateLimit(0),
69 mySetCursorPosX (0xFFFF),
70 mySetCursorPosY (0xFFFF)
71 {
72  Create(Mode, Title, WindowStyle, Params);
73 }
74 
75 
79 Window::Window(WindowHandle Handle, const WindowSettings& Params) :
80 myWindow (NULL),
81 myLastFrameTime (0.f),
82 myIsExternal (true),
83 myFramerateLimit(0),
84 mySetCursorPosX (0xFFFF),
85 mySetCursorPosY (0xFFFF)
86 {
87  Create(Handle, Params);
88 }
89 
90 
95 {
96  // Close the window
97  Close();
98 }
99 
100 
104 void Window::Create(VideoMode Mode, const std::string& Title, unsigned long WindowStyle, const WindowSettings& Params)
105 {
106  // Destroy the previous window implementation
107  Close();
108 
109  // Fullscreen style requires some tests
110  if (WindowStyle & Style::Fullscreen)
111  {
112  // Make sure there's not already a fullscreen window (only one is allowed)
113  if (FullscreenWindow)
114  {
115  std::cerr << "Creating two fullscreen windows is not allowed, switching to windowed mode" << std::endl;
116  WindowStyle &= ~Style::Fullscreen;
117  }
118  else
119  {
120  // Make sure the chosen video mode is compatible
121  if (!Mode.IsValid())
122  {
123  std::cerr << "The requested video mode is not available, switching to a valid mode" << std::endl;
124  Mode = VideoMode::GetMode(0);
125  }
126 
127  // Update the fullscreen window
128  FullscreenWindow = this;
129  }
130  }
131 
132  // Check validity of style
133  if ((WindowStyle & Style::Close) || (WindowStyle & Style::Resize))
134  WindowStyle |= Style::Titlebar;
135 
136  // Activate the global context
138 
139  mySettings = Params;
140  Initialize(priv::WindowImpl::New(Mode, Title, WindowStyle, mySettings));
141 }
142 
143 
147 void Window::Create(WindowHandle Handle, const WindowSettings& Params)
148 {
149  // Destroy the previous window implementation
150  Close();
151 
152  // Activate the global context
154 
155  mySettings = Params;
156  Initialize(priv::WindowImpl::New(Handle, mySettings));
157 }
158 
159 
166 {
167  // Delete the window implementation
168  delete myWindow;
169  myWindow = NULL;
170 
171  // Update the fullscreen window
172  if (this == FullscreenWindow)
173  FullscreenWindow = NULL;
174 }
175 
176 
182 bool Window::IsOpened() const
183 {
184  return myWindow != NULL;
185 }
186 
187 
191 unsigned int Window::GetWidth() const
192 {
193  return myWindow ? myWindow->GetWidth() : 0;
194 }
195 
196 
200 unsigned int Window::GetHeight() const
201 {
202  return myWindow ? myWindow->GetHeight() : 0;
203 }
204 
205 
210 {
211  return mySettings;
212 }
213 
214 
218 bool Window::GetEvent(Event& EventReceived)
219 {
220  // Let the window implementation process incoming events if the events queue is empty
221  if (myWindow && myEvents.empty())
222  myWindow->DoEvents();
223 
224  // Pop first event of queue, if not empty
225  if (!myEvents.empty())
226  {
227  EventReceived = myEvents.front();
228  myEvents.pop();
229 
230  return true;
231  }
232 
233  return false;
234 }
235 
236 
240 void Window::UseVerticalSync(bool Enabled)
241 {
242  if (SetActive())
243  myWindow->UseVerticalSync(Enabled);
244 }
245 
246 
250 void Window::ShowMouseCursor(bool Show)
251 {
252  if (myWindow)
253  myWindow->ShowMouseCursor(Show);
254 }
255 
256 
260 void Window::SetCursorPosition(unsigned int Left, unsigned int Top)
261 {
262  if (myWindow)
263  {
264  // Keep coordinates for later checking (to reject the generated MouseMoved event)
265  mySetCursorPosX = Left;
266  mySetCursorPosY = Top;
267 
268  myWindow->SetCursorPosition(Left, Top);
269  }
270 }
271 
272 
276 void Window::SetPosition(int Left, int Top)
277 {
278  if (!myIsExternal)
279  {
280  if (myWindow)
281  myWindow->SetPosition(Left, Top);
282  }
283  else
284  {
285  std::cerr << "Warning : trying to change the position of an external SFML window, which is not allowed" << std::endl;
286  }
287 }
288 
289 
293 void Window::SetSize(unsigned int Width, unsigned int Height)
294 {
295  if (myWindow)
296  myWindow->SetSize(Width, Height);
297 }
298 
299 
303 void Window::Show(bool State)
304 {
305  if (!myIsExternal)
306  {
307  if (myWindow)
308  myWindow->Show(State);
309  }
310 }
311 
312 
317 void Window::EnableKeyRepeat(bool Enabled)
318 {
319  if (myWindow)
320  myWindow->EnableKeyRepeat(Enabled);
321 }
322 
323 
327 void Window::SetIcon(unsigned int Width, unsigned int Height, const Uint8* Pixels)
328 {
329  if (myWindow)
330  myWindow->SetIcon(Width, Height, Pixels);
331 }
332 
333 
338 bool Window::SetActive(bool Active) const
339 {
340  if (myWindow)
341  {
342  myWindow->SetActive(Active);
343  return true;
344  }
345 
346  return false;
347 }
348 
349 
354 {
355  // Limit the framerate if needed
356  if (myFramerateLimit > 0)
357  {
358  float RemainingTime = 1.f / myFramerateLimit - myClock.GetElapsedTime();
359  if (RemainingTime > 0)
360  Sleep(RemainingTime);
361  }
362 
363  // Measure the time elapsed since last frame
364  myLastFrameTime = myClock.GetElapsedTime();
365  myClock.Reset();
366 
367  // Display the backbuffer on screen
368  if (SetActive())
369  myWindow->Display();
370 }
371 
372 
376 const Input& Window::GetInput() const
377 {
378  return myInput;
379 }
380 
381 
385 void Window::SetFramerateLimit(unsigned int Limit)
386 {
387  myFramerateLimit = Limit;
388 }
389 
390 
394 float Window::GetFrameTime() const
395 {
396  return myLastFrameTime;
397 }
398 
399 
404 void Window::SetJoystickThreshold(float Threshold)
405 {
406  if (myWindow)
407  myWindow->SetJoystickThreshold(Threshold);
408 }
409 
410 
414 void Window::OnCreate()
415 {
416  // Nothing by default
417 }
418 
419 
423 void Window::OnEvent(const Event& EventReceived)
424 {
425  // Discard MouseMove events generated by SetCursorPosition
426  if ((EventReceived.Type == Event::MouseMoved) &&
427  (EventReceived.MouseMove.X == mySetCursorPosX) &&
428  (EventReceived.MouseMove.Y == mySetCursorPosY))
429  {
430  mySetCursorPosX = 0xFFFF;
431  mySetCursorPosY = 0xFFFF;
432  return;
433  }
434 
435  myEvents.push(EventReceived);
436 }
437 
438 
442 void Window::Initialize(priv::WindowImpl* Window)
443 {
444  // Assign and initialize the new window
445  myWindow = Window;
446  myWindow->Initialize();
447 
448  // Clear the event queue
449  while (!myEvents.empty())
450  myEvents.pop();
451 
452  // Listen to events from the new window
453  myWindow->AddListener(this);
454  myWindow->AddListener(&myInput);
455 
456  // Setup default behaviours (to get a consistent behaviour across different implementations)
457  Show(true);
458  UseVerticalSync(false);
459  ShowMouseCursor(true);
460  EnableKeyRepeat(true);
461 
462  // Reset frame time
463  myClock.Reset();
464  myLastFrameTime = 0.f;
465 
466  // Activate the window
467  SetActive(true);
468 
469  // Notify the derived class
470  OnCreate();
471 }
472 
473 } // namespace sf
Titlebar + resizable border + maximize button.
Definition: WindowStyle.hpp:41
void EnableKeyRepeat(bool Enabled)
Enable or disable automatic key-repeat.
Definition: Window.cpp:317
Title bar + fixed border.
Definition: WindowStyle.hpp:40
Window()
Default constructor.
Definition: Window.cpp:49
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
virtual ~Window()
Destructor.
Definition: Window.cpp:94
bool IsValid() const
Tell whether or not the video mode is supported.
Definition: VideoMode.cpp:129
static VideoMode GetMode(std::size_t Index)
Get a valid video mode Index must be in range [0, GetModesCount()[ Modes are sorted from best to wors...
Definition: VideoMode.cpp:102
float GetFrameTime() const
Get time elapsed since last frame.
Definition: Window.cpp:394
void SetIcon(unsigned int Width, unsigned int Height, const Uint8 *Pixels)
Change the window&#39;s icon.
Definition: Window.cpp:327
void SetPosition(int Left, int Top)
Change the position of the window on screen.
Definition: Window.cpp:276
bool IsOpened() const
Tell whether or not the window is opened (ie.
Definition: Window.cpp:182
static Context & GetGlobal()
Get the global context.
Definition: Context.cpp:80
VideoMode defines a video mode (width, height, bpp, frequency) and provides static functions for gett...
Definition: VideoMode.hpp:42
void UseVerticalSync(bool Enabled)
Enable / disable vertical synchronization.
Definition: Window.cpp:240
Fullscreen mode (this flag and all others are mutually exclusive)
Definition: WindowStyle.hpp:43
bool GetEvent(Event &EventReceived)
Get the event on top of events stack, if any, and pop it.
Definition: Window.cpp:218
bool SetActive(bool Active=true) const
Activate of deactivate the window as the current target for rendering.
Definition: Window.cpp:338
void SetFramerateLimit(unsigned int Limit)
Limit the framerate to a maximum fixed frequency.
Definition: Window.cpp:385
float GetElapsedTime() const
Get the time elapsed since last reset.
Definition: Clock.cpp:46
void Close()
Close (destroy) the window.
Definition: Window.cpp:165
unsigned int GetHeight() const
Get the height of the rendering region of the window.
Definition: Window.cpp:200
void Display()
Display the window on screen.
Definition: Window.cpp:353
const WindowSettings & GetSettings() const
Get the creation settings of the window.
Definition: Window.cpp:209
void SetSize(unsigned int Width, unsigned int Height)
Change the size of the rendering region of the window.
Definition: Window.cpp:293
Window is a rendering window ; it can create a new window or connect to an existing one...
void Reset()
Restart the timer.
Definition: Clock.cpp:55
void SetActive(bool Active)
Activate or deactivate the context.
Definition: Context.cpp:62
Input handles real-time input from keyboard and mouse.
Definition: Input.hpp:44
void SetCursorPosition(unsigned int Left, unsigned int Top)
Change the position of the mouse cursor.
Definition: Window.cpp:260
void Show(bool State)
Show or hide the window.
Definition: Window.cpp:303
Titlebar + close button.
Definition: WindowStyle.hpp:42
void SetJoystickThreshold(float Threshold)
Change the joystick threshold, ie.
Definition: Window.cpp:404
void ShowMouseCursor(bool Show)
Show or hide the mouse cursor.
Definition: Window.cpp:250
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
Event defines a system event and its parameters.
Definition: Event.hpp:197
const Input & GetInput() const
Get the input manager of the window.
Definition: Window.cpp:376