source: trunk/src/sdl/sdl_audio.cc @ 202

Last change on this file since 202 was 202, checked in by epyon, 12 years ago
  • compilation fix
  • missing sdl_audio implementation file
File size: 1.5 KB
Line 
1// Copyright (C) 2012-2013 ChaosForge / Kornel Kisielewicz
2// http://chaosforge.org/
3//
4// This file is part of NV Libraries.
5// For conditions of distribution and use, see copyright notice in nv.hh
6
7#include "nv/sdl/sdl_audio.hh"
8
9#include "nv/lib/sdl.hh"
10#include "nv/lib/sdl_mixer.hh"
11#include "nv/logging.hh"
12
13using namespace nv;
14
15sdl::audio::audio()
16{
17        nv::load_sdl_library();
18        nv::load_sdl_mixer_library();
19
20        if ( SDL_Init( SDL_INIT_AUDIO ) == -1 )
21        {
22                NV_LOG( LOG_CRITICAL, "SDL_AUDIO failed to load -- " << SDL_GetError() );
23                return;
24        }
25
26        if( Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, 2, 1024 ) == -1 )
27        {
28                NV_LOG( LOG_CRITICAL, "SDL_mixer failed to load -- " << Mix_GetError() );
29                return;
30        }
31}
32
33
34nv::channel* sdl::audio::play_sound( nv::sound* a_sound )
35{
36        if ( Mix_PlayChannel(-1, (Mix_Chunk*)( down_cast< sdl::sound >( a_sound )->m_sound), 0) == -1 )
37        {
38                NV_LOG( LOG_WARNING, "SDL_mixer failed to play -- " << Mix_GetError() );
39        }
40        return nullptr;
41}
42
43nv::sound* nv::sdl::audio::load_sound( const std::string& a_path )
44{
45        Mix_Chunk *sample = Mix_LoadWAV( a_path.c_str() );
46        if ( sample == nullptr )
47        {
48                NV_LOG( LOG_ERROR, "SDL_mixer failed to load sample '" << a_path << "' -- " << Mix_GetError() );
49                return nullptr;
50        }
51        return new sdl::sound( sample );
52}
53
54void nv::sdl::audio::update()
55{
56        // no-op
57}
58
59nv::sdl::audio::~audio()
60{
61        Mix_CloseAudio();
62        // TODO: should we do it here?
63        SDL_Quit();
64}
65
66nv::sdl::sound::~sound()
67{
68        Mix_FreeChunk( (Mix_Chunk*)m_sound );
69}
Note: See TracBrowser for help on using the repository browser.