update TileFont codes

This commit is contained in:
2020-07-31 22:21:44 +08:00
parent 1f80f793ac
commit 18b00d40fe
5 changed files with 150 additions and 27 deletions

View File

@@ -3,27 +3,104 @@
#include<hgl/graph/TileData.h>
#include<hgl/graph/font/FontSource.h>
#include<hgl/type/ResPoolManage.h>
namespace hgl
{
namespace graph
{
class TileObjectPool
template<typename T> 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<typename T> struct RefData
template<typename T> class DataPool
{
public:
DataPool();
virtual ~DataPool()=default;
virtual bool Acquire(T &)=0;
virtual void Release(const T)=0;
};//
template<typename K,typename V> class ResPool
{
protected:
using ActiveItem=RefData<V>;
DataPool<V> *pool;
MapObject<K,ActiveItem> active_items; ///<活跃的数据
Map<K,V> idle_items; ///<引用计数为0的
public:
TileObjectPool(TileData *td):tile_data(td);
};
ResPool(DataPool<V> *dp):pool(dp){}
virtual ~ResPool()=default;
using TileObjectManage=_ResPoolManage<u32char,TileObject *,TileObjectPool>;
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<typename K,typename V> class ResPool
using TileResPool=ResPool<u32char,TileObject *>;
/**
* Tile字符管理<br>
@@ -34,8 +111,8 @@ namespace hgl
FontSource *source;
TileData *tile_data;
TileObjectPool *tile_pool;
TileObjectManage *ch_tile_pool;
DataPool<TileObject *> *to_pool;
TileResPool *to_res;
public:
@@ -47,7 +124,7 @@ namespace hgl
TileFont(TileData *td,FontSource *fs);
virtual ~TileFont();
bool Registry(List<RectScope2f> &,const List<u32char> &); ///<注册要使用的字符
bool Registry(List<TileUVFloat> &,const List<u32char> &); ///<注册要使用的字符
void Unregistry(const List<u32char> &); ///<注销要使用的字符
};//class TileFont
}//namespace graph