Skip to content

Commit

Permalink
[CR] Add audio support for curses mode (#78880)
Browse files Browse the repository at this point in the history
* works! sound should be functional within ncurses without TILES=1

* style changes

* updated CMake and cleaned up Makefile to support new feature

* updated docs to reflect new feature

---------

Co-authored-by: Jodi McJodson <[email protected]>
  • Loading branch information
JodiJodington and DaQueenJodi authored Jan 5, 2025
1 parent 0d0be90 commit 0fdda05
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 19 deletions.
8 changes: 0 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -366,14 +366,6 @@ if (CURSES)
endif ()

if (SOUND)
# You need TILES to be able to use SOUND
if (NOT TILES)
message(FATAL_ERROR
"You must enable graphical support with -DTILES=1 \
to be able to enable sound support. \
See doc/COMPILING/COMPILING-CMAKE.md for details and more info.")
endif ()

# Sound requires SDL_mixer library
message(STATUS "Searching for SDL2_mixer library --")
find_package(SDL2_mixer)
Expand Down
5 changes: 0 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -835,9 +835,6 @@ else
endif # TILES

ifeq ($(SOUND), 1)
ifneq ($(TILES),1)
$(error "SOUND=1 only works with TILES=1")
endif
ifeq ($(NATIVE),osx)
ifndef FRAMEWORK # libsdl build
ifeq ($(MACPORTS), 1)
Expand Down Expand Up @@ -1271,7 +1268,6 @@ ifdef LANGUAGES
mkdir -p $(APPRESOURCESDIR)/lang/mo/
cp -pR lang/mo/* $(APPRESOURCESDIR)/lang/mo/
endif
ifeq ($(TILES), 1)
ifeq ($(SOUND), 1)
cp -R data/sound $(APPDATADIR)
endif # ifeq ($(SOUND), 1)
Expand All @@ -1284,7 +1280,6 @@ ifeq ($(SOUND), 1)
cp -R $(FRAMEWORKSDIR)/SDL2_mixer.framework $(APPRESOURCESDIR)/
endif # ifeq ($(SOUND), 1)
endif # ifdef FRAMEWORK
endif # ifdef TILES
dylibbundler -of -b -x $(APPRESOURCESDIR)/$(APPTARGET) -d $(APPRESOURCESDIR)/ -p @executable_path/

dmgdistclean:
Expand Down
2 changes: 1 addition & 1 deletion doc/COMPILING/COMPILING.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ Given you're building from source you have a number of choices to make:
* `RELEASE=1` - without this you'll get a debug build (see note below)
* `LTO=1` - enables link-time optimization with GCC/Clang
* `TILES=1` - with this you'll get the tiles version, without it the curses version
* `SOUND=1` - if you want sound; this requires `TILES=1`
* `SOUND=1` - if you want sound
* `LOCALIZE=0` - this disables localizations so `gettext` is not needed
* `CLANG=1` - use Clang instead of GCC
* `CCACHE=1` - use ccache
Expand Down
4 changes: 2 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@

class ui_adaptor;

#if defined(TILES)
#if defined(TILES) || defined(SDL_SOUND)
# if defined(_MSC_VER) && defined(USE_VCPKG)
# include <SDL2/SDL_version.h>
# else
Expand Down Expand Up @@ -738,7 +738,7 @@ int main( int argc, const char *argv[] )
DebugLog( D_INFO, DC_ALL ) << "[main] C locale set to " << setlocale( LC_ALL, nullptr );
DebugLog( D_INFO, DC_ALL ) << "[main] C++ locale set to " << std::locale().name();

#if defined(TILES)
#if defined(TILES) || defined(SDL_SOUND)
SDL_version compiled;
SDL_VERSION( &compiled );
DebugLog( D_INFO, DC_ALL ) << "SDL version used during compile is "
Expand Down
12 changes: 12 additions & 0 deletions src/ncurses_def.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
#include <langinfo.h>
#endif

#if defined(SDL_SOUND)
#include "sdlsound.h"
#endif

std::unique_ptr<cataimgui::client> imclient;

static void curses_check_result( const int result, const int expected, const char *const /*name*/ )
Expand Down Expand Up @@ -361,6 +365,14 @@ void catacurses::init_interface()
// The below line tries to force the mouse pointer events to be turned on anyway. ImTui misbehaves without them.
printf( "\033[?1003h\n" );
#endif

#if defined(SDL_SOUND)
initSDLAudioOnly();
init_sound();
if( sound_init_success ) {
load_soundset();
}
#endif
}

bool catacurses::supports_256_colors()
Expand Down
8 changes: 5 additions & 3 deletions src/sdl_wrappers.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if defined(TILES)
#if defined(TILES) || defined(SDL_SOUND)

#include "sdl_wrappers.h"

Expand Down Expand Up @@ -35,6 +35,8 @@ void throwErrorIf( const bool condition, const char *const message )
throw std::runtime_error( std::string( message ) + ": " + SDL_GetError() );
}

#if defined(TILES)

void RenderCopy( const SDL_Renderer_Ptr &renderer, const SDL_Texture_Ptr &texture,
const SDL_Rect *srcrect, const SDL_Rect *dstrect )
{
Expand Down Expand Up @@ -191,5 +193,5 @@ SDL_Surface_Ptr CreateRGBSurface( const Uint32 flags, const int width, const int
throwErrorIf( !surface, "Failed to create surface" );
return surface;
}

#endif
#endif // defined(TILES)
#endif // defined(TILES) || defined(SDL_SOUND)
10 changes: 10 additions & 0 deletions src/sdlsound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -912,4 +912,14 @@ void load_soundset()
}
}

// capitalized to mirror cata_tiles::InitSDL()
void initSDLAudioOnly()
{
const int ret = SDL_Init( SDL_INIT_AUDIO );
throwErrorIf( ret != 0, "SDL_Init failed" );
if( atexit( SDL_Quit ) ) {
debugmsg( "atexit failed to register SDL_Quit" );
}
}

#endif
1 change: 1 addition & 0 deletions src/sdlsound.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* Attempt to initialize an audio device. Returns false if initialization fails.
*/
bool init_sound();
void initSDLAudioOnly();
void shutdown_sound();
void play_music( const std::string &playlist );
void stop_music();
Expand Down
13 changes: 13 additions & 0 deletions src/wincurse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
#include "ui_manager.h"
#include "wcwidth.h"


#if defined(SDL_SOUND)
#include "sdlsound.h"
#endif

//***********************************
//Globals *
//***********************************
Expand Down Expand Up @@ -654,6 +659,14 @@ void catacurses::init_interface()
//newwin calls `new WINDOW`, and that will throw, but not return nullptr.

initialized = true;

#if defined(SDL_SOUND)
initSDLAudioOnly();
init_sound();
if( sound_init_success ) {
load_soundset();
}
#endif
}

bool catacurses::supports_256_colors()
Expand Down

0 comments on commit 0fdda05

Please sign in to comment.