Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Oct 5, 2009, 1:36:33 AM (15 years ago)
Author:
rgrieder
Message:

Added GameMode::playsSound(). This function checks whether sound is available. You MUST NEVER assume that the SoundManager exists and ALWAYS check it (even if graphics is all loaded).
Also cleaned SoundManager c'tor to use Exceptions.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/core5/src/orxonox/sound/SoundManager.cc

    r5877 r5878  
    3131#include <AL/alut.h>
    3232
     33#include "util/Exception.h"
    3334#include "util/Math.h"
     35#include "util/ScopeGuard.h"
     36#include "core/GameMode.h"
    3437#include "core/ScopedSingletonManager.h"
    3538#include "CameraManager.h"
     
    4245    ManageScopedSingleton(SoundManager, ScopeID::Graphics, true);
    4346
    44     /**
    45      * Default constructor
    46      */
    4747    SoundManager::SoundManager()
    4848    {
    49         this->device_ = NULL;
    50         this->soundavailable_ = true;
    51         if(!alutInitWithoutContext(NULL,NULL))
    52         {
    53             COUT(2) << "Sound: OpenAL ALUT: " << alutGetErrorString(alutGetError()) << std::endl;
    54             this->soundavailable_ = false;
    55         }
     49        if (!alutInitWithoutContext(NULL,NULL))
     50            ThrowException(InitialisationFailed, "OpenAL ALUT error: " << alutGetErrorString(alutGetError()));
     51        Loki::ScopeGuard alutExitGuard = Loki::MakeGuard(&alutExit);
     52
     53        COUT(3) << "OpenAL: Opening sound device..." << std::endl;
     54        this->device_ = alcOpenDevice(NULL);
     55        if (this->device_ == NULL)
     56            ThrowException(InitialisationFailed, "OpenAL error: Could not open sound device.");
     57        Loki::ScopeGuard closeDeviceGuard = Loki::MakeGuard(&alcCloseDevice, this->device_);
     58
     59        COUT(3) << "OpenAL: Sound device opened" << std::endl;
     60        this->context_ = alcCreateContext(this->device_, NULL);
     61        if (this->context_ == NULL)
     62            ThrowException(InitialisationFailed, "OpenAL error: Could not create sound context");
     63        Loki::ScopeGuard desroyContextGuard = Loki::MakeGuard(&alcDestroyContext, this->context_);
     64
     65        if (alcMakeContextCurrent(this->context_) == AL_TRUE)
     66            COUT(3) << "OpenAL: Context " << this->context_ << " loaded" << std::endl;
     67
     68        COUT(4) << "Sound: OpenAL ALUT version: " << alutGetMajorVersion() << "." << alutGetMinorVersion() << std::endl;
     69
     70        const char* str = alutGetMIMETypes(ALUT_LOADER_BUFFER);
     71        if (str == NULL)
     72            COUT(2) << "OpenAL ALUT error: " << alutGetErrorString(alutGetError()) << std::endl;
    5673        else
    57         {
    58             assert(this->device_ == NULL);
    59             COUT(3) << "Sound: OpenAL: Open sound device..." << std::endl;
    60             this->device_ = alcOpenDevice(NULL);
     74            COUT(4) << "OpenAL ALUT supported MIME types: " << str << std::endl;
     75        ThrowException(InitialisationFailed, "Just testing");
    6176
    62             if(this->device_ == NULL)
    63             {
    64                 COUT(2) << "Sound: OpenAL: Could not open sound device" << std::endl;
    65                 this->soundavailable_ = false;
    66             }
    67             else
    68             {
    69                 COUT(3) << "Sound: OpenAL: Sound device opened" << std::endl;
    70                 this->context_ = alcCreateContext(this->device_, NULL);
    71                 if(this->context_ == NULL)
    72                 {
    73                     COUT(2) << "Sound: OpenAL: Could not create sound context" << std::endl;
    74                     this->soundavailable_ = false;
    75                 }
    76                 else
    77                 {
    78                     if(alcMakeContextCurrent(this->context_) == AL_TRUE)
    79                         COUT(3) << "Sound: OpenAL: Context " << this->context_ << " loaded" << std::endl;
    80 
    81                     COUT(4) << "Sound: OpenAL ALUT version: " << alutGetMajorVersion() << "." << alutGetMinorVersion() << std::endl;
    82                     const char* str = alutGetMIMETypes(ALUT_LOADER_BUFFER);
    83                     if (str == NULL)
    84                         COUT(2) << "Sound: OpenAL ALUT: " << alutGetErrorString(alutGetError()) << std::endl;
    85                     else
    86                         COUT(4) << "Sound: OpenAL ALUT supported MIME types: " << str << std::endl;
    87                 }
    88             }
    89         }
     77        GameMode::setPlaysSound(true);
     78        // Disarm guards
     79        alutExitGuard.Dismiss();
     80        closeDeviceGuard.Dismiss();
     81        desroyContextGuard.Dismiss();
    9082    }
    9183
    9284    SoundManager::~SoundManager()
    9385    {
     86        GameMode::setPlaysSound(false);
    9487        alcDestroyContext(this->context_);
    9588        alcCloseDevice(this->device_);
Note: See TracChangeset for help on using the changeset viewer.