diff --git a/CMCore b/CMCore index 5866f923..fbb3c6d1 160000 --- a/CMCore +++ b/CMCore @@ -1 +1 @@ -Subproject commit 5866f9238750b11ddd925d51201da788cb234d7e +Subproject commit fbb3c6d13c17d9d5d6e2fba16f1a9614c5c9cf26 diff --git a/inc/hgl/graph/TileData.h b/inc/hgl/graph/TileData.h index c41da1f1..469e69c6 100644 --- a/inc/hgl/graph/TileData.h +++ b/inc/hgl/graph/TileData.h @@ -12,12 +12,15 @@ namespace hgl { namespace graph { + using TileUVPixel=RectScope2i; + using TileUVFloat=RectScope2d; + struct TileObject { int col,row; //当前tile在整个纹理中的tile位置 - RectScope2i uv_pixel; //以象素为单位的tile位置和尺寸 - RectScope2d uv_float; //以浮点为单位的tile位置和尺寸 + TileUVPixel uv_pixel; //以象素为单位的tile位置和尺寸 + TileUVFloat uv_float; //以浮点为单位的tile位置和尺寸 };//struct TileObject /** @@ -69,6 +72,8 @@ namespace hgl TileObject *Commit(const void *,const uint,const int=-1,const int=-1); ///<提交一个Tile TileObject *Commit(BitmapData *bmp){return this->Commit(bmp->data,bmp->total_bytes,bmp->width,bmp->height);}///<提交一个Tile + + TileObject *Acquire(); ///<请求一个Tile bool Change(TileObject *,const void *,const uint,const int=-1,const int=-1); ///<更改一个Tile的数据内容 int EndCommit(); diff --git a/inc/hgl/graph/font/TileFont.h b/inc/hgl/graph/font/TileFont.h index b886b296..63fc071b 100644 --- a/inc/hgl/graph/font/TileFont.h +++ b/inc/hgl/graph/font/TileFont.h @@ -3,27 +3,104 @@ #include #include -#include namespace hgl { namespace graph { - class TileObjectPool + template struct RefData { - TileData *tile_data; + int ref_count; + T data; public: - TileObject *Acquire(); - void Release(TileObject *); + RefData(const T &value) + { + ref_count=1; + data=value; + } + };//template struct RefData + + template class DataPool + { + public: + + DataPool(); + virtual ~DataPool()=default; + + virtual bool Acquire(T &)=0; + virtual void Release(const T)=0; + };// + + template class ResPool + { + protected: + + using ActiveItem=RefData; + + DataPool *pool; + + MapObject active_items; ///<活跃的数据 + Map idle_items; ///<引用计数为0的 public: - TileObjectPool(TileData *td):tile_data(td); - }; + ResPool(DataPool *dp):pool(dp){} + virtual ~ResPool()=default; - using TileObjectManage=_ResPoolManage; + bool Get(const K &key,V &value) + { + ActiveItem *ai; + + if(active_items.Get(key,ai)) //在活跃列表中找 + { + ++ai->ref_count; + value=ai->data; + return(true); + } + + if(idle_items.Get(key,value)) //在限制列表中找 + { + active_items.Add(key,new ActiveItem(value)); + return(true); + } + + return(false); + } + + bool Create(const K &key,V &value) + { + if(!pool->Acquire(value)) + return(false); + + active_items.Add(key,new ActiveItem(value)); + return(true); + } + + void Release(const K &key) + { + int pos; + ActiveItem *ai; + + pos=active_items.GetValueAndSerial(key,ai); + + if(pos>0) + { + --ai->ref_count; + + if(ai->ref_count==0) + { + idle_items.Add(ai->data); + active_items.DeleteBySerial(pos); + } + + return; + } + } + };//template class ResPool + + using TileResPool=ResPool; /** * Tile字符管理
@@ -34,8 +111,8 @@ namespace hgl FontSource *source; TileData *tile_data; - TileObjectPool *tile_pool; - TileObjectManage *ch_tile_pool; + DataPool *to_pool; + TileResPool *to_res; public: @@ -47,7 +124,7 @@ namespace hgl TileFont(TileData *td,FontSource *fs); virtual ~TileFont(); - bool Registry(List &,const List &); ///<注册要使用的字符 + bool Registry(List &,const List &); ///<注册要使用的字符 void Unregistry(const List &); ///<注销要使用的字符 };//class TileFont }//namespace graph diff --git a/src/SceneGraph/TileData.cpp b/src/SceneGraph/TileData.cpp index df2ed948..976685e9 100644 --- a/src/SceneGraph/TileData.cpp +++ b/src/SceneGraph/TileData.cpp @@ -147,6 +147,16 @@ namespace hgl return(obj); } + TileObject *TileData::Acquire() + { + TileObject *obj; + + if(!to_pool.Get(obj)) + return(nullptr); + + return obj; + } + /** * 删除一个Tile * @param obj 要删除的Tile的对象指针 diff --git a/src/SceneGraph/font/TileFont.cpp b/src/SceneGraph/font/TileFont.cpp index 4361789d..74c6f0ad 100644 --- a/src/SceneGraph/font/TileFont.cpp +++ b/src/SceneGraph/font/TileFont.cpp @@ -6,13 +6,30 @@ namespace hgl { namespace graph { - TileObject *TileObjectPool::Acquire() + namespace { - } + using TileObjectPointer=TileObject *; - void TileObjectPool::Release(TileObject *) - { - } + class TileObjectPool:public DataPool + { + TileData *tile_data; + + public: + + TileObjectPool(TileData *td):tile_data(td){} + + bool Acquire(TileObjectPointer &top) override + { + top=tile_data->Acquire(); + + return(top); + } + + void Release(TileObjectPointer) override + { + } + };//class TileObjectPool:public DataPool + }//namespace TileFont::TileFont(TileData *td,FontSource *fs) { @@ -21,22 +38,22 @@ namespace hgl if(fs) { source=fs; - source->RefAcquire(this); + source->RefAcquire(this); } - tile_pool=new TileObjectPool(tile_data); - ch_tile_pool=new TileObjectManage(tile_pool); + to_pool=new TileObjectPool(tile_data); + to_res=new TileResPool(to_pool); } TileFont::~TileFont() { - delete ch_tile_pool; - delete tile_pool; + delete to_res; + delete to_pool; if(source) source->RefRelease(this); - SAFE_CLEAR(tile_data); + delete tile_data; } /** @@ -44,9 +61,23 @@ namespace hgl * @param rs 每个字符在纹理中的UV坐标 * @param ch_list 要注册的字符列表 */ - bool TileFont::Registry(List &rs,const List &ch_list) + bool TileFont::Registry(List &rs,const List &ch_list) { - + const u32char *cp=ch_list.GetData(); + TileObject *to; + + List new_chars; + + for(uint i=0;iGet(*cp,to)) + new_chars.Add(*cp); + + cp++; + } + + tile_data->BeginCommit(); + tile_data->EndCommit(); } /**