fix draw text example.
This commit is contained in:
parent
346d8b3279
commit
cea2f8da9e
2
CMCore
2
CMCore
@ -1 +1 @@
|
||||
Subproject commit 2d6883be633abae7f0f10810e2b61367175bcfc4
|
||||
Subproject commit c2c7c42cd43759a9a613b6ad2c890a2e1d948b37
|
@ -1,4 +1,4 @@
|
||||
// DrawTile
|
||||
// DrawTile
|
||||
// 该示例使用TileData,演示多个tile图片在一张纹理上
|
||||
|
||||
#include<hgl/type/StringList.h>
|
||||
@ -15,13 +15,6 @@
|
||||
using namespace hgl;
|
||||
using namespace hgl::graph;
|
||||
|
||||
/**
|
||||
* 文本绘制技术流程:
|
||||
*
|
||||
* 1.由TextLayout模块排版所有的字符,并向FontSource获取所有字符的Bitmap。生成文本可渲染对象的vertex position数据
|
||||
* 2.由TextLayout向TileData提交需要渲染的所有字符的Bitmap,并得到每个Bitmap对应的UV数据。生成文本可渲染对象的uv数据
|
||||
*/
|
||||
|
||||
constexpr uint32_t SCREEN_WIDTH =1280;
|
||||
constexpr uint32_t SCREEN_HEIGHT=960;
|
||||
|
||||
@ -31,6 +24,8 @@ constexpr uint CHAR_BITMAP_BORDER=1; //边界象素尺寸
|
||||
class TestApp:public VulkanApplicationFramework
|
||||
{
|
||||
Camera cam;
|
||||
|
||||
Color4f color;
|
||||
|
||||
private:
|
||||
|
||||
@ -39,6 +34,7 @@ private:
|
||||
vulkan::MaterialInstance * material_instance =nullptr;
|
||||
vulkan::Renderable * render_obj =nullptr;
|
||||
vulkan::Buffer * ubo_world_matrix =nullptr;
|
||||
vulkan::Buffer * ubo_color =nullptr;
|
||||
|
||||
vulkan::Pipeline * pipeline =nullptr;
|
||||
|
||||
@ -71,7 +67,7 @@ private:
|
||||
{
|
||||
material=shader_manage->CreateMaterial( OS_TEXT("res/shader/DrawRect2D.vert"),
|
||||
OS_TEXT("res/shader/DrawRect2D.geom"),
|
||||
OS_TEXT("res/shader/FlatTexture.frag"));
|
||||
OS_TEXT("res/shader/FlatLumTexture.frag"));
|
||||
if(!material)
|
||||
return(false);
|
||||
|
||||
@ -81,6 +77,7 @@ private:
|
||||
|
||||
material_instance->BindSampler("tex",tile_font->GetTexture(),sampler);
|
||||
material_instance->BindUBO("world",ubo_world_matrix);
|
||||
material_instance->BindUBO("color_material",ubo_color);
|
||||
material_instance->Update();
|
||||
|
||||
db->Add(material);
|
||||
@ -102,6 +99,13 @@ private:
|
||||
if(!ubo_world_matrix)
|
||||
return(false);
|
||||
|
||||
color.One();
|
||||
|
||||
ubo_color=db->CreateUBO(sizeof(Color4f),&color);
|
||||
|
||||
if(!ubo_color)
|
||||
return(false);
|
||||
|
||||
return(true);
|
||||
}
|
||||
|
||||
@ -130,6 +134,7 @@ private:
|
||||
|
||||
text_rc=new RenderableCreater(db,material);
|
||||
|
||||
tl_engine.Set(tile_font->GetFontSource());
|
||||
tl_engine.Set(text_rc);
|
||||
tl_engine.Set(&tla);
|
||||
tl_engine.SetTextDirection(0);
|
||||
|
@ -1,4 +1,4 @@
|
||||
// DrawTile
|
||||
// DrawTile
|
||||
// 该示例使用TileData,演示多个tile图片在一张纹理上
|
||||
|
||||
#include<hgl/type/StringList.h>
|
||||
|
@ -1,4 +1,4 @@
|
||||
// 2.RectanglePrimivate
|
||||
// 2.RectanglePrimivate
|
||||
// 该示例是texture_rect的进化,演示使用GeometryShader画矩形
|
||||
|
||||
#include"VulkanAppFramework.h"
|
||||
|
@ -3,6 +3,7 @@
|
||||
|
||||
#include<hgl/graph/SceneDB.h>
|
||||
#include<hgl/graph/VertexAttribDataAccess.h>
|
||||
#include<hgl/graph/vulkan/VKShaderModule.h>
|
||||
namespace hgl
|
||||
{
|
||||
namespace graph
|
||||
|
@ -79,7 +79,7 @@ namespace hgl
|
||||
virtual ~FontSource()=default;
|
||||
|
||||
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 &); ///<取得字符排版信息
|
||||
virtual int GetCharHeight ()const=0; ///<取得字符高度
|
||||
|
||||
@ -109,11 +109,11 @@ namespace hgl
|
||||
virtual ~FontSourceSingle()=default;
|
||||
|
||||
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;} ///<取得字符高度
|
||||
};//class FontSourceSingle:public FontSource
|
||||
|
||||
FontSourceSingle *CreateFontSource(const Font &f);
|
||||
FontSource *CreateFontSource(const Font &f);
|
||||
|
||||
/**
|
||||
* 文字位图多重数据源
|
||||
@ -148,7 +148,7 @@ namespace hgl
|
||||
public:
|
||||
|
||||
FontBitmap *GetCharBitmap (const u32char &ch) override;
|
||||
const bool GetCharMetrics (CharMetricsInfo &,const u32char &); ///<取得字符绘制信息
|
||||
const bool GetCharMetrics (CharMetricsInfo &,const u32char &)override; ///<取得字符绘制信息
|
||||
int GetCharHeight ()const override; ///<取得字符高度
|
||||
};//class FontSourceMulti:public FontSource
|
||||
}//namespace graph
|
||||
|
@ -85,6 +85,7 @@ namespace hgl
|
||||
{
|
||||
protected:
|
||||
|
||||
FontSource *font_source;
|
||||
RenderableCreater *rc;
|
||||
TextLayoutAttributes tla;
|
||||
|
||||
@ -102,13 +103,13 @@ namespace hgl
|
||||
|
||||
struct CharDrawAttr
|
||||
{
|
||||
CLA *cla;
|
||||
const CLA *cla;
|
||||
TileUVFloat uv;
|
||||
};
|
||||
|
||||
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:
|
||||
|
||||
@ -120,6 +121,9 @@ namespace hgl
|
||||
int sl_v_r2l();
|
||||
int sl_v_l2r();
|
||||
|
||||
template<typename T>
|
||||
int SimpleLayout (TileFont *,const BaseString<T> &); ///<简易排版
|
||||
|
||||
protected:
|
||||
|
||||
TEXT_COORD_TYPE x,y;
|
||||
@ -134,8 +138,8 @@ namespace hgl
|
||||
|
||||
protected:
|
||||
|
||||
AutoDelete<VB4f> vertex,
|
||||
tex_coord;
|
||||
VB4f *vertex;
|
||||
VB4f *tex_coord;
|
||||
|
||||
public:
|
||||
|
||||
@ -154,7 +158,7 @@ namespace hgl
|
||||
|
||||
void Set (RenderableCreater *_rc) {if(_rc)rc=_rc;}
|
||||
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 Set (const TextAlign &ta) {tla.align=ta;}
|
||||
void SetMaxWidth (const float mw) {tla.max_width=mw;}
|
||||
@ -162,10 +166,8 @@ namespace hgl
|
||||
|
||||
virtual bool Init (); ///<初始化排版
|
||||
|
||||
// virtual int Layout (const int max_chars,const BaseString<T> &)=0; ///<排版
|
||||
|
||||
template<typename T>
|
||||
int SimpleLayout (TileFont *,const BaseString<T> &); ///<简易排版
|
||||
int SimpleLayout (TileFont *,const UTF16String &); ///<简易排版
|
||||
int SimpleLayout (TileFont *,const UTF32String &); ///<简易排版
|
||||
};//class TextLayout
|
||||
}//namespace graph
|
||||
}//namespace hgl
|
||||
|
@ -72,8 +72,8 @@ public:
|
||||
* 获取输入流绑定点,需要注意的时,这里获取的binding并非是shader中的binding/location,而是绑定顺序的序列号。对应vkCmdBindVertexBuffer的缓冲区序列号
|
||||
*/
|
||||
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 uint GetStageInputCount()const{return shader_resource->GetStageInputCount();}
|
||||
const ShaderStage * GetStageInput (const AnsiString &name)const{return shader_resource->GetStageInput(name);}
|
||||
const uint GetStageInputCount () const{return shader_resource->GetStageInputCount();}
|
||||
|
||||
const uint32_t GetAttrCount()const{return attr_count;}
|
||||
|
||||
|
@ -426,7 +426,7 @@ Device *CreateRenderDevice(VkInstance inst,const PhysicalDevice *physical_device
|
||||
if(!device_attr->pipeline_cache)
|
||||
return(nullptr);
|
||||
|
||||
auto_delete.Clear();
|
||||
auto_delete.Discard();
|
||||
|
||||
return(new Device(device_attr));
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
#include<hgl/graph/font/TileFont.h>
|
||||
#include<hgl/graph/font/TileFont.h>
|
||||
#include<hgl/graph/vulkan/VKDevice.h>
|
||||
|
||||
namespace hgl
|
||||
|
@ -1,4 +1,4 @@
|
||||
#include<hgl/graph/font/FontSource.h>
|
||||
#include<hgl/graph/font/FontSource.h>
|
||||
|
||||
namespace hgl
|
||||
{
|
||||
|
@ -6,43 +6,48 @@ namespace hgl
|
||||
{
|
||||
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;
|
||||
uint8 *sp=src,*p;
|
||||
uint8 *tp;
|
||||
|
||||
while(h--)
|
||||
while(src_h--)
|
||||
{
|
||||
pos=w;
|
||||
pos=src_w;
|
||||
p=sp;
|
||||
tp=dst;
|
||||
|
||||
while(pos--)
|
||||
{
|
||||
if(*p==64)*dst=255;
|
||||
else *dst=(*p)<<2;
|
||||
if(*p==64)*tp=255;
|
||||
else *tp=(*p)<<2;
|
||||
|
||||
dst++;
|
||||
tp++;
|
||||
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 *tp;
|
||||
uint8 bit;
|
||||
|
||||
while(h--)
|
||||
while(src_h--)
|
||||
{
|
||||
p=sp;
|
||||
tp=dst;
|
||||
|
||||
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)
|
||||
{
|
||||
@ -52,7 +57,8 @@ namespace hgl
|
||||
else bit>>=1;
|
||||
}
|
||||
|
||||
sp+=line_bytes;
|
||||
sp+=src_line_bytes;
|
||||
dst+=dst_w;
|
||||
}//while(h--)
|
||||
}
|
||||
}//namespace
|
||||
@ -120,8 +126,8 @@ namespace hgl
|
||||
|
||||
GetGlyphOutlineW(hdc,ch,ggo,&gm,buffer_size,buffer,&mat);
|
||||
|
||||
bmp->metrics_info.w =gm.gmBlackBoxX;
|
||||
bmp->metrics_info.h =gm.gmBlackBoxY;
|
||||
bmp->metrics_info.w =((gm.gmBlackBoxX+3)>>2)<<2;
|
||||
bmp->metrics_info.h =((gm.gmBlackBoxY+3)>>2)<<2;
|
||||
|
||||
bmp->metrics_info.x =gm.gmptGlyphOrigin.x;
|
||||
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];
|
||||
|
||||
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
|
||||
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);
|
||||
}
|
||||
|
||||
FontSourceSingle *CreateFontSource(const Font &f)
|
||||
FontSource *CreateFontSource(const Font &f)
|
||||
{
|
||||
return(new WinBitmapFont(f));
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
#include<hgl/graph/font/TextLayout.h>
|
||||
#include<hgl/graph/font/TileFont.h>
|
||||
#include<hgl/type/Extent.h>
|
||||
#include<hgl/type/UnicodeBlocks.h>
|
||||
|
||||
namespace hgl
|
||||
{
|
||||
@ -9,8 +9,8 @@ namespace hgl
|
||||
bool TextLayout::Init()
|
||||
{
|
||||
if(!rc
|
||||
||!tla.font_source
|
||||
||!tla.char_attributes)
|
||||
||(!tla.font_source&&!font_source)
|
||||
||!tla.char_layout_attr)
|
||||
return(false);
|
||||
|
||||
direction.text_direction=tla.text_direction;
|
||||
@ -26,7 +26,10 @@ namespace hgl
|
||||
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;
|
||||
char_height =ceil(origin_char_height);
|
||||
@ -45,36 +48,42 @@ namespace hgl
|
||||
* 预处理所有的字符,获取所有字符的宽高,以及是否标点符号等信息
|
||||
*/
|
||||
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)
|
||||
return 0;
|
||||
|
||||
draw_chars_count=0;
|
||||
alone_chars.ClearData();
|
||||
if(!tile_font
|
||||
||!str||!*str||str_length<=0
|
||||
||!font_source)
|
||||
return(false);
|
||||
|
||||
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); //统计所有不重复字符
|
||||
++draw_chars_count;
|
||||
}
|
||||
cda=new CharDrawAttr;
|
||||
|
||||
++cp;
|
||||
++cda;
|
||||
cda->cla=font_source->GetCLA(*cp);
|
||||
|
||||
if(cda->cla->visible)
|
||||
{
|
||||
alone_chars.Add(*cp); //统计所有不重复字符
|
||||
++draw_chars_count;
|
||||
}
|
||||
|
||||
draw_chars_list.Add(cda);
|
||||
|
||||
++cp;
|
||||
}
|
||||
}
|
||||
|
||||
//注册不重复字符给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();
|
||||
alone_chars.ClearData();
|
||||
@ -83,15 +92,18 @@ namespace hgl
|
||||
}
|
||||
|
||||
//为可绘制字符列表中的字符获取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 left=0,top=0;
|
||||
|
||||
float *tp=vertex->Begin();
|
||||
float *tcp=tex_coord->Begin();
|
||||
float *tp=vertex->Get();
|
||||
float *tcp=tex_coord->Get();
|
||||
|
||||
for(int i=0;i<count;i++)
|
||||
{
|
||||
@ -204,14 +216,14 @@ namespace hgl
|
||||
if(!tf||str.IsEmpty())
|
||||
return(-1);
|
||||
|
||||
if(preprocess<T>(str.c_str(),str.Length())<=0)
|
||||
return(-3);
|
||||
if(!preprocess<T>(tf,str.c_str(),str.Length()))
|
||||
return(-2);
|
||||
|
||||
if(draw_chars_count<=0) //可绘制字符为0???这是全空格?
|
||||
return(-4);
|
||||
return(-3);
|
||||
|
||||
if(!rc->Init(draw_chars_count)) //创建
|
||||
return(-5);
|
||||
return(-4);
|
||||
|
||||
vertex =rc->CreateVADA<VB4f>(VAN::Vertex);
|
||||
tex_coord =rc->CreateVADA<VB4f>(VAN::TexCoord);
|
||||
@ -222,19 +234,22 @@ namespace hgl
|
||||
if(direction.vertical)
|
||||
{
|
||||
if(direction.right_to_left)
|
||||
return pl_v_r2l();
|
||||
return sl_v_r2l();
|
||||
else
|
||||
return pl_v_l2r();
|
||||
return sl_v_l2r();
|
||||
}
|
||||
else
|
||||
{
|
||||
if(direction.right_to_left)
|
||||
return pl_h_r2l();
|
||||
return sl_h_r2l();
|
||||
else
|
||||
return pl_h_l2r();
|
||||
return sl_h_l2r();
|
||||
}
|
||||
|
||||
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 hgl
|
||||
|
Loading…
x
Reference in New Issue
Block a user