Skip to content

Commit

Permalink
Change #6173: Update SDL driver to use SDL 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolas committed Sep 17, 2019
1 parent 1f41855 commit e69eb4e
Show file tree
Hide file tree
Showing 9 changed files with 993 additions and 11 deletions.
23 changes: 19 additions & 4 deletions config.lib
Expand Up @@ -751,7 +751,7 @@ check_params() {
log 1 "checking GDI video driver... not Windows, skipping"
fi

if [ -z "$allegro_config" ] && [ -z "$sdl_config" ] && [ "$with_cocoa" = 0 ] && [ "$os" != "MINGW" ] && [ "$os" != "CYGWIN" ]; then
if [ -z "$allegro_config" ] && [ -z "$sdl2_config" ] && [ -z "$sdl_config" ] && [ "$with_cocoa" = 0 ] && [ "$os" != "MINGW" ] && [ "$os" != "CYGWIN" ]; then
log 1 "configure: error: no video driver development files found"
log 1 " If you want a dedicated server use --enable-dedicated as parameter"
exit 1
Expand Down Expand Up @@ -1629,7 +1629,16 @@ make_cflags_and_ldflags() {
fi
fi

if [ -n "$sdl_config" ]; then
if [ -n "$sdl2_config" ]; then
CFLAGS="$CFLAGS -DWITH_SDL2"
# SDL must not add _GNU_SOURCE as it breaks many platforms
CFLAGS="$CFLAGS `$sdl2_config --cflags | sed 's@-D_GNU_SOURCE[^ ]*@@'`"
if [ "$enable_static" != "0" ]; then
LIBS="$LIBS `$sdl2_config --static --libs`"
else
LIBS="$LIBS `$sdl2_config --libs`"
fi
elif [ -n "$sdl_config" ]; then
CFLAGS="$CFLAGS -DWITH_SDL"
# SDL must not add _GNU_SOURCE as it breaks many platforms
CFLAGS="$CFLAGS `$sdl_config --cflags | sed 's@-D_GNU_SOURCE[^ ]*@@'`"
Expand Down Expand Up @@ -2426,7 +2435,13 @@ detect_sdl() {
sleep 5
fi

detect_pkg_config "$with_sdl" "sdl" "sdl_config" "1.2"
if [ $with_sdl = "sdl1" ]; then
detect_pkg_config "2" "sdl" "sdl_config" "1.2"
elif [ $with_sdl = "sdl2" ] || [ -x `which sdl2-config` ]; then
detect_pkg_config "2" "sdl2" "sdl2_config" "2.0"
else
detect_pkg_config "$with_sdl" "sdl" "sdl_config" "1.2"
fi
}

detect_osx_sdk() {
Expand Down Expand Up @@ -3496,7 +3511,7 @@ showhelp() {
echo " --with-allegro[=\"pkg-config allegro\"]"
echo " enables Allegro video driver support"
echo " --with-cocoa enables COCOA video driver (OSX ONLY)"
echo " --with-sdl[=\"pkg-config sdl\"] enables SDL video driver support"
echo " --with-sdl[=\"sdl1|sdl2\"] enables SDL video driver support"
echo " --with-zlib[=\"pkg-config zlib\"]"
echo " enables zlib support"
echo " --with-liblzma[=\"pkg-config liblzma\"]"
Expand Down
1 change: 1 addition & 0 deletions configure
Expand Up @@ -110,6 +110,7 @@ AWKCOMMAND='
if ($0 == "ALLEGRO" && "'$allegro_config'" == "") { next; }
if ($0 == "SDL" && "'$sdl_config'" == "") { next; }
if ($0 == "SDL2" && "'$sdl2_config'" == "") { next; }
if ($0 == "PNG" && "'$png_config'" == "") { next; }
if ($0 == "OSX" && "'$os'" != "OSX") { next; }
if ($0 == "OS2" && "'$os'" != "OS2") { next; }
Expand Down
2 changes: 1 addition & 1 deletion os/debian/control
Expand Up @@ -3,7 +3,7 @@ Section: games
Priority: optional
Maintainer: Matthijs Kooijman <matthijs@stdin.nl>
Uploaders: Jordi Mallach <jordi@debian.org>
Build-Depends: debhelper (>= 7.0.50), libsdl-dev, zlib1g-dev, libpng-dev, libfreetype6-dev, libfontconfig-dev, libicu-dev, liblzma-dev, liblzo2-dev
Build-Depends: debhelper (>= 7.0.50), libsdl2-dev, zlib1g-dev, libpng-dev, libfreetype6-dev, libfontconfig-dev, libicu-dev, liblzma-dev, liblzo2-dev
Standards-Version: 3.8.4
Vcs-Browser: http://anonscm.debian.org/gitweb/?p=collab-maint/openttd.git
Vcs-Git: git://anonscm.debian.org/collab-maint/openttd.git
Expand Down
2 changes: 1 addition & 1 deletion os/rpm/openttd.spec
Expand Up @@ -91,7 +91,7 @@ Group: Amusements/Games/Strategy/Other
Requires: %{name}
Conflicts: %{name}-dedicated

BuildRequires: SDL-devel
BuildRequires: SDL2-devel
BuildRequires: fontconfig-devel

%if 0%{?rhel_version} != 600
Expand Down
7 changes: 7 additions & 0 deletions source.list
Expand Up @@ -314,6 +314,7 @@ safeguards.h
screenshot.h
sound/sdl_s.h
video/sdl_v.h
video/sdl2_v.h
settings_func.h
settings_gui.h
settings_internal.h
Expand Down Expand Up @@ -1097,6 +1098,9 @@ video/null_v.cpp
#if SDL
video/sdl_v.cpp
#end
#if SDL2
video/sdl2_v.cpp
#end
#if WIN32
video/win32_v.cpp
#end
Expand Down Expand Up @@ -1139,6 +1143,9 @@ sound/null_s.cpp
#if SDL
sound/sdl_s.cpp
#end
#if SDL2
sound/sdl2_s.cpp
#end
#if WIN32
sound/win32_s.cpp
#if USE_XAUDIO2
Expand Down
16 changes: 11 additions & 5 deletions src/crashlog.cpp
Expand Up @@ -61,9 +61,9 @@
#ifdef WITH_LZO
#include <lzo/lzo1x.h>
#endif
#ifdef WITH_SDL
#if defined(WITH_SDL) || defined(WITH_SDL2)
# include <SDL.h>
#endif /* WITH_SDL */
#endif /* WITH_SDL || WITH_SDL2 */
#ifdef WITH_ZLIB
# include <zlib.h>
#endif
Expand Down Expand Up @@ -267,9 +267,15 @@ char *CrashLog::LogLibraries(char *buffer, const char *last) const
#endif /* WITH_PNG */

#ifdef WITH_SDL
const SDL_version *v = SDL_Linked_Version();
buffer += seprintf(buffer, last, " SDL: %d.%d.%d\n", v->major, v->minor, v->patch);
#endif /* WITH_SDL */
const SDL_version *sdl_v = SDL_Linked_Version();
buffer += seprintf(buffer, last, " SDL: %d.%d.%d\n", sdl_v->major, sdl_v->minor, sdl_v->patch);
#else
#ifdef WITH_SDL2
SDL_version sdl2_v;
SDL_GetVersion(&sdl2_v);
buffer += seprintf(buffer, last, " SDL2: %d.%d.%d\n", sdl2_v.major, sdl2_v.minor, sdl2_v.patch);
#endif
#endif /* WITH_SDL2 */

#ifdef WITH_ZLIB
buffer += seprintf(buffer, last, " Zlib: %s\n", zlibVersion());
Expand Down
70 changes: 70 additions & 0 deletions src/sound/sdl2_s.cpp
@@ -0,0 +1,70 @@
/* $Id$ */

/*
* 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 sdl2_s.cpp Playing sound via SDL2. */

#ifdef WITH_SDL2

#include "../stdafx.h"

#include "../mixer.h"
#include "sdl_s.h"
#include <SDL.h>

#include "../safeguards.h"

/** Factory for the SDL sound driver. */
static FSoundDriver_SDL iFSoundDriver_SDL;

/**
* Callback that fills the sound buffer.
* @param userdata Ignored.
* @param stream The stream to put data into.
* @param len The length of the stream in bytes.
*/
static void CDECL fill_sound_buffer(void *userdata, Uint8 *stream, int len)
{
MxMixSamples(stream, len / 4);
}

const char *SoundDriver_SDL::Start(const char * const *parm)
{
SDL_AudioSpec spec;
SDL_AudioSpec spec_actual;

/* Only initialise SDL if the video driver hasn't done it already */
int ret_code = 0;
if (SDL_WasInit(SDL_INIT_EVERYTHING) == 0) {
ret_code = SDL_Init(SDL_INIT_AUDIO);
} else if (SDL_WasInit(SDL_INIT_AUDIO) == 0) {
ret_code = SDL_InitSubSystem(SDL_INIT_AUDIO);
}
if (ret_code == -1) return SDL_GetError();

spec.freq = GetDriverParamInt(parm, "hz", 44100);
spec.format = AUDIO_S16SYS;
spec.channels = 2;
spec.samples = GetDriverParamInt(parm, "samples", 1024);
spec.callback = fill_sound_buffer;
SDL_AudioDeviceID dev = SDL_OpenAudioDevice(nullptr, 0, &spec, &spec_actual, SDL_AUDIO_ALLOW_FREQUENCY_CHANGE);
MxInitialize(spec_actual.freq);
SDL_PauseAudioDevice(dev, 0);
return nullptr;
}

void SoundDriver_SDL::Stop()
{
SDL_CloseAudio();
SDL_QuitSubSystem(SDL_INIT_AUDIO);
if (SDL_WasInit(SDL_INIT_EVERYTHING) == 0) {
SDL_Quit(); // If there's nothing left, quit SDL
}
}

#endif /* WITH_SDL2 */

0 comments on commit e69eb4e

Please sign in to comment.