update TileData class

This commit is contained in:
hyzboy 2020-07-24 17:50:46 +08:00
parent 42de618db7
commit 1784e1b2e2
3 changed files with 59 additions and 99 deletions

2
CMCore

@ -1 +1 @@
Subproject commit 95f8e803d3557f4bac1a7b4ef6bca116d64a5ea9 Subproject commit b003585d43a6050d76d4ea804725c57fa359bc8a

View File

@ -2,6 +2,7 @@
#define HGL_GRAPH_TILE_DATA_INCLUDE #define HGL_GRAPH_TILE_DATA_INCLUDE
#include<hgl/graph/vulkan/VKTexture.h> #include<hgl/graph/vulkan/VKTexture.h>
#include<hgl/type/Pool.h>
VK_NAMESPACE_USING VK_NAMESPACE_USING
@ -21,12 +22,13 @@ namespace hgl
struct Object struct Object
{ {
int index; int col,row;
double left,top;
double fl,ft,fw,fh; double fl,ft,fw,fh;
int width,height; int width,height;
};//struct TileObject };//struct Object
protected: protected:
@ -34,7 +36,7 @@ namespace hgl
Texture2D *tile_texture; ///<TileData所用的纹理对象 Texture2D *tile_texture; ///<TileData所用的纹理对象
TileData::Object **tile_object; ///<所有的Tile对象 ObjectPool<TileData::Object> 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字节数
@ -43,8 +45,8 @@ namespace hgl
protected: protected:
int FindSpace(); ///<寻找一个空位 TileData::Object *FindSpace(); ///<寻找一个空位
void WriteTile(const int,TileData::Object *,const void *,const uint,const int,const int); ///<写入一个Tile数据 bool WriteTile(TileData::Object *,const void *,const uint,const int,const int); ///<写入一个Tile数据
public: public:

View File

@ -22,7 +22,28 @@ namespace hgl
tile_max_count=tile_rows*tile_cols; tile_max_count=tile_rows*tile_cols;
tile_count=0; tile_count=0;
NEW_NULL_ARRAY(tile_object,TileData::Object *,tile_max_count); 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_bytes=tile_width*tile_height*GetStrideByFormat(tile_texture->GetFormat());
@ -33,53 +54,37 @@ namespace hgl
{ {
SAFE_CLEAR(tile_buffer); SAFE_CLEAR(tile_buffer);
SAFE_CLEAR(tile_texture); SAFE_CLEAR(tile_texture);
SAFE_CLEAR_OBJECT_ARRAY(tile_object,tile_max_count);
} }
int TileData::FindSpace() TileData::Object *TileData::FindSpace()
{ {
if(!tile_object)return(-1); TileData::Object *obj;
if(tile_count>=tile_max_count)return(-1);
int n=tile_max_count; if(!to_pool.Get(obj))
return(nullptr);
while(n--) return obj;
if(!(tile_object[n]))
return(n);
LOG_PROBLEM(OS_TEXT("无法在Tile数据区内找到足够的空间"));
return(-1);
} }
void TileData::WriteTile(const int index,TileData::Object *obj,const void *data,const uint bytes,int ctw,int cth) bool TileData::WriteTile(TileData::Object *obj,const void *data,const uint bytes,int ctw,int cth)
{ {
int col,row; if(!obj||!data||!bytes||ctw<=0||cth<=0)
double left,top; return(false);
col=index%tile_cols;
row=index/tile_cols;
left=tile_width *col;
top =tile_height*row;
obj->index =index;
obj->width =(ctw==-1)?tile_width:ctw; obj->width =(ctw==-1)?tile_width:ctw;
obj->height =(cth==-1)?tile_height:cth; obj->height =(cth==-1)?tile_height:cth;
obj->fl=left/double(tile_texture->GetWidth()); obj->fl=obj->left/double(tile_texture->GetWidth());
obj->ft=top /double(tile_texture->GetHeight()); obj->ft=obj->top /double(tile_texture->GetHeight());
obj->fw=double(obj->width)/double(tile_texture->GetWidth()); obj->fw=double(obj->width)/double(tile_texture->GetWidth());
obj->fh=double(obj->height)/double(tile_texture->GetHeight()); obj->fh=double(obj->height)/double(tile_texture->GetHeight());
tile_object[index]=obj;
tile_buffer->Write(data,0,bytes); tile_buffer->Write(data,0,bytes);
device->ChangeTexture2D(tile_texture, device->ChangeTexture2D(tile_texture,
tile_buffer, tile_buffer,
left, obj->left,
top, obj->top,
tile_width, tile_width,
tile_height); tile_height);
@ -94,6 +99,8 @@ namespace hgl
// texcoord->End(); // texcoord->End();
// vertex->End(); // vertex->End();
//} //}
return(true);
} }
/** /**
@ -106,20 +113,15 @@ namespace hgl
*/ */
TileData::Object *TileData::Add(const void *data,const uint bytes,const int ctw,const int cth) TileData::Object *TileData::Add(const void *data,const uint bytes,const int ctw,const int cth)
{ {
if(!tile_object)return(nullptr); if(!data||!bytes||ctw<=0||cth<=0)
int index;
index=FindSpace();
if(index==-1)
{
LOG_PROBLEM(OS_TEXT("找不到空的Tile数据区!"));
return(nullptr); return(nullptr);
}
TileData::Object *obj=new TileData::Object; TileData::Object *obj=FindSpace();
WriteTile(index,obj,data,bytes,ctw,cth); if(!obj)
return(nullptr);
WriteTile(obj,data,bytes,ctw,cth);
tile_count++; tile_count++;
return(obj); return(obj);
@ -132,30 +134,9 @@ namespace hgl
*/ */
bool TileData::Delete(TileData::Object *obj) bool TileData::Delete(TileData::Object *obj)
{ {
if(!tile_object)return(false); if(!obj)return(false);
if(tile_object[obj->index]) return to_pool.Release(obj);
{
if(tile_object[obj->index]!=obj)
{
LOG_PROBLEM(OS_TEXT("要删除的TileData::Object和TileData中的不对应"));
return(false);
}
else
{
tile_object[obj->index]=nullptr;
tile_count--;
delete obj;
return(true);
}
}
else
{
LOG_PROBLEM(OS_TEXT("要删除的TileData::Object对象在TileData中不存在"));
return(false);
}
} }
/** /**
@ -169,27 +150,13 @@ namespace hgl
*/ */
bool TileData::Change(TileData::Object *obj,const void *data,const uint bytes,const int ctw,const int cth) bool TileData::Change(TileData::Object *obj,const void *data,const uint bytes,const int ctw,const int cth)
{ {
if(!tile_object)return(false); if(!obj||!data||!bytes||ctw<=0||cth<=0)
if(tile_object[obj->index])
{
if(tile_object[obj->index]!=obj)
{
LOG_PROBLEM(OS_TEXT("要更改的TileData::Object和TileData中的不对应"));
return(false);
}
else
{
WriteTile(obj->index,obj,data,bytes,ctw,cth);
return(true);
}
}
else
{
LOG_PROBLEM(OS_TEXT("要更改的TileData::Object对象在TileData中不存在"));
return(false); return(false);
}
if(!to_pool.IsActive(obj))
return(false);
return WriteTile(obj,data,bytes,ctw,cth);
} }
/** /**
@ -197,16 +164,7 @@ namespace hgl
*/ */
void TileData::Clear() void TileData::Clear()
{ {
if(!tile_object)return; to_pool.ClearAll();
int n=tile_max_count;
while(n--)
if(tile_object[n])
{
delete tile_object[n];
tile_object[n]=nullptr;
}
} }
}//namespace graph }//namespace graph
}//namespace hgl }//namespace hgl