27 |
|
|
28 |
#include <Foundation/NSException.h> |
#include <Foundation/NSException.h> |
29 |
#include <Foundation/NSInvocation.h> |
#include <Foundation/NSInvocation.h> |
30 |
|
#include <AppKit/NSEvent.h> |
31 |
|
|
32 |
#include "Interface/UiSdl.h" |
#include "Interface/UiSdl.h" |
33 |
|
|
34 |
@implementation UiSdl |
@implementation UiSdl |
35 |
|
|
36 |
/* private functions */ |
/* private functions */ |
37 |
|
|
38 |
|
/** |
39 |
|
* private functions who handles the actions which cooresponds to a |
40 |
|
* special key |
41 |
|
*/ |
42 |
- (void) _handleKeyDown: (SDL_keysym *) keysym |
- (void) _handleKeyDown: (SDL_keysym *) keysym |
43 |
{ |
{ |
44 |
switch (keysym->sym) |
switch (keysym->sym) |
51 |
} |
} |
52 |
} |
} |
53 |
|
|
54 |
- (void) _processEvents |
- (NSEvent *) ourMouseEventWithType: (NSEventType) type |
55 |
|
flags: (unsigned) flags |
56 |
|
location: (NSPoint) location |
57 |
{ |
{ |
58 |
SDL_Event event; |
/* FIXME - be aware of autorealease objects */ |
59 |
|
NSEvent *tmp = [NSEvent mouseEventWithType: type |
60 |
|
location: location |
61 |
|
modifierFlags: flags |
62 |
|
timestamp: 0 |
63 |
|
windowNumber: 0 |
64 |
|
context: nil |
65 |
|
eventNumber: 0 |
66 |
|
clickCount: 0 |
67 |
|
pressure: 0.0f]; |
68 |
|
|
69 |
|
RETAIN (tmp); |
70 |
|
|
71 |
while (SDL_PollEvent(&event)) |
return tmp; |
72 |
|
} |
73 |
|
|
74 |
|
- (NSEvent *) ourMouseEventWithType: (NSEventType) type |
75 |
|
flags: (unsigned) flags |
76 |
|
location: (NSPoint) location |
77 |
|
keysym: (unsigned short)keysym |
78 |
|
{ |
79 |
|
/* FIXME - be aware of autorealease objects */ |
80 |
|
NSEvent *tmp = [NSEvent keyEventWithType: type |
81 |
|
location: location |
82 |
|
modifierFlags: flags |
83 |
|
timestamp: 0 |
84 |
|
windowNumber: 0 |
85 |
|
context: nil |
86 |
|
characters: nil |
87 |
|
charactersIgnoringModifiers: nil |
88 |
|
isARepeat: NO |
89 |
|
keyCode: keysym]; |
90 |
|
|
91 |
|
RETAIN (tmp); |
92 |
|
|
93 |
|
return tmp; |
94 |
|
} |
95 |
|
|
96 |
|
|
97 |
|
/** |
98 |
|
* This function look at the event of the SDL and then make an |
99 |
|
* appropriate NSEvent which corresponds to a key press or a clmick |
100 |
|
* from the mouse. It calls the function which distribute the event |
101 |
|
* over the frame |
102 |
|
*/ |
103 |
|
- (void) _processEvents |
104 |
|
{ |
105 |
|
SDL_Event sdlEvent; |
106 |
|
|
107 |
|
/* FIXME - intialisation of type */ |
108 |
|
NSEventType type; |
109 |
|
unsigned flags = 0; |
110 |
|
int x, y; |
111 |
|
|
112 |
|
SDL_GetMouseState (&x, &y); |
113 |
|
y = ( (int) _screenSize.height ) - y; |
114 |
|
|
115 |
|
while (SDL_PollEvent(&sdlEvent)) |
116 |
{ |
{ |
117 |
switch (event.type) |
flags = SDL_GetModState (); |
118 |
|
|
119 |
|
switch (sdlEvent.type) |
120 |
{ |
{ |
121 |
|
case SDL_MOUSEBUTTONDOWN: |
122 |
|
if (sdlEvent.button.button == SDL_BUTTON_LEFT) |
123 |
|
type = NSLeftMouseDown; |
124 |
|
else if (sdlEvent.button.button == SDL_BUTTON_RIGHT) |
125 |
|
type = NSRightMouseDown; |
126 |
|
else if (sdlEvent.button.button == SDL_BUTTON_MIDDLE) |
127 |
|
type = NSOtherMouseDown; |
128 |
|
|
129 |
|
_event = [NSEvent mouseEventWithType: type |
130 |
|
//location: NSZeroPoint |
131 |
|
location: NSMakePoint ((float)x, (float)y) |
132 |
|
modifierFlags: flags |
133 |
|
timestamp: 0 |
134 |
|
windowNumber: 0 |
135 |
|
context: nil |
136 |
|
eventNumber: 0 |
137 |
|
clickCount: 0 |
138 |
|
pressure: 0.0f]; |
139 |
|
|
140 |
|
break; |
141 |
|
|
142 |
|
case SDL_MOUSEBUTTONUP: |
143 |
|
if (sdlEvent.button.button == SDL_BUTTON_LEFT) |
144 |
|
type = NSLeftMouseUp; |
145 |
|
else if (sdlEvent.button.button == SDL_BUTTON_RIGHT) |
146 |
|
type = NSRightMouseUp; |
147 |
|
else if (sdlEvent.button.button == SDL_BUTTON_MIDDLE) |
148 |
|
type = NSOtherMouseUp; |
149 |
|
|
150 |
|
_event = [NSEvent mouseEventWithType: type |
151 |
|
//location: NSZeroPoint |
152 |
|
location: NSMakePoint ((float)x, (float)y) |
153 |
|
modifierFlags: flags |
154 |
|
timestamp: 0 |
155 |
|
windowNumber: 0 |
156 |
|
context: nil |
157 |
|
eventNumber: 0 |
158 |
|
clickCount: 0 |
159 |
|
pressure: 0.0f]; |
160 |
|
|
161 |
|
break; |
162 |
case SDL_KEYDOWN: |
case SDL_KEYDOWN: |
163 |
[self _handleKeyDown: &event.key.keysym]; |
if (sdlEvent.type) |
164 |
|
type = NSKeyDown; |
165 |
|
|
166 |
|
_event = [NSEvent keyEventWithType: type |
167 |
|
//location: NSZeroPoint |
168 |
|
location: NSMakePoint ((float)x, (float)y) |
169 |
|
modifierFlags: flags |
170 |
|
timestamp: 0 |
171 |
|
windowNumber: 0 |
172 |
|
context: nil |
173 |
|
characters: nil |
174 |
|
charactersIgnoringModifiers: nil |
175 |
|
isARepeat: NO |
176 |
|
keyCode: sdlEvent.key.keysym.sym]; |
177 |
|
|
178 |
|
break; |
179 |
|
case SDL_KEYUP: |
180 |
|
if (sdlEvent.type) |
181 |
|
type = NSKeyUp; |
182 |
|
|
183 |
|
_event = [NSEvent keyEventWithType: type |
184 |
|
//location: NSZeroPoint |
185 |
|
location: NSMakePoint ((float)x, (float)y) |
186 |
|
modifierFlags: flags |
187 |
|
timestamp: 0 |
188 |
|
windowNumber: 0 |
189 |
|
context: nil |
190 |
|
characters: nil |
191 |
|
charactersIgnoringModifiers: nil |
192 |
|
isARepeat: NO |
193 |
|
keyCode: sdlEvent.key.keysym.sym]; |
194 |
|
|
195 |
break; |
break; |
196 |
case SDL_QUIT: |
case SDL_QUIT: |
197 |
exit (0); |
exit (0); |
198 |
break; |
break; |
199 |
|
default: |
200 |
|
_event = nil; |
201 |
|
break; |
202 |
} |
} |
203 |
|
|
204 |
|
if (_event != nil) |
205 |
|
[self processEvents: _event]; |
206 |
} |
} |
207 |
} |
} |
208 |
|
|
209 |
|
/** |
210 |
|
* This function call the display and call the processing of events in |
211 |
|
* an infinite loop |
212 |
|
*/ |
213 |
- (void) mainLoop |
- (void) mainLoop |
214 |
{ |
{ |
215 |
while (1) |
while (1) |
221 |
} |
} |
222 |
} |
} |
223 |
|
|
224 |
|
/** |
225 |
|
* Initialize the display with SDL |
226 |
|
*/ |
227 |
- (void) _sdlInit: (NSSize)screenSize |
- (void) _sdlInit: (NSSize)screenSize |
228 |
{ |
{ |
229 |
if (SDL_Init(SDL_INIT_VIDEO) < 0) |
if (SDL_Init(SDL_INIT_VIDEO) < 0) |
241 |
exit(2); |
exit(2); |
242 |
} |
} |
243 |
|
|
|
/* SDL_GL_SetAttribute (SDL_GL_RED_SIZE, 5); |
|
|
SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, 5); |
|
|
SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, 5);*/ |
|
244 |
SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 16); |
SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 16); |
245 |
SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1); |
SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1); |
246 |
|
|
247 |
glViewport (0, 0, screenSize.width, screenSize.height); |
glViewport (0, 0, screenSize.width, screenSize.height); |
248 |
} |
} |
249 |
|
|
250 |
- (id) initWithScreenSize: (NSSize)size |
/** |
251 |
|
* Initialise an UiSdl object with a screen size |
252 |
|
* @param (NSSize)screenSize the size of the game display |
253 |
|
*/ |
254 |
|
- (id) initWithScreenSize: (NSSize)screenSize |
255 |
{ |
{ |
256 |
|
NSParameterAssert(screenSize.width >= 640); |
257 |
|
NSParameterAssert(screenSize.height >= 480); |
258 |
|
|
259 |
self = [super init]; |
self = [super init]; |
260 |
if (self != nil) |
if (self != nil) |
261 |
{ |
{ |
262 |
[self _sdlInit: size]; |
_event = nil; |
263 |
|
_screenSize = screenSize; |
264 |
|
[self _sdlInit: _screenSize]; |
265 |
} |
} |
266 |
|
|
267 |
return self; |
return self; |
274 |
[super dealloc]; |
[super dealloc]; |
275 |
} |
} |
276 |
|
|
277 |
|
/** |
278 |
|
* Set the delegate with an object |
279 |
|
*/ |
280 |
- (void) setDelegate: (id)delegate |
- (void) setDelegate: (id)delegate |
281 |
{ |
{ |
282 |
NSParameterAssert (delegate); |
NSParameterAssert (delegate); |
289 |
return _delegate; |
return _delegate; |
290 |
} |
} |
291 |
|
|
292 |
|
/** |
293 |
|
* The forward invocation of the UiSdl class call the delegate (an |
294 |
|
* UiFrame object) to respond to the display method call by UiInterface |
295 |
|
*/ |
296 |
- (void) forwardInvocation: (NSInvocation*)objectInvoke |
- (void) forwardInvocation: (NSInvocation*)objectInvoke |
297 |
{ |
{ |
|
// printf("Forward invocation\n"); |
|
|
|
|
298 |
if ([_delegate respondsToSelector: [objectInvoke selector]]) |
if ([_delegate respondsToSelector: [objectInvoke selector]]) |
299 |
return [objectInvoke invokeWithTarget: _delegate]; |
return [objectInvoke invokeWithTarget: _delegate]; |
300 |
else |
else |
301 |
return [self doesNotRecognizeSelector: [objectInvoke selector]]; |
return [self doesNotRecognizeSelector: [objectInvoke selector]]; |
302 |
} |
} |
303 |
|
|
304 |
|
- (NSEvent *) event |
305 |
|
{ |
306 |
|
/* FIXME - return a copy of the NSEvent */ |
307 |
|
return _event; |
308 |
|
} |
309 |
|
|
310 |
|
/** |
311 |
|
* sets the current screen size |
312 |
|
* @param a screen resolution greater or equal then 640x480 |
313 |
|
*/ |
314 |
|
- (void) setScreenSize: (NSSize)screenSize |
315 |
|
{ |
316 |
|
NSParameterAssert(screenSize.width >= 640); |
317 |
|
NSParameterAssert(screenSize.height >= 480); |
318 |
|
|
319 |
|
_screenSize = screenSize; |
320 |
|
} |
321 |
|
|
322 |
|
- (NSSize) screenSize |
323 |
|
{ |
324 |
|
return _screenSize; |
325 |
|
} |
326 |
|
|
327 |
@end |
@end |