SFML logo
  • Main Page
  • Namespaces
  • Classes
  • Files
  • File List

Window.cpp

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/Window/Window.hpp>
00029 #include <SFML/Window/ContextGL.hpp>
00030 #include <SFML/Window/WindowImpl.hpp>
00031 #include <SFML/System/Sleep.hpp>
00032 #include <SFML/System/Err.hpp>
00033 
00034 
00036 // Private data
00038 namespace
00039 {
00040     const sf::Window* fullscreenWindow = NULL;
00041 }
00042 
00043 
00044 namespace sf
00045 {
00047 Window::Window() :
00048 myWindow        (NULL),
00049 myContext       (NULL),
00050 myLastFrameTime (0.f),
00051 myIsExternal    (false),
00052 myFramerateLimit(0),
00053 mySetCursorPosX (0xFFFF),
00054 mySetCursorPosY (0xFFFF)
00055 {
00056 
00057 }
00058 
00059 
00061 Window::Window(VideoMode mode, const std::string& title, unsigned long style, const ContextSettings& settings) :
00062 myWindow        (NULL),
00063 myContext       (NULL),
00064 myLastFrameTime (0.f),
00065 myIsExternal    (false),
00066 myFramerateLimit(0),
00067 mySetCursorPosX (0xFFFF),
00068 mySetCursorPosY (0xFFFF)
00069 {
00070     Create(mode, title, style, settings);
00071 }
00072 
00073 
00075 Window::Window(WindowHandle handle, const ContextSettings& settings) :
00076 myWindow        (NULL),
00077 myContext       (NULL),
00078 myLastFrameTime (0.f),
00079 myIsExternal    (true),
00080 myFramerateLimit(0),
00081 mySetCursorPosX (0xFFFF),
00082 mySetCursorPosY (0xFFFF)
00083 {
00084     Create(handle, settings);
00085 }
00086 
00087 
00089 Window::~Window()
00090 {
00091     Close();
00092 }
00093 
00094 
00096 void Window::Create(VideoMode mode, const std::string& title, unsigned long style, const ContextSettings& settings)
00097 {
00098     // Destroy the previous window implementation
00099     Close();
00100 
00101     // Fullscreen style requires some tests
00102     if (style & Style::Fullscreen)
00103     {
00104         // Make sure there's not already a fullscreen window (only one is allowed)
00105         if (fullscreenWindow)
00106         {
00107             Err() << "Creating two fullscreen windows is not allowed, switching to windowed mode" << std::endl;
00108             style &= ~Style::Fullscreen;
00109         }
00110         else
00111         {
00112             // Make sure the chosen video mode is compatible
00113             if (!mode.IsValid())
00114             {
00115                 Err() << "The requested video mode is not available, switching to a valid mode" << std::endl;
00116                 mode = VideoMode::GetMode(0);
00117             }
00118 
00119             // Update the fullscreen window
00120             fullscreenWindow = this;
00121         }
00122     }
00123 
00124     // Check validity of style
00125     if ((style & Style::Close) || (style & Style::Resize))
00126         style |= Style::Titlebar;
00127 
00128     // Recreate the window implementation
00129     myWindow = priv::WindowImpl::New(mode, title, style);
00130 
00131     // Recreate the context
00132     myContext = priv::ContextGL::New(myWindow, mode.BitsPerPixel, settings);
00133 
00134     // Perform common initializations
00135     Initialize();
00136 }
00137 
00138 
00140 void Window::Create(WindowHandle handle, const ContextSettings& settings)
00141 {
00142     // Destroy the previous window implementation
00143     Close();
00144 
00145     // Recreate the window implementation
00146     myWindow = priv::WindowImpl::New(handle);
00147 
00148     // Recreate the context
00149     myContext = priv::ContextGL::New(myWindow, VideoMode::GetDesktopMode().BitsPerPixel, settings);
00150 
00151     // Perform common initializations
00152     Initialize();
00153 }
00154 
00155 
00157 void Window::Close()
00158 {
00159     if (myContext)
00160     {
00161         // Delete the context
00162         delete myContext;
00163         myContext = NULL;
00164     }
00165 
00166     if (myWindow)
00167     {
00168         // Delete the window implementation
00169         delete myWindow;
00170         myWindow = NULL;
00171     }
00172 
00173     // Update the fullscreen window
00174     if (this == fullscreenWindow)
00175         fullscreenWindow = NULL;
00176 }
00177 
00178 
00180 bool Window::IsOpened() const
00181 {
00182     return myWindow != NULL;
00183 }
00184 
00185 
00187 unsigned int Window::GetWidth() const
00188 {
00189     return myWindow ? myWindow->GetWidth() : 0;
00190 }
00191 
00192 
00194 unsigned int Window::GetHeight() const
00195 {
00196     return myWindow ? myWindow->GetHeight() : 0;
00197 }
00198 
00199 
00201 const ContextSettings& Window::GetSettings() const
00202 {
00203     static const ContextSettings empty(0, 0, 0);
00204 
00205     return myContext ? myContext->GetSettings() : empty;
00206 }
00207 
00208 
00210 bool Window::GetEvent(Event& event)
00211 {
00212     if (myWindow && myWindow->PopEvent(event, false))
00213     {
00214         return FilterEvent(event);
00215     }
00216     else
00217     {
00218         return false;
00219     }
00220 }
00221 
00222 
00224 bool Window::WaitEvent(Event& event)
00225 {
00226     if (myWindow && myWindow->PopEvent(event, true))
00227     {
00228         return FilterEvent(event);
00229     }
00230     else
00231     {
00232         return false;
00233     }
00234 }
00235 
00236 
00238 void Window::UseVerticalSync(bool enabled)
00239 {
00240     if (SetActive())
00241         myContext->UseVerticalSync(enabled);
00242 }
00243 
00244 
00246 void Window::ShowMouseCursor(bool show)
00247 {
00248     if (myWindow)
00249         myWindow->ShowMouseCursor(show);
00250 }
00251 
00252 
00254 void Window::SetCursorPosition(unsigned int left, unsigned int top)
00255 {
00256     if (myWindow)
00257     {
00258         // Keep coordinates for later checking (to reject the generated MouseMoved event)
00259         mySetCursorPosX = left;
00260         mySetCursorPosY = top;
00261 
00262         myWindow->SetCursorPosition(left, top);
00263     }
00264 }
00265 
00266 
00268 void Window::SetPosition(int left, int top)
00269 {
00270     if (myWindow)
00271         myWindow->SetPosition(left, top);
00272 }
00273 
00274 
00276 void Window::SetSize(unsigned int width, unsigned int height)
00277 {
00278     if (myWindow)
00279         myWindow->SetSize(width, height);
00280 }
00281 
00282 
00284 void Window::Show(bool show)
00285 {
00286     if (myWindow)
00287         myWindow->Show(show);
00288 }
00289 
00290 
00292 void Window::EnableKeyRepeat(bool enabled)
00293 {
00294     if (myWindow)
00295         myWindow->EnableKeyRepeat(enabled);
00296 }
00297 
00298 
00300 void Window::SetIcon(unsigned int width, unsigned int height, const Uint8* pixels)
00301 {
00302     if (myWindow)
00303         myWindow->SetIcon(width, height, pixels);
00304 }
00305 
00306 
00308 bool Window::SetActive(bool active) const
00309 {
00310     if (myContext)
00311     {
00312         if (myContext->SetActive(active))
00313         {
00314             return true;
00315         }
00316         else
00317         {
00318             Err() << "Failed to activate the window's context" << std::endl;
00319             return false;
00320         }
00321     }
00322     else
00323     {
00324         return false;
00325     }
00326 }
00327 
00328 
00330 void Window::Display()
00331 {
00332     // Limit the framerate if needed
00333     if (myFramerateLimit > 0)
00334     {
00335         float remainingTime = 1.f / myFramerateLimit - myClock.GetElapsedTime();
00336         if (remainingTime > 0)
00337             Sleep(remainingTime);
00338     }
00339 
00340     // Measure the time elapsed since last frame
00341     myLastFrameTime = myClock.GetElapsedTime();
00342     myClock.Reset();
00343 
00344     // Display the backbuffer on screen
00345     if (SetActive())
00346         myContext->Display();
00347 }
00348 
00349 
00351 const Input& Window::GetInput() const
00352 {
00353     return myInput;
00354 }
00355 
00356 
00358 void Window::SetFramerateLimit(unsigned int limit)
00359 {
00360     myFramerateLimit = limit;
00361 }
00362 
00363 
00365 float Window::GetFrameTime() const
00366 {
00367     return myLastFrameTime;
00368 }
00369 
00370 
00372 void Window::SetJoystickThreshold(float threshold)
00373 {
00374     if (myWindow)
00375         myWindow->SetJoystickThreshold(threshold);
00376 }
00377 
00378 
00380 void Window::OnCreate()
00381 {
00382     // Nothing by default
00383 }
00384 
00385 
00387 void Window::OnResize()
00388 {
00389     // Nothing by default
00390 }
00391 
00392 
00394 bool Window::FilterEvent(const Event& event)
00395 {
00396     // Discard MouseMove events generated by SetCursorPosition
00397     if ((event.Type        == Event::MouseMoved) &&
00398         (event.MouseMove.X == mySetCursorPosX)   &&
00399         (event.MouseMove.Y == mySetCursorPosY))
00400     {
00401         mySetCursorPosX = 0xFFFF;
00402         mySetCursorPosY = 0xFFFF;
00403         return false;
00404     }
00405 
00406     // Notify resize events to the derived class
00407     if (event.Type == Event::Resized)
00408         OnResize();
00409 
00410     // Notify the input object
00411     myInput.OnEvent(event);
00412 
00413     return true;
00414 }
00415 
00416 
00418 void Window::Initialize()
00419 {
00420     // Setup default behaviours (to get a consistent behaviour across different implementations)
00421     Show(true);
00422     UseVerticalSync(false);
00423     ShowMouseCursor(true);
00424     EnableKeyRepeat(true);
00425 
00426     // Reset frame time
00427     myClock.Reset();
00428     myLastFrameTime = 0.f;
00429 
00430     // Activate the window
00431     SetActive();
00432 
00433     // Notify the derived class
00434     OnCreate();
00435 }
00436 
00437 } // namespace sf

 ::  Copyright © 2007-2008 Laurent Gomila, all rights reserved  ::  Documentation generated by doxygen 1.5.2  ::