src/video/cocoa_v.m
author celestar
Thu, 11 Jan 2007 13:16:26 +0000
branchcustombridgeheads
changeset 5648 1608018c5ff2
parent 5643 3778051e8095
child 5650 aefc131bf5ce
permissions -rw-r--r--
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
2743
b3a6f4ddfdb8 (svn r3288) Set svn properties on cocoa related files, and add Id tag.
peter1138
parents: 2741
diff changeset
     1
/* $Id$ */
b3a6f4ddfdb8 (svn r3288) Set svn properties on cocoa related files, and add Id tag.
peter1138
parents: 2741
diff changeset
     2
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
     3
/******************************************************************************
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
     4
 *                             Cocoa video driver                             *
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
     5
 * Known things left to do:                                                   *
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
     6
 *  Nothing at the moment.                                                    *
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
     7
 ******************************************************************************/
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
     8
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
     9
#ifdef WITH_COCOA
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    10
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    11
#import <Cocoa/Cocoa.h>
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    12
#import <sys/time.h> /* gettimeofday */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    13
#import <sys/param.h> /* for MAXPATHLEN */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    14
#import <unistd.h>
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    15
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    16
/* Portions of CPS.h */
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
    17
typedef struct CPSProcessSerNum {
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
    18
	UInt32 lo;
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
    19
	UInt32 hi;
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    20
} CPSProcessSerNum;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    21
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
    22
extern OSErr CPSGetCurrentProcess(CPSProcessSerNum* psn);
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
    23
extern OSErr CPSEnableForegroundOperation(CPSProcessSerNum* psn, UInt32 _arg2, UInt32 _arg3, UInt32 _arg4, UInt32 _arg5);
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
    24
extern OSErr CPSSetFrontProcess(CPSProcessSerNum* psn);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    25
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    26
/* From Menus.h (according to Xcode Developer Documentation) */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    27
extern void ShowMenuBar(void);
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    28
extern void HideMenuBar(void);
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    29
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    30
/* Disables a warning. This is needed since the method exists but has been dropped from the header, supposedly as of 10.4. */
2741
011462ebb67b (svn r3286) -Fix: [OSX] 10.2 works again and universal binaries got 10.2 as target by default
bjarni
parents: 2736
diff changeset
    31
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4)
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    32
@interface NSApplication(NSAppleMenu)
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    33
- (void)setAppleMenu:(NSMenu *)menu;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    34
@end
2741
011462ebb67b (svn r3286) -Fix: [OSX] 10.2 works again and universal binaries got 10.2 as target by default
bjarni
parents: 2736
diff changeset
    35
#endif
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    36
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    37
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    38
/* Defined in ppc/param.h or i386/param.h included from sys/param.h */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    39
#undef ALIGN
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    40
/* Defined in stdbool.h */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    41
#ifndef __cplusplus
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    42
# ifndef __BEOS__
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    43
#  undef bool
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    44
#  undef false
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    45
#  undef true
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    46
# endif
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    47
#endif
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    48
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    49
#include "../stdafx.h"
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    50
#include "../openttd.h"
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    51
#include "../debug.h"
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    52
#include "../functions.h"
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    53
#include "../gfx.h"
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    54
#include "../macros.h"
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    55
#include "../sdl.h"
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    56
#include "../window.h"
5642
bfa6074e2833 (svn r7789) [cbh] - Sync with r7720:7758 from trunk
celestar
parents: 5571
diff changeset
    57
#include "../network/network.h"
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    58
#include "../variables.h"
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    59
#include "../os/macosx/splash.h"
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    60
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    61
#include "cocoa_v.h"
3035
57f039f65023 (svn r3615) Remove the Quartz key codes from the public cocoa video driver interface
tron
parents: 3016
diff changeset
    62
#include "cocoa_keys.h"
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    63
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    64
#undef Point
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    65
#undef Rect
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    66
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    67
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    68
/* Subclass of NSWindow to fix genie effect and support resize events  */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    69
@interface OTTD_QuartzWindow : NSWindow
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    70
- (void)miniaturize:(id)sender;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    71
- (void)display;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    72
- (void)setFrame:(NSRect)frameRect display:(BOOL)flag;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    73
- (void)appDidHide:(NSNotification*)note;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    74
- (void)appWillUnhide:(NSNotification*)note;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    75
- (void)appDidUnhide:(NSNotification*)note;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    76
- (id)initWithContentRect:(NSRect)contentRect styleMask:(unsigned int)styleMask backing:(NSBackingStoreType)backingType defer:(BOOL)flag;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    77
@end
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    78
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    79
/* Delegate for our NSWindow to send ask for quit on close */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    80
@interface OTTD_QuartzWindowDelegate : NSObject
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    81
- (BOOL)windowShouldClose:(id)sender;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    82
@end
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    83
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    84
@interface OTTDMain : NSObject
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    85
@end
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    86
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    87
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
    88
/* Structure for rez switch gamma fades
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
    89
 * We can hide the monitor flicker by setting the gamma tables to 0
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
    90
 */
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    91
#define QZ_GAMMA_TABLE_SIZE 256
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    92
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    93
typedef struct {
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    94
	CGGammaValue red[QZ_GAMMA_TABLE_SIZE];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    95
	CGGammaValue green[QZ_GAMMA_TABLE_SIZE];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    96
	CGGammaValue blue[QZ_GAMMA_TABLE_SIZE];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    97
} OTTD_QuartzGammaTable;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
    98
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
    99
/* Add methods to get at private members of NSScreen.
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   100
 * Since there is a bug in Apple's screen switching code that does not update
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   101
 * this variable when switching to fullscreen, we'll set it manually (but only
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   102
 * for the main screen).
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   103
 */
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   104
@interface NSScreen (NSScreenAccess)
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   105
	- (void) setFrame:(NSRect)frame;
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   106
@end
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   107
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   108
@implementation NSScreen (NSScreenAccess)
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   109
- (void) setFrame:(NSRect)frame;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   110
{
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   111
	_frame = frame;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   112
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   113
@end
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   114
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   115
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   116
static void QZ_Draw(void);
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   117
static void QZ_UnsetVideoMode(void);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   118
static void QZ_UpdatePalette(uint start, uint count);
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   119
static void QZ_WarpCursor(int x, int y);
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   120
static void QZ_ShowMouse(void);
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   121
static void QZ_HideMouse(void);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   122
static void CocoaVideoFullScreen(bool full_screen);
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   123
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   124
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   125
static NSAutoreleasePool *_ottd_autorelease_pool;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   126
static OTTDMain *_ottd_main;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   127
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   128
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   129
static struct CocoaVideoData {
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   130
	bool isset;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   131
	bool issetting;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   132
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   133
	CGDirectDisplayID  display_id;         /* 0 == main display (only support single display) */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   134
	CFDictionaryRef    mode;               /* current mode of the display */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   135
	CFDictionaryRef    save_mode;          /* original mode of the display */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   136
	CFArrayRef         mode_list;          /* list of available fullscreen modes */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   137
	CGDirectPaletteRef palette;            /* palette of an 8-bit display */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   138
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   139
	uint32 device_width;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   140
	uint32 device_height;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   141
	uint32 device_bpp;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   142
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   143
	void *realpixels;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   144
	uint8 *pixels;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   145
	uint32 width;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   146
	uint32 height;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   147
	uint32 pitch;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   148
	bool fullscreen;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   149
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   150
	unsigned int current_mods;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   151
	bool tab_is_down;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   152
	bool emulating_right_button;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   153
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   154
	bool cursor_visible;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   155
	bool active;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   156
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   157
#ifdef _DEBUG
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   158
	uint32 tEvent;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   159
#endif
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   160
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   161
	OTTD_QuartzWindow *window;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   162
	NSQuickDrawView *qdview;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   163
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   164
#define MAX_DIRTY_RECTS 100
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   165
	OTTDRect dirty_rects[MAX_DIRTY_RECTS];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   166
	int num_dirty_rects;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   167
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   168
	uint16 palette16[256];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   169
	uint32 palette32[256];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   170
} _cocoa_video_data;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   171
2827
61e2ac8b0801 (svn r3375) -Add: [ FS#29 ] show an error dialog for OSX cocoa driver (egladil)
truelight
parents: 2743
diff changeset
   172
static bool _cocoa_video_started = false;
61e2ac8b0801 (svn r3375) -Add: [ FS#29 ] show an error dialog for OSX cocoa driver (egladil)
truelight
parents: 2743
diff changeset
   173
static bool _cocoa_video_dialog = false;
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   174
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   175
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   176
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   177
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   178
/******************************************************************************
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   179
 *                             Game loop and accessories                      *
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   180
 ******************************************************************************/
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   181
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   182
static uint32 GetTick(void)
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   183
{
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   184
	struct timeval tim;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   185
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   186
	gettimeofday(&tim, NULL);
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   187
	return tim.tv_usec / 1000 + tim.tv_sec * 1000;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   188
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   189
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   190
static void QZ_CheckPaletteAnim(void)
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   191
{
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   192
	if (_pal_last_dirty != -1) {
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   193
		QZ_UpdatePalette(_pal_first_dirty, _pal_last_dirty - _pal_first_dirty + 1);
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   194
		_pal_last_dirty = -1;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   195
	}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   196
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   197
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   198
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   199
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   200
typedef struct VkMapping {
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   201
	unsigned short vk_from;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   202
	byte map_to;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   203
} VkMapping;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   204
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   205
#define AS(x, z) {x, z}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   206
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   207
static const VkMapping _vk_mapping[] = {
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   208
	AS(QZ_BACKQUOTE,  WKC_BACKQUOTE), // key left of '1'
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   209
	AS(QZ_BACKQUOTE2, WKC_BACKQUOTE), // some keyboards have it on another scancode
2845
258ea64174da (svn r3393) -Fix: [OSX] some keyboards had a different scancode for opening the console (egladil)
bjarni
parents: 2827
diff changeset
   210
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   211
	// Pageup stuff + up/down
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   212
	//AM(SDLK_PAGEUP, SDLK_PAGEDOWN, WKC_PAGEUP, WKC_PAGEDOWN),  <==== Does this include HOME/END?
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   213
	AS(QZ_PAGEUP,   WKC_PAGEUP),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   214
	AS(QZ_PAGEDOWN, WKC_PAGEDOWN),
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   215
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   216
	AS(QZ_UP,    WKC_UP),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   217
	AS(QZ_DOWN,  WKC_DOWN),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   218
	AS(QZ_LEFT,  WKC_LEFT),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   219
	AS(QZ_RIGHT, WKC_RIGHT),
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   220
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   221
	AS(QZ_HOME, WKC_HOME),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   222
	AS(QZ_END,  WKC_END),
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   223
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   224
	AS(QZ_INSERT, WKC_INSERT),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   225
	AS(QZ_DELETE, WKC_DELETE),
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   226
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   227
	// Letters. QZ_[a-z] is not in numerical order so we can't use AM(...)
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   228
	AS(QZ_a, 'A'),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   229
	AS(QZ_b, 'B'),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   230
	AS(QZ_c, 'C'),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   231
	AS(QZ_d, 'D'),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   232
	AS(QZ_e, 'E'),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   233
	AS(QZ_f, 'F'),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   234
	AS(QZ_g, 'G'),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   235
	AS(QZ_h, 'H'),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   236
	AS(QZ_i, 'I'),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   237
	AS(QZ_j, 'J'),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   238
	AS(QZ_k, 'K'),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   239
	AS(QZ_l, 'L'),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   240
	AS(QZ_m, 'M'),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   241
	AS(QZ_n, 'N'),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   242
	AS(QZ_o, 'O'),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   243
	AS(QZ_p, 'P'),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   244
	AS(QZ_q, 'Q'),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   245
	AS(QZ_r, 'R'),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   246
	AS(QZ_s, 'S'),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   247
	AS(QZ_t, 'T'),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   248
	AS(QZ_u, 'U'),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   249
	AS(QZ_v, 'V'),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   250
	AS(QZ_w, 'W'),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   251
	AS(QZ_x, 'X'),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   252
	AS(QZ_y, 'Y'),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   253
	AS(QZ_z, 'Z'),
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   254
	// Same thing for digits
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   255
	AS(QZ_0, '0'),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   256
	AS(QZ_1, '1'),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   257
	AS(QZ_2, '2'),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   258
	AS(QZ_3, '3'),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   259
	AS(QZ_4, '4'),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   260
	AS(QZ_5, '5'),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   261
	AS(QZ_6, '6'),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   262
	AS(QZ_7, '7'),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   263
	AS(QZ_8, '8'),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   264
	AS(QZ_9, '9'),
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   265
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   266
	AS(QZ_ESCAPE,    WKC_ESC),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   267
	AS(QZ_PAUSE,     WKC_PAUSE),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   268
	AS(QZ_BACKSPACE, WKC_BACKSPACE),
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   269
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   270
	AS(QZ_SPACE,  WKC_SPACE),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   271
	AS(QZ_RETURN, WKC_RETURN),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   272
	AS(QZ_TAB,    WKC_TAB),
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   273
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   274
	// Function keys
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   275
	AS(QZ_F1,  WKC_F1),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   276
	AS(QZ_F2,  WKC_F2),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   277
	AS(QZ_F3,  WKC_F3),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   278
	AS(QZ_F4,  WKC_F4),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   279
	AS(QZ_F5,  WKC_F5),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   280
	AS(QZ_F6,  WKC_F6),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   281
	AS(QZ_F7,  WKC_F7),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   282
	AS(QZ_F8,  WKC_F8),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   283
	AS(QZ_F9,  WKC_F9),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   284
	AS(QZ_F10, WKC_F10),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   285
	AS(QZ_F11, WKC_F11),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   286
	AS(QZ_F12, WKC_F12),
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   287
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   288
	// Numeric part.
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   289
	AS(QZ_KP0,         WKC_NUM_0),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   290
	AS(QZ_KP1,         WKC_NUM_1),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   291
	AS(QZ_KP2,         WKC_NUM_2),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   292
	AS(QZ_KP3,         WKC_NUM_3),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   293
	AS(QZ_KP4,         WKC_NUM_4),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   294
	AS(QZ_KP5,         WKC_NUM_5),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   295
	AS(QZ_KP6,         WKC_NUM_6),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   296
	AS(QZ_KP7,         WKC_NUM_7),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   297
	AS(QZ_KP8,         WKC_NUM_8),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   298
	AS(QZ_KP9,         WKC_NUM_9),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   299
	AS(QZ_KP_DIVIDE,   WKC_NUM_DIV),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   300
	AS(QZ_KP_MULTIPLY, WKC_NUM_MUL),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   301
	AS(QZ_KP_MINUS,    WKC_NUM_MINUS),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   302
	AS(QZ_KP_PLUS,     WKC_NUM_PLUS),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   303
	AS(QZ_KP_ENTER,    WKC_NUM_ENTER),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   304
	AS(QZ_KP_PERIOD,   WKC_NUM_DECIMAL)
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   305
};
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   306
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   307
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   308
static uint32 QZ_MapKey(unsigned short sym)
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   309
{
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   310
	const VkMapping *map;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   311
	uint32 key = 0;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   312
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   313
	for (map = _vk_mapping; map != endof(_vk_mapping); ++map) {
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   314
		if (sym == map->vk_from) {
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   315
			key = map->map_to;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   316
			break;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   317
		}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   318
	}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   319
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   320
	if (_cocoa_video_data.current_mods & NSShiftKeyMask)     key |= WKC_SHIFT;
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   321
	if (_cocoa_video_data.current_mods & NSControlKeyMask)   key |= WKC_CTRL;
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   322
	if (_cocoa_video_data.current_mods & NSAlternateKeyMask) key |= WKC_ALT;
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   323
	if (_cocoa_video_data.current_mods & NSCommandKeyMask)   key |= WKC_META;
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   324
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   325
	return key << 16;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   326
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   327
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   328
static void QZ_KeyEvent(unsigned short keycode, unsigned short unicode, BOOL down)
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   329
{
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   330
	switch (keycode) {
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   331
		case QZ_UP:    SB(_dirkeys, 1, 1, down); break;
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   332
		case QZ_DOWN:  SB(_dirkeys, 3, 1, down); break;
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   333
		case QZ_LEFT:  SB(_dirkeys, 0, 1, down); break;
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   334
		case QZ_RIGHT: SB(_dirkeys, 2, 1, down); break;
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   335
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   336
		case QZ_TAB: _cocoa_video_data.tab_is_down = down; break;
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   337
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   338
		case QZ_RETURN:
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   339
		case QZ_f:
5648
1608018c5ff2 (svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents: 5643
diff changeset
   340
			if (down && (_cocoa_video_data.current_mods & NSCommandKeyMask)) {
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   341
				CocoaVideoFullScreen(!_fullscreen);
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   342
			}
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   343
			break;
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   344
	}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   345
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   346
	if (down) {
5086
99e0b202b9fb (svn r7153) -Fix [FS#279]: Some keyboard events possibly lost under high CPU load, handle
Darkvater
parents: 4550
diff changeset
   347
		uint32 pressed_key = QZ_MapKey(keycode) | unicode;
99e0b202b9fb (svn r7153) -Fix [FS#279]: Some keyboard events possibly lost under high CPU load, handle
Darkvater
parents: 4550
diff changeset
   348
		HandleKeypress(pressed_key);
5571
2d8adccea2fd (svn r7569) -Fix (r7565): cocoa files were (somehow) forgotten.
rubidium
parents: 5090
diff changeset
   349
		DEBUG(driver, 2, "cocoa_v: QZ_KeyEvent: %x (%x), down, mapping: %x", keycode, unicode, pressed_key);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   350
	} else {
5571
2d8adccea2fd (svn r7569) -Fix (r7565): cocoa files were (somehow) forgotten.
rubidium
parents: 5090
diff changeset
   351
		DEBUG(driver, 2, "cocoa_v: QZ_KeyEvent: %x (%x), up", keycode, unicode);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   352
	}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   353
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   354
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   355
static void QZ_DoUnsidedModifiers(unsigned int newMods)
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   356
{
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   357
	const int mapping[] = { QZ_CAPSLOCK, QZ_LSHIFT, QZ_LCTRL, QZ_LALT, QZ_LMETA };
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   358
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   359
	int i;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   360
	int bit;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   361
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   362
	if (_cocoa_video_data.current_mods == newMods) return;
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   363
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   364
	/* Iterate through the bits, testing each against the current modifiers */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   365
	for (i = 0, bit = NSAlphaShiftKeyMask; bit <= NSCommandKeyMask; bit <<= 1, ++i) {
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   366
		unsigned int currentMask, newMask;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   367
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   368
		currentMask = _cocoa_video_data.current_mods & bit;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   369
		newMask     = newMods & bit;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   370
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   371
		if (currentMask && currentMask != newMask) { /* modifier up event */
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   372
			/* If this was Caps Lock, we need some additional voodoo to make SDL happy (is this needed in ottd?) */
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   373
			if (bit == NSAlphaShiftKeyMask) QZ_KeyEvent(mapping[i], 0, YES);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   374
			QZ_KeyEvent(mapping[i], 0, NO);
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   375
		} else if (newMask && currentMask != newMask) { /* modifier down event */
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   376
			QZ_KeyEvent(mapping[i], 0, YES);
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   377
			/* If this was Caps Lock, we need some additional voodoo to make SDL happy (is this needed in ottd?) */
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   378
			if (bit == NSAlphaShiftKeyMask) QZ_KeyEvent(mapping[i], 0, NO);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   379
		}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   380
	}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   381
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   382
	_cocoa_video_data.current_mods = newMods;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   383
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   384
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   385
static void QZ_MouseMovedEvent(int x, int y)
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   386
{
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   387
	if (_cursor.fix_at) {
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   388
		int dx = x - _cursor.pos.x;
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   389
		int dy = y - _cursor.pos.y;
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   390
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   391
		if (dx != 0 || dy != 0) {
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   392
			_cursor.delta.x += dx;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   393
			_cursor.delta.y += dy;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   394
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   395
			QZ_WarpCursor(_cursor.pos.x, _cursor.pos.y);
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   396
		}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   397
	} else {
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   398
		_cursor.delta.x = x - _cursor.pos.x;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   399
		_cursor.delta.y = y - _cursor.pos.y;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   400
		_cursor.pos.x = x;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   401
		_cursor.pos.y = y;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   402
		_cursor.dirty = true;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   403
	}
5090
8907f67c2c15 (svn r7157) -Fix [FS#221, SF1168820]: Some mouse events possibly lost under high CPU load, handle
Darkvater
parents: 5086
diff changeset
   404
	HandleMouseEvents();
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   405
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   406
4509
ad99f5ff4e8d (svn r6294) -Cleanup: Add static
tron
parents: 4475
diff changeset
   407
ad99f5ff4e8d (svn r6294) -Cleanup: Add static
tron
parents: 4475
diff changeset
   408
static void QZ_MouseButtonEvent(int button, BOOL down)
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   409
{
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   410
	switch (button) {
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   411
		case 0:
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   412
			if (down) {
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   413
				_left_button_down = true;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   414
			} else {
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   415
				_left_button_down = false;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   416
				_left_button_clicked = false;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   417
			}
5090
8907f67c2c15 (svn r7157) -Fix [FS#221, SF1168820]: Some mouse events possibly lost under high CPU load, handle
Darkvater
parents: 5086
diff changeset
   418
			HandleMouseEvents();
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   419
			break;
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   420
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   421
		case 1:
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   422
			if (down) {
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   423
				_right_button_down = true;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   424
				_right_button_clicked = true;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   425
			} else {
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   426
				_right_button_down = false;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   427
			}
5090
8907f67c2c15 (svn r7157) -Fix [FS#221, SF1168820]: Some mouse events possibly lost under high CPU load, handle
Darkvater
parents: 5086
diff changeset
   428
			HandleMouseEvents();
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   429
			break;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   430
	}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   431
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   432
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   433
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   434
static inline NSPoint QZ_GetMouseLocation(NSEvent *event)
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   435
{
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   436
	NSPoint pt;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   437
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   438
	if (_cocoa_video_data.fullscreen) {
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   439
		pt = [ NSEvent mouseLocation ];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   440
		pt.y = _cocoa_video_data.height - pt.y;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   441
	} else {
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   442
		pt = [event locationInWindow];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   443
		pt = [_cocoa_video_data.qdview convertPoint:pt fromView:nil];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   444
	}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   445
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   446
	return pt;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   447
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   448
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   449
static bool QZ_MouseIsInsideView(NSPoint *pt)
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   450
{
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   451
	if (_cocoa_video_data.fullscreen) {
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   452
		return pt->x >= 0 && pt->y >= 0 && pt->x < _cocoa_video_data.width && pt->y < _cocoa_video_data.height;
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   453
	} else {
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   454
		return [ _cocoa_video_data.qdview mouse:*pt inRect:[ _cocoa_video_data.qdview bounds ] ];
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   455
	}
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   456
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   457
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   458
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   459
static bool QZ_PollEvent(void)
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   460
{
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   461
	NSEvent *event;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   462
	NSPoint pt;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   463
	NSString *chars;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   464
#ifdef _DEBUG
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   465
	uint32 et0, et;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   466
#endif
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   467
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   468
#ifdef _DEBUG
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   469
	et0 = GetTick();
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   470
#endif
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   471
	event = [ NSApp nextEventMatchingMask:NSAnyEventMask
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   472
			untilDate: [ NSDate distantPast ]
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   473
			inMode: NSDefaultRunLoopMode dequeue:YES ];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   474
#ifdef _DEBUG
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   475
	et = GetTick();
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   476
	_cocoa_video_data.tEvent+= et - et0;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   477
#endif
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   478
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   479
	if (event == nil) return false;
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   480
	if (!_cocoa_video_data.active) {
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   481
		QZ_ShowMouse();
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   482
		[NSApp sendEvent:event];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   483
		return true;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   484
	}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   485
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   486
	QZ_DoUnsidedModifiers( [ event modifierFlags ] );
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   487
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   488
	switch ([event type]) {
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   489
		case NSMouseMoved:
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   490
		case NSOtherMouseDragged:
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   491
		case NSRightMouseDragged:
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   492
		case NSLeftMouseDragged:
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   493
			pt = QZ_GetMouseLocation(event);
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   494
			if (!QZ_MouseIsInsideView(&pt) &&
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   495
					!_cocoa_video_data.emulating_right_button) {
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   496
				QZ_ShowMouse();
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   497
				[NSApp sendEvent:event];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   498
				break;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   499
			}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   500
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   501
			QZ_HideMouse();
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   502
			QZ_MouseMovedEvent((int)pt.x, (int)pt.y);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   503
			break;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   504
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   505
		case NSLeftMouseDown:
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   506
			if (!([ event modifierFlags ] & NSCommandKeyMask) ||
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   507
					!QZ_MouseIsInsideView(&pt)) {
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   508
				[NSApp sendEvent:event];
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   509
			}
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   510
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   511
			pt = QZ_GetMouseLocation(event);
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   512
			if (!QZ_MouseIsInsideView(&pt)) {
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   513
				QZ_ShowMouse();
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   514
				break;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   515
			}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   516
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   517
			QZ_HideMouse();
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   518
			QZ_MouseMovedEvent((int)pt.x, (int)pt.y);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   519
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   520
			/* Right mouse button emulation */
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   521
			if ([ event modifierFlags ] & NSCommandKeyMask) {
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   522
				_cocoa_video_data.emulating_right_button = true;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   523
				QZ_MouseButtonEvent(1, YES);
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   524
			} else {
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   525
				QZ_MouseButtonEvent(0, YES);
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   526
			}
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   527
			break;
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   528
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   529
		case NSLeftMouseUp:
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   530
			[NSApp sendEvent:event];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   531
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   532
			pt = QZ_GetMouseLocation(event);
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   533
			if (!QZ_MouseIsInsideView(&pt)) {
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   534
				QZ_ShowMouse();
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   535
				break;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   536
			}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   537
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   538
			QZ_HideMouse();
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   539
			QZ_MouseMovedEvent((int)pt.x, (int)pt.y);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   540
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   541
			/* Right mouse button emulation */
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   542
			if (_cocoa_video_data.emulating_right_button) {
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   543
				_cocoa_video_data.emulating_right_button = false;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   544
				QZ_MouseButtonEvent(1, NO);
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   545
			} else {
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   546
				QZ_MouseButtonEvent(0, NO);
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   547
			}
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   548
			break;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   549
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   550
		case NSRightMouseDown:
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   551
			pt = QZ_GetMouseLocation(event);
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   552
			if (!QZ_MouseIsInsideView(&pt)) {
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   553
				QZ_ShowMouse();
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   554
				[NSApp sendEvent:event];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   555
				break;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   556
			}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   557
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   558
			QZ_HideMouse();
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   559
			QZ_MouseMovedEvent((int)pt.x, (int)pt.y);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   560
			QZ_MouseButtonEvent(1, YES);
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   561
			break;
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   562
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   563
		case NSRightMouseUp:
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   564
			pt = QZ_GetMouseLocation(event);
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   565
			if (!QZ_MouseIsInsideView(&pt)) {
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   566
				QZ_ShowMouse();
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   567
				[NSApp sendEvent:event];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   568
				break;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   569
			}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   570
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   571
			QZ_HideMouse();
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   572
			QZ_MouseMovedEvent((int)pt.x, (int)pt.y);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   573
			QZ_MouseButtonEvent(1, NO);
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   574
			break;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   575
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   576
#if 0
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   577
		/* This is not needed since openttd currently only use two buttons */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   578
		case NSOtherMouseDown:
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   579
			pt = QZ_GetMouseLocation(event);
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   580
			if (!QZ_MouseIsInsideView(&pt)) {
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   581
				QZ_ShowMouse();
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   582
				[NSApp sendEvent:event];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   583
				break;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   584
			}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   585
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   586
			QZ_HideMouse();
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   587
			QZ_MouseMovedEvent((int)pt.x, (int)pt.y);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   588
			QZ_MouseButtonEvent([ event buttonNumber ], YES);
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   589
			break;
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   590
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   591
		case NSOtherMouseUp:
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   592
			pt = QZ_GetMouseLocation(event);
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   593
			if (!QZ_MouseIsInsideView(&pt)) {
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   594
				QZ_ShowMouse();
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   595
				[NSApp sendEvent:event];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   596
				break;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   597
			}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   598
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   599
			QZ_HideMouse();
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   600
			QZ_MouseMovedEvent((int)pt.x, (int)pt.y);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   601
			QZ_MouseButtonEvent([ event buttonNumber ], NO);
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   602
			break;
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   603
#endif
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   604
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   605
		case NSKeyDown:
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   606
			/* Quit, hide and minimize */
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   607
			switch ([event keyCode]) {
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   608
				case QZ_q:
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   609
				case QZ_h:
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   610
				case QZ_m:
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   611
					if ([ event modifierFlags ] & NSCommandKeyMask) {
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   612
						[NSApp sendEvent:event];
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   613
					}
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   614
					break;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   615
			}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   616
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   617
			chars = [ event characters ];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   618
			QZ_KeyEvent([event keyCode], [ chars length ] ? [ chars characterAtIndex:0 ] : 0, YES);
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   619
			break;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   620
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   621
		case NSKeyUp:
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   622
			/* Quit, hide and minimize */
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   623
			switch ([event keyCode]) {
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   624
				case QZ_q:
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   625
				case QZ_h:
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   626
				case QZ_m:
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   627
					if ([ event modifierFlags ] & NSCommandKeyMask) {
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   628
						[NSApp sendEvent:event];
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   629
					}
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   630
					break;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   631
			}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   632
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   633
			chars = [ event characters ];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   634
			QZ_KeyEvent([event keyCode], [ chars length ] ? [ chars characterAtIndex:0 ] : 0, NO);
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   635
			break;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   636
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   637
		case NSScrollWheel:
4116
a8406022f8bc (svn r5460) -Feature: [OSX] macs with touchpads that support two finger scrolling can now use this "scrollwheel" to scroll up/down (ln-)
bjarni
parents: 4000
diff changeset
   638
			if ([ event deltaY ] > 0.0) { /* Scroll up */
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   639
				_cursor.wheel--;
4116
a8406022f8bc (svn r5460) -Feature: [OSX] macs with touchpads that support two finger scrolling can now use this "scrollwheel" to scroll up/down (ln-)
bjarni
parents: 4000
diff changeset
   640
			} else if ([ event deltaY ] < 0.0) { /* Scroll down */
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   641
				_cursor.wheel++;
4116
a8406022f8bc (svn r5460) -Feature: [OSX] macs with touchpads that support two finger scrolling can now use this "scrollwheel" to scroll up/down (ln-)
bjarni
parents: 4000
diff changeset
   642
			} /* else: deltaY was 0.0 and we don't want to do anything */
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   643
			break;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   644
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   645
		default:
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   646
			[NSApp sendEvent:event];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   647
	}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   648
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   649
	return true;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   650
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   651
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   652
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   653
static void QZ_GameLoop(void)
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   654
{
5648
1608018c5ff2 (svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents: 5643
diff changeset
   655
	uint32 cur_ticks = GetTick();
1608018c5ff2 (svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents: 5643
diff changeset
   656
	uint32 next_tick = cur_ticks + 30;
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   657
	uint32 pal_tick = 0;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   658
#ifdef _DEBUG
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   659
	uint32 et0, et, st0, st;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   660
#endif
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   661
	int i;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   662
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   663
#ifdef _DEBUG
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   664
	et0 = GetTick();
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   665
	st = 0;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   666
#endif
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   667
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   668
	_screen.dst_ptr = _cocoa_video_data.pixels;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   669
	DisplaySplashImage();
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   670
	QZ_CheckPaletteAnim();
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   671
	QZ_Draw();
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   672
	CSleep(1);
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   673
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   674
	for (i = 0; i < 2; i++) GameLoop();
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   675
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   676
	_screen.dst_ptr = _cocoa_video_data.pixels;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   677
	UpdateWindows();
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   678
	QZ_CheckPaletteAnim();
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   679
	QZ_Draw();
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   680
	CSleep(1);
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   681
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   682
	for (;;) {
5648
1608018c5ff2 (svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents: 5643
diff changeset
   683
		uint32 prev_cur_ticks; // to check for wrapping
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   684
		InteractiveRandom(); // randomness
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   685
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   686
		while (QZ_PollEvent()) {}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   687
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   688
		if (_exit_game) break;
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   689
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   690
#if defined(_DEBUG)
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   691
		if (_cocoa_video_data.current_mods & NSShiftKeyMask)
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   692
#else
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   693
		if (_cocoa_video_data.tab_is_down)
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   694
#endif
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   695
		{
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   696
			if (!_networking && _game_mode != GM_MENU) _fast_forward |= 2;
4536
f13408cba18f (svn r6365) -Cleanup: remove IsGeneratingWorld calls that are either not called or have no effect.
rubidium
parents: 4509
diff changeset
   697
		} else if (_fast_forward & 2) {
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   698
			_fast_forward = 0;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   699
		}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   700
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   701
		cur_ticks = GetTick();
5648
1608018c5ff2 (svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents: 5643
diff changeset
   702
		if (cur_ticks >= next_tick || (_fast_forward && !_pause) || cur_ticks < prev_cur_ticks) {
1608018c5ff2 (svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents: 5643
diff changeset
   703
			next_tick = cur_ticks + 30;
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   704
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   705
			_ctrl_pressed = !!(_cocoa_video_data.current_mods & NSControlKeyMask);
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   706
			_shift_pressed = !!(_cocoa_video_data.current_mods & NSShiftKeyMask);
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   707
#ifdef _DEBUG
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   708
			_dbg_screen_rect = !!(_cocoa_video_data.current_mods & NSAlphaShiftKeyMask);
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   709
#endif
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   710
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   711
			GameLoop();
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   712
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   713
			_screen.dst_ptr = _cocoa_video_data.pixels;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   714
			UpdateWindows();
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   715
			if (++pal_tick > 4) {
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   716
				QZ_CheckPaletteAnim();
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   717
				pal_tick = 1;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   718
			}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   719
			QZ_Draw();
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   720
		} else {
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   721
#ifdef _DEBUG
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   722
			st0 = GetTick();
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   723
#endif
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   724
			CSleep(1);
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   725
#ifdef _DEBUG
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   726
			st += GetTick() - st0;
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   727
#endif
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   728
			_screen.dst_ptr = _cocoa_video_data.pixels;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   729
			DrawTextMessage();
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   730
			DrawMouseCursor();
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   731
			QZ_Draw();
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   732
		}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   733
	}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   734
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   735
#ifdef _DEBUG
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   736
	et = GetTick();
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   737
5571
2d8adccea2fd (svn r7569) -Fix (r7565): cocoa files were (somehow) forgotten.
rubidium
parents: 5090
diff changeset
   738
	DEBUG(driver, 1, "cocoa_v: nextEventMatchingMask took %i ms total", _cocoa_video_data.tEvent);
2d8adccea2fd (svn r7569) -Fix (r7565): cocoa files were (somehow) forgotten.
rubidium
parents: 5090
diff changeset
   739
	DEBUG(driver, 1, "cocoa_v: game loop took %i ms total (%i ms without sleep)", et - et0, et - et0 - st);
2d8adccea2fd (svn r7569) -Fix (r7565): cocoa files were (somehow) forgotten.
rubidium
parents: 5090
diff changeset
   740
	DEBUG(driver, 1, "cocoa_v: (nextEventMatchingMask total)/(game loop total) is %f%%", (double)_cocoa_video_data.tEvent / (double)(et - et0) * 100);
2d8adccea2fd (svn r7569) -Fix (r7565): cocoa files were (somehow) forgotten.
rubidium
parents: 5090
diff changeset
   741
	DEBUG(driver, 1, "cocoa_v: (nextEventMatchingMask total)/(game loop without sleep total) is %f%%", (double)_cocoa_video_data.tEvent / (double)(et - et0 - st) * 100);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   742
#endif
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   743
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   744
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   745
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   746
/******************************************************************************
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   747
 *                             Windowed mode                                  *
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   748
 ******************************************************************************/
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   749
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   750
/* This function makes the *game region* of the window 100% opaque.
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   751
 * The genie effect uses the alpha component. Otherwise,
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   752
 * it doesn't seem to matter what value it has.
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   753
 */
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   754
static void QZ_SetPortAlphaOpaque(void)
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   755
{
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   756
	if (_cocoa_video_data.device_bpp == 32) {
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   757
		uint32* pixels = (uint32*)_cocoa_video_data.realpixels;
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   758
		uint32 rowPixels = _cocoa_video_data.pitch / 4;
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   759
		uint32 i;
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   760
		uint32 j;
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   761
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   762
		for (i = 0; i < _cocoa_video_data.height; i++)
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   763
			for (j = 0; j < _cocoa_video_data.width; j++) {
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   764
			pixels[i * rowPixels + j] |= 0xFF000000;
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   765
		}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   766
	}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   767
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   768
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   769
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   770
@implementation OTTD_QuartzWindow
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   771
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   772
/* we override these methods to fix the miniaturize animation/dock icon bug */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   773
- (void)miniaturize:(id)sender
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   774
{
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   775
	/* make the alpha channel opaque so anim won't have holes in it */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   776
	QZ_SetPortAlphaOpaque ();
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   777
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   778
	/* window is hidden now */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   779
	_cocoa_video_data.active = false;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   780
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   781
	QZ_ShowMouse();
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   782
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   783
	[ super miniaturize:sender ];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   784
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   785
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   786
- (void)display
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   787
{
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   788
	/* This method fires just before the window deminaturizes from the Dock.
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   789
	 * We'll save the current visible surface, let the window manager redraw any
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   790
	 * UI elements, and restore the surface. This way, no expose event
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   791
	 * is required, and the deminiaturize works perfectly.
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   792
	 */
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   793
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   794
	QZ_SetPortAlphaOpaque();
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   795
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   796
	/* save current visible surface */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   797
	[ self cacheImageInRect:[ _cocoa_video_data.qdview frame ] ];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   798
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   799
	/* let the window manager redraw controls, border, etc */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   800
	[ super display ];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   801
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   802
	/* restore visible surface */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   803
	[ self restoreCachedImage ];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   804
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   805
	/* window is visible again */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   806
	_cocoa_video_data.active = true;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   807
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   808
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   809
- (void)setFrame:(NSRect)frameRect display:(BOOL)flag
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   810
{
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   811
	NSRect newViewFrame;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   812
	CGrafPtr thePort;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   813
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   814
	[ super setFrame:frameRect display:flag ];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   815
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   816
	/* Don't do anything if the window is currently beign created */
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   817
	if (_cocoa_video_data.issetting) return;
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   818
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   819
	if (_cocoa_video_data.window == nil) return;
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   820
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   821
	newViewFrame = [ _cocoa_video_data.qdview frame ];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   822
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   823
	/* Update the pixels and pitch */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   824
	thePort = [ _cocoa_video_data.qdview qdPort ];
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   825
	LockPortBits(thePort);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   826
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   827
	_cocoa_video_data.realpixels = GetPixBaseAddr(GetPortPixMap(thePort));
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   828
	_cocoa_video_data.pitch      = GetPixRowBytes(GetPortPixMap(thePort));
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   829
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   830
	/* _cocoa_video_data.realpixels now points to the window's pixels
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   831
	 * We want it to point to the *view's* pixels
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   832
	 */
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   833
	{
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   834
		int vOffset = [ _cocoa_video_data.window frame ].size.height - newViewFrame.size.height - newViewFrame.origin.y;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   835
		int hOffset = newViewFrame.origin.x;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   836
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   837
		_cocoa_video_data.realpixels = (uint8*)_cocoa_video_data.realpixels + (vOffset * _cocoa_video_data.pitch) + hOffset * (_cocoa_video_data.device_bpp / 8);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   838
	}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   839
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   840
	UnlockPortBits(thePort);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   841
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   842
	/* Allocate new buffer */
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   843
	free(_cocoa_video_data.pixels);
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   844
	_cocoa_video_data.pixels = (uint8*)malloc(newViewFrame.size.width * newViewFrame.size.height);
3261
618bb5210e94 (svn r3971) reverted r3742 as those new errors were not the right way to handle those exceptions
bjarni
parents: 3129
diff changeset
   845
	assert(_cocoa_video_data.pixels != NULL);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   846
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   847
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   848
	/* Tell the game that the resolution changed */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   849
	_cocoa_video_data.width = newViewFrame.size.width;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   850
	_cocoa_video_data.height = newViewFrame.size.height;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   851
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   852
	_screen.width = _cocoa_video_data.width;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   853
	_screen.height = _cocoa_video_data.height;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   854
	_screen.pitch = _cocoa_video_data.width;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   855
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   856
	GameSizeChanged();
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   857
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   858
	/* Redraw screen */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   859
	_cocoa_video_data.num_dirty_rects = MAX_DIRTY_RECTS;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   860
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   861
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   862
- (void)appDidHide:(NSNotification*)note
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   863
{
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   864
	_cocoa_video_data.active = false;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   865
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   866
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   867
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   868
- (void)appWillUnhide:(NSNotification*)note
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   869
{
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   870
	QZ_SetPortAlphaOpaque ();
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   871
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   872
	/* save current visible surface */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   873
	[ self cacheImageInRect:[ _cocoa_video_data.qdview frame ] ];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   874
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   875
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   876
- (void)appDidUnhide:(NSNotification*)note
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   877
{
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   878
	/* restore cached image, since it may not be current, post expose event too */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   879
	[ self restoreCachedImage ];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   880
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   881
	_cocoa_video_data.active = true;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   882
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   883
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   884
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   885
- (id)initWithContentRect:(NSRect)contentRect styleMask:(unsigned int)styleMask backing:(NSBackingStoreType)backingType defer:(BOOL)flag
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   886
{
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   887
	/* Make our window subclass receive these application notifications */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   888
	[ [ NSNotificationCenter defaultCenter ] addObserver:self
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   889
	selector:@selector(appDidHide:) name:NSApplicationDidHideNotification object:NSApp ];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   890
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   891
	[ [ NSNotificationCenter defaultCenter ] addObserver:self
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   892
	selector:@selector(appDidUnhide:) name:NSApplicationDidUnhideNotification object:NSApp ];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   893
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   894
	[ [ NSNotificationCenter defaultCenter ] addObserver:self
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   895
	selector:@selector(appWillUnhide:) name:NSApplicationWillUnhideNotification object:NSApp ];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   896
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   897
	return [ super initWithContentRect:contentRect styleMask:styleMask backing:backingType defer:flag ];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   898
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   899
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   900
@end
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   901
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   902
@implementation OTTD_QuartzWindowDelegate
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   903
- (BOOL)windowShouldClose:(id)sender
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   904
{
4548
23b56455df33 (svn r6380) -Codechange: unify all ways to quit OTTD.
rubidium
parents: 4536
diff changeset
   905
	HandleExitGameRequest();
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   906
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   907
	return NO;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   908
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   909
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   910
- (void)windowDidBecomeKey:(NSNotification*)aNotification
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   911
{
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   912
	_cocoa_video_data.active = true;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   913
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   914
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   915
- (void)windowDidResignKey:(NSNotification*)aNotification
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   916
{
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   917
	_cocoa_video_data.active = false;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   918
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   919
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   920
- (void)windowDidBecomeMain:(NSNotification*)aNotification
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   921
{
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   922
	_cocoa_video_data.active = true;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   923
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   924
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   925
- (void)windowDidResignMain:(NSNotification*)aNotification
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   926
{
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   927
	_cocoa_video_data.active = false;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   928
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   929
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   930
@end
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   931
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   932
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   933
static void QZ_UpdateWindowPalette(uint start, uint count)
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   934
{
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   935
	uint i;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   936
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   937
	switch (_cocoa_video_data.device_bpp) {
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   938
		case 32:
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   939
			for (i = start; i < start + count; i++) {
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   940
				uint32 clr32 = 0xff000000;
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   941
				clr32 |= (uint32)_cur_palette[i].r << 16;
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   942
				clr32 |= (uint32)_cur_palette[i].g << 8;
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   943
				clr32 |= (uint32)_cur_palette[i].b;
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   944
				_cocoa_video_data.palette32[i] = clr32;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   945
			}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   946
			break;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   947
		case 16:
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   948
			for (i = start; i < start + count; i++) {
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   949
				uint16 clr16 = 0x0000;
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   950
				clr16 |= (uint16)((_cur_palette[i].r >> 3) & 0x1f) << 10;
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   951
				clr16 |= (uint16)((_cur_palette[i].g >> 3) & 0x1f) << 5;
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   952
				clr16 |= (uint16)((_cur_palette[i].b >> 3) & 0x1f);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   953
				_cocoa_video_data.palette16[i] = clr16;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   954
			}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   955
			break;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   956
	}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   957
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   958
	_cocoa_video_data.num_dirty_rects = MAX_DIRTY_RECTS;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   959
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   960
3038
45c3cb2141e8 (svn r3618) -Codechange: [OSX] reduce the time spend in the cocoa video driver
bjarni
parents: 3037
diff changeset
   961
static inline void QZ_WindowBlitIndexedPixelsToView32(uint left, uint top, uint right, uint bottom)
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   962
{
3038
45c3cb2141e8 (svn r3618) -Codechange: [OSX] reduce the time spend in the cocoa video driver
bjarni
parents: 3037
diff changeset
   963
	const uint32* pal = _cocoa_video_data.palette32;
45c3cb2141e8 (svn r3618) -Codechange: [OSX] reduce the time spend in the cocoa video driver
bjarni
parents: 3037
diff changeset
   964
	const uint8* src = _cocoa_video_data.pixels;
45c3cb2141e8 (svn r3618) -Codechange: [OSX] reduce the time spend in the cocoa video driver
bjarni
parents: 3037
diff changeset
   965
	uint32* dst = (uint32*)_cocoa_video_data.realpixels;
45c3cb2141e8 (svn r3618) -Codechange: [OSX] reduce the time spend in the cocoa video driver
bjarni
parents: 3037
diff changeset
   966
	uint width = _cocoa_video_data.width;
45c3cb2141e8 (svn r3618) -Codechange: [OSX] reduce the time spend in the cocoa video driver
bjarni
parents: 3037
diff changeset
   967
	uint pitch = _cocoa_video_data.pitch / 4;
45c3cb2141e8 (svn r3618) -Codechange: [OSX] reduce the time spend in the cocoa video driver
bjarni
parents: 3037
diff changeset
   968
	uint x;
45c3cb2141e8 (svn r3618) -Codechange: [OSX] reduce the time spend in the cocoa video driver
bjarni
parents: 3037
diff changeset
   969
	uint y;
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   970
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   971
	for (y = top; y < bottom; y++) {
3038
45c3cb2141e8 (svn r3618) -Codechange: [OSX] reduce the time spend in the cocoa video driver
bjarni
parents: 3037
diff changeset
   972
		for (x = left; x < right; x++) {
45c3cb2141e8 (svn r3618) -Codechange: [OSX] reduce the time spend in the cocoa video driver
bjarni
parents: 3037
diff changeset
   973
			dst[y * pitch + x] = pal[src[y * width + x]];
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   974
		}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   975
	}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   976
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   977
3038
45c3cb2141e8 (svn r3618) -Codechange: [OSX] reduce the time spend in the cocoa video driver
bjarni
parents: 3037
diff changeset
   978
static inline void QZ_WindowBlitIndexedPixelsToView16(uint left, uint top, uint right, uint bottom)
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   979
{
3038
45c3cb2141e8 (svn r3618) -Codechange: [OSX] reduce the time spend in the cocoa video driver
bjarni
parents: 3037
diff changeset
   980
	const uint16* pal = _cocoa_video_data.palette16;
45c3cb2141e8 (svn r3618) -Codechange: [OSX] reduce the time spend in the cocoa video driver
bjarni
parents: 3037
diff changeset
   981
	const uint8* src = _cocoa_video_data.pixels;
45c3cb2141e8 (svn r3618) -Codechange: [OSX] reduce the time spend in the cocoa video driver
bjarni
parents: 3037
diff changeset
   982
	uint16* dst = (uint16*)_cocoa_video_data.realpixels;
45c3cb2141e8 (svn r3618) -Codechange: [OSX] reduce the time spend in the cocoa video driver
bjarni
parents: 3037
diff changeset
   983
	uint width = _cocoa_video_data.width;
45c3cb2141e8 (svn r3618) -Codechange: [OSX] reduce the time spend in the cocoa video driver
bjarni
parents: 3037
diff changeset
   984
	uint pitch = _cocoa_video_data.pitch / 2;
45c3cb2141e8 (svn r3618) -Codechange: [OSX] reduce the time spend in the cocoa video driver
bjarni
parents: 3037
diff changeset
   985
	uint x;
45c3cb2141e8 (svn r3618) -Codechange: [OSX] reduce the time spend in the cocoa video driver
bjarni
parents: 3037
diff changeset
   986
	uint y;
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   987
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   988
	for (y = top; y < bottom; y++) {
3038
45c3cb2141e8 (svn r3618) -Codechange: [OSX] reduce the time spend in the cocoa video driver
bjarni
parents: 3037
diff changeset
   989
		for (x = left; x < right; x++) {
45c3cb2141e8 (svn r3618) -Codechange: [OSX] reduce the time spend in the cocoa video driver
bjarni
parents: 3037
diff changeset
   990
			dst[y * pitch + x] = pal[src[y * width + x]];
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   991
		}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   992
	}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   993
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   994
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   995
static inline void QZ_WindowBlitIndexedPixelsToView(int left, int top, int right, int bottom)
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
   996
{
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   997
	switch (_cocoa_video_data.device_bpp) {
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   998
		case 32: QZ_WindowBlitIndexedPixelsToView32(left, top, right, bottom); break;
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
   999
		case 16: QZ_WindowBlitIndexedPixelsToView16(left, top, right, bottom); break;
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1000
	}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1001
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1002
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1003
static bool _resize_icon[] = {
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1004
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1005
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1006
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0,
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1007
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0,
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1008
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1,
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1009
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1,
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1010
	0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0,
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1011
	0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0,
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1012
	0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1,
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1013
	0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1,
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1014
	0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0,
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1015
	0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0,
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1016
	0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1,
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1017
	0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1,
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1018
	0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0,
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1019
	1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1020
};
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1021
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1022
static void QZ_DrawResizeIcon(void)
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1023
{
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1024
	int xoff = _cocoa_video_data.width - 16;
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1025
	int yoff = _cocoa_video_data.height - 16;
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1026
	int x;
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1027
	int y;
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1028
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1029
	for (y = 0; y < 16; y++) {
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1030
		uint16* trg16 = (uint16*)_cocoa_video_data.realpixels + (yoff + y) * _cocoa_video_data.pitch / 2 + xoff;
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1031
		uint32* trg32 = (uint32*)_cocoa_video_data.realpixels + (yoff + y) * _cocoa_video_data.pitch / 4 + xoff;
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1032
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1033
		for (x = 0; x < 16; x++, trg16++, trg32++) {
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1034
			if (!_resize_icon[y * 16 + x]) continue;
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1035
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1036
			switch (_cocoa_video_data.device_bpp) {
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1037
				case 32: *trg32 = 0xff000000; break;
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1038
				case 16: *trg16 = 0x0000;     break;
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1039
			}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1040
		}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1041
	}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1042
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1043
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1044
static void QZ_DrawWindow(void)
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1045
{
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1046
	int i;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1047
	RgnHandle dirty, temp;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1048
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1049
	/* Check if we need to do anything */
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1050
	if (_cocoa_video_data.num_dirty_rects == 0 ||
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1051
			[ _cocoa_video_data.window isMiniaturized ]) {
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1052
		return;
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1053
	}
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1054
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1055
	if (_cocoa_video_data.num_dirty_rects >= MAX_DIRTY_RECTS) {
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1056
		_cocoa_video_data.num_dirty_rects = 1;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1057
		_cocoa_video_data.dirty_rects[0].left = 0;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1058
		_cocoa_video_data.dirty_rects[0].top = 0;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1059
		_cocoa_video_data.dirty_rects[0].right = _cocoa_video_data.width;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1060
		_cocoa_video_data.dirty_rects[0].bottom = _cocoa_video_data.height;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1061
	}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1062
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1063
	dirty = NewRgn();
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1064
	temp  = NewRgn();
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1065
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1066
	SetEmptyRgn(dirty);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1067
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1068
	/* Build the region of dirty rectangles */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1069
	for (i = 0; i < _cocoa_video_data.num_dirty_rects; i++) {
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1070
		QZ_WindowBlitIndexedPixelsToView(
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1071
			_cocoa_video_data.dirty_rects[i].left,
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1072
			_cocoa_video_data.dirty_rects[i].top,
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1073
			_cocoa_video_data.dirty_rects[i].right,
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1074
			_cocoa_video_data.dirty_rects[i].bottom
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1075
		);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1076
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1077
		MacSetRectRgn(
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1078
			temp,
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1079
			_cocoa_video_data.dirty_rects[i].left,
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1080
			_cocoa_video_data.dirty_rects[i].top,
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1081
			_cocoa_video_data.dirty_rects[i].right,
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1082
			_cocoa_video_data.dirty_rects[i].bottom
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1083
		);
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1084
		MacUnionRgn(dirty, temp, dirty);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1085
	}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1086
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1087
	QZ_DrawResizeIcon();
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1088
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1089
	/* Flush the dirty region */
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1090
	QDFlushPortBuffer([ _cocoa_video_data.qdview qdPort ], dirty);
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1091
	DisposeRgn(dirty);
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1092
	DisposeRgn(temp);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1093
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1094
	_cocoa_video_data.num_dirty_rects = 0;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1095
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1096
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1097
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1098
extern const char _openttd_revision[];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1099
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1100
static const char* QZ_SetVideoWindowed(uint width, uint height)
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1101
{
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1102
	char caption[50];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1103
	NSString *nsscaption;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1104
	unsigned int style;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1105
	NSRect contentRect;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1106
	BOOL isCustom = NO;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1107
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1108
	if (width > _cocoa_video_data.device_width)
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1109
		width = _cocoa_video_data.device_width;
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1110
	if (height > _cocoa_video_data.device_height)
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1111
		height = _cocoa_video_data.device_height;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1112
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1113
	_cocoa_video_data.width = width;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1114
	_cocoa_video_data.height = height;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1115
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1116
	contentRect = NSMakeRect(0, 0, width, height);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1117
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1118
	/* Check if we should completely destroy the previous mode
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1119
	 * - If it is fullscreen
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1120
	 */
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1121
	if (_cocoa_video_data.isset && _cocoa_video_data.fullscreen)
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1122
		QZ_UnsetVideoMode();
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1123
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1124
	/* Check if we should recreate the window */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1125
	if (_cocoa_video_data.window == nil) {
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1126
		/* Set the window style */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1127
		style = NSTitledWindowMask;
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1128
		style |= (NSMiniaturizableWindowMask | NSClosableWindowMask);
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1129
		style |= NSResizableWindowMask;
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1130
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1131
		/* Manually create a window, avoids having a nib file resource */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1132
		_cocoa_video_data.window = [ [ OTTD_QuartzWindow alloc ]
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1133
										initWithContentRect:contentRect
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1134
										styleMask:style
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1135
										backing:NSBackingStoreBuffered
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1136
										defer:NO ];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1137
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1138
		if (_cocoa_video_data.window == nil)
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1139
			return "Could not create the Cocoa window";
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1140
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1141
		snprintf(caption, sizeof(caption), "OpenTTD %s", _openttd_revision);
2741
011462ebb67b (svn r3286) -Fix: [OSX] 10.2 works again and universal binaries got 10.2 as target by default
bjarni
parents: 2736
diff changeset
  1142
		nsscaption = [ [ NSString alloc ] initWithCString:caption ];
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1143
		[ _cocoa_video_data.window setTitle:nsscaption ];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1144
		[ _cocoa_video_data.window setMiniwindowTitle:nsscaption ];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1145
		[ nsscaption release ];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1146
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1147
		[ _cocoa_video_data.window setAcceptsMouseMovedEvents:YES ];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1148
		[ _cocoa_video_data.window setViewsNeedDisplay:NO ];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1149
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1150
		[ _cocoa_video_data.window setDelegate: [ [ [ OTTD_QuartzWindowDelegate alloc ] init ] autorelease ] ];
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1151
	} else {
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1152
		/* We already have a window, just change its size */
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1153
		if (!isCustom) {
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1154
			[ _cocoa_video_data.window setContentSize:contentRect.size ];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1155
			[ _cocoa_video_data.qdview setFrameSize:contentRect.size ];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1156
		}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1157
	}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1158
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1159
	[ _cocoa_video_data.window center ];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1160
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1161
	/* Only recreate the view if it doesn't already exist */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1162
	if (_cocoa_video_data.qdview == nil) {
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1163
		_cocoa_video_data.qdview = [ [ NSQuickDrawView alloc ] initWithFrame:contentRect ];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1164
		[ _cocoa_video_data.qdview setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable ];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1165
		[ [ _cocoa_video_data.window contentView ] addSubview:_cocoa_video_data.qdview ];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1166
		[ _cocoa_video_data.qdview release ];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1167
		[ _cocoa_video_data.window makeKeyAndOrderFront:nil ];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1168
	}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1169
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1170
	LockPortBits([ _cocoa_video_data.qdview qdPort ]);
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1171
	_cocoa_video_data.realpixels = GetPixBaseAddr(GetPortPixMap([ _cocoa_video_data.qdview qdPort ]));
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1172
	_cocoa_video_data.pitch = GetPixRowBytes(GetPortPixMap([ _cocoa_video_data.qdview qdPort ]));
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1173
	UnlockPortBits([ _cocoa_video_data.qdview qdPort ]);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1174
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1175
	/* _cocoa_video_data.realpixels now points to the window's pixels
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1176
	 * We want it to point to the *view's* pixels
4549
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4548
diff changeset
  1177
	 */
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1178
	{
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1179
		int vOffset = [ _cocoa_video_data.window frame ].size.height - [ _cocoa_video_data.qdview frame ].size.height - [ _cocoa_video_data.qdview frame ].origin.y;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1180
		int hOffset = [ _cocoa_video_data.qdview frame ].origin.x;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1181
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1182
		_cocoa_video_data.realpixels = (uint8*)_cocoa_video_data.realpixels + (vOffset * _cocoa_video_data.pitch) + hOffset * (_cocoa_video_data.device_bpp / 8);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1183
	}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1184
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1185
	free(_cocoa_video_data.pixels);
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1186
	_cocoa_video_data.pixels = (uint8*)malloc(width * height);
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1187
	if (_cocoa_video_data.pixels == NULL) return "Failed to allocate 8-bit buffer";
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1188
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1189
	_cocoa_video_data.fullscreen = false;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1190
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1191
	return NULL;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1192
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1193
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1194
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1195
/******************************************************************************
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1196
 *                             Fullscreen mode                                *
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1197
 ******************************************************************************/
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1198
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1199
/* Gamma functions to try to hide the flash from a rez switch
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1200
 * Fade the display from normal to black
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1201
 * Save gamma tables for fade back to normal
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1202
 */
3037
0cf63a3e4fc0 (svn r3617) Fix warning introduced in r3616
tron
parents: 3036
diff changeset
  1203
static uint32 QZ_FadeGammaOut(OTTD_QuartzGammaTable* table)
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1204
{
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1205
	CGGammaValue redTable[QZ_GAMMA_TABLE_SIZE];
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1206
	CGGammaValue greenTable[QZ_GAMMA_TABLE_SIZE];
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1207
	CGGammaValue blueTable[QZ_GAMMA_TABLE_SIZE];
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1208
	float percent;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1209
	int j;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1210
	unsigned int actual;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1211
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1212
	if (CGGetDisplayTransferByTable(
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1213
				_cocoa_video_data.display_id, QZ_GAMMA_TABLE_SIZE,
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1214
				table->red, table->green, table->blue, &actual
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1215
			) != CGDisplayNoErr ||
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1216
			actual != QZ_GAMMA_TABLE_SIZE) {
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1217
		return 1;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1218
	}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1219
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1220
	memcpy(redTable,   table->red,   sizeof(redTable));
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1221
	memcpy(greenTable, table->green, sizeof(greenTable));
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1222
	memcpy(blueTable,  table->blue,  sizeof(greenTable));
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1223
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1224
	for (percent = 1.0; percent >= 0.0; percent -= 0.01) {
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1225
		for (j = 0; j < QZ_GAMMA_TABLE_SIZE; j++) {
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1226
			redTable[j]   = redTable[j]   * percent;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1227
			greenTable[j] = greenTable[j] * percent;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1228
			blueTable[j]  = blueTable[j]  * percent;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1229
		}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1230
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1231
		if (CGSetDisplayTransferByTable(
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1232
					_cocoa_video_data.display_id, QZ_GAMMA_TABLE_SIZE,
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1233
					redTable, greenTable, blueTable
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1234
				) != CGDisplayNoErr) {
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1235
			CGDisplayRestoreColorSyncSettings();
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1236
			return 1;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1237
		}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1238
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1239
		CSleep(10);
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1240
	}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1241
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1242
	return 0;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1243
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1244
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1245
/* Fade the display from black to normal
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1246
 * Restore previously saved gamma values
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1247
 */
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1248
static uint32 QZ_FadeGammaIn(const OTTD_QuartzGammaTable* table)
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1249
{
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1250
	CGGammaValue redTable[QZ_GAMMA_TABLE_SIZE];
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1251
	CGGammaValue greenTable[QZ_GAMMA_TABLE_SIZE];
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1252
	CGGammaValue blueTable[QZ_GAMMA_TABLE_SIZE];
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1253
	float percent;
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1254
	int j;
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1255
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1256
	memset(redTable, 0, sizeof(redTable));
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1257
	memset(greenTable, 0, sizeof(greenTable));
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1258
	memset(blueTable, 0, sizeof(greenTable));
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1259
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1260
	for (percent = 0.0; percent <= 1.0; percent += 0.01) {
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1261
		for (j = 0; j < QZ_GAMMA_TABLE_SIZE; j++) {
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1262
			redTable[j]   = table->red[j]   * percent;
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1263
			greenTable[j] = table->green[j] * percent;
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1264
			blueTable[j]  = table->blue[j]  * percent;
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1265
		}
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1266
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1267
		if (CGSetDisplayTransferByTable(
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1268
					_cocoa_video_data.display_id, QZ_GAMMA_TABLE_SIZE,
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1269
					redTable, greenTable, blueTable
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1270
				) != CGDisplayNoErr) {
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1271
			CGDisplayRestoreColorSyncSettings();
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1272
			return 1;
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1273
		}
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1274
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1275
		CSleep(10);
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1276
	}
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1277
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1278
	return 0;
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1279
}
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1280
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1281
static const char* QZ_SetVideoFullScreen(int width, int height)
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1282
{
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1283
	const char* errstr = "QZ_SetVideoFullScreen error";
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1284
	int exact_match;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1285
	CFNumberRef number;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1286
	int bpp;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1287
	int gamma_error;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1288
	OTTD_QuartzGammaTable gamma_table;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1289
	NSRect screen_rect;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1290
	CGError error;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1291
	NSPoint pt;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1292
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1293
	/* Destroy any previous mode */
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1294
	if (_cocoa_video_data.isset) QZ_UnsetVideoMode();
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1295
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1296
	/* See if requested mode exists */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1297
	_cocoa_video_data.mode = CGDisplayBestModeForParameters(_cocoa_video_data.display_id, 8, width, height, &exact_match);
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1298
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1299
	/* If the mode wasn't an exact match, check if it has the right bpp, and update width and height */
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1300
	if (!exact_match) {
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1301
		number = CFDictionaryGetValue (_cocoa_video_data.mode, kCGDisplayBitsPerPixel);
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1302
		CFNumberGetValue(number, kCFNumberSInt32Type, &bpp);
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1303
		if (bpp != 8) {
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1304
			errstr = "Failed to find display resolution";
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1305
			goto ERR_NO_MATCH;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1306
		}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1307
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1308
		number = CFDictionaryGetValue(_cocoa_video_data.mode, kCGDisplayWidth);
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1309
		CFNumberGetValue(number, kCFNumberSInt32Type, &width);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1310
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1311
		number = CFDictionaryGetValue(_cocoa_video_data.mode, kCGDisplayHeight);
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1312
		CFNumberGetValue(number, kCFNumberSInt32Type, &height);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1313
	}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1314
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1315
	/* Fade display to zero gamma */
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1316
	gamma_error = QZ_FadeGammaOut(&gamma_table);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1317
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1318
	/* Put up the blanking window (a window above all other windows) */
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1319
	error = CGDisplayCapture(_cocoa_video_data.display_id);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1320
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1321
	if (CGDisplayNoErr != error) {
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1322
		errstr = "Failed capturing display";
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1323
		goto ERR_NO_CAPTURE;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1324
	}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1325
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1326
	/* Do the physical switch */
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1327
	if (CGDisplaySwitchToMode(_cocoa_video_data.display_id, _cocoa_video_data.mode) != CGDisplayNoErr) {
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1328
		errstr = "Failed switching display resolution";
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1329
		goto ERR_NO_SWITCH;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1330
	}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1331
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1332
	_cocoa_video_data.realpixels = (uint8*)CGDisplayBaseAddress(_cocoa_video_data.display_id);
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1333
	_cocoa_video_data.pitch  = CGDisplayBytesPerRow(_cocoa_video_data.display_id);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1334
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1335
	_cocoa_video_data.width = CGDisplayPixelsWide(_cocoa_video_data.display_id);
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1336
	_cocoa_video_data.height = CGDisplayPixelsHigh(_cocoa_video_data.display_id);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1337
	_cocoa_video_data.fullscreen = true;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1338
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1339
	/* Setup double-buffer emulation */
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1340
	_cocoa_video_data.pixels = (uint8*)malloc(width * height);
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1341
	if (_cocoa_video_data.pixels == NULL) {
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1342
		errstr = "Failed to allocate memory for double buffering";
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1343
		goto ERR_DOUBLEBUF;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1344
	}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1345
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1346
	if (!CGDisplayCanSetPalette(_cocoa_video_data.display_id)) {
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1347
		errstr = "Not an indexed display mode.";
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1348
		goto ERR_NOT_INDEXED;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1349
	}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1350
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1351
	/* If we don't hide menu bar, it will get events and interrupt the program */
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1352
	HideMenuBar();
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1353
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1354
	/* Fade the display to original gamma */
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1355
	if (!gamma_error) QZ_FadeGammaIn(&gamma_table);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1356
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1357
	/* There is a bug in Cocoa where NSScreen doesn't synchronize
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1358
	 * with CGDirectDisplay, so the main screen's frame is wrong.
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1359
	 * As a result, coordinate translation produces incorrect results.
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1360
	 * We can hack around this bug by setting the screen rect ourselves.
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1361
	 * This hack should be removed if/when the bug is fixed.
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1362
	 */
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1363
	screen_rect = NSMakeRect(0, 0, width, height);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1364
	[ [ NSScreen mainScreen ] setFrame:screen_rect ];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1365
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1366
	/* we're fullscreen, so flag all input states... */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1367
	_cocoa_video_data.active = true;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1368
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1369
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1370
	pt = [ NSEvent mouseLocation ];
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1371
	pt.y = CGDisplayPixelsHigh(_cocoa_video_data.display_id) - pt.y;
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1372
	if (QZ_MouseIsInsideView(&pt)) QZ_HideMouse();
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1373
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1374
	return NULL;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1375
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1376
/* Since the blanking window covers *all* windows (even force quit) correct recovery is crucial */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1377
ERR_NOT_INDEXED:
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1378
	free(_cocoa_video_data.pixels);
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1379
	_cocoa_video_data.pixels = NULL;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1380
ERR_DOUBLEBUF:
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1381
	CGDisplaySwitchToMode(_cocoa_video_data.display_id, _cocoa_video_data.save_mode);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1382
ERR_NO_SWITCH:
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1383
	CGReleaseAllDisplays();
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1384
ERR_NO_CAPTURE:
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1385
	if (!gamma_error) QZ_FadeGammaIn(&gamma_table);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1386
ERR_NO_MATCH:
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1387
	return errstr;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1388
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1389
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1390
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1391
static void QZ_UpdateFullscreenPalette(uint first_color, uint num_colors)
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1392
{
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1393
	CGTableCount  index;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1394
	CGDeviceColor color;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1395
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1396
	for (index = first_color; index < first_color+num_colors; index++) {
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1397
		/* Clamp colors between 0.0 and 1.0 */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1398
		color.red   = _cur_palette[index].r / 255.0;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1399
		color.blue  = _cur_palette[index].b / 255.0;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1400
		color.green = _cur_palette[index].g / 255.0;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1401
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1402
		CGPaletteSetColorAtIndex(_cocoa_video_data.palette, color, index);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1403
	}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1404
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1405
	CGDisplaySetPalette(_cocoa_video_data.display_id, _cocoa_video_data.palette);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1406
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1407
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1408
/* Wait for the VBL to occur (estimated since we don't have a hardware interrupt) */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1409
static void QZ_WaitForVerticalBlank(void)
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1410
{
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1411
	/* The VBL delay is based on Ian Ollmann's RezLib <iano@cco.caltech.edu> */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1412
	double refreshRate;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1413
	double linesPerSecond;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1414
	double target;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1415
	double position;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1416
	double adjustment;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1417
	CFNumberRef refreshRateCFNumber;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1418
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1419
	refreshRateCFNumber = CFDictionaryGetValue(_cocoa_video_data.mode, kCGDisplayRefreshRate);
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1420
	if (refreshRateCFNumber == NULL) return;
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1421
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1422
	if (CFNumberGetValue(refreshRateCFNumber, kCFNumberDoubleType, &refreshRate) == 0)
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1423
		return;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1424
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1425
	if (refreshRate == 0) return;
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1426
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1427
	linesPerSecond = refreshRate * _cocoa_video_data.height;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1428
	target = _cocoa_video_data.height;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1429
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1430
	/* Figure out the first delay so we start off about right */
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1431
	position = CGDisplayBeamPosition(_cocoa_video_data.display_id);
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1432
	if (position > target) position = 0;
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1433
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1434
	adjustment = (target - position) / linesPerSecond;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1435
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1436
	CSleep((uint32)(adjustment * 1000));
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1437
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1438
4475
ad2f3ff95d23 (svn r6260) Improve readability
tron
parents: 4434
diff changeset
  1439
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1440
static void QZ_DrawScreen(void)
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1441
{
4475
ad2f3ff95d23 (svn r6260) Improve readability
tron
parents: 4434
diff changeset
  1442
	const uint8* src = _cocoa_video_data.pixels;
ad2f3ff95d23 (svn r6260) Improve readability
tron
parents: 4434
diff changeset
  1443
	uint8* dst       = (uint8*)_cocoa_video_data.realpixels;
ad2f3ff95d23 (svn r6260) Improve readability
tron
parents: 4434
diff changeset
  1444
	uint pitch       = _cocoa_video_data.pitch;
ad2f3ff95d23 (svn r6260) Improve readability
tron
parents: 4434
diff changeset
  1445
	uint width       = _cocoa_video_data.width;
ad2f3ff95d23 (svn r6260) Improve readability
tron
parents: 4434
diff changeset
  1446
	uint num_dirty   = _cocoa_video_data.num_dirty_rects;
3327
11e3a7838d81 (svn r4099) -Codechange: [OSX] the cocoa video driver speedup of around 1000% now also applies to Intel macs
bjarni
parents: 3317
diff changeset
  1447
	uint i;
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1448
3327
11e3a7838d81 (svn r4099) -Codechange: [OSX] the cocoa video driver speedup of around 1000% now also applies to Intel macs
bjarni
parents: 3317
diff changeset
  1449
	/* Check if we need to do anything */
4475
ad2f3ff95d23 (svn r6260) Improve readability
tron
parents: 4434
diff changeset
  1450
	if (num_dirty == 0) return;
3128
1a6b26d523f4 (svn r3741) -Fix: [i686 OSX] reverted the change in rev 3670 for non PowerPC (done with #ifdef) since it appeared to crash intel based macs
bjarni
parents: 3082
diff changeset
  1451
4475
ad2f3ff95d23 (svn r6260) Improve readability
tron
parents: 4434
diff changeset
  1452
	if (num_dirty >= MAX_DIRTY_RECTS) {
ad2f3ff95d23 (svn r6260) Improve readability
tron
parents: 4434
diff changeset
  1453
		num_dirty = 1;
ad2f3ff95d23 (svn r6260) Improve readability
tron
parents: 4434
diff changeset
  1454
		_cocoa_video_data.dirty_rects[0].left   = 0;
ad2f3ff95d23 (svn r6260) Improve readability
tron
parents: 4434
diff changeset
  1455
		_cocoa_video_data.dirty_rects[0].top    = 0;
ad2f3ff95d23 (svn r6260) Improve readability
tron
parents: 4434
diff changeset
  1456
		_cocoa_video_data.dirty_rects[0].right  = _cocoa_video_data.width;
3327
11e3a7838d81 (svn r4099) -Codechange: [OSX] the cocoa video driver speedup of around 1000% now also applies to Intel macs
bjarni
parents: 3317
diff changeset
  1457
		_cocoa_video_data.dirty_rects[0].bottom = _cocoa_video_data.height;
11e3a7838d81 (svn r4099) -Codechange: [OSX] the cocoa video driver speedup of around 1000% now also applies to Intel macs
bjarni
parents: 3317
diff changeset
  1458
	}
3128
1a6b26d523f4 (svn r3741) -Fix: [i686 OSX] reverted the change in rev 3670 for non PowerPC (done with #ifdef) since it appeared to crash intel based macs
bjarni
parents: 3082
diff changeset
  1459
3327
11e3a7838d81 (svn r4099) -Codechange: [OSX] the cocoa video driver speedup of around 1000% now also applies to Intel macs
bjarni
parents: 3317
diff changeset
  1460
	QZ_WaitForVerticalBlank();
11e3a7838d81 (svn r4099) -Codechange: [OSX] the cocoa video driver speedup of around 1000% now also applies to Intel macs
bjarni
parents: 3317
diff changeset
  1461
	/* Build the region of dirty rectangles */
4475
ad2f3ff95d23 (svn r6260) Improve readability
tron
parents: 4434
diff changeset
  1462
	for (i = 0; i < num_dirty; i++) {
ad2f3ff95d23 (svn r6260) Improve readability
tron
parents: 4434
diff changeset
  1463
		uint y      = _cocoa_video_data.dirty_rects[i].top;
ad2f3ff95d23 (svn r6260) Improve readability
tron
parents: 4434
diff changeset
  1464
		uint left   = _cocoa_video_data.dirty_rects[i].left;
ad2f3ff95d23 (svn r6260) Improve readability
tron
parents: 4434
diff changeset
  1465
		uint length = _cocoa_video_data.dirty_rects[i].right - left;
ad2f3ff95d23 (svn r6260) Improve readability
tron
parents: 4434
diff changeset
  1466
		uint bottom = _cocoa_video_data.dirty_rects[i].bottom;
3327
11e3a7838d81 (svn r4099) -Codechange: [OSX] the cocoa video driver speedup of around 1000% now also applies to Intel macs
bjarni
parents: 3317
diff changeset
  1467
4475
ad2f3ff95d23 (svn r6260) Improve readability
tron
parents: 4434
diff changeset
  1468
		for (; y < bottom; y++) {
ad2f3ff95d23 (svn r6260) Improve readability
tron
parents: 4434
diff changeset
  1469
			memcpy(dst + y * pitch + left, src + y * width + left, length);
ad2f3ff95d23 (svn r6260) Improve readability
tron
parents: 4434
diff changeset
  1470
		}
3081
77b05fe9ca72 (svn r3670) -Codechange: [OSX] rewrote a part of the cocoa video driver to speed it up by 1000% in fullscreen
bjarni
parents: 3038
diff changeset
  1471
	}
3327
11e3a7838d81 (svn r4099) -Codechange: [OSX] the cocoa video driver speedup of around 1000% now also applies to Intel macs
bjarni
parents: 3317
diff changeset
  1472
11e3a7838d81 (svn r4099) -Codechange: [OSX] the cocoa video driver speedup of around 1000% now also applies to Intel macs
bjarni
parents: 3317
diff changeset
  1473
	_cocoa_video_data.num_dirty_rects = 0;
4475
ad2f3ff95d23 (svn r6260) Improve readability
tron
parents: 4434
diff changeset
  1474
}
3327
11e3a7838d81 (svn r4099) -Codechange: [OSX] the cocoa video driver speedup of around 1000% now also applies to Intel macs
bjarni
parents: 3317
diff changeset
  1475
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1476
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1477
static int QZ_ListFullscreenModes(OTTDPoint* mode_list, int max_modes)
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1478
{
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1479
	CFIndex num_modes;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1480
	CFIndex i;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1481
	int list_size = 0;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1482
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1483
	num_modes = CFArrayGetCount(_cocoa_video_data.mode_list);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1484
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1485
	/* Build list of modes with the requested bpp */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1486
	for (i = 0; i < num_modes && list_size < max_modes; i++) {
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1487
		CFDictionaryRef onemode;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1488
		CFNumberRef     number;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1489
		int bpp;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1490
		int intvalue;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1491
		bool hasMode;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1492
		uint16 width, height;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1493
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1494
		onemode = CFArrayGetValueAtIndex(_cocoa_video_data.mode_list, i);
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1495
		number = CFDictionaryGetValue(onemode, kCGDisplayBitsPerPixel);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1496
		CFNumberGetValue (number, kCFNumberSInt32Type, &bpp);
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1497
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1498
		if (bpp != 8) continue;
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1499
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1500
		number = CFDictionaryGetValue(onemode, kCGDisplayWidth);
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1501
		CFNumberGetValue(number, kCFNumberSInt32Type, &intvalue);
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1502
		width = (uint16)intvalue;
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1503
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1504
		number = CFDictionaryGetValue(onemode, kCGDisplayHeight);
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1505
		CFNumberGetValue(number, kCFNumberSInt32Type, &intvalue);
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1506
		height = (uint16)intvalue;
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1507
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1508
		/* Check if mode is already in the list */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1509
		{
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1510
			int i;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1511
			hasMode = false;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1512
			for (i = 0; i < list_size; i++) {
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1513
				if (mode_list[i].x == width &&  mode_list[i].y == height) {
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1514
					hasMode = true;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1515
					break;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1516
				}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1517
			}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1518
		}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1519
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1520
		if (hasMode) continue;
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1521
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1522
		/* Add mode to the list */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1523
		mode_list[list_size].x = width;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1524
		mode_list[list_size].y = height;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1525
		list_size++;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1526
	}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1527
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1528
	/* Sort list smallest to largest */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1529
	{
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1530
		int i, j;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1531
		for (i = 0; i < list_size; i++) {
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1532
			for (j = 0; j < list_size-1; j++) {
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1533
				if (mode_list[j].x > mode_list[j + 1].x || (
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1534
							mode_list[j].x == mode_list[j + 1].x &&
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1535
							mode_list[j].y >  mode_list[j + 1].y
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1536
						)) {
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1537
					uint tmpw = mode_list[j].x;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1538
					uint tmph = mode_list[j].y;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1539
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1540
					mode_list[j].x = mode_list[j + 1].x;
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1541
					mode_list[j].y = mode_list[j + 1].y;
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1542
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1543
					mode_list[j + 1].x = tmpw;
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1544
					mode_list[j + 1].y = tmph;
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1545
				}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1546
			}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1547
		}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1548
	}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1549
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1550
	return list_size;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1551
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1552
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1553
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1554
/******************************************************************************
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1555
 *                             Windowed and fullscreen common code            *
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1556
 ******************************************************************************/
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1557
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1558
static void QZ_UpdatePalette(uint start, uint count)
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1559
{
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1560
	if (_cocoa_video_data.fullscreen) {
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1561
		QZ_UpdateFullscreenPalette(start, count);
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1562
	} else {
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1563
		QZ_UpdateWindowPalette(start, count);
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1564
	}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1565
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1566
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1567
static void QZ_InitPalette(void)
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1568
{
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1569
	QZ_UpdatePalette(0, 256);
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1570
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1571
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1572
static void QZ_Draw(void)
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1573
{
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1574
	if (_cocoa_video_data.fullscreen) {
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1575
		QZ_DrawScreen();
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1576
	} else {
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1577
		QZ_DrawWindow();
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1578
	}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1579
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1580
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1581
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1582
static const OTTDPoint _default_resolutions[] = {
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1583
	{ 640,  480},
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1584
	{ 800,  600},
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1585
	{1024,  768},
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1586
	{1152,  864},
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1587
	{1280,  800},
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1588
	{1280,  960},
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1589
	{1280, 1024},
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1590
	{1400, 1050},
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1591
	{1600, 1200},
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1592
	{1680, 1050},
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1593
	{1920, 1200}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1594
};
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1595
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1596
static void QZ_UpdateVideoModes(void)
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1597
{
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1598
	uint i, j, count;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1599
	OTTDPoint modes[32];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1600
	const OTTDPoint *current_modes;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1601
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1602
	if (_cocoa_video_data.fullscreen) {
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1603
		count = QZ_ListFullscreenModes(modes, 32);
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1604
		current_modes = modes;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1605
	} else {
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1606
		count = lengthof(_default_resolutions);
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1607
		current_modes = _default_resolutions;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1608
	}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1609
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1610
	for (i = 0, j = 0; j < lengthof(_resolutions) && i < count; i++) {
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1611
		if (_cocoa_video_data.fullscreen || (
3316
5d172314aa4e (svn r4082) -Fix: [OSX] FS#80 Crash when switching from windowed to fullscreen
bjarni
parents: 3261
diff changeset
  1612
					(uint)current_modes[i].x < _cocoa_video_data.device_width &&
5d172314aa4e (svn r4082) -Fix: [OSX] FS#80 Crash when switching from windowed to fullscreen
bjarni
parents: 3261
diff changeset
  1613
					(uint)current_modes[i].y < _cocoa_video_data.device_height)
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1614
				) {
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1615
			_resolutions[j][0] = current_modes[i].x;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1616
			_resolutions[j][1] = current_modes[i].y;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1617
			j++;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1618
		}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1619
	}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1620
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1621
	_num_resolutions = j;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1622
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1623
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1624
static void QZ_UnsetVideoMode(void)
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1625
{
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1626
	if (_cocoa_video_data.fullscreen) {
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1627
		/* Release fullscreen resources */
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1628
		OTTD_QuartzGammaTable gamma_table;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1629
		int gamma_error;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1630
		NSRect screen_rect;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1631
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1632
		gamma_error = QZ_FadeGammaOut(&gamma_table);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1633
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1634
		/* Restore original screen resolution/bpp */
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1635
		CGDisplaySwitchToMode(_cocoa_video_data.display_id, _cocoa_video_data.save_mode);
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1636
		CGReleaseAllDisplays();
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1637
		ShowMenuBar();
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1638
		/* Reset the main screen's rectangle
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1639
		 * See comment in QZ_SetVideoFullscreen for why we do this
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1640
		 */
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1641
		screen_rect = NSMakeRect(0,0,_cocoa_video_data.device_width,_cocoa_video_data.device_height);
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1642
		[ [ NSScreen mainScreen ] setFrame:screen_rect ];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1643
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1644
		if (!gamma_error) QZ_FadeGammaIn(&gamma_table);
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1645
	} else {
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1646
		/* Release window mode resources */
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1647
		[ _cocoa_video_data.window close ];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1648
		_cocoa_video_data.window = nil;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1649
		_cocoa_video_data.qdview = nil;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1650
	}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1651
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1652
	free(_cocoa_video_data.pixels);
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1653
	_cocoa_video_data.pixels = NULL;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1654
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1655
	/* Signal successful teardown */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1656
	_cocoa_video_data.isset = false;
3317
30f7ccb35464 (svn r4083) -Feature: [OSX] Ported r4075 (Undraw the mouse when it leaves the window and Draw it again when it enters).
egladil
parents: 3316
diff changeset
  1657
30f7ccb35464 (svn r4083) -Feature: [OSX] Ported r4075 (Undraw the mouse when it leaves the window and Draw it again when it enters).
egladil
parents: 3316
diff changeset
  1658
	QZ_ShowMouse();
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1659
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1660
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1661
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1662
static const char* QZ_SetVideoMode(uint width, uint height, bool fullscreen)
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1663
{
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1664
	const char *ret;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1665
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1666
	_cocoa_video_data.issetting = true;
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1667
	if (fullscreen) {
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1668
		/* Setup full screen video */
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1669
		ret = QZ_SetVideoFullScreen(width, height);
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1670
	} else {
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1671
		/* Setup windowed video */
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1672
		ret = QZ_SetVideoWindowed(width, height);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1673
	}
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1674
	_cocoa_video_data.issetting = false;
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1675
	if (ret != NULL) return ret;
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1676
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1677
	/* Signal successful completion (used internally) */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1678
	_cocoa_video_data.isset = true;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1679
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1680
	/* Tell the game that the resolution has changed */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1681
	_screen.width = _cocoa_video_data.width;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1682
	_screen.height = _cocoa_video_data.height;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1683
	_screen.pitch = _cocoa_video_data.width;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1684
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1685
	QZ_UpdateVideoModes();
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1686
	GameSizeChanged();
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1687
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1688
	QZ_InitPalette();
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1689
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1690
	return NULL;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1691
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1692
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1693
static const char* QZ_SetVideoModeAndRestoreOnFailure(uint width, uint height, bool fullscreen)
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1694
{
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1695
	bool wasset = _cocoa_video_data.isset;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1696
	uint32 oldwidth = _cocoa_video_data.width;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1697
	uint32 oldheight = _cocoa_video_data.height;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1698
	bool oldfullscreen = _cocoa_video_data.fullscreen;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1699
	const char *ret;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1700
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1701
	ret = QZ_SetVideoMode(width, height, fullscreen);
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1702
	if (ret != NULL && wasset) QZ_SetVideoMode(oldwidth, oldheight, oldfullscreen);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1703
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1704
	return ret;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1705
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1706
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1707
static void QZ_VideoInit(void)
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1708
{
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1709
	memset(&_cocoa_video_data, 0, sizeof(_cocoa_video_data));
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1710
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1711
	/* Initialize the video settings; this data persists between mode switches */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1712
	_cocoa_video_data.display_id = kCGDirectMainDisplay;
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1713
	_cocoa_video_data.save_mode  = CGDisplayCurrentMode(_cocoa_video_data.display_id);
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1714
	_cocoa_video_data.mode_list  = CGDisplayAvailableModes(_cocoa_video_data.display_id);
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1715
	_cocoa_video_data.palette    = CGPaletteCreateDefaultColorPalette();
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1716
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1717
	/* Gather some information that is useful to know about the display */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1718
	/* Maybe this should be moved to QZ_SetVideoMode, in case this is changed after startup */
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1719
	CFNumberGetValue(
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1720
		CFDictionaryGetValue(_cocoa_video_data.save_mode, kCGDisplayBitsPerPixel),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1721
		kCFNumberSInt32Type, &_cocoa_video_data.device_bpp
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1722
	);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1723
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1724
	CFNumberGetValue(
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1725
		CFDictionaryGetValue(_cocoa_video_data.save_mode, kCGDisplayWidth),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1726
		kCFNumberSInt32Type, &_cocoa_video_data.device_width
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1727
	);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1728
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1729
	CFNumberGetValue(
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1730
		CFDictionaryGetValue(_cocoa_video_data.save_mode, kCGDisplayHeight),
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1731
		kCFNumberSInt32Type, &_cocoa_video_data.device_height
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1732
	);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1733
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1734
	_cocoa_video_data.cursor_visible = true;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1735
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1736
	/* register for sleep notifications so wake from sleep generates SDL_VIDEOEXPOSE */
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1737
//	QZ_RegisterForSleepNotifications();
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1738
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1739
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1740
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1741
/* Convert local coordinate to window server (CoreGraphics) coordinate */
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1742
static CGPoint QZ_PrivateLocalToCG(NSPoint* p)
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1743
{
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1744
	CGPoint cgp;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1745
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1746
	if (!_cocoa_video_data.fullscreen) {
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1747
		*p = [ _cocoa_video_data.qdview convertPoint:*p toView: nil ];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1748
		*p = [ _cocoa_video_data.window convertBaseToScreen:*p ];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1749
		p->y = _cocoa_video_data.device_height - p->y;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1750
	}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1751
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1752
	cgp.x = p->x;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1753
	cgp.y = p->y;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1754
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1755
	return cgp;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1756
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1757
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1758
static void QZ_WarpCursor(int x, int y)
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1759
{
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1760
	NSPoint p;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1761
	CGPoint cgp;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1762
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1763
	/* Only allow warping when in foreground */
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1764
	if (![ NSApp isActive ]) return;
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1765
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1766
	p = NSMakePoint(x, y);
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1767
	cgp = QZ_PrivateLocalToCG(&p);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1768
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1769
	/* this is the magic call that fixes cursor "freezing" after warp */
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1770
	CGSetLocalEventsSuppressionInterval(0.0);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1771
	/* Do the actual warp */
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1772
	CGWarpMouseCursorPosition(cgp);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1773
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1774
	/* Generate the mouse moved event */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1775
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1776
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1777
static void QZ_ShowMouse(void)
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1778
{
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1779
	if (!_cocoa_video_data.cursor_visible) {
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1780
		[ NSCursor unhide ];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1781
		_cocoa_video_data.cursor_visible = true;
3317
30f7ccb35464 (svn r4083) -Feature: [OSX] Ported r4075 (Undraw the mouse when it leaves the window and Draw it again when it enters).
egladil
parents: 3316
diff changeset
  1782
30f7ccb35464 (svn r4083) -Feature: [OSX] Ported r4075 (Undraw the mouse when it leaves the window and Draw it again when it enters).
egladil
parents: 3316
diff changeset
  1783
		// Hide the openttd cursor when leaving the window
30f7ccb35464 (svn r4083) -Feature: [OSX] Ported r4075 (Undraw the mouse when it leaves the window and Draw it again when it enters).
egladil
parents: 3316
diff changeset
  1784
		if (_cocoa_video_data.isset)
30f7ccb35464 (svn r4083) -Feature: [OSX] Ported r4075 (Undraw the mouse when it leaves the window and Draw it again when it enters).
egladil
parents: 3316
diff changeset
  1785
			UndrawMouseCursor();
30f7ccb35464 (svn r4083) -Feature: [OSX] Ported r4075 (Undraw the mouse when it leaves the window and Draw it again when it enters).
egladil
parents: 3316
diff changeset
  1786
		_cursor.in_window = false;
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1787
	}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1788
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1789
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1790
static void QZ_HideMouse(void)
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1791
{
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1792
	if (_cocoa_video_data.cursor_visible) {
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1793
#ifndef _DEBUG
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1794
		[ NSCursor hide ];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1795
#endif
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1796
		_cocoa_video_data.cursor_visible = false;
3317
30f7ccb35464 (svn r4083) -Feature: [OSX] Ported r4075 (Undraw the mouse when it leaves the window and Draw it again when it enters).
egladil
parents: 3316
diff changeset
  1797
30f7ccb35464 (svn r4083) -Feature: [OSX] Ported r4075 (Undraw the mouse when it leaves the window and Draw it again when it enters).
egladil
parents: 3316
diff changeset
  1798
		// Show the openttd cursor again
30f7ccb35464 (svn r4083) -Feature: [OSX] Ported r4075 (Undraw the mouse when it leaves the window and Draw it again when it enters).
egladil
parents: 3316
diff changeset
  1799
		_cursor.in_window = true;
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1800
	}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1801
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1802
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1803
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1804
/******************************************************************************
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1805
 *                             OS X application creation                      *
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1806
 ******************************************************************************/
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1807
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1808
/* The main class of the application, the application's delegate */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1809
@implementation OTTDMain
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1810
/* Called when the internal event loop has just started running */
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1811
- (void) applicationDidFinishLaunching: (NSNotification*) note
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1812
{
2827
61e2ac8b0801 (svn r3375) -Add: [ FS#29 ] show an error dialog for OSX cocoa driver (egladil)
truelight
parents: 2743
diff changeset
  1813
	/* Hand off to main application code */
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1814
	QZ_GameLoop();
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1815
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1816
	/* We're done, thank you for playing */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1817
	[ NSApp stop:_ottd_main ];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1818
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1819
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1820
/* Display the in game quit confirmation dialog */
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1821
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication*) sender
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1822
{
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1823
4550
d44b489e4413 (svn r6385) -Fix (r6380): one instance of QZ_AskQuit was left out in the conversion to HandleExitGameRequest.
rubidium
parents: 4549
diff changeset
  1824
	HandleExitGameRequest();
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1825
4434
4175805666a5 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4300
diff changeset
  1826
	return NSTerminateCancel; // NSTerminateLater ?
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1827
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1828
@end
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1829
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1830
static void setApplicationMenu(void)
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1831
{
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1832
	/* warning: this code is very odd */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1833
	NSMenu *appleMenu;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1834
	NSMenuItem *menuItem;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1835
	NSString *title;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1836
	NSString *appName;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1837
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1838
	appName = @"OTTD";
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1839
	appleMenu = [[NSMenu alloc] initWithTitle:appName];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1840
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1841
	/* Add menu items */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1842
	title = [@"About " stringByAppendingString:appName];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1843
	[appleMenu addItemWithTitle:title action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1844
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1845
	[appleMenu addItem:[NSMenuItem separatorItem]];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1846
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1847
	title = [@"Hide " stringByAppendingString:appName];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1848
	[appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@"h"];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1849
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1850
	menuItem = (NSMenuItem*)[appleMenu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@"h"];
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1851
	[menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1852
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1853
	[appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1854
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1855
	[appleMenu addItem:[NSMenuItem separatorItem]];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1856
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1857
	title = [@"Quit " stringByAppendingString:appName];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1858
	[appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@"q"];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1859
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1860
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1861
	/* Put menu into the menubar */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1862
	menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1863
	[menuItem setSubmenu:appleMenu];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1864
	[[NSApp mainMenu] addItem:menuItem];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1865
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1866
	/* Tell the application object that this is now the application menu */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1867
	[NSApp setAppleMenu:appleMenu];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1868
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1869
	/* Finally give up our references to the objects */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1870
	[appleMenu release];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1871
	[menuItem release];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1872
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1873
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1874
/* Create a window menu */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1875
static void setupWindowMenu(void)
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1876
{
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1877
	NSMenu* windowMenu;
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1878
	NSMenuItem* windowMenuItem;
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1879
	NSMenuItem* menuItem;
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1880
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1881
	windowMenu = [[NSMenu alloc] initWithTitle:@"Window"];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1882
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1883
	/* "Minimize" item */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1884
	menuItem = [[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1885
	[windowMenu addItem:menuItem];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1886
	[menuItem release];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1887
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1888
	/* Put menu into the menubar */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1889
	windowMenuItem = [[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1890
	[windowMenuItem setSubmenu:windowMenu];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1891
	[[NSApp mainMenu] addItem:windowMenuItem];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1892
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1893
	/* Tell the application object that this is now the window menu */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1894
	[NSApp setWindowsMenu:windowMenu];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1895
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1896
	/* Finally give up our references to the objects */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1897
	[windowMenu release];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1898
	[windowMenuItem release];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1899
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1900
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1901
static void setupApplication(void)
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1902
{
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1903
	CPSProcessSerNum PSN;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1904
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1905
	/* Ensure the application object is initialised */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1906
	[NSApplication sharedApplication];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1907
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1908
	/* Tell the dock about us */
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1909
	if (!CPSGetCurrentProcess(&PSN) &&
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1910
			!CPSEnableForegroundOperation(&PSN, 0x03, 0x3C, 0x2C, 0x1103) &&
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1911
			!CPSSetFrontProcess(&PSN)) {
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1912
		[NSApplication sharedApplication];
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1913
	}
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1914
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1915
	/* Set up the menubar */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1916
	[NSApp setMainMenu:[[NSMenu alloc] init]];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1917
	setApplicationMenu();
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1918
	setupWindowMenu();
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1919
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1920
	/* Create OTTDMain and make it the app delegate */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1921
	_ottd_main = [[OTTDMain alloc] init];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1922
	[NSApp setDelegate:_ottd_main];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1923
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1924
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1925
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1926
/******************************************************************************
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1927
 *                             Video driver interface                         *
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1928
 ******************************************************************************/
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1929
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1930
static void CocoaVideoStop(void)
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1931
{
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1932
	if (!_cocoa_video_started) return;
2827
61e2ac8b0801 (svn r3375) -Add: [ FS#29 ] show an error dialog for OSX cocoa driver (egladil)
truelight
parents: 2743
diff changeset
  1933
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1934
	if (_cocoa_video_data.isset) QZ_UnsetVideoMode();
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1935
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1936
	[_ottd_main release];
2827
61e2ac8b0801 (svn r3375) -Add: [ FS#29 ] show an error dialog for OSX cocoa driver (egladil)
truelight
parents: 2743
diff changeset
  1937
61e2ac8b0801 (svn r3375) -Add: [ FS#29 ] show an error dialog for OSX cocoa driver (egladil)
truelight
parents: 2743
diff changeset
  1938
	_cocoa_video_started = false;
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1939
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1940
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1941
static const char *CocoaVideoStart(const char * const *parm)
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1942
{
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1943
	const char *ret;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1944
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1945
	if (_cocoa_video_started) return "Already started";
2827
61e2ac8b0801 (svn r3375) -Add: [ FS#29 ] show an error dialog for OSX cocoa driver (egladil)
truelight
parents: 2743
diff changeset
  1946
	_cocoa_video_started = true;
61e2ac8b0801 (svn r3375) -Add: [ FS#29 ] show an error dialog for OSX cocoa driver (egladil)
truelight
parents: 2743
diff changeset
  1947
61e2ac8b0801 (svn r3375) -Add: [ FS#29 ] show an error dialog for OSX cocoa driver (egladil)
truelight
parents: 2743
diff changeset
  1948
	memset(&_cocoa_video_data, 0, sizeof(_cocoa_video_data));
61e2ac8b0801 (svn r3375) -Add: [ FS#29 ] show an error dialog for OSX cocoa driver (egladil)
truelight
parents: 2743
diff changeset
  1949
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1950
	setupApplication();
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1951
2827
61e2ac8b0801 (svn r3375) -Add: [ FS#29 ] show an error dialog for OSX cocoa driver (egladil)
truelight
parents: 2743
diff changeset
  1952
	/* Don't create a window or enter fullscreen if we're just going to show a dialog. */
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1953
	if (_cocoa_video_dialog) return NULL;
2827
61e2ac8b0801 (svn r3375) -Add: [ FS#29 ] show an error dialog for OSX cocoa driver (egladil)
truelight
parents: 2743
diff changeset
  1954
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1955
	QZ_VideoInit();
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1956
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1957
	ret = QZ_SetVideoMode(_cur_resolution[0], _cur_resolution[1], _fullscreen);
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1958
	if (ret != NULL) CocoaVideoStop();
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1959
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1960
	return ret;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1961
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1962
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1963
static void CocoaVideoMakeDirty(int left, int top, int width, int height)
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1964
{
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1965
	if (_cocoa_video_data.num_dirty_rects < MAX_DIRTY_RECTS) {
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1966
		_cocoa_video_data.dirty_rects[_cocoa_video_data.num_dirty_rects].left = left;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1967
		_cocoa_video_data.dirty_rects[_cocoa_video_data.num_dirty_rects].top = top;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1968
		_cocoa_video_data.dirty_rects[_cocoa_video_data.num_dirty_rects].right = left + width;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1969
		_cocoa_video_data.dirty_rects[_cocoa_video_data.num_dirty_rects].bottom = top + height;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1970
	}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1971
	_cocoa_video_data.num_dirty_rects++;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1972
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1973
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1974
static void CocoaVideoMainLoop(void)
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1975
{
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1976
	/* Start the main event loop */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1977
	[NSApp run];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1978
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1979
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1980
static bool CocoaVideoChangeRes(int w, int h)
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1981
{
5571
2d8adccea2fd (svn r7569) -Fix (r7565): cocoa files were (somehow) forgotten.
rubidium
parents: 5090
diff changeset
  1982
	const char *ret = QZ_SetVideoModeAndRestoreOnFailure((uint)w, (uint)h, _cocoa_video_data.fullscreen);
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1983
	if (ret != NULL) {
5571
2d8adccea2fd (svn r7569) -Fix (r7565): cocoa files were (somehow) forgotten.
rubidium
parents: 5090
diff changeset
  1984
		DEBUG(driver, 0, "cocoa_v: CocoaVideoChangeRes failed with message: %s", ret);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1985
	}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1986
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1987
	return ret == NULL;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1988
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1989
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1990
static void CocoaVideoFullScreen(bool full_screen)
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1991
{
5571
2d8adccea2fd (svn r7569) -Fix (r7565): cocoa files were (somehow) forgotten.
rubidium
parents: 5090
diff changeset
  1992
	const char *ret = QZ_SetVideoModeAndRestoreOnFailure(_cocoa_video_data.width, _cocoa_video_data.height, full_screen);
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  1993
	if (ret != NULL) {
5571
2d8adccea2fd (svn r7569) -Fix (r7565): cocoa files were (somehow) forgotten.
rubidium
parents: 5090
diff changeset
  1994
		DEBUG(driver, 0, "cocoa_v: CocoaVideoFullScreen failed with message: %s", ret);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1995
	}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1996
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1997
	_fullscreen = _cocoa_video_data.fullscreen;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1998
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  1999
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  2000
const HalVideoDriver _cocoa_video_driver = {
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  2001
	CocoaVideoStart,
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  2002
	CocoaVideoStop,
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  2003
	CocoaVideoMakeDirty,
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  2004
	CocoaVideoMainLoop,
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  2005
	CocoaVideoChangeRes,
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  2006
	CocoaVideoFullScreen,
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  2007
};
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  2008
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  2009
2827
61e2ac8b0801 (svn r3375) -Add: [ FS#29 ] show an error dialog for OSX cocoa driver (egladil)
truelight
parents: 2743
diff changeset
  2010
/* This is needed since sometimes assert is called before the videodriver is initialized */
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  2011
void CocoaDialog(const char* title, const char* message, const char* buttonLabel)
2827
61e2ac8b0801 (svn r3375) -Add: [ FS#29 ] show an error dialog for OSX cocoa driver (egladil)
truelight
parents: 2743
diff changeset
  2012
{
61e2ac8b0801 (svn r3375) -Add: [ FS#29 ] show an error dialog for OSX cocoa driver (egladil)
truelight
parents: 2743
diff changeset
  2013
	bool wasstarted;
61e2ac8b0801 (svn r3375) -Add: [ FS#29 ] show an error dialog for OSX cocoa driver (egladil)
truelight
parents: 2743
diff changeset
  2014
61e2ac8b0801 (svn r3375) -Add: [ FS#29 ] show an error dialog for OSX cocoa driver (egladil)
truelight
parents: 2743
diff changeset
  2015
	_cocoa_video_dialog = true;
61e2ac8b0801 (svn r3375) -Add: [ FS#29 ] show an error dialog for OSX cocoa driver (egladil)
truelight
parents: 2743
diff changeset
  2016
61e2ac8b0801 (svn r3375) -Add: [ FS#29 ] show an error dialog for OSX cocoa driver (egladil)
truelight
parents: 2743
diff changeset
  2017
	wasstarted = _cocoa_video_started;
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  2018
	if (!_cocoa_video_started && CocoaVideoStart(NULL) != NULL) {
2827
61e2ac8b0801 (svn r3375) -Add: [ FS#29 ] show an error dialog for OSX cocoa driver (egladil)
truelight
parents: 2743
diff changeset
  2019
		fprintf(stderr, "%s: %s\n", title, message);
61e2ac8b0801 (svn r3375) -Add: [ FS#29 ] show an error dialog for OSX cocoa driver (egladil)
truelight
parents: 2743
diff changeset
  2020
		return;
61e2ac8b0801 (svn r3375) -Add: [ FS#29 ] show an error dialog for OSX cocoa driver (egladil)
truelight
parents: 2743
diff changeset
  2021
	}
61e2ac8b0801 (svn r3375) -Add: [ FS#29 ] show an error dialog for OSX cocoa driver (egladil)
truelight
parents: 2743
diff changeset
  2022
61e2ac8b0801 (svn r3375) -Add: [ FS#29 ] show an error dialog for OSX cocoa driver (egladil)
truelight
parents: 2743
diff changeset
  2023
	NSRunAlertPanel([NSString stringWithCString: title], [NSString stringWithCString: message], [NSString stringWithCString: buttonLabel], nil, nil);
61e2ac8b0801 (svn r3375) -Add: [ FS#29 ] show an error dialog for OSX cocoa driver (egladil)
truelight
parents: 2743
diff changeset
  2024
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  2025
	if (!wasstarted) CocoaVideoStop();
2827
61e2ac8b0801 (svn r3375) -Add: [ FS#29 ] show an error dialog for OSX cocoa driver (egladil)
truelight
parents: 2743
diff changeset
  2026
61e2ac8b0801 (svn r3375) -Add: [ FS#29 ] show an error dialog for OSX cocoa driver (egladil)
truelight
parents: 2743
diff changeset
  2027
	_cocoa_video_dialog = false;
61e2ac8b0801 (svn r3375) -Add: [ FS#29 ] show an error dialog for OSX cocoa driver (egladil)
truelight
parents: 2743
diff changeset
  2028
}
61e2ac8b0801 (svn r3375) -Add: [ FS#29 ] show an error dialog for OSX cocoa driver (egladil)
truelight
parents: 2743
diff changeset
  2029
61e2ac8b0801 (svn r3375) -Add: [ FS#29 ] show an error dialog for OSX cocoa driver (egladil)
truelight
parents: 2743
diff changeset
  2030
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  2031
/* This is needed since OS X applications are started with the working dir set to / when double-clicked */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  2032
void cocoaSetWorkingDirectory(void)
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  2033
{
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  2034
	char parentdir[MAXPATHLEN];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  2035
	int chdir_ret;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  2036
	CFURLRef url = CFBundleCopyBundleURL(CFBundleGetMainBundle());
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  2037
	CFURLRef url2 = CFURLCreateCopyDeletingLastPathComponent(0, url);
3036
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  2038
	if (CFURLGetFileSystemRepresentation(url2, true, (unsigned char*)parentdir, MAXPATHLEN)) {
d6670ee6981f (svn r3616) Style sweep
tron
parents: 3035
diff changeset
  2039
		chdir_ret = chdir(parentdir); /* chdir to the binary app's parent */
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  2040
		assert(chdir_ret == 0);
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  2041
	}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  2042
	CFRelease(url);
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  2043
	CFRelease(url2);
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  2044
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  2045
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  2046
/* These are called from main() to prevent a _NSAutoreleaseNoPool error when
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  2047
 * exiting before the cocoa video driver has been loaded
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  2048
 */
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  2049
void cocoaSetupAutoreleasePool(void)
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  2050
{
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  2051
	_ottd_autorelease_pool = [[NSAutoreleasePool alloc] init];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  2052
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  2053
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  2054
void cocoaReleaseAutoreleasePool(void)
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  2055
{
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  2056
	[_ottd_autorelease_pool release];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  2057
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  2058
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
diff changeset
  2059
#endif /* WITH_COCOA */