OpenTTD Source 20260129-master-g2bb01bd0e4
cocoa_wnd.mm File Reference

Code related to OS interface for the cocoa video driver. More...

#include "../../stdafx.h"
#include "../../os/macosx/macos.h"
#include "../../os/macosx/macos_objective_c.h"
#include "../../openttd.h"
#include "../../debug.h"
#include "cocoa_v.h"
#include "cocoa_wnd.h"
#include "../../settings_type.h"
#include "../../string_func.h"
#include "../../gfx_func.h"
#include "../../window_func.h"
#include "../../window_gui.h"
#include "../../spritecache.h"
#include "../../textbuf_type.h"
#include "../../toolbar_gui.h"
#include "../../core/utf8.hpp"
#include "../../table/sprites.h"
#include "cocoa_keys.h"

Go to the source code of this file.

Data Structures

struct  TouchBarButton
 Structure to store information about single touch bar button. More...
 
class  OTTDMain
 The main class of the application, the application's delegate. More...
 

Functions

static NSUInteger CountUtf16Units (std::string_view str)
 Count the number of UTF-16 code points in a range of an UTF-8 string.
 
static size_t Utf8AdvanceByUtf16Units (std::string_view str, NSUInteger count)
 Advance an UTF-8 string by a number of equivalent UTF-16 code points.
 
static std::vector< char32_t > NSStringToUTF32 (NSString *s)
 Convert a NSString to an UTF-32 encoded string.
 
static void CGDataFreeCallback (void *, const void *data, size_t)
 Free memory where data was stored.
 
static NSImage * NSImageFromSprite (SpriteID sprite_id, ZoomLevel zoom)
 Render an OTTD sprite to a Cocoa image.
 
static void setApplicationMenu ()
 Initialize the application menu shown in top bar.
 
static void setupWindowMenu ()
 Create a window menu.
 
bool CocoaSetupApplication ()
 Startup the application.
 
void CocoaExitApplication ()
 Deregister app delegate.
 
void CocoaDialog (std::string_view title, std::string_view message, std::string_view buttonLabel)
 Catch asserts prior to initialization of the videodriver.
 

Variables

static const std::array< TouchBarButton, 9 > _touchbar_buttons
 Storage of defined touch bar buttons.
 
bool _allow_hidpi_window = true
 Storage for allow_hidpi setting. If true renders OTTD in native resolution.
 
NSString * OTTDMainLaunchGameEngine = @"@"ottdmain_launch_game_engine"
 Name of notification observer used to restart the game loop if necessary.
 
bool _tab_is_down
 Is tab button pressed.
 
static bool _cocoa_video_dialog = false
 True iff inside the scope of CocoaDialog method.
 
static OTTDMain_ottd_main
 App delegate instance of OTTDMain.
 
bool touchbar_created
 Whether the touchbar exists.
 
NSUInteger _current_mods
 Currently applied modifier flags.
 
bool _emulated_down
 Whether the mouse button is emulated or real.
 
bool _use_hidpi
 Render content in native resolution?
 

Detailed Description

Code related to OS interface for the cocoa video driver.

Attention
Notice regarding all modifications!!!!!!! There are certain limitations because the file is objective C++. gdb has limitations. C++ and objective C code can't be joined in all cases (classes stuff). Read http://developer.apple.com/releasenotes/Cocoa/Objective-C++.html for more information.
Todo:
List available resolutions.

Definition in file cocoa_wnd.mm.

Function Documentation

◆ CGDataFreeCallback()

static void CGDataFreeCallback ( void *  ,
const void *  data,
size_t   
)
static

Free memory where data was stored.

Parameters
dataPointer to memory that will be freed.

Definition at line 143 of file cocoa_wnd.mm.

Referenced by NSImageFromSprite().

◆ CocoaDialog()

void CocoaDialog ( std::string_view  title,
std::string_view  message,
std::string_view  buttonLabel 
)

Catch asserts prior to initialization of the videodriver.

Parameters
titleWindow title.
messageMessage text.
buttonLabelButton text.
Note
This is needed since sometimes assert is called before the videodriver is initialized.

Definition at line 379 of file cocoa_wnd.mm.

References _cocoa_video_dialog, _cocoa_video_started, CocoaSetupApplication(), VideoDriver::GetInstance(), and Driver::Stop().

Referenced by ShowMacDialog().

◆ CocoaExitApplication()

void CocoaExitApplication ( )

Deregister app delegate.

Definition at line 364 of file cocoa_wnd.mm.

Referenced by VideoDriver_Cocoa::Stop().

◆ CocoaSetupApplication()

bool CocoaSetupApplication ( )

Startup the application.

Returns
True iff startup was successful.

Definition at line 328 of file cocoa_wnd.mm.

References _ottd_main, Debug, setApplicationMenu(), and setupWindowMenu().

Referenced by CocoaDialog(), and VideoDriver_Cocoa::Initialize().

◆ CountUtf16Units()

static NSUInteger CountUtf16Units ( std::string_view  str)
static

Count the number of UTF-16 code points in a range of an UTF-8 string.

Parameters
strThe view in range of the string.
Returns
Number of UTF-16 code points in the range.

Definition at line 88 of file cocoa_wnd.mm.

◆ NSImageFromSprite()

static NSImage * NSImageFromSprite ( SpriteID  sprite_id,
ZoomLevel  zoom 
)
static

Render an OTTD sprite to a Cocoa image.

Parameters
sprite_idSprite to make a NSImage from.
zoomZoom level to render the sprite in.
Returns
Autorelease'd image or nullptr on any error.

Definition at line 154 of file cocoa_wnd.mm.

References CGDataFreeCallback(), DrawSpriteToRgbaBuffer(), and GetSpriteSize().

◆ NSStringToUTF32()

static std::vector< char32_t > NSStringToUTF32 ( NSString *  s)
static

Convert a NSString to an UTF-32 encoded string.

Parameters
sString to convert.
Returns
Vector of UTF-32 characters.

Definition at line 119 of file cocoa_wnd.mm.

References Utf16DecodeSurrogate(), Utf16IsLeadSurrogate(), and Utf16IsTrailSurrogate().

◆ setApplicationMenu()

static void setApplicationMenu ( )
static

Initialize the application menu shown in top bar.

Definition at line 257 of file cocoa_wnd.mm.

Referenced by CocoaSetupApplication().

◆ setupWindowMenu()

static void setupWindowMenu ( )
static

Create a window menu.

Definition at line 301 of file cocoa_wnd.mm.

Referenced by CocoaSetupApplication().

◆ Utf8AdvanceByUtf16Units()

static size_t Utf8AdvanceByUtf16Units ( std::string_view  str,
NSUInteger  count 
)
static

Advance an UTF-8 string by a number of equivalent UTF-16 code points.

Parameters
strUTF-8 string.
countNumber of UTF-16 code points to advance the string by.
Returns
Position inside str.

Definition at line 103 of file cocoa_wnd.mm.

Variable Documentation

◆ _allow_hidpi_window

bool _allow_hidpi_window = true

Storage for allow_hidpi setting. If true renders OTTD in native resolution.

See also
table/misc_settings.ini

Definition at line 69 of file cocoa_wnd.mm.

◆ _cocoa_video_dialog

bool _cocoa_video_dialog = false
static

True iff inside the scope of CocoaDialog method.

Definition at line 79 of file cocoa_wnd.mm.

Referenced by CocoaDialog().

◆ _current_mods

NSUInteger _current_mods
Initial value:
{
float _current_magnification

Currently applied modifier flags.

Definition at line 541 of file cocoa_wnd.mm.

◆ _emulated_down

bool _emulated_down

Whether the mouse button is emulated or real.

Definition at line 542 of file cocoa_wnd.mm.

◆ _ottd_main

OTTDMain* _ottd_main
static

App delegate instance of OTTDMain.

Definition at line 80 of file cocoa_wnd.mm.

Referenced by CocoaSetupApplication().

◆ _tab_is_down

bool _tab_is_down

Is tab button pressed.

Definition at line 77 of file cocoa_wnd.mm.

Referenced by VideoDriver_Cocoa::InputLoop().

◆ _touchbar_buttons

const std::array<TouchBarButton, 9> _touchbar_buttons
static
Initial value:
{{
{ @"openttd.pause", SPR_IMG_PAUSE, MTHK_PAUSE, @"Pause" },
{ @"openttd.fastforward", SPR_IMG_FASTFORWARD, MTHK_FASTFORWARD, @"Fast Forward" },
{ @"openttd.zoom_in", SPR_IMG_ZOOMIN, MTHK_ZOOM_IN, @"Zoom In" },
{ @"openttd.zoom_out", SPR_IMG_ZOOMOUT, MTHK_ZOOM_OUT, @"Zoom Out" },
{ @"openttd.build_rail", SPR_IMG_BUILDRAIL, MTHK_BUILD_RAIL, @"Rail" },
{ @"openttd.build_road", SPR_IMG_BUILDROAD, MTHK_BUILD_ROAD, @"Road" },
{ @"openttd.build_tram", SPR_IMG_BUILDTRAMS, MTHK_BUILD_TRAM, @"Tram" },
{ @"openttd.build_docks", SPR_IMG_BUILDWATER, MTHK_BUILD_DOCKS, @"Docks" },
{ @"openttd.build_airport", SPR_IMG_BUILDAIR, MTHK_BUILD_AIRPORT, @"Airport" }
}}

Storage of defined touch bar buttons.

Note
9 items can be displayed on the touch bar when using default buttons.

Definition at line 57 of file cocoa_wnd.mm.

◆ _use_hidpi

bool _use_hidpi

Render content in native resolution?

Definition at line 543 of file cocoa_wnd.mm.

◆ OTTDMainLaunchGameEngine

NSString* OTTDMainLaunchGameEngine = @"@"ottdmain_launch_game_engine"

Name of notification observer used to restart the game loop if necessary.

Definition at line 75 of file cocoa_wnd.mm.

Referenced by VideoDriver_Cocoa::MainLoop().

◆ touchbar_created

bool touchbar_created
Initial value:

Whether the touchbar exists.

Definition at line 432 of file cocoa_wnd.mm.