update TileData

This commit is contained in:
hyzboy 2020-07-24 20:25:30 +08:00
parent 22c30427b3
commit a89f150161
3 changed files with 40 additions and 52 deletions

2
CMCore

@ -1 +1 @@
Subproject commit b003585d43a6050d76d4ea804725c57fa359bc8a Subproject commit dcaf7f89602ecc24a377bc16f22f074718fb5162

View File

@ -3,6 +3,7 @@
#include<hgl/graph/vulkan/VKTexture.h> #include<hgl/graph/vulkan/VKTexture.h>
#include<hgl/type/Pool.h> #include<hgl/type/Pool.h>
#include<hgl/type/RectScope.h>
VK_NAMESPACE_USING VK_NAMESPACE_USING
@ -10,6 +11,14 @@ namespace hgl
{ {
namespace graph namespace graph
{ {
struct TileObject
{
int col,row; //当前tile在整个纹理中的tile位置
RectScope2d uv_pixel; //以象素为单位的tile位置和尺寸
RectScope2d uv_float; //以浮点为单位的tile位置和尺寸
};//struct TileObject
/** /**
* TileData是一种处理大量等同尺寸及格式贴图的管理机制(Tile的大小不必符合2次幂) * TileData是一种处理大量等同尺寸及格式贴图的管理机制(Tile的大小不必符合2次幂)
* Tile的增加或删除I/O消耗 * Tile的增加或删除I/O消耗
@ -18,25 +27,13 @@ namespace hgl
{ {
Device *device; Device *device;
public:
struct Object
{
int col,row;
double left,top;
double fl,ft,fw,fh;
int width,height;
};//struct Object
protected: protected:
vulkan::Buffer *tile_buffer; ///<Tile暂存缓冲区 vulkan::Buffer *tile_buffer; ///<Tile暂存缓冲区
Texture2D *tile_texture; ///<TileData所用的纹理对象 Texture2D *tile_texture; ///<TileData所用的纹理对象
ObjectPool<TileData::Object> to_pool; ///<Tile对象池 ObjectPool<TileObject> to_pool; ///<Tile对象池
uint tile_width,tile_height; ///<Tile的宽和高 uint tile_width,tile_height; ///<Tile的宽和高
uint32_t tile_bytes; ///<一个tile字节数 uint32_t tile_bytes; ///<一个tile字节数
@ -45,8 +42,8 @@ namespace hgl
protected: protected:
TileData::Object *FindSpace(); ///<寻找一个空位 TileObject *FindSpace(); ///<寻找一个空位
bool WriteTile(TileData::Object *,const void *,const uint,const int,const int); ///<写入一个Tile数据 bool WriteTile(TileObject *,const void *,const uint,const int,const int); ///<写入一个Tile数据
public: public:
@ -63,11 +60,11 @@ namespace hgl
TileData(Device *,Texture2D *,const uint tw,const uint th); TileData(Device *,Texture2D *,const uint tw,const uint th);
virtual ~TileData(); virtual ~TileData();
TileData::Object *Add(const void *,const uint,const int=-1,const int=-1); ///<增加一个Tile TileObject *Add(const void *,const uint,const int=-1,const int=-1); ///<增加一个Tile
// TileData::Object *Add(Bitmap2D *,int=-1,int=-1); ///<增加一个Tile // TileObject *Add(Bitmap2D *,int=-1,int=-1); ///<增加一个Tile
bool Delete(TileData::Object *); ///<删除一个Tile bool Delete(TileObject *); ///<删除一个Tile
bool Change(TileData::Object *,const void *,const uint,const int=-1,const int=-1); ///<更改一个Tile的数据内容 bool Change(TileObject *,const void *,const uint,const int=-1,const int=-1); ///<更改一个Tile的数据内容
void Clear(); ///<清除Tile数据 void Clear(); ///<清除Tile数据
};//class TileData };//class TileData
}//namespace graph }//namespace graph

View File

@ -25,18 +25,21 @@ namespace hgl
to_pool.PreMalloc(tile_max_count); to_pool.PreMalloc(tile_max_count);
{ {
int col=0,row=0; int col=0,row=0;
TileData::Object **to=to_pool.GetInactiveData(); TileObject **to=to_pool.GetInactiveData();
for(uint i=0;i<tile_max_count;i++) for(uint i=0;i<tile_max_count;i++)
{ {
(*to)->col =col; (*to)->col =col;
(*to)->row =row; (*to)->row =row;
(*to)->left =col*tile_width;
(*to)->top =row*tile_height; (*to)->uv_pixel.Set(col*tile_width,
row*tile_height,
0,
0);
++to; ++to;
++col; ++col;
if(col==tile_cols) if(col==tile_cols)
{ {
++row; ++row;
@ -56,9 +59,9 @@ namespace hgl
SAFE_CLEAR(tile_texture); SAFE_CLEAR(tile_texture);
} }
TileData::Object *TileData::FindSpace() TileObject *TileData::FindSpace()
{ {
TileData::Object *obj; TileObject *obj;
if(!to_pool.Get(obj)) if(!to_pool.Get(obj))
return(nullptr); return(nullptr);
@ -66,40 +69,28 @@ namespace hgl
return obj; return obj;
} }
bool TileData::WriteTile(TileData::Object *obj,const void *data,const uint bytes,int ctw,int cth) bool TileData::WriteTile(TileObject *obj,const void *data,const uint bytes,int ctw,int cth)
{ {
if(!obj||!data||!bytes||ctw<=0||cth<=0) if(!obj||!data||!bytes||ctw<=0||cth<=0)
return(false); return(false);
obj->width =(ctw==-1)?tile_width:ctw; obj->uv_pixel.SetSize( (ctw==-1)?tile_width:ctw,
obj->height =(cth==-1)?tile_height:cth; (cth==-1)?tile_height:cth);
obj->fl=obj->left/double(tile_texture->GetWidth()); UVFloatFromPixel( obj->uv_float,
obj->ft=obj->top /double(tile_texture->GetHeight()); obj->uv_pixel,
obj->fw=double(obj->width)/double(tile_texture->GetWidth()); tile_texture->GetWidth(),
obj->fh=double(obj->height)/double(tile_texture->GetHeight()); tile_texture->GetHeight());
tile_buffer->Write(data,0,bytes); tile_buffer->Write(data,0,bytes);
device->ChangeTexture2D(tile_texture, device->ChangeTexture2D(tile_texture,
tile_buffer, tile_buffer,
obj->left, obj->uv_pixel.GetLeft(),
obj->top, obj->uv_pixel.GetTop(),
tile_width, tile_width,
tile_height); tile_height);
//请保留这段代码,以便未来使用时该数据时不会使用
//{
// vertex->Begin(index*6);
// texcoord->Begin(index*6);
// texcoord->WriteRect(obj->fl,obj->ft,obj->fw,obj->fh);
// vertex->WriteRect(0,0,obj->width,obj->height);
// texcoord->End();
// vertex->End();
//}
return(true); return(true);
} }
@ -111,12 +102,12 @@ namespace hgl
* @param cth tile高度,-1 * @param cth tile高度,-1
* @return Tile创建的对象 * @return Tile创建的对象
*/ */
TileData::Object *TileData::Add(const void *data,const uint bytes,const int ctw,const int cth) TileObject *TileData::Add(const void *data,const uint bytes,const int ctw,const int cth)
{ {
if(!data||!bytes||ctw<=0||cth<=0) if(!data||!bytes||ctw<=0||cth<=0)
return(nullptr); return(nullptr);
TileData::Object *obj=FindSpace(); TileObject *obj=FindSpace();
if(!obj) if(!obj)
return(nullptr); return(nullptr);
@ -132,7 +123,7 @@ namespace hgl
* @param obj Tile的对象指针 * @param obj Tile的对象指针
* @return * @return
*/ */
bool TileData::Delete(TileData::Object *obj) bool TileData::Delete(TileObject *obj)
{ {
if(!obj)return(false); if(!obj)return(false);
@ -148,7 +139,7 @@ namespace hgl
* @param cth tile高度,-1 * @param cth tile高度,-1
* @return * @return
*/ */
bool TileData::Change(TileData::Object *obj,const void *data,const uint bytes,const int ctw,const int cth) bool TileData::Change(TileObject *obj,const void *data,const uint bytes,const int ctw,const int cth)
{ {
if(!obj||!data||!bytes||ctw<=0||cth<=0) if(!obj||!data||!bytes||ctw<=0||cth<=0)
return(false); return(false);
@ -164,7 +155,7 @@ namespace hgl
*/ */
void TileData::Clear() void TileData::Clear()
{ {
to_pool.ClearAll(); to_pool.ClearActive();
} }
}//namespace graph }//namespace graph
}//namespace hgl }//namespace hgl