OpenTTD Source  20241121-master-g67a0fccfad
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 #include "../../core/geometry_type.hpp"
15 
16 
17 extern bool _cocoa_video_started;
18 
20 @class OTTD_CocoaWindow;
21 @class OTTD_CocoaView;
22 
24 private:
27 
28 public:
29  bool setup;
30 
33  CGColorSpaceRef color_space;
34 
36 
37 public:
38  VideoDriver_Cocoa(bool uses_hardware_acceleration = false);
39 
40  void Stop() override;
41  void MainLoop() override;
42 
43  void MakeDirty(int left, int top, int width, int height) override;
44  bool AfterBlitterChange() override;
45 
46  bool ChangeResolution(int w, int h) override;
47  bool ToggleFullscreen(bool fullscreen) override;
48 
49  void ClearSystemSprites() override;
50  void PopulateSystemSprites() override;
51 
52  void EditBoxLostFocus() override;
53 
54  std::vector<int> GetListOfMonitorRefreshRates() override;
55 
56  /* --- The following methods should be private, but can't be due to Obj-C limitations. --- */
57 
58  void MainLoopReal();
59 
60  virtual void AllocateBackingStore(bool force = false) = 0;
61 
62 protected:
65 
66  Dimension GetScreenSize() const override;
67  float GetDPIScale() override;
68  void InputLoop() override;
69  bool LockVideoBuffer() override;
70  void UnlockVideoBuffer() override;
71  bool PollEvent() override;
72 
73  void GameSizeChanged();
74 
75  std::optional<std::string_view> Initialize();
76 
77  void UpdateVideoModes();
78 
79  bool MakeWindow(int width, int height);
80 
81  virtual NSView *AllocateDrawView() = 0;
82 
84  virtual void *GetVideoPointer() = 0;
86  virtual void ReleaseVideoPointer() {}
87 
88 private:
89  bool IsFullscreen();
90 };
91 
93 private:
95  void *pixel_buffer;
96  void *window_buffer;
97 
100  int window_pitch;
101 
102  uint32_t palette[256];
103 
104  void BlitIndexedToView32(int left, int top, int right, int bottom);
105  void UpdatePalette(uint first_color, uint num_colors);
106 
107 public:
108  CGContextRef cgcontext;
109 
111 
112  std::optional<std::string_view> Start(const StringList &param) override;
113  void Stop() override;
114 
116  std::string_view GetName() const override { return "cocoa"; }
117 
118  void AllocateBackingStore(bool force = false) override;
119 
120 protected:
121  void Paint() override;
122  void CheckPaletteAnim() override;
123 
124  NSView *AllocateDrawView() override;
125 
126  void *GetVideoPointer() override { return this->buffer_depth == 8 ? this->pixel_buffer : this->window_buffer; }
127 };
128 
130 public:
131  FVideoDriver_CocoaQuartz() : DriverFactoryBase(Driver::DT_VIDEO, 8, "cocoa", "Cocoa Video Driver") {}
132  Driver *CreateInstance() const override { return new VideoDriver_CocoaQuartz(); }
133 };
134 
135 #endif /* VIDEO_COCOA_H */
Base for all driver factories.
Definition: driver.h:57
DriverFactoryBase(Driver::Type type, int priority, const char *name, const char *description)
Construct a new DriverFactory.
Definition: driver.cpp:244
A driver for communicating with the user.
Definition: driver.h:21
@ DT_VIDEO
A video driver.
Definition: driver.h:42
Driver * CreateInstance() const override
Create an instance of this driver-class.
Definition: cocoa_v.h:132
std::string_view GetName() const override
Return driver name.
Definition: cocoa_v.h:116
CGContextRef cgcontext
Context reference for Quartz subdriver.
Definition: cocoa_v.h:108
uint32_t palette[256]
Colour Palette.
Definition: cocoa_v.h:102
void * window_buffer
Colour translation from palette to screen.
Definition: cocoa_v.h:96
int buffer_depth
Colour depth of used frame buffer.
Definition: cocoa_v.h:94
int window_width
Current window width in pixel.
Definition: cocoa_v.h:98
void CheckPaletteAnim() override
Process any pending palette animation.
std::optional< std::string_view > Start(const StringList &param) override
Start this driver.
void Stop() override
Stop this driver.
void Paint() override
Paint the window.
void * GetVideoPointer() override
Get a pointer to the video buffer.
Definition: cocoa_v.h:126
int window_height
Current window height in pixel.
Definition: cocoa_v.h:99
void * pixel_buffer
used for direct pixel access
Definition: cocoa_v.h:95
bool ToggleFullscreen(bool fullscreen) override
Change the full screen setting.
Dimension GetScreenSize() const override
Get the resolution of the main screen.
bool refresh_sys_sprites
System sprites need refreshing.
Definition: cocoa_v.h:26
void MainLoop() override
Perform the actual drawing.
OTTD_CocoaWindow * window
Pointer to window object.
Definition: cocoa_v.h:31
OTTD_CocoaView * cocoaview
Pointer to view object.
Definition: cocoa_v.h:32
virtual void ReleaseVideoPointer()
Hand video buffer back to the drawing backend.
Definition: cocoa_v.h:86
OTTD_CocoaWindowDelegate * delegate
Window delegate object.
Definition: cocoa_v.h:35
void InputLoop() override
Handle input logic, is CTRL pressed, should we fast-forward, etc.
bool PollEvent() override
Process a single system event.
void MakeDirty(int left, int top, int width, int height) override
Mark a particular area dirty.
void PopulateSystemSprites() override
Populate all sprites in cache.
bool LockVideoBuffer() override
Make sure the video buffer is ready for drawing.
void EditBoxLostFocus() override
An edit box lost the input focus.
void ClearSystemSprites() override
Clear all cached sprites.
Rect dirty_rect
Region of the screen that needs redrawing.
Definition: cocoa_v.h:63
void UnlockVideoBuffer() override
Unlock a previously locked video buffer.
void Stop() override
Stop this driver.
std::vector< int > GetListOfMonitorRefreshRates() override
Get a list of refresh rates of each available monitor.
bool buffer_locked
Video buffer was locked by the main thread.
Definition: cocoa_v.h:64
virtual void * GetVideoPointer()=0
Get a pointer to the video buffer.
Dimension orig_res
Saved window size for non-fullscreen mode.
Definition: cocoa_v.h:25
bool setup
Window is currently being created.
Definition: cocoa_v.h:29
CGColorSpaceRef color_space
Window color space.
Definition: cocoa_v.h:33
bool ChangeResolution(int w, int h) override
Change the resolution of the window.
bool AfterBlitterChange() override
Callback invoked after the blitter was changed.
float GetDPIScale() override
Get DPI scaling factor of the screen OTTD is displayed on.
The base of all video drivers.
Subclass of NSView to support mouse awareness and text input.
Definition: cocoa_wnd.h:49
Delegate for our NSWindow to send ask for quit on close.
Definition: cocoa_wnd.h:57
Subclass of NSWindow to cater our special needs.
Definition: cocoa_wnd.h:37
std::vector< std::string > StringList
Type for a list of strings.
Definition: string_type.h:60
Dimensions (a width and height) of a rectangle in 2D.
Specification of a rectangle with absolute coordinates of all edges.