OpenTTD Source 20260208-master-g43af8e94d0
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#ifndef VIDEO_COCOA_H
11#define VIDEO_COCOA_H
12
13#include "../video_driver.hpp"
15
16
17extern bool _cocoa_video_started;
18
20@class OTTD_CocoaWindow;
21@class OTTD_CocoaView;
22
23class VideoDriver_Cocoa : public VideoDriver {
24private:
27
28public:
29 bool setup;
30
33 CGColorSpaceRef colour_space;
34
36
37public:
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
62protected:
65
66 Dimension GetScreenSize() const override;
67 void InputLoop() override;
68 bool LockVideoBuffer() override;
69 void UnlockVideoBuffer() override;
70 bool PollEvent() override;
71
72 void GameSizeChanged();
73
74 std::optional<std::string_view> Initialize();
75
76 void UpdateVideoModes();
77
78 bool MakeWindow(int width, int height);
79
80 virtual NSView *AllocateDrawView() = 0;
81
83 virtual void *GetVideoPointer() = 0;
85 virtual void ReleaseVideoPointer() {}
86
87private:
88 bool IsFullscreen();
89};
90
91class VideoDriver_CocoaQuartz : public VideoDriver_Cocoa {
92private:
94 std::unique_ptr<uint8_t[]> pixel_buffer;
95 std::unique_ptr<uint32_t[]> window_buffer;
96
99 int window_pitch;
100
101 uint32_t palette[256];
102
103 void BlitIndexedToView32(int left, int top, int right, int bottom);
104 void UpdatePalette(uint first_colour, uint num_colours);
105
106public:
107 CGContextRef cgcontext;
108
109 VideoDriver_CocoaQuartz();
110
111 std::optional<std::string_view> Start(const StringList &param) override;
112 void Stop() override;
113
115 std::string_view GetName() const override { return "cocoa"; }
116
117 void AllocateBackingStore(bool force = false) override;
118
119protected:
120 void Paint() override;
121 void CheckPaletteAnim() override;
122
123 NSView *AllocateDrawView() override;
124
125 void *GetVideoPointer() override { return this->buffer_depth == 8 ? static_cast<void *>(this->pixel_buffer.get()) : static_cast<void *>(this->window_buffer.get()); }
126};
127
128class FVideoDriver_CocoaQuartz : public DriverFactoryBase {
129public:
130 FVideoDriver_CocoaQuartz() : DriverFactoryBase(Driver::DT_VIDEO, 8, "cocoa", "Cocoa Video Driver") {}
131 std::unique_ptr<Driver> CreateInstance() const override { return std::make_unique<VideoDriver_CocoaQuartz>(); }
132};
133
134#endif /* VIDEO_COCOA_H */
DriverFactoryBase(Driver::Type type, int priority, std::string_view name, std::string_view description)
Construct a new DriverFactory.
Definition driver.cpp:246
@ DT_VIDEO
A video driver.
Definition driver.h:42
std::unique_ptr< Driver > CreateInstance() const override
Create an instance of this driver-class.
Definition cocoa_v.h:131
std::optional< std::string_view > Start(const StringList &param) override
Start this driver.
Definition cocoa_v.mm:584
std::string_view GetName() const override
Return driver name.
Definition cocoa_v.h:115
CGContextRef cgcontext
Context reference for Quartz subdriver.
Definition cocoa_v.h:107
uint32_t palette[256]
Colour Palette.
Definition cocoa_v.h:101
int buffer_depth
Colour depth of used frame buffer.
Definition cocoa_v.h:93
int window_width
Current window width in pixel.
Definition cocoa_v.h:97
void CheckPaletteAnim() override
Process any pending palette animation.
Definition cocoa_v.mm:727
std::unique_ptr< uint32_t[]> window_buffer
Colour translation from palette to screen.
Definition cocoa_v.h:95
void AllocateBackingStore(bool force=false) override
Resize the window.
Definition cocoa_v.mm:633
void BlitIndexedToView32(int left, int top, int right, int bottom)
This function copies 8bpp pixels from the screen buffer in 32bpp windowed mode.
Definition cocoa_v.mm:692
std::unique_ptr< uint8_t[]> pixel_buffer
used for direct pixel access
Definition cocoa_v.h:94
void UpdatePalette(uint first_colour, uint num_colours)
Update the palette.
Definition cocoa_v.mm:712
void Stop() override
Stop Cocoa video driver.
Definition cocoa_v.mm:614
void Paint() override
Draw window.
Definition cocoa_v.mm:751
void * GetVideoPointer() override
Get a pointer to the video buffer.
Definition cocoa_v.h:125
int window_height
Current window height in pixel.
Definition cocoa_v.h:98
void GameSizeChanged()
Handle a change of the display area.
Definition cocoa_v.mm:302
bool ToggleFullscreen(bool fullscreen) override
Toggle between windowed and full screen mode for cocoa display driver.
Definition cocoa_v.mm:179
void UpdateVideoModes()
Update the video mode.
Definition cocoa_v.mm:322
std::vector< int > GetListOfMonitorRefreshRates() override
Get refresh rates of all connected monitors.
Definition cocoa_v.mm:234
Dimension GetScreenSize() const override
Get the resolution of the main screen.
Definition cocoa_v.mm:257
std::optional< std::string_view > Initialize()
Common driver initialization.
Definition cocoa_v.mm:104
bool refresh_sys_sprites
System sprites need refreshing.
Definition cocoa_v.h:26
void MainLoop() override
Start the main programme loop when using a cocoa video driver.
Definition cocoa_v.mm:134
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:85
virtual void * GetVideoPointer()=0
Get a pointer to the video buffer.
OTTD_CocoaWindowDelegate * delegate
Window delegate object.
Definition cocoa_v.h:35
bool IsFullscreen()
Are we in fullscreen mode?
Definition cocoa_v.mm:294
void InputLoop() override
Handle input logic, is CTRL pressed, should we fast-forward, etc.
Definition cocoa_v.mm:439
bool MakeWindow(int width, int height)
Build window and view with a given size.
Definition cocoa_v.mm:347
bool PollEvent() override
Poll and handle a single event from the OS.
Definition cocoa_v.mm:427
void MakeDirty(int left, int top, int width, int height) override
Set dirty a rectangle managed by a cocoa video subdriver.
Definition cocoa_v.mm:125
void PopulateSystemSprites() override
Populate all sprites in cache.
Definition cocoa_v.mm:202
bool LockVideoBuffer() override
Lock video buffer for drawing if it isn't already mapped.
Definition cocoa_v.mm:267
void EditBoxLostFocus() override
An edit box lost the input focus.
Definition cocoa_v.mm:223
void ClearSystemSprites() override
Clear all cached sprites.
Definition cocoa_v.mm:197
Rect dirty_rect
Region of the screen that needs redrawing.
Definition cocoa_v.h:63
void UnlockVideoBuffer() override
Unlock video buffer.
Definition cocoa_v.mm:279
void Stop() override
Stop Cocoa video driver.
Definition cocoa_v.mm:84
bool buffer_locked
Video buffer was locked by the main thread.
Definition cocoa_v.h:64
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
bool ChangeResolution(int w, int h) override
Change the resolution when using a cocoa video driver.
Definition cocoa_v.mm:150
bool AfterBlitterChange() override
Callback invoked after the blitter was changed.
Definition cocoa_v.mm:214
CGColorSpaceRef colour_space
Window colour space.
Definition cocoa_v.h:33
void MainLoopReal()
Main game loop.
Definition cocoa_v.mm:454
bool _cocoa_video_started
Is the Cocoa video driver running.
Definition cocoa_v.mm:44
All geometry types in OpenTTD.
Subclass of NSView to support mouse awareness and text input.
Definition cocoa_wnd.h:43
Delegate for our NSWindow to send ask for quit on close.
Definition cocoa_wnd.h:51
Subclass of NSWindow to cater our special needs.
Definition cocoa_wnd.h:33
#define Rect
Macro that prevents name conflicts between included headers.
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.
Base of all video drivers.