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

WindowImpl.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/WindowImpl.hpp>
00029 #include <SFML/Window/Event.hpp>
00030 #include <algorithm>
00031 #include <cmath>
00032 
00033 #if defined(SFML_SYSTEM_WINDOWS)
00034 
00035     #include <SFML/Window/Win32/WindowImplWin32.hpp>
00036     typedef sf::priv::WindowImplWin32 WindowImplType;
00037 
00038 #elif defined(SFML_SYSTEM_LINUX) || defined(SFML_SYSTEM_FREEBSD)
00039 
00040     #include <SFML/Window/Linux/WindowImplX11.hpp>
00041     typedef sf::priv::WindowImplX11 WindowImplType;
00042 
00043 #elif defined(SFML_SYSTEM_MACOS)
00044 
00045     #include <SFML/Window/Cocoa/WindowImplCocoa.hpp>
00046     typedef sf::priv::WindowImplCocoa WindowImplType;
00047 
00048 #endif
00049 
00050 
00051 namespace sf
00052 {
00053 namespace priv
00054 {
00056 WindowImpl* WindowImpl::New(VideoMode mode, const std::string& title, unsigned long style)
00057 {
00058     return new WindowImplType(mode, title, style);
00059 }
00060 
00061 
00063 WindowImpl* WindowImpl::New(WindowHandle handle)
00064 {
00065     return new WindowImplType(handle);
00066 }
00067 
00068 
00070 WindowImpl::WindowImpl() :
00071 myWidth       (0),
00072 myHeight      (0),
00073 myJoyThreshold(0.1f)
00074 {
00075     // Initialize the joysticks
00076     for (unsigned int i = 0; i < Joy::Count; ++i)
00077     {
00078         myJoysticks[i].Initialize(i);
00079         myJoyStates[i] = myJoysticks[i].UpdateState();
00080     }
00081 }
00082 
00083 
00085 WindowImpl::~WindowImpl()
00086 {
00087     // Nothing to do
00088 }
00089 
00090 
00092 unsigned int WindowImpl::GetWidth() const
00093 {
00094     return myWidth;
00095 }
00096 
00097 
00099 unsigned int WindowImpl::GetHeight() const
00100 {
00101     return myHeight;
00102 }
00103 
00104 
00106 void WindowImpl::SetJoystickThreshold(float threshold)
00107 {
00108     myJoyThreshold = threshold;
00109 }
00110 
00111 
00113 bool WindowImpl::PopEvent(Event& event, bool block)
00114 {
00115     // If the event queue is empty, let's first check if new events are available from the OS
00116     if (myEvents.empty())
00117     {
00118         ProcessJoystickEvents();
00119         ProcessEvents(block);
00120     }
00121 
00122     // Pop the first event of the queue, if it is not empty
00123     if (!myEvents.empty())
00124     {
00125         event = myEvents.front();
00126         myEvents.pop();
00127 
00128         return true;
00129     }
00130 
00131     return false;
00132 }
00133 
00134 
00136 void WindowImpl::PushEvent(const Event& event)
00137 {
00138     myEvents.push(event);
00139 }
00140 
00141 
00143 void WindowImpl::ProcessJoystickEvents()
00144 {
00145     for (unsigned int i = 0; i < Joy::Count; ++i)
00146     {
00147         // Copy the previous state of the joystick and get the new one
00148         JoystickState previousState = myJoyStates[i];
00149         myJoyStates[i] = myJoysticks[i].UpdateState();
00150 
00151         // Axis
00152         for (unsigned int j = 0; j < Joy::AxisCount; ++j)
00153         {
00154             Joy::Axis axis = static_cast<Joy::Axis>(j);
00155             if (myJoysticks[i].HasAxis(axis))
00156             {
00157                 float prevPos = previousState.Axis[axis];
00158                 float currPos = myJoyStates[i].Axis[axis];
00159                 if (fabs(currPos - prevPos) >= myJoyThreshold)
00160                 {
00161                     Event event;
00162                     event.Type               = Event::JoyMoved;
00163                     event.JoyMove.JoystickId = i;
00164                     event.JoyMove.Axis       = axis;
00165                     event.JoyMove.Position   = currPos;
00166                     PushEvent(event);
00167                 }
00168             }
00169         }
00170 
00171         // Buttons
00172         for (unsigned int j = 0; j < myJoysticks[i].GetButtonsCount(); ++j)
00173         {
00174             bool prevPressed = previousState.Buttons[j];
00175             bool currPressed = myJoyStates[i].Buttons[j];
00176 
00177             if ((!prevPressed && currPressed) || (prevPressed && !currPressed))
00178             {
00179                 Event event;
00180                 event.Type                 = currPressed ? Event::JoyButtonPressed : Event::JoyButtonReleased;
00181                 event.JoyButton.JoystickId = i;
00182                 event.JoyButton.Button     = j;
00183                 PushEvent(event);
00184             }
00185         }
00186     }
00187 }
00188 
00189 
00190 } // namespace priv
00191 
00192 } // namespace sf

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