OpenTTD Source  1.10.1
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  CGColorSpaceRef color_space; //< Window color space
88  id window;
89 
90 # define MAX_DIRTY_RECTS 100
91  Rect dirty_rects[MAX_DIRTY_RECTS];
93  uint32 palette[256];
94 
95  bool active;
96  bool setup;
97 
98  id cocoaview;
99 
100  /* Separate driver vars for Quarz
101  * Needed here in order to avoid much code duplication */
102  CGContextRef cgcontext;
103 
104  /* Driver methods */
106  virtual ~CocoaSubdriver() {}
107 
111  virtual void Draw(bool force_update = false) = 0;
112 
119  virtual void MakeDirty(int left, int top, int width, int height) = 0;
120 
122  virtual void UpdatePalette(uint first_color, uint num_colors) = 0;
123 
124  virtual uint ListModes(OTTD_Point *modes, uint max_modes) = 0;
125 
131  virtual bool ChangeResolution(int w, int h, int bpp) = 0;
132 
136  virtual bool IsFullscreen() = 0;
137 
141  virtual bool ToggleFullscreen() { return false; };
142 
146  virtual int GetWidth() = 0;
147 
151  virtual int GetHeight() = 0;
152 
156  virtual void *GetPixelBuffer() = 0;
157 
162  virtual CGPoint PrivateLocalToCG(NSPoint *p) = 0;
163 
168  virtual NSPoint GetMouseLocation(NSEvent *event) = 0;
169 
174  virtual bool MouseIsInsideView(NSPoint *pt) = 0;
175 
179  virtual bool IsActive() = 0;
180 
182  virtual void SetPortAlphaOpaque() { return; };
183 
187  virtual bool WindowResized() { return false; };
188 };
189 
190 extern CocoaSubdriver *_cocoa_subdriver;
191 
192 CocoaSubdriver *QZ_CreateFullscreenSubdriver(int width, int height, int bpp);
193 
194 #ifdef ENABLE_COCOA_QUICKDRAW
195 CocoaSubdriver *QZ_CreateWindowQuickdrawSubdriver(int width, int height, int bpp);
196 #endif
197 
198 #ifdef ENABLE_COCOA_QUARTZ
199 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
200 CocoaSubdriver *QZ_CreateWindowQuartzSubdriver(int width, int height, int bpp);
201 #endif
202 #endif
203 
204 void QZ_GameSizeChanged();
205 
206 void QZ_GameLoop();
207 
208 uint QZ_ListModes(OTTD_Point *modes, uint max_modes, CGDirectDisplayID display_id, int display_depth);
209 
212 + (NSCursor *) clearCocoaCursor;
213 @end
214 
216 @interface OTTD_CocoaWindow : NSWindow {
217  CocoaSubdriver *driver;
218 }
219 
220 - (void)setDriver:(CocoaSubdriver*)drv;
221 
222 - (void)miniaturize:(id)sender;
223 - (void)display;
224 - (void)setFrame:(NSRect)frameRect display:(BOOL)flag;
225 - (void)appDidHide:(NSNotification*)note;
226 - (void)appWillUnhide:(NSNotification*)note;
227 - (void)appDidUnhide:(NSNotification*)note;
228 - (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)styleMask backing:(NSBackingStoreType)backingType defer:(BOOL)flag;
229 @end
230 
232 @interface OTTD_CocoaView : NSView
233 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
234 # if MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_4
235  <NSTextInputClient, NSTextInput>
236 # else
237  <NSTextInputClient>
238 # endif /* MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_4 */
239 #else
240  <NSTextInput>
241 #endif /* MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 */
242 {
243  CocoaSubdriver *driver;
244  NSTrackingRectTag trackingtag;
245 }
246 - (void)setDriver:(CocoaSubdriver*)drv;
247 - (void)drawRect:(NSRect)rect;
248 - (BOOL)isOpaque;
249 - (BOOL)acceptsFirstResponder;
250 - (BOOL)becomeFirstResponder;
251 - (void)setTrackingRect;
252 - (void)clearTrackingRect;
253 - (void)resetCursorRects;
254 - (void)viewWillMoveToWindow:(NSWindow *)win;
255 - (void)viewDidMoveToWindow;
256 - (void)mouseEntered:(NSEvent *)theEvent;
257 - (void)mouseExited:(NSEvent *)theEvent;
258 @end
259 
261 @interface OTTD_CocoaWindowDelegate : NSObject
262 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
263  <NSWindowDelegate>
264 #endif
265 {
266  CocoaSubdriver *driver;
267 }
268 
269 - (void)setDriver:(CocoaSubdriver*)drv;
270 
271 - (BOOL)windowShouldClose:(id)sender;
272 - (void)windowDidEnterFullScreen:(NSNotification *)aNotification;
273 - (void)windowDidChangeScreenProfile:(NSNotification *)aNotification;
274 @end
275 
276 
277 #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:211
Delegate for our NSWindow to send ask for quit on close.
Definition: cocoa_v.h:261
id cocoaview
Pointer to view object.
Definition: cocoa_v.h:98
virtual bool WindowResized()
Whether the window was successfully resized.
Definition: cocoa_v.h:187
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:141
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:95
virtual void SetPortAlphaOpaque()
Makes the game region of the window 100% opaque.
Definition: cocoa_v.h:182
Subclass of NSWindow to cater our special needs.
Definition: cocoa_v.h:216
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:92
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:102
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:106
id window
Pointer to window object.
Definition: cocoa_v.h:88
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:232