2020-10-14 21:05:12 +08:00
|
|
|
|
#include<hgl/type/StringList.h>
|
2020-07-31 18:01:28 +08:00
|
|
|
|
#include<hgl/graph/TextureLoader.h>
|
|
|
|
|
#include<hgl/graph/TileData.h>
|
|
|
|
|
#include<hgl/graph/font/TileFont.h>
|
|
|
|
|
#include<hgl/graph/font/TextLayout.h>
|
2020-08-08 20:12:37 +08:00
|
|
|
|
#include<hgl/graph/font/TextRenderable.h>
|
2020-07-31 18:01:28 +08:00
|
|
|
|
|
2020-07-08 21:56:39 +08:00
|
|
|
|
#include"VulkanAppFramework.h"
|
2020-10-21 11:43:18 +08:00
|
|
|
|
#include<hgl/graph/VKTexture.h>
|
|
|
|
|
#include<hgl/graph/VKSampler.h>
|
2020-07-08 21:56:39 +08:00
|
|
|
|
#include<hgl/math/Math.h>
|
2020-06-28 22:16:07 +08:00
|
|
|
|
|
|
|
|
|
using namespace hgl;
|
2020-07-08 21:56:39 +08:00
|
|
|
|
using namespace hgl::graph;
|
|
|
|
|
|
2020-07-31 18:01:28 +08:00
|
|
|
|
constexpr uint32_t SCREEN_WIDTH =1280;
|
2020-08-04 18:28:34 +08:00
|
|
|
|
constexpr uint32_t SCREEN_HEIGHT=SCREEN_WIDTH/16*9;
|
2020-07-08 21:56:39 +08:00
|
|
|
|
|
2020-08-10 18:20:34 +08:00
|
|
|
|
constexpr uint CHAR_BITMAP_SIZE=12; //字符尺寸
|
2020-06-28 22:16:07 +08:00
|
|
|
|
|
2020-07-08 21:56:39 +08:00
|
|
|
|
class TestApp:public VulkanApplicationFramework
|
2020-06-28 22:16:07 +08:00
|
|
|
|
{
|
2020-07-08 21:56:39 +08:00
|
|
|
|
Camera cam;
|
2020-08-04 01:27:35 +08:00
|
|
|
|
|
|
|
|
|
Color4f color;
|
2020-07-31 18:01:28 +08:00
|
|
|
|
|
2020-07-08 21:56:39 +08:00
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
vulkan::Sampler * sampler =nullptr;
|
|
|
|
|
vulkan::MaterialInstance * material_instance =nullptr;
|
2020-07-16 17:02:24 +08:00
|
|
|
|
vulkan::Buffer * ubo_world_matrix =nullptr;
|
2020-08-04 01:27:35 +08:00
|
|
|
|
vulkan::Buffer * ubo_color =nullptr;
|
2020-07-08 21:56:39 +08:00
|
|
|
|
|
|
|
|
|
vulkan::Pipeline * pipeline =nullptr;
|
|
|
|
|
|
2020-07-31 18:01:28 +08:00
|
|
|
|
private:
|
|
|
|
|
|
2020-08-10 18:20:34 +08:00
|
|
|
|
FontSource * eng_fs =nullptr;
|
|
|
|
|
FontSource * chs_fs =nullptr;
|
|
|
|
|
FontSourceMulti * font_source =nullptr;
|
2020-08-04 18:28:34 +08:00
|
|
|
|
|
2020-08-08 20:12:37 +08:00
|
|
|
|
TileFont * tile_font =nullptr;
|
2020-07-31 18:01:28 +08:00
|
|
|
|
TextLayout tl_engine; ///<文本排版引擎
|
|
|
|
|
|
2020-08-08 20:12:37 +08:00
|
|
|
|
TextRenderable * text_render_obj =nullptr;
|
2020-09-28 11:16:45 +08:00
|
|
|
|
RenderableInstance * render_instance =nullptr;
|
2020-07-31 18:01:28 +08:00
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
~TestApp()
|
|
|
|
|
{
|
|
|
|
|
SAFE_CLEAR(tile_font);
|
|
|
|
|
}
|
2020-07-08 21:56:39 +08:00
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
bool InitMaterial()
|
|
|
|
|
{
|
2020-09-28 11:16:45 +08:00
|
|
|
|
material_instance=db->CreateMaterialInstance(OS_TEXT("res/material/LumTextureRect2D"));
|
|
|
|
|
if(!material_instance)return(false);
|
2020-07-08 21:56:39 +08:00
|
|
|
|
|
2020-10-16 17:24:01 +08:00
|
|
|
|
pipeline=CreatePipeline(material_instance,vulkan::InlinePipeline::Solid2D,Prim::Rectangles);
|
2020-09-28 11:16:45 +08:00
|
|
|
|
if(!pipeline)return(false);
|
2020-07-08 21:56:39 +08:00
|
|
|
|
|
|
|
|
|
sampler=db->CreateSampler();
|
|
|
|
|
|
2020-09-28 11:16:45 +08:00
|
|
|
|
material_instance->BindSampler("lum_texture",tile_font->GetTexture(),sampler);
|
2020-07-14 20:42:01 +08:00
|
|
|
|
material_instance->BindUBO("world",ubo_world_matrix);
|
2020-08-04 01:27:35 +08:00
|
|
|
|
material_instance->BindUBO("color_material",ubo_color);
|
2020-07-08 21:56:39 +08:00
|
|
|
|
material_instance->Update();
|
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool InitUBO()
|
|
|
|
|
{
|
|
|
|
|
const VkExtent2D extent=sc_render_target->GetExtent();
|
2020-06-28 22:16:07 +08:00
|
|
|
|
|
2020-07-08 21:56:39 +08:00
|
|
|
|
cam.width=extent.width;
|
|
|
|
|
cam.height=extent.height;
|
2020-06-28 22:16:07 +08:00
|
|
|
|
|
2020-07-08 21:56:39 +08:00
|
|
|
|
cam.Refresh();
|
2020-06-28 22:16:07 +08:00
|
|
|
|
|
2020-07-14 20:42:01 +08:00
|
|
|
|
ubo_world_matrix=db->CreateUBO(sizeof(WorldMatrix),&cam.matrix);
|
2020-06-28 22:16:07 +08:00
|
|
|
|
|
2020-07-14 20:42:01 +08:00
|
|
|
|
if(!ubo_world_matrix)
|
2020-07-08 21:56:39 +08:00
|
|
|
|
return(false);
|
2020-06-28 22:16:07 +08:00
|
|
|
|
|
2020-08-04 01:27:35 +08:00
|
|
|
|
color.One();
|
|
|
|
|
|
|
|
|
|
ubo_color=db->CreateUBO(sizeof(Color4f),&color);
|
|
|
|
|
|
|
|
|
|
if(!ubo_color)
|
|
|
|
|
return(false);
|
|
|
|
|
|
2020-07-08 21:56:39 +08:00
|
|
|
|
return(true);
|
|
|
|
|
}
|
2020-06-28 22:16:07 +08:00
|
|
|
|
|
2020-08-08 22:54:09 +08:00
|
|
|
|
bool InitTileFont()
|
|
|
|
|
{
|
2020-08-10 18:20:34 +08:00
|
|
|
|
Font eng_fnt(OS_TEXT("Source Code Pro"),0,CHAR_BITMAP_SIZE);
|
2020-08-08 22:54:09 +08:00
|
|
|
|
Font chs_fnt(OS_TEXT("微软雅黑"),0,CHAR_BITMAP_SIZE);
|
|
|
|
|
|
2020-08-10 18:20:34 +08:00
|
|
|
|
eng_fs=AcquireFontSource(eng_fnt);
|
|
|
|
|
chs_fs=AcquireFontSource(chs_fnt);
|
|
|
|
|
|
|
|
|
|
font_source=new FontSourceMulti(eng_fs);
|
|
|
|
|
font_source->AddCJK(chs_fs);
|
2020-08-08 22:54:09 +08:00
|
|
|
|
|
|
|
|
|
tile_font=device->CreateTileFont(font_source);
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool InitTextLayoutEngine()
|
2020-08-03 21:22:02 +08:00
|
|
|
|
{
|
|
|
|
|
CharLayoutAttr cla;
|
|
|
|
|
TextLayoutAttributes tla;
|
|
|
|
|
|
|
|
|
|
cla.CharColor=Color4f(COLOR::White);
|
|
|
|
|
cla.BackgroundColor=Color4f(COLOR::Black);
|
|
|
|
|
|
|
|
|
|
tla.char_layout_attr=&cla;
|
2020-08-10 18:20:34 +08:00
|
|
|
|
tla.line_gap=0.1f;
|
2020-08-03 21:22:02 +08:00
|
|
|
|
|
2020-09-11 17:11:32 +08:00
|
|
|
|
tl_engine.SetFont(tile_font->GetFontSource());
|
|
|
|
|
tl_engine.SetTLA(&tla);
|
2020-08-03 21:22:02 +08:00
|
|
|
|
tl_engine.SetTextDirection(0);
|
2020-09-11 17:11:32 +08:00
|
|
|
|
tl_engine.SetAlign(TextAlign::Left);
|
2020-08-03 21:22:02 +08:00
|
|
|
|
tl_engine.SetMaxWidth(0);
|
|
|
|
|
tl_engine.SetMaxHeight(0);
|
|
|
|
|
|
2020-08-08 22:54:09 +08:00
|
|
|
|
return tl_engine.Init();
|
|
|
|
|
}
|
2020-08-23 19:43:50 +08:00
|
|
|
|
|
2020-08-08 22:54:09 +08:00
|
|
|
|
bool InitTextRenderable()
|
|
|
|
|
{
|
2020-08-04 02:23:22 +08:00
|
|
|
|
UTF16String str;
|
2020-08-08 22:54:09 +08:00
|
|
|
|
|
2020-08-10 18:20:34 +08:00
|
|
|
|
LoadStringFromTextFile(str,OS_TEXT("README.md"));
|
2020-08-03 21:22:02 +08:00
|
|
|
|
|
2020-09-28 11:16:45 +08:00
|
|
|
|
text_render_obj=db->CreateTextRenderable(material_instance->GetMaterial());
|
2020-10-14 21:05:12 +08:00
|
|
|
|
|
2020-09-28 11:16:45 +08:00
|
|
|
|
if(tl_engine.SimpleLayout(text_render_obj,tile_font,str)<=0)return(false);
|
|
|
|
|
|
|
|
|
|
render_instance=db->CreateRenderableInstance(text_render_obj,material_instance,pipeline);
|
2020-08-08 22:54:09 +08:00
|
|
|
|
|
2020-09-28 11:16:45 +08:00
|
|
|
|
return(render_instance);
|
2020-08-03 21:22:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-08 21:56:39 +08:00
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
bool Init()
|
|
|
|
|
{
|
2020-07-31 18:01:28 +08:00
|
|
|
|
if(!VulkanApplicationFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT))
|
|
|
|
|
return(false);
|
2020-08-10 18:20:34 +08:00
|
|
|
|
|
|
|
|
|
if(!InitTileFont())
|
|
|
|
|
return(false);
|
2020-07-31 18:01:28 +08:00
|
|
|
|
|
2020-07-08 21:56:39 +08:00
|
|
|
|
if(!InitUBO())
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
if(!InitMaterial())
|
|
|
|
|
return(false);
|
|
|
|
|
|
2020-08-08 22:54:09 +08:00
|
|
|
|
if(!InitTextLayoutEngine())
|
|
|
|
|
return(false);
|
|
|
|
|
|
2020-08-03 21:22:02 +08:00
|
|
|
|
if(!InitTextRenderable())
|
|
|
|
|
return(false);
|
2020-07-08 21:56:39 +08:00
|
|
|
|
|
2020-09-28 11:16:45 +08:00
|
|
|
|
BuildCommandBuffer(render_instance);
|
2020-07-08 21:56:39 +08:00
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Resize(int w,int h)override
|
|
|
|
|
{
|
|
|
|
|
cam.width=w;
|
|
|
|
|
cam.height=h;
|
|
|
|
|
|
|
|
|
|
cam.Refresh();
|
|
|
|
|
|
2020-07-14 20:42:01 +08:00
|
|
|
|
ubo_world_matrix->Write(&cam.matrix);
|
2020-09-28 11:16:45 +08:00
|
|
|
|
|
|
|
|
|
BuildCommandBuffer(render_instance);
|
2020-07-08 21:56:39 +08:00
|
|
|
|
}
|
|
|
|
|
};//class TestApp:public VulkanApplicationFramework
|
|
|
|
|
|
2020-07-31 18:01:28 +08:00
|
|
|
|
int main(int,char **)
|
2020-07-08 21:56:39 +08:00
|
|
|
|
{
|
|
|
|
|
TestApp app;
|
2020-06-28 22:16:07 +08:00
|
|
|
|
|
2020-07-08 21:56:39 +08:00
|
|
|
|
if(!app.Init())
|
|
|
|
|
return(-1);
|
2020-06-28 22:16:07 +08:00
|
|
|
|
|
2020-07-08 21:56:39 +08:00
|
|
|
|
while(app.Run());
|
2020-06-28 22:16:07 +08:00
|
|
|
|
|
2020-07-08 21:56:39 +08:00
|
|
|
|
return 0;
|
|
|
|
|
}
|