OpenTTD Source  20240919-master-gdf0233f4c2
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 */
VideoDriver_Cocoa::GetVideoPointer
virtual void * GetVideoPointer()=0
Get a pointer to the video buffer.
VideoDriver_CocoaQuartz::buffer_depth
int buffer_depth
Colour depth of used frame buffer.
Definition: cocoa_v.h:94
VideoDriver_CocoaQuartz::GetVideoPointer
void * GetVideoPointer() override
Get a pointer to the video buffer.
Definition: cocoa_v.h:126
VideoDriver_CocoaQuartz::window_width
int window_width
Current window width in pixel.
Definition: cocoa_v.h:98
VideoDriver_Cocoa::AfterBlitterChange
bool AfterBlitterChange() override
Callback invoked after the blitter was changed.
VideoDriver
The base of all video drivers.
Definition: video_driver.hpp:34
Dimension
Dimensions (a width and height) of a rectangle in 2D.
Definition: geometry_type.hpp:30
VideoDriver_Cocoa::InputLoop
void InputLoop() override
Handle input logic, is CTRL pressed, should we fast-forward, etc.
VideoDriver_CocoaQuartz::palette
uint32_t palette[256]
Colour Palette.
Definition: cocoa_v.h:102
FVideoDriver_CocoaQuartz::CreateInstance
Driver * CreateInstance() const override
Create an instance of this driver-class.
Definition: cocoa_v.h:132
VideoDriver_Cocoa::ChangeResolution
bool ChangeResolution(int w, int h) override
Change the resolution of the window.
VideoDriver_CocoaQuartz::cgcontext
CGContextRef cgcontext
Context reference for Quartz subdriver.
Definition: cocoa_v.h:108
DriverFactoryBase::DriverFactoryBase
DriverFactoryBase(Driver::Type type, int priority, const char *name, const char *description)
Construct a new DriverFactory.
Definition: driver.cpp:244
VideoDriver_Cocoa::refresh_sys_sprites
bool refresh_sys_sprites
System sprites need refreshing.
Definition: cocoa_v.h:26
OTTD_CocoaWindowDelegate
Delegate for our NSWindow to send ask for quit on close.
Definition: cocoa_wnd.h:57
VideoDriver_CocoaQuartz
Definition: cocoa_v.h:92
VideoDriver_CocoaQuartz::window_height
int window_height
Current window height in pixel.
Definition: cocoa_v.h:99
VideoDriver_Cocoa::GetDPIScale
float GetDPIScale() override
Get DPI scaling factor of the screen OTTD is displayed on.
VideoDriver_Cocoa::cocoaview
OTTD_CocoaView * cocoaview
Pointer to view object.
Definition: cocoa_v.h:32
VideoDriver_CocoaQuartz::Start
std::optional< std::string_view > Start(const StringList &param) override
Start this driver.
VideoDriver_CocoaQuartz::GetName
std::string_view GetName() const override
Return driver name.
Definition: cocoa_v.h:116
VideoDriver_Cocoa::ReleaseVideoPointer
virtual void ReleaseVideoPointer()
Hand video buffer back to the drawing backend.
Definition: cocoa_v.h:86
VideoDriver_CocoaQuartz::pixel_buffer
void * pixel_buffer
used for direct pixel access
Definition: cocoa_v.h:95
VideoDriver_Cocoa::buffer_locked
bool buffer_locked
Video buffer was locked by the main thread.
Definition: cocoa_v.h:64
VideoDriver_Cocoa::GetScreenSize
Dimension GetScreenSize() const override
Get the resolution of the main screen.
VideoDriver_CocoaQuartz::Paint
void Paint() override
Paint the window.
VideoDriver_Cocoa::dirty_rect
Rect dirty_rect
Region of the screen that needs redrawing.
Definition: cocoa_v.h:63
VideoDriver_Cocoa::PollEvent
bool PollEvent() override
Process a single system event.
VideoDriver_Cocoa::ClearSystemSprites
void ClearSystemSprites() override
Clear all cached sprites.
VideoDriver_Cocoa::Stop
void Stop() override
Stop this driver.
StringList
std::vector< std::string > StringList
Type for a list of strings.
Definition: string_type.h:60
VideoDriver_Cocoa::window
OTTD_CocoaWindow * window
Pointer to window object.
Definition: cocoa_v.h:31
VideoDriver_CocoaQuartz::Stop
void Stop() override
Stop this driver.
VideoDriver_Cocoa::PopulateSystemSprites
void PopulateSystemSprites() override
Populate all sprites in cache.
VideoDriver_CocoaQuartz::window_buffer
void * window_buffer
Colour translation from palette to screen.
Definition: cocoa_v.h:96
VideoDriver_Cocoa::setup
bool setup
Window is currently being created.
Definition: cocoa_v.h:29
FVideoDriver_CocoaQuartz
Definition: cocoa_v.h:129
VideoDriver_Cocoa::UnlockVideoBuffer
void UnlockVideoBuffer() override
Unlock a previously locked video buffer.
OTTD_CocoaView
Subclass of NSView to support mouse awareness and text input.
Definition: cocoa_wnd.h:49
VideoDriver_Cocoa::ToggleFullscreen
bool ToggleFullscreen(bool fullscreen) override
Change the full screen setting.
VideoDriver_Cocoa
Definition: cocoa_v.h:23
Driver::DT_VIDEO
@ DT_VIDEO
A video driver.
Definition: driver.h:42
VideoDriver_Cocoa::delegate
OTTD_CocoaWindowDelegate * delegate
Window delegate object.
Definition: cocoa_v.h:35
OTTD_CocoaWindow
Subclass of NSWindow to cater our special needs.
Definition: cocoa_wnd.h:36
VideoDriver_Cocoa::MainLoop
void MainLoop() override
Perform the actual drawing.
VideoDriver_CocoaQuartz::CheckPaletteAnim
void CheckPaletteAnim() override
Process any pending palette animation.
VideoDriver_Cocoa::orig_res
Dimension orig_res
Saved window size for non-fullscreen mode.
Definition: cocoa_v.h:25
VideoDriver_Cocoa::LockVideoBuffer
bool LockVideoBuffer() override
Make sure the video buffer is ready for drawing.
Driver
A driver for communicating with the user.
Definition: driver.h:21
Rect
Specification of a rectangle with absolute coordinates of all edges.
Definition: geometry_type.hpp:75
VideoDriver_Cocoa::color_space
CGColorSpaceRef color_space
Window color space.
Definition: cocoa_v.h:33
VideoDriver_Cocoa::GetListOfMonitorRefreshRates
std::vector< int > GetListOfMonitorRefreshRates() override
Get a list of refresh rates of each available monitor.
DriverFactoryBase
Base for all driver factories.
Definition: driver.h:57
VideoDriver_Cocoa::EditBoxLostFocus
void EditBoxLostFocus() override
An edit box lost the input focus.
VideoDriver_Cocoa::MakeDirty
void MakeDirty(int left, int top, int width, int height) override
Mark a particular area dirty.