#include "wxGLString.h" #ifdef __WXMAC__ #include "OpenGL/gl.h" #else #include #endif #include "wx/wx.h" GLuint* loadImage(wxImage* img) { GLuint* ID=new GLuint[1]; glGenTextures( 1, &ID[0] ); glBindTexture( GL_TEXTURE_2D, *ID ); 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; int bytesPerPixel = 4; int imageSize = w * h * bytesPerPixel; imageData=(GLubyte *)malloc(imageSize); int rev_val=h-1; for(int y=0; ygetID()[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)); } 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)) )); wxBitmap bmp(power_of_2_w, power_of_2_h); assert(bmp.IsOk()); { wxMemoryDC temp_dc(bmp); temp_dc.SetBrush(*wxWHITE_BRUSH); temp_dc.Clear(); if(font.IsOk()) temp_dc.SetFont(font); else temp_dc.SetFont(wxSystemSettings::GetFont(wxSYS_SYSTEM_FONT)); temp_dc.DrawText(*this, 0, 0); } if(img != NULL) delete img; img = new TextTexture(bmp); 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_y1 = 1; TextGLDrawable::setImage(img); } void wxGLString::consolidateFromArray(wxDC* dc, double x, double y) { dc->DrawText(*this, x, y); } void wxGLString::setFont(wxFont font) { wxGLString::font = font; } void wxGLString::render(const double x, const double y) { TextGLDrawable::move(x, y); TextGLDrawable::render(); } 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 . - ") ) { number_location = new int[13]; } 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)); 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[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(); space_w = dc->GetTextExtent(wxT(" ")).GetWidth(); } void wxGLNumberRenderer::renderNumber(int i, double x, double y) { wxString s; s << i; renderNumber(s, x, y); } void wxGLNumberRenderer::renderNumber(float f, double x, double y) { wxString s; s << f; renderNumber(s, x, 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; } void wxGLStringArray::consolidate(wxDC* dc) { int x=0, y=0; 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 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; wxBitmap bmp(power_of_2_w, power_of_2_h); assert(bmp.IsOk()); { wxMemoryDC temp_dc(bmp); temp_dc.SetBrush(*wxWHITE_BRUSH); temp_dc.Clear(); 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 { y = 0; x += longest_string; } } } if(img != NULL) delete img; img = new TextTexture(bmp); for(int n=0; n