4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
10 /** @file cocoa_v.h The Cocoa video driver. */
15 #include "../video_driver.hpp"
17 class VideoDriver_Cocoa
: public VideoDriver
{
19 /* virtual */ const char *Start(const char * const *param
);
21 /** Stop the video driver */
22 /* virtual */ void Stop();
24 /** Mark dirty a screen region
25 * @param left x-coordinate of left border
26 * @param top y-coordinate of top border
27 * @param width width or dirty rectangle
28 * @param height height of dirty rectangle
30 /* virtual */ void MakeDirty(int left
, int top
, int width
, int height
);
32 /** Programme main loop */
33 /* virtual */ void MainLoop();
35 /** Change window resolution
36 * @param w New window width
37 * @param h New window height
38 * @return Whether change was successful
40 /* virtual */ bool ChangeResolution(int w
, int h
);
42 /** Set a new window mode
43 * @param fullscreen Whether to set fullscreen mode or not
44 * @return Whether changing the screen mode was successful
46 /* virtual */ bool ToggleFullscreen(bool fullscreen
);
48 /** Callback invoked after the blitter was changed.
49 * @return True if no error.
51 /* virtual */ bool AfterBlitterChange();
54 * An edit box lost the input focus. Abort character compositing if necessary.
56 /* virtual */ void EditBoxLostFocus();
58 /** Return driver name
61 /* virtual */ const char *GetName() const { return "cocoa"; }
64 class FVideoDriver_Cocoa
: public DriverFactoryBase
{
66 FVideoDriver_Cocoa() : DriverFactoryBase(Driver::DT_VIDEO
, 10, "cocoa", "Cocoa Video Driver") {}
67 /* virtual */ Driver
*CreateInstance() const { return new VideoDriver_Cocoa(); }
72 * Generic display driver for cocoa
73 * On grounds to not duplicate some code, it contains a few variables
74 * which are not used by all device drivers.
76 class CocoaSubdriver
{
78 int device_width
; ///< Width of device in pixel
79 int device_height
; ///< Height of device in pixel
80 int device_depth
; ///< Colour depth of device in bit
82 int window_width
; ///< Current window width in pixel
83 int window_height
; ///< Current window height in pixel
86 int buffer_depth
; ///< Colour depth of used frame buffer
87 void *pixel_buffer
; ///< used for direct pixel access
88 void *window_buffer
; ///< Colour translation from palette to screen
89 id window
; ///< Pointer to window object
91 # define MAX_DIRTY_RECTS 100
92 Rect dirty_rects
[MAX_DIRTY_RECTS
]; ///< dirty rectangles
93 int num_dirty_rects
; ///< Number of dirty rectangles
94 uint32 palette
[256]; ///< Colour Palette
96 bool active
; ///< Whether the window is visible
99 id cocoaview
; ///< Pointer to view object
101 /* Separate driver vars for Quarz
102 * Needed here in order to avoid much code duplication */
103 CGContextRef cgcontext
; ///< Context reference for Quartz subdriver
106 /** Initialize driver */
107 virtual ~CocoaSubdriver() {}
110 * @param force_update Whether to redraw unconditionally
112 virtual void Draw(bool force_update
= false) = 0;
114 /** Mark dirty a screen region
115 * @param left x-coordinate of left border
116 * @param top y-coordinate of top border
117 * @param width width or dirty rectangle
118 * @param height height of dirty rectangle
120 virtual void MakeDirty(int left
, int top
, int width
, int height
) = 0;
122 /** Update the palette */
123 virtual void UpdatePalette(uint first_color
, uint num_colors
) = 0;
125 virtual uint
ListModes(OTTD_Point
*modes
, uint max_modes
) = 0;
127 /** Change window resolution
128 * @param w New window width
129 * @param h New window height
130 * @return Whether change was successful
132 virtual bool ChangeResolution(int w
, int h
, int bpp
) = 0;
134 /** Are we in fullscreen mode
135 * @return whether fullscreen mode is currently used
137 virtual bool IsFullscreen() = 0;
139 /** Toggle between fullscreen and windowed mode
140 * @return whether switch was successful
142 virtual bool ToggleFullscreen() { return false; };
144 /** Return the width of the current view
145 * @return width of the current view
147 virtual int GetWidth() = 0;
149 /** Return the height of the current view
150 * @return height of the current view
152 virtual int GetHeight() = 0;
154 /** Return the current pixel buffer
155 * @return pixelbuffer
157 virtual void *GetPixelBuffer() = 0;
159 /** Convert local coordinate to window server (CoreGraphics) coordinate
160 * @param p local coordinates
161 * @return window driver coordinates
163 virtual CGPoint
PrivateLocalToCG(NSPoint
*p
) = 0;
165 /** Return the mouse location
166 * @param event UI event
167 * @return mouse location as NSPoint
169 virtual NSPoint
GetMouseLocation(NSEvent
*event
) = 0;
171 /** Return whether the mouse is within our view
172 * @param pt Mouse coordinates
173 * @return Whether mouse coordinates are within view
175 virtual bool MouseIsInsideView(NSPoint
*pt
) = 0;
177 /** Return whether the window is active (visible)
178 * @return whether the window is visible or not
180 virtual bool IsActive() = 0;
182 /** Makes the *game region* of the window 100% opaque. */
183 virtual void SetPortAlphaOpaque() { return; };
185 /** Whether the window was successfully resized
186 * @return whether the window was successfully resized
188 virtual bool WindowResized() { return false; };
191 extern CocoaSubdriver
*_cocoa_subdriver
;
193 CocoaSubdriver
*QZ_CreateFullscreenSubdriver(int width
, int height
, int bpp
);
195 #ifdef ENABLE_COCOA_QUICKDRAW
196 CocoaSubdriver
*QZ_CreateWindowQuickdrawSubdriver(int width
, int height
, int bpp
);
199 #ifdef ENABLE_COCOA_QUARTZ
200 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
201 CocoaSubdriver
*QZ_CreateWindowQuartzSubdriver(int width
, int height
, int bpp
);
205 void QZ_GameSizeChanged();
209 uint
QZ_ListModes(OTTD_Point
*modes
, uint max_modes
, CGDirectDisplayID display_id
, int display_depth
);
211 /** Category of NSCursor to allow cursor showing/hiding */
212 @interface
NSCursor (OTTD_QuickdrawCursor
)
213 + (NSCursor
*) clearCocoaCursor
;
216 /** Subclass of NSWindow to cater our special needs */
217 @interface OTTD_CocoaWindow
: NSWindow
{
218 CocoaSubdriver
*driver
;
221 - (void)setDriver
:(CocoaSubdriver
*)drv
;
223 - (void)miniaturize
:(id
)sender
;
225 - (void)setFrame
:(NSRect
)frameRect display
:(BOOL
)flag
;
226 - (void)appDidHide
:(NSNotification
*)note
;
227 - (void)appWillUnhide
:(NSNotification
*)note
;
228 - (void)appDidUnhide
:(NSNotification
*)note
;
229 - (id
)initWithContentRect
:(NSRect
)contentRect styleMask
:(NSUInteger
)styleMask backing
:(NSBackingStoreType
)backingType defer
:(BOOL
)flag
;
232 /** Subclass of NSView to fix Quartz rendering and mouse awareness */
233 @interface OTTD_CocoaView
: NSView
234 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
235 # if MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_4
236 <NSTextInputClient
, NSTextInput
>
239 # endif /* MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_4 */
242 #endif /* MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 */
244 CocoaSubdriver
*driver
;
245 NSTrackingRectTag trackingtag
;
247 - (void)setDriver
:(CocoaSubdriver
*)drv
;
248 - (void)drawRect
:(NSRect
)rect
;
250 - (BOOL
)acceptsFirstResponder
;
251 - (BOOL
)becomeFirstResponder
;
252 - (void)setTrackingRect
;
253 - (void)clearTrackingRect
;
254 - (void)resetCursorRects
;
255 - (void)viewWillMoveToWindow
:(NSWindow
*)win
;
256 - (void)viewDidMoveToWindow
;
257 - (void)mouseEntered
:(NSEvent
*)theEvent
;
258 - (void)mouseExited
:(NSEvent
*)theEvent
;
261 /** Delegate for our NSWindow to send ask for quit on close */
262 @interface OTTD_CocoaWindowDelegate
: NSObject
{
263 CocoaSubdriver
*driver
;
266 - (void)setDriver
:(CocoaSubdriver
*)drv
;
268 - (BOOL
)windowShouldClose
:(id
)sender
;
269 - (void)windowDidEnterFullScreen
:(NSNotification
*)aNotification
;
273 #endif /* VIDEO_COCOA_H */