D:/Programmation/Cpp/SFML/src/SFML/Window/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 
00032 
00037 sfWindow::sfWindow() :
00038 myWindow       (NULL),
00039 myLastFrameTime(0.f)
00040 {
00041 
00042 }
00043 
00044 
00048 sfWindow::sfWindow(sfVideoMode Mode, const std::string& Title, bool Fullscreen) :
00049 myWindow       (NULL),
00050 myLastFrameTime(0.f)
00051 {
00052     Create(Mode, Title, Fullscreen);
00053 }
00054 
00055 
00059 sfWindow::sfWindow(void* Handle) :
00060 myWindow       (NULL),
00061 myLastFrameTime(0.f)
00062 {
00063     Create(Handle);
00064 }
00065 
00066 
00070 sfWindow::~sfWindow()
00071 {
00072     // Destroy window implementation
00073     delete myWindow;
00074 }
00075 
00076 
00080 void sfWindow::Create(sfVideoMode Mode, const std::string& Title, bool Fullscreen)
00081 {
00082     // Check validity of video mode
00083     if (Fullscreen && !Mode.IsValid())
00084     {
00085         std::cerr << "Requested video mode is not available, switching to a valid mode" << std::endl;
00086         Mode = sfVideoMode::GetMode(0);
00087     }
00088 
00089     Initialize(sf_private::sfWindowImpl::New(Mode, Title, Fullscreen));
00090 }
00091 
00092 
00096 void sfWindow::Create(void* Handle)
00097 {
00098     Initialize(sf_private::sfWindowImpl::New(Handle));
00099 }
00100 
00101 
00105 unsigned int sfWindow::GetWidth() const
00106 {
00107     return myWindow ? myWindow->GetWidth() : 0;
00108 }
00109 
00110 
00114 unsigned int sfWindow::GetHeight() const
00115 {
00116     return myWindow ? myWindow->GetHeight() : 0;
00117 }
00118 
00119 
00123 bool sfWindow::GetEvent(sfEvent& Event)
00124 {
00125     // Pop first event of queue, if not empty
00126     if (!myEvents.empty())
00127     {
00128         Event = myEvents.front();
00129         myEvents.pop();
00130 
00131         return true;
00132     }
00133 
00134     return false;
00135 }
00136 
00137 
00141 void sfWindow::UseVerticalSync(bool Enabled)
00142 {
00143     if (SetCurrent())
00144         myWindow->UseVerticalSync(Enabled);
00145 }
00146 
00147 
00151 bool sfWindow::SetCurrent() const
00152 {
00153     if (myWindow)
00154     {
00155         myWindow->SetCurrent();
00156         return true;
00157     }
00158 
00159     return false;
00160 }
00161 
00162 
00166 void sfWindow::Display()
00167 {
00168     if (SetCurrent())
00169     {
00170         // Display backbuffer on screen
00171         myLastFrameTime = myWindow->Display();
00172 
00173         // Let the window implementation process incoming events
00174         myWindow->ProcessEvents();
00175     }
00176 }
00177 
00178 
00182 const sfInput& sfWindow::GetInput() const
00183 {
00184     return myInput;
00185 }
00186 
00187 
00191 float sfWindow::GetFrameTime() const
00192 {
00193     return myLastFrameTime;
00194 }
00195 
00196 
00200 void sfWindow::OnEvent(const sfEvent& Event)
00201 {
00202     myEvents.push(Event);
00203 }
00204 
00205 
00209 void sfWindow::Initialize(sf_private::sfWindowImpl* Window)
00210 {
00211     // Destroy window if already created
00212     if (myWindow)
00213         delete myWindow;
00214 
00215     // Assign new window and listen to its events
00216     myWindow = Window;
00217     myWindow->AddListener(this);
00218 
00219     // Attach input to the window
00220     myWindow->AddListener(&myInput);
00221 
00222     // Disable vertical synchronization by default (to get a consistent behavior)
00223     UseVerticalSync(false);
00224 
00225     // Reset frame time
00226     myLastFrameTime = 0.f;
00227 }

Generated for SFML by  doxygen 1.5.2