fix draw text example.

This commit is contained in:
hyzboy 2020-08-04 01:27:35 +08:00
parent 346d8b3279
commit cea2f8da9e
13 changed files with 116 additions and 87 deletions

2
CMCore

@ -1 +1 @@
Subproject commit 2d6883be633abae7f0f10810e2b61367175bcfc4 Subproject commit c2c7c42cd43759a9a613b6ad2c890a2e1d948b37

View File

@ -1,4 +1,4 @@
// DrawTile // DrawTile
// 该示例使用TileData演示多个tile图片在一张纹理上 // 该示例使用TileData演示多个tile图片在一张纹理上
#include<hgl/type/StringList.h> #include<hgl/type/StringList.h>
@ -15,13 +15,6 @@
using namespace hgl; using namespace hgl;
using namespace hgl::graph; using namespace hgl::graph;
/**
*
*
* 1.TextLayout模块排版所有的字符FontSource获取所有字符的Bitmapvertex position数据
* 2.TextLayout向TileData提交需要渲染的所有字符的BitmapBitmap对应的UV数据uv数据
*/
constexpr uint32_t SCREEN_WIDTH =1280; constexpr uint32_t SCREEN_WIDTH =1280;
constexpr uint32_t SCREEN_HEIGHT=960; constexpr uint32_t SCREEN_HEIGHT=960;
@ -31,6 +24,8 @@ constexpr uint CHAR_BITMAP_BORDER=1; //边界象素尺寸
class TestApp:public VulkanApplicationFramework class TestApp:public VulkanApplicationFramework
{ {
Camera cam; Camera cam;
Color4f color;
private: private:
@ -39,6 +34,7 @@ private:
vulkan::MaterialInstance * material_instance =nullptr; vulkan::MaterialInstance * material_instance =nullptr;
vulkan::Renderable * render_obj =nullptr; vulkan::Renderable * render_obj =nullptr;
vulkan::Buffer * ubo_world_matrix =nullptr; vulkan::Buffer * ubo_world_matrix =nullptr;
vulkan::Buffer * ubo_color =nullptr;
vulkan::Pipeline * pipeline =nullptr; vulkan::Pipeline * pipeline =nullptr;
@ -71,7 +67,7 @@ private:
{ {
material=shader_manage->CreateMaterial( OS_TEXT("res/shader/DrawRect2D.vert"), material=shader_manage->CreateMaterial( OS_TEXT("res/shader/DrawRect2D.vert"),
OS_TEXT("res/shader/DrawRect2D.geom"), OS_TEXT("res/shader/DrawRect2D.geom"),
OS_TEXT("res/shader/FlatTexture.frag")); OS_TEXT("res/shader/FlatLumTexture.frag"));
if(!material) if(!material)
return(false); return(false);
@ -81,6 +77,7 @@ private:
material_instance->BindSampler("tex",tile_font->GetTexture(),sampler); material_instance->BindSampler("tex",tile_font->GetTexture(),sampler);
material_instance->BindUBO("world",ubo_world_matrix); material_instance->BindUBO("world",ubo_world_matrix);
material_instance->BindUBO("color_material",ubo_color);
material_instance->Update(); material_instance->Update();
db->Add(material); db->Add(material);
@ -102,6 +99,13 @@ private:
if(!ubo_world_matrix) if(!ubo_world_matrix)
return(false); return(false);
color.One();
ubo_color=db->CreateUBO(sizeof(Color4f),&color);
if(!ubo_color)
return(false);
return(true); return(true);
} }
@ -130,6 +134,7 @@ private:
text_rc=new RenderableCreater(db,material); text_rc=new RenderableCreater(db,material);
tl_engine.Set(tile_font->GetFontSource());
tl_engine.Set(text_rc); tl_engine.Set(text_rc);
tl_engine.Set(&tla); tl_engine.Set(&tla);
tl_engine.SetTextDirection(0); tl_engine.SetTextDirection(0);

View File

@ -1,4 +1,4 @@
// DrawTile // DrawTile
// 该示例使用TileData演示多个tile图片在一张纹理上 // 该示例使用TileData演示多个tile图片在一张纹理上
#include<hgl/type/StringList.h> #include<hgl/type/StringList.h>

View File

@ -1,4 +1,4 @@
// 2.RectanglePrimivate // 2.RectanglePrimivate
// 该示例是texture_rect的进化演示使用GeometryShader画矩形 // 该示例是texture_rect的进化演示使用GeometryShader画矩形
#include"VulkanAppFramework.h" #include"VulkanAppFramework.h"

View File

@ -3,6 +3,7 @@
#include<hgl/graph/SceneDB.h> #include<hgl/graph/SceneDB.h>
#include<hgl/graph/VertexAttribDataAccess.h> #include<hgl/graph/VertexAttribDataAccess.h>
#include<hgl/graph/vulkan/VKShaderModule.h>
namespace hgl namespace hgl
{ {
namespace graph namespace graph

View File

@ -79,7 +79,7 @@ namespace hgl
virtual ~FontSource()=default; virtual ~FontSource()=default;
virtual FontBitmap *GetCharBitmap (const u32char &)=0; ///<取得字符位图数据 virtual FontBitmap *GetCharBitmap (const u32char &)=0; ///<取得字符位图数据
virtual const bool GetCharMetrics (CharMetricsInfo &,const u32char &); ///<取得字符绘制信息 virtual const bool GetCharMetrics (CharMetricsInfo &,const u32char &)=0; ///<取得字符绘制信息
const CLA * GetCLA (const u32char &); ///<取得字符排版信息 const CLA * GetCLA (const u32char &); ///<取得字符排版信息
virtual int GetCharHeight ()const=0; ///<取得字符高度 virtual int GetCharHeight ()const=0; ///<取得字符高度
@ -109,11 +109,11 @@ namespace hgl
virtual ~FontSourceSingle()=default; virtual ~FontSourceSingle()=default;
FontBitmap *GetCharBitmap (const u32char &ch) override; ///<取得字符位图数据 FontBitmap *GetCharBitmap (const u32char &ch) override; ///<取得字符位图数据
const bool GetCharMetrics (CharMetricsInfo &,const u32char &); ///<取得字符绘制信息 const bool GetCharMetrics (CharMetricsInfo &,const u32char &)override;///<取得字符绘制信息
virtual int GetCharHeight ()const override{return fnt.height;} ///<取得字符高度 virtual int GetCharHeight ()const override{return fnt.height;} ///<取得字符高度
};//class FontSourceSingle:public FontSource };//class FontSourceSingle:public FontSource
FontSourceSingle *CreateFontSource(const Font &f); FontSource *CreateFontSource(const Font &f);
/** /**
* *
@ -148,7 +148,7 @@ namespace hgl
public: public:
FontBitmap *GetCharBitmap (const u32char &ch) override; FontBitmap *GetCharBitmap (const u32char &ch) override;
const bool GetCharMetrics (CharMetricsInfo &,const u32char &); ///<取得字符绘制信息 const bool GetCharMetrics (CharMetricsInfo &,const u32char &)override; ///<取得字符绘制信息
int GetCharHeight ()const override; ///<取得字符高度 int GetCharHeight ()const override; ///<取得字符高度
};//class FontSourceMulti:public FontSource };//class FontSourceMulti:public FontSource
}//namespace graph }//namespace graph

View File

@ -85,6 +85,7 @@ namespace hgl
{ {
protected: protected:
FontSource *font_source;
RenderableCreater *rc; RenderableCreater *rc;
TextLayoutAttributes tla; TextLayoutAttributes tla;
@ -102,13 +103,13 @@ namespace hgl
struct CharDrawAttr struct CharDrawAttr
{ {
CLA *cla; const CLA *cla;
TileUVFloat uv; TileUVFloat uv;
}; };
ObjectList<CharDrawAttr> draw_chars_list; ///<所有字符属性列表 ObjectList<CharDrawAttr> draw_chars_list; ///<所有字符属性列表
template<typename T> int preprocess(const T *,const int); template<typename T> bool preprocess(TileFont *,const T *,const int);
protected: protected:
@ -120,6 +121,9 @@ namespace hgl
int sl_v_r2l(); int sl_v_r2l();
int sl_v_l2r(); int sl_v_l2r();
template<typename T>
int SimpleLayout (TileFont *,const BaseString<T> &); ///<简易排版
protected: protected:
TEXT_COORD_TYPE x,y; TEXT_COORD_TYPE x,y;
@ -134,8 +138,8 @@ namespace hgl
protected: protected:
AutoDelete<VB4f> vertex, VB4f *vertex;
tex_coord; VB4f *tex_coord;
public: public:
@ -154,7 +158,7 @@ namespace hgl
void Set (RenderableCreater *_rc) {if(_rc)rc=_rc;} void Set (RenderableCreater *_rc) {if(_rc)rc=_rc;}
void Set (const TextLayoutAttributes *_tla) {if(_tla)memcpy(&tla,_tla,sizeof(TextLayoutAttributes));} void Set (const TextLayoutAttributes *_tla) {if(_tla)memcpy(&tla,_tla,sizeof(TextLayoutAttributes));}
void Set (FontSource *fs) {if(fs)tla.font_source=fs;} void Set (FontSource *fs) {if(fs)font_source=fs;}
void SetTextDirection (const uint8 &td) {tla.text_direction=td;} void SetTextDirection (const uint8 &td) {tla.text_direction=td;}
void Set (const TextAlign &ta) {tla.align=ta;} void Set (const TextAlign &ta) {tla.align=ta;}
void SetMaxWidth (const float mw) {tla.max_width=mw;} void SetMaxWidth (const float mw) {tla.max_width=mw;}
@ -162,10 +166,8 @@ namespace hgl
virtual bool Init (); ///<初始化排版 virtual bool Init (); ///<初始化排版
// virtual int Layout (const int max_chars,const BaseString<T> &)=0; ///<排版 int SimpleLayout (TileFont *,const UTF16String &); ///<简易排版
int SimpleLayout (TileFont *,const UTF32String &); ///<简易排版
template<typename T>
int SimpleLayout (TileFont *,const BaseString<T> &); ///<简易排版
};//class TextLayout };//class TextLayout
}//namespace graph }//namespace graph
}//namespace hgl }//namespace hgl

View File

@ -72,8 +72,8 @@ public:
* binding并非是shader中的binding/locationvkCmdBindVertexBuffer的缓冲区序列号 * binding并非是shader中的binding/locationvkCmdBindVertexBuffer的缓冲区序列号
*/ */
const int GetStageInputBinding(const AnsiString &name)const{return shader_resource->GetStageInputBinding(name);} const int GetStageInputBinding(const AnsiString &name)const{return shader_resource->GetStageInputBinding(name);}
const ShaderStage * GetStageInput(const AnsiString &name)const{return shader_resource->GetStageInput(name);} const ShaderStage * GetStageInput (const AnsiString &name)const{return shader_resource->GetStageInput(name);}
const uint GetStageInputCount()const{return shader_resource->GetStageInputCount();} const uint GetStageInputCount () const{return shader_resource->GetStageInputCount();}
const uint32_t GetAttrCount()const{return attr_count;} const uint32_t GetAttrCount()const{return attr_count;}

View File

@ -426,7 +426,7 @@ Device *CreateRenderDevice(VkInstance inst,const PhysicalDevice *physical_device
if(!device_attr->pipeline_cache) if(!device_attr->pipeline_cache)
return(nullptr); return(nullptr);
auto_delete.Clear(); auto_delete.Discard();
return(new Device(device_attr)); return(new Device(device_attr));
} }

View File

@ -1,4 +1,4 @@
#include<hgl/graph/font/TileFont.h> #include<hgl/graph/font/TileFont.h>
#include<hgl/graph/vulkan/VKDevice.h> #include<hgl/graph/vulkan/VKDevice.h>
namespace hgl namespace hgl

View File

@ -1,4 +1,4 @@
#include<hgl/graph/font/FontSource.h> #include<hgl/graph/font/FontSource.h>
namespace hgl namespace hgl
{ {

View File

@ -6,43 +6,48 @@ namespace hgl
{ {
namespace namespace
{ {
void Convert8BitGrey(uint8 *dst,uint8 *src,int w,int h,int line_bytes) void Convert8BitGrey(uint8 *dst,int dst_w,uint8 *src,int src_w,int src_h,int src_line_bytes)
{ {
int pos; int pos;
uint8 *sp=src,*p; uint8 *sp=src,*p;
uint8 *tp;
while(h--) while(src_h--)
{ {
pos=w; pos=src_w;
p=sp; p=sp;
tp=dst;
while(pos--) while(pos--)
{ {
if(*p==64)*dst=255; if(*p==64)*tp=255;
else *dst=(*p)<<2; else *tp=(*p)<<2;
dst++; tp++;
p++; p++;
} }
sp+=line_bytes; sp+=src_line_bytes;
dst+=dst_w;
} }
} }
void ConvertBitmap(uint8 *dst,uint8 *src,int w,int h,int line_bytes) void ConvertBitmap(uint8 *dst,int dst_w,uint8 *src,int src_w,int src_h,int src_line_bytes)
{ {
uint8 *sp=src,*p; uint8 *sp=src,*p;
uint8 *tp;
uint8 bit; uint8 bit;
while(h--) while(src_h--)
{ {
p=sp; p=sp;
tp=dst;
bit=1<<7; bit=1<<7;
for(int i=0;i<w;i++) for(int i=0;i<src_w;i++)
{ {
*dst++=(*p&bit)?255:0; *tp++=(*p&bit)?255:0;
if(bit==0x01) if(bit==0x01)
{ {
@ -52,7 +57,8 @@ namespace hgl
else bit>>=1; else bit>>=1;
} }
sp+=line_bytes; sp+=src_line_bytes;
dst+=dst_w;
}//while(h--) }//while(h--)
} }
}//namespace }//namespace
@ -120,8 +126,8 @@ namespace hgl
GetGlyphOutlineW(hdc,ch,ggo,&gm,buffer_size,buffer,&mat); GetGlyphOutlineW(hdc,ch,ggo,&gm,buffer_size,buffer,&mat);
bmp->metrics_info.w =gm.gmBlackBoxX; bmp->metrics_info.w =((gm.gmBlackBoxX+3)>>2)<<2;
bmp->metrics_info.h =gm.gmBlackBoxY; bmp->metrics_info.h =((gm.gmBlackBoxY+3)>>2)<<2;
bmp->metrics_info.x =gm.gmptGlyphOrigin.x; bmp->metrics_info.x =gm.gmptGlyphOrigin.x;
bmp->metrics_info.y =gm.gmptGlyphOrigin.y; bmp->metrics_info.y =gm.gmptGlyphOrigin.y;
@ -132,14 +138,14 @@ namespace hgl
bmp->data=new uint8[bmp->metrics_info.w*bmp->metrics_info.h]; bmp->data=new uint8[bmp->metrics_info.w*bmp->metrics_info.h];
if(ggo==GGO_GRAY8_BITMAP) if(ggo==GGO_GRAY8_BITMAP)
Convert8BitGrey(bmp->data,buffer,gm.gmBlackBoxX,gm.gmBlackBoxY,size/bmp->metrics_info.h); Convert8BitGrey(bmp->data,bmp->metrics_info.w,buffer,gm.gmBlackBoxX,gm.gmBlackBoxY,size/gm.gmBlackBoxY);
else else
ConvertBitmap(bmp->data,buffer,gm.gmBlackBoxX,gm.gmBlackBoxY,size/bmp->metrics_info.h); ConvertBitmap(bmp->data,bmp->metrics_info.w,buffer,gm.gmBlackBoxX,gm.gmBlackBoxY,size/gm.gmBlackBoxY);
return(true); return(true);
} }
FontSourceSingle *CreateFontSource(const Font &f) FontSource *CreateFontSource(const Font &f)
{ {
return(new WinBitmapFont(f)); return(new WinBitmapFont(f));
} }

View File

@ -1,6 +1,6 @@
#include<hgl/graph/font/TextLayout.h> #include<hgl/graph/font/TextLayout.h>
#include<hgl/graph/font/TileFont.h>
#include<hgl/type/Extent.h> #include<hgl/type/Extent.h>
#include<hgl/type/UnicodeBlocks.h>
namespace hgl namespace hgl
{ {
@ -9,8 +9,8 @@ namespace hgl
bool TextLayout::Init() bool TextLayout::Init()
{ {
if(!rc if(!rc
||!tla.font_source ||(!tla.font_source&&!font_source)
||!tla.char_attributes) ||!tla.char_layout_attr)
return(false); return(false);
direction.text_direction=tla.text_direction; direction.text_direction=tla.text_direction;
@ -26,7 +26,10 @@ namespace hgl
splite_line_max_limit = tla.max_height; splite_line_max_limit = tla.max_height;
} }
const float origin_char_height=tla.font_source->GetCharHeight(); if(!font_source)
font_source=tla.font_source;
const float origin_char_height=font_source->GetCharHeight();
x=y=0; x=y=0;
char_height =ceil(origin_char_height); char_height =ceil(origin_char_height);
@ -45,36 +48,42 @@ namespace hgl
* *
*/ */
template<typename T> template<typename T>
int TextLayout::preprocess(const T *str,const int str_length) bool TextLayout::preprocess(TileFont *tile_font,const T *str,const int str_length)
{ {
if(!str||!*str||str_length<=0) if(!tile_font
return 0; ||!str||!*str||str_length<=0
||!font_source)
draw_chars_count=0; return(false);
alone_chars.ClearData();
draw_chars_list.ClearData(); //遍历所有字符,取得每一个字符的基本绘制信息
draw_chars_list.SetCount(str_length);
CharDrawAttr **cda=draw_chars_list.GetData();
const T *cp=str;
for(int i=0;i<count;i++)
{ {
(*cda)->cla=tla->font_source.GetCLA(*cp); draw_chars_count=0;
alone_chars.ClearData();
draw_chars_list.ClearData();
if((*cda)->cla->visible) const T *cp=str;
CharDrawAttr *cda;
for(int i=0;i<str_length;i++)
{ {
alone_chars.Add(*cp); //统计所有不重复字符 cda=new CharDrawAttr;
++draw_chars_count;
}
++cp; cda->cla=font_source->GetCLA(*cp);
++cda;
if(cda->cla->visible)
{
alone_chars.Add(*cp); //统计所有不重复字符
++draw_chars_count;
}
draw_chars_list.Add(cda);
++cp;
}
} }
//注册不重复字符给tile font系统获取所有字符的UV //注册不重复字符给tile font系统获取所有字符的UV
if(!tf->Registry(alone_chars_uv,alone_chars.GetData(),alone_chars.GetCount())) if(!tile_font->Registry(alone_chars_uv,alone_chars.GetData(),alone_chars.GetCount()))
{ {
draw_chars_list.ClearData(); draw_chars_list.ClearData();
alone_chars.ClearData(); alone_chars.ClearData();
@ -83,15 +92,18 @@ namespace hgl
} }
//为可绘制字符列表中的字符获取UV //为可绘制字符列表中的字符获取UV
cda=draw_chars_list.GetData();
for(int i=0;i<count;i++)
{ {
alone_chars_uv.Get((*cda)->cla->ch,(*cda)->uv); CharDrawAttr **cda=draw_chars_list.GetData();
++cda; for(int i=0;i<str_length;i++)
{
alone_chars_uv.Get((*cda)->cla->ch,(*cda)->uv);
++cda;
}
} }
return count; return(true);
} }
/** /**
@ -161,8 +173,8 @@ namespace hgl
int cur_size=0; int cur_size=0;
int left=0,top=0; int left=0,top=0;
float *tp=vertex->Begin(); float *tp=vertex->Get();
float *tcp=tex_coord->Begin(); float *tcp=tex_coord->Get();
for(int i=0;i<count;i++) for(int i=0;i<count;i++)
{ {
@ -204,14 +216,14 @@ namespace hgl
if(!tf||str.IsEmpty()) if(!tf||str.IsEmpty())
return(-1); return(-1);
if(preprocess<T>(str.c_str(),str.Length())<=0) if(!preprocess<T>(tf,str.c_str(),str.Length()))
return(-3); return(-2);
if(draw_chars_count<=0) //可绘制字符为0这是全空格 if(draw_chars_count<=0) //可绘制字符为0这是全空格
return(-4); return(-3);
if(!rc->Init(draw_chars_count)) //创建 if(!rc->Init(draw_chars_count)) //创建
return(-5); return(-4);
vertex =rc->CreateVADA<VB4f>(VAN::Vertex); vertex =rc->CreateVADA<VB4f>(VAN::Vertex);
tex_coord =rc->CreateVADA<VB4f>(VAN::TexCoord); tex_coord =rc->CreateVADA<VB4f>(VAN::TexCoord);
@ -222,19 +234,22 @@ namespace hgl
if(direction.vertical) if(direction.vertical)
{ {
if(direction.right_to_left) if(direction.right_to_left)
return pl_v_r2l(); return sl_v_r2l();
else else
return pl_v_l2r(); return sl_v_l2r();
} }
else else
{ {
if(direction.right_to_left) if(direction.right_to_left)
return pl_h_r2l(); return sl_h_r2l();
else else
return pl_h_l2r(); return sl_h_l2r();
} }
return 0; return 0;
} }
int TextLayout::SimpleLayout(TileFont *tf,const UTF16String &str){return this->SimpleLayout<u16char>(tf,str);}
int TextLayout::SimpleLayout(TileFont *tf,const UTF32String &str){return this->SimpleLayout<u32char>(tf,str);}
}//namespace graph }//namespace graph
}//namespace hgl }//namespace hgl