MMORPG大型游戏设计与开发(客户端架构 part11 of vegine)

从早年的无声电影到现在的逼真3D大片,人类在科技上可谓是一再突破。不知道有没有人经历过那无声的日子,没有声音的世界,咱们的耳朵也就失去了它本有的用途了。在游戏世界中,声音元素成了必不可少的一部分,一个没有声音的游戏现在可谓是太少见了,而且存活下来的希望自然不高。当前在游戏中,特别是3D游戏中,声音分为3D音效和平面音效。3D音效中主要是指环境音效,比如说某个地方的流水发出的声音,某片树林里充满的鸟叫声。平面音效即咱们感官的普通音效,背景音乐,自身的技能、UI声音。

CODE

  模块sound 文件buffer.h -- 声音资源缓存

/**
 * PAP Engine ( -- )
 * $Id buffer.h
 * @link -- for the canonical source repository
 * @copyright Copyright (c) 2013-2014 viticm( viticm@126.com )
 * @license
 * @user viticm<viticm@126.com/viticm.ti@gmail.com>
 * @date 2014-3-19 20:14:03
 * @uses vengine sound buffer class
 */
#ifndef VENGINE_SOUND_BUFFER_H_
#define VENGINE_SOUND_BUFFER_H_

#include "vengine/config.h"

namespace vengine_sound {

class VENGINE_API Buffer {

 public:
   virtual bool read_wavbuffer(const STRING& name) = 0; //从文件中读取buffer
   virtual void release() = 0; //释放
   virtual int32_t get_reference() const = 0; //获得引用资源


};

}; //namespace vengine_sound

#endif //VENGINE_SOUND_BUFFER_H_

  模块sound 文件source.h --声音资源类

/**
 * PAP Engine ( -- )
 * $Id source.h
 * @link -- for the canonical source repository
 * @copyright Copyright (c) 2013-2014 viticm( viticm@126.com )
 * @license
 * @user viticm<viticm@126.com/viticm.ti@gmail.com>
 * @date 2014-3-20 11:41:14
 * @uses vengine sound source class
 */
#ifndef VENGINE_SOUND_SOURCE_H_
#define VENGINE_SOUND_SOURCE_H_

#include "vengine/config.h"
#include "vengine/sound/buffer.h"
#include "vengine/math/base.h"

namespace vengine_sound {

class VENGINE_API Source {

 public:
   typedef enum {
     kTypeBackground, //背景音乐
     kTypeSkill, //技能
     kTypeEnvironment, //环境音乐
     kTypeUI //UI特效
   } type_enum;
   
 public:
   virtual int32_t getid() const = 0;
   //播放中不立即生效,需要stop后重新play
   virtual void setbuffer(Buffer* buffer) = 0;
   virtual Buffer* getbuffer() = 0;
   //循环播放
   virtual void setlooping(bool flag) = 0;
   virtual bool islooping() const = 0;
   virtual void play() = 0;
   virtual void stop() = 0;
   virtual bool isplaying() const = 0;
   virtual bool isstoped() const = 0;
   virtual void setposition(
       const vengine_math::base::threefloat_vector_t& position) = 0;
   virtual const vengine_math::base::threefloat_vector_t& getposition() = 0;
   virtual void updatevolume() = 0;
};

}; //namespace vengine_sound

#endif //VENGINE_SOUND_SOURCE_H_

  模块sound 文件system.h

/**
 * PAP Engine ( -- )
 * $Id system.h
 * @link -- for the canonical source repository
 * @copyright Copyright (c) 2013-2014 viticm( viticm@126.com )
 * @license
 * @user viticm<viticm@126.com/viticm.ti@gmail.com>
 * @date 2014-3-20 15:03:13
 * @uses vengine sound system module
 */
#ifndef VENGINE_SOUND_SYSTEM_H_
#define VENGINE_SOUND_SYSTEM_H_

#include "vengine/config.h"
#include "vengine/kernel/node.h"
#include "vengine/sound/buffer.h"
#include "vengine/sound/source.h"

namespace vengine_sound {

//外提供的声音播放接口,返回声音源HANDLE 
typedef int32_t (__stdcall* function_play)
  (const char* soundfile, float* position, bool loop);
//外提供声音的停止接口
typedef void (__stdcall* function_stop)(int32_t id);

class VENGINE_API System : public vengine_kernel::Node {

VENGINE_KERNEL_DECLARE_DYNAMIC(vengine_sound_System);

 public:
   virtual Buffer* createbuffer(int32_t id) = 0; //创建一个声音资源
   virtual Buffer* createbuffer(const char* filename) = 0; //创建一个声音资源
   //创建/删除播放源
   //3D模式 - 音量会随着位置变化
   //自动删除模式 - 只播放一遍,然后自动释放
   virtual Source* createsource(Source::type_enum type, 
                                bool is3d, 
                                bool autodestroy) = 0;
   virtual Source* createsource(Source::type_enum type,
                                bool is3d,
                                bool autodestroy,
                                Source** reference) = 0;
   virtual void destroysource(Source* source) = 0;
   virtual void destroysource(int32_t id) = 0;
   virtual void play_UIsound(int32_t id) = 0;

   //收听者
   virtual void set_listenerposition(
       vengine_math::base::threefloat_vector_t& position) = 0;
   virtual vengine_math::base::threefloat_vector_t& 
     get_listenerposition() = 0;
   
   virtual function_play get_playfunction() = 0;
   virtual function_stop get_stopfunction() = 0;

};

}; //namespace vengine_sound

#endif //VENGINE_SOUND_SYSTEM_H_

SIMPLE

  看看天龙八部里面有哪些声音配置

总结

  本次设计中用到的模式与武侠世界/天龙八部的声音模块相似的配置,依赖于声音库fmod,大家可以去了解一下该库的用法,如果对代码中不明白的接口,欢迎提出问题。由于时间关系,现阶段我就不逐条解释了。下节讲的是资源和输入模块,虽然是两个模块,但是比较小。

posted @ 2014-08-15 11:40  恋月  阅读(1306)  评论(2编辑  收藏  举报