From 926b6152f103c704d3a5033d3a756c3f06563082 Mon Sep 17 00:00:00 2001 From: Thomas Goyne Date: Fri, 23 Jul 2010 04:15:36 +0000 Subject: [PATCH] Fix detached video crashes. Originally committed to SVN as r4701. --- aegisub/src/gl_text.cpp | 72 +++++++++++++++---------------- aegisub/src/gl_text.h | 57 +++++++----------------- aegisub/src/video_context.cpp | 6 +-- aegisub/src/video_display.cpp | 2 +- aegisub/src/visual_tool_cross.cpp | 9 ++-- aegisub/src/visual_tool_cross.h | 6 +++ 6 files changed, 64 insertions(+), 88 deletions(-) diff --git a/aegisub/src/gl_text.cpp b/aegisub/src/gl_text.cpp index 354a66e0e..b0d90e4d0 100644 --- a/aegisub/src/gl_text.cpp +++ b/aegisub/src/gl_text.cpp @@ -40,27 +40,30 @@ #include #include #include + #include #endif #include "gl_text.h" #include "utils.h" +#undef max + /// @class OpenGLTextGlyph /// @brief Struct storing the information needed to draw a glyph -class OpenGLTextGlyph { -public: - wxString str; /// String containing the glyph(s) this is for - int tex; /// OpenGL texture to draw for this glyph - float x1; /// Left x coordinate of this glyph in the containing texture - float x2; /// Right x coordinate of this glyph in the containing texture - float y1; /// Left y coordinate of this glyph in the containing texture - float y2; /// Right y coordinate of this glyph in the containing texture - int w; /// Width of the glyph in pixels - int h; /// Height of the glyph in pixels +struct OpenGLTextGlyph { + wxString str; ///< String containing the glyph(s) this is for + int tex; ///< OpenGL texture to draw for this glyph + float x1; ///< Left x coordinate of this glyph in the containing texture + float x2; ///< Right x coordinate of this glyph in the containing texture + float y1; ///< Left y coordinate of this glyph in the containing texture + float y2; ///< Right y coordinate of this glyph in the containing texture + int w; ///< Width of the glyph in pixels + int h; ///< Height of the glyph in pixels + wxFont font; ///< Font used for this glyph - OpenGLTextGlyph(int value); + OpenGLTextGlyph(int value, wxFont const& font); void Draw(int x,int y) const; }; @@ -68,14 +71,13 @@ public: /// @class OpenGLTextTexture /// @brief OpenGL texture which stores one or more glyphs as sprites class OpenGLTextTexture { -private: - int x; /// Next x coordinate at which a glyph can be inserted - int y; /// Next y coordinate at which a glyph can be inserted - int nextY; /// Y coordinate of the next line; tracked due to that lines - /// are only as tall as needed to fit the glyphs in them - int width; /// Width of the texture - int height; /// Height of the texture - GLuint tex; /// The texture + int x; ///< Next x coordinate at which a glyph can be inserted + int y; ///< Next y coordinate at which a glyph can be inserted + int nextY; ///< Y coordinate of the next line; tracked due to that lines + ///< are only as tall as needed to fit the glyphs in them + int width; ///< Width of the texture + int height; ///< Height of the texture + GLuint tex; ///< The texture /// Insert the glyph into this texture at the current coordinates void Insert(OpenGLTextGlyph &glyph); @@ -106,12 +108,7 @@ OpenGLText::OpenGLText() OpenGLText::~OpenGLText() { } -OpenGLText& OpenGLText::GetInstance() { - static OpenGLText instance; - return instance; -} - -void OpenGLText::DoSetFont(wxString face,int size,bool bold,bool italics) { +void OpenGLText::SetFont(wxString face,int size,bool bold,bool italics) { // No change required if (size == fontSize && face == fontFace && bold == fontBold && italics == fontItalics) return; @@ -129,14 +126,14 @@ void OpenGLText::DoSetFont(wxString face,int size,bool bold,bool italics) { glyphs.clear(); } -void OpenGLText::DoSetColour(wxColour col,float alpha) { +void OpenGLText::SetColour(wxColour col,float alpha) { r = col.Red() / 255.f; g = col.Green() / 255.f; b = col.Blue() / 255.f; a = alpha; } -void OpenGLText::DoPrint(const wxString &text,int x,int y) { +void OpenGLText::Print(const wxString &text,int x,int y) { glEnable(GL_BLEND); glEnable(GL_TEXTURE_2D); glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); @@ -181,7 +178,7 @@ void OpenGLText::DrawString(const wxString &text,int x,int y) { } } -void OpenGLText::DoGetExtent(wxString const& text, int &w, int &h) { +void OpenGLText::GetExtent(wxString const& text, int &w, int &h) { size_t len = text.Length(); lineHeight = 0; int dx=0,dy=0; @@ -222,7 +219,7 @@ OpenGLTextGlyph const& OpenGLText::GetGlyph(int i) { } OpenGLTextGlyph const& OpenGLText::CreateGlyph(int n) { - OpenGLTextGlyph &glyph = glyphs.insert(std::make_pair(n, OpenGLTextGlyph(n))).first->second; + OpenGLTextGlyph &glyph = glyphs.insert(std::make_pair(n, OpenGLTextGlyph(n, font))).first->second; // Insert into some texture bool ok = false; @@ -235,7 +232,7 @@ OpenGLTextGlyph const& OpenGLText::CreateGlyph(int n) { // No texture could fit it, create a new one if (!ok) { - textures.push_back(boost::shared_ptr(new OpenGLTextTexture(glyph))); + textures.push_back(std::tr1::shared_ptr(new OpenGLTextTexture(glyph))); } return glyph; @@ -245,12 +242,10 @@ OpenGLTextGlyph const& OpenGLText::CreateGlyph(int n) { /// @param w /// @param h OpenGLTextTexture::OpenGLTextTexture(OpenGLTextGlyph &glyph) { - using std::max; - x = y = nextY = 0; - width = max(SmallestPowerOf2(glyph.w), 64); - height = max(SmallestPowerOf2(glyph.h), 64); - width = height = max(width, height); + width = std::max(SmallestPowerOf2(glyph.w), 64); + height = std::max(SmallestPowerOf2(glyph.h), 64); + width = height = std::max(width, height); tex = 0; // Generate and bind @@ -322,7 +317,7 @@ void OpenGLTextTexture::Insert(OpenGLTextGlyph &glyph) { // Draw text and convert to image dc.SetBackground(wxBrush(wxColour(0,0,0))); dc.Clear(); - dc.SetFont(OpenGLText::GetFont()); + dc.SetFont(glyph.font); dc.SetTextForeground(wxColour(255,255,255)); dc.DrawText(glyph.str,0,0); wxImage img = bmp.ConvertToImage(); @@ -366,13 +361,14 @@ void OpenGLTextGlyph::Draw(int x,int y) const { } /// @brief DOCME -OpenGLTextGlyph::OpenGLTextGlyph(int value) +OpenGLTextGlyph::OpenGLTextGlyph(int value, wxFont const& font) : str(wxChar(value)) +, font(font) { wxCoord desc,lead; wxBitmap tempBmp(32, 32, 24); wxMemoryDC dc(tempBmp); - dc.SetFont(OpenGLText::GetFont()); + dc.SetFont(font); dc.GetTextExtent(str,&w,&h,&desc,&lead); } diff --git a/aegisub/src/gl_text.h b/aegisub/src/gl_text.h index 55091fc41..830ae2ca6 100644 --- a/aegisub/src/gl_text.h +++ b/aegisub/src/gl_text.h @@ -36,8 +36,8 @@ #ifndef AGI_PRE #include +#include #include -#include "boost/shared_ptr.hpp" #include #include @@ -49,7 +49,7 @@ #include #endif -class OpenGLTextGlyph; +struct OpenGLTextGlyph; class OpenGLTextTexture; /// DOCME @@ -61,8 +61,6 @@ typedef std::map glyphMap; /// /// DOCME class OpenGLText { -private: - /// DOCME /// DOCME @@ -95,10 +93,8 @@ private: glyphMap glyphs; /// DOCME - std::vector > textures; + std::vector > textures; - OpenGLText(); - ~OpenGLText(); OpenGLText(OpenGLText const&); OpenGLText& operator=(OpenGLText const&); @@ -109,55 +105,32 @@ private: /// @brief Create a new glyph OpenGLTextGlyph const& CreateGlyph(int chr); - /// @brief Get the singleton OpenGLText instance - static OpenGLText& GetInstance(); + void DrawString(const wxString &text,int x,int y); +public: + /// @brief Get the currently active font + wxFont GetFont() const { return font; } + /// @brief Set the currently active font /// @param face Name of the desired font /// @param size Size in points of the desired font /// @param bold Should the font be bold? /// @param italics Should the font be italic? - void DoSetFont(wxString face,int size,bool bold,bool italics); + void SetFont(wxString face,int size,bool bold,bool italics); /// @brief Set the text color /// @param col Color /// @param alpha Alpha value from 0.f-1.f - void DoSetColour(wxColour col,float alpha); - /// @brief Print a string onscreen + void SetColour(wxColour col,float alpha); + /// @brief Print a string on screen /// @param text String to print /// @param x x coordinate /// @param y y coordinate - void DoPrint(const wxString &text,int x,int y); - void DrawString(const wxString &text,int x,int y); + void Print(const wxString &text,int x,int y); /// @brief Get the extents of a string printed with the current font in pixels /// @param text String to get extends of /// @param[out] w Width /// @param[out] h Height - void DoGetExtent(const wxString &text,int &w,int &h); + void GetExtent(const wxString &text,int &w,int &h); -public: - /// @brief Get the currently active font - static wxFont GetFont() { return GetInstance().font; } - - /// @brief Set the currently active font - /// @param face Name of the desired font - /// @param size Size in points of the desired font - /// @param bold Should the font be bold? - /// @param italics Should the font be italic? - static void SetFont(wxString face=_T("Verdana"),int size=10,bool bold=true,bool italics=false) { GetInstance().DoSetFont(face,size,bold,italics); } - - /// @brief Set the text color - /// @param col Color - /// @param alpha Alpha value from 0.f-1.f - static void SetColour(wxColour col,float alpha=1.0f) { GetInstance().DoSetColour(col,alpha); } - - /// @brief Print a string onscreen - /// @param text String to print - /// @param x x coordinate - /// @param y y coordinate - static void Print(const wxString &text,int x,int y) { GetInstance().DoPrint(text,x,y); } - - /// @brief Get the extents of a string printed with the current font in pixels - /// @param text String to get extends of - /// @param[out] w Width - /// @param[out] h Height - static void GetExtent(const wxString &text,int &w,int &h) { GetInstance().DoGetExtent(text,w,h); } + OpenGLText(); + ~OpenGLText(); }; diff --git a/aegisub/src/video_context.cpp b/aegisub/src/video_context.cpp index bf481ca89..152707ba1 100644 --- a/aegisub/src/video_context.cpp +++ b/aegisub/src/video_context.cpp @@ -226,13 +226,13 @@ void VideoContext::UpdateDisplays(bool full, bool seek) { for (std::list::iterator cur=displayList.begin();cur!=displayList.end();cur++) { VideoDisplay *display = *cur; + if (!seek) { + display->Refresh(); + } if (full) { display->UpdateSize(); display->SetFrameRange(0,GetLength()-1); } - if (!seek) { - display->Refresh(); - } if (seek || full) { display->SetFrame(GetFrameN()); } diff --git a/aegisub/src/video_display.cpp b/aegisub/src/video_display.cpp index 3cb5ea18f..62ff9aa8b 100644 --- a/aegisub/src/video_display.cpp +++ b/aegisub/src/video_display.cpp @@ -437,7 +437,7 @@ void VideoDisplay::UpdateSize() { video.w = w; video.h = h; - tool->Refresh(); + if (tool.get()) tool->Refresh(); wxGLCanvas::Refresh(false); } diff --git a/aegisub/src/visual_tool_cross.cpp b/aegisub/src/visual_tool_cross.cpp index a7a8f8a01..30992dbbf 100644 --- a/aegisub/src/visual_tool_cross.cpp +++ b/aegisub/src/visual_tool_cross.cpp @@ -44,6 +44,7 @@ VisualToolCross::VisualToolCross(VideoDisplay *parent, VideoState const& video, wxToolBar *) : VisualTool(parent, video) +, glText(new OpenGLText) { } @@ -100,9 +101,9 @@ void VisualToolCross::Draw() { wxString mouseText = wxString::Format(L"%i,%i", tx, ty); int tw,th; - OpenGLText::SetFont(L"Verdana", 12, true); - OpenGLText::SetColour(wxColour(255, 255, 255)); - OpenGLText::GetExtent(mouseText, tw, th); + glText->SetFont(L"Verdana", 12, true, false); + glText->SetColour(wxColour(255, 255, 255), 1.f); + glText->GetExtent(mouseText, tw, th); // Calculate draw position int dx = video.x; @@ -117,5 +118,5 @@ void VisualToolCross::Draw() { else dy -= th + 3; // Draw text - OpenGLText::Print(mouseText, dx, dy); + glText->Print(mouseText, dx, dy); } diff --git a/aegisub/src/visual_tool_cross.h b/aegisub/src/visual_tool_cross.h index df06f449e..9c4ccaf6e 100644 --- a/aegisub/src/visual_tool_cross.h +++ b/aegisub/src/visual_tool_cross.h @@ -34,9 +34,14 @@ /// @ingroup visual_ts /// +#ifndef AGI_PRE +#include +#endif + #include "visual_feature.h" #include "visual_tool.h" +class OpenGLText; /// DOCME /// @class VisualToolCross @@ -46,6 +51,7 @@ class VisualToolCross : public VisualTool { bool Update(); void Draw(); + std::tr1::shared_ptr glText; public: VisualToolCross(VideoDisplay *parent, VideoState const& video, wxToolBar *); };