OpenTTD Source 20260211-master-g04436b7401
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
64 virtual void AllocateBackingStore(bool force = false) = 0;
65
66protected:
69
70 Dimension GetScreenSize() const override;
71 void InputLoop() override;
72 bool LockVideoBuffer() override;
73 void UnlockVideoBuffer() override;
74 bool PollEvent() override;
75
76 void GameSizeChanged();
77
78 std::optional<std::string_view> Initialize();
79
80 void UpdateVideoModes();
81
82 bool MakeWindow(int width, int height);
83
88 virtual NSView *AllocateDrawView() = 0;
89
94 virtual void *GetVideoPointer() = 0;
96 virtual void ReleaseVideoPointer() {}
97
98private:
99 bool IsFullscreen();
100};
101
102class VideoDriver_CocoaQuartz : public VideoDriver_Cocoa {
103private:
105 std::unique_ptr<uint8_t[]> pixel_buffer;
106 std::unique_ptr<uint32_t[]> window_buffer;
107
110 int window_pitch;
111
112 uint32_t palette[256];
113
114 void BlitIndexedToView32(int left, int top, int right, int bottom);
115 void UpdatePalette(uint first_colour, uint num_colours);
116
117public:
118 CGContextRef cgcontext;
119
120 VideoDriver_CocoaQuartz();
121
122 std::optional<std::string_view> Start(const StringList &param) override;
123 void Stop() override;
124
126 std::string_view GetName() const override { return "cocoa"; }
127
128 void AllocateBackingStore(bool force = false) override;
129
130protected:
131 void Paint() override;
132 void CheckPaletteAnim() override;
133
134 NSView *AllocateDrawView() override;
135
136 void *GetVideoPointer() override { return this->buffer_depth == 8 ? static_cast<void *>(this->pixel_buffer.get()) : static_cast<void *>(this->window_buffer.get()); }
137};
138
139class FVideoDriver_CocoaQuartz : public DriverFactoryBase {
140public:
141 FVideoDriver_CocoaQuartz() : DriverFactoryBase(Driver::DT_VIDEO, 8, "cocoa", "Cocoa Video Driver") {}
142 std::unique_ptr<Driver> CreateInstance() const override { return std::make_unique<VideoDriver_CocoaQuartz>(); }
143};
144
145#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:142
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:126
CGContextRef cgcontext
Context reference for Quartz subdriver.
Definition cocoa_v.h:118
uint32_t palette[256]
Colour Palette.
Definition cocoa_v.h:112
int buffer_depth
Colour depth of used frame buffer.
Definition cocoa_v.h:104
int window_width
Current window width in pixel.
Definition cocoa_v.h:108
void CheckPaletteAnim() override
Process any pending palette animation.
Definition cocoa_v.mm:723
std::unique_ptr< uint32_t[]> window_buffer
Colour translation from palette to screen.
Definition cocoa_v.h:106
NSView * AllocateDrawView() override
Allocate the view to show the game on.
Definition cocoa_v.mm:624
void AllocateBackingStore(bool force=false) override
Resize the window.
Definition cocoa_v.mm:629
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:688
std::unique_ptr< uint8_t[]> pixel_buffer
used for direct pixel access
Definition cocoa_v.h:105
void UpdatePalette(uint first_colour, uint num_colours)
Update the palette.
Definition cocoa_v.mm:708
void Stop() override
Stop Cocoa video driver.
Definition cocoa_v.mm:614
void Paint() override
Draw window.
Definition cocoa_v.mm:747
void * GetVideoPointer() override
Get a pointer to the video buffer.
Definition cocoa_v.h:136
int window_height
Current window height in pixel.
Definition cocoa_v.h:109
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:96
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
virtual NSView * AllocateDrawView()=0
Allocate the view to show the game on.
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:67
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:68
virtual void AllocateBackingStore(bool force=false)=0
Resize the window.
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.