ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/MacOSX/EmulatorView.mm
Revision: 1.3
Committed: 2002-05-12T10:34:16Z (22 years, 6 months ago) by nigel
Branch: MAIN
Changes since 1.2: +2 -9 lines
Log Message:
Took out redundant method (was used before event handling re-write)

File Contents

# Content
1 /*
2 * EmulatorView.mm - Custom NSView for Basilisk II graphics output
3 *
4 * $Id: EmulatorView.mm,v 1.2 2002/03/18 11:00:27 nigel Exp $
5 *
6 * Basilisk II (C) 1997-2002 Christian Bauer
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23 #import "sysdeps.h" // Types used in Basilisk C++ code,
24
25 #define DEBUG 0
26 #import <debug.h>
27
28 #import <Cocoa/Cocoa.h>
29
30 #import "main_macosx.h" // For WarningAlert() et al prototypes
31 #import "misc_macosx.h" // For InfoSheet() prototype
32 #import "video_macosx.h" // For init_* globals, and bitmap drawing strategy
33
34 #import "EmulatorView.h"
35
36 @implementation EmulatorView
37
38
39 //
40 // Standard NSView methods that we override
41 //
42
43 - (id) initWithFrame: (NSRect) frameRect
44 {
45 self = [super initWithFrame: frameRect];
46
47 output = self; // Set global for access by Basilisk C++ code
48 // bitmap = nil; // Set by readyToDraw:
49 drawView = NO; // Disable drawing until later
50
51 #ifdef SAVE_GSTATE
52 [self allocateGState];
53 #endif
54
55 return self;
56 }
57
58 - (void) dealloc
59 {
60 #ifdef SAVE_GSTATE
61 [self releaseGState];
62 #endif
63 [super dealloc];
64 }
65
66 // Mouse click in this window. If window is not active, should click be passed to this view?
67 - (BOOL) acceptsFirstMouse: (NSEvent *) event
68 {
69 return [self mouseInView];
70 }
71
72
73 #include <adb.h>
74
75 static int prevFlags;
76
77 - (void) flagsChanged: (NSEvent *) event
78 {
79 int flags = [event modifierFlags];
80
81 if ( (flags & NSAlphaShiftKeyMask) != (prevFlags & NSAlphaShiftKeyMask) )
82 if ( flags & NSAlphaShiftKeyMask )
83 ADBKeyDown(0x39); // CAPS_LOCK
84 else
85 ADBKeyUp(0x39);
86
87 if ( (flags & NSShiftKeyMask) != (prevFlags & NSShiftKeyMask) )
88 if ( flags & NSShiftKeyMask )
89 ADBKeyDown(0x38); // SHIFT_LEFT
90 else
91 ADBKeyUp(0x38);
92
93 if ( (flags & NSControlKeyMask) != (prevFlags & NSControlKeyMask) )
94 if ( flags & NSControlKeyMask )
95 ADBKeyDown(0x36); // CTL_LEFT
96 else
97 ADBKeyUp(0x36);
98
99 if ( (flags & NSAlternateKeyMask) != (prevFlags & NSAlternateKeyMask) )
100 if ( flags & NSAlternateKeyMask )
101 ADBKeyDown(0x3a); // OPTION_LEFT
102 else
103 ADBKeyUp(0x3a);
104
105 if ( (flags & NSCommandKeyMask) != (prevFlags & NSCommandKeyMask) )
106 if ( flags & NSCommandKeyMask )
107 ADBKeyDown(0x37); // APPLE_LEFT
108 else
109 ADBKeyUp(0x37);
110
111 prevFlags = flags;
112 }
113
114 - (BOOL) mouseInView: (NSEvent *) event
115 {
116 NSPoint loc = [event locationInWindow];
117 NSRect box = [self frame];
118 D(NSLog (@"%s - loc.x=%f, loc.y=%f, box.origin.x=%f, box.origin.y=%f",
119 __PRETTY_FUNCTION__, loc.x, loc.y, box.origin.x, box.origin.y));
120 return [self mouse: loc inRect: box];
121 }
122
123 - (BOOL) mouseInView
124 {
125 NSPoint loc = [[self window] mouseLocationOutsideOfEventStream];
126 NSRect box = [self frame];
127 D(NSLog (@"%s - loc.x=%f, loc.y=%f, box.origin.x=%f, box.origin.y=%f",
128 __PRETTY_FUNCTION__, loc.x, loc.y, box.origin.x, box.origin.y));
129 return [self mouse: loc inRect: box];
130 }
131
132
133 //
134 // Custom methods
135 //
136
137 - (void) benchmark
138 {
139 int i;
140 float seconds;
141 NSDate *startDate;
142
143 if ( ! drawView )
144 return;
145
146 drawView = NO;
147 [self lockFocus];
148 startDate = [NSDate date];
149 for (i = 1; i < 300; ++i )
150 #ifdef NSBITMAP
151 [bitmap draw];
152 #endif
153 #ifdef CGIMAGEREF
154 cgDrawInto([self bounds], bitmap);
155 #endif
156 #ifdef CGDRAWBITMAP
157 [self CGDrawBitmap];
158 #endif
159 seconds = -[startDate timeIntervalSinceNow];
160 [self unlockFocus];
161 drawView = YES;
162
163 InfoSheet(@"Benchmark run. 300 frames.",
164 [NSString stringWithFormat:
165 @"%.2f seconds, %.3f frames per second", seconds, i/seconds],
166 @"Thanks", [self window]);
167 }
168
169 // Return a TIFF for a snapshot of the screen image
170 - (NSData *) TIFFrep
171 {
172 #ifdef NSBITMAP
173 return [bitmap TIFFRepresentation];
174 #else
175 WarningAlert("How do I get a TIFF from a CGImageRef?");
176 #endif
177 return nil;
178 }
179
180 // Enable display of, and drawing into, the view
181 #ifdef NSBITMAP
182 - (void) readyToDraw: (NSBitmapImageRep *) theBitmap
183 imageWidth: (short) width
184 imageHeight: (short) height
185 {
186 bitmap = theBitmap;
187 #endif
188 #ifdef CGIMAGEREF
189 - (void) readyToDraw: (CGImageRef) image
190 imageWidth: (short) width
191 imageHeight: (short) height
192 {
193 bitmap = image;
194 #endif
195 #ifdef CGDRAWBITMAP
196 - (void) readyToDraw: (void *) theBitmap
197 width: (short) width
198 height: (short) height
199 bps: (short) bitsPerSample
200 spp: (short) samplesPerPixel
201 bpp: (short) bitsPerPixel
202 bpr: (int) bpr
203 isPlanar: (BOOL) planar
204 hasAlpha: (BOOL) alpha
205 {
206 bitmap = theBitmap;
207 bps = bitsPerSample;
208 spp = samplesPerPixel;
209 bpp = bitsPerPixel;
210 bytesPerRow = bpr;
211 isPlanar = planar;
212 hasAlpha = alpha;
213 #endif
214 x = width, y = height;
215 drawView = YES;
216
217 [[self window] setAcceptsMouseMovedEvents: YES];
218 // [[self window] setInitialFirstResponder: self];
219 [[self window] makeFirstResponder: self];
220 }
221
222 - (void) disableDrawing
223 {
224 drawView = NO;
225 }
226
227 - (short) width
228 {
229 return (short)[self bounds].size.width;
230 }
231
232 - (short) height
233 {
234 return (short)[self bounds].size.height;
235 }
236
237 - (BOOL) isOpaque
238 {
239 return drawView;
240 }
241
242 - (BOOL) processKeyEvent: (NSEvent *) event
243 {
244 if ( [self acceptsFirstMouse: event] || FULLSCREEN )
245 if ( [event isARepeat] )
246 return NO;
247 else
248 return YES;
249
250 [self interpretKeyEvents:[NSArray arrayWithObject:event]];
251 return NO;
252 }
253
254 - (void) keyDown: (NSEvent *) event
255 {
256 if ( [self processKeyEvent: event] )
257 {
258 int code = [event keyCode];
259
260 if ( code == 126 ) code = 0x3e; // CURS_UP
261 if ( code == 125 ) code = 0x3d; // CURS_DOWN
262 if ( code == 124 ) code = 0x3c; // CURS_RIGHT
263 if ( code == 123 ) code = 0x3b; // CURS_LEFT
264
265 ADBKeyDown(code);
266 }
267 }
268
269 - (void) keyUp: (NSEvent *) event
270 {
271 if ( [self processKeyEvent: event] )
272 {
273 int code = [event keyCode];
274
275 if ( code == 126 ) code = 0x3e; // CURS_UP
276 if ( code == 125 ) code = 0x3d; // CURS_DOWN
277 if ( code == 124 ) code = 0x3c; // CURS_RIGHT
278 if ( code == 123 ) code = 0x3b; // CURS_LEFT
279
280 ADBKeyUp(code);
281 }
282 }
283
284 static NSPoint mouse; // Previous/current mouse location
285
286 - (BOOL) processMouseMove: (NSEvent *) event
287 {
288 NSPoint locInView;
289
290 if ( FULLSCREEN )
291 locInView = [NSEvent mouseLocation];
292 else
293 locInView = [self convertPoint: [event locationInWindow] fromView:nil];
294
295 if ( NSEqualPoints(locInView, mouse) )
296 return NO;
297
298 mouse = locInView;
299
300 if ( FULLSCREEN )
301 {
302 ADBMouseMoved((int)mouse.x, screen_height - (int)mouse.y);
303 return YES;
304 }
305
306 #ifdef CAN_RESIZE_VIEW
307 int mouseY = y - y * mouse.y / [self height];
308 int mouseX = x * mouse.x / [self width];
309 #else
310 int mouseY = y - (int) mouse.y;
311 int mouseX = (int) mouse.x;
312 #endif
313
314 ADBMouseMoved(mouseX, mouseY);
315 return YES;
316 }
317
318 - (void) mouseDown: (NSEvent *) event
319 {
320 [self processMouseMove: event];
321 ADBMouseDown(0);
322 }
323
324 - (void) mouseDragged: (NSEvent *) event
325 {
326 [self processMouseMove: event];
327 }
328
329 - (void) mouseMoved: (NSEvent *) event
330 {
331 #if DEBUG
332 if ( ! [self mouseInView] )
333 {
334 NSLog (@"%s - Received event while outside of view", __PRETTY_FUNCTION__);
335 return;
336 }
337 #endif
338 [self processMouseMove: event];
339 }
340
341 - (void) mouseUp: (NSEvent *) event
342 {
343 [self processMouseMove: event];
344 ADBMouseUp(0);
345 }
346
347 #if DEBUG
348 - (void) randomise // Draw some coloured snow in the bitmap
349 {
350 unsigned char *pixel;
351
352 for ( int i = 0; i < 1000; ++i )
353 {
354 pixel = [bitmap bitmapData]
355 + (int) (1.0 * [bitmap bytesPerRow] * 342 //[bitmap height]
356 * rand() / RAND_MAX);
357 *pixel = (unsigned char) (256.0 * rand() / RAND_MAX);
358 }
359 }
360 #endif
361
362 - (void) drawRect: (NSRect) rect
363 {
364 if ( ! drawView ) // If the emulator is still being setup,
365 return; // we do not want to draw
366
367 #if DEBUG
368 NSLog(@"In drawRect");
369 [self randomise];
370 #endif
371
372 #ifdef NSBITMAP
373 NSRectClip(rect);
374 [bitmap draw];
375 #endif
376 #ifdef CGIMAGEREF
377 cgDrawInto(rect, bitmap);
378 #endif
379 #ifdef CGDRAWBITMAP
380 [self CGDrawBitmap];
381 #endif
382 }
383
384 //
385 // Extra drawing stuff
386 //
387
388 #ifdef CGDRAWBITMAP
389 extern "C" void CGDrawBitmap();
390
391 - (void) CGDrawBitmap
392 {
393 CGContextRef cgContext = [[NSGraphicsContext currentContext]
394 graphicsPort];
395 NSRect rect = [self bounds];
396 CGRect cgRect = {
397 {rect.origin.x, rect.origin.y},
398 {rect.size.width, rect.size.height}
399 };
400
401 CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();
402
403
404 CGContextSetShouldAntialias(cgContext, NO); // Seems to have no effect?
405
406 CGDrawBitmap(cgContext, cgRect, x, y, bps, spp, bpp,
407 bytesPerRow, isPlanar, hasAlpha, colourSpace, &bitmap);
408 }
409 #endif
410
411 #ifdef CGIMAGEREF
412 void
413 cgDrawInto(NSRect rect, CGImageRef bitmap)
414 {
415 CGContextRef cgContext = [[NSGraphicsContext currentContext]
416 graphicsPort];
417 CGRect cgRect = {
418 {rect.origin.x, rect.origin.y},
419 {rect.size.width, rect.size.height}
420 };
421
422 CGContextSetShouldAntialias(cgContext, NO); // Seems to have no effect?
423
424 CGContextDrawImage(cgContext, cgRect, bitmap);
425 }
426 #endif
427
428 @end