From a1932eec23589ed67d824873b37de3e83a09cd24 Mon Sep 17 00:00:00 2001 From: Thales1330 Date: Tue, 3 Jan 2017 19:03:12 -0200 Subject: Text element optimizated, but buggy Element/parent search buggy --- Project/wxGLString.cpp | 407 +++++++++++++++++++++++-------------------------- 1 file changed, 190 insertions(+), 217 deletions(-) (limited to 'Project/wxGLString.cpp') diff --git a/Project/wxGLString.cpp b/Project/wxGLString.cpp index b54a953..23286bc 100644 --- a/Project/wxGLString.cpp +++ b/Project/wxGLString.cpp @@ -8,85 +8,76 @@ #include "wx/wx.h" - GLuint* loadImage(wxImage* img) { - GLuint* ID=new GLuint[1]; - glGenTextures( 1, &ID[0] ); - - glBindTexture( GL_TEXTURE_2D, *ID ); + GLuint* ID = new GLuint[1]; + glGenTextures(1, &ID[0]); + glBindTexture(GL_TEXTURE_2D, *ID); - glPixelStorei(GL_UNPACK_ALIGNMENT, 1 ); + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); const int w = img->GetWidth(), h = img->GetHeight(); - // note: must make a local copy before passing the data to OpenGL, as GetData() returns RGB // and we want the Alpha channel. Furthermore, the current rendering is black-on-white, we'll // convert it to an alpha channel by the way (not all platforms support transparency in wxDCs // so it's the easiest way to go) - GLubyte *bitmapData=img->GetData(); - GLubyte *imageData; + GLubyte* bitmapData = img->GetData(); + GLubyte* imageData; int bytesPerPixel = 4; int imageSize = w * h * bytesPerPixel; - imageData=(GLubyte *)malloc(imageSize); + imageData = (GLubyte*)malloc(imageSize); - int rev_val=h-1; + int rev_val = h - 1; - for(int y=0; ygetID()[0] ); -} +void wxGLString::operator=(wxString& string) { (*((wxString*)this)) = string; } +void wxGLString::bind() const { glBindTexture(GL_TEXTURE_2D, img->getID()[0]); } void wxGLString::calculateSize(wxDC* dc, const bool ignore_font /* when from array */) { - if(!ignore_font) - { - if(font.IsOk()) dc->SetFont(font); - else dc->SetFont(wxSystemSettings::GetFont(wxSYS_SYSTEM_FONT)); + if(!ignore_font) { + if(font.IsOk()) + dc->SetFont(font); + else + dc->SetFont(wxSystemSettings::GetFont(wxSYS_SYSTEM_FONT)); } - + dc->GetTextExtent(*this, &w, &h); } void wxGLString::consolidate(wxDC* dc) { calculateSize(dc); - const int power_of_2_w = std::max(32, (int)pow( (double)2, (int)ceil((float)log((double)w)/log(2.0)) )); - const int power_of_2_h = std::max(32, (int)pow( (double)2, (int)ceil((float)log((double)h)/log(2.0)) )); + const int power_of_2_w = std::max(32, (int)pow((double)2, (int)ceil((float)log((double)w) / log(2.0)))); + const int power_of_2_h = std::max(32, (int)pow((double)2, (int)ceil((float)log((double)h) / log(2.0)))); wxBitmap bmp(power_of_2_w, power_of_2_h); assert(bmp.IsOk()); @@ -262,9 +231,11 @@ void wxGLString::consolidate(wxDC* dc) temp_dc.SetBrush(*wxWHITE_BRUSH); temp_dc.Clear(); - if(font.IsOk()) temp_dc.SetFont(font); - else temp_dc.SetFont(wxSystemSettings::GetFont(wxSYS_SYSTEM_FONT)); - + if(font.IsOk()) + temp_dc.SetFont(font); + else + temp_dc.SetFont(wxSystemSettings::GetFont(wxSYS_SYSTEM_FONT)); + temp_dc.DrawText(*this, 0, 0); } @@ -274,23 +245,17 @@ void wxGLString::consolidate(wxDC* dc) TextGLDrawable::texw = power_of_2_w; TextGLDrawable::texh = power_of_2_h; TextGLDrawable::tex_coord_x2 = (float)w / (float)power_of_2_w; - TextGLDrawable::tex_coord_y2 = 1-(float)h / (float)power_of_2_h; + TextGLDrawable::tex_coord_y2 = 1 - (float)h / (float)power_of_2_h; TextGLDrawable::tex_coord_y1 = 1; TextGLDrawable::setImage(img); } -void wxGLString::consolidateFromArray(wxDC* dc, double x, double y) -{ - dc->DrawText(*this, x, y); -} +void wxGLString::consolidateFromArray(wxDC* dc, double x, double y) { dc->DrawText(*this, x, y); } -void wxGLString::setFont(wxFont font) -{ - wxGLString::font = font; -} +void wxGLString::setFont(wxFont font) { wxGLString::font = font; } -void wxGLString::render(const double x, const double y) +void wxGLString::render(const double x, const double y) { TextGLDrawable::move(x, y); TextGLDrawable::render(); @@ -300,38 +265,37 @@ wxGLString::~wxGLString() if(img != NULL) delete img; } - #if 0 #pragma mark - #pragma mark wxGLNumberRenderer implementation #endif -wxGLNumberRenderer::wxGLNumberRenderer() : wxGLString( wxT("0 1 2 3 4 5 6 7 8 9 . - ") ) +wxGLNumberRenderer::wxGLNumberRenderer() + : wxGLString(wxT("0 1 2 3 4 5 6 7 8 9 . - ")) { number_location = new int[13]; } -wxGLNumberRenderer::~wxGLNumberRenderer() -{ - delete[] number_location; -} +wxGLNumberRenderer::~wxGLNumberRenderer() { delete[] number_location; } void wxGLNumberRenderer::consolidate(wxDC* dc) { wxGLString::consolidate(dc); - if(font.IsOk()) dc->SetFont(font); - else dc->SetFont(wxSystemSettings::GetFont(wxSYS_SYSTEM_FONT)); - + if(font.IsOk()) + dc->SetFont(font); + else + dc->SetFont(wxSystemSettings::GetFont(wxSYS_SYSTEM_FONT)); + number_location[0] = 0; - number_location[1] = dc->GetTextExtent(wxT("0 ")).GetWidth(); - number_location[2] = dc->GetTextExtent(wxT("0 1 ")).GetWidth(); - number_location[3] = dc->GetTextExtent(wxT("0 1 2 ")).GetWidth(); - number_location[4] = dc->GetTextExtent(wxT("0 1 2 3 ")).GetWidth(); - number_location[5] = dc->GetTextExtent(wxT("0 1 2 3 4 ")).GetWidth(); - number_location[6] = dc->GetTextExtent(wxT("0 1 2 3 4 5 ")).GetWidth(); - number_location[7] = dc->GetTextExtent(wxT("0 1 2 3 4 5 6 ")).GetWidth(); - number_location[8] = dc->GetTextExtent(wxT("0 1 2 3 4 5 6 7 ")).GetWidth(); - number_location[9] = dc->GetTextExtent(wxT("0 1 2 3 4 5 6 7 8 ")).GetWidth(); + number_location[1] = dc->GetTextExtent(wxT("0 ")).GetWidth(); + number_location[2] = dc->GetTextExtent(wxT("0 1 ")).GetWidth(); + number_location[3] = dc->GetTextExtent(wxT("0 1 2 ")).GetWidth(); + number_location[4] = dc->GetTextExtent(wxT("0 1 2 3 ")).GetWidth(); + number_location[5] = dc->GetTextExtent(wxT("0 1 2 3 4 ")).GetWidth(); + number_location[6] = dc->GetTextExtent(wxT("0 1 2 3 4 5 ")).GetWidth(); + number_location[7] = dc->GetTextExtent(wxT("0 1 2 3 4 5 6 ")).GetWidth(); + number_location[8] = dc->GetTextExtent(wxT("0 1 2 3 4 5 6 7 ")).GetWidth(); + number_location[9] = dc->GetTextExtent(wxT("0 1 2 3 4 5 6 7 8 ")).GetWidth(); number_location[10] = dc->GetTextExtent(wxT("0 1 2 3 4 5 6 7 8 9 ")).GetWidth(); number_location[11] = dc->GetTextExtent(wxT("0 1 2 3 4 5 6 7 8 9 . ")).GetWidth(); number_location[12] = dc->GetTextExtent(wxT("0 1 2 3 4 5 6 7 8 9 . - ")).GetWidth(); @@ -353,39 +317,62 @@ void wxGLNumberRenderer::renderNumber(float f, double x, double y) void wxGLNumberRenderer::renderNumber(wxString s, double x, double y) { const int full_string_w = TextGLDrawable::texw; - + const int char_amount = s.Length(); - for(int c=0; cgetID()[0] ); -} -void wxGLStringArray::addString(wxString string) -{ - strings.push_back( wxGLString(string) ); -} -void wxGLStringArray::setFont(wxFont font) -{ - wxGLStringArray::font = font; -} +wxGLString& wxGLStringArray::get(const int id) { return strings[id]; } +void wxGLStringArray::bind() { glBindTexture(GL_TEXTURE_2D, img->getID()[0]); } +void wxGLStringArray::addString(wxString string) { strings.push_back(wxGLString(string)); } +void wxGLStringArray::setFont(wxFont font) { wxGLStringArray::font = font; } void wxGLStringArray::consolidate(wxDC* dc) { - int x=0, y=0; + int x = 0, y = 0; + + if(font.IsOk()) + dc->SetFont(font); + else + dc->SetFont(wxSystemSettings::GetFont(wxSYS_SYSTEM_FONT)); - if(font.IsOk()) dc->SetFont(font); - else dc->SetFont(wxSystemSettings::GetFont(wxSYS_SYSTEM_FONT)); - // find how much space we need int longest_string = 0; const int amount = strings.size(); - for(int n=0; n longest_string) longest_string = strings[n].w; - }//next + } // next const int average_string_height = y / amount; - + // split in multiple columns if necessary int column_amount = 1; - while (amount/column_amount > 30 && column_amount<10) - column_amount ++; - - const int power_of_2_w = pow( (double)2, (int)ceil((float)log((double)longest_string*(double)column_amount)/log(2.0)) ); - const int power_of_2_h = pow( (double)2, (int)ceil((float)log((double)y/(double)column_amount)/log(2.0)) ); - - //std::cout << "bitmap size : " << power_of_2_w << ", " << power_of_2_h << " // " << column_amount << " columns" << std::endl; - + while(amount / column_amount > 30 && column_amount < 10) column_amount++; + + const int power_of_2_w = + pow((double)2, (int)ceil((float)log((double)longest_string * (double)column_amount) / log(2.0))); + const int power_of_2_h = pow((double)2, (int)ceil((float)log((double)y / (double)column_amount) / log(2.0))); + + // std::cout << "bitmap size : " << power_of_2_w << ", " << power_of_2_h << " // " << column_amount << " columns" + // << std::endl; + wxBitmap bmp(power_of_2_w, power_of_2_h); assert(bmp.IsOk()); @@ -475,17 +448,18 @@ void wxGLStringArray::consolidate(wxDC* dc) y = 0; x = 0; - if(font.IsOk()) temp_dc.SetFont(font); - else temp_dc.SetFont(wxSystemSettings::GetFont(wxSYS_SYSTEM_FONT)); - - for(int n=0; n power_of_2_h - average_string_height) // check if we need to switch to next column @@ -498,6 +472,5 @@ void wxGLStringArray::consolidate(wxDC* dc) if(img != NULL) delete img; img = new TextTexture(bmp); - for(int n=0; n Date: Thu, 5 Jan 2017 19:31:28 -0200 Subject: Text bugs fixed --- Project/wxGLString.cpp | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) (limited to 'Project/wxGLString.cpp') diff --git a/Project/wxGLString.cpp b/Project/wxGLString.cpp index 23286bc..07c8c48 100644 --- a/Project/wxGLString.cpp +++ b/Project/wxGLString.cpp @@ -25,7 +25,7 @@ GLuint* loadImage(wxImage* img) // convert it to an alpha channel by the way (not all platforms support transparency in wxDCs // so it's the easiest way to go) GLubyte* bitmapData = img->GetData(); - GLubyte* imageData; + GLubyte* imageData = NULL; int bytesPerPixel = 4; @@ -43,7 +43,7 @@ GLuint* loadImage(wxImage* img) // alpha imageData[(x + y * w) * bytesPerPixel + 3] = 255 - bitmapData[(x + (rev_val - y) * w) * 3]; } // next - } // next + } // next glTexImage2D(GL_TEXTURE_2D, 0, bytesPerPixel, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData); @@ -67,7 +67,7 @@ class TextTexture friend class wxGLStringNumber; private: - GLuint* ID; + GLuint* ID = NULL; protected: GLuint* getID(); @@ -97,7 +97,7 @@ TextGLDrawable::TextGLDrawable(TextTexture* image_arg) xflip = false; yflip = false; - if(image_arg != NULL) + if(image_arg) setImage(image_arg); else image = NULL; @@ -138,7 +138,7 @@ void TextGLDrawable::rotate(int angle) { TextGLDrawable::angle = angle; } void TextGLDrawable::render() const { - assert(image != NULL); + assert(image); glPushMatrix(); glTranslatef(x - w / 2, y - h / 2, 0); @@ -182,7 +182,7 @@ GLuint* TextTexture::getID() { return ID; } TextTexture::~TextTexture() { glDeleteTextures(1, ID); - delete ID; + if(ID) delete ID; // Memory leak? } #if 0 @@ -203,7 +203,10 @@ wxGLString::wxGLString(wxString message) img = NULL; } void wxGLString::operator=(wxString& string) { (*((wxString*)this)) = string; } -void wxGLString::bind() const { glBindTexture(GL_TEXTURE_2D, img->getID()[0]); } +void wxGLString::bind() const +{ + if(img->getID()[0]) glBindTexture(GL_TEXTURE_2D, img->getID()[0]); +} void wxGLString::calculateSize(wxDC* dc, const bool ignore_font /* when from array */) { if(!ignore_font) { @@ -239,7 +242,7 @@ void wxGLString::consolidate(wxDC* dc) temp_dc.DrawText(*this, 0, 0); } - if(img != NULL) delete img; + if(img) delete img; img = new TextTexture(bmp); TextGLDrawable::texw = power_of_2_w; @@ -262,7 +265,7 @@ void wxGLString::render(const double x, const double y) } wxGLString::~wxGLString() { - if(img != NULL) delete img; + if(img) delete img; } #if 0 @@ -397,11 +400,14 @@ wxGLStringArray::wxGLStringArray(const wxString strings_arg[], int amount) } wxGLStringArray::~wxGLStringArray() { - if(img != NULL) delete img; + if(img) delete img; } wxGLString& wxGLStringArray::get(const int id) { return strings[id]; } -void wxGLStringArray::bind() { glBindTexture(GL_TEXTURE_2D, img->getID()[0]); } +void wxGLStringArray::bind() +{ + if(img->getID()[0]) glBindTexture(GL_TEXTURE_2D, img->getID()[0]); +} void wxGLStringArray::addString(wxString string) { strings.push_back(wxGLString(string)); } void wxGLStringArray::setFont(wxFont font) { wxGLStringArray::font = font; } @@ -469,7 +475,7 @@ void wxGLStringArray::consolidate(wxDC* dc) } } } - if(img != NULL) delete img; + if(img) delete img; img = new TextTexture(bmp); for(int n = 0; n < amount; n++) strings[n].setImage(img); -- cgit