ULRE/src/SceneGraph/TileData.cpp

171 lines
4.9 KiB
C++
Raw Normal View History

2020-06-21 02:27:08 +08:00
#include<hgl/graph/TileData.h>
2020-06-21 02:23:11 +08:00
#include<hgl/log/LogInfo.h>
#include<hgl/graph/vulkan/VKDevice.h>
#include<hgl/graph/vulkan/VKBuffer.h>
2020-06-20 19:40:09 +08:00
namespace hgl
{
namespace graph
{
TileData::TileData(Device *dev,Texture2D *tt,const uint tw,const uint th)
2020-06-20 19:40:09 +08:00
{
device=dev;
2020-06-21 02:23:11 +08:00
tile_texture=tt;
tile_width=tw;
tile_height=th;
tile_rows=tile_texture->GetHeight()/tile_height;
tile_cols=tile_texture->GetWidth()/tile_width;
tile_max_count=tile_rows*tile_cols;
tile_count=0;
2020-07-24 17:50:46 +08:00
to_pool.PreMalloc(tile_max_count);
{
int col=0,row=0;
TileData::Object **to=to_pool.GetInactiveData();
for(uint i=0;i<tile_max_count;i++)
{
(*to)->col =col;
(*to)->row =row;
(*to)->left =col*tile_width;
(*to)->top =row*tile_height;
++to;
++col;
if(col==tile_cols)
{
++row;
col=0;
}
}
}
tile_bytes=tile_width*tile_height*GetStrideByFormat(tile_texture->GetFormat());
tile_buffer=device->CreateBuffer(VK_BUFFER_USAGE_TRANSFER_SRC_BIT,tile_bytes,nullptr);
2020-06-21 02:23:11 +08:00
}
TileData::~TileData()
{
SAFE_CLEAR(tile_buffer);
2020-06-21 02:23:11 +08:00
SAFE_CLEAR(tile_texture);
}
2020-07-24 17:50:46 +08:00
TileData::Object *TileData::FindSpace()
2020-06-21 02:23:11 +08:00
{
2020-07-24 17:50:46 +08:00
TileData::Object *obj;
2020-06-21 02:23:11 +08:00
2020-07-24 17:50:46 +08:00
if(!to_pool.Get(obj))
return(nullptr);
2020-06-21 02:23:11 +08:00
2020-07-24 17:50:46 +08:00
return obj;
2020-06-21 02:23:11 +08:00
}
2020-07-24 17:50:46 +08:00
bool TileData::WriteTile(TileData::Object *obj,const void *data,const uint bytes,int ctw,int cth)
2020-06-21 02:23:11 +08:00
{
2020-07-24 17:50:46 +08:00
if(!obj||!data||!bytes||ctw<=0||cth<=0)
return(false);
2020-06-21 02:23:11 +08:00
obj->width =(ctw==-1)?tile_width:ctw;
obj->height =(cth==-1)?tile_height:cth;
2020-07-24 17:50:46 +08:00
obj->fl=obj->left/double(tile_texture->GetWidth());
obj->ft=obj->top /double(tile_texture->GetHeight());
2020-06-21 02:23:11 +08:00
obj->fw=double(obj->width)/double(tile_texture->GetWidth());
obj->fh=double(obj->height)/double(tile_texture->GetHeight());
tile_buffer->Write(data,0,bytes);
device->ChangeTexture2D(tile_texture,
tile_buffer,
2020-07-24 17:50:46 +08:00
obj->left,
obj->top,
tile_width,
tile_height);
2020-06-21 02:23:11 +08:00
//请保留这段代码,以便未来使用时该数据时不会使用
//{
// 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();
//}
2020-07-24 17:50:46 +08:00
return(true);
2020-06-21 02:23:11 +08:00
}
/**
* Tile
* @param data
* @param bytes
* @param ctw tile宽度,-1
* @param cth tile高度,-1
* @return Tile创建的对象
*/
TileData::Object *TileData::Add(const void *data,const uint bytes,const int ctw,const int cth)
{
2020-07-24 17:50:46 +08:00
if(!data||!bytes||ctw<=0||cth<=0)
return(nullptr);
2020-07-24 17:50:46 +08:00
TileData::Object *obj=FindSpace();
2020-07-24 17:50:46 +08:00
if(!obj)
return(nullptr);
2020-07-24 17:50:46 +08:00
WriteTile(obj,data,bytes,ctw,cth);
tile_count++;
return(obj);
}
/**
* Tile
* @param obj Tile的对象指针
* @return
*/
bool TileData::Delete(TileData::Object *obj)
{
2020-07-24 17:50:46 +08:00
if(!obj)return(false);
2020-07-24 17:50:46 +08:00
return to_pool.Release(obj);
}
/**
* Tile的数据内容
* @param obj Tile的对象指针
* @param data
* @param bytes
* @param ctw tile宽度,-1
* @param cth tile高度,-1
* @return
*/
bool TileData::Change(TileData::Object *obj,const void *data,const uint bytes,const int ctw,const int cth)
{
2020-07-24 17:50:46 +08:00
if(!obj||!data||!bytes||ctw<=0||cth<=0)
return(false);
2020-07-24 17:50:46 +08:00
if(!to_pool.IsActive(obj))
return(false);
2020-07-24 17:50:46 +08:00
return WriteTile(obj,data,bytes,ctw,cth);
}
/**
* Tile数据
*/
void TileData::Clear()
{
2020-07-24 17:50:46 +08:00
to_pool.ClearAll();
}
2020-06-20 19:40:09 +08:00
}//namespace graph
}//namespace hgl