OpenTTD Source: src/video/cocoa/cocoa_v.h Source File

OpenTTD Source 15.0-RC2

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
10#ifndef VIDEO_COCOA_H
11#define VIDEO_COCOA_H
12
13#include "../video_driver.hpp"
14#include "../../core/geometry_type.hpp"
15
16
17extern bool _cocoa_video_started;
18
19@class OTTD_CocoaWindowDelegate;
20@class OTTD_CocoaWindow;
21@class OTTD_CocoaView;
22
23 class VideoDriver_Cocoa : public VideoDriver {
24private:
25 Dimension orig_res;
26 bool refresh_sys_sprites;
27
28public:
29 bool setup;
30
31 OTTD_CocoaWindow *window;
32 OTTD_CocoaView *cocoaview;
33 CGColorSpaceRef colour_space;
34
35 OTTD_CocoaWindowDelegate *delegate;
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:
63 Rect dirty_rect;
64 bool buffer_locked;
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
91 class VideoDriver_CocoaQuartz : public VideoDriver_Cocoa {
92private:
93 int buffer_depth;
94 std::unique_ptr<uint8_t[]> pixel_buffer;
95 std::unique_ptr<uint32_t[]> window_buffer;
96
97 int window_width;
98 int window_height;
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
128 class 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
Base for all driver factories.
Definition driver.h:57
Driver::DT_VIDEO
@ DT_VIDEO
A video driver.
Definition driver.h:42
FVideoDriver_CocoaQuartz
Definition cocoa_v.h:128
FVideoDriver_CocoaQuartz::CreateInstance
std::unique_ptr< Driver > CreateInstance() const override
Create an instance of this driver-class.
Definition cocoa_v.h:131
VideoDriver_CocoaQuartz
Definition cocoa_v.h:91
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:115
VideoDriver_CocoaQuartz::cgcontext
CGContextRef cgcontext
Context reference for Quartz subdriver.
Definition cocoa_v.h:107
VideoDriver_CocoaQuartz::palette
uint32_t palette[256]
Colour Palette.
Definition cocoa_v.h:101
VideoDriver_CocoaQuartz::buffer_depth
int buffer_depth
Colour depth of used frame buffer.
Definition cocoa_v.h:93
VideoDriver_CocoaQuartz::window_width
int window_width
Current window width in pixel.
Definition cocoa_v.h:97
VideoDriver_CocoaQuartz::CheckPaletteAnim
void CheckPaletteAnim() override
Process any pending palette animation.
VideoDriver_CocoaQuartz::window_buffer
std::unique_ptr< uint32_t[]> window_buffer
Colour translation from palette to screen.
Definition cocoa_v.h:95
VideoDriver_CocoaQuartz::pixel_buffer
std::unique_ptr< uint8_t[]> pixel_buffer
used for direct pixel access
Definition cocoa_v.h:94
VideoDriver_CocoaQuartz::Stop
void Stop() override
Stop this driver.
VideoDriver_CocoaQuartz::Paint
void Paint() override
Paint the window.
VideoDriver_CocoaQuartz::GetVideoPointer
void * GetVideoPointer() override
Get a pointer to the video buffer.
Definition cocoa_v.h:125
VideoDriver_CocoaQuartz::window_height
int window_height
Current window height in pixel.
Definition cocoa_v.h:98
VideoDriver_Cocoa
Definition cocoa_v.h:23
VideoDriver_Cocoa::ToggleFullscreen
bool ToggleFullscreen(bool fullscreen) override
Change the full screen setting.
VideoDriver_Cocoa::GetListOfMonitorRefreshRates
std::vector< int > GetListOfMonitorRefreshRates() override
Get a list of refresh rates of each available monitor.
VideoDriver_Cocoa::GetScreenSize
Dimension GetScreenSize() const override
Get the resolution of the main screen.
VideoDriver_Cocoa::refresh_sys_sprites
bool refresh_sys_sprites
System sprites need refreshing.
Definition cocoa_v.h:26
VideoDriver_Cocoa::MainLoop
void MainLoop() override
Perform the actual drawing.
VideoDriver_Cocoa::window
OTTD_CocoaWindow * window
Pointer to window object.
Definition cocoa_v.h:31
VideoDriver_Cocoa::cocoaview
OTTD_CocoaView * cocoaview
Pointer to view object.
Definition cocoa_v.h:32
VideoDriver_Cocoa::ReleaseVideoPointer
virtual void ReleaseVideoPointer()
Hand video buffer back to the drawing backend.
Definition cocoa_v.h:85
VideoDriver_Cocoa::GetVideoPointer
virtual void * GetVideoPointer()=0
Get a pointer to the video buffer.
VideoDriver_Cocoa::delegate
OTTD_CocoaWindowDelegate * delegate
Window delegate object.
Definition cocoa_v.h:35
VideoDriver_Cocoa::InputLoop
void InputLoop() override
Handle input logic, is CTRL pressed, should we fast-forward, etc.
VideoDriver_Cocoa::PollEvent
bool PollEvent() override
Process a single system event.
VideoDriver_Cocoa::MakeDirty
void MakeDirty(int left, int top, int width, int height) override
Mark a particular area dirty.
VideoDriver_Cocoa::PopulateSystemSprites
void PopulateSystemSprites() override
Populate all sprites in cache.
VideoDriver_Cocoa::LockVideoBuffer
bool LockVideoBuffer() override
Make sure the video buffer is ready for drawing.
VideoDriver_Cocoa::EditBoxLostFocus
void EditBoxLostFocus() override
An edit box lost the input focus.
VideoDriver_Cocoa::ClearSystemSprites
void ClearSystemSprites() override
Clear all cached sprites.
VideoDriver_Cocoa::dirty_rect
Rect dirty_rect
Region of the screen that needs redrawing.
Definition cocoa_v.h:63
VideoDriver_Cocoa::UnlockVideoBuffer
void UnlockVideoBuffer() override
Unlock a previously locked video buffer.
VideoDriver_Cocoa::Stop
void Stop() override
Stop this driver.
VideoDriver_Cocoa::buffer_locked
bool buffer_locked
Video buffer was locked by the main thread.
Definition cocoa_v.h:64
VideoDriver_Cocoa::orig_res
Dimension orig_res
Saved window size for non-fullscreen mode.
Definition cocoa_v.h:25
VideoDriver_Cocoa::setup
bool setup
Window is currently being created.
Definition cocoa_v.h:29
VideoDriver_Cocoa::ChangeResolution
bool ChangeResolution(int w, int h) override
Change the resolution of the window.
VideoDriver_Cocoa::AfterBlitterChange
bool AfterBlitterChange() override
Callback invoked after the blitter was changed.
VideoDriver_Cocoa::colour_space
CGColorSpaceRef colour_space
Window colour space.
Definition cocoa_v.h:33
VideoDriver
The base of all video drivers.
Definition video_driver.hpp:35
OTTD_CocoaView
Subclass of NSView to support mouse awareness and text input.
Definition cocoa_wnd.h:49
OTTD_CocoaWindowDelegate
Delegate for our NSWindow to send ask for quit on close.
Definition cocoa_wnd.h:57
OTTD_CocoaWindow
Subclass of NSWindow to cater our special needs.
Definition cocoa_wnd.h:37
StringList
std::vector< std::string > StringList
Type for a list of strings.
Definition string_type.h:60
Dimension
Dimensions (a width and height) of a rectangle in 2D.
Definition geometry_type.hpp:56
Rect
Specification of a rectangle with absolute coordinates of all edges.
Definition geometry_type.hpp:101