Window.cpp

00001 
00002 //
00003 // SFML - Simple and Fast Multimedia Library
00004 // Copyright (C) 2007 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/WindowImpl.hpp>
00030 #include <iostream>
00031 #include <memory>
00032 
00033 namespace
00034 {
00035     // Create a global dummy window, so that we have a valid OpenGL context at program startup
00036     //
00037     // TODO : provide a way to control the dummy creation / destruction, as the order of
00038     // initialization of globals is completely random across compile units...
00039     //
00040     std::auto_ptr<sf_private::sfWindowImpl> DummyWindow(sf_private::sfWindowImpl::New());
00041 }
00042 
00043 
00048 sfWindow::sfWindow() :
00049 myWindow       (NULL),
00050 myLastFrameTime(0.f)
00051 {
00052 
00053 }
00054 
00055 
00059 sfWindow::sfWindow(sfVideoMode Mode, const std::string& Title, bool Fullscreen) :
00060 myWindow       (NULL),
00061 myLastFrameTime(0.f)
00062 {
00063     Create(Mode, Title, Fullscreen);
00064 }
00065 
00066 
00070 sfWindow::sfWindow(sfWindowHandle Handle) :
00071 myWindow       (NULL),
00072 myLastFrameTime(0.f)
00073 {
00074     Create(Handle);
00075 }
00076 
00077 
00081 sfWindow::~sfWindow()
00082 {
00083     // Destroy the window implementation
00084     delete myWindow;
00085 }
00086 
00087 
00091 void sfWindow::Create(sfVideoMode Mode, const std::string& Title, bool Fullscreen)
00092 {
00093     // Check validity of video mode
00094     if (Fullscreen && !Mode.IsValid())
00095     {
00096         std::cerr << "Requested video mode is not available, switching to a valid mode" << std::endl;
00097         Mode = sfVideoMode::GetMode(0);
00098     }
00099 
00100     Initialize(sf_private::sfWindowImpl::New(Mode, Title, Fullscreen));
00101 }
00102 
00103 
00107 void sfWindow::Create(sfWindowHandle Handle)
00108 {
00109     Initialize(sf_private::sfWindowImpl::New(Handle));
00110 }
00111 
00112 
00116 unsigned int sfWindow::GetWidth() const
00117 {
00118     return myWindow ? myWindow->GetWidth() : 0;
00119 }
00120 
00121 
00125 unsigned int sfWindow::GetHeight() const
00126 {
00127     return myWindow ? myWindow->GetHeight() : 0;
00128 }
00129 
00130 
00134 unsigned int sfWindow::GetDepthBits() const
00135 {
00136     return myWindow ? myWindow->GetDepthBits() : 0;
00137 }
00138 
00139 
00143 unsigned int sfWindow::GetStencilBits() const
00144 {
00145     return myWindow ? myWindow->GetStencilBits() : 0;
00146 }
00147 
00148 
00152 bool sfWindow::GetEvent(sfEvent& Event)
00153 {
00154     // Pop first event of queue, if not empty
00155     if (!myEvents.empty())
00156     {
00157         Event = myEvents.front();
00158         myEvents.pop();
00159 
00160         return true;
00161     }
00162 
00163     return false;
00164 }
00165 
00166 
00170 void sfWindow::UseVerticalSync(bool Enabled)
00171 {
00172     if (SetCurrent())
00173         myWindow->UseVerticalSync(Enabled);
00174 }
00175 
00176 
00180 void sfWindow::ShowMouseCursor(bool Show)
00181 {
00182     if (SetCurrent())
00183         myWindow->ShowMouseCursor(Show);
00184 }
00185 
00186 
00190 bool sfWindow::SetCurrent() const
00191 {
00192     if (myWindow)
00193     {
00194         myWindow->SetCurrent();
00195         return true;
00196     }
00197 
00198     return false;
00199 }
00200 
00201 
00205 void sfWindow::Display()
00206 {
00207     // Measure the time elapsed since last frame
00208     myLastFrameTime = myClock.GetElapsedTime();
00209     myClock.Reset();
00210 
00211     if (SetCurrent())
00212     {
00213         // Display the backbuffer on screen
00214         myWindow->Display();
00215 
00216         // Let the window implementation process incoming events
00217         myWindow->ProcessEvents();
00218     }
00219 }
00220 
00221 
00225 const sfInput& sfWindow::GetInput() const
00226 {
00227     return myInput;
00228 }
00229 
00230 
00234 float sfWindow::GetFrameTime() const
00235 {
00236     return myLastFrameTime;
00237 }
00238 
00239 
00243 void sfWindow::OnEvent(const sfEvent& Event)
00244 {
00245     myEvents.push(Event);
00246 }
00247 
00248 
00252 void sfWindow::Initialize(sf_private::sfWindowImpl* Window)
00253 {
00254     // Destroy window if already created
00255     if (myWindow)
00256         delete myWindow;
00257 
00258     // Assign new window and listen to its events
00259     myWindow = Window;
00260     myWindow->AddListener(this);
00261 
00262     // Attach input to the window
00263     myWindow->AddListener(&myInput);
00264 
00265     // Disable vertical synchronization by default (to get a consistent behavior)
00266     UseVerticalSync(false);
00267 
00268     // Reset frame time
00269     myClock.Reset();
00270     myLastFrameTime = 0.f;
00271 }