Freeciv21
Develop your civilization from humble roots to a global empire
music.cpp
Go to the documentation of this file.
1 /*
2 _ ._ Copyright (c) 1996-2023 Freeciv21 and Freeciv contributors.
3  \ | This file is part of Freeciv21. Freeciv21 is free software: you
4  \_| can redistribute it and/or modify it under the terms of the
5  .' '. GNU General Public License as published by the Free
6  :O O: Software Foundation, either version 3 of the License,
7  '/ \' or (at your option) any later version. You should have
8  :X: received a copy of the GNU General Public License along with
9  :X: Freeciv21. If not, see https://www.gnu.org/licenses/.
10  */
11 
12 // utility
13 #include "connection.h"
14 #include "log.h"
15 #include "nation.h"
16 #include "style.h"
17 // client
18 #include "audio/audio.h"
19 #include "client_main.h"
20 #include "options.h"
21 
22 #include "music.h"
23 
28 {
29  if (client_state() != C_S_RUNNING) {
30  // Style music plays in running game only
31  return;
32  }
33 
34  if (client.conn.playing == nullptr) {
35  /* Detached connections and global observers currently not
36  * supported */
37  return;
38  }
39 
41  struct music_style *pms;
42 
44 
46 
47  if (pms != nullptr) {
48  QString tag;
49 
50  switch (client.conn.playing->client.mood) {
51  case MOOD_COUNT:
52  fc_assert(client.conn.playing->client.mood != MOOD_COUNT);
53  fc__fallthrough; // No break but use default tag
54  case MOOD_PEACEFUL:
55  tag = pms->music_peaceful;
56  break;
57  case MOOD_COMBAT:
58  tag = pms->music_combat;
59  break;
60  }
61 
62  if (!tag.isEmpty()) {
63  log_debug("Play %s", qUtf8Printable(tag));
64  audio_play_music(tag, nullptr, MU_INGAME);
65  }
66  }
67  }
68 }
69 
74 
78 void start_menu_music(const QString &tag, const QString &alt_tag)
79 {
81  audio_play_music(tag, alt_tag, MU_MENU);
82  }
83 }
84 
89 
93 void play_single_track(const QString &tag)
94 {
95  if (client_state() != C_S_RUNNING) {
96  // Only in running game
97  return;
98  }
99 
100  audio_play_track(tag, nullptr);
101 }
102 
106 void musicspec_reread_callback(struct option *poption)
107 {
108  const char *musicset_name = option_str_get(poption);
109 
110  audio_restart(sound_set_name, musicset_name);
111 
112  // Start suitable music from the new set
113  if (client_state() != C_S_RUNNING) {
114  start_menu_music(QStringLiteral("music_menu"), nullptr);
115  } else {
117  }
118 }
void audio_play_track(const QString &tag, const QString &alt_tag)
Play single track as suggested by sound tags.
Definition: audio.cpp:555
void audio_play_music(const QString &tag, const QString &alt_tag, enum music_usage usage)
Loop music as suggested by sound tags.
Definition: audio.cpp:544
void audio_stop_usage()
Stop looping sound.
Definition: audio.cpp:577
void audio_restart(const QString &soundset_name, const QString &musicset_name)
Switch soundset.
Definition: audio.cpp:343
@ MU_INGAME
Definition: audio.h:33
@ MU_MENU
Definition: audio.h:33
enum client_states client_state()
Return current client state.
struct civclient client
QString sound_set_name
@ C_S_RUNNING
Definition: client_main.h:43
#define fc_assert(condition)
Definition: log.h:89
#define log_debug(message,...)
Definition: log.h:65
void start_style_music()
Start music suitable for current game situation.
Definition: music.cpp:27
void musicspec_reread_callback(struct option *poption)
Musicset changed in options.
Definition: music.cpp:106
void start_menu_music(const QString &tag, const QString &alt_tag)
Start menu music.
Definition: music.cpp:78
void play_single_track(const QString &tag)
Play single track before continuing normal style music.
Definition: music.cpp:93
void stop_menu_music()
Stop menu music completely.
Definition: music.cpp:88
void stop_style_music()
Stop style music completely.
Definition: music.cpp:73
const char * option_str_get(const struct option *poption)
Returns the current value of this string option.
Definition: options.cpp:553
client_options * gui_options
Definition: options.cpp:74
struct connection conn
Definition: client_main.h:89
bool sound_enable_menu_music
Definition: options.h:116
bool sound_enable_game_music
Definition: options.h:117
struct player * playing
Definition: connection.h:142
QString music_peaceful
Definition: style.h:26
QString music_combat
Definition: style.h:27
The base class for options.
Definition: options.cpp:209
struct player::@65::@68 client
int music_style
Definition: player.h:262
struct music_style * music_style_by_number(int id)
Return music style of given id.
Definition: style.cpp:158
#define fc__fallthrough
Definition: support.h:49