upgrade FontSource serial codes
This commit is contained in:
parent
af1510b6fc
commit
0b07d04eb2
@ -1,47 +1,154 @@
|
||||
#include<hgl/type/StrChar.h>
|
||||
#include<hgl/type/Map.h>
|
||||
#include<hgl/graph/font/Font.h>
|
||||
// 2.RectanglePrimivate
|
||||
// 该示例是texture_rect的进化,演示使用GeometryShader画矩形
|
||||
|
||||
#include"VulkanAppFramework.h"
|
||||
#include<hgl/graph/vulkan/VKTexture.h>
|
||||
#include<hgl/graph/vulkan/VKSampler.h>
|
||||
#include<hgl/math/Math.h>
|
||||
#include<hgl/graph/font/TextLayout.h>
|
||||
|
||||
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<u32char,FontBitmapCache::Bitmap *> 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
|
||||
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<vulkan::PipelineCreater>
|
||||
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;
|
||||
}
|
||||
|
@ -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<UnicodeBlock,FontSourcePointer> 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
|
||||
|
@ -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;i<count;i++)
|
||||
if(count>0)
|
||||
{
|
||||
if(IsInUnicodeBlock((*fsp)->left,ch))
|
||||
return (*fsp)->right->GetCharBitmap(ch);
|
||||
auto **fsp=source_map.GetDataList();
|
||||
|
||||
++fsp;
|
||||
for(int i=0;i<count;i++)
|
||||
{
|
||||
if(IsInUnicodeBlock((*fsp)->left,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
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user