Debian Codegraph @

src/gengine/Dialog.cpp

raw
1 /*
2 * Copyright (C) 2004 Ivo Danihelka ([email protected])
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 */
9 #include "Dialog.h"
10
11 #include "Log.h"
12 #include "SoundAgent.h"
13 #include "ResSoundPack.h"
14 #include "Path.h"
15 #include "minmax.h"
16
17 const std::string Dialog::DEFAULT_LANG = "en";
18 //-----------------------------------------------------------------
19 /**
20 * Create new dialog.
21 * Both sound file and subtitles are optional.
22 */
23 Dialog::Dialog(const std::string &lang,
24 const std::string &soundfile, const std::string &subtitle)
25 : m_soundfile(soundfile), m_lang(lang), m_subtitle(subtitle)
26 {
27 m_sound = NULL;
28 }
29 //-----------------------------------------------------------------
30 Dialog::~Dialog()
31 {
32 if (m_sound) {
33 Mix_FreeChunk(m_sound);
34 }
35 }
36 //-----------------------------------------------------------------
37 /**
38 * Run dialog.
39 * Do lazy loading of sound.
40 *
41 * @param volume sound volume
42 * @param loops numer of loops. 0=play once, 1=play twice, -1=play infinite
43 * @return channel number where the sound is played or -1
44 */
45 int
46 Dialog::talk(int volume, int loops) const
47 {
48 if (NULL == m_sound && !m_soundfile.empty()) {
49 Path soundPath = Path::dataReadPath(m_soundfile);
50 m_sound = ResSoundPack::loadSound(soundPath);
51 }
52
53 int channel = SoundAgent::agent()->playSound(m_sound, volume, loops);
54 return channel;
55 }
56 //-----------------------------------------------------------------
57 /**
58 * Override this method to run subtitles.
59 */
60 void
61 Dialog::runSubtitle(const StringTool::t_args &args) const
62 {
63 LOG_INFO(ExInfo("subtitle")
64 .addInfo("content", getFormatedSubtitle(args)));
65 }
66 //-----------------------------------------------------------------
67 /**
68 * Replace %1, %2, ... with the arguments.
69 * NOTE: %0 is not expanded
70 */
71 std::string
72 Dialog::getFormatedSubtitle(const StringTool::t_args &args) const
73 {
74 std::string buffer = m_subtitle;
75 for (unsigned int i = 1; i < args.size(); ++i) {
76 StringTool::replace(buffer,
77 "%" + StringTool::toString(i), args[i]);
78 }
79 return buffer;
80 }
81 //-----------------------------------------------------------------
82 /**
83 * Minimal time according subtitle length.
84 * @return minimal number of cycles for talk
85 */
86 int
87 Dialog::getMinTime() const
88 {
89 return min(180, StringTool::utf8Length(m_subtitle));
90 }
91
92