9722
|
1 |
/* $Id$ */
|
|
2 |
|
|
3 |
/******************************************************************************
|
|
4 |
* Cocoa video driver *
|
|
5 |
* Known things left to do: *
|
|
6 |
* List available resolutions. *
|
|
7 |
******************************************************************************/
|
|
8 |
|
|
9 |
#ifdef WITH_COCOA
|
|
10 |
|
|
11 |
#include <AvailabilityMacros.h>
|
|
12 |
|
|
13 |
#import <Cocoa/Cocoa.h>
|
|
14 |
#import <sys/time.h> /* gettimeofday */
|
|
15 |
#import <sys/param.h> /* for MAXPATHLEN */
|
|
16 |
#import <unistd.h>
|
|
17 |
|
|
18 |
/**
|
|
19 |
* Important notice regarding all modifications!!!!!!!
|
|
20 |
* There are certain limitations because the file is objective C++.
|
|
21 |
* gdb has limitations.
|
|
22 |
* C++ and objective C code can't be joined in all cases (classes stuff).
|
|
23 |
* Read http://developer.apple.com/releasenotes/Cocoa/Objective-C++.html for more information.
|
|
24 |
*/
|
|
25 |
|
|
26 |
|
|
27 |
/* Defined in stdbool.h */
|
|
28 |
#ifndef __cplusplus
|
|
29 |
# ifndef __BEOS__
|
|
30 |
# undef bool
|
|
31 |
# undef false
|
|
32 |
# undef true
|
|
33 |
# endif
|
|
34 |
#endif
|
|
35 |
|
|
36 |
|
|
37 |
#include "../../stdafx.h"
|
|
38 |
#include "../../openttd.h"
|
|
39 |
#include "../../debug.h"
|
|
40 |
#include "../../macros.h"
|
|
41 |
#include "../../os/macosx/splash.h"
|
|
42 |
#include "../../variables.h"
|
|
43 |
#include "../../gfx.h"
|
|
44 |
#include "cocoa_v.h"
|
|
45 |
#include "cocoa_keys.h"
|
|
46 |
#include "../../blitter/factory.hpp"
|
|
47 |
#include "../../fileio.h"
|
|
48 |
|
|
49 |
#undef Point
|
|
50 |
#undef Rect
|
|
51 |
|
|
52 |
|
|
53 |
class WindowQuickdrawSubdriver;
|
|
54 |
|
|
55 |
|
|
56 |
/* Subclass of NSWindow to fix genie effect and support resize events */
|
|
57 |
@interface OTTD_QuickdrawWindow : NSWindow {
|
|
58 |
WindowQuickdrawSubdriver *driver;
|
|
59 |
}
|
|
60 |
|
|
61 |
- (void)setDriver:(WindowQuickdrawSubdriver*)drv;
|
|
62 |
|
|
63 |
- (void)miniaturize:(id)sender;
|
|
64 |
- (void)display;
|
|
65 |
- (void)setFrame:(NSRect)frameRect display:(BOOL)flag;
|
|
66 |
- (void)appDidHide:(NSNotification*)note;
|
|
67 |
- (void)appWillUnhide:(NSNotification*)note;
|
|
68 |
- (void)appDidUnhide:(NSNotification*)note;
|
|
69 |
- (id)initWithContentRect:(NSRect)contentRect styleMask:(unsigned int)styleMask backing:(NSBackingStoreType)backingType defer:(BOOL)flag;
|
|
70 |
@end
|
|
71 |
|
|
72 |
/* Delegate for our NSWindow to send ask for quit on close */
|
|
73 |
@interface OTTD_QuickdrawWindowDelegate : NSObject{
|
|
74 |
WindowQuickdrawSubdriver *driver;
|
|
75 |
}
|
|
76 |
|
|
77 |
- (void)setDriver:(WindowQuickdrawSubdriver*)drv;
|
|
78 |
|
|
79 |
- (BOOL)windowShouldClose:(id)sender;
|
|
80 |
@end
|
|
81 |
|
|
82 |
class WindowQuickdrawSubdriver: public CocoaSubdriver {
|
|
83 |
int device_width;
|
|
84 |
int device_height;
|
|
85 |
int device_depth;
|
|
86 |
|
|
87 |
int window_width;
|
|
88 |
int window_height;
|
|
89 |
int window_pitch;
|
|
90 |
|
|
91 |
int buffer_depth;
|
|
92 |
|
|
93 |
void *pixel_buffer;
|
|
94 |
void *window_buffer;
|
|
95 |
|
|
96 |
OTTD_QuickdrawWindow *window;
|
|
97 |
|
|
98 |
#define MAX_DIRTY_RECTS 100
|
|
99 |
Rect dirty_rects[MAX_DIRTY_RECTS];
|
|
100 |
int num_dirty_rects;
|
|
101 |
|
|
102 |
uint16 palette16[256];
|
|
103 |
uint32 palette32[256];
|
|
104 |
|
|
105 |
public:
|
|
106 |
bool active;
|
|
107 |
bool setup;
|
|
108 |
|
|
109 |
NSQuickDrawView *qdview;
|
|
110 |
|
|
111 |
private:
|
|
112 |
void GetDeviceInfo();
|
|
113 |
|
|
114 |
bool SetVideoMode(int width, int height);
|
|
115 |
|
|
116 |
/**
|
|
117 |
* This function copies 32bpp pixels from the screen buffer in 16bpp windowed mode.
|
|
118 |
*
|
|
119 |
* @param left The x coord for the left edge of the box to blit.
|
|
120 |
* @param top The y coord for the top edge of the box to blit.
|
|
121 |
* @param right The x coord for the right edge of the box to blit.
|
|
122 |
* @param bottom The y coord for the bottom edge of the box to blit.
|
|
123 |
*/
|
|
124 |
void Blit32ToView32(int left, int top, int right, int bottom);
|
|
125 |
|
|
126 |
/**
|
|
127 |
* This function copies 8bpp pixels from the screen buffer in 32bpp windowed mode.
|
|
128 |
*
|
|
129 |
* @param left The x coord for the left edge of the box to blit.
|
|
130 |
* @param top The y coord for the top edge of the box to blit.
|
|
131 |
* @param right The x coord for the right edge of the box to blit.
|
|
132 |
* @param bottom The y coord for the bottom edge of the box to blit.
|
|
133 |
*/
|
|
134 |
void BlitIndexedToView32(int left, int top, int right, int bottom);
|
|
135 |
|
|
136 |
/**
|
|
137 |
* This function copies 8bpp pixels from the screen buffer in 16bpp windowed mode.
|
|
138 |
*
|
|
139 |
* @param left The x coord for the left edge of the box to blit.
|
|
140 |
* @param top The y coord for the top edge of the box to blit.
|
|
141 |
* @param right The x coord for the right edge of the box to blit.
|
|
142 |
* @param bottom The y coord for the bottom edge of the box to blit.
|
|
143 |
*/
|
|
144 |
void BlitIndexedToView16(int left, int top, int right, int bottom);
|
|
145 |
|
|
146 |
inline void BlitToView(int left, int top, int right, int bottom);
|
|
147 |
void DrawResizeIcon();
|
|
148 |
|
|
149 |
|
|
150 |
public:
|
|
151 |
WindowQuickdrawSubdriver(int bpp);
|
|
152 |
virtual ~WindowQuickdrawSubdriver();
|
|
153 |
|
|
154 |
virtual void Draw();
|
|
155 |
virtual void MakeDirty(int left, int top, int width, int height);
|
|
156 |
virtual void UpdatePalette(uint first_color, uint num_colors);
|
|
157 |
|
|
158 |
virtual uint ListModes(OTTDPoint* modes, uint max_modes);
|
|
159 |
|
|
160 |
virtual bool ChangeResolution(int w, int h);
|
|
161 |
|
|
162 |
virtual bool IsFullscreen() { return false; }
|
|
163 |
|
|
164 |
virtual int GetWidth() { return window_width; }
|
|
165 |
virtual int GetHeight() { return window_height; }
|
|
166 |
virtual void *GetPixelBuffer() { return pixel_buffer; }
|
|
167 |
|
|
168 |
/* Convert local coordinate to window server (CoreGraphics) coordinate */
|
|
169 |
virtual CGPoint PrivateLocalToCG(NSPoint* p);
|
|
170 |
|
|
171 |
virtual NSPoint GetMouseLocation(NSEvent *event);
|
|
172 |
virtual bool MouseIsInsideView(NSPoint *pt);
|
|
173 |
|
|
174 |
virtual bool IsActive() { return active; }
|
|
175 |
|
|
176 |
|
|
177 |
void SetPortAlphaOpaque();
|
|
178 |
bool WindowResized();
|
|
179 |
};
|
|
180 |
|
|
181 |
|
|
182 |
@implementation OTTD_QuickdrawWindow
|
|
183 |
|
|
184 |
- (void)setDriver:(WindowQuickdrawSubdriver*)drv
|
|
185 |
{
|
|
186 |
driver = drv;
|
|
187 |
}
|
|
188 |
|
|
189 |
|
|
190 |
/* we override these methods to fix the miniaturize animation/dock icon bug */
|
|
191 |
- (void)miniaturize:(id)sender
|
|
192 |
{
|
|
193 |
/* make the alpha channel opaque so anim won't have holes in it */
|
|
194 |
driver->SetPortAlphaOpaque ();
|
|
195 |
|
|
196 |
/* window is hidden now */
|
|
197 |
driver->active = false;
|
|
198 |
|
|
199 |
QZ_ShowMouse();
|
|
200 |
|
|
201 |
[ super miniaturize:sender ];
|
|
202 |
}
|
|
203 |
|
|
204 |
- (void)display
|
|
205 |
{
|
|
206 |
/* This method fires just before the window deminaturizes from the Dock.
|
|
207 |
* We'll save the current visible surface, let the window manager redraw any
|
|
208 |
* UI elements, and restore the surface. This way, no expose event
|
|
209 |
* is required, and the deminiaturize works perfectly.
|
|
210 |
*/
|
|
211 |
|
|
212 |
driver->SetPortAlphaOpaque();
|
|
213 |
|
|
214 |
/* save current visible surface */
|
|
215 |
[ self cacheImageInRect:[ driver->qdview frame ] ];
|
|
216 |
|
|
217 |
/* let the window manager redraw controls, border, etc */
|
|
218 |
[ super display ];
|
|
219 |
|
|
220 |
/* restore visible surface */
|
|
221 |
[ self restoreCachedImage ];
|
|
222 |
|
|
223 |
/* window is visible again */
|
|
224 |
driver->active = true;
|
|
225 |
}
|
|
226 |
|
|
227 |
- (void)setFrame:(NSRect)frameRect display:(BOOL)flag
|
|
228 |
{
|
|
229 |
[ super setFrame:frameRect display:flag ];
|
|
230 |
|
|
231 |
/* Don't do anything if the window is currently being created */
|
|
232 |
if (driver->setup) return;
|
|
233 |
|
|
234 |
if (!driver->WindowResized())
|
|
235 |
error("Cocoa: Failed to resize window.");
|
|
236 |
}
|
|
237 |
|
|
238 |
- (void)appDidHide:(NSNotification*)note
|
|
239 |
{
|
|
240 |
driver->active = false;
|
|
241 |
}
|
|
242 |
|
|
243 |
|
|
244 |
- (void)appWillUnhide:(NSNotification*)note
|
|
245 |
{
|
|
246 |
driver->SetPortAlphaOpaque ();
|
|
247 |
|
|
248 |
/* save current visible surface */
|
|
249 |
[ self cacheImageInRect:[ driver->qdview frame ] ];
|
|
250 |
}
|
|
251 |
|
|
252 |
- (void)appDidUnhide:(NSNotification*)note
|
|
253 |
{
|
|
254 |
/* restore cached image, since it may not be current, post expose event too */
|
|
255 |
[ self restoreCachedImage ];
|
|
256 |
|
|
257 |
driver->active = true;
|
|
258 |
}
|
|
259 |
|
|
260 |
|
|
261 |
- (id)initWithContentRect:(NSRect)contentRect styleMask:(unsigned int)styleMask backing:(NSBackingStoreType)backingType defer:(BOOL)flag
|
|
262 |
{
|
|
263 |
/* Make our window subclass receive these application notifications */
|
|
264 |
[ [ NSNotificationCenter defaultCenter ] addObserver:self
|
|
265 |
selector:@selector(appDidHide:) name:NSApplicationDidHideNotification object:NSApp ];
|
|
266 |
|
|
267 |
[ [ NSNotificationCenter defaultCenter ] addObserver:self
|
|
268 |
selector:@selector(appDidUnhide:) name:NSApplicationDidUnhideNotification object:NSApp ];
|
|
269 |
|
|
270 |
[ [ NSNotificationCenter defaultCenter ] addObserver:self
|
|
271 |
selector:@selector(appWillUnhide:) name:NSApplicationWillUnhideNotification object:NSApp ];
|
|
272 |
|
|
273 |
return [ super initWithContentRect:contentRect styleMask:styleMask backing:backingType defer:flag ];
|
|
274 |
}
|
|
275 |
|
|
276 |
@end
|
|
277 |
|
|
278 |
@implementation OTTD_QuickdrawWindowDelegate
|
|
279 |
- (void)setDriver:(WindowQuickdrawSubdriver*)drv
|
|
280 |
{
|
|
281 |
driver = drv;
|
|
282 |
}
|
|
283 |
|
|
284 |
- (BOOL)windowShouldClose:(id)sender
|
|
285 |
{
|
|
286 |
HandleExitGameRequest();
|
|
287 |
|
|
288 |
return NO;
|
|
289 |
}
|
|
290 |
|
|
291 |
- (void)windowDidBecomeKey:(NSNotification*)aNotification
|
|
292 |
{
|
|
293 |
driver->active = true;
|
|
294 |
}
|
|
295 |
|
|
296 |
- (void)windowDidResignKey:(NSNotification*)aNotification
|
|
297 |
{
|
|
298 |
driver->active = false;
|
|
299 |
}
|
|
300 |
|
|
301 |
- (void)windowDidBecomeMain:(NSNotification*)aNotification
|
|
302 |
{
|
|
303 |
driver->active = true;
|
|
304 |
}
|
|
305 |
|
|
306 |
- (void)windowDidResignMain:(NSNotification*)aNotification
|
|
307 |
{
|
|
308 |
driver->active = false;
|
|
309 |
}
|
|
310 |
|
|
311 |
@end
|
|
312 |
|
|
313 |
|
|
314 |
static const int _resize_icon_width = 16;
|
|
315 |
static const int _resize_icon_height = 16;
|
|
316 |
|
|
317 |
static bool _resize_icon[] = {
|
|
318 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
|
|
319 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,
|
|
320 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0,
|
|
321 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0,
|
|
322 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1,
|
|
323 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1,
|
|
324 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0,
|
|
325 |
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0,
|
|
326 |
0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1,
|
|
327 |
0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1,
|
|
328 |
0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0,
|
|
329 |
0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0,
|
|
330 |
0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1,
|
|
331 |
0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1,
|
|
332 |
0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0,
|
|
333 |
1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0
|
|
334 |
};
|
|
335 |
|
|
336 |
|
|
337 |
|
|
338 |
extern const char _openttd_revision[];
|
|
339 |
|
|
340 |
|
|
341 |
void WindowQuickdrawSubdriver::GetDeviceInfo()
|
|
342 |
{
|
|
343 |
CFDictionaryRef cur_mode;
|
|
344 |
|
|
345 |
/* Initialize the video settings; this data persists between mode switches */
|
|
346 |
cur_mode = CGDisplayCurrentMode(kCGDirectMainDisplay);
|
|
347 |
|
|
348 |
/* Gather some information that is useful to know about the display */
|
|
349 |
CFNumberGetValue(
|
|
350 |
(const __CFNumber*)CFDictionaryGetValue(cur_mode, kCGDisplayBitsPerPixel),
|
|
351 |
kCFNumberSInt32Type, &device_depth
|
|
352 |
);
|
|
353 |
|
|
354 |
CFNumberGetValue(
|
|
355 |
(const __CFNumber*)CFDictionaryGetValue(cur_mode, kCGDisplayWidth),
|
|
356 |
kCFNumberSInt32Type, &device_width
|
|
357 |
);
|
|
358 |
|
|
359 |
CFNumberGetValue(
|
|
360 |
(const __CFNumber*)CFDictionaryGetValue(cur_mode, kCGDisplayHeight),
|
|
361 |
kCFNumberSInt32Type, &device_height
|
|
362 |
);
|
|
363 |
}
|
|
364 |
|
|
365 |
bool WindowQuickdrawSubdriver::SetVideoMode(int width, int height)
|
|
366 |
{
|
|
367 |
char caption[50];
|
|
368 |
NSString *nsscaption;
|
|
369 |
unsigned int style;
|
|
370 |
NSRect contentRect;
|
|
371 |
BOOL isCustom = NO;
|
|
372 |
bool ret;
|
|
373 |
|
|
374 |
setup = true;
|
|
375 |
|
|
376 |
GetDeviceInfo();
|
|
377 |
|
|
378 |
if (buffer_depth > device_depth) {
|
|
379 |
DEBUG(driver, 0, "Cannot use a blitter with a higer screen depth than the display when running in windowed mode.");
|
|
380 |
setup = false;
|
|
381 |
return false;
|
|
382 |
}
|
|
383 |
|
|
384 |
if (width > device_width)
|
|
385 |
width = device_width;
|
|
386 |
if (height > device_height)
|
|
387 |
height = device_height;
|
|
388 |
|
|
389 |
contentRect = NSMakeRect(0, 0, width, height);
|
|
390 |
|
|
391 |
/* Check if we should recreate the window */
|
|
392 |
if (window == nil) {
|
|
393 |
OTTD_QuickdrawWindowDelegate *delegate;
|
|
394 |
|
|
395 |
/* Set the window style */
|
|
396 |
style = NSTitledWindowMask;
|
|
397 |
style |= (NSMiniaturizableWindowMask | NSClosableWindowMask);
|
|
398 |
style |= NSResizableWindowMask;
|
|
399 |
|
|
400 |
/* Manually create a window, avoids having a nib file resource */
|
|
401 |
window = [ [ OTTD_QuickdrawWindow alloc ]
|
|
402 |
initWithContentRect:contentRect
|
|
403 |
styleMask:style
|
|
404 |
backing:NSBackingStoreBuffered
|
|
405 |
defer:NO ];
|
|
406 |
|
|
407 |
if (window == nil) {
|
|
408 |
DEBUG(driver, 0, "Could not create the Cocoa window.");
|
|
409 |
setup = false;
|
|
410 |
return false;
|
|
411 |
}
|
|
412 |
|
|
413 |
[ window setDriver:this ];
|
|
414 |
|
|
415 |
snprintf(caption, sizeof(caption), "OpenTTD %s", _openttd_revision);
|
|
416 |
nsscaption = [ [ NSString alloc ] initWithCString:caption ];
|
|
417 |
[ window setTitle:nsscaption ];
|
|
418 |
[ window setMiniwindowTitle:nsscaption ];
|
|
419 |
[ nsscaption release ];
|
|
420 |
|
|
421 |
[ window setAcceptsMouseMovedEvents:YES ];
|
|
422 |
[ window setViewsNeedDisplay:NO ];
|
|
423 |
|
|
424 |
delegate = [ [ OTTD_QuickdrawWindowDelegate alloc ] init ];
|
|
425 |
[ delegate setDriver:this ];
|
|
426 |
[ window setDelegate: [ delegate autorelease ] ];
|
|
427 |
} else {
|
|
428 |
/* We already have a window, just change its size */
|
|
429 |
if (!isCustom) {
|
|
430 |
[ window setContentSize:contentRect.size ];
|
|
431 |
// Ensure frame height - title bar height >= view height
|
|
432 |
contentRect.size.height = Clamp(height, 0, [ window frame ].size.height - 22 /* 22 is the height of title bar of window*/);
|
|
433 |
height = contentRect.size.height;
|
|
434 |
[ qdview setFrameSize:contentRect.size ];
|
|
435 |
}
|
|
436 |
}
|
|
437 |
|
|
438 |
// Update again
|
|
439 |
window_width = width;
|
|
440 |
window_height = height;
|
|
441 |
|
|
442 |
[ window center ];
|
|
443 |
|
|
444 |
/* Only recreate the view if it doesn't already exist */
|
|
445 |
if (qdview == nil) {
|
|
446 |
qdview = [ [ NSQuickDrawView alloc ] initWithFrame:contentRect ];
|
|
447 |
if (qdview == nil) {
|
|
448 |
DEBUG(driver, 0, "Could not create the Quickdraw view.");
|
|
449 |
setup = false;
|
|
450 |
return false;
|
|
451 |
}
|
|
452 |
|
|
453 |
[ qdview setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable ];
|
|
454 |
[ [ window contentView ] addSubview:qdview ];
|
|
455 |
[ qdview release ];
|
|
456 |
[ window makeKeyAndOrderFront:nil ];
|
|
457 |
}
|
|
458 |
|
|
459 |
ret = WindowResized();
|
|
460 |
|
|
461 |
UpdatePalette(0, 256);
|
|
462 |
|
|
463 |
setup = false;
|
|
464 |
|
|
465 |
return ret;
|
|
466 |
}
|
|
467 |
|
|
468 |
void WindowQuickdrawSubdriver::Blit32ToView32(int left, int top, int right, int bottom)
|
|
469 |
{
|
|
470 |
const uint32* src = (uint32*)pixel_buffer;
|
|
471 |
uint32* dst = (uint32*)window_buffer;
|
|
472 |
uint width = window_width;
|
|
473 |
uint pitch = window_pitch / 4;
|
|
474 |
int y;
|
|
475 |
|
|
476 |
|
|
477 |
dst += top * pitch + left;
|
|
478 |
src += top * width + left;
|
|
479 |
|
|
480 |
for (y = top; y < bottom; y++, dst+= pitch, src+= width) {
|
|
481 |
memcpy(dst, src, (right - left) * 4);
|
|
482 |
}
|
|
483 |
}
|
|
484 |
|
|
485 |
void WindowQuickdrawSubdriver::BlitIndexedToView32(int left, int top, int right, int bottom)
|
|
486 |
{
|
|
487 |
const uint32* pal = palette32;
|
|
488 |
const uint8* src = (uint8*)pixel_buffer;
|
|
489 |
uint32* dst = (uint32*)window_buffer;
|
|
490 |
uint width = window_width;
|
|
491 |
uint pitch = window_pitch / 4;
|
|
492 |
int x;
|
|
493 |
int y;
|
|
494 |
|
|
495 |
for (y = top; y < bottom; y++) {
|
|
496 |
for (x = left; x < right; x++) {
|
|
497 |
dst[y * pitch + x] = pal[src[y * width + x]];
|
|
498 |
}
|
|
499 |
}
|
|
500 |
}
|
|
501 |
|
|
502 |
void WindowQuickdrawSubdriver::BlitIndexedToView16(int left, int top, int right, int bottom)
|
|
503 |
{
|
|
504 |
const uint16* pal = palette16;
|
|
505 |
const uint8* src = (uint8*)pixel_buffer;
|
|
506 |
uint16* dst = (uint16*)window_buffer;
|
|
507 |
uint width = window_width;
|
|
508 |
uint pitch = window_pitch / 2;
|
|
509 |
int x;
|
|
510 |
int y;
|
|
511 |
|
|
512 |
for (y = top; y < bottom; y++) {
|
|
513 |
for (x = left; x < right; x++) {
|
|
514 |
dst[y * pitch + x] = pal[src[y * width + x]];
|
|
515 |
}
|
|
516 |
}
|
|
517 |
}
|
|
518 |
|
|
519 |
|
|
520 |
inline void WindowQuickdrawSubdriver::BlitToView(int left, int top, int right, int bottom)
|
|
521 |
{
|
|
522 |
switch (device_depth) {
|
|
523 |
case 32:
|
|
524 |
switch(buffer_depth) {
|
|
525 |
case 32:
|
|
526 |
Blit32ToView32(left, top, right, bottom);
|
|
527 |
break;
|
|
528 |
case 8:
|
|
529 |
BlitIndexedToView32(left, top, right, bottom);
|
|
530 |
break;
|
|
531 |
}
|
|
532 |
break;
|
|
533 |
case 16:
|
|
534 |
BlitIndexedToView16(left, top, right, bottom);
|
|
535 |
break;
|
|
536 |
}
|
|
537 |
}
|
|
538 |
|
|
539 |
void WindowQuickdrawSubdriver::DrawResizeIcon()
|
|
540 |
{
|
|
541 |
int xoff = window_width - _resize_icon_width;
|
|
542 |
int yoff = window_height - _resize_icon_height;
|
|
543 |
int x;
|
|
544 |
int y;
|
|
545 |
|
|
546 |
switch (device_depth) {
|
|
547 |
case 32:
|
|
548 |
for (y = 0; y < _resize_icon_height; y++) {
|
|
549 |
uint32* trg = (uint32*)window_buffer + (yoff + y) * window_pitch / 4 + xoff;
|
|
550 |
|
|
551 |
for (x = 0; x < _resize_icon_width; x++, trg++) {
|
|
552 |
if (_resize_icon[y * _resize_icon_width + x]) *trg = 0xff000000;
|
|
553 |
}
|
|
554 |
}
|
|
555 |
break;
|
|
556 |
case 16:
|
|
557 |
for (y = 0; y < _resize_icon_height; y++) {
|
|
558 |
uint16* trg = (uint16*)window_buffer + (yoff + y) * window_pitch / 2 + xoff;
|
|
559 |
|
|
560 |
for (x = 0; x < _resize_icon_width; x++, trg++) {
|
|
561 |
if (_resize_icon[y * _resize_icon_width + x]) *trg = 0x0000;
|
|
562 |
}
|
|
563 |
}
|
|
564 |
break;
|
|
565 |
}
|
|
566 |
}
|
|
567 |
|
|
568 |
|
|
569 |
WindowQuickdrawSubdriver::WindowQuickdrawSubdriver(int bpp)
|
|
570 |
{
|
|
571 |
window_width = 0;
|
|
572 |
window_height = 0;
|
|
573 |
buffer_depth = bpp;
|
|
574 |
pixel_buffer = NULL;
|
|
575 |
active = false;
|
|
576 |
setup = false;
|
|
577 |
|
|
578 |
window = nil;
|
|
579 |
qdview = nil;
|
|
580 |
|
|
581 |
num_dirty_rects = MAX_DIRTY_RECTS;
|
|
582 |
}
|
|
583 |
|
|
584 |
WindowQuickdrawSubdriver::~WindowQuickdrawSubdriver()
|
|
585 |
{
|
|
586 |
QZ_ShowMouse();
|
|
587 |
|
|
588 |
/* Release window mode resources */
|
|
589 |
if (window != nil) [ window close ];
|
|
590 |
|
|
591 |
free(pixel_buffer);
|
|
592 |
}
|
|
593 |
|
|
594 |
void WindowQuickdrawSubdriver::Draw()
|
|
595 |
{
|
|
596 |
int i;
|
|
597 |
RgnHandle dirty, temp;
|
|
598 |
|
|
599 |
/* Check if we need to do anything */
|
|
600 |
if (num_dirty_rects == 0 ||
|
|
601 |
[ window isMiniaturized ]) {
|
|
602 |
return;
|
|
603 |
}
|
|
604 |
|
|
605 |
if (num_dirty_rects >= MAX_DIRTY_RECTS) {
|
|
606 |
num_dirty_rects = 1;
|
|
607 |
dirty_rects[0].left = 0;
|
|
608 |
dirty_rects[0].top = 0;
|
|
609 |
dirty_rects[0].right = window_width;
|
|
610 |
dirty_rects[0].bottom = window_height;
|
|
611 |
}
|
|
612 |
|
|
613 |
dirty = NewRgn();
|
|
614 |
temp = NewRgn();
|
|
615 |
|
|
616 |
SetEmptyRgn(dirty);
|
|
617 |
|
|
618 |
/* Build the region of dirty rectangles */
|
|
619 |
for (i = 0; i < num_dirty_rects; i++) {
|
|
620 |
BlitToView(
|
|
621 |
dirty_rects[i].left,
|
|
622 |
dirty_rects[i].top,
|
|
623 |
dirty_rects[i].right,
|
|
624 |
dirty_rects[i].bottom
|
|
625 |
);
|
|
626 |
|
|
627 |
MacSetRectRgn(
|
|
628 |
temp,
|
|
629 |
dirty_rects[i].left,
|
|
630 |
dirty_rects[i].top,
|
|
631 |
dirty_rects[i].right,
|
|
632 |
dirty_rects[i].bottom
|
|
633 |
);
|
|
634 |
MacUnionRgn(dirty, temp, dirty);
|
|
635 |
}
|
|
636 |
|
|
637 |
DrawResizeIcon();
|
|
638 |
|
|
639 |
/* Flush the dirty region */
|
|
640 |
QDFlushPortBuffer( (OpaqueGrafPtr*) [ qdview qdPort ], dirty);
|
|
641 |
DisposeRgn(dirty);
|
|
642 |
DisposeRgn(temp);
|
|
643 |
|
|
644 |
num_dirty_rects = 0;
|
|
645 |
}
|
|
646 |
|
|
647 |
void WindowQuickdrawSubdriver::MakeDirty(int left, int top, int width, int height)
|
|
648 |
{
|
|
649 |
if (num_dirty_rects < MAX_DIRTY_RECTS) {
|
|
650 |
dirty_rects[num_dirty_rects].left = left;
|
|
651 |
dirty_rects[num_dirty_rects].top = top;
|
|
652 |
dirty_rects[num_dirty_rects].right = left + width;
|
|
653 |
dirty_rects[num_dirty_rects].bottom = top + height;
|
|
654 |
}
|
|
655 |
num_dirty_rects++;
|
|
656 |
}
|
|
657 |
|
|
658 |
void WindowQuickdrawSubdriver::UpdatePalette(uint first_color, uint num_colors)
|
|
659 |
{
|
|
660 |
uint i;
|
|
661 |
|
|
662 |
if (buffer_depth != 8)
|
|
663 |
return;
|
|
664 |
|
|
665 |
switch (device_depth) {
|
|
666 |
case 32:
|
|
667 |
for (i = first_color; i < first_color + num_colors; i++) {
|
|
668 |
uint32 clr32 = 0xff000000;
|
|
669 |
clr32 |= (uint32)_cur_palette[i].r << 16;
|
|
670 |
clr32 |= (uint32)_cur_palette[i].g << 8;
|
|
671 |
clr32 |= (uint32)_cur_palette[i].b;
|
|
672 |
palette32[i] = clr32;
|
|
673 |
}
|
|
674 |
break;
|
|
675 |
case 16:
|
|
676 |
for (i = first_color; i < first_color + num_colors; i++) {
|
|
677 |
uint16 clr16 = 0x0000;
|
|
678 |
clr16 |= (uint16)((_cur_palette[i].r >> 3) & 0x1f) << 10;
|
|
679 |
clr16 |= (uint16)((_cur_palette[i].g >> 3) & 0x1f) << 5;
|
|
680 |
clr16 |= (uint16)((_cur_palette[i].b >> 3) & 0x1f);
|
|
681 |
palette16[i] = clr16;
|
|
682 |
}
|
|
683 |
break;
|
|
684 |
}
|
|
685 |
|
|
686 |
num_dirty_rects = MAX_DIRTY_RECTS;
|
|
687 |
}
|
|
688 |
|
|
689 |
uint WindowQuickdrawSubdriver::ListModes(OTTDPoint* modes, uint max_modes)
|
|
690 |
{
|
|
691 |
if (max_modes == 0) return 0;
|
|
692 |
|
|
693 |
modes[0].x = window_width;
|
|
694 |
modes[0].y = window_height;
|
|
695 |
|
|
696 |
return 1;
|
|
697 |
}
|
|
698 |
|
|
699 |
bool WindowQuickdrawSubdriver::ChangeResolution(int w, int h)
|
|
700 |
{
|
|
701 |
int old_width = window_width;
|
|
702 |
int old_height = window_height;
|
|
703 |
|
|
704 |
if (SetVideoMode(w, h))
|
|
705 |
return true;
|
|
706 |
|
|
707 |
if (old_width != 0 && old_height != 0)
|
|
708 |
SetVideoMode(old_width, old_height);
|
|
709 |
|
|
710 |
return false;
|
|
711 |
}
|
|
712 |
|
|
713 |
/* Convert local coordinate to window server (CoreGraphics) coordinate */
|
|
714 |
CGPoint WindowQuickdrawSubdriver::PrivateLocalToCG(NSPoint* p)
|
|
715 |
{
|
|
716 |
CGPoint cgp;
|
|
717 |
|
|
718 |
*p = [ qdview convertPoint:*p toView: nil ];
|
|
719 |
*p = [ window convertBaseToScreen:*p ];
|
|
720 |
p->y = device_height - p->y;
|
|
721 |
|
|
722 |
cgp.x = p->x;
|
|
723 |
cgp.y = p->y;
|
|
724 |
|
|
725 |
return cgp;
|
|
726 |
}
|
|
727 |
|
|
728 |
NSPoint WindowQuickdrawSubdriver::GetMouseLocation(NSEvent *event)
|
|
729 |
{
|
|
730 |
NSPoint pt;
|
|
731 |
|
|
732 |
pt = [ event locationInWindow ];
|
|
733 |
pt = [ qdview convertPoint:pt fromView:nil ];
|
|
734 |
|
|
735 |
return pt;
|
|
736 |
}
|
|
737 |
|
|
738 |
bool WindowQuickdrawSubdriver::MouseIsInsideView(NSPoint *pt)
|
|
739 |
{
|
|
740 |
return [ qdview mouse:*pt inRect:[ qdview bounds ] ];
|
|
741 |
}
|
|
742 |
|
|
743 |
|
|
744 |
/* This function makes the *game region* of the window 100% opaque.
|
|
745 |
* The genie effect uses the alpha component. Otherwise,
|
|
746 |
* it doesn't seem to matter what value it has.
|
|
747 |
*/
|
|
748 |
void WindowQuickdrawSubdriver::SetPortAlphaOpaque()
|
|
749 |
{
|
|
750 |
if (device_depth != 32)
|
|
751 |
return;
|
|
752 |
|
|
753 |
uint32* pixels = (uint32*)window_buffer;
|
|
754 |
uint32 pitch = window_pitch / 4;
|
|
755 |
int x, y;
|
|
756 |
|
|
757 |
for (y = 0; y < window_height; y++)
|
|
758 |
for (x = 0; x < window_width; x++) {
|
|
759 |
pixels[y * pitch + x] |= 0xFF000000;
|
|
760 |
}
|
|
761 |
}
|
|
762 |
|
|
763 |
bool WindowQuickdrawSubdriver::WindowResized()
|
|
764 |
{
|
|
765 |
if (window == nil || qdview == nil) return true;
|
|
766 |
|
|
767 |
NSRect newframe = [ qdview frame ];
|
|
768 |
CGrafPtr thePort = (OpaqueGrafPtr*) [ qdview qdPort ];
|
|
769 |
int voff, hoff;
|
|
770 |
|
|
771 |
LockPortBits(thePort);
|
|
772 |
window_buffer = GetPixBaseAddr(GetPortPixMap(thePort));
|
|
773 |
window_pitch = GetPixRowBytes(GetPortPixMap(thePort));
|
|
774 |
UnlockPortBits(thePort);
|
|
775 |
|
|
776 |
/* _cocoa_video_data.realpixels now points to the window's pixels
|
|
777 |
* We want it to point to the *view's* pixels
|
|
778 |
*/
|
|
779 |
voff = [ window frame ].size.height - newframe.size.height - newframe.origin.y;
|
|
780 |
hoff = [ qdview frame ].origin.x;
|
|
781 |
window_buffer = (uint8*)window_buffer + (voff * window_pitch) + hoff * (device_depth / 8);
|
|
782 |
|
|
783 |
window_width = newframe.size.width;
|
|
784 |
window_height = newframe.size.height;
|
|
785 |
|
|
786 |
free(pixel_buffer);
|
|
787 |
pixel_buffer = malloc(window_width * window_height * buffer_depth / 8);
|
|
788 |
if (pixel_buffer == NULL) {
|
|
789 |
DEBUG(driver, 0, "Failed to allocate pixel buffer");
|
|
790 |
return false;
|
|
791 |
}
|
|
792 |
|
|
793 |
QZ_GameSizeChanged();
|
|
794 |
|
|
795 |
/* Redraw screen */
|
|
796 |
num_dirty_rects = MAX_DIRTY_RECTS;
|
|
797 |
|
|
798 |
return true;
|
|
799 |
}
|
|
800 |
|
|
801 |
|
|
802 |
CocoaSubdriver *QZ_CreateWindowQuickdrawSubdriver(int width, int height, int bpp)
|
|
803 |
{
|
|
804 |
WindowQuickdrawSubdriver *ret;
|
|
805 |
|
|
806 |
if (bpp != 8 && bpp != 32) {
|
|
807 |
DEBUG(driver, 0, "The cocoa quickdraw subdriver only supports 8 and 32 bpp.");
|
|
808 |
return NULL;
|
|
809 |
}
|
|
810 |
|
|
811 |
ret = new WindowQuickdrawSubdriver(bpp);
|
|
812 |
|
|
813 |
if (!ret->ChangeResolution(width, height)) {
|
|
814 |
delete ret;
|
|
815 |
return NULL;
|
|
816 |
}
|
|
817 |
|
|
818 |
return ret;
|
|
819 |
}
|
|
820 |
|
|
821 |
#endif /* WITH_COCOA */
|