summaryrefslogtreecommitdiff
path: root/src/video/cocoa/cocoa_v.h
blob: f9920c20e4f99f50d9f671837cff5a5df283af7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
 * This file is part of OpenTTD.
 * 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.
 * 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.
 * 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/>.
 */

/** @file cocoa_v.h The Cocoa video driver. */

#ifndef VIDEO_COCOA_H
#define VIDEO_COCOA_H

#include "../video_driver.hpp"
#include "../../core/geometry_type.hpp"


extern bool _cocoa_video_started;

class VideoDriver_Cocoa : public VideoDriver {
private:
	Dimension orig_res;   ///< Saved window size for non-fullscreen mode.

	int device_width;     ///< Width of device in pixel
	int device_height;    ///< Height of device in pixel

	int window_width;     ///< Current window width in pixel
	int window_height;    ///< Current window height in pixel
	int window_pitch;

	int buffer_depth;     ///< Colour depth of used frame buffer
	void *pixel_buffer;   ///< used for direct pixel access
	void *window_buffer;  ///< Colour translation from palette to screen

	static const int MAX_DIRTY_RECTS = 100;

	Rect dirty_rects[MAX_DIRTY_RECTS]; ///< dirty rectangles
	uint num_dirty_rects;  ///< Number of dirty rectangles
	uint32 palette[256];  ///< Colour Palette

public:
	bool active;          ///< Whether the window is visible
	bool setup;

	id window;            ///< Pointer to window object
	id cocoaview;         ///< Pointer to view object
	CGColorSpaceRef color_space; ///< Window color space
	CGContextRef cgcontext;      ///< Context reference for Quartz subdriver

public:
	VideoDriver_Cocoa();

	const char *Start(const StringList &param) override;
	void Stop() override;
	void MainLoop() override;

	void MakeDirty(int left, int top, int width, int height) override;
	bool AfterBlitterChange() override;

	bool ChangeResolution(int w, int h) override;
	bool ToggleFullscreen(bool fullscreen) override;

	void EditBoxLostFocus() override;

	/** Return driver name */
	const char *GetName() const override { return "cocoa"; }

	/* --- The following methods should be private, but can't be due to Obj-C limitations. --- */

	/** Main game loop. */
	void GameLoop(); // In event.mm.

	bool WindowResized();

protected:
	Dimension GetScreenSize() const override;

private:
	NSPoint GetMouseLocation(NSEvent *event);
	bool MouseIsInsideView(NSPoint *pt);
	CGPoint PrivateLocalToCG(NSPoint *p);
	bool PollEvent(); // In event.mm.
	void MouseMovedEvent(int x, int y); // In event.mm.

	bool IsFullscreen();
	void GameSizeChanged();

	void UpdateVideoModes();
	void GetDeviceInfo();
	bool SetVideoMode(int width, int height, int bpp);

	void UpdatePalette(uint first_color, uint num_colors);
	void CheckPaletteAnim();

	void Draw(bool force_update = false);
	void BlitIndexedToView32(int left, int top, int right, int bottom);
};

class FVideoDriver_Cocoa : public DriverFactoryBase {
public:
	FVideoDriver_Cocoa() : DriverFactoryBase(Driver::DT_VIDEO, 10, "cocoa", "Cocoa Video Driver") {}
	Driver *CreateInstance() const override { return new VideoDriver_Cocoa(); }
};

#endif /* VIDEO_COCOA_H */