游戏开发论坛

 找回密码
 立即注册
搜索
查看: 2207|回复: 0

盖莫游戏引擎2.1.1-多线程资源载入类

[复制链接]

56

主题

94

帖子

98

积分

注册会员

Rank: 2

积分
98
发表于 2010-4-6 21:28:00 | 显示全部楼层 |阅读模式

今天突然想给引擎加入一个新的功能
那就是使用多线程载入资源
如果游戏资源过多,而只采用一个线程载入资源显然是不够的

所以就加入这个功能吧
设计代码有:
1.资源基类
////////////////////////////////////////////////////////////
/// 定义资源基类
////////////////////////////////////////////////////////////
class  Resource : public Object
{
public:
    virtual ~Resource(){}
    virtual bool Load(const engine_string& file) = 0;
   
DECLARE_OBJECT(Resource)
};
这是引擎所有资源的基类

2.具体的资源
例如图形:
////////////////////////////////////////////////////////
/// 定义引擎图形基类
////////////////////////////////////////////////////////
class Image : public Resource
{
public:
    ////////////////////////////////////////////////////////
    /// 图形构造函数
    ////////////////////////////////////////////////////////
    Image(){}
   
////////////////////////////////////////////////////////
    /// 图形析构函数
    ////////////////////////////////////////////////////////
virtual ~Image(){}
////////////////////////////////////////////////////////
    /// 获取图形文件类型
    ////////////////////////////////////////////////////////
virtual const ImageType& GetFileType() const = 0;
  
    ////////////////////////////////////////////////////////
    /// 获取图形数据指针
    ////////////////////////////////////////////////////////
    virtual  uint8* GetData()const = 0;  

    ////////////////////////////////////////////////////////
    /// 获取图形宽高
    ////////////////////////////////////////////////////////
virtual const int GetWidth() const = 0;
    virtual const int GetHeight() const = 0;

    ////////////////////////////////////////////////////////
    /// 获取图形BPP
    ////////////////////////////////////////////////////////
virtual const int GetBPP() const = 0;

    ////////////////////////////////////////////////////////
    /// 检测图形是否具有alpha通道
    ////////////////////////////////////////////////////////
virtual const bool HasAlpha() const = 0;

    ////////////////////////////////////////////////////////
    /// 获取图形状态
    ////////////////////////////////////////////////////////  
    virtual const bool IsValid() = 0;

    ////////////////////////////////////////////////////////
    /// 获取指定像素颜色值
    ////////////////////////////////////////////////////////
virtual const Color GetPixel(int x, int y) const = 0;
    ////////////////////////////////////////////////////////
    /// 设置指定像素颜色值
    ////////////////////////////////////////////////////////
virtual bool SetPixel(int x, int y, const Color &color) = 0;

    ////////////////////////////////////////////////////////
    /// 获取红色掩码
    ////////////////////////////////////////////////////////
virtual int GetRedMask() const = 0;
    ////////////////////////////////////////////////////////
    /// 获取绿色掩码
    ////////////////////////////////////////////////////////
virtual int GetGreenMask() const = 0;
    ////////////////////////////////////////////////////////
    /// 获取蓝色掩码
    ////////////////////////////////////////////////////////
virtual int GetBlueMask() const = 0;

    ////////////////////////////////////////////////////////
    /// 获取扫描宽度
    ////////////////////////////////////////////////////////
virtual int GetPitch() const =0;

////////////////////////////////////////////////////////
    /// 获取图形背景色
    ////////////////////////////////////////////////////////
    virtual const Color GetKeyColor() const = 0;

////////////////////////////////////////////////////////
    /// 设置图形背景色
    ////////////////////////////////////////////////////////  
virtual bool SetKeyColor(const Color& color) = 0;

////////////////////////////////////////////////////////
    /// 图形拉伸
    ////////////////////////////////////////////////////////
    virtual bool RescaleImage(int w, int h) = 0;

    ////////////////////////////////////////////////////////
    /// 图形切割(获取指定区域图形)
    ////////////////////////////////////////////////////////
    virtual bool SplitImage(int x, int y, int w, int h) = 0;
  
    ////////////////////////////////////////////////////////
    /// 图形反色
    ////////////////////////////////////////////////////////
    virtual bool GetInvertImage() = 0;   

    ////////////////////////////////////////////////////////
    /// 图形翻转(做水平或者竖直翻转)
    ////////////////////////////////////////////////////////
virtual bool FlipImage(ImageFlip type) = 0;

////////////////////////////////////////////////////////
    /// 图形旋转
    ////////////////////////////////////////////////////////
virtual bool RotatedImage(ImageRotation type)= 0 ;

    ////////////////////////////////////////////////////////
    /// 使用指定颜色填充图形
    ////////////////////////////////////////////////////////
virtual bool FillImage(const Color &color) =0;

////////////////////////////////////////////////////////
    /// 图形载入
    ////////////////////////////////////////////////////////
    virtual bool Load(const engine_string& file) = 0;

    ////////////////////////////////////////////////////////
    /// 图形保存(可保存格式bmp,gif,png,jpg)
    ////////////////////////////////////////////////////////
    virtual bool Save(const engine_string& file) = 0;

////////////////////////////////////////////////////////
/// 调整图形gamma值
////////////////////////////////////////////////////////
virtual bool AdjustGamma(float gamma) = 0;
  
////////////////////////////////////////////////////////
/// 调整图像亮度
////////////////////////////////////////////////////////
virtual bool AdjustBrightness(float percent) = 0;

////////////////////////////////////////////////////////
/// 调整图像对比度
////////////////////////////////////////////////////////
virtual bool AdjustContrast(float percent) = 0;

////////////////////////////////////////////////////////
/// 调整图像深度
////////////////////////////////////////////////////////
virtual bool ConvertTo4Bits() = 0;
    virtual bool ConvertTo8Bits() = 0;
    virtual bool ConvertTo16Bits555() = 0;
    virtual bool ConvertTo16Bits565() = 0;
virtual bool ConvertTo24Bits() = 0;
virtual bool ConvertTo32Bits() = 0;

////////////////////////////////////////////////////////
    /// 图形复制函数
    ////////////////////////////////////////////////////////
virtual RefPtr<Image> CopyImage() = 0;

    DECLARE_OBJECT(Image)
};

3.线程资源载入类
////////////////////////////////////////////////////////////
/// 定义资源线程载入器O(∩_∩)O~
////////////////////////////////////////////////////////////'
class ResThreadLoader : public Object
{   
public:
    ////////////////////////////////////////////////////////
    /// 构造,析构资源线程载入器
    ////////////////////////////////////////////////////////'
    ResThreadLoader(){}
virtual ~ResThreadLoader(){}

    ////////////////////////////////////////////////////////
    /// 资源的绑定(绑定成功则返回真)
    ////////////////////////////////////////////////////////'
    virtual bool AttachResource(const engine_string& name,Resource* resource) = 0;
    ////////////////////////////////////////////////////////
    /// 调用线程载入资源(一个资源对应一个线程)
    ////////////////////////////////////////////////////////'  
    virtual void Excute() = 0;
   
    DECLARE_OBJECT(ResThreadLoader)  
};

在使用的时候首先从资源管理器中获取全局线程资源载入器
然后绑定要加载的资源
其后调用Excute(函数名写错了!)
其后资源就被载入

下面是具体的例子:
    float time = device->GetTime();
   
    //! 单线程载入
    //for(int i = 0; i < 12 ; i ++)
    //   image = resource_manager->GetImageManager()->CreateObject(logoindex);"..\\image/logo.jpg");
     
    //! 多线程载入
    for(int i = 0; i < 12 ; i ++)
       image = resource_manager->GetImageManager()->CreateObject(logoindex);
    RefPtr<ResThreadLoader> loader = device->GetResourceManager()->GetResLoader();
    for(int i = 0; i < 12; i++)
       loader->AttachResource("..\\image/logo.jpg",image.get());  
     
    //! 多线程载入
    loader->Excute();
   
    std::cout<<device->GetTime()- time<<std::endl;
     
    //! 反色   
    image[0]->GetInvertImage();
    //! 水平翻转
    image[1]->FlipImage(core::ImageFlip_Horizontal);
    //! 竖直翻转
    image[2]->FlipImage(core::ImageFlip_Vertical);   
    //! Gamma调整
    image[3]->AdjustGamma(0.4);
    //! 亮度调整
    image[4]->AdjustBrightness(0.4);
    //! 对比度调整
    image[5]->AdjustContrast(0.6);
    //! 水平翻转
    image[6]->FlipImage(core::ImageFlip_Horizontal);
    //! 竖直翻转
    image[7]->FlipImage(core::ImageFlip_Vertical);   
    //! Gamma调整
    image[8]->AdjustGamma(0.1);
    //! 亮度调整
    image[9]->AdjustBrightness(0.8);
    //! 对比度调整
    image[10]->AdjustContrast(0.3);  
   
    RefPtr<TextureManager> texturemanager = resource_manager->GetTextureManager();
        
    for(int i = 0; i < 12; i++)
    {      
        texture = texturemanager->CreateObject(logoindex.c_str(),image);
        texture->Generate();
    }

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2024-5-14 00:38

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表