Skip to content

Commit

Permalink
allow compiling on SDL2 as well as 1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
urkle committed Jul 28, 2013
1 parent e20579d commit 026e7e1
Show file tree
Hide file tree
Showing 14 changed files with 93 additions and 63 deletions.
25 changes: 23 additions & 2 deletions CMakeLists.txt
Expand Up @@ -13,6 +13,8 @@ IF(NOT APP_PRIVATE_LIB_DIR AND PRIVATE_LIB_DIR)
SET(APP_PRIVATE_LIB_DIR ${PRIVATE_LIB_DIR})
ENDIF()

OPTION(USE_SDL2 "Use SDL2 instead of SDL1.2" OFF)

IF(APP_PRIVATE_LIB_DIR AND "${CMAKE_SYSTEM}" MATCHES "Linux")
IF(NOT EXISTS ${APP_PRIVATE_LIB_DIR}/include)
MESSAGE(FATAL_ERROR "folder named include not found in private lib dir ${APP_PRIVATE_LIB_DIR}")
Expand Down Expand Up @@ -74,7 +76,18 @@ IF(APP_PRIVATE_LIB_DIR AND "${CMAKE_SYSTEM}" MATCHES "Linux")
ELSEIF(APP_PRIVATE_LIB_DIR AND APPLE)
FIND_LIBRARY(OGG_LIBRARY Ogg PATHS ${APP_PRIVATE_LIB_DIR} NO_DEFAULT_PATH)
FIND_LIBRARY(VORBIS_LIBRARY Vorbis PATHS ${APP_PRIVATE_LIB_DIR} NO_DEFAULT_PATH)
FIND_LIBRARY(SDL_LIBRARY SDL PATHS ${APP_PRIVATE_LIB_DIR} NO_DEFAULT_PATH)
IF(USE_SDL2)
FIND_LIBRARY(SDL_LIBRARY SDL2 PATHS ${APP_PRIVATE_LIB_DIR} NO_DEFAULT_PATH)
ELSE()
FIND_LIBRARY(SDL_LIBRARY SDL PATHS ${APP_PRIVATE_LIB_DIR} NO_DEFAULT_PATH)
ENDIF()
FIND_PATH(SDL_INCLUDE_DIR SDL.h
PATHS ${SDL_LIBRARY}
PATH_SUFFIXES Headers
NO_DEFAULT_PATH)
INCLUDE_DIRECTORIES(
${SDL_INCLUDE_DIR}
)
FIND_PACKAGE(OpenAL)
ELSE()
FIND_PACKAGE(OGG)
Expand All @@ -83,7 +96,11 @@ ELSE()
FIND_PACKAGE(VorbisFile)
ENDIF()
FIND_PACKAGE(OpenAL)
FIND_PACKAGE(SDL)
IF(USE_SDL2)
FIND_PACKAGE(SDL2)
ELSE()
FIND_PACKAGE(SDL)
ENDIF()

INCLUDE_DIRECTORIES(
${OGG_INCLUDE_DIR}
Expand Down Expand Up @@ -124,6 +141,10 @@ SET(all_sources
sources/OAL_Types.cpp
)

IF(USE_SDL2)
add_definitions(-DUSE_SDL2)
ENDIF()

add_library(OALWrapper STATIC ${all_sources})
TARGET_LINK_LIBRARIES(OALWrapper
${OGG_LIBRARY}
Expand Down
2 changes: 0 additions & 2 deletions include/OALWrapper/OAL_EFXManager.h
Expand Up @@ -12,8 +12,6 @@
#include "OAL_Helper.h"
#include "OAL_LoggerObject.h"

#include <SDL/SDL.h>

typedef enum
{
OAL_Effect_Type_Reverb,
Expand Down
2 changes: 0 additions & 2 deletions include/OALWrapper/OAL_Effect.h
Expand Up @@ -10,8 +10,6 @@

#include "OAL_LowLevelObject.h"

#include <SDL/SDL.h>

class cOAL_Effect : public iOAL_LowLevelObject
{
public:
Expand Down
2 changes: 0 additions & 2 deletions include/OALWrapper/OAL_EffectSlot.h
Expand Up @@ -11,8 +11,6 @@
#include "OAL_Types.h"
#include "OAL_LowLevelObject.h"

#include <SDL/SDL.h>

class cOAL_EFXManager;

class cOAL_EffectSlot : public iOAL_LowLevelObject
Expand Down
2 changes: 1 addition & 1 deletion include/OALWrapper/OAL_Sample.h
Expand Up @@ -26,7 +26,7 @@ class cOAL_Sample : public iOAL_AudioData
public:

cOAL_Sample();
virtual ~cOAL_Sample();
virtual ~cOAL_Sample();

void Destroy(){}

Expand Down
4 changes: 1 addition & 3 deletions include/OALWrapper/OAL_SourceManager.h
Expand Up @@ -12,16 +12,14 @@
#include "OAL_Helper.h"
#include "OAL_LoggerObject.h"

#include <SDL/SDL.h>

class cOAL_SourceManager : public iOAL_LoggerObject
{
public:

cOAL_SourceManager();
~cOAL_SourceManager();

cOAL_Source* GetSource ( int alSourceHandle, bool abSkipRefCountCheck = false );
cOAL_Source* GetSource ( int alSourceHandle, bool abSkipRefCountCheck = false );
cOAL_Source* GetAvailableSource ( unsigned int alPriority, int alNumOfVoices );

bool Initialize( bool abManageVoices, int alNumSourcesHint, bool abUseThreading, int alUpdateFreq, int alEFXSends = 0 );
Expand Down
4 changes: 4 additions & 0 deletions include/OALWrapper/OAL_Types.h
Expand Up @@ -56,6 +56,10 @@
#include <vector>
#include <list>

// SDL forward declares
struct SDL_mutex;
struct SDL_Thread;

#ifdef __APPLE__
// system headers
#include <OpenAL/al.h>
Expand Down
9 changes: 7 additions & 2 deletions sources/OAL_EFXManager.cpp
Expand Up @@ -12,8 +12,9 @@
#include "OALWrapper/OAL_Filter.h"
#include "OALWrapper/OAL_Device.h"



#include <SDL_thread.h>
#include <SDL_timer.h>
#include <SDL_version.h>

int SlotUpdaterThread(void* alUnusedArg);

Expand Down Expand Up @@ -202,7 +203,11 @@ bool cOAL_EFXManager::Initialize(int alNumSlotsHint, int alNumSends, bool abUseT

mlThreadWaitTime = 1000/alSlotUpdateFreq;

#if SDL_VERSION_ATLEAST(2, 0, 0)
mpUpdaterThread = SDL_CreateThread ( SlotUpdaterThread, "EFX Slot Updater", NULL );
#else
mpUpdaterThread = SDL_CreateThread ( SlotUpdaterThread, NULL );
#endif
}

LogMsg("",eOAL_LogVerbose_Medium, eOAL_LogMsg_Info, "EFX succesfully initialized.\n" );
Expand Down
1 change: 1 addition & 0 deletions sources/OAL_Effect.cpp
Expand Up @@ -8,6 +8,7 @@
#include "OALWrapper/OAL_Effect.h"
#include "OALWrapper/OAL_Helper.h"

#include <SDL_mutex.h>

cOAL_Effect::cOAL_Effect() : iOAL_LowLevelObject("Effect"),
mbNeedsUpdate(true),
Expand Down
1 change: 1 addition & 0 deletions sources/OAL_EffectSlot.cpp
Expand Up @@ -10,6 +10,7 @@
#include "OALWrapper/OAL_Helper.h"
#include "OALWrapper/OAL_EFXManager.h"

#include <SDL_mutex.h>

cOAL_EffectSlot::cOAL_EffectSlot( cOAL_EFXManager* apEFXManager, int alId) : iOAL_LowLevelObject("EffectSlot"),
mlId(alId),
Expand Down
8 changes: 4 additions & 4 deletions sources/OAL_Sample.cpp
Expand Up @@ -43,10 +43,10 @@ cOAL_Sample::~cOAL_Sample()
{
tSourceListIt it;
for(it=mlstBoundSources.begin(); it!=mlstBoundSources.end(); ++it)
{
cOAL_Source* pSource = *it;
pSource->Stop(false);
}
{
cOAL_Source* pSource = *it;
pSource->Stop(false);
}
mlstBoundSources.clear();
}
}
Expand Down
3 changes: 1 addition & 2 deletions sources/OAL_Source.cpp
Expand Up @@ -25,8 +25,7 @@
#include "OALWrapper/OAL_Helper.h"
#include "OALWrapper/OAL_SourceManager.h"

#include <SDL/SDL.h>
#include <SDL/SDL_thread.h>
#include <SDL_mutex.h>

//--------------------------------------------------------------------------------

Expand Down
73 changes: 40 additions & 33 deletions sources/OAL_SourceManager.cpp
Expand Up @@ -9,6 +9,9 @@
#include "OALWrapper/OAL_Source.h"
#include "OALWrapper/OAL_Device.h"

#include <SDL_thread.h>
#include <SDL_timer.h>
#include <SDL_version.h>

//-----------------------------------------------------------------------------------

Expand Down Expand Up @@ -88,7 +91,11 @@ bool cOAL_SourceManager::Initialize ( bool abManageVoices, int alNumSourcesHint,
// This converts the desired frequency in aInput to amount of milliseconds to wait.
// Note that this is an int value, so any freq above 1000 will turn mlThreadWaitTime to 0;
mlThreadWaitTime = 1000/alUpdateFreq;
#if SDL_VERSION_ATLEAST(2, 0, 0)
mpUpdaterThread = SDL_CreateThread ( UpdaterThread, "OAL Updater", NULL );
#else
mpUpdaterThread = SDL_CreateThread ( UpdaterThread, NULL );
#endif
mpStreamListMutex = SDL_CreateMutex ();

LogMsg("", eOAL_LogVerbose_Medium, eOAL_LogMsg_Info, "Done\n" );
Expand Down Expand Up @@ -135,31 +142,31 @@ void cOAL_SourceManager::Destroy()

//-----------------------------------------------------------------------------------

cOAL_Source* cOAL_SourceManager::GetSource(int alSourceHandle, bool abSkipRefCountCheck)
cOAL_Source* cOAL_SourceManager::GetSource(int alSourceHandle, bool abSkipRefCountCheck)
{
//////////////////////////////////
//Unpack source ID and refcount from the source
//////////////////////////////////
//Unpack source ID and refcount from the source
int lHandleId = GetUnpackedSourceId(alSourceHandle);
int lHandleRefCount = GetUnpackedRefCount(alSourceHandle);

//////////////////////////////////
//Check so handle is valid and not out of bounds
//////////////////////////////////
//Check so handle is valid and not out of bounds
if( (lHandleId < 0) || (lHandleId >= (int)mvSources.size()) )
return NULL;

cOAL_Source* pSource = mvSources[lHandleId];

//////////////////////////////////
// Check so that ref count is valid. (this will invalidate sources have been changed)
if(abSkipRefCountCheck==false)
{
//////////////////////////////////
// Check so that ref count is valid. (this will invalidate sources have been changed)
if(abSkipRefCountCheck==false)
{
pSource->Lock();
int lSourceRefCount = pSource->GetRefCount();
int lSourceRefCount = pSource->GetRefCount();
pSource->Unlock();

if(lHandleRefCount!=lSourceRefCount)
if(lHandleRefCount!=lSourceRefCount)
pSource = NULL;
}
}

return pSource;
}
Expand Down Expand Up @@ -244,21 +251,21 @@ cOAL_Source* cOAL_SourceManager::GetAvailableSource ( unsigned int alPriority, i
bool bFreeSourceFound = false;
eOAL_SourceStatus status;

///////////////////////
// Debug stuff:
///////////////////////
// Debug stuff:
if(!mbManageVoices)
alNumOfVoices = 1;

////////////////////
//If number of available voices, are less than the number wanted voices,
//then need to try and get voices from already active ones.
//Loop through and inactivate voices until the number wanted is available.
////////////////////
//If number of available voices, are less than the number wanted voices,
//then need to try and get voices from already active ones.
//Loop through and inactivate voices until the number wanted is available.
while(mlAvailableVoices < alNumOfVoices)
{
lLowestPrioSource = -1;

//////////////////////
// Find the voice with the lowest priority
//////////////////////
// Find the voice with the lowest priority
for(int i=0; i<(int)mvSources.size(); ++i )
{
pSource = mvSources[i];
Expand All @@ -272,26 +279,26 @@ cOAL_Source* cOAL_SourceManager::GetAvailableSource ( unsigned int alPriority, i
lLowestPrioSource = i;
}
}
//////////////////////
// Found a source, stop it.
//////////////////////
// Found a source, stop it.
if(lLowestPrioSource != -1)
{
pSource = mvSources[lLowestPrioSource];
pSource->Lock();
pSource->Stop();
pSource->IncRefCount(); //Do this to make sure the source is invalidated!
pSource->IncRefCount(); //Do this to make sure the source is invalidated!
pSource->Unlock();
}
//////////////////////
// No source found, leave loop
//////////////////////
// No source found, leave loop
else
{
{
break;
}
}
}

//////////////////////
// If there are voices available, get one to use
//////////////////////
// If there are voices available, get one to use
if(mlAvailableVoices >= alNumOfVoices)
{
for(int i=0; i<(int)mvSources.size(); ++i )
Expand All @@ -310,12 +317,12 @@ cOAL_Source* cOAL_SourceManager::GetAvailableSource ( unsigned int alPriority, i
}
}

////////////////////////////
// Return source
////////////////////////////
// Return source
if(lSourceHandle != -1)
return mvSources[lSourceHandle];
return mvSources[lSourceHandle];
else
return NULL;
return NULL;
}

//-----------------------------------------------------------------------------------
Expand Down
20 changes: 10 additions & 10 deletions sources/OAL_Stream.cpp
Expand Up @@ -124,17 +124,17 @@ bool cOAL_Stream::HasBufferUnderrun()
if(mpBoundSource==NULL)
return false;

if(IsEOF()==false)
{
//hpl::Log("Have not reached end of file\n");
if(IsEOF()==false)
{
//hpl::Log("Have not reached end of file\n");
return true;
}

if(mpBoundSource->GetQueuedBuffers()>0)
{
//hpl::Log("Source still has %d buffers to play\n", mpBoundSource->GetQueuedBuffers());
return true;
}

if(mpBoundSource->GetQueuedBuffers()>0)
{
//hpl::Log("Source still has %d buffers to play\n", mpBoundSource->GetQueuedBuffers());
return true;
}
}

return false;
}
Expand Down

0 comments on commit 026e7e1

Please sign in to comment.