2020-08-04 01:27:35 +08:00
|
|
|
|
// DrawTile
|
2020-07-28 16:37:01 +08:00
|
|
|
|
// 该示例使用TileData,演示多个tile图片在一张纹理上
|
|
|
|
|
|
|
|
|
|
#include<hgl/type/StringList.h>
|
|
|
|
|
#include<hgl/graph/TextureLoader.h>
|
2020-07-28 18:50:05 +08:00
|
|
|
|
#include<hgl/graph/TileData.h>
|
2020-07-28 16:37:01 +08:00
|
|
|
|
|
2020-07-28 18:50:05 +08:00
|
|
|
|
#include"VulkanAppFramework.h"
|
|
|
|
|
#include<hgl/graph/vulkan/VKTexture.h>
|
|
|
|
|
#include<hgl/graph/vulkan/VKSampler.h>
|
|
|
|
|
#include<hgl/math/Math.h>
|
2020-07-28 16:37:01 +08:00
|
|
|
|
|
|
|
|
|
using namespace hgl;
|
|
|
|
|
using namespace hgl::graph;
|
|
|
|
|
|
2020-07-28 18:50:05 +08:00
|
|
|
|
constexpr uint32_t SCREEN_WIDTH =1024;
|
|
|
|
|
constexpr uint32_t SCREEN_HEIGHT=512;
|
2020-07-28 16:37:01 +08:00
|
|
|
|
|
2020-07-28 18:50:05 +08:00
|
|
|
|
constexpr float BORDER=2;
|
2020-09-28 11:16:45 +08:00
|
|
|
|
constexpr uint TILE_COLS=10;
|
2020-07-28 16:37:01 +08:00
|
|
|
|
|
2020-07-28 18:50:05 +08:00
|
|
|
|
struct TileBitmap
|
2020-07-28 16:37:01 +08:00
|
|
|
|
{
|
2020-07-28 18:50:05 +08:00
|
|
|
|
BitmapData *bmp;
|
|
|
|
|
TileObject *to;
|
2020-07-28 16:37:01 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class TestApp:public VulkanApplicationFramework
|
|
|
|
|
{
|
|
|
|
|
Camera cam;
|
2020-07-28 18:50:05 +08:00
|
|
|
|
|
|
|
|
|
ObjectList<TileBitmap> tile_list;
|
|
|
|
|
|
|
|
|
|
TileData *tile_data;
|
|
|
|
|
|
|
|
|
|
float *vertex_data=nullptr;
|
|
|
|
|
float *tex_coord_data=nullptr;
|
2020-07-28 16:37:01 +08:00
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
vulkan::Sampler * sampler =nullptr;
|
|
|
|
|
vulkan::MaterialInstance * material_instance =nullptr;
|
|
|
|
|
vulkan::Renderable * render_obj =nullptr;
|
2020-09-28 11:16:45 +08:00
|
|
|
|
vulkan::RenderableInstance *render_instance =nullptr;
|
2020-07-28 16:37:01 +08:00
|
|
|
|
vulkan::Buffer * ubo_world_matrix =nullptr;
|
|
|
|
|
|
|
|
|
|
vulkan::Pipeline * pipeline =nullptr;
|
|
|
|
|
|
2020-07-28 18:50:05 +08:00
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
~TestApp()
|
|
|
|
|
{
|
|
|
|
|
SAFE_CLEAR(tile_data);
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-28 16:37:01 +08:00
|
|
|
|
private:
|
|
|
|
|
|
2020-07-28 18:50:05 +08:00
|
|
|
|
int LoadIcons()
|
|
|
|
|
{
|
|
|
|
|
const OSString icon_path=OS_TEXT("res/image/icon/freepik/");
|
|
|
|
|
|
|
|
|
|
UTF8StringList sl;
|
|
|
|
|
|
|
|
|
|
const int count=LoadStringListFromTextFile(sl,icon_path+OS_TEXT("list.txt"));
|
|
|
|
|
|
|
|
|
|
Bitmap2DLoader loader;
|
|
|
|
|
|
|
|
|
|
int result=0;
|
|
|
|
|
|
|
|
|
|
for(int i=0;i<count;i++)
|
|
|
|
|
{
|
|
|
|
|
if(loader.Load(icon_path+ToOSString(sl[i])+OS_TEXT(".Tex2D")))
|
|
|
|
|
{
|
|
|
|
|
TileBitmap *tb=new TileBitmap;
|
|
|
|
|
|
|
|
|
|
tb->bmp =loader.GetBitmap();
|
|
|
|
|
tb->to =nullptr;
|
|
|
|
|
|
|
|
|
|
tile_list.Add(tb);
|
|
|
|
|
++result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool InitTileTexture()
|
|
|
|
|
{
|
2020-08-07 16:14:29 +08:00
|
|
|
|
tile_data=device->CreateTileData( FMT_BC1_RGBAUN, //纹理格式,因VK不支持实时转换,所以提交的数据格式必须与此一致
|
2020-07-29 11:46:11 +08:00
|
|
|
|
512,512, //TILE大小
|
|
|
|
|
tile_list.GetCount()); //TILE需求数量
|
2020-07-28 18:50:05 +08:00
|
|
|
|
|
|
|
|
|
if(!tile_data)
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
const int count=tile_list.GetCount();
|
|
|
|
|
TileBitmap **tb=tile_list.GetData();
|
|
|
|
|
|
|
|
|
|
vertex_data=new float[count*4];
|
|
|
|
|
tex_coord_data=new float[count*4];
|
|
|
|
|
|
|
|
|
|
float *vp=vertex_data;
|
|
|
|
|
float *tp=tex_coord_data;
|
|
|
|
|
|
|
|
|
|
int col=0;
|
|
|
|
|
int row=0;
|
|
|
|
|
|
2020-09-28 11:16:45 +08:00
|
|
|
|
float size =SCREEN_WIDTH/TILE_COLS;
|
2020-07-28 19:25:23 +08:00
|
|
|
|
float view_size =size-BORDER*2;
|
|
|
|
|
float left =0;
|
|
|
|
|
float top =0;
|
2020-07-28 18:50:05 +08:00
|
|
|
|
|
2020-07-29 17:06:43 +08:00
|
|
|
|
tile_data->BeginCommit();
|
|
|
|
|
|
2020-07-28 18:50:05 +08:00
|
|
|
|
for(int i=0;i<count;i++)
|
|
|
|
|
{
|
2020-09-28 11:16:45 +08:00
|
|
|
|
(*tb)->to=tile_data->Commit((*tb)->bmp); //添加一个tile图片
|
2020-07-28 18:50:05 +08:00
|
|
|
|
|
2020-07-29 11:46:11 +08:00
|
|
|
|
vp=WriteRect(vp,left+BORDER, //产生绘制顶点信息
|
2020-07-28 19:25:23 +08:00
|
|
|
|
top +BORDER,
|
|
|
|
|
view_size,
|
|
|
|
|
view_size);
|
2020-07-28 18:50:05 +08:00
|
|
|
|
|
2020-07-29 11:46:11 +08:00
|
|
|
|
tp=WriteRect(tp,(*tb)->to->uv_float); //产生绘制纹理坐标信息
|
2020-07-28 18:50:05 +08:00
|
|
|
|
|
|
|
|
|
++col;
|
2020-09-28 11:16:45 +08:00
|
|
|
|
if(col==TILE_COLS)
|
2020-07-28 18:50:05 +08:00
|
|
|
|
{
|
2020-07-28 19:25:23 +08:00
|
|
|
|
left=0;
|
|
|
|
|
top+=size;
|
2020-07-28 18:50:05 +08:00
|
|
|
|
++row;
|
|
|
|
|
col=0;
|
|
|
|
|
}
|
2020-07-28 19:25:23 +08:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
left+=size;
|
|
|
|
|
}
|
2020-07-28 18:50:05 +08:00
|
|
|
|
|
|
|
|
|
++tb;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-29 17:06:43 +08:00
|
|
|
|
tile_data->EndCommit();
|
|
|
|
|
|
2020-07-28 18:50:05 +08:00
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-28 16:37:01 +08:00
|
|
|
|
bool InitMaterial()
|
|
|
|
|
{
|
2020-09-28 11:16:45 +08:00
|
|
|
|
material_instance=db->CreateMaterialInstance(OS_TEXT("res/material/TextureRect2D"));
|
|
|
|
|
if(!material_instance)
|
2020-07-28 16:37:01 +08:00
|
|
|
|
return(false);
|
|
|
|
|
|
2020-10-16 17:24:01 +08:00
|
|
|
|
pipeline=CreatePipeline(material_instance,vulkan::InlinePipeline::Solid2D,Prim::Rectangles);
|
2020-07-28 16:37:01 +08:00
|
|
|
|
|
|
|
|
|
sampler=db->CreateSampler();
|
|
|
|
|
|
2020-07-28 18:50:05 +08:00
|
|
|
|
material_instance->BindSampler("tex",tile_data->GetTexture(),sampler);
|
2020-07-28 16:37:01 +08:00
|
|
|
|
material_instance->BindUBO("world",ubo_world_matrix);
|
|
|
|
|
material_instance->Update();
|
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool InitUBO()
|
|
|
|
|
{
|
|
|
|
|
const VkExtent2D extent=sc_render_target->GetExtent();
|
|
|
|
|
|
|
|
|
|
cam.width=extent.width;
|
|
|
|
|
cam.height=extent.height;
|
|
|
|
|
|
|
|
|
|
cam.Refresh();
|
|
|
|
|
|
|
|
|
|
ubo_world_matrix=db->CreateUBO(sizeof(WorldMatrix),&cam.matrix);
|
|
|
|
|
|
|
|
|
|
if(!ubo_world_matrix)
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-28 11:16:45 +08:00
|
|
|
|
bool InitVBO()
|
2020-07-28 16:37:01 +08:00
|
|
|
|
{
|
2020-09-28 11:16:45 +08:00
|
|
|
|
const uint tile_count=tile_list.GetCount();
|
2020-07-28 18:50:05 +08:00
|
|
|
|
|
2020-09-28 11:16:45 +08:00
|
|
|
|
render_obj=db->CreateRenderable(tile_count);
|
|
|
|
|
if(!render_obj)return(false);
|
2020-07-28 16:37:01 +08:00
|
|
|
|
|
2020-09-28 11:16:45 +08:00
|
|
|
|
render_obj->Set(VAN::Position,db->CreateVAB(VAF_VEC4,tile_count,vertex_data));
|
|
|
|
|
render_obj->Set(VAN::TexCoord,db->CreateVAB(VAF_VEC4,tile_count,tex_coord_data));
|
2020-07-28 16:37:01 +08:00
|
|
|
|
|
2020-09-28 11:16:45 +08:00
|
|
|
|
render_instance=db->CreateRenderableInstance(render_obj,material_instance,pipeline);
|
2020-07-28 16:37:01 +08:00
|
|
|
|
|
2020-09-28 11:16:45 +08:00
|
|
|
|
return(render_instance);
|
2020-07-28 16:37:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
bool Init()
|
|
|
|
|
{
|
2020-07-28 18:50:05 +08:00
|
|
|
|
if(LoadIcons()<=0)
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
if(!VulkanApplicationFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT))
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
if(!InitTileTexture())
|
2020-07-28 16:37:01 +08:00
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
if(!InitUBO())
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
if(!InitMaterial())
|
|
|
|
|
return(false);
|
|
|
|
|
|
2020-09-28 11:16:45 +08:00
|
|
|
|
if(!InitVBO())
|
2020-07-28 16:37:01 +08:00
|
|
|
|
return(false);
|
|
|
|
|
|
2020-09-28 11:16:45 +08:00
|
|
|
|
BuildCommandBuffer(render_instance);
|
2020-07-28 16:37:01 +08:00
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Resize(int w,int h)override
|
|
|
|
|
{
|
|
|
|
|
cam.width=w;
|
|
|
|
|
cam.height=h;
|
|
|
|
|
|
|
|
|
|
cam.Refresh();
|
|
|
|
|
|
|
|
|
|
ubo_world_matrix->Write(&cam.matrix);
|
2020-09-28 11:16:45 +08:00
|
|
|
|
|
|
|
|
|
BuildCommandBuffer(render_instance);
|
2020-07-28 16:37:01 +08:00
|
|
|
|
}
|
|
|
|
|
};//class TestApp:public VulkanApplicationFramework
|
|
|
|
|
|
|
|
|
|
int main(int,char **)
|
|
|
|
|
{
|
|
|
|
|
TestApp app;
|
|
|
|
|
|
|
|
|
|
if(!app.Init())
|
|
|
|
|
return(-1);
|
|
|
|
|
|
|
|
|
|
while(app.Run());
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|