summaryrefslogtreecommitdiff
path: root/src/SFML/Window/Window.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/SFML/Window/Window.cpp')
-rwxr-xr-xsrc/SFML/Window/Window.cpp191
1 files changed, 125 insertions, 66 deletions
diff --git a/src/SFML/Window/Window.cpp b/src/SFML/Window/Window.cpp
index 0643b34..ff0e21a 100755
--- a/src/SFML/Window/Window.cpp
+++ b/src/SFML/Window/Window.cpp
@@ -1,7 +1,7 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
-// Copyright (C) 2007 Laurent Gomila (laurent.gom@gmail.com)
+// Copyright (C) 2007-2008 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
@@ -29,22 +29,17 @@
#include <SFML/Window/WindowImpl.hpp>
#include <SFML/System/Sleep.hpp>
#include <iostream>
-#include <memory>
-
-namespace
-{
- // Create a global dummy window, so that we have a valid OpenGL context at program startup
- //
- // TODO : provide a way to control the dummy creation / destruction, as the order of
- // initialization of globals is completely random across compile units...
- //
- std::auto_ptr<sf::priv::WindowImpl> DummyWindow(sf::priv::WindowImpl::New());
-}
namespace sf
{
////////////////////////////////////////////////////////////
+// Static member data
+////////////////////////////////////////////////////////////
+priv::WindowImpl* Window::ourDummyWindow = NULL;
+
+
+////////////////////////////////////////////////////////////
/// Default constructor
///
////////////////////////////////////////////////////////////
@@ -52,7 +47,9 @@ Window::Window() :
myWindow (NULL),
myLastFrameTime (0.f),
myIsExternal (false),
-myFramerateLimit(0)
+myFramerateLimit(0),
+mySetCursorPosX (-1),
+mySetCursorPosY (-1)
{
}
@@ -61,26 +58,30 @@ myFramerateLimit(0)
////////////////////////////////////////////////////////////
/// Construct a new window
////////////////////////////////////////////////////////////
-Window::Window(VideoMode Mode, const std::string& Title, unsigned long WindowStyle, int AntialiasingLevel) :
+Window::Window(VideoMode Mode, const std::string& Title, unsigned long WindowStyle, const WindowSettings& Params) :
myWindow (NULL),
myLastFrameTime (0.f),
myIsExternal (false),
-myFramerateLimit(0)
+myFramerateLimit(0),
+mySetCursorPosX (-1),
+mySetCursorPosY (-1)
{
- Create(Mode, Title, WindowStyle, AntialiasingLevel);
+ Create(Mode, Title, WindowStyle, Params);
}
////////////////////////////////////////////////////////////
/// Construct the window from an existing control
////////////////////////////////////////////////////////////
-Window::Window(WindowHandle Handle, int AntialiasingLevel) :
+Window::Window(WindowHandle Handle, const WindowSettings& Params) :
myWindow (NULL),
myLastFrameTime (0.f),
myIsExternal (true),
-myFramerateLimit(0)
+myFramerateLimit(0),
+mySetCursorPosX (-1),
+mySetCursorPosY (-1)
{
- Create(Handle, AntialiasingLevel);
+ Create(Handle, Params);
}
@@ -89,16 +90,19 @@ myFramerateLimit(0)
////////////////////////////////////////////////////////////
Window::~Window()
{
- // Destroy the window implementation
- delete myWindow;
+ // Close the window
+ Close();
}
////////////////////////////////////////////////////////////
/// Create the window
////////////////////////////////////////////////////////////
-void Window::Create(VideoMode Mode, const std::string& Title, unsigned long WindowStyle, int AntialiasingLevel)
+void Window::Create(VideoMode Mode, const std::string& Title, unsigned long WindowStyle, const WindowSettings& Params)
{
+ // Make sure we have a valid OpenGL context
+ ForceContextInit();
+
// Check validity of video mode
if ((WindowStyle & Style::Fullscreen) && !Mode.IsValid())
{
@@ -106,52 +110,80 @@ void Window::Create(VideoMode Mode, const std::string& Title, unsigned long Wind
Mode = VideoMode::GetMode(0);
}
- Initialize(priv::WindowImpl::New(Mode, Title, WindowStyle, AntialiasingLevel));
+ // Check validity of style
+ if ((WindowStyle & Style::Close) || (WindowStyle & Style::Close))
+ WindowStyle |= Style::Titlebar;
+
+ mySettings = Params;
+ Initialize(priv::WindowImpl::New(Mode, Title, WindowStyle, mySettings));
}
////////////////////////////////////////////////////////////
/// Create the window from an existing control
////////////////////////////////////////////////////////////
-void Window::Create(WindowHandle Handle, int AntialiasingLevel)
+void Window::Create(WindowHandle Handle, const WindowSettings& Params)
{
- Initialize(priv::WindowImpl::New(Handle, AntialiasingLevel));
+ // Make sure we have a valid OpenGL context
+ ForceContextInit();
+
+ mySettings = Params;
+ Initialize(priv::WindowImpl::New(Handle, mySettings));
}
////////////////////////////////////////////////////////////
-/// Get the width of the rendering region of the window
+/// Close (destroy) the window.
+/// The sf::Window instance remains valid and you can call
+/// Create to recreate the window
////////////////////////////////////////////////////////////
-unsigned int Window::GetWidth() const
+void Window::Close()
{
- return myWindow ? myWindow->GetWidth() : 0;
+ // Make sure we always have a valid OpenGL context
+ if (ourDummyWindow)
+ ourDummyWindow->SetActive(true);
+
+ // Delete the window implementation
+ delete myWindow;
+ myWindow = NULL;
}
////////////////////////////////////////////////////////////
-/// Get the height of the rendering region of the window
+/// Tell whether or not the window is opened (ie. has been created).
+/// Note that a hidden window (Show(false))
+/// will still return true
////////////////////////////////////////////////////////////
-unsigned int Window::GetHeight() const
+bool Window::IsOpened() const
{
- return myWindow ? myWindow->GetHeight() : 0;
+ return myWindow != NULL;
+}
+
+
+////////////////////////////////////////////////////////////
+/// Get the width of the rendering region of the window
+////////////////////////////////////////////////////////////
+unsigned int Window::GetWidth() const
+{
+ return myWindow ? myWindow->GetWidth() : 0;
}
////////////////////////////////////////////////////////////
-/// Get the depth buffer bits
+/// Get the height of the rendering region of the window
////////////////////////////////////////////////////////////
-unsigned int Window::GetDepthBits() const
+unsigned int Window::GetHeight() const
{
- return myWindow ? myWindow->GetDepthBits() : 0;
+ return myWindow ? myWindow->GetHeight() : 0;
}
////////////////////////////////////////////////////////////
-/// Get the stencil buffer bits
+/// Get the creation settings of the window
////////////////////////////////////////////////////////////
-unsigned int Window::GetStencilBits() const
+const WindowSettings& Window::GetSettings() const
{
- return myWindow ? myWindow->GetStencilBits() : 0;
+ return mySettings;
}
@@ -160,6 +192,10 @@ unsigned int Window::GetStencilBits() const
////////////////////////////////////////////////////////////
bool Window::GetEvent(Event& EventReceived)
{
+ // Let the window implementation process incoming events if the events queue is empty
+ if (myWindow && myEvents.empty())
+ myWindow->DoEvents();
+
// Pop first event of queue, if not empty
if (!myEvents.empty())
{
@@ -178,7 +214,7 @@ bool Window::GetEvent(Event& EventReceived)
////////////////////////////////////////////////////////////
void Window::UseVerticalSync(bool Enabled)
{
- if (SetCurrent())
+ if (SetActive())
myWindow->UseVerticalSync(Enabled);
}
@@ -199,7 +235,13 @@ void Window::ShowMouseCursor(bool Show)
void Window::SetCursorPosition(unsigned int Left, unsigned int Top)
{
if (myWindow)
+ {
+ // Keep coordinates for later checking (to reject the generated MouseMoved event)
+ mySetCursorPosX = Left;
+ mySetCursorPosY = Top;
+
myWindow->SetCursorPosition(Left, Top);
+ }
}
@@ -230,21 +272,29 @@ void Window::Show(bool State)
if (myWindow)
myWindow->Show(State);
}
- else
- {
- std::cerr << "Warning : trying to show/hide an external SFML window, which is not allowed" << std::endl;
- }
}
////////////////////////////////////////////////////////////
-/// Set the window as the current target for rendering
+/// Enable or disable automatic key-repeat.
+/// Automatic key-repeat is enabled by default
+////////////////////////////////////////////////////////////
+void Window::EnableKeyRepeat(bool Enabled)
+{
+ if (myWindow)
+ myWindow->EnableKeyRepeat(Enabled);
+}
+
+
+////////////////////////////////////////////////////////////
+/// Activate of deactivate the window as the current target
+/// for rendering
////////////////////////////////////////////////////////////
-bool Window::SetCurrent() const
+bool Window::SetActive(bool Active) const
{
if (myWindow)
{
- myWindow->SetCurrent();
+ myWindow->SetActive(Active);
return true;
}
@@ -260,23 +310,20 @@ void Window::Display()
// Limit the framerate if needed
if (myFramerateLimit > 0)
{
- float FrameTime = 1.f / myFramerateLimit;
- if (myClock.GetElapsedTime() < FrameTime)
- Sleep(FrameTime - myClock.GetElapsedTime());
+ float RemainingTime = 1.f / myFramerateLimit - myClock.GetElapsedTime();
+ if (RemainingTime > 0)
+ Sleep(RemainingTime);
}
// Measure the time elapsed since last frame
myLastFrameTime = myClock.GetElapsedTime();
myClock.Reset();
- if (SetCurrent())
+ if (SetActive())
{
// Display the backbuffer on screen
myWindow->Display();
- // Let the window implementation process incoming events
- myWindow->DoEvents();
-
// Notify the derived class
OnDisplay();
}
@@ -322,27 +369,31 @@ void Window::SetJoystickThreshold(float Threshold)
////////////////////////////////////////////////////////////
-/// Called after the window has been created
+/// Force a valid OpenGL context to exist even if
+/// no window has been created
////////////////////////////////////////////////////////////
-void Window::OnCreate()
+void Window::ForceContextInit()
{
- // Nothing by default
+ // We force a context to exist by creating a dummy window
+ // It might cause a small memory leak as this window won't be destroyed
+ if (!ourDummyWindow)
+ ourDummyWindow = sf::priv::WindowImpl::New();
}
////////////////////////////////////////////////////////////
-/// Called when the window displays its content on screen
+/// Called after the window has been created
////////////////////////////////////////////////////////////
-void Window::OnDisplay()
+void Window::OnCreate()
{
// Nothing by default
}
////////////////////////////////////////////////////////////
-/// Called after an event has been received
+/// Called when the window displays its content on screen
////////////////////////////////////////////////////////////
-void Window::OnEventReceived(const Event&)
+void Window::OnDisplay()
{
// Nothing by default
}
@@ -353,10 +404,17 @@ void Window::OnEventReceived(const Event&)
////////////////////////////////////////////////////////////
void Window::OnEvent(const Event& EventReceived)
{
- myEvents.push(EventReceived);
+ // Discard MouseMove events generated by SetCursorPosition
+ if ((EventReceived.Type == Event::MouseMoved) &&
+ (EventReceived.MouseMove.X == static_cast<unsigned int>(mySetCursorPosX)) &&
+ (EventReceived.MouseMove.Y == static_cast<unsigned int>(mySetCursorPosY)))
+ {
+ mySetCursorPosX = -1;
+ mySetCursorPosY = -1;
+ return;
+ }
- SetCurrent();
- OnEventReceived(EventReceived);
+ myEvents.push(EventReceived);
}
@@ -366,8 +424,7 @@ void Window::OnEvent(const Event& EventReceived)
void Window::Initialize(priv::WindowImpl* Window)
{
// Destroy window if already created
- if (myWindow)
- delete myWindow;
+ delete myWindow;
// Assign new window and listen to its events
myWindow = Window;
@@ -376,16 +433,18 @@ void Window::Initialize(priv::WindowImpl* Window)
// Attach input to the window
myWindow->AddListener(&myInput);
- // Disable vertical synchronization and show mouse cursor by default (to get a consistent behavior)
+ // Setup default behaviours (to get a consistent behaviour across different implementations)
+ Show(true);
UseVerticalSync(false);
ShowMouseCursor(true);
+ EnableKeyRepeat(true);
// Reset frame time
myClock.Reset();
myLastFrameTime = 0.f;
// Notify the derived class
- SetCurrent();
+ SetActive();
OnCreate();
}