Mercurial > sdl-ios-xcode
annotate src/thread/generic/SDL_syssem.c @ 4167:a6f635e5eaa6 SDL-1.2
Fixed bug #611
From Tim Angus 2008-08-12 11:18:06
I'm one of the maintainers of ioquake3.org, an updated version of the
Quake 3 engine. Relatively recently, we moved ioq3 to use SDL as a
replacement for 95% of the platform specific code that was there. On the
whole it's doing a great job but unfortunately since the move we've been
getting complaints about the quality of the mouse input on the Windows
platform to the point where for many the game is unplayable. Put in
other terms, the current stable SDL 1.2 is basically not fit for purpose
if you need high quality mouse input as you do in a first person shooter.
Over the weekend I decided to pull my finger out and actually figure out
what's going on. There are basically two major problems. Firstly, when
using the "windib" driver, mouse input is gathered via the WM_MOUSEMOVE
message. Googling for this indicates that often this is known to result
in "spurious" and/or "missing" mouse movement events; this is the
primary cause of the poor mouse input. The second problem is that the
"directx" driver does not work at all in combination with OpenGL meaning
that you can't use DirectInput if your application also uses OpenGL. In
other words you're locked into using the "windib" driver and its poor
mouse input.
In order to address these problems I've done the following:
* Remove WM_MOUSEMOVE based motion event generation and replace with
calls to GetCursorPos which seems much more reliable. In order to
achieve this I've moved mouse motion out into a separate function that
is called once per DIB_PumpEvents.
* Remove the restriction on the "directx" driver being inoperable in
combination with OpenGL. There is a bug for this issues that I've
hijacked to a certain extent
(http://bugzilla.libsdl.org/show_bug.cgi?id=265). I'm the first to admit
I don't really understand why this restriction is there in the first
place. The commit message for the bug fix that introduced this
restriction (r581) isn't very elaborate and I couldn't see any other bug
tracking the issue. If anyone has more information on the bug that was
avoided by r581 it would be helpful as I/someone could then look into
addressing the problem without disabling the "directx" driver.
* I've also removed the restriction on not being allowed to use
DirectInput in windowed mode. I couldn't see any reason for this, at
least not from our perspective. I have my suspicions that it'll be
something like matching up the cursor with the mouse coordinates...
* I bumped up the DirectInput API used to version 7 in order to get
access to mouse buttons 4-7. I've had to inject a little bit of the DX7
headers into SDL there as the MinGW ones aren't up to date in this respect.
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Thu, 02 Apr 2009 04:43:36 +0000 |
parents | a1b03ba2fcd0 |
children | 62e86ab81e3c |
rev | line source |
---|---|
0 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
4159 | 3 Copyright (C) 1997-2009 Sam Lantinga |
0 | 4 |
5 This library is free software; you can redistribute it and/or | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
6 modify it under the terms of the GNU Lesser General Public |
0 | 7 License as published by the Free Software Foundation; either |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
8 version 2.1 of the License, or (at your option) any later version. |
0 | 9 |
10 This library is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
13 Lesser General Public License for more details. |
0 | 14 |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
15 You should have received a copy of the GNU Lesser General Public |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
16 License along with this library; if not, write to the Free Software |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
0 | 18 |
19 Sam Lantinga | |
252
e8157fcb3114
Updated the source with the correct e-mail address
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
20 slouken@libsdl.org |
0 | 21 */ |
1402
d910939febfa
Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents:
1361
diff
changeset
|
22 #include "SDL_config.h" |
0 | 23 |
24 /* An implementation of semaphores using mutexes and condition variables */ | |
25 | |
26 #include "SDL_timer.h" | |
27 #include "SDL_thread.h" | |
28 #include "SDL_systhread_c.h" | |
29 | |
30 | |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
31 #if SDL_THREADS_DISABLED |
0 | 32 |
33 SDL_sem *SDL_CreateSemaphore(Uint32 initial_value) | |
34 { | |
35 SDL_SetError("SDL not configured with thread support"); | |
36 return (SDL_sem *)0; | |
37 } | |
38 | |
39 void SDL_DestroySemaphore(SDL_sem *sem) | |
40 { | |
41 return; | |
42 } | |
43 | |
44 int SDL_SemTryWait(SDL_sem *sem) | |
45 { | |
46 SDL_SetError("SDL not configured with thread support"); | |
47 return -1; | |
48 } | |
49 | |
50 int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout) | |
51 { | |
52 SDL_SetError("SDL not configured with thread support"); | |
53 return -1; | |
54 } | |
55 | |
56 int SDL_SemWait(SDL_sem *sem) | |
57 { | |
58 SDL_SetError("SDL not configured with thread support"); | |
59 return -1; | |
60 } | |
61 | |
62 Uint32 SDL_SemValue(SDL_sem *sem) | |
63 { | |
64 return 0; | |
65 } | |
66 | |
67 int SDL_SemPost(SDL_sem *sem) | |
68 { | |
69 SDL_SetError("SDL not configured with thread support"); | |
70 return -1; | |
71 } | |
72 | |
73 #else | |
74 | |
75 struct SDL_semaphore | |
76 { | |
77 Uint32 count; | |
78 Uint32 waiters_count; | |
79 SDL_mutex *count_lock; | |
80 SDL_cond *count_nonzero; | |
81 }; | |
82 | |
83 SDL_sem *SDL_CreateSemaphore(Uint32 initial_value) | |
84 { | |
85 SDL_sem *sem; | |
86 | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
87 sem = (SDL_sem *)SDL_malloc(sizeof(*sem)); |
0 | 88 if ( ! sem ) { |
89 SDL_OutOfMemory(); | |
1627 | 90 return NULL; |
0 | 91 } |
92 sem->count = initial_value; | |
93 sem->waiters_count = 0; | |
94 | |
95 sem->count_lock = SDL_CreateMutex(); | |
96 sem->count_nonzero = SDL_CreateCond(); | |
97 if ( ! sem->count_lock || ! sem->count_nonzero ) { | |
98 SDL_DestroySemaphore(sem); | |
1627 | 99 return NULL; |
0 | 100 } |
101 | |
1627 | 102 return sem; |
0 | 103 } |
104 | |
105 /* WARNING: | |
106 You cannot call this function when another thread is using the semaphore. | |
107 */ | |
108 void SDL_DestroySemaphore(SDL_sem *sem) | |
109 { | |
110 if ( sem ) { | |
111 sem->count = 0xFFFFFFFF; | |
112 while ( sem->waiters_count > 0) { | |
113 SDL_CondSignal(sem->count_nonzero); | |
114 SDL_Delay(10); | |
115 } | |
116 SDL_DestroyCond(sem->count_nonzero); | |
1627 | 117 if ( sem->count_lock ) { |
118 SDL_mutexP(sem->count_lock); | |
119 SDL_mutexV(sem->count_lock); | |
120 SDL_DestroyMutex(sem->count_lock); | |
121 } | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
122 SDL_free(sem); |
0 | 123 } |
124 } | |
125 | |
126 int SDL_SemTryWait(SDL_sem *sem) | |
127 { | |
128 int retval; | |
129 | |
130 if ( ! sem ) { | |
131 SDL_SetError("Passed a NULL semaphore"); | |
132 return -1; | |
133 } | |
134 | |
135 retval = SDL_MUTEX_TIMEDOUT; | |
136 SDL_LockMutex(sem->count_lock); | |
137 if ( sem->count > 0 ) { | |
138 --sem->count; | |
139 retval = 0; | |
140 } | |
141 SDL_UnlockMutex(sem->count_lock); | |
142 | |
143 return retval; | |
144 } | |
145 | |
146 int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout) | |
147 { | |
148 int retval; | |
149 | |
150 if ( ! sem ) { | |
151 SDL_SetError("Passed a NULL semaphore"); | |
152 return -1; | |
153 } | |
154 | |
155 /* A timeout of 0 is an easy case */ | |
156 if ( timeout == 0 ) { | |
157 return SDL_SemTryWait(sem); | |
158 } | |
159 | |
160 SDL_LockMutex(sem->count_lock); | |
161 ++sem->waiters_count; | |
162 retval = 0; | |
163 while ( (sem->count == 0) && (retval != SDL_MUTEX_TIMEDOUT) ) { | |
164 retval = SDL_CondWaitTimeout(sem->count_nonzero, | |
165 sem->count_lock, timeout); | |
166 } | |
167 --sem->waiters_count; | |
168 --sem->count; | |
169 SDL_UnlockMutex(sem->count_lock); | |
170 | |
171 return retval; | |
172 } | |
173 | |
174 int SDL_SemWait(SDL_sem *sem) | |
175 { | |
176 return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT); | |
177 } | |
178 | |
179 Uint32 SDL_SemValue(SDL_sem *sem) | |
180 { | |
181 Uint32 value; | |
182 | |
183 value = 0; | |
184 if ( sem ) { | |
185 SDL_LockMutex(sem->count_lock); | |
186 value = sem->count; | |
187 SDL_UnlockMutex(sem->count_lock); | |
188 } | |
189 return value; | |
190 } | |
191 | |
192 int SDL_SemPost(SDL_sem *sem) | |
193 { | |
194 if ( ! sem ) { | |
195 SDL_SetError("Passed a NULL semaphore"); | |
196 return -1; | |
197 } | |
198 | |
199 SDL_LockMutex(sem->count_lock); | |
200 if ( sem->waiters_count > 0 ) { | |
201 SDL_CondSignal(sem->count_nonzero); | |
202 } | |
203 ++sem->count; | |
204 SDL_UnlockMutex(sem->count_lock); | |
205 | |
206 return 0; | |
207 } | |
208 | |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
209 #endif /* SDL_THREADS_DISABLED */ |