From 1902252126bcc67c8a006dd5099bba96acdf5799 Mon Sep 17 00:00:00 2001 From: "HuYingzhuo(hugo/hyzboy)" Date: Wed, 28 Jun 2023 18:40:06 +0800 Subject: [PATCH] added BitmapFont.cpp/.h and VGA8.F16 --- BitmapFont.cpp | 34 ++++ BitmapFont.h | 12 ++ CMakeLists.txt | 2 +- DistributionChart2D.cpp | 361 +++++++++++++++++++++++----------------- VGA8.F16 | Bin 0 -> 4096 bytes 5 files changed, 256 insertions(+), 153 deletions(-) create mode 100644 BitmapFont.cpp create mode 100644 BitmapFont.h create mode 100644 VGA8.F16 diff --git a/BitmapFont.cpp b/BitmapFont.cpp new file mode 100644 index 0000000..ce35c87 --- /dev/null +++ b/BitmapFont.cpp @@ -0,0 +1,34 @@ +#include + +using namespace hgl; + +namespace +{ + uint8 *bitmap_font_data=nullptr; +}//namespace + +bool LoadBitmapFont() +{ + if(bitmap_font_data) + return(true); + + bitmap_font_data=new uint8[256*16]; + + if(!filesystem::LoadFileToMemory(OS_TEXT("VGA8.F16"), (void **)&bitmap_font_data)) + return(false); + + return(true); +} + +void ClearBitmapFont() +{ + SAFE_CLEAR_ARRAY(bitmap_font_data); +} + +const uint GetCharWidth(){return bitmap_font_data?8:0;} +const uint GetCharHeight(){return bitmap_font_data?16:0;} + +const uint8 *GetBitmapChar(const char ch) +{ + return bitmap_font_data+uchar(ch)*16; +} \ No newline at end of file diff --git a/BitmapFont.h b/BitmapFont.h new file mode 100644 index 0000000..1b4fa2e --- /dev/null +++ b/BitmapFont.h @@ -0,0 +1,12 @@ +#pragma once +#include + +using namespace hgl; + +bool LoadBitmapFont(); +void ClearBitmapFont(); + +const uint GetCharWidth(); +const uint GetCharHeight(); + +const uint8 *GetBitmapChar(const char ch); \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 182ac49..6244bdd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -55,5 +55,5 @@ target_link_libraries(OSFontList PRIVATE CMUtil) add_executable(RuntimeAssetManagerTest RuntimeAssetManagerTest.cpp RuntimeAssetManager.h RAM_TestClass.h RAM_TestClass.cpp) cm_example_project(RuntimeAssetManagerTest) -add_executable(DistributionChart2D DistributionChart2D.cpp) +add_executable(DistributionChart2D DistributionChart2D.cpp BitmapFont.cpp BitmapFont.h) cm_example_project(DistributionChart2D) \ No newline at end of file diff --git a/DistributionChart2D.cpp b/DistributionChart2D.cpp index 9465569..2d6f263 100644 --- a/DistributionChart2D.cpp +++ b/DistributionChart2D.cpp @@ -1,90 +1,61 @@ #include #include +#include #include #include #include #include +#include"BitmapFont.h" using namespace hgl; -constexpr const uint CHAR_BITMAP_WIDTH=3; -constexpr const uint CHAR_BITMAP_HEIGHT=5; -constexpr const uint CHAR_BITMAP_SCALE=4; +uint CHAR_BITMAP_WIDTH=0; +uint CHAR_BITMAP_HEIGHT=0; +uint CHAR_BITMAP_SCALE=0; +uint CHAR_LINE_HEIGHT=0; -constexpr const uint8 bitmap_font[10][15]= +bool InitBitmapFont() { - { - 1,1,1, - 1,0,1, - 1,0,1, - 1,0,1, - 1,1,1 - }, - { - 0,1,0, - 1,1,0, - 0,1,0, - 0,1,0, - 1,1,1 - }, - { - 1,1,1, - 0,0,1, - 1,1,1, - 1,0,0, - 1,1,1 - }, - { - 1,1,1, - 0,0,1, - 1,1,1, - 0,0,1, - 1,1,1 - }, - { - 1,0,1, - 1,0,1, - 1,1,1, - 0,0,1, - 0,0,1 - }, - { - 1,1,1, - 1,0,0, - 1,1,1, - 0,0,1, - 1,1,1 - }, - { - 1,1,1, - 1,0,0, - 1,1,1, - 1,0,1, - 1,1,1 - }, - { - 1,1,1, - 0,0,1, - 0,0,1, - 0,0,1, - 0,0,1 - }, - { - 1,1,1, - 1,0,1, - 1,1,1, - 1,0,1, - 1,1,1 - }, - { - 1,1,1, - 1,0,1, - 1,1,1, - 0,0,1, - 1,1,1 - } + if(!LoadBitmapFont()) + return(false); + + CHAR_BITMAP_WIDTH=GetCharWidth(); + CHAR_BITMAP_HEIGHT=GetCharHeight(); + + CHAR_BITMAP_SCALE=1; + CHAR_LINE_HEIGHT=CHAR_BITMAP_HEIGHT*CHAR_BITMAP_SCALE; + + return(true); +} + +constexpr const Vector3u8 white_color={255,255,255}; + +constexpr const uint STOP_COUNT=5; + +constexpr const Vector3u8 stop_color[STOP_COUNT]= +{ + {255,0,0}, + {255,255,0}, + {0,255,0}, + {0,255,255}, + {0,0,255}, }; +HGL_GRADIENT_DEFINE(GradientColor3u8,uint,Vector3u8) +{ + result.r=start.r+float(end.r-start.r)*pos; + result.g=start.g+float(end.g-start.g)*pos; + result.b=start.b+float(end.b-start.b)*pos; +} + +GradientColor3u8 ColorGradient; + +void InitGradient(uint max_count) +{ + for(uint i=0;i>=1; } + + ++sp; } } - void DrawNumber(const uint number,const uint x,const uint y,const Vector3u8 &color,const uint8 alpha) + void DrawString(const AnsiString &str,const uint x,const uint y,const Vector3u8 &stop_color,const uint8 alpha) { - AnsiString str=AnsiString::numberOf(number); - const char *sp=str.c_str(); const uint len=str.Length(); @@ -305,12 +286,43 @@ public: for(uint i=0;icount_data+(x+y*width); - if(*cp32<0xFFFFFFFF) - ++(*cp32); + ++(*cp32); + + if(*cp32>max_count)max_count=*cp32; ++p; } } + //统计占比 + { + uint32 *cp32=chart->count_data; + + hgl_zero(step_count); + + for(uint y=0;y0) + for(uint i=0;imax_count*(STOP_COUNT-1-i)/STOP_COUNT) + { + step_count[i]+=*cp32; + break; + } + + ++cp32; + } + } + } + //画圆 { uint32 *cp32=chart->count_data; @@ -352,7 +390,7 @@ Chart *ToChart32(const PositionStat *ps) for(uint x=0;x0) - chart->Circle(x,y,(*cp32)*10); + chart->DrawCircle(x,y,*cp32); ++cp32; } @@ -373,13 +411,15 @@ Chart *ToChart32(const PositionStat *ps) std::cout<<"max_count: "<max_count<max_count); + //生成权重图 { uint32 *cp32=chart->circle_data; uint8 *cp8=chart->chart_data; - float result; - float r,g,b; + float alpha; + Vector3u8 final_color; //1.0 1 0 0 //0.8 1 1 0 @@ -390,72 +430,86 @@ Chart *ToChart32(const PositionStat *ps) for(uint i=0;imax_count); + alpha=float(*cp32)/float(chart->max_count); - if(result>=0.8f) //0.8 - 1.0 + ColorGradient.Get(final_color,*cp32); + + if(*cp32>0) //为了避免最后什么都看不见,所以把没数据的挑出来,剩下的透明度全部加0.25 { - r=1.0f; - g=1.0f-(result-0.8f)*5.0f; - b=0.0f; - } - else - { - if(result>=0.6f) //0.6 - 0.8 - { - r=(result-0.6f)*5.0f; - g=1.0f; - b=0; - } - else - { - if(result>=0.4f) //0.4 - 0.6 - { - r=0.0f; - g=1.0f; - b=1.0f-(result-0.4f)*5.0f; - } - else - if(result>=0.2f) //0.2 - 0.4 - { - r=0.0f; - g=(result-0.2f)*5.0f; - b=1.0f; - } - else //0.0 - 0.25 - { - r=0.0f; - g=0.0f; - b=result*5.0f; - } - } + alpha+=0.25f; + + if(alpha>1) + alpha=1; } - cp8[0]=b*255.0f; - cp8[1]=g*255.0f; - cp8[2]=r*255.0f; - cp8[3]=result*255.0f; + cp8[0]=final_color.b; + cp8[1]=final_color.g; + cp8[2]=final_color.r; + cp8[3]=alpha*255.0f; ++cp32; cp8+=4; } } + if(CHAR_LINE_HEIGHT==0) + return chart; + //写入数值 { - const Vector3u8 color[6]= + uint col=10; + uint row=10; + + AnsiString str; + AnsiString num_str; + const AnsiString str_total=AnsiString::numberOf(ps->count); + + AnsiString step_str[6]= { - {255,0,0}, - {255,255,0}, - {0,255,0}, - {0,255,255}, - {0,0,255}, - {0,0,0}, + AnsiString::numberOf(max_count), + AnsiString::numberOf(uint(max_count*0.8f)), + AnsiString::numberOf(uint(max_count*0.6f)), + AnsiString::numberOf(uint(max_count*0.4f)), + AnsiString::numberOf(uint(max_count*0.2f)), + AnsiString::numberOf(0), }; - uint num=chart->max_count; + char space[32]; - for(uint i=0;i<6;i++) - chart->DrawNumber(float(num)*(1.0f-float(i)*0.2f),10,10+i*(CHAR_BITMAP_HEIGHT*CHAR_BITMAP_SCALE+1),color[i],255); + memset(space,' ',32); + + str=AnsiString("TOTAL - ")+str_total; + + chart->DrawString(str,col,row,white_color,255); + row+=CHAR_LINE_HEIGHT; + + chart->DrawGradient(col,row,CHAR_BITMAP_WIDTH*CHAR_BITMAP_SCALE,CHAR_LINE_HEIGHT*STOP_COUNT); + + col+=CHAR_BITMAP_WIDTH*CHAR_BITMAP_SCALE*2; + + for(uint i=0;i0) + { + str+=" - "; + str+=AnsiString::numberOf(float(step_count[i])*100.0f/float(ps->count)); + str+="%"; + } + } + + chart->DrawString(str,col,row,stop_color[i],255); + row+=CHAR_LINE_HEIGHT; + } } return chart; @@ -481,6 +535,8 @@ int os_main(int argc,os_char **argv) return(-1); } + bool font=InitBitmapFont(); + std::cout<<"file total line: "< ps=ToVector2i(sl); @@ -494,17 +550,18 @@ int os_main(int argc,os_char **argv) io::OpenFileOutputStream fos(OS_TEXT("chart.tga"),io::FileOpenMode::CreateTrunc); - if(!fos) + if(fos) { - std::cerr<<"Create chart.tga failed!"<Write(&tga_header,util::TGAHeaderSize); + fos->Write(chart->chart_data,chart->width*chart->height*4); + fos->Close(); } - - fos->Write(&tga_header,util::TGAHeaderSize); - fos->Write(chart->chart_data,chart->width*chart->height*4); - fos->Close(); + else + std::cerr<<"Create chart.tga failed!"<hJ`aivILPXw`sBVQp+DsdN-D~8}|vk)PloBIyeq)U_U zU|8%RQiv;;1#cEt-sU(A0pAuzo8S90qtPaBZArg*Z{FXTr;#!HRA2mladG+n;-VH# zy}Q}%t~a}#Vq0l}x=oTi|&`&+^1ug_nDe%jp4Wgtmz@=boD z@^9z2_uFoJe>;E6>?hIac+yRdN6`}%&!IZ0ox_IAfp8c8`OVGEuZKj8tER|o)7UI- zDiPo-rA#2wA^J3*f-$82(-WvSh;+qWtX$TS7G04B%FTQ}SM5Cd+;;YP&p*hrs&eKr zjN(;UU%pAgf~zJ@(xQ1M(=uzb=?e9l zGDc|%VdxdIOi3&QX%aIfeS~Gc%Qf39gX}n4UKL#KCzvoBD$>t*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