Freeciv-3.3: /fast/freeciv/nightly/main/client/music.c Source File

Freeciv-3.3

Loading...
Searching...
No Matches
music.c
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 2005 - The Freeciv Team
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12***********************************************************************/
13
14#ifdef HAVE_CONFIG_H
15#include <fc_config.h>
16#endif
17
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21
22/* utility */
23#include "connection.h"
24#include "log.h"
25
26/* client */
27#include "audio.h"
28#include "client_main.h"
29#include "options.h"
30
31#include "music.h"
32
33
34 struct musicset {
35 char name[MAX_LEN_NAME];
36 char version[MAX_LEN_NAME];
37 char *summary;
38 char *description;
39} current_ms = { .summary = NULL, .description = NULL };
40
41/**********************************************************************/
44 void start_style_music(void)
45{
46 if (client_state() != C_S_RUNNING) {
47 /* Style music plays in running game only */
48 return;
49 }
50
51 if (client.conn.playing == NULL) {
52 /* Detached connections and global observers currently not
53 * supported */
54 return;
55 }
56
57 if (gui_options.sound_enable_game_music) {
58 struct music_style *pms;
59
60 stop_style_music();
61
62 pms = music_style_by_number(client.conn.playing->music_style);
63
64 if (pms != NULL) {
65 const char *tag = NULL;
66
67 switch (client.conn.playing->client.mood) {
68 case MOOD_COUNT:
69 fc_assert(client.conn.playing->client.mood != MOOD_COUNT);
70 fc__fallthrough; /* No break but use default tag */
71 case MOOD_PEACEFUL:
72 tag = pms->music_peaceful;
73 break;
74 case MOOD_COMBAT:
75 tag = pms->music_combat;
76 break;
77 }
78
79 if (tag != NULL && tag[0] != '\0') {
80 log_debug("Play %s", tag);
81 audio_play_music(tag, NULL, MU_INGAME);
82 }
83 }
84 }
85}
86
87/**********************************************************************/
90 void stop_style_music(void)
91{
92 audio_stop_usage();
93}
94
95/**********************************************************************/
98 void start_menu_music(const char *const tag, char *const alt_tag)
99{
100 if (gui_options.sound_enable_menu_music) {
101 audio_play_music(tag, alt_tag, MU_MENU);
102 }
103}
104
105/**********************************************************************/
108 void stop_menu_music(void)
109{
110 audio_stop_usage();
111}
112
113/**********************************************************************/
116 void play_single_track(const char *const tag)
117{
118 if (client_state() != C_S_RUNNING) {
119 /* Only in running game */
120 return;
121 }
122
123 audio_play_track(tag, NULL);
124}
125
126/**********************************************************************/
129 void musicspec_reread_callback(struct option *poption)
130{
131 const char *musicset_name = option_str_get(poption);
132
133 audio_restart(sound_set_name, musicset_name);
134
135 /* Start suitable music from the new set */
136 if (client_state() != C_S_RUNNING) {
137 start_menu_music("music_menu", NULL);
138 } else {
139 start_style_music();
140 }
141}
142
143/**********************************************************************/
148 struct section_file *musicspec_load(const char *ms_filename)
149{
150 struct section_file *tagfile = secfile_load(ms_filename, TRUE);
151
152 if (tagfile != NULL) {
153 const char *mstr;
154
155 mstr = secfile_lookup_str(tagfile, "musicspec.name");
156
157 if (mstr == NULL) {
158 log_error(_("Musicset from %s has no name defined!"), ms_filename);
159 secfile_destroy(tagfile);
160
161 return NULL;
162 }
163
164 /* Musicset name found */
165 sz_strlcpy(current_ms.name, mstr);
166
167 mstr = secfile_lookup_str_default(tagfile, "", "musicspec.version");
168 if (mstr[0] != '\0') {
169 /* Musicset version found */
170 sz_strlcpy(current_ms.version, mstr);
171 } else {
172 /* No version information */
173 current_ms.version[0] = '\0';
174 }
175
176 mstr = secfile_lookup_str_default(tagfile, "", "musicspec.summary");
177 if (mstr[0] != '\0') {
178 size_t len;
179
180 /* Musicset summary found */
181 len = strlen(mstr);
182 current_ms.summary = fc_malloc(len + 1);
183 fc_strlcpy(current_ms.summary, mstr, len + 1);
184 } else {
185 /* No summary */
186 if (current_ms.summary != NULL) {
187 free(current_ms.summary);
188 current_ms.summary = NULL;
189 }
190 }
191
192 mstr = secfile_lookup_str_default(tagfile, "", "musicspec.description");
193 if (mstr[0] != '\0') {
194 size_t len;
195
196 /* Musicset description found */
197 len = strlen(mstr);
198 current_ms.description = fc_malloc(len + 1);
199 fc_strlcpy(current_ms.description, mstr, len + 1);
200 } else {
201 /* No Description */
202 if (current_ms.description != NULL) {
203 free(current_ms.description);
204 current_ms.description = NULL;
205 }
206 }
207 }
208
209 return tagfile;
210}
211
212/**********************************************************************/
219 void musicspec_close(struct section_file *tagfile)
220{
221 if (tagfile != NULL) {
222 free(current_ms.summary);
223 current_ms.summary = NULL;
224 free(current_ms.description);
225 current_ms.description = NULL;
226
227 secfile_destroy(tagfile);
228 }
229}
230
231/**********************************************************************/
234 const char *current_musicset_name(void)
235{
236 return current_ms.name;
237}
238
239/**********************************************************************/
242 const char *current_musicset_version(void)
243{
244 if (current_ms.version[0] == '\0') {
245 return NULL;
246 }
247
248 return current_ms.version;
249}
250
251/**********************************************************************/
254 const char *current_musicset_summary(void)
255{
256 return current_ms.summary;
257}
258
259/**********************************************************************/
262 const char *current_musicset_description(void)
263{
264 return current_ms.description;
265}
audio_restart
void audio_restart(const char *soundset_name, const char *musicset_name)
Definition audio.c:376
audio_play_track
void audio_play_track(const char *const tag, char *const alt_tag)
Definition audio.c:589
audio_play_music
void audio_play_music(const char *const tag, char *const alt_tag, enum music_usage usage)
Definition audio.c:578
audio_stop_usage
void audio_stop_usage(void)
Definition audio.c:630
audio.h
MU_INGAME
@ MU_INGAME
Definition audio.h:46
MU_MENU
@ MU_MENU
Definition audio.h:46
client
struct civclient client
Definition client_main.c:150
client_state
enum client_states client_state(void)
Definition client_main.c:1026
sound_set_name
char sound_set_name[512]
Definition client_main.c:139
client_main.h
C_S_RUNNING
@ C_S_RUNNING
Definition client_main.h:47
incite_cost
char * incite_cost
Definition comments.c:74
connection.h
MAX_LEN_NAME
#define MAX_LEN_NAME
Definition fc_types.h:66
_
#define _(String)
Definition fcintl.h:67
log.h
fc_assert
#define fc_assert(condition)
Definition log.h:176
log_debug
#define log_debug(message,...)
Definition log.h:115
log_error
#define log_error(message,...)
Definition log.h:103
fc_malloc
#define fc_malloc(sz)
Definition mem.h:34
play_single_track
void play_single_track(const char *const tag)
Definition music.c:116
musicspec_reread_callback
void musicspec_reread_callback(struct option *poption)
Definition music.c:129
current_musicset_version
const char * current_musicset_version(void)
Definition music.c:242
current_musicset_name
const char * current_musicset_name(void)
Definition music.c:234
current_ms
struct musicset current_ms
start_menu_music
void start_menu_music(const char *const tag, char *const alt_tag)
Definition music.c:98
current_musicset_summary
const char * current_musicset_summary(void)
Definition music.c:254
current_musicset_description
const char * current_musicset_description(void)
Definition music.c:262
stop_menu_music
void stop_menu_music(void)
Definition music.c:108
musicspec_load
struct section_file * musicspec_load(const char *ms_filename)
Definition music.c:148
start_style_music
void start_style_music(void)
Definition music.c:44
stop_style_music
void stop_style_music(void)
Definition music.c:90
musicspec_close
void musicspec_close(struct section_file *tagfile)
Definition music.c:219
music.h
option_str_get
const char * option_str_get(const struct option *poption)
Definition options.c:928
gui_options
struct client_options gui_options
Definition options.c:71
options.h
len
int len
Definition packhand.c:127
secfile_load
struct section_file * secfile_load(const char *filename, bool allow_duplicates)
Definition registry.c:50
secfile_destroy
void secfile_destroy(struct section_file *secfile)
Definition section_file.c:95
secfile_lookup_str
const char * secfile_lookup_str(const struct section_file *secfile, const char *path,...)
Definition registry_ini.c:2175
secfile_lookup_str_default
const char * secfile_lookup_str_default(const struct section_file *secfile, const char *def, const char *path,...)
Definition registry_ini.c:2205
civclient::conn
struct connection conn
Definition client_main.h:96
client_options::sound_enable_menu_music
bool sound_enable_menu_music
Definition options.h:189
client_options::sound_enable_game_music
bool sound_enable_game_music
Definition options.h:190
connection::playing
struct player * playing
Definition connection.h:151
music_style
Definition style.h:29
musicset
Definition music.c:34
musicset::name
char name[MAX_LEN_NAME]
Definition music.c:35
musicset::version
char version[MAX_LEN_NAME]
Definition music.c:36
musicset::summary
char * summary
Definition music.c:37
musicset::description
char * description
Definition music.c:38
option
Definition options.c:514
player::client
struct player::@70::@73 client
player::mood
enum mood_type mood
Definition player.h:361
player::music_style
int music_style
Definition player.h:280
section_file
Definition section_file.h:36
music_style_by_number
struct music_style * music_style_by_number(int id)
Definition style.c:171
fc_strlcpy
size_t fc_strlcpy(char *dest, const char *src, size_t n)
Definition support.c:777
sz_strlcpy
#define sz_strlcpy(dest, src)
Definition support.h:189
TRUE
#define TRUE
Definition support.h:46
fc__fallthrough
#define fc__fallthrough
Definition support.h:119