OpenTTD
cocoa_v.h
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
10 #ifndef VIDEO_COCOA_H
11 #define VIDEO_COCOA_H
12 
13 #include "../video_driver.hpp"
14 
16 public:
17  const char *Start(const char * const *param) override;
18 
20  void Stop() override;
21 
28  void MakeDirty(int left, int top, int width, int height) override;
29 
31  void MainLoop() override;
32 
38  bool ChangeResolution(int w, int h) override;
39 
44  bool ToggleFullscreen(bool fullscreen) override;
45 
49  bool AfterBlitterChange() override;
50 
54  void EditBoxLostFocus() override;
55 
59  const char *GetName() const override { return "cocoa"; }
60 };
61 
63 public:
64  FVideoDriver_Cocoa() : DriverFactoryBase(Driver::DT_VIDEO, 10, "cocoa", "Cocoa Video Driver") {}
65  Driver *CreateInstance() const override { return new VideoDriver_Cocoa(); }
66 };
67 
68 
75 public:
79 
82  int window_pitch;
83 
85  void *pixel_buffer;
86  void *window_buffer;
87  id window;
88 
89 # define MAX_DIRTY_RECTS 100
90  Rect dirty_rects[MAX_DIRTY_RECTS];
92  uint32 palette[256];
93 
94  bool active;
95  bool setup;
96 
97  id cocoaview;
98 
99  /* Separate driver vars for Quarz
100  * Needed here in order to avoid much code duplication */
101  CGContextRef cgcontext;
102 
103  /* Driver methods */
105  virtual ~CocoaSubdriver() {}
106 
110  virtual void Draw(bool force_update = false) = 0;
111 
118  virtual void MakeDirty(int left, int top, int width, int height) = 0;
119 
121  virtual void UpdatePalette(uint first_color, uint num_colors) = 0;
122 
123  virtual uint ListModes(OTTD_Point *modes, uint max_modes) = 0;
124 
130  virtual bool ChangeResolution(int w, int h, int bpp) = 0;
131 
135  virtual bool IsFullscreen() = 0;
136 
140  virtual bool ToggleFullscreen() { return false; };
141 
145  virtual int GetWidth() = 0;
146 
150  virtual int GetHeight() = 0;
151 
155  virtual void *GetPixelBuffer() = 0;
156 
161  virtual CGPoint PrivateLocalToCG(NSPoint *p) = 0;
162 
167  virtual NSPoint GetMouseLocation(NSEvent *event) = 0;
168 
173  virtual bool MouseIsInsideView(NSPoint *pt) = 0;
174 
178  virtual bool IsActive() = 0;
179 
181  virtual void SetPortAlphaOpaque() { return; };
182 
186  virtual bool WindowResized() { return false; };
187 };
188 
189 extern CocoaSubdriver *_cocoa_subdriver;
190 
191 CocoaSubdriver *QZ_CreateFullscreenSubdriver(int width, int height, int bpp);
192 
193 #ifdef ENABLE_COCOA_QUICKDRAW
194 CocoaSubdriver *QZ_CreateWindowQuickdrawSubdriver(int width, int height, int bpp);
195 #endif
196 
197 #ifdef ENABLE_COCOA_QUARTZ
198 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
199 CocoaSubdriver *QZ_CreateWindowQuartzSubdriver(int width, int height, int bpp);
200 #endif
201 #endif
202 
203 void QZ_GameSizeChanged();
204 
205 void QZ_GameLoop();
206 
207 uint QZ_ListModes(OTTD_Point *modes, uint max_modes, CGDirectDisplayID display_id, int display_depth);
208 
211 + (NSCursor *) clearCocoaCursor;
212 @end
213 
215 @interface OTTD_CocoaWindow : NSWindow {
216  CocoaSubdriver *driver;
217 }
218 
219 - (void)setDriver:(CocoaSubdriver*)drv;
220 
221 - (void)miniaturize:(id)sender;
222 - (void)display;
223 - (void)setFrame:(NSRect)frameRect display:(BOOL)flag;
224 - (void)appDidHide:(NSNotification*)note;
225 - (void)appWillUnhide:(NSNotification*)note;
226 - (void)appDidUnhide:(NSNotification*)note;
227 - (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)styleMask backing:(NSBackingStoreType)backingType defer:(BOOL)flag;
228 @end
229 
231 @interface OTTD_CocoaView : NSView
232 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
233 # if MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_4
234  <NSTextInputClient, NSTextInput>
235 # else
236  <NSTextInputClient>
237 # endif /* MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_4 */
238 #else
239  <NSTextInput>
240 #endif /* MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 */
241 {
242  CocoaSubdriver *driver;
243  NSTrackingRectTag trackingtag;
244 }
245 - (void)setDriver:(CocoaSubdriver*)drv;
246 - (void)drawRect:(NSRect)rect;
247 - (BOOL)isOpaque;
248 - (BOOL)acceptsFirstResponder;
249 - (BOOL)becomeFirstResponder;
250 - (void)setTrackingRect;
251 - (void)clearTrackingRect;
252 - (void)resetCursorRects;
253 - (void)viewWillMoveToWindow:(NSWindow *)win;
254 - (void)viewDidMoveToWindow;
255 - (void)mouseEntered:(NSEvent *)theEvent;
256 - (void)mouseExited:(NSEvent *)theEvent;
257 @end
258 
260 @interface OTTD_CocoaWindowDelegate : NSObject
261 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
262  <NSWindowDelegate>
263 #endif
264 {
265  CocoaSubdriver *driver;
266 }
267 
268 - (void)setDriver:(CocoaSubdriver*)drv;
269 
270 - (BOOL)windowShouldClose:(id)sender;
271 - (void)windowDidEnterFullScreen:(NSNotification *)aNotification;
272 - (void)windowDidChangeScreenProfile:(NSNotification *)aNotification;
273 @end
274 
275 
276 #endif /* VIDEO_COCOA_H */
bool ToggleFullscreen(bool fullscreen) override
Set a new window mode.
void EditBoxLostFocus() override
An edit box lost the input focus.
const char * GetName() const override
Return driver name.
Definition: cocoa_v.h:59
bool ChangeResolution(int w, int h) override
Change window resolution.
int window_height
Current window height in pixel.
Definition: cocoa_v.h:81
Category of NSCursor to allow cursor showing/hiding.
Definition: cocoa_v.h:210
Delegate for our NSWindow to send ask for quit on close.
Definition: cocoa_v.h:260
id cocoaview
Pointer to view object.
Definition: cocoa_v.h:97
virtual bool WindowResized()
Whether the window was successfully resized.
Definition: cocoa_v.h:186
void * window_buffer
Colour translation from palette to screen.
Definition: cocoa_v.h:86
void MakeDirty(int left, int top, int width, int height) override
Mark dirty a screen region.
virtual bool ToggleFullscreen()
Toggle between fullscreen and windowed mode.
Definition: cocoa_v.h:140
Base for all driver factories.
Definition: driver.h:58
void * pixel_buffer
used for direct pixel access
Definition: cocoa_v.h:85
int device_width
Width of device in pixel.
Definition: cocoa_v.h:76
int device_depth
Colour depth of device in bit.
Definition: cocoa_v.h:78
void Stop() override
Stop the video driver.
Generic display driver for cocoa On grounds to not duplicate some code, it contains a few variables w...
Definition: cocoa_v.h:74
const char * Start(const char *const *param) override
Start this driver.
A driver for communicating with the user.
Definition: driver.h:22
bool active
Whether the window is visible.
Definition: cocoa_v.h:94
virtual void SetPortAlphaOpaque()
Makes the game region of the window 100% opaque.
Definition: cocoa_v.h:181
Subclass of NSWindow to cater our special needs.
Definition: cocoa_v.h:215
bool AfterBlitterChange() override
Callback invoked after the blitter was changed.
int buffer_depth
Colour depth of used frame buffer.
Definition: cocoa_v.h:84
Driver * CreateInstance() const override
Create an instance of this driver-class.
Definition: cocoa_v.h:65
int num_dirty_rects
Number of dirty rectangles.
Definition: cocoa_v.h:91
void MainLoop() override
Programme main loop.
The base of all video drivers.
A video driver.
Definition: driver.h:43
CGContextRef cgcontext
Context reference for Quartz subdriver.
Definition: cocoa_v.h:101
int device_height
Height of device in pixel.
Definition: cocoa_v.h:77
Specification of a rectangle with absolute coordinates of all edges.
virtual ~CocoaSubdriver()
Initialize driver.
Definition: cocoa_v.h:105
id window
Pointer to window object.
Definition: cocoa_v.h:87
int window_width
Current window width in pixel.
Definition: cocoa_v.h:80
Subclass of NSView to fix Quartz rendering and mouse awareness.
Definition: cocoa_v.h:231