ULRE/example/Vulkan/DrawText.cpp

117 lines
2.3 KiB
C++
Raw Normal View History

2020-10-14 21:05:12 +08:00
#include<hgl/type/StringList.h>
2022-02-16 23:26:50 +08:00
#include<hgl/graph/font/TextRender.h>
2020-07-08 21:56:39 +08:00
#include"VulkanAppFramework.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
2022-02-16 18:19:41 +08:00
class TestApp:public VulkanApplicationFramework
{
Camera cam;
2022-02-16 18:19:41 +08:00
private:
GPUBuffer * ubo_camera_info =nullptr;
private:
TextRender * text_render =nullptr;
TextRenderable * text_render_obj =nullptr;
RenderableInstance *render_instance =nullptr;
public:
~TestApp()
{
SAFE_CLEAR(text_render)
}
2022-02-16 18:19:41 +08:00
private:
bool InitUBO()
{
2022-02-16 18:19:41 +08:00
const VkExtent2D extent=sc_render_target->GetExtent();
2022-02-16 18:19:41 +08:00
cam.width=extent.width;
cam.height=extent.height;
2022-02-16 18:19:41 +08:00
cam.RefreshCameraInfo();
2022-02-16 18:19:41 +08:00
ubo_camera_info=db->CreateUBO(sizeof(CameraInfo),&cam.info);
2022-02-16 18:19:41 +08:00
if(!ubo_camera_info)
return(false);
return(true);
}
2020-08-23 19:43:50 +08:00
bool InitTextRenderable()
{
UTF16String str;
2022-02-17 11:02:55 +08:00
FontSource *fs=CreateFontSource(OS_TEXT("微软雅黑"),12);
text_render=CreateTextRender(device,fs,device_render_pass,ubo_camera_info);
if(!text_render)
return(false);
2021-09-27 12:20:44 +08:00
LoadStringFromTextFile(str,OS_TEXT("res/text/DaoDeBible.txt"));
2022-02-17 11:02:55 +08:00
text_render_obj=text_render->CreateRenderable(str);
2022-02-16 18:19:41 +08:00
if(!text_render_obj)
return(false);
2020-09-28 11:16:45 +08:00
2022-02-17 11:02:55 +08:00
render_instance=text_render->CreateRenderableInstance(text_render_obj);
2022-02-16 18:19:41 +08:00
if(!render_instance)
return(false);
2022-02-16 18:19:41 +08:00
return(true);
}
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-07-08 21:56:39 +08:00
if(!InitUBO())
return(false);
if(!InitTextRenderable())
return(false);
2022-02-16 18:19:41 +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;
2022-02-15 15:02:53 +08:00
cam.RefreshCameraInfo();
2020-07-08 21:56:39 +08:00
ubo_camera_info->Write(&cam.info);
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;
}