diff options
author | Thales1330 <thaleslima.ufu@gmail.com> | 2017-01-03 19:03:12 -0200 |
---|---|---|
committer | Thales1330 <thaleslima.ufu@gmail.com> | 2017-01-03 19:03:12 -0200 |
commit | a1932eec23589ed67d824873b37de3e83a09cd24 (patch) | |
tree | db2bed8e7a9c87ad6115a1b8a6a347cad6753b06 /Project/wxGLString.cpp | |
parent | 2c0b51586b3b2243ae0fdcd653139818e4b4e23e (diff) | |
download | PSP.git-a1932eec23589ed67d824873b37de3e83a09cd24.tar.gz PSP.git-a1932eec23589ed67d824873b37de3e83a09cd24.tar.xz PSP.git-a1932eec23589ed67d824873b37de3e83a09cd24.zip |
Text element optimizated, but buggy
Element/parent search buggy
Diffstat (limited to 'Project/wxGLString.cpp')
-rw-r--r-- | Project/wxGLString.cpp | 407 |
1 files changed, 190 insertions, 217 deletions
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; y<h; y++) - { - for(int x=0; x<w; x++) - { - imageData[(x+y*w)*bytesPerPixel+0] = 255; - imageData[(x+y*w)*bytesPerPixel+1] = 255; - imageData[(x+y*w)*bytesPerPixel+2] = 255; + for(int y = 0; y < h; y++) { + for(int x = 0; x < w; x++) { + imageData[(x + y * w) * bytesPerPixel + 0] = 255; + imageData[(x + y * w) * bytesPerPixel + 1] = 255; + imageData[(x + y * w) * bytesPerPixel + 2] = 255; // alpha - imageData[(x+y*w)*bytesPerPixel+3] = 255 - bitmapData[( x+(rev_val-y)*w)*3]; - }//next - }//next + imageData[(x + y * w) * bytesPerPixel + 3] = 255 - bitmapData[(x + (rev_val - y) * w) * 3]; + } // next + } // next - glTexImage2D(GL_TEXTURE_2D, 0, bytesPerPixel, w, h, 0, - GL_RGBA, GL_UNSIGNED_BYTE, imageData); + glTexImage2D(GL_TEXTURE_2D, 0, bytesPerPixel, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData); free(imageData); - // set texture parameters as you wish - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + // set texture parameters as you wish + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - // GL_CLAMP_TO_EDGE - glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); - glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); - - return ID; + // GL_CLAMP_TO_EDGE + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); + return ID; } - class TextTexture { friend class wxGLString; friend class wxGLStringArray; friend class wxGLStringNumber; + private: GLuint* ID; -protected: +protected: GLuint* getID(); TextTexture(); TextTexture(wxBitmap& bmp); void load(wxImage* img); -public: - +public: ~TextTexture(); - }; #if 0 @@ -94,21 +85,22 @@ public: #pragma mark TextGLDrawable implementation #endif - TextGLDrawable::TextGLDrawable(TextTexture* image_arg) { - x=0; - y=0; - angle=0; + x = 0; + y = 0; + angle = 0; - xscale=1; - yscale=1; + xscale = 1; + yscale = 1; - xflip=false; - yflip=false; + xflip = false; + yflip = false; - if(image_arg!=NULL) setImage(image_arg); - else image=NULL; + if(image_arg != NULL) + setImage(image_arg); + else + image = NULL; tex_coord_x1 = 0; tex_coord_y1 = 1; @@ -118,140 +110,117 @@ TextGLDrawable::TextGLDrawable(TextTexture* image_arg) void TextGLDrawable::setFlip(bool x, bool y) { - xflip=x; - yflip=y; + xflip = x; + yflip = y; } void TextGLDrawable::move(double x, double y) { - TextGLDrawable::x=x; - TextGLDrawable::y=y; + TextGLDrawable::x = x; + TextGLDrawable::y = y; } void TextGLDrawable::scale(float x, float y) { - TextGLDrawable::xscale=x; - TextGLDrawable::yscale=y; + TextGLDrawable::xscale = x; + TextGLDrawable::yscale = y; } void TextGLDrawable::scale(float k) { - TextGLDrawable::xscale=k; - TextGLDrawable::yscale=k; + TextGLDrawable::xscale = k; + TextGLDrawable::yscale = k; } -void TextGLDrawable::setImage(TextTexture* image) -{ - TextGLDrawable::image=image; -} +void TextGLDrawable::setImage(TextTexture* image) { TextGLDrawable::image = image; } -void TextGLDrawable::rotate(int angle) -{ - TextGLDrawable::angle=angle; -} +void TextGLDrawable::rotate(int angle) { TextGLDrawable::angle = angle; } void TextGLDrawable::render() const { - assert(image!=NULL); + assert(image != NULL); glPushMatrix(); - glTranslatef(x - w/2, y - h/2, 0); - if(xscale!=1 || yscale!=1) glScalef(xscale, yscale, 1); - if(angle!=0) glRotatef(angle, 0,0,1); - + glTranslatef(x - w / 2, y - h / 2, 0); + if(xscale != 1 || yscale != 1) glScalef(xscale, yscale, 1); + if(angle != 0) glRotatef(angle, 0, 0, 1); glBegin(GL_QUADS); - glTexCoord2f(xflip? tex_coord_x2 : tex_coord_x1, - yflip? tex_coord_y2 : tex_coord_y1); - glVertex2f( 0, 0 ); + glTexCoord2f(xflip ? tex_coord_x2 : tex_coord_x1, yflip ? tex_coord_y2 : tex_coord_y1); + glVertex2f(0, 0); - glTexCoord2f(xflip? tex_coord_x1 : tex_coord_x2, - yflip? tex_coord_y2 : tex_coord_y1); - glVertex2f( w, 0 ); + glTexCoord2f(xflip ? tex_coord_x1 : tex_coord_x2, yflip ? tex_coord_y2 : tex_coord_y1); + glVertex2f(w, 0); - glTexCoord2f(xflip? tex_coord_x1 : tex_coord_x2, - yflip? tex_coord_y1 : tex_coord_y2); - glVertex2f( w, h ); + glTexCoord2f(xflip ? tex_coord_x1 : tex_coord_x2, yflip ? tex_coord_y1 : tex_coord_y2); + glVertex2f(w, h); - glTexCoord2f(xflip? tex_coord_x2 : tex_coord_x1, - yflip? tex_coord_y1 : tex_coord_y2); - glVertex2f( 0, h ); + glTexCoord2f(xflip ? tex_coord_x2 : tex_coord_x1, yflip ? tex_coord_y1 : tex_coord_y2); + glVertex2f(0, h); glEnd(); glPopMatrix(); } - #if 0 #pragma mark - #pragma mark TextTexture implementation #endif -TextTexture::TextTexture() -{ -} +TextTexture::TextTexture() {} TextTexture::TextTexture(wxBitmap& bmp) { wxImage img = bmp.ConvertToImage(); load(&img); } -void TextTexture::load(wxImage* img) -{ - ID=loadImage(img); -} +void TextTexture::load(wxImage* img) { ID = loadImage(img); } -GLuint* TextTexture::getID() -{ - return ID; -} +GLuint* TextTexture::getID() { return ID; } TextTexture::~TextTexture() { - //glDeleteTextures (1, ID); - glDeleteTextures (1, ID); + glDeleteTextures(1, ID); delete ID; } - #if 0 #pragma mark - #pragma mark wxGLString implementation #endif -wxGLString::wxGLString() : wxString(wxT("")), TextGLDrawable() +wxGLString::wxGLString() + : wxString(wxT("")) + , TextGLDrawable() { img = NULL; } -wxGLString::wxGLString(wxString message) : wxString(message), TextGLDrawable() +wxGLString::wxGLString(wxString message) + : wxString(message) + , TextGLDrawable() { img = NULL; } -void wxGLString::operator=(wxString& string) -{ - (*((wxString*)this))=string; -} -void wxGLString::bind() const -{ - glBindTexture(GL_TEXTURE_2D, img->getID()[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; c<char_amount; c++) - { + for(int c = 0; c < char_amount; c++) { int charid = -1; char schar = s[c]; - switch(schar) - { - case '0' : charid = 0; break; - case '1' : charid = 1; break; - case '2' : charid = 2; break; - case '3' : charid = 3; break; - case '4' : charid = 4; break; - case '5' : charid = 5; break; - case '6' : charid = 6; break; - case '7' : charid = 7; break; - case '8' : charid = 8; break; - case '9' : charid = 9; break; - case '.' : - case ',' : charid = 10; break; - case '-' : charid = 11; break; - default: printf("Warning: character %c unexpected in number!\n", schar); continue; + switch(schar) { + case '0': + charid = 0; + break; + case '1': + charid = 1; + break; + case '2': + charid = 2; + break; + case '3': + charid = 3; + break; + case '4': + charid = 4; + break; + case '5': + charid = 5; + break; + case '6': + charid = 6; + break; + case '7': + charid = 7; + break; + case '8': + charid = 8; + break; + case '9': + charid = 9; + break; + case '.': + case ',': + charid = 10; + break; + case '-': + charid = 11; + break; + default: + printf("Warning: character %c unexpected in number!\n", schar); + continue; } - assert( charid != -1 ); + assert(charid != -1); TextGLDrawable::tex_coord_x1 = (float)number_location[charid] / (float)full_string_w; - TextGLDrawable::tex_coord_x2 = (float)(number_location[charid+1]-space_w) / (float)full_string_w; - - const int char_width = number_location[charid+1] - number_location[charid] - space_w; - TextGLDrawable::w = char_width; + TextGLDrawable::tex_coord_x2 = (float)(number_location[charid + 1] - space_w) / (float)full_string_w; + const int char_width = number_location[charid + 1] - number_location[charid] - space_w; + TextGLDrawable::w = char_width; TextGLDrawable::move(x, y); TextGLDrawable::render(); @@ -393,7 +380,7 @@ void wxGLNumberRenderer::renderNumber(wxString s, double x, double y) x += char_width; } // next - // TextGLDrawable::w = full_string_w; + // TextGLDrawable::w = full_string_w; } #if 0 @@ -401,69 +388,55 @@ void wxGLNumberRenderer::renderNumber(wxString s, double x, double y) #pragma mark wxGLStringArray implementation #endif -wxGLStringArray::wxGLStringArray() -{ - img = NULL; -} +wxGLStringArray::wxGLStringArray() { img = NULL; } wxGLStringArray::wxGLStringArray(const wxString strings_arg[], int amount) { img = NULL; - - for(int n=0; n<amount; n++) - strings.push_back( wxGLString(strings_arg[n]) ); + + for(int n = 0; n < amount; n++) strings.push_back(wxGLString(strings_arg[n])); } wxGLStringArray::~wxGLStringArray() { if(img != NULL) delete img; } -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; -} +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<amount; n++) - { + for(int n = 0; n < amount; n++) { strings[n].calculateSize(dc, true); y += strings[n].h; if(strings[n].w > 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<amount; n++) - { + if(font.IsOk()) + temp_dc.SetFont(font); + else + temp_dc.SetFont(wxSystemSettings::GetFont(wxSYS_SYSTEM_FONT)); + + for(int n = 0; n < amount; n++) { strings[n].consolidateFromArray(&temp_dc, x, y); - strings[n].tex_coord_x1 = (float)x/(float)power_of_2_w; - strings[n].tex_coord_y1 = 1.0 - (float)y/(float)power_of_2_h; - strings[n].tex_coord_x2 = (float)(x+strings[n].w)/(float)power_of_2_w; - strings[n].tex_coord_y2 = 1.0 - (float)(y+strings[n].h)/(float)power_of_2_h; + strings[n].tex_coord_x1 = (float)x / (float)power_of_2_w; + strings[n].tex_coord_y1 = 1.0 - (float)y / (float)power_of_2_h; + strings[n].tex_coord_x2 = (float)(x + strings[n].w) / (float)power_of_2_w; + strings[n].tex_coord_y2 = 1.0 - (float)(y + strings[n].h) / (float)power_of_2_h; y += strings[n].h; if(y > 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<amount; n++) - strings[n].setImage(img); + for(int n = 0; n < amount; n++) strings[n].setImage(img); } |