Merge branch 'master' into jgrpp
# Conflicts: # src/os/macosx/macos.mm # src/video/cocoa/cocoa_v.mm # src/video/cocoa/fullscreen.mm # src/video/cocoa/wnd_quartz.mm # src/video/cocoa/wnd_quickdraw.mmpull/175/head
commit
74b6981d97
@ -1,344 +0,0 @@
|
||||
/*
|
||||
* 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 qtmidi.cpp
|
||||
* @brief MIDI music player for MacOS X using QuickTime.
|
||||
*
|
||||
* This music player should work in all MacOS X releases starting from 10.0,
|
||||
* as QuickTime is an integral part of the system since the old days of the
|
||||
* Motorola 68k-based Macintoshes. The only extra dependency apart from
|
||||
* QuickTime itself is Carbon, which is included since 10.0 as well.
|
||||
*
|
||||
* QuickTime gets fooled with the MIDI files from Transport Tycoon Deluxe
|
||||
* because of the @c .gm suffix. To force QuickTime to load the MIDI files
|
||||
* without the need of dealing with the individual QuickTime components
|
||||
* needed to play music (data source, MIDI parser, note allocators,
|
||||
* synthesizers and the like) some Carbon functions are used to set the file
|
||||
* type as seen by QuickTime, using @c FSpSetFInfo() (which modifies the
|
||||
* file's resource fork).
|
||||
*/
|
||||
|
||||
|
||||
#ifndef NO_QUICKTIME
|
||||
|
||||
#include "../stdafx.h"
|
||||
#include "qtmidi.h"
|
||||
#include "midifile.hpp"
|
||||
#include "../debug.h"
|
||||
#include "../base_media_base.h"
|
||||
|
||||
#define Rect OTTD_Rect
|
||||
#define Point OTTD_Point
|
||||
#define WindowClass OTTD_WindowClass
|
||||
#include <QuickTime/QuickTime.h>
|
||||
#undef Rect
|
||||
#undef Point
|
||||
#undef WindowClass
|
||||
|
||||
#include "../safeguards.h"
|
||||
|
||||
static FMusicDriver_QtMidi iFMusicDriver_QtMidi;
|
||||
|
||||
|
||||
static const uint MIDI_TYPE = 'Midi'; ///< OSType code for MIDI songs.
|
||||
|
||||
|
||||
/**
|
||||
* Sets the @c OSType of a given file to @c 'Midi', but only if it's not
|
||||
* already set.
|
||||
*
|
||||
* @param *ref A @c FSSpec structure referencing a file.
|
||||
*/
|
||||
static void SetMIDITypeIfNeeded(const FSRef *ref)
|
||||
{
|
||||
FSCatalogInfo catalogInfo;
|
||||
|
||||
assert(ref);
|
||||
|
||||
if (noErr != FSGetCatalogInfo(ref, kFSCatInfoNodeFlags | kFSCatInfoFinderInfo, &catalogInfo, nullptr, nullptr, nullptr)) return;
|
||||
if (!(catalogInfo.nodeFlags & kFSNodeIsDirectoryMask)) {
|
||||
FileInfo * const info = (FileInfo *) catalogInfo.finderInfo;
|
||||
if (info->fileType != MIDI_TYPE && !(info->finderFlags & kIsAlias)) {
|
||||
OSErr e;
|
||||
info->fileType = MIDI_TYPE;
|
||||
e = FSSetCatalogInfo(ref, kFSCatInfoFinderInfo, &catalogInfo);
|
||||
if (e == noErr) {
|
||||
DEBUG(driver, 3, "qtmidi: changed filetype to 'Midi'");
|
||||
} else {
|
||||
DEBUG(driver, 0, "qtmidi: changing filetype to 'Midi' failed - error %d", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Loads a MIDI file and returns it as a QuickTime Movie structure.
|
||||
*
|
||||
* @param *path String with the path of an existing MIDI file.
|
||||
* @param *moov Pointer to a @c Movie where the result will be stored.
|
||||
* @return Whether the file was loaded and the @c Movie successfully created.
|
||||
*/
|
||||
static bool LoadMovieForMIDIFile(const char *path, Movie *moov)
|
||||
{
|
||||
int fd;
|
||||
int ret;
|
||||
char magic[4];
|
||||
FSRef fsref;
|
||||
FSSpec fsspec;
|
||||
short refnum = 0;
|
||||
short resid = 0;
|
||||
|
||||
assert(path != nullptr);
|
||||
assert(moov != nullptr);
|
||||
|
||||
DEBUG(driver, 2, "qtmidi: start loading '%s'...", path);
|
||||
|
||||
/*
|
||||
* XXX Manual check for MIDI header ('MThd'), as I don't know how to make
|
||||
* QuickTime load MIDI files without a .mid suffix without knowing it's
|
||||
* a MIDI file and setting the OSType of the file to the 'Midi' value.
|
||||
* Perhaps ugly, but it seems that it does the Right Thing(tm).
|
||||
*/
|
||||
fd = open(path, O_RDONLY, 0);
|
||||
if (fd == -1) return false;
|
||||
ret = read(fd, magic, 4);
|
||||
close(fd);
|
||||
if (ret < 4) return false;
|
||||
|
||||
DEBUG(driver, 3, "qtmidi: header is '%.4s'", magic);
|
||||
if (magic[0] != 'M' || magic[1] != 'T' || magic[2] != 'h' || magic[3] != 'd') {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (noErr != FSPathMakeRef((const UInt8 *) path, &fsref, nullptr)) return false;
|
||||
SetMIDITypeIfNeeded(&fsref);
|
||||
|
||||
if (noErr != FSGetCatalogInfo(&fsref, kFSCatInfoNone, nullptr, nullptr, &fsspec, nullptr)) return false;
|
||||
if (OpenMovieFile(&fsspec, &refnum, fsRdPerm) != noErr) return false;
|
||||
DEBUG(driver, 3, "qtmidi: '%s' successfully opened", path);
|
||||
|
||||
if (noErr != NewMovieFromFile(moov, refnum, &resid, nullptr,
|
||||
newMovieActive | newMovieDontAskUnresolvedDataRefs, nullptr)) {
|
||||
CloseMovieFile(refnum);
|
||||
return false;
|
||||
}
|
||||
DEBUG(driver, 3, "qtmidi: movie container created");
|
||||
|
||||
CloseMovieFile(refnum);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Flag which has the @c true value when QuickTime is available and
|
||||
* initialized.
|
||||
*/
|
||||
static bool _quicktime_started = false;
|
||||
|
||||
|
||||
/**
|
||||
* Initialize QuickTime if needed. This function sets the
|
||||
* #_quicktime_started flag to @c true if QuickTime is present in the system
|
||||
* and it was initialized properly.
|
||||
*/
|
||||
static void InitQuickTimeIfNeeded()
|
||||
{
|
||||
OSStatus dummy;
|
||||
|
||||
if (_quicktime_started) return;
|
||||
|
||||
DEBUG(driver, 2, "qtmidi: initializing Quicktime");
|
||||
/* Be polite: check whether QuickTime is available and initialize it. */
|
||||
_quicktime_started =
|
||||
(noErr == Gestalt(gestaltQuickTime, &dummy)) &&
|
||||
(noErr == EnterMovies());
|
||||
if (!_quicktime_started) DEBUG(driver, 0, "qtmidi: Quicktime initialization failed!");
|
||||
}
|
||||
|
||||
|
||||
/** Possible states of the QuickTime music driver. */
|
||||
enum QTStates {
|
||||
QT_STATE_IDLE, ///< No file loaded.
|
||||
QT_STATE_PLAY, ///< File loaded, playing.
|
||||
QT_STATE_STOP, ///< File loaded, stopped.
|
||||
};
|
||||
|
||||
|
||||
static Movie _quicktime_movie; ///< Current QuickTime @c Movie.
|
||||
static byte _quicktime_volume = 127; ///< Current volume.
|
||||
static int _quicktime_state = QT_STATE_IDLE; ///< Current player state.
|
||||
|
||||
|
||||
/**
|
||||
* Maps OpenTTD volume to QuickTime notion of volume.
|
||||
*/
|
||||
#define VOLUME ((short)((0x00FF & _quicktime_volume) << 1))
|
||||
|
||||
|
||||
/**
|
||||
* Initialized the MIDI player, including QuickTime initialization.
|
||||
*
|
||||
* @todo Give better error messages by inspecting error codes returned by
|
||||
* @c Gestalt() and @c EnterMovies(). Needs changes in
|
||||
* #InitQuickTimeIfNeeded.
|
||||
*/
|
||||
const char *MusicDriver_QtMidi::Start(const char * const *parm)
|
||||
{
|
||||
InitQuickTimeIfNeeded();
|
||||
return (_quicktime_started) ? nullptr : "can't initialize QuickTime";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Checks whether the player is active.
|
||||
*
|
||||
* This function is called at regular intervals from OpenTTD's main loop, so
|
||||
* we call @c MoviesTask() from here to let QuickTime do its work.
|
||||
*/
|
||||
bool MusicDriver_QtMidi::IsSongPlaying()
|
||||
{
|
||||
if (!_quicktime_started) return true;
|
||||
|
||||
switch (_quicktime_state) {
|
||||
case QT_STATE_IDLE:
|
||||
case QT_STATE_STOP:
|
||||
/* Do nothing. */
|
||||
break;
|
||||
|
||||
case QT_STATE_PLAY:
|
||||
MoviesTask(_quicktime_movie, 0);
|
||||
/* Check whether movie ended. */
|
||||
if (IsMovieDone(_quicktime_movie) ||
|
||||
(GetMovieTime(_quicktime_movie, nullptr) >=
|
||||
GetMovieDuration(_quicktime_movie))) {
|
||||
_quicktime_state = QT_STATE_STOP;
|
||||
}
|
||||
}
|
||||
|
||||
return _quicktime_state == QT_STATE_PLAY;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Stops the MIDI player.
|
||||
*
|
||||
* Stops playing and frees any used resources before returning. As it
|
||||
* deinitilizes QuickTime, the #_quicktime_started flag is set to @c false.
|
||||
*/
|
||||
void MusicDriver_QtMidi::Stop()
|
||||
{
|
||||
if (!_quicktime_started) return;
|
||||
|
||||
DEBUG(driver, 2, "qtmidi: stopping driver...");
|
||||
switch (_quicktime_state) {
|
||||
case QT_STATE_IDLE:
|
||||
DEBUG(driver, 3, "qtmidi: stopping not needed, already idle");
|
||||
/* Do nothing. */
|
||||
break;
|
||||
|
||||
case QT_STATE_PLAY:
|
||||
StopSong();
|
||||
FALLTHROUGH;
|
||||
|
||||
case QT_STATE_STOP:
|
||||
DisposeMovie(_quicktime_movie);
|
||||
}
|
||||
|
||||
ExitMovies();
|
||||
_quicktime_started = false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Starts playing a new song.
|
||||
*
|
||||
* @param filename Path to a MIDI file.
|
||||
*/
|
||||
void MusicDriver_QtMidi::PlaySong(const MusicSongInfo &song)
|
||||
{
|
||||
if (!_quicktime_started) return;
|
||||
|
||||
std::string filename = MidiFile::GetSMFFile(song);
|
||||
if (filename.empty()) return;
|
||||
|
||||
DEBUG(driver, 2, "qtmidi: trying to play '%s'", filename.c_str());
|
||||
switch (_quicktime_state) {
|
||||
case QT_STATE_PLAY:
|
||||
StopSong();
|
||||
DEBUG(driver, 3, "qtmidi: previous tune stopped");
|
||||
FALLTHROUGH;
|
||||
|
||||
case QT_STATE_STOP:
|
||||
DisposeMovie(_quicktime_movie);
|
||||
DEBUG(driver, 3, "qtmidi: previous tune disposed");
|
||||
_quicktime_state = QT_STATE_IDLE;
|
||||
FALLTHROUGH;
|
||||
|
||||
case QT_STATE_IDLE:
|
||||
LoadMovieForMIDIFile(filename.c_str(), &_quicktime_movie);
|
||||
SetMovieVolume(_quicktime_movie, VOLUME);
|
||||
StartMovie(_quicktime_movie);
|
||||
_quicktime_state = QT_STATE_PLAY;
|
||||
}
|
||||
DEBUG(driver, 3, "qtmidi: playing '%s'", filename.c_str());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Stops playing the current song, if the player is active.
|
||||
*/
|
||||
void MusicDriver_QtMidi::StopSong()
|
||||
{
|
||||
if (!_quicktime_started) return;
|
||||
|
||||
switch (_quicktime_state) {
|
||||
case QT_STATE_IDLE:
|
||||
FALLTHROUGH;
|
||||
|
||||
case QT_STATE_STOP:
|
||||
DEBUG(driver, 3, "qtmidi: stop requested, but already idle");
|
||||
/* Do nothing. */
|
||||
break;
|
||||
|
||||
case QT_STATE_PLAY:
|
||||
StopMovie(_quicktime_movie);
|
||||
_quicktime_state = QT_STATE_STOP;
|
||||
DEBUG(driver, 3, "qtmidi: player stopped");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Changes the playing volume of the MIDI player.
|
||||
*
|
||||
* As QuickTime controls volume in a per-movie basis, the desired volume is
|
||||
* stored in #_quicktime_volume, and the volume is set here using the
|
||||
* #VOLUME macro, @b and when loading new song in #PlaySong.
|
||||
*
|
||||
* @param vol The desired volume, range of the value is @c 0-127
|
||||
*/
|
||||
void MusicDriver_QtMidi::SetVolume(byte vol)
|
||||
{
|
||||
if (!_quicktime_started) return;
|
||||
|
||||
_quicktime_volume = vol;
|
||||
|
||||
DEBUG(driver, 2, "qtmidi: set volume to %u (%hi)", vol, VOLUME);
|
||||
switch (_quicktime_state) {
|
||||
case QT_STATE_IDLE:
|
||||
/* Do nothing. */
|
||||
break;
|
||||
|
||||
case QT_STATE_PLAY:
|
||||
case QT_STATE_STOP:
|
||||
SetMovieVolume(_quicktime_movie, VOLUME);
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* NO_QUICKTIME */
|
@ -1,37 +0,0 @@
|
||||
/*
|
||||
* 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 qtmidi.h Base of music playback via the QuickTime driver. */
|
||||
|
||||
#ifndef MUSIC_MACOSX_QUICKTIME_H
|
||||
#define MUSIC_MACOSX_QUICKTIME_H
|
||||
|
||||
#include "music_driver.hpp"
|
||||
|
||||
class MusicDriver_QtMidi : public MusicDriver {
|
||||
public:
|
||||
const char *Start(const char * const *param) override;
|
||||
|
||||
void Stop() override;
|
||||
|
||||
void PlaySong(const MusicSongInfo &song) override;
|
||||
|
||||
void StopSong() override;
|
||||
|
||||
bool IsSongPlaying() override;
|
||||
|
||||
void SetVolume(byte vol) override;
|
||||
const char *GetName() const override { return "qt"; }
|
||||
};
|
||||
|
||||
class FMusicDriver_QtMidi : public DriverFactoryBase {
|
||||
public:
|
||||
FMusicDriver_QtMidi() : DriverFactoryBase(Driver::DT_MUSIC, 5, "qt", "QuickTime MIDI Driver") {}
|
||||
Driver *CreateInstance() const override { return new MusicDriver_QtMidi(); }
|
||||
};
|
||||
|
||||
#endif /* MUSIC_MACOSX_QUICKTIME_H */
|
@ -1,523 +0,0 @@
|
||||
/*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
/******************************************************************************
|
||||
* Cocoa video driver *
|
||||
* Known things left to do: *
|
||||
* Scale© the old pixel buffer to the new one when switching resolution. *
|
||||
******************************************************************************/
|
||||
|
||||
#ifdef WITH_COCOA
|
||||
|
||||
#include "../../stdafx.h"
|
||||
|
||||
#if (MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_9)
|
||||
|
||||
#define Rect OTTDRect
|
||||
#define Point OTTDPoint
|
||||
#import <Cocoa/Cocoa.h>
|
||||
#undef Rect
|
||||
#undef Point
|
||||
|
||||
#include "../../debug.h"
|
||||
#include "../../core/geometry_type.hpp"
|
||||
#include "../../core/sort_func.hpp"
|
||||
#include "cocoa_v.h"
|
||||
#include "../../gfx_func.h"
|
||||
#include "../../thread.h"
|
||||
#include "../../os/macosx/macos.h"
|
||||
|
||||
/**
|
||||
* Important 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.
|
||||
*/
|
||||
|
||||
|
||||
/* From Menus.h (according to Xcode Developer Documentation) */
|
||||
extern "C" void ShowMenuBar();
|
||||
extern "C" void HideMenuBar();
|
||||
|
||||
|
||||
/* Structure for rez switch gamma fades
|
||||
* We can hide the monitor flicker by setting the gamma tables to 0
|
||||
*/
|
||||
#define QZ_GAMMA_TABLE_SIZE 256
|
||||
|
||||
struct OTTD_QuartzGammaTable {
|
||||
CGGammaValue red[QZ_GAMMA_TABLE_SIZE];
|
||||
CGGammaValue green[QZ_GAMMA_TABLE_SIZE];
|
||||
CGGammaValue blue[QZ_GAMMA_TABLE_SIZE];
|
||||
};
|
||||
|
||||
/* Add methods to get at private members of NSScreen.
|
||||
* Since there is a bug in Apple's screen switching code that does not update
|
||||
* this variable when switching to fullscreen, we'll set it manually (but only
|
||||
* for the main screen).
|
||||
*/
|
||||
@interface NSScreen (NSScreenAccess)
|
||||
- (void) setFrame:(NSRect)frame;
|
||||
@end
|
||||
|
||||
@implementation NSScreen (NSScreenAccess)
|
||||
- (void) setFrame:(NSRect)frame
|
||||
{
|
||||
/* The 64 bits libraries don't seem to know about _frame, so this hack won't work. */
|
||||
#ifndef __LP64__
|
||||
_frame = frame;
|
||||
#endif
|
||||
}
|
||||
@end
|
||||
|
||||
class FullscreenSubdriver : public CocoaSubdriver {
|
||||
CGDirectDisplayID display_id; ///< 0 == main display (only support single display)
|
||||
CFDictionaryRef cur_mode; ///< current mode of the display
|
||||
CFDictionaryRef save_mode; ///< original mode of the display
|
||||
CGDirectPaletteRef palette; ///< palette of an 8-bit display
|
||||
|
||||
|
||||
/* Gamma functions to try to hide the flash from a res switch
|
||||
* Fade the display from normal to black
|
||||
* Save gamma tables for fade back to normal
|
||||
*/
|
||||
uint32 FadeGammaOut(OTTD_QuartzGammaTable *table)
|
||||
{
|
||||
CGGammaValue redTable[QZ_GAMMA_TABLE_SIZE];
|
||||
CGGammaValue greenTable[QZ_GAMMA_TABLE_SIZE];
|
||||
CGGammaValue blueTable[QZ_GAMMA_TABLE_SIZE];
|
||||
|
||||
unsigned int actual;
|
||||
if (CGGetDisplayTransferByTable(this->display_id, QZ_GAMMA_TABLE_SIZE, table->red, table->green, table->blue, &actual) != CGDisplayNoErr
|
||||
|| actual != QZ_GAMMA_TABLE_SIZE) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
memcpy(redTable, table->red, sizeof(redTable));
|
||||
memcpy(greenTable, table->green, sizeof(greenTable));
|
||||
memcpy(blueTable, table->blue, sizeof(greenTable));
|
||||
|
||||
for (float percent = 1.0; percent >= 0.0; percent -= 0.01) {
|
||||
for (int j = 0; j < QZ_GAMMA_TABLE_SIZE; j++) {
|
||||
redTable[j] = redTable[j] * percent;
|
||||
greenTable[j] = greenTable[j] * percent;
|
||||
blueTable[j] = blueTable[j] * percent;
|
||||
}
|
||||
|
||||
if (CGSetDisplayTransferByTable(this->display_id, QZ_GAMMA_TABLE_SIZE, redTable, greenTable, blueTable) != CGDisplayNoErr) {
|
||||
CGDisplayRestoreColorSyncSettings();
|
||||
return 1;
|
||||
}
|
||||
|
||||
CSleep(10);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Fade the display from black to normal
|
||||
* Restore previously saved gamma values
|
||||
*/
|
||||
uint32 FadeGammaIn(const OTTD_QuartzGammaTable *table)
|
||||
{
|
||||
CGGammaValue redTable[QZ_GAMMA_TABLE_SIZE];
|
||||
CGGammaValue greenTable[QZ_GAMMA_TABLE_SIZE];
|
||||
CGGammaValue blueTable[QZ_GAMMA_TABLE_SIZE];
|
||||
|
||||
memset(redTable, 0, sizeof(redTable));
|
||||
memset(greenTable, 0, sizeof(greenTable));
|
||||
memset(blueTable, 0, sizeof(greenTable));
|
||||
|
||||
for (float percent = 0.0; percent <= 1.0; percent += 0.01) {
|
||||
for (int j = 0; j < QZ_GAMMA_TABLE_SIZE; j++) {
|
||||
redTable[j] = table->red[j] * percent;
|
||||
greenTable[j] = table->green[j] * percent;
|
||||
blueTable[j] = table->blue[j] * percent;
|
||||
}
|
||||
|
||||
if (CGSetDisplayTransferByTable(this->display_id, QZ_GAMMA_TABLE_SIZE, redTable, greenTable, blueTable) != CGDisplayNoErr) {
|
||||
CGDisplayRestoreColorSyncSettings();
|
||||
return 1;
|
||||
}
|
||||
|
||||
CSleep(10);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/** Wait for the VBL to occur (estimated since we don't have a hardware interrupt) */
|
||||
void WaitForVerticalBlank()
|
||||
{
|
||||
/* The VBL delay is based on Ian Ollmann's RezLib <iano@cco.caltech.edu> */
|
||||
|
||||
CFNumberRef refreshRateCFNumber = (const __CFNumber*)CFDictionaryGetValue(this->cur_mode, kCGDisplayRefreshRate);
|
||||
if (refreshRateCFNumber == nullptr) return;
|
||||
|
||||
double refreshRate;
|
||||
if (CFNumberGetValue(refreshRateCFNumber, kCFNumberDoubleType, &refreshRate) == 0) return;
|
||||
|
||||
if (refreshRate == 0) return;
|
||||
|
||||
double linesPerSecond = refreshRate * this->device_height;
|
||||
double target = this->device_height;
|
||||
|
||||
/* Figure out the first delay so we start off about right */
|
||||
double position = CGDisplayBeamPosition(this->display_id);
|
||||
if (position > target) position = 0;
|
||||
|
||||
double adjustment = (target - position) / linesPerSecond;
|
||||
|
||||
CSleep((uint32)adjustment * 1000);
|
||||
}
|
||||
|
||||
|
||||
bool SetVideoMode(int w, int h, int bpp)
|
||||
{
|
||||
/* Define this variables at the top (against coding style) because
|
||||
* otherwise GCC 4.2 barfs at the goto's jumping over variable initialization. */
|
||||
NSRect screen_rect;
|
||||
int gamma_error;
|
||||
NSPoint mouseLocation;
|
||||
|
||||
/* Destroy any previous mode */
|
||||
if (this->pixel_buffer != nullptr) {
|
||||
free(this->pixel_buffer);
|
||||
this->pixel_buffer = nullptr;
|
||||
}
|
||||
|
||||
/* See if requested mode exists */
|
||||
boolean_t exact_match;
|
||||
this->cur_mode = CGDisplayBestModeForParameters(this->display_id, bpp, w, h, &exact_match);
|
||||
|
||||
/* If the mode wasn't an exact match, check if it has the right bpp, and update width and height */
|
||||
if (!exact_match) {
|
||||
int act_bpp;
|
||||
CFNumberRef number = (const __CFNumber*) CFDictionaryGetValue(this->cur_mode, kCGDisplayBitsPerPixel);
|
||||
CFNumberGetValue(number, kCFNumberSInt32Type, &act_bpp);
|
||||
if (act_bpp != bpp) {
|
||||
DEBUG(driver, 0, "Failed to find display resolution");
|
||||
goto ERR_NO_MATCH;
|
||||
}
|
||||
|
||||
number = (const __CFNumber*)CFDictionaryGetValue(this->cur_mode, kCGDisplayWidth);
|
||||
CFNumberGetValue(number, kCFNumberSInt32Type, &w);
|
||||
|
||||
number = (const __CFNumber*)CFDictionaryGetValue(this->cur_mode, kCGDisplayHeight);
|
||||
CFNumberGetValue(number, kCFNumberSInt32Type, &h);
|
||||
}
|
||||
|
||||
/* Capture the main screen */
|
||||
CGDisplayCapture(this->display_id);
|
||||
|
||||
/* Store the mouse coordinates relative to the total screen */
|
||||
mouseLocation = [ NSEvent mouseLocation ];
|
||||
mouseLocation.x /= this->device_width;
|
||||
mouseLocation.y /= this->device_height;
|
||||
|
||||
/* Fade display to zero gamma */
|
||||
OTTD_QuartzGammaTable gamma_table;
|
||||
gamma_error = this->FadeGammaOut(&gamma_table);
|
||||
|
||||
/* Put up the blanking window (a window above all other windows) */
|
||||
if (CGDisplayCapture(this->display_id) != CGDisplayNoErr ) {
|
||||
DEBUG(driver, 0, "Failed capturing display");
|
||||
goto ERR_NO_CAPTURE;
|
||||
}
|
||||
|
||||
/* Do the physical switch */
|
||||
if (CGDisplaySwitchToMode(this->display_id, this->cur_mode) != CGDisplayNoErr) {
|
||||
DEBUG(driver, 0, "Failed switching display resolution");
|
||||
goto ERR_NO_SWITCH;
|
||||
}
|
||||
|
||||
/* Since CGDisplayBaseAddress and CGDisplayBytesPerRow are no longer available on 10.7,
|
||||
* disable until a replacement can be found. */
|
||||
if (MacOSVersionIsAtLeast(10, 7, 0)) {
|
||||
this->window_buffer = nullptr;
|
||||
this->window_pitch = 0;
|
||||
} else {
|
||||
#if (MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7)
|
||||
this->window_buffer = CGDisplayBaseAddress(this->display_id);
|
||||
this->window_pitch = CGDisplayBytesPerRow(this->display_id);
|
||||
#endif
|
||||
}
|
||||
|
||||
this->device_width = CGDisplayPixelsWide(this->display_id);
|
||||
this->device_height = CGDisplayPixelsHigh(this->display_id);
|
||||
this->device_depth = bpp;
|
||||
|
||||
/* Setup double-buffer emulation */
|
||||
this->pixel_buffer = malloc(this->device_width * this->device_height * this->device_depth / 8);
|
||||
if (this->pixel_buffer == nullptr) {
|
||||
DEBUG(driver, 0, "Failed to allocate memory for double buffering");
|
||||
goto ERR_DOUBLEBUF;
|
||||
}
|
||||
|
||||
if (this->device_depth == 8 && !CGDisplayCanSetPalette(this->display_id)) {
|
||||
DEBUG(driver, 0, "Not an indexed display mode.");
|
||||
goto ERR_NOT_INDEXED;
|
||||
}
|
||||
|
||||
/* If we don't hide menu bar, it will get events and interrupt the program */
|
||||
HideMenuBar();
|
||||
|
||||
/* Hide the OS cursor */
|
||||
CGDisplayHideCursor(this->display_id);
|
||||
|
||||
/* Fade the display to original gamma */
|
||||
if (!gamma_error) FadeGammaIn(&gamma_table);
|
||||
|
||||
/* There is a bug in Cocoa where NSScreen doesn't synchronize
|
||||
* with CGDirectDisplay, so the main screen's frame is wrong.
|
||||
* As a result, coordinate translation produces incorrect results.
|
||||
* We can hack around this bug by setting the screen rect ourselves.
|
||||
* This hack should be removed if/when the bug is fixed.
|
||||
*/
|
||||
screen_rect = NSMakeRect(0, 0, this->device_width, this->device_height);
|
||||
[ [ NSScreen mainScreen ] setFrame:screen_rect ];
|
||||
|
||||
this->UpdatePalette(0, 256);
|
||||
|
||||
/* Move the mouse cursor to approx. the same location */
|
||||
CGPoint display_mouseLocation;
|
||||
display_mouseLocation.x = mouseLocation.x * this->device_width;
|
||||
display_mouseLocation.y = this->device_height - (mouseLocation.y * this->device_height);
|
||||
|
||||
_cursor.in_window = true;
|
||||
|
||||
CGDisplayMoveCursorToPoint(this->display_id, display_mouseLocation);
|
||||
|
||||
return true;
|
||||
|
||||
/* Since the blanking window covers *all* windows (even force quit) correct recovery is crucial */
|
||||
ERR_NOT_INDEXED:
|
||||
free(this->pixel_buffer);
|
||||
this->pixel_buffer = nullptr;
|
||||
ERR_DOUBLEBUF:
|
||||
CGDisplaySwitchToMode(this->display_id, this->save_mode);
|
||||
ERR_NO_SWITCH:
|
||||
CGReleaseAllDisplays();
|
||||
ERR_NO_CAPTURE:
|
||||
if (!gamma_error) this->FadeGammaIn(&gamma_table);
|
||||
ERR_NO_MATCH:
|
||||
this->device_width = 0;
|
||||
this->device_height = 0;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void RestoreVideoMode()
|
||||
{
|
||||
/* Release fullscreen resources */
|
||||
OTTD_QuartzGammaTable gamma_table;
|
||||
int gamma_error = this->FadeGammaOut(&gamma_table);
|
||||
|
||||
/* Restore original screen resolution/bpp */
|
||||
CGDisplaySwitchToMode(this->display_id, this->save_mode);
|
||||
|
||||
CGReleaseAllDisplays();
|
||||
|
||||
/* Bring back the cursor */
|
||||
CGDisplayShowCursor(this->display_id);
|
||||
|
||||
ShowMenuBar();
|
||||
|
||||
/* Reset the main screen's rectangle
|
||||
* See comment in SetVideoMode for why we do this */
|
||||
NSRect screen_rect = NSMakeRect(0, 0, CGDisplayPixelsWide(this->display_id), CGDisplayPixelsHigh(this->display_id));
|
||||
[ [ NSScreen mainScreen ] setFrame:screen_rect ];
|
||||
|
||||
/* Destroy the pixel buffer */
|
||||
if (this->pixel_buffer != nullptr) {
|
||||
free(this->pixel_buffer);
|
||||
this->pixel_buffer = nullptr;
|
||||
}
|
||||
|
||||
if (!gamma_error) this->FadeGammaIn(&gamma_table);
|
||||
|
||||
this->device_width = CGDisplayPixelsWide(this->display_id);
|
||||
this->device_height = CGDisplayPixelsHigh(this->display_id);
|
||||
}
|
||||
|
||||
public:
|
||||
FullscreenSubdriver()
|
||||
{
|
||||
/* Initialize the video settings; this data persists between mode switches */
|
||||
this->display_id = kCGDirectMainDisplay;
|
||||
this->save_mode = CGDisplayCurrentMode(this->display_id);
|
||||
|
||||
this->palette = CGPaletteCreateDefaultColorPalette();
|
||||
|
||||
this->device_width = CGDisplayPixelsWide(this->display_id);
|
||||
this->device_height = CGDisplayPixelsHigh(this->display_id);
|
||||
this->device_depth = 0;
|
||||
this->pixel_buffer = nullptr;
|
||||
|
||||
this->num_dirty_rects = MAX_DIRTY_RECTS;
|
||||
}
|
||||
|
||||
virtual ~FullscreenSubdriver()
|
||||
{
|
||||
this->RestoreVideoMode();
|
||||
}
|
||||
|
||||
virtual void Draw(bool force_update)
|
||||
{
|
||||
const uint8 *src = (uint8 *)this->pixel_buffer;
|
||||
uint8 *dst = (uint8 *)this->window_buffer;
|
||||
uint pitch = this->window_pitch;
|
||||
uint width = this->device_width;
|
||||
uint num_dirty = this->num_dirty_rects;
|
||||
uint bytesperpixel = this->device_depth / 8;
|
||||
|
||||
/* Check if we need to do anything */
|
||||
if (num_dirty == 0) return;
|
||||
|
||||
if (num_dirty >= MAX_DIRTY_RECTS) {
|
||||
num_dirty = 1;
|
||||
this->dirty_rects[0].left = 0;
|
||||
this->dirty_rects[0].top = 0;
|
||||
this->dirty_rects[0].right = this->device_width;
|
||||
this->dirty_rects[0].bottom = this->device_height;
|
||||
}
|
||||
|
||||
WaitForVerticalBlank();
|
||||
/* Build the region of dirty rectangles */
|
||||
for (uint i = 0; i < num_dirty; i++) {
|
||||
uint y = this->dirty_rects[i].top;
|
||||
uint left = this->dirty_rects[i].left;
|
||||
uint length = this->dirty_rects[i].right - left;
|
||||
uint bottom = this->dirty_rects[i].bottom;
|
||||
|
||||
for (; y < bottom; y++) {
|
||||
memcpy(dst + y * pitch + left * bytesperpixel, src + y * width * bytesperpixel + left * bytesperpixel, length * bytesperpixel);
|
||||
}
|
||||
}
|
||||
|
||||
this->num_dirty_rects = 0;
|
||||
}
|
||||
|
||||
virtual void MakeDirty(int left, int top, int width, int height)
|
||||
{
|
||||
if (this->num_dirty_rects < MAX_DIRTY_RECTS) {
|
||||
this->dirty_rects[this->num_dirty_rects].left = left;
|
||||
this->dirty_rects[this->num_dirty_rects].top = top;
|
||||
this->dirty_rects[this->num_dirty_rects].right = left + width;
|
||||
this->dirty_rects[this->num_dirty_rects].bottom = top + height;
|
||||
}
|
||||
this->num_dirty_rects++;
|
||||
}
|
||||
|
||||
virtual void UpdatePalette(uint first_color, uint num_colors)
|
||||
{
|
||||
if (this->device_depth != 8) return;
|
||||
|
||||
for (uint32_t index = first_color; index < first_color + num_colors; index++) {
|
||||
/* Clamp colors between 0.0 and 1.0 */
|
||||
CGDeviceColor color;
|
||||
color.red = _cur_palette.palette[index].r / 255.0;
|
||||
color.blue = _cur_palette.palette[index].b / 255.0;
|
||||
color.green = _cur_palette.palette[index].g / 255.0;
|
||||
|
||||
CGPaletteSetColorAtIndex(this->palette, color, index);
|
||||
}
|
||||
|
||||
CGDisplaySetPalette(this->display_id, this->palette);
|
||||
}
|
||||
|
||||
virtual uint ListModes(OTTD_Point *modes, uint max_modes)
|
||||
{
|
||||
return QZ_ListModes(modes, max_modes, this->display_id, this->device_depth);
|
||||
}
|
||||
|
||||
virtual bool ChangeResolution(int w, int h, int bpp)
|
||||
{
|
||||
int old_width = this->device_width;
|
||||
int old_height = this->device_height;
|
||||
int old_bpp = this->device_depth;
|
||||
|
||||
if (bpp != 8 && bpp != 32) error("Cocoa: This video driver only supports 8 and 32 bpp blitters.");
|
||||
|
||||
if (SetVideoMode(w, h, bpp)) return true;
|
||||
if (old_width != 0 && old_height != 0) SetVideoMode(old_width, old_height, old_bpp);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
virtual bool IsFullscreen()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
virtual int GetWidth()
|
||||
{
|
||||
return this->device_width;
|
||||
}
|
||||
|
||||
virtual int GetHeight()
|
||||
{
|
||||
return this->device_height;
|
||||
}
|
||||
|
||||
virtual void *GetPixelBuffer()
|
||||
{
|
||||
return this->pixel_buffer;
|
||||
}
|
||||
|
||||
/*
|
||||
* Convert local coordinate to window server (CoreGraphics) coordinate.
|
||||
* In fullscreen mode this just means copying the coords.
|
||||
*/
|
||||
virtual CGPoint PrivateLocalToCG(NSPoint *p)
|
||||
{
|
||||
return CGPointMake(p->x, p->y);
|
||||
}
|
||||
|
||||
virtual NSPoint GetMouseLocation(NSEvent *event)
|
||||
{
|
||||
NSPoint pt = [ NSEvent mouseLocation ];
|
||||
pt.y = this->device_height - pt.y;
|
||||
|
||||
return pt;
|
||||
}
|
||||
|
||||
virtual bool MouseIsInsideView(NSPoint *pt)
|
||||
{
|
||||
return pt->x >= 0 && pt->y >= 0 && pt->x < this->device_width && pt->y < this->device_height;
|
||||
}
|
||||
|
||||
virtual bool IsActive()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
CocoaSubdriver *QZ_CreateFullscreenSubdriver(int width, int height, int bpp)
|
||||
{
|
||||
/* OSX 10.7 doesn't support this way of the fullscreen driver. If we end up here
|
||||
* OpenTTD was compiled without SDK 10.7 available and - and thus we don't support
|
||||
* fullscreen mode in OSX 10.7 or higher, as necessary elements for this way have
|
||||
* been removed from the API.
|
||||
*/
|
||||
if (MacOSVersionIsAtLeast(10, 7, 0)) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
FullscreenSubdriver *ret = new FullscreenSubdriver();
|
||||
|
||||
if (!ret->ChangeResolution(width, height, bpp)) {
|
||||
delete ret;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endif /* (MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_9) */
|
||||
#endif /* WITH_COCOA */
|
@ -1,561 +0,0 @@
|
||||
/*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
/******************************************************************************
|
||||
* Cocoa video driver *
|
||||
* Known things left to do: *
|
||||
* List available resolutions. *
|
||||
******************************************************************************/
|
||||
|
||||
#ifdef WITH_COCOA
|
||||
#ifdef ENABLE_COCOA_QUICKDRAW
|
||||
|
||||
#define MAC_OS_X_VERSION_MIN_REQUIRED MAC_OS_X_VERSION_10_3
|
||||
#include "../../stdafx.h"
|
||||
#include "../../os/macosx/macos.h"
|
||||
|
||||
#define Rect OTTDRect
|
||||
#define Point OTTDPoint
|
||||
#import <Cocoa/Cocoa.h>
|
||||
#undef Rect
|
||||
#undef Point
|
||||
|
||||
#include "../../debug.h"
|
||||
#include "../../rev.h"
|
||||
#include "../../core/geometry_type.hpp"
|
||||
#include "cocoa_v.h"
|
||||
#include "../../core/math_func.hpp"
|
||||
#include "../../gfx_func.h"
|
||||
#include "../../framerate_type.h"
|
||||
|
||||
/**
|
||||
* Important 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.
|
||||
*/
|
||||
|
||||
|
||||
class WindowQuickdrawSubdriver;
|
||||
|
||||
|
||||
class WindowQuickdrawSubdriver : public CocoaSubdriver {
|
||||
private:
|
||||
/**
|
||||
* This function copies 32bpp pixels from the screen buffer in 16bpp windowed mode.
|
||||
*
|
||||
* @param left The x coord for the left edge of the box to blit.
|
||||
* @param top The y coord for the top edge of the box to blit.
|
||||
* @param right The x coord for the right edge of the box to blit.
|
||||
* @param bottom The y coord for the bottom edge of the box to blit.
|
||||
*/
|
||||
void Blit32ToView32(int left, int top, int right, int bottom);
|
||||
|
||||
/**
|
||||
* This function copies 8bpp pixels from the screen buffer in 32bpp windowed mode.
|
||||
*
|
||||
* @param left The x coord for the left edge of the box to blit.
|
||||
* @param top The y coord for the top edge of the box to blit.
|
||||
* @param right The x coord for the right edge of the box to blit.
|
||||
* @param bottom The y coord for the bottom edge of the box to blit.
|
||||
*/
|
||||
void BlitIndexedToView32(int left, int top, int right, int bottom);
|
||||
|
||||
/**
|
||||
* This function copies 8bpp pixels from the screen buffer in 16bpp windowed mode.
|
||||
*
|
||||
* @param left The x coord for the left edge of the box to blit.
|
||||
* @param top The y coord for the top edge of the box to blit.
|
||||
* @param right The x coord for the right edge of the box to blit.
|
||||
* @param bottom The y coord for the bottom edge of the box to blit.
|
||||
*/
|
||||
void BlitIndexedToView16(int left, int top, int right, int bottom);
|
||||
|
||||
inline void BlitToView(int left, int top, int right, int bottom);
|
||||
void DrawResizeIcon();
|
||||
|
||||
virtual void GetDeviceInfo();
|
||||
virtual bool SetVideoMode(int width, int height, int bpp);
|
||||
|
||||
public:
|
||||
WindowQuickdrawSubdriver();
|
||||
virtual ~WindowQuickdrawSubdriver();
|
||||
|
||||
virtual void Draw(bool force_update);
|
||||
virtual void MakeDirty(int left, int top, int width, int height);
|
||||
virtual void UpdatePalette(uint first_color, uint num_colors);
|
||||
|
||||
virtual uint ListModes(OTTD_Point *modes, uint max_modes);
|
||||
|
||||
virtual bool ChangeResolution(int w, int h, int bpp);
|
||||
|
||||
virtual bool IsFullscreen() { return false; }
|
||||
|
||||
virtual int GetWidth() { return window_width; }
|
||||
virtual int GetHeight() { return window_height; }
|
||||
virtual void *GetPixelBuffer() { return pixel_buffer; }
|
||||
|
||||
/* Convert local coordinate to window server (CoreGraphics) coordinate */
|
||||
virtual CGPoint PrivateLocalToCG(NSPoint *p);
|
||||
|
||||
virtual NSPoint GetMouseLocation(NSEvent *event);
|
||||
virtual bool MouseIsInsideView(NSPoint *pt);
|
||||
|
||||
virtual bool IsActive() { return active; }
|
||||
|
||||
void SetPortAlphaOpaque();
|
||||
bool WindowResized();
|
||||
};
|
||||
|
||||
static const int _resize_icon_width = 16;
|
||||
static const int _resize_icon_height = 16;
|
||||
|
||||
static bool _resize_icon[] = {
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1,
|
||||
0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1,
|
||||
0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0,
|
||||
0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0,
|
||||
0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1,
|
||||
0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1,
|
||||
0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0,
|
||||
1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0
|
||||
};
|
||||
|
||||
|
||||
void WindowQuickdrawSubdriver::GetDeviceInfo()
|
||||
{
|
||||
/* Initialize the video settings; this data persists between mode switches */
|
||||
CFDictionaryRef cur_mode = CGDisplayCurrentMode(kCGDirectMainDisplay);
|
||||
|
||||
/* Gather some information that is useful to know about the display */
|
||||
CFNumberGetValue((const __CFNumber*)CFDictionaryGetValue(cur_mode, kCGDisplayBitsPerPixel),
|
||||
kCFNumberSInt32Type, &this->device_depth);
|
||||
|
||||
CFNumberGetValue((const __CFNumber*)CFDictionaryGetValue(cur_mode, kCGDisplayWidth),
|
||||
kCFNumberSInt32Type, &this->device_width);
|
||||
|
||||
CFNumberGetValue((const __CFNumber*)CFDictionaryGetValue(cur_mode, kCGDisplayHeight),
|
||||
kCFNumberSInt32Type, &this->device_height);
|
||||
}
|
||||
|
||||
bool WindowQuickdrawSubdriver::SetVideoMode(int width, int height, int bpp)
|
||||
{
|
||||
this->setup = true;
|
||||
this->GetDeviceInfo();
|
||||
|
||||
if (bpp > this->device_depth) {
|
||||
DEBUG(driver, 0, "Cannot use a blitter with a higher screen depth than the display when running in windowed mode.");
|
||||
this->setup = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (width > this->device_width) width = this->device_width;
|
||||
if (height > this->device_height) height = this->device_height;
|
||||
|
||||
NSRect contentRect = NSMakeRect(0, 0, width, height);
|
||||
|
||||
/* Check if we should recreate the window */
|
||||
if (this->window == nil) {
|
||||
OTTD_CocoaWindowDelegate *delegate;
|
||||
|
||||
/* Set the window style */
|
||||
unsigned int style = NSTitledWindowMask;
|
||||
style |= (NSMiniaturizableWindowMask | NSClosableWindowMask);
|
||||
style |= NSResizableWindowMask;
|
||||
|
||||
/* Manually create a window, avoids having a nib file resource */
|
||||
this->window = [ [ OTTD_CocoaWindow alloc ] initWithContentRect:contentRect
|
||||
styleMask:style backing:NSBackingStoreBuffered defer:NO ];
|
||||
|
||||
if (this->window == nil) {
|
||||
DEBUG(driver, 0, "Could not create the Cocoa window.");
|
||||
this->setup = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
[ this->window setDriver:this ];
|
||||
|
||||
char caption[50];
|
||||
snprintf(caption, sizeof(caption), "OpenTTD %s", _openttd_revision);
|
||||
NSString *nsscaption = [ [ NSString alloc ] initWithUTF8String:caption ];
|
||||
[ this->window setTitle:nsscaption ];
|
||||
[ this->window setMiniwindowTitle:nsscaption ];
|
||||
[ nsscaption release ];
|
||||
|
||||
[ this->window setContentMinSize:NSMakeSize(64.0f, 64.0f) ];
|
||||
|
||||
[ this->window setAcceptsMouseMovedEvents:YES ];
|
||||
[ this->window setViewsNeedDisplay:NO ];
|
||||
|
||||
delegate = [ [ OTTD_CocoaWindowDelegate alloc ] init ];
|
||||
[ delegate setDriver:this ];
|
||||
[ this->window setDelegate: [ delegate autorelease ] ];
|
||||
} else {
|
||||
/* We already have a window, just change its size */
|
||||
[ this->window setContentSize:contentRect.size ];
|
||||
/* Ensure frame height - title bar height >= view height
|
||||
* The height of title bar of the window is 22 pixels */
|
||||
contentRect.size.height = Clamp(height, 0, [ this->window frame ].size.height - 22);
|
||||
height = contentRect.size.height;
|
||||
[ this->cocoaview setFrameSize:contentRect.size ];
|
||||
}
|
||||
|
||||
/* Update again */
|
||||
this->window_width = width;
|
||||
this->window_height = height;
|
||||
this->buffer_depth = bpp;
|
||||
|
||||
[ this->window center ];
|
||||
|
||||
/* Only recreate the view if it doesn't already exist */
|
||||
if (this->cocoaview == nil) {
|
||||
this->cocoaview = [ [ NSQuickDrawView alloc ] initWithFrame:contentRect ];
|
||||
if (this->cocoaview == nil) {
|
||||
DEBUG(driver, 0, "Could not create the Quickdraw view.");
|
||||
this->setup = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
[ this->cocoaview setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable ];
|
||||
[ [ this->window contentView ] addSubview:this->cocoaview ];
|
||||
[ this->cocoaview release ];
|
||||
[ this->window makeKeyAndOrderFront:nil ];
|
||||
}
|
||||
|
||||
bool ret = this->WindowResized();
|
||||
this->UpdatePalette(0, 256);
|
||||
|
||||
this->setup = false;
|
||||
return ret;
|
||||
}
|
||||
|
||||
void WindowQuickdrawSubdriver::Blit32ToView32(int left, int top, int right, int bottom)
|
||||
{
|
||||
const uint32 *src = (uint32*)this->pixel_buffer;
|
||||
uint32 *dst = (uint32*)this->window_buffer;
|
||||
uint width = this->window_width;
|
||||
uint pitch = this->window_pitch / 4;
|
||||
|
||||
dst += top * pitch + left;
|
||||
src += top * width + left;
|
||||
|
||||
for (int y = top; y < bottom; y++, dst+= pitch, src+= width) {
|
||||
memcpy(dst, src, (right - left) * 4);
|
||||
}
|
||||
}
|
||||
|
||||
void WindowQuickdrawSubdriver::BlitIndexedToView32(int left, int top, int right, int bottom)
|
||||
{
|
||||
const uint32 *pal = this->palette;
|
||||
const uint8 *src = (uint8*)this->pixel_buffer;
|
||||
uint32 *dst = (uint32*)this->window_buffer;
|
||||
uint width = this->window_width;
|
||||
uint pitch = this->window_pitch / 4;
|
||||
|
||||
for (int y = top; y < bottom; y++) {
|
||||
for (int x = left; x < right; x++) {
|
||||
dst[y * pitch + x] = pal[src[y * width + x]];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void WindowQuickdrawSubdriver::BlitIndexedToView16(int left, int top, int right, int bottom)
|
||||
{
|
||||
const uint32 *pal = this->palette;
|
||||
const uint8 *src = (uint8*)this->pixel_buffer;
|
||||
uint16 *dst = (uint16*)this->window_buffer;
|
||||
uint width = this->window_width;
|
||||
uint pitch = this->window_pitch / 2;
|
||||
|
||||
for (int y = top; y < bottom; y++) {
|
||||
for (int x = left; x < right; x++) {
|
||||
dst[y * pitch + x] = pal[src[y * width + x]];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
inline void WindowQuickdrawSubdriver::BlitToView(int left, int top, int right, int bottom)
|
||||
{
|
||||
switch (this->device_depth) {
|
||||
case 32:
|
||||
switch (this->buffer_depth) {
|
||||
case 32:
|
||||
this->Blit32ToView32(left, top, right, bottom);
|
||||
break;
|
||||
case 8:
|
||||
this->BlitIndexedToView32(left, top, right, bottom);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case 16:
|
||||
this->BlitIndexedToView16(left, top, right, bottom);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void WindowQuickdrawSubdriver::DrawResizeIcon()
|
||||
{
|
||||
int xoff = this->window_width - _resize_icon_width;
|
||||
int yoff = this->window_height - _resize_icon_height;
|
||||
|
||||
switch (this->device_depth) {
|
||||
case 32:
|
||||
for (int y = 0; y < _resize_icon_height; y++) {
|
||||
uint32 *trg = (uint32*)this->window_buffer + (yoff + y) * this->window_pitch / 4 + xoff;
|
||||
|
||||
for (int x = 0; x < _resize_icon_width; x++, trg++) {
|
||||
if (_resize_icon[y * _resize_icon_width + x]) *trg = 0xff000000;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 16:
|
||||
for (int y = 0; y < _resize_icon_height; y++) {
|
||||
uint16 *trg = (uint16*)this->window_buffer + (yoff + y) * this->window_pitch / 2 + xoff;
|
||||
|
||||
for (int x = 0; x < _resize_icon_width; x++, trg++) {
|
||||
if (_resize_icon[y * _resize_icon_width + x]) *trg = 0x0000;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
WindowQuickdrawSubdriver::WindowQuickdrawSubdriver()
|
||||
{
|
||||
this->window_width = 0;
|
||||
this->window_height = 0;
|
||||
this->buffer_depth = 0;
|
||||
this->pixel_buffer = nullptr;
|
||||
this->active = false;
|
||||
this->setup = false;
|
||||
|
||||
this->window = nil;
|
||||
this->cocoaview = nil;
|
||||
|
||||
this->num_dirty_rects = MAX_DIRTY_RECTS;
|
||||
}
|
||||
|
||||
WindowQuickdrawSubdriver::~WindowQuickdrawSubdriver()
|
||||
{
|
||||
/* Release window mode resources */
|
||||
if (this->window != nil) [ this->window close ];
|
||||
|
||||
free(this->pixel_buffer);
|
||||
}
|
||||
|
||||
void WindowQuickdrawSubdriver::Draw(bool force_update)
|
||||
{
|
||||
PerformanceMeasurer framerate(PFE_VIDEO);
|
||||
|
||||
/* Check if we need to do anything */
|
||||
if (this->num_dirty_rects == 0 || [ this->window isMiniaturized ]) return;
|
||||
|
||||
if (this->num_dirty_rects >= MAX_DIRTY_RECTS) {
|
||||
this->num_dirty_rects = 1;
|
||||
this->dirty_rects[0].left = 0;
|
||||
this->dirty_rects[0].top = 0;
|
||||
this->dirty_rects[0].right = this->window_width;
|
||||
this->dirty_rects[0].bottom = this->window_height;
|
||||
}
|
||||
|
||||
RgnHandle dirty = NewRgn();
|
||||
RgnHandle temp = NewRgn();
|
||||
|
||||
SetEmptyRgn(dirty);
|
||||
|
||||
/* Build the region of dirty rectangles */
|
||||
for (int i = 0; i < this->num_dirty_rects; i++) {
|
||||
this->BlitToView(this->dirty_rects[i].left, this->dirty_rects[i].top,
|
||||
this->dirty_rects[i].right, this->dirty_rects[i].bottom);
|
||||
|
||||
MacSetRectRgn(temp, this->dirty_rects[i].left, this->dirty_rects[i].top,
|
||||
this->dirty_rects[i].right, this->dirty_rects[i].bottom);
|
||||
MacUnionRgn(dirty, temp, dirty);
|
||||
}
|
||||
|
||||
this->DrawResizeIcon();
|
||||
|
||||
/* Flush the dirty region */
|
||||
QDFlushPortBuffer( (OpaqueGrafPtr*) [ this->cocoaview qdPort ], dirty);
|
||||
DisposeRgn(dirty);
|
||||
DisposeRgn(temp);
|
||||
|
||||
this->num_dirty_rects = 0;
|
||||
}
|
||||
|
||||
void WindowQuickdrawSubdriver::MakeDirty(int left, int top, int width, int height)
|
||||
{
|
||||
if (this->num_dirty_rects < MAX_DIRTY_RECTS) {
|
||||
this->dirty_rects[this->num_dirty_rects].left = left;
|
||||
this->dirty_rects[this->num_dirty_rects].top = top;
|
||||
this->dirty_rects[this->num_dirty_rects].right = left + width;
|
||||
this->dirty_rects[this->num_dirty_rects].bottom = top + height;
|
||||
}
|
||||
this->num_dirty_rects++;
|
||||
}
|
||||
|
||||
void WindowQuickdrawSubdriver::UpdatePalette(uint first_color, uint num_colors)
|
||||
{
|
||||
if (this->buffer_depth != 8) return;
|
||||
|
||||
switch (this->device_depth) {
|
||||
case 32:
|
||||
for (uint i = first_color; i < first_color + num_colors; i++) {
|
||||
uint32 clr32 = 0xff000000;
|
||||
clr32 |= (uint32)_cur_palette.palette[i].r << 16;
|
||||
clr32 |= (uint32)_cur_palette.palette[i].g << 8;
|
||||
clr32 |= (uint32)_cur_palette.palette[i].b;
|
||||
this->palette[i] = clr32;
|
||||
}
|
||||
break;
|
||||
case 16:
|
||||
for (uint i = first_color; i < first_color + num_colors; i++) {
|
||||
uint16 clr16 = 0x0000;
|
||||
clr16 |= (uint16)((_cur_palette.palette[i].r >> 3) & 0x1f) << 10;
|
||||
clr16 |= (uint16)((_cur_palette.palette[i].g >> 3) & 0x1f) << 5;
|
||||
clr16 |= (uint16)((_cur_palette.palette[i].b >> 3) & 0x1f);
|
||||
this->palette[i] = clr16;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
this->num_dirty_rects = MAX_DIRTY_RECTS;
|
||||
}
|
||||
|
||||
uint WindowQuickdrawSubdriver::ListModes(OTTD_Point *modes, uint max_modes)
|
||||
{
|
||||
return QZ_ListModes(modes, max_modes, kCGDirectMainDisplay, this->buffer_depth);
|
||||
}
|
||||
|
||||
bool WindowQuickdrawSubdriver::ChangeResolution(int w, int h, int bpp)
|
||||
{
|
||||
int old_width = this->window_width;
|
||||
int old_height = this->window_height;
|
||||
int old_bpp = this->buffer_depth;
|
||||
|
||||
if (this->SetVideoMode(w, h, bpp)) return true;
|
||||
|
||||
if (old_width != 0 && old_height != 0) this->SetVideoMode(old_width, old_height, old_bpp);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Convert local coordinate to window server (CoreGraphics) coordinate */
|
||||
CGPoint WindowQuickdrawSubdriver::PrivateLocalToCG(NSPoint *p)
|
||||
{
|
||||
*p = [ this->cocoaview convertPoint:*p toView: nil ];
|
||||
*p = [ this->window convertBaseToScreen:*p ];
|
||||
p->y = this->device_height - p->y;
|
||||
|
||||
return CGPointMake(p->x, p->y);
|
||||
}
|
||||
|
||||
NSPoint WindowQuickdrawSubdriver::GetMouseLocation(NSEvent *event)
|
||||
{
|
||||
NSPoint pt = [ event locationInWindow ];
|
||||
pt = [ this->cocoaview convertPoint:pt fromView:nil ];
|
||||
|
||||
return pt;
|
||||
}
|
||||
|
||||
bool WindowQuickdrawSubdriver::MouseIsInsideView(NSPoint *pt)
|
||||
{
|
||||
return [ this->cocoaview mouse:*pt inRect:[ this->cocoaview bounds ] ];
|
||||
}
|
||||
|
||||
|
||||
/* This function makes the *game region* of the window 100% opaque.
|
||||
* The genie effect uses the alpha component. Otherwise,
|
||||
* it doesn't seem to matter what value it has.
|
||||
*/
|
||||
void WindowQuickdrawSubdriver::SetPortAlphaOpaque()
|
||||
{
|
||||
if (this->device_depth != 32) return;
|
||||
|
||||
uint32 *pixels = (uint32*)this->window_buffer;
|
||||
uint32 pitch = this->window_pitch / 4;
|
||||
|
||||
for (int y = 0; y < this->window_height; y++)
|
||||
for (int x = 0; x < this->window_width; x++) {
|
||||
pixels[y * pitch + x] |= 0xFF000000;
|
||||
}
|
||||
}
|
||||
|
||||
bool WindowQuickdrawSubdriver::WindowResized()
|
||||
{
|
||||
if (this->window == nil || this->cocoaview == nil) return true;
|
||||
|
||||
NSRect newframe = [ this->cocoaview frame ];
|
||||
CGrafPtr thePort = (OpaqueGrafPtr*) [ this->cocoaview qdPort ];
|
||||
|
||||
LockPortBits(thePort);
|
||||
this->window_buffer = GetPixBaseAddr(GetPortPixMap(thePort));
|
||||
this->window_pitch = GetPixRowBytes(GetPortPixMap(thePort));
|
||||
UnlockPortBits(thePort);
|
||||
|
||||
/* _cocoa_video_data.realpixels now points to the window's pixels
|
||||
* We want it to point to the *view's* pixels
|
||||
*/
|
||||
int voff = [ this->window frame ].size.height - newframe.size.height - newframe.origin.y;
|
||||
int hoff = [ this->cocoaview frame ].origin.x;
|
||||
this->window_buffer = (uint8*)this->window_buffer + (voff * this->window_pitch) + hoff * (this->device_depth / 8);
|
||||
|
||||
this->window_width = newframe.size.width;
|
||||
this->window_height = newframe.size.height;
|
||||
|
||||
free(this->pixel_buffer);
|
||||
this->pixel_buffer = malloc(this->window_width * this->window_height * this->buffer_depth / 8);
|
||||
if (this->pixel_buffer == nullptr) {
|
||||
DEBUG(driver, 0, "Failed to allocate pixel buffer");
|
||||
return false;
|
||||
}
|
||||
|
||||
QZ_GameSizeChanged();
|
||||
|
||||
/* Redraw screen */
|
||||
this->num_dirty_rects = MAX_DIRTY_RECTS;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
CocoaSubdriver *QZ_CreateWindowQuickdrawSubdriver(int width, int height, int bpp)
|
||||
{
|
||||
WindowQuickdrawSubdriver *ret;
|
||||
|
||||
if (MacOSVersionIsAtLeast(10, 5, 0)) {
|
||||
DEBUG(driver, 0, "The cocoa quickdraw subdriver is not recommended for Mac OS X 10.5 or later.");
|
||||
}
|
||||
|
||||
if (bpp != 8 && bpp != 32) {
|
||||
DEBUG(driver, 0, "The cocoa quickdraw subdriver only supports 8 and 32 bpp.");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
ret = new WindowQuickdrawSubdriver();
|
||||
|
||||
if (!ret->ChangeResolution(width, height, bpp)) {
|
||||
delete ret;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endif /* ENABLE_COCOA_QUICKDRAW */
|
||||
#endif /* WITH_COCOA */
|
Loading…
Reference in New Issue