(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)

you can still use SDL drivers if you like and you have to run "make upgradeconf" to start using the cocoa drivers (or manually write WITH_COCOA:=1)
	since SDL breaks the cocoa drivers, you can't compile with both SDL and cocoa support
	Using cocoa drivers makes it easier to make universal binaries and it solves:
	-FS#18 [OSX] SDL is weird in universal binaries
	-FS#2  [OSX] lazy pointer crash on exit
	-FS#10 [OSX] linking error when linking statically to SDL 1.2.8 (needless to explain this, but it means it should be able to compile statically with the default settings now)
	-[ 1215073 ] Switching to large size out of fullscreen crashes
	Using SDL drivers will still have those issues though
This commit is contained in:
bjarni 2005-12-10 11:16:45 +00:00
parent f1877b3fe9
commit 6a6e145010
16 changed files with 2651 additions and 8 deletions

View File

@ -43,6 +43,7 @@
# WITH_ZLIB: savegames using zlib
# WITH_PNG: screenshots using PNG
# WITH_SDL: SDL video driver support
# WITH_COCOA: Cocoa video driver support
#
# Summary of other defines:
# MANUAL_CONFIG: do not use Makefile.config, config options set manually
@ -206,6 +207,14 @@ endif
endif
endif
ifdef WITH_COCOA
ifdef WITH_SDL
$(error You can not use both the SDL video driver and the Cocoa video driver at the same time)
endif
ifdef DEDICATED
$(error You can not use the Cocoa video driver in a dedicated server)
endif
else
# Force SDL on UNIX platforms
ifndef WITH_SDL
ifdef UNIX
@ -214,6 +223,7 @@ $(error You need to have SDL installed in order to run OpenTTD on UNIX. Use DEDI
endif
endif
endif
endif
# remove the dependancy for sdl if DEDICALTED is used
# and add -lpthread to LDFLAGS, because SDL normally adds that...
@ -502,7 +512,6 @@ else
STRGEN_FLAGS=
endif
# OSX specific setup
ifdef OSX
# set the endian flag for OSX, that can't fail
@ -519,6 +528,11 @@ ifdef OSX
LIBS += -framework QuickTime
endif
ifdef WITH_COCOA
CDEFS += -DWITH_COCOA
LIBS += -F/System/Library/Frameworks -framework Cocoa -framework Carbon -framework AudioUnit
endif
# OSX path setup
ifndef SECOND_DATA_PATH
SECOND_DATA_PATH:="$(OSXAPP)/Contents/Data/"
@ -744,10 +758,15 @@ else
endif
ifdef OSX
SRCS += os/macosx/macos.m
ifndef DEDICATED
SRCS += music/qtmidi.c
endif
SRCS += os/macosx/macos.m
ifndef DEDICATED
SRCS += music/qtmidi.c
endif
ifdef WITH_COCOA
SRCS += video/cocoa_v.m
SRCS += sound/cocoa_s.c
SRCS += os/macosx/splash.c
endif
endif
ifdef BEOS

View File

@ -18,11 +18,13 @@
#include "sound/null_s.h"
#include "sound/sdl_s.h"
#include "sound/cocoa_s.h"
#include "sound/win32_s.h"
#include "video/dedicated_v.h"
#include "video/null_v.h"
#include "video/sdl_v.h"
#include "video/cocoa_v.h"
#include "video/win32_v.h"
typedef struct DriverDesc {
@ -70,6 +72,9 @@ static const DriverDesc _sound_driver_descs[] = {
#endif
#ifdef WITH_SDL
M("sdl", "SDL Sound Driver", &_sdl_sound_driver),
#endif
#ifdef WITH_COCOA
M("cocoa", "Cocoa Sound Driver", &_cocoa_sound_driver),
#endif
M("null", "Null Sound Driver", &_null_sound_driver),
M(NULL, NULL, NULL)
@ -81,6 +86,9 @@ static const DriverDesc _video_driver_descs[] = {
#endif
#ifdef WITH_SDL
M("sdl", "SDL Video Driver", &_sdl_video_driver),
#endif
#ifdef WITH_COCOA
M("cocoa", "Cocoa Video Driver", &_cocoa_video_driver),
#endif
M("null", "Null Video Driver", &_null_video_driver),
#ifdef ENABLE_NETWORK

View File

@ -141,6 +141,37 @@ bool FiosCheckFileExists(const char *filename)
}
}
FILE *FioFOpenFile(const char *filename)
{
FILE *f;
char buf[MAX_PATH];
sprintf(buf, "%s%s", _path.data_dir, filename);
f = fopen(buf, "rb");
#if !defined(WIN32)
if (f == NULL) {
char *s;
// Make lower case and try again
for(s=buf + strlen(_path.data_dir) - 1; *s != 0; s++)
*s = tolower(*s);
f = fopen(buf, "rb");
#if defined SECOND_DATA_DIR
// tries in the 2nd data directory
if (f == NULL) {
sprintf(buf, "%s%s", _path.second_data_dir, filename);
for(s=buf + strlen(_path.second_data_dir) - 1; *s != 0; s++)
*s = tolower(*s);
f = fopen(buf, "rb");
}
#endif
}
#endif
return f;
}
void FioOpenFile(int slot, const char *filename)
{
FILE *f;

View File

@ -10,6 +10,7 @@ byte FioReadByte(void);
uint16 FioReadWord(void);
uint32 FioReadDword(void);
void FioCloseAll(void);
FILE *FioFOpenFile(const char *filename);
void FioOpenFile(int slot, const char *filename);
void FioReadBlock(void *ptr, uint size);
void FioSkipBytes(int n);

View File

@ -72,6 +72,7 @@ $(MAKE_CONFIG):
$(call CONFIG_LINE,WITH_SDL:=$(WITH_SDL))
$(call CONFIG_LINE,WITH_PNG:=$(WITH_PNG))
$(call CONFIG_LINE,STATIC_ZLIB_PATH:=$(STATIC_ZLIB_PATH))
$(call CONFIG_LINE,WITH_COCOA:=$(WITH_COCOA))
$(call CONFIG_LINE,)
$(call CONFIG_LINE,\# Lib paths for OSX. Read os/MacOSX/Makefile for more info)

View File

@ -93,6 +93,14 @@ endif
endif
endif
ifdef OSX
# we prefer to use cocoa drivers rather than SDL drivers
# if you really want SDL drivers, you can always modify Makefile.config
WITH_COCOA:=1
WITH_SDL:=
endif
# workaround
# cygwin have problems with libpng, so we will just disable it for now until the problem is solved
ifdef CYGWIN

View File

@ -45,6 +45,7 @@ $(BUILD_OSX_BUNDLE): $(TTD) $(FAT_BINARY)
$(Q)cp os/macosx/openttd.icns "$(OSXAPP)"/Contents/Resources/openttd.icns
$(Q)os/macosx/plistgen.sh "$(OSXAPP)" "$(REV)"
$(Q)cp data/* "$(OSXAPP)"/Contents/Data/
$(Q)cp os/macosx/splash.png "$(OSXAPP)"/Contents/Data/
$(Q)cp lang/*.lng "$(OSXAPP)"/Contents/Lang/
$(Q)cp $(TTD) "$(OSXAPP)"/Contents/MacOS/$(TTD)
$(COPY_x86_SDL_LIB)

View File

@ -15,12 +15,12 @@ void ShowMacDialog ( const char *title, const char *message, const char *buttonL
void ShowMacAssertDialog ( const char *function, const char *file, const int line, const char *expression )
{
const char *buffer =
const char *buffer =
[[NSString stringWithFormat:@"An assertion has failed and OpenTTD must quit.\n%s in %s (line %d)\n\"%s\"\n\nYou should report this error the OpenTTD developers if you think you found a bug.",
function, file, line, expression] cStringUsingEncoding:NSASCIIStringEncoding];
NSLog(@"%s", buffer);
ShowMacDialog( "Assertion Failed", buffer, "Quit" );
// abort so that a debugger has a chance to notice
abort();
}

145
os/macosx/splash.c Normal file
View File

@ -0,0 +1,145 @@
#include "../../stdafx.h"
#include "../../openttd.h"
#include "../../variables.h"
#include "../../macros.h"
#include "../../debug.h"
#include "../../functions.h"
#include "../../gfx.h"
#include "../../fileio.h"
#include "splash.h"
#ifdef WITH_PNG
#include <png.h>
static void PNGAPI png_my_error(png_structp png_ptr, png_const_charp message)
{
DEBUG(misc, 0) ("ERROR(libpng): %s - %s", message, (char *)png_get_error_ptr(png_ptr));
longjmp(png_ptr->jmpbuf, 1);
}
static void PNGAPI png_my_warning(png_structp png_ptr, png_const_charp message)
{
DEBUG(misc, 0) ("WARNING(libpng): %s - %s", message, (char *)png_get_error_ptr(png_ptr));
}
void DisplaySplashImage(void)
{
png_byte header[8];
FILE *f;
png_structp png_ptr;
png_infop info_ptr, end_info;
uint width, height, bit_depth, color_type;
png_colorp palette;
int num_palette;
png_bytep *row_pointers;
uint8 *src, *dst;
uint y;
uint xoff, yoff;
int i;
f = FioFOpenFile(SPLASH_IMAGE_FILE);
if (f == NULL) return;
fread(header, 1, 8, f);
if (png_sig_cmp(header, 0, 8) != 0) {
fclose(f);
return;
}
png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, (png_voidp) NULL, png_my_error, png_my_warning);
if (png_ptr == NULL) {
fclose(f);
return;
}
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL) {
png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
fclose(f);
return;
}
end_info = png_create_info_struct(png_ptr);
if (end_info == NULL) {
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
fclose(f);
return;
}
if (setjmp(png_jmpbuf(png_ptr))) {
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
fclose(f);
return;
}
png_init_io(png_ptr, f);
png_set_sig_bytes(png_ptr, 8);
png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
width = png_get_image_width(png_ptr, info_ptr);
height = png_get_image_height(png_ptr, info_ptr);
bit_depth = png_get_bit_depth(png_ptr, info_ptr);
color_type = png_get_color_type(png_ptr, info_ptr);
if(color_type != PNG_COLOR_TYPE_PALETTE || bit_depth != 8) {
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
fclose(f);
return;
}
if(!png_get_valid(png_ptr, info_ptr, PNG_INFO_PLTE)) {
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
fclose(f);
return;
}
png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette);
row_pointers = png_get_rows(png_ptr, info_ptr);
memset(_screen.dst_ptr, 0xff, _screen.pitch * _screen.height);
if(width > (uint) _screen.width)
width = _screen.width;
if(height > (uint) _screen.height)
height = _screen.height;
xoff = (_screen.width - width) / 2;
yoff = (_screen.height - height) / 2;
for(y = 0; y < height; y++) {
src = row_pointers[y];
dst = ((uint8 *) _screen.dst_ptr) + (yoff + y) * _screen.pitch + xoff;
memcpy(dst, src, width);
}
for (i = 0; i < num_palette; i++) {
_cur_palette[i].r = palette[i].red;
_cur_palette[i].g = palette[i].green;
_cur_palette[i].b = palette[i].blue;
}
_cur_palette[0xff].r = 0;
_cur_palette[0xff].g = 0;
_cur_palette[0xff].b = 0;
_pal_first_dirty = 0;
_pal_last_dirty = 0xff;
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
fclose(f);
return;
}
#else // WITH_PNG
void DisplaySplashImage(void) {}
#endif // WITH_PNG

9
os/macosx/splash.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef SPLASH_H
#define SPLASH_H
#define SPLASH_IMAGE_FILE "splash.png"
void DisplaySplashImage(void);
#endif

BIN
os/macosx/splash.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

144
sound/cocoa_s.c Normal file
View File

@ -0,0 +1,144 @@
/******************************************************************************************
* Cocoa sound driver *
* Known things left to do: *
* - Might need to do endian checking for it to work on both ppc and x86 *
******************************************************************************************/
#ifdef WITH_COCOA
#include <AudioUnit/AudioUnit.h>
/* Name conflict */
#define Rect OTTDRect
#define Point OTTDPoint
#define WindowClass OTTDWindowClass
/* Defined in stdbool.h */
#ifndef __cplusplus
# ifndef __BEOS__
# undef bool
# undef false
# undef true
# endif
#endif
#include "../stdafx.h"
#include "../openttd.h"
#include "../debug.h"
#include "../driver.h"
#include "../mixer.h"
#include "../sdl.h"
#include "cocoa_s.h"
#undef WindowClass
#undef Point
#undef Rect
static AudioUnit _outputAudioUnit;
/* The CoreAudio callback */
static OSStatus audioCallback (void *inRefCon, AudioUnitRenderActionFlags inActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, AudioBuffer *ioData)
{
MxMixSamples(_mixer, ioData->mData, ioData->mDataByteSize / 4);
return noErr;
}
static const char *CocoaSoundStart(const char * const *parm)
{
Component comp;
ComponentDescription desc;
struct AudioUnitInputCallback callback;
AudioStreamBasicDescription requestedDesc;
DEBUG(driver, 1)("cocoa_s: CocoaSoundStart");
/* Setup a AudioStreamBasicDescription with the requested format */
requestedDesc.mFormatID = kAudioFormatLinearPCM;
requestedDesc.mFormatFlags = kLinearPCMFormatFlagIsPacked;
requestedDesc.mChannelsPerFrame = 2;
requestedDesc.mSampleRate = GetDriverParamInt(parm, "hz", 11025);
requestedDesc.mBitsPerChannel = 16;
requestedDesc.mFormatFlags |= kLinearPCMFormatFlagIsSignedInteger;
#if 1 // Big endian?
requestedDesc.mFormatFlags |= kLinearPCMFormatFlagIsBigEndian;
#endif
requestedDesc.mFramesPerPacket = 1;
requestedDesc.mBytesPerFrame = requestedDesc.mBitsPerChannel * requestedDesc.mChannelsPerFrame / 8;
requestedDesc.mBytesPerPacket = requestedDesc.mBytesPerFrame * requestedDesc.mFramesPerPacket;
/* Locate the default output audio unit */
desc.componentType = kAudioUnitComponentType;
desc.componentSubType = kAudioUnitSubType_Output;
desc.componentManufacturer = kAudioUnitID_DefaultOutput;
desc.componentFlags = 0;
desc.componentFlagsMask = 0;
comp = FindNextComponent (NULL, &desc);
if (comp == NULL)
return "cocoa_s: Failed to start CoreAudio: FindNextComponent returned NULL";
/* Open & initialize the default output audio unit */
if(OpenAComponent(comp, &_outputAudioUnit) != noErr)
return "cocoa_s: Failed to start CoreAudio: OpenAComponent";
if(AudioUnitInitialize(_outputAudioUnit) != noErr)
return "cocoa_s: Failed to start CoreAudio: AudioUnitInitialize";
/* Set the input format of the audio unit. */
if(AudioUnitSetProperty(_outputAudioUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &requestedDesc, sizeof (requestedDesc)) != noErr)
return "cocoa_s: Failed to start CoreAudio: AudioUnitSetProperty (kAudioUnitProperty_StreamFormat)";
/* Set the audio callback */
callback.inputProc = audioCallback;
callback.inputProcRefCon = NULL;
if(AudioUnitSetProperty(_outputAudioUnit, kAudioUnitProperty_SetInputCallback, kAudioUnitScope_Input, 0, &callback, sizeof(callback)) != noErr)
return "cocoa_s: Failed to start CoreAudio: AudioUnitSetProperty (kAudioUnitProperty_SetInputCallback)";
/* Finally, start processing of the audio unit */
if(AudioOutputUnitStart (_outputAudioUnit) != noErr)
return "cocoa_s: Failed to start CoreAudio: AudioOutputUnitStart";
/* We're running! */
return NULL;
}
static void CocoaSoundStop(void)
{
struct AudioUnitInputCallback callback;
DEBUG(driver, 1)("cocoa_s: CocoaSoundStop");
/* stop processing the audio unit */
if(AudioOutputUnitStop(_outputAudioUnit) != noErr) {
DEBUG(driver, 1)("cocoa_s: Core_CloseAudio: AudioOutputUnitStop failed");
return;
}
/* Remove the input callback */
callback.inputProc = 0;
callback.inputProcRefCon = 0;
if(AudioUnitSetProperty(_outputAudioUnit, kAudioUnitProperty_SetInputCallback, kAudioUnitScope_Input, 0, &callback, sizeof(callback)) != noErr) {
DEBUG(driver, 1)("cocoa_s: Core_CloseAudio: AudioUnitSetProperty (kAudioUnitProperty_SetInputCallback) failed");
return;
}
if (CloseComponent(_outputAudioUnit) != noErr) {
DEBUG(driver, 1)("cocoa_s: Core_CloseAudio: CloseComponent failed");
return;
}
}
const HalSoundDriver _cocoa_sound_driver = {
CocoaSoundStart,
CocoaSoundStop,
};
#endif /* WITH_COCOA */

9
sound/cocoa_s.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef SOUND_COCOA_H
#define SOUND_COCOA_H
#include "../hal.h"
const HalSoundDriver _cocoa_sound_driver;
#endif

26
unix.c
View File

@ -444,8 +444,26 @@ void ShowOSErrorBox(const char *buf)
#endif
}
#ifdef WITH_COCOA
void cocoaSetWorkingDirectory(void);
void cocoaSetupAutoreleasePool(void);
void cocoaReleaseAutoreleasePool(void);
#endif
int CDECL main(int argc, char* argv[])
{
int ret;
#ifdef WITH_COCOA
cocoaSetupAutoreleasePool();
/* This is passed if we are launched by double-clicking */
if(argc >= 2 && strncmp (argv[1], "-psn", 4) == 0) {
argv[1] = NULL;
argc = 1;
cocoaSetWorkingDirectory();
}
#endif
// change the working directory to enable doubleclicking in UIs
#if defined(__BEOS__) || defined(__linux__)
ChangeWorkingDirectory(argv[0]);
@ -456,7 +474,13 @@ int CDECL main(int argc, char* argv[])
signal(SIGPIPE, SIG_IGN);
return ttd_main(argc, argv);
ret = ttd_main(argc, argv);
#ifdef WITH_COCOA
cocoaReleaseAutoreleasePool();
#endif
return ret;
}
void DeterminePaths(void)

130
video/cocoa_v.h Normal file
View File

@ -0,0 +1,130 @@
#ifndef VIDEO_COCOA_H
#define VIDEO_COCOA_H
#include "../hal.h"
extern const HalVideoDriver _cocoa_video_driver;
/* From SDL_QuartzKeys.h */
/* These are the Macintosh key scancode constants -- from Inside Macintosh */
#define QZ_ESCAPE 0x35
#define QZ_F1 0x7A
#define QZ_F2 0x78
#define QZ_F3 0x63
#define QZ_F4 0x76
#define QZ_F5 0x60
#define QZ_F6 0x61
#define QZ_F7 0x62
#define QZ_F8 0x64
#define QZ_F9 0x65
#define QZ_F10 0x6D
#define QZ_F11 0x67
#define QZ_F12 0x6F
#define QZ_PRINT 0x69
#define QZ_SCROLLOCK 0x6B
#define QZ_PAUSE 0x71
#define QZ_POWER 0x7F
#define QZ_BACKQUOTE 0x32
#define QZ_1 0x12
#define QZ_2 0x13
#define QZ_3 0x14
#define QZ_4 0x15
#define QZ_5 0x17
#define QZ_6 0x16
#define QZ_7 0x1A
#define QZ_8 0x1C
#define QZ_9 0x19
#define QZ_0 0x1D
#define QZ_MINUS 0x1B
#define QZ_EQUALS 0x18
#define QZ_BACKSPACE 0x33
#define QZ_INSERT 0x72
#define QZ_HOME 0x73
#define QZ_PAGEUP 0x74
#define QZ_NUMLOCK 0x47
#define QZ_KP_EQUALS 0x51
#define QZ_KP_DIVIDE 0x4B
#define QZ_KP_MULTIPLY 0x43
#define QZ_TAB 0x30
#define QZ_q 0x0C
#define QZ_w 0x0D
#define QZ_e 0x0E
#define QZ_r 0x0F
#define QZ_t 0x11
#define QZ_y 0x10
#define QZ_u 0x20
#define QZ_i 0x22
#define QZ_o 0x1F
#define QZ_p 0x23
#define QZ_LEFTBRACKET 0x21
#define QZ_RIGHTBRACKET 0x1E
#define QZ_BACKSLASH 0x2A
#define QZ_DELETE 0x75
#define QZ_END 0x77
#define QZ_PAGEDOWN 0x79
#define QZ_KP7 0x59
#define QZ_KP8 0x5B
#define QZ_KP9 0x5C
#define QZ_KP_MINUS 0x4E
#define QZ_CAPSLOCK 0x39
#define QZ_a 0x00
#define QZ_s 0x01
#define QZ_d 0x02
#define QZ_f 0x03
#define QZ_g 0x05
#define QZ_h 0x04
#define QZ_j 0x26
#define QZ_k 0x28
#define QZ_l 0x25
#define QZ_SEMICOLON 0x29
#define QZ_QUOTE 0x27
#define QZ_RETURN 0x24
#define QZ_KP4 0x56
#define QZ_KP5 0x57
#define QZ_KP6 0x58
#define QZ_KP_PLUS 0x45
#define QZ_LSHIFT 0x38
#define QZ_z 0x06
#define QZ_x 0x07
#define QZ_c 0x08
#define QZ_v 0x09
#define QZ_b 0x0B
#define QZ_n 0x2D
#define QZ_m 0x2E
#define QZ_COMMA 0x2B
#define QZ_PERIOD 0x2F
#define QZ_SLASH 0x2C
#if 1 /* Panther now defines right side keys */
#define QZ_RSHIFT 0x3C
#endif
#define QZ_UP 0x7E
#define QZ_KP1 0x53
#define QZ_KP2 0x54
#define QZ_KP3 0x55
#define QZ_KP_ENTER 0x4C
#define QZ_LCTRL 0x3B
#define QZ_LALT 0x3A
#define QZ_LMETA 0x37
#define QZ_SPACE 0x31
#if 1 /* Panther now defines right side keys */
#define QZ_RMETA 0x36
#define QZ_RALT 0x3D
#define QZ_RCTRL 0x3E
#endif
#define QZ_LEFT 0x7B
#define QZ_DOWN 0x7D
#define QZ_RIGHT 0x7C
#define QZ_KP0 0x52
#define QZ_KP_PERIOD 0x41
/* Wierd, these keys are on my iBook under MacOS X */
#define QZ_IBOOK_ENTER 0x34
#define QZ_IBOOK_LEFT 0x3B
#define QZ_IBOOK_RIGHT 0x3C
#define QZ_IBOOK_DOWN 0x3D
#define QZ_IBOOK_UP 0x3E
#endif

2113
video/cocoa_v.m Normal file

File diff suppressed because it is too large Load Diff