From 0b07d04eb2bef3cef557a5ff29bd6a6bd66b0775 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Wed, 8 Jul 2020 21:56:39 +0800 Subject: [PATCH] upgrade FontSource serial codes --- example/Vulkan/DrawText.cpp | 173 +++++++++++++++++++----- inc/hgl/graph/font/FontSource.h | 13 +- src/SceneGraph/font/FontSourceMulti.cpp | 41 ++++-- src/SceneGraph/font/FontSourceWin.h | 1 + 4 files changed, 182 insertions(+), 46 deletions(-) diff --git a/example/Vulkan/DrawText.cpp b/example/Vulkan/DrawText.cpp index 73dc9a81..186a7231 100644 --- a/example/Vulkan/DrawText.cpp +++ b/example/Vulkan/DrawText.cpp @@ -1,47 +1,154 @@ -#include -#include -#include +// 2.RectanglePrimivate +// 该示例是texture_rect的进化,演示使用GeometryShader画矩形 + +#include"VulkanAppFramework.h" +#include +#include +#include +#include using namespace hgl; +using namespace hgl::graph; -namespace hgl +VK_NAMESPACE_BEGIN +Texture2D *CreateTextureFromFile(Device *device,const OSString &filename); +VK_NAMESPACE_END + +constexpr uint32_t SCREEN_SIZE=512; + +class TestApp:public VulkanApplicationFramework { - namespace graph - { - /** - * 文本位图缓冲 - */ - class FontBitmapCache - { - /** - * 字体位图数据 - */ - struct Bitmap - { - int count; //使用次数 + Camera cam; - int x,y; //图像显示偏移 - int w,h; //图像尺寸 +private: - int adv_x,adv_y;//字符尺寸 + TextLayout text_layout; - uint8 *data; - };//struct Bitmap +private: - protected: + vulkan::Material * material =nullptr; + vulkan::Texture2D * texture =nullptr; + vulkan::Sampler * sampler =nullptr; + vulkan::MaterialInstance * material_instance =nullptr; + vulkan::Renderable * render_obj =nullptr; + vulkan::Buffer * ubo_mvp =nullptr; - MapObject chars_bitmap; ///<字符位图 + vulkan::Pipeline * pipeline =nullptr; - protected: + vulkan::VertexBuffer * vertex_buffer =nullptr; + vulkan::VertexBuffer * tex_coord_buffer =nullptr; - virtual bool MakeCharBitmap(u32char)=0; ///<产生字体数据 - virtual int GetLineHeight()const=0; ///<取得行高 +private: - public: + bool InitMaterial() + { + material=shader_manage->CreateMaterial( OS_TEXT("res/shader/DrawRect2D.vert"), + OS_TEXT("res/shader/DrawRect2D.geom"), + OS_TEXT("res/shader/FlatTexture.frag")); + if(!material) + return(false); - FontBitmapCache(const Font &); + render_obj=material->CreateRenderable(VERTEX_COUNT); + material_instance=material->CreateInstance(); - FontBitmapCache::Bitmap *GetCharBitmap(const u32char); ///<取得字符位图数据 - };//class FontBitmapCache - }//namespace graph -}//namespace hgl \ No newline at end of file + texture=vulkan::CreateTextureFromFile(device,OS_TEXT("res/image/lena.Tex2D")); + + sampler=db->CreateSampler(); + + material_instance->BindSampler("tex",texture,sampler); + material_instance->BindUBO("world",ubo_mvp); + material_instance->Update(); + + db->Add(material); + db->Add(material_instance); + db->Add(texture); + db->Add(render_obj); + return(true); + } + + bool InitUBO() + { + const VkExtent2D extent=sc_render_target->GetExtent(); + + cam.width=extent.width; + cam.height=extent.height; + + cam.Refresh(); + + ubo_mvp=db->CreateUBO(sizeof(WorldMatrix),&cam.matrix); + + if(!ubo_mvp) + return(false); + + return(true); + } + + void InitVBO() + { + vertex_buffer =db->CreateVBO(FMT_RGBA32F,VERTEX_COUNT,vertex_data); + tex_coord_buffer=db->CreateVBO(FMT_RGBA32F,VERTEX_COUNT,tex_coord_data); + + render_obj->Set("Vertex",vertex_buffer); + render_obj->Set("TexCoord",tex_coord_buffer); + } + + bool InitPipeline() + { + AutoDelete + pipeline_creater=new vulkan::PipelineCreater(device,material,sc_render_target); + pipeline_creater->CloseCullFace(); + pipeline_creater->Set(PRIM_RECTANGLES); + + pipeline=pipeline_creater->Create(); + + db->Add(pipeline); + return pipeline; + } + +public: + + bool Init() + { + if(!VulkanApplicationFramework::Init(SCREEN_SIZE,SCREEN_SIZE)) + return(false); + + if(!InitUBO()) + return(false); + + if(!InitMaterial()) + return(false); + + InitVBO(); + + if(!InitPipeline()) + return(false); + + BuildCommandBuffer(pipeline,material_instance,render_obj); + + return(true); + } + + void Resize(int w,int h)override + { + cam.width=w; + cam.height=h; + + cam.Refresh(); + + ubo_mvp->Write(&cam.matrix); + + BuildCommandBuffer(pipeline,material_instance,render_obj); + } +};//class TestApp:public VulkanApplicationFramework + +int os_main(int,os_char **) +{ + TestApp app; + + if(!app.Init()) + return(-1); + + while(app.Run()); + + return 0; +} diff --git a/inc/hgl/graph/font/FontSource.h b/inc/hgl/graph/font/FontSource.h index 8587e1b6..9476d29c 100644 --- a/inc/hgl/graph/font/FontSource.h +++ b/inc/hgl/graph/font/FontSource.h @@ -42,6 +42,7 @@ namespace hgl virtual ~FontSource()=default; virtual FontBitmap *GetCharBitmap(const u32char &)=0; ///<取得字符位图数据 + virtual int GetCharAdvWidth(const u32char &)=0; ///<取得字符绘制宽度 void RefAcquire(void *); ///<引用请求 void RefRelease(void *); ///<引用释放 @@ -61,15 +62,16 @@ namespace hgl protected: - virtual bool MakeCharBitmap(FontBitmap *,u32char)=0; ///<产生字体数据 - virtual int GetLineHeight()const=0; ///<取得行高 + virtual bool MakeCharBitmap(FontBitmap *,u32char)=0; ///<产生字符位图数据 public: FontSourceSingle(const Font &f){fnt=f;} virtual ~FontSourceSingle()=default; - FontBitmap *GetCharBitmap(const u32char &ch) override; + FontBitmap * GetCharBitmap(const u32char &ch) override; ///<取得字符位图数据 + virtual int GetCharAdvWidth(const u32char &)=0; ///<取得字符绘制宽度 + virtual int GetLineHeight()const=0; ///<取得行高 };//class FontSourceSingle:public FontSource /** @@ -82,6 +84,10 @@ namespace hgl FontSource *default_source; Map source_map; + protected: + + FontSource *GetFontSource(const u32char &ch); + public: /** @@ -95,6 +101,7 @@ namespace hgl void Remove(FontSource *); FontBitmap *GetCharBitmap(const u32char &ch) override; + int GetCharAdvWidth(const u32char &) override; };//class FontSourceMulti:public FontSource }//namespace graph }//namespace hgl diff --git a/src/SceneGraph/font/FontSourceMulti.cpp b/src/SceneGraph/font/FontSourceMulti.cpp index 32cec1d8..9b81db35 100644 --- a/src/SceneGraph/font/FontSourceMulti.cpp +++ b/src/SceneGraph/font/FontSourceMulti.cpp @@ -50,26 +50,47 @@ namespace hgl source_map.DeleteByValue(fs); } } - - FontBitmap *FontSourceMulti::GetCharBitmap(const u32char &ch) + + FontSource *FontSourceMulti::GetFontSource(const u32char &ch) { if(hgl::isspace(ch))return(nullptr); //不能显示的数据或是空格 const auto count=source_map.GetCount(); - auto **fsp=source_map.GetDataList(); - for(int i=0;i0) { - if(IsInUnicodeBlock((*fsp)->left,ch)) - return (*fsp)->right->GetCharBitmap(ch); + auto **fsp=source_map.GetDataList(); - ++fsp; + for(int i=0;ileft,ch)) + return (*fsp)->right; + + ++fsp; + } } - if(default_source) - return default_source->GetCharBitmap(ch); + return default_source; + } - return nullptr; + FontBitmap *FontSourceMulti::GetCharBitmap(const u32char &ch) + { + FontSource *s=GetFontSource(ch); + + if(!s) + return(nullptr); + + return s->GetCharBitmap(ch); + } + + int FontSourceMulti::GetCharAdvWidth(const u32char &ch) + { + FontSource *s=GetFontSource(ch); + + if(!s) + return(0); + + return s->GetCharAdvWidth(ch); } }//namespace graph }//namespace hgl diff --git a/src/SceneGraph/font/FontSourceWin.h b/src/SceneGraph/font/FontSourceWin.h index 2c511526..51d77840 100644 --- a/src/SceneGraph/font/FontSourceWin.h +++ b/src/SceneGraph/font/FontSourceWin.h @@ -33,6 +33,7 @@ namespace hgl bool MakeCharBitmap(FontBitmap *,u32char) override; ///<产生字体数据 int GetLineHeight()const override{return LineHeight;} ///<取得行高 + int GetCharWidth(const u32char &) override; };//class WinBitmapFont }//namespace graph }//namespace hgl