OpenClonk
C4MusicSystem.h
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 1998-2000, Matthes Bender
5  * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
6  * Copyright (c) 2010-2016, The OpenClonk Team and contributors
7  *
8  * Distributed under the terms of the ISC license; see accompanying file
9  * "COPYING" for details.
10  *
11  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
12  * See accompanying file "TRADEMARK" for details.
13  *
14  * To redistribute this file separately, substitute the full license texts
15  * for the above references.
16  */
17 
18 /* Handles Music.ocg and randomly plays songs */
19 
20 #ifndef INC_C4MusicSystem
21 #define INC_C4MusicSystem
22 
23 #include "c4group/C4Group.h"
25 
27 class C4MusicFile;
28 
30 {
31  friend class C4SoundEffect;
32  friend class C4SoundInstance;
33  friend class C4SoundSystem;
34 public:
35  C4MusicSystem();
37  void Clear();
38  void ClearGame();
39  void UpdateVolume(); // compute volume from game + config data
40  void Execute(bool force_buffer_checks = false);
41  void NotifySuccess();
42  bool Init(const char * PlayList = nullptr);
43  bool InitForScenario(C4Group & hGroup);
44  bool Play(const char *szSongname = nullptr, bool fLoop = false, int fadetime_ms = 0, double max_resume_time = 0.0, bool allow_break = false);
45  bool Play(C4MusicFile *NewFile, bool fLoop, double max_resume_time);
46  bool Stop();
47  void FadeOut(int fadeout_ms);
48 
49  int SetPlayList(const char *szPlayList, bool fForceSwitch = false, int fadetime_ms = 0, double max_resume_time = 0.0);
50 
51  bool ToggleOnOff(); // keyboard callback
52 
53 protected:
54  // song list
55  C4MusicFile* Songs{nullptr};
57 
58  // play
60  int Volume{100}; bool Loop;
61 
62  // fading between two songs
65 
66  // Wait time until next song
67  bool is_waiting{false};
69 
70  void LoadDir(const char *szPath); // load some music files (by wildcard / directory)
71  void Load(const char *szFile); // load a music file
72  void LoadMoreMusic(); // load music file names from MoreMusic.txt
73  void ClearSongs();
74 
75  bool GrpContainsMusic(C4Group &rGrp); // return whether this group contains music files
76 
77  bool ScheduleWaitTime();
78 
79  // SDL_mixer / OpenAL
81  bool InitializeMOD();
82  void DeinitializeMOD();
83 #if AUDIO_TK == AUDIO_TK_OPENAL
84 private:
85  ALCdevice* alcDevice{nullptr};
86  ALCcontext* alcContext{nullptr};
87 public:
88  void SelectContext();
89  ALCcontext *GetContext() const { return alcContext; }
90  ALCdevice *GetDevice() const { return alcDevice; }
91 #endif
92 public:
93  inline bool IsMODInitialized() {return MODInitialized;}
94 
95 private:
96  // scenario-defined music level
97  int32_t game_music_level{100};
98  // current play list
99  StdCopyStrBuf playlist;
100  bool playlist_valid{false};
101  // Set to nonzero to allow pauses between songs
102  int32_t music_break_min, music_break_max, music_break_chance;
103  // Maximum time (in seconds) last position in a song is remembered until it would just be restarted from the beginning
104  int32_t music_max_position_memory;
105 
106  static const int32_t DefaultMusicBreak;
107  static const int32_t DefaultMusicBreakChance;
108  static const int32_t DefaultMusicMaxPositionMemory;
109 
110 public:
111  void CompileFunc(class StdCompiler *comp);
112 
113  void SetMusicBreakMin(int32_t val) { music_break_min = std::max<int32_t>(val, 0); }
114  void SetMusicBreakMax(int32_t val) { music_break_max = std::max<int32_t>(val, 0); }
115  void SetMusicBreakChance(int32_t val) { music_break_chance = Clamp<int32_t>(val, 0, 100); }
116  void SetMusicMaxPositionMemory(int32_t val) { music_max_position_memory = val; }
117  int32_t SetGameMusicLevel(int32_t val);
118 };
119 
120 
121 // --- helper stuff --- //
122 
124 
125 class C4MusicFileInfoNode // We need this for the MoreMusic.txt stuff
126 {
127 public:
128  C4MusicFileInfoNode() { next=nullptr; str=nullptr; };
129  ~C4MusicFileInfoNode() { if (str) delete [] str; }
130  char* str;
133 };
134 
135 MusicType GetMusicFileTypeByExtension(const char* ext);
136 
137 #endif
MusicType
@ MUSICTYPE_OGG
@ MUSICTYPE_MID
@ MUSICTYPE_UNKNOWN
@ MUSICTYPE_MOD
@ MUSICTYPE_MP3
MusicType GetMusicFileTypeByExtension(const char *ext)
C4MusicFileInfoNode * next
C4TimeMilliseconds FadeTimeStart
Definition: C4MusicSystem.h:64
C4TimeMilliseconds wait_time_end
Definition: C4MusicSystem.h:68
bool GrpContainsMusic(C4Group &rGrp)
ALCcontext * GetContext() const
Definition: C4MusicSystem.h:89
void CompileFunc(class StdCompiler *comp)
void FadeOut(int fadeout_ms)
bool Play(const char *szSongname=nullptr, bool fLoop=false, int fadetime_ms=0, double max_resume_time=0.0, bool allow_break=false)
C4MusicFile * FadeMusicFile
Definition: C4MusicSystem.h:63
bool IsMODInitialized()
Definition: C4MusicSystem.h:93
bool Init(const char *PlayList=nullptr)
C4TimeMilliseconds FadeTimeEnd
Definition: C4MusicSystem.h:64
void SetMusicBreakChance(int32_t val)
void SetMusicMaxPositionMemory(int32_t val)
void SelectContext()
void Execute(bool force_buffer_checks=false)
void SetMusicBreakMax(int32_t val)
void DeinitializeMOD()
ALCdevice * GetDevice() const
Definition: C4MusicSystem.h:90
C4MusicFile * PlayMusicFile
Definition: C4MusicSystem.h:59
int SetPlayList(const char *szPlayList, bool fForceSwitch=false, int fadetime_ms=0, double max_resume_time=0.0)
void LoadDir(const char *szPath)
int32_t SetGameMusicLevel(int32_t val)
bool InitForScenario(C4Group &hGroup)
C4MusicFile * Songs
Definition: C4MusicSystem.h:55
bool InitializeMOD()
bool ScheduleWaitTime()
C4MusicFile * upcoming_music_file
Definition: C4MusicSystem.h:63
void Load(const char *szFile)
void SetMusicBreakMin(int32_t val)