Main Page | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

Dialog.cpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004 Ivo Danihelka (ivo@danihelka.net)
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
00008  */
00009 #include "Dialog.h"
00010 
00011 #include "Log.h"
00012 #include "SoundAgent.h"
00013 #include "ResSoundPack.h"
00014 #include "Path.h"
00015 #include "minmax.h"
00016 
00017 const std::string Dialog::DEFAULT_LANG = "en";
00018 //-----------------------------------------------------------------
00019 /**
00020  * Create new dialog.
00021  * Both sound file and subtitles are optional.
00022  */
00023 Dialog::Dialog(const std::string &lang,
00024         const std::string &soundfile, const std::string &subtitle)
00025     : m_soundfile(soundfile), m_lang(lang), m_subtitle(subtitle)
00026 {
00027     m_sound = NULL;
00028 }
00029 //-----------------------------------------------------------------
00030 Dialog::~Dialog()
00031 {
00032     if (m_sound) {
00033         Mix_FreeChunk(m_sound);
00034     }
00035 }
00036 //-----------------------------------------------------------------
00037 /**
00038  * Run dialog.
00039  * Do lazy loading of sound.
00040  *
00041  * @param volume sound volume
00042  * @param loops numer of loops. 0=play once, 1=play twice, -1=play infinite
00043  * @return channel number where the sound is played or -1
00044  */
00045     int
00046 Dialog::talk(int volume, int loops) const
00047 {
00048     if (NULL == m_sound && !m_soundfile.empty()) {
00049         Path soundPath = Path::dataReadPath(m_soundfile);
00050         m_sound = ResSoundPack::loadSound(soundPath);
00051     }
00052 
00053     int channel = SoundAgent::agent()->playSound(m_sound, volume, loops);
00054     return channel;
00055 }
00056 //-----------------------------------------------------------------
00057 /**
00058  * Override this method to run subtitles.
00059  */
00060     void
00061 Dialog::runSubtitle(const StringTool::t_args &args) const
00062 {
00063     LOG_INFO(ExInfo("subtitle")
00064             .addInfo("content", getFormatedSubtitle(args)));
00065 }
00066 //-----------------------------------------------------------------
00067 /**
00068  * Replace %1, %2, ... with the arguments.
00069  * NOTE: %0 is not expanded
00070  */
00071 std::string
00072 Dialog::getFormatedSubtitle(const StringTool::t_args &args) const
00073 {
00074     std::string buffer = m_subtitle;
00075     for (unsigned int i = 1; i < args.size(); ++i) {
00076         StringTool::replace(buffer,
00077                 "%" + StringTool::toString(i), args[i]);
00078     }
00079     return buffer;
00080 }
00081 //-----------------------------------------------------------------
00082 /**
00083  * Minimal time according subtitle length.
00084  * @return minimal number of cycles for talk
00085  */
00086 int
00087 Dialog::getMinTime() const
00088 {
00089     return min(180, m_subtitle.size());
00090 }
00091 
00092 

Generated on Wed Jun 1 09:54:30 2005 for Fish Fillets - Next Generation by  doxygen 1.4.2