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>
|
2020-06-21 17:48:50 +08:00
|
|
|
|
#include<hgl/graph/vulkan/VKDevice.h>
|
|
|
|
|
#include<hgl/graph/vulkan/VKBuffer.h>
|
2020-06-20 19:40:09 +08:00
|
|
|
|
|
|
|
|
|
namespace hgl
|
|
|
|
|
{
|
|
|
|
|
namespace graph
|
|
|
|
|
{
|
2020-06-21 17:48:50 +08:00
|
|
|
|
TileData::TileData(Device *dev,Texture2D *tt,const uint tw,const uint th)
|
2020-06-20 19:40:09 +08:00
|
|
|
|
{
|
2020-06-21 17:48:50 +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;
|
2020-07-24 20:25:30 +08:00
|
|
|
|
TileObject **to=to_pool.GetInactiveData();
|
2020-07-24 17:50:46 +08:00
|
|
|
|
|
|
|
|
|
for(uint i=0;i<tile_max_count;i++)
|
|
|
|
|
{
|
|
|
|
|
(*to)->col =col;
|
|
|
|
|
(*to)->row =row;
|
|
|
|
|
|
2020-07-24 20:25:30 +08:00
|
|
|
|
(*to)->uv_pixel.Set(col*tile_width,
|
|
|
|
|
row*tile_height,
|
|
|
|
|
0,
|
|
|
|
|
0);
|
2020-07-24 17:50:46 +08:00
|
|
|
|
|
2020-07-24 20:25:30 +08:00
|
|
|
|
++to;
|
2020-07-24 17:50:46 +08:00
|
|
|
|
++col;
|
2020-07-24 20:25:30 +08:00
|
|
|
|
|
2020-07-24 17:50:46 +08:00
|
|
|
|
if(col==tile_cols)
|
|
|
|
|
{
|
|
|
|
|
++row;
|
|
|
|
|
col=0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-21 17:48:50 +08:00
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
{
|
2020-06-21 17:48:50 +08:00
|
|
|
|
SAFE_CLEAR(tile_buffer);
|
2020-06-21 02:23:11 +08:00
|
|
|
|
SAFE_CLEAR(tile_texture);
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-24 20:25:30 +08:00
|
|
|
|
TileObject *TileData::FindSpace()
|
2020-06-21 02:23:11 +08:00
|
|
|
|
{
|
2020-07-24 20:25:30 +08:00
|
|
|
|
TileObject *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 20:25:30 +08:00
|
|
|
|
bool TileData::WriteTile(TileObject *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
|
|
|
|
|
2020-07-24 20:25:30 +08:00
|
|
|
|
obj->uv_pixel.SetSize( (ctw==-1)?tile_width:ctw,
|
|
|
|
|
(cth==-1)?tile_height:cth);
|
2020-06-21 02:23:11 +08:00
|
|
|
|
|
2020-07-24 20:25:30 +08:00
|
|
|
|
UVFloatFromPixel( obj->uv_float,
|
|
|
|
|
obj->uv_pixel,
|
|
|
|
|
tile_texture->GetWidth(),
|
|
|
|
|
tile_texture->GetHeight());
|
2020-06-21 02:23:11 +08:00
|
|
|
|
|
2020-06-21 17:48:50 +08:00
|
|
|
|
tile_buffer->Write(data,0,bytes);
|
|
|
|
|
|
|
|
|
|
device->ChangeTexture2D(tile_texture,
|
|
|
|
|
tile_buffer,
|
2020-07-24 20:25:30 +08:00
|
|
|
|
obj->uv_pixel.GetLeft(),
|
|
|
|
|
obj->uv_pixel.GetTop(),
|
2020-06-21 17:48:50 +08:00
|
|
|
|
tile_width,
|
|
|
|
|
tile_height);
|
|
|
|
|
|
2020-07-24 17:50:46 +08:00
|
|
|
|
return(true);
|
2020-06-21 02:23:11 +08:00
|
|
|
|
}
|
2020-06-21 17:48:50 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 增加一个Tile
|
|
|
|
|
* @param data 图形原始数据
|
|
|
|
|
* @param bytes 图形原始数据字节数
|
|
|
|
|
* @param ctw 当前tile宽度,-1表示等同全局设置
|
|
|
|
|
* @param cth 当前tile高度,-1表示等同全局设置
|
|
|
|
|
* @return 为增加的Tile创建的对象
|
|
|
|
|
*/
|
2020-07-24 20:25:30 +08:00
|
|
|
|
TileObject *TileData::Add(const void *data,const uint bytes,const int ctw,const int cth)
|
2020-06-21 17:48:50 +08:00
|
|
|
|
{
|
2020-07-24 17:50:46 +08:00
|
|
|
|
if(!data||!bytes||ctw<=0||cth<=0)
|
|
|
|
|
return(nullptr);
|
2020-06-21 17:48:50 +08:00
|
|
|
|
|
2020-07-24 20:25:30 +08:00
|
|
|
|
TileObject *obj=FindSpace();
|
2020-06-21 17:48:50 +08:00
|
|
|
|
|
2020-07-24 17:50:46 +08:00
|
|
|
|
if(!obj)
|
2020-06-21 17:48:50 +08:00
|
|
|
|
return(nullptr);
|
|
|
|
|
|
2020-07-24 17:50:46 +08:00
|
|
|
|
WriteTile(obj,data,bytes,ctw,cth);
|
2020-06-21 17:48:50 +08:00
|
|
|
|
|
|
|
|
|
tile_count++;
|
|
|
|
|
return(obj);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 删除一个Tile
|
|
|
|
|
* @param obj 要删除的Tile的对象指针
|
|
|
|
|
* @return 删除是否成功
|
|
|
|
|
*/
|
2020-07-24 20:25:30 +08:00
|
|
|
|
bool TileData::Delete(TileObject *obj)
|
2020-06-21 17:48:50 +08:00
|
|
|
|
{
|
2020-07-24 17:50:46 +08:00
|
|
|
|
if(!obj)return(false);
|
2020-06-21 17:48:50 +08:00
|
|
|
|
|
2020-07-24 17:50:46 +08:00
|
|
|
|
return to_pool.Release(obj);
|
2020-06-21 17:48:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 更改一个Tile的数据内容
|
|
|
|
|
* @param obj 要更改的Tile的对象指针
|
|
|
|
|
* @param data 图形原始数据
|
|
|
|
|
* @param bytes 图形原始数据字节数
|
|
|
|
|
* @param ctw 当前tile宽度,-1表示等同全局设置
|
|
|
|
|
* @param cth 当前tile高度,-1表示等同全局设置
|
|
|
|
|
* @return 更改是否成功
|
|
|
|
|
*/
|
2020-07-24 20:25:30 +08:00
|
|
|
|
bool TileData::Change(TileObject *obj,const void *data,const uint bytes,const int ctw,const int cth)
|
2020-06-21 17:48:50 +08:00
|
|
|
|
{
|
2020-07-24 17:50:46 +08:00
|
|
|
|
if(!obj||!data||!bytes||ctw<=0||cth<=0)
|
|
|
|
|
return(false);
|
2020-06-21 17:48:50 +08:00
|
|
|
|
|
2020-07-24 17:50:46 +08:00
|
|
|
|
if(!to_pool.IsActive(obj))
|
2020-06-21 17:48:50 +08:00
|
|
|
|
return(false);
|
2020-07-24 17:50:46 +08:00
|
|
|
|
|
|
|
|
|
return WriteTile(obj,data,bytes,ctw,cth);
|
2020-06-21 17:48:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 清除所有Tile数据
|
|
|
|
|
*/
|
|
|
|
|
void TileData::Clear()
|
|
|
|
|
{
|
2020-07-24 20:25:30 +08:00
|
|
|
|
to_pool.ClearActive();
|
2020-06-21 17:48:50 +08:00
|
|
|
|
}
|
2020-06-20 19:40:09 +08:00
|
|
|
|
}//namespace graph
|
|
|
|
|
}//namespace hgl
|