WindowImpl.cpp
1
2//
3// SFML - Simple and Fast Multimedia Library
4// Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
5//
6// This software is provided 'as-is', without any express or implied warranty.
7// In no event will the authors be held liable for any damages arising from the use of this software.
8//
9// Permission is granted to anyone to use this software for any purpose,
10// including commercial applications, and to alter it and redistribute it freely,
11// subject to the following restrictions:
12//
13// 1. The origin of this software must not be misrepresented;
14// you must not claim that you wrote the original software.
15// If you use this software in a product, an acknowledgment
16// in the product documentation would be appreciated but is not required.
17//
18// 2. Altered source versions must be plainly marked as such,
19// and must not be misrepresented as being the original software.
20//
21// 3. This notice may not be removed or altered from any source distribution.
22//
24
26// Headers
28#include <SFML/Window/WindowImpl.hpp>
29#include <SFML/Window/Event.hpp>
30#include <SFML/Window/WindowListener.hpp>
31#include <algorithm>
32#include <cmath>
33
34#if defined(SFML_SYSTEM_WINDOWS)
35
36 #include <SFML/Window/Win32/WindowImplWin32.hpp>
37 typedef sf::priv::WindowImplWin32 WindowImplType;
38
39#elif defined(SFML_SYSTEM_LINUX) || defined(SFML_SYSTEM_FREEBSD)
40
41 #include <SFML/Window/Linux/WindowImplX11.hpp>
42 typedef sf::priv::WindowImplX11 WindowImplType;
43
44#elif defined(SFML_SYSTEM_MACOS)
45
46 #include <SFML/Window/Cocoa/WindowImplCocoa.hpp>
47 typedef sf::priv::WindowImplCocoa WindowImplType;
48
49#endif
50
51
52namespace sf
53{
54namespace priv
55{
59WindowImpl* WindowImpl::New()
60{
61 return new WindowImplType();
62}
63
64
68WindowImpl* WindowImpl::New(VideoMode Mode, const std::string& Title, unsigned long WindowStyle, WindowSettings& Params)
69{
70 return new WindowImplType(Mode, Title, WindowStyle, Params);
71}
72
73
77WindowImpl* WindowImpl::New(WindowHandle Handle, WindowSettings& Params)
78{
79 return new WindowImplType(Handle, Params);
80}
81
82
86WindowImpl::WindowImpl() :
87myWidth (0),
88myHeight (0),
89myJoyThreshold(0.1f)
90{
91}
92
93
97WindowImpl::~WindowImpl()
98{
99 // Nothing to do
100}
101
102
106void WindowImpl::AddListener(WindowListener* Listener)
107{
108 if (Listener)
109 myListeners.insert(Listener);
110}
111
112
116void WindowImpl::RemoveListener(WindowListener* Listener)
117{
118 myListeners.erase(Listener);
119}
120
121
125void WindowImpl::Initialize()
126{
127 // Initialize the joysticks
128 for (unsigned int i = 0; i < Joy::Count; ++i)
129 {
130 myJoysticks[i].Initialize(i);
131 myJoyStates[i] = myJoysticks[i].UpdateState();
132 }
133}
134
135
139unsigned int WindowImpl::GetWidth() const
140{
141 return myWidth;
142}
143
144
148unsigned int WindowImpl::GetHeight() const
149{
150 return myHeight;
151}
152
153
158void WindowImpl::SetJoystickThreshold(float Threshold)
159{
160 myJoyThreshold = Threshold;
161}
162
163
167void WindowImpl::DoEvents()
168{
169 // Read the joysticks state and generate the appropriate events
170 ProcessJoystickEvents();
171
172 // Let the derived class process other events
173 ProcessEvents();
174}
175
176
180bool WindowImpl::IsContextActive()
181{
182 return WindowImplType::IsContextActive();
183}
184
185
189void WindowImpl::SendEvent(const Event& EventToSend)
190{
191 for (std::set<WindowListener*>::iterator i = myListeners.begin(); i != myListeners.end(); ++i)
192 {
193 (*i)->OnEvent(EventToSend);
194 }
195}
196
197
203int WindowImpl::EvaluateConfig(const VideoMode& Mode, const WindowSettings& Settings, int ColorBits, int DepthBits, int StencilBits, int Antialiasing)
204{
205 return abs(static_cast<int>(Mode.BitsPerPixel - ColorBits)) +
206 abs(static_cast<int>(Settings.DepthBits - DepthBits)) +
207 abs(static_cast<int>(Settings.StencilBits - StencilBits)) +
208 abs(static_cast<int>(Settings.AntialiasingLevel - Antialiasing));
209}
210
211
215void WindowImpl::ProcessJoystickEvents()
216{
217 for (unsigned int i = 0; i < Joy::Count; ++i)
218 {
219 // Copy the previous state of the joystick and get the new one
220 JoystickState PreviousState = myJoyStates[i];
221 myJoyStates[i] = myJoysticks[i].UpdateState();
222
223 // Axis
224 for (unsigned int j = 0; j < Joy::AxisCount; ++j)
225 {
226 Joy::Axis Axis = static_cast<Joy::Axis>(j);
227 if (myJoysticks[i].HasAxis(Axis))
228 {
229 float PrevPos = PreviousState.Axis[j];
230 float CurrPos = myJoyStates[i].Axis[j];
231 if (fabs(CurrPos - PrevPos) >= myJoyThreshold)
232 {
233 Event Event;
234 Event.Type = Event::JoyMoved;
235 Event.JoyMove.JoystickId = i;
236 Event.JoyMove.Axis = Axis;
237 Event.JoyMove.Position = CurrPos;
238 SendEvent(Event);
239 }
240 }
241 }
242
243 // Buttons
244 for (unsigned int j = 0; j < myJoysticks[i].GetButtonsCount(); ++j)
245 {
246 bool PrevPressed = PreviousState.Buttons[j];
247 bool CurrPressed = myJoyStates[i].Buttons[j];
248
249 if ((!PrevPressed && CurrPressed) || (PrevPressed && !CurrPressed))
250 {
251 Event Event;
252 Event.Type = CurrPressed ? Event::JoyButtonPressed : Event::JoyButtonReleased;
253 Event.JoyButton.JoystickId = i;
254 Event.JoyButton.Button = j;
255 SendEvent(Event);
256 }
257 }
258 }
259}
260
261
262} // namespace priv
263
264} // namespace sf