Fix detached video crashes.

Originally committed to SVN as r4701.
This commit is contained in:
Thomas Goyne 2010-07-23 04:15:36 +00:00
parent 242b1ee47b
commit 926b6152f1
6 changed files with 64 additions and 88 deletions

View file

@ -40,27 +40,30 @@
#include <wx/bitmap.h> #include <wx/bitmap.h>
#include <wx/dcmemory.h> #include <wx/dcmemory.h>
#include <wx/image.h> #include <wx/image.h>
#include <algorithm> #include <algorithm>
#endif #endif
#include "gl_text.h" #include "gl_text.h"
#include "utils.h" #include "utils.h"
#undef max
/// @class OpenGLTextGlyph /// @class OpenGLTextGlyph
/// @brief Struct storing the information needed to draw a glyph /// @brief Struct storing the information needed to draw a glyph
class OpenGLTextGlyph { struct OpenGLTextGlyph {
public: wxString str; ///< String containing the glyph(s) this is for
wxString str; /// String containing the glyph(s) this is for int tex; ///< OpenGL texture to draw for this glyph
int tex; /// OpenGL texture to draw for this glyph float x1; ///< Left x coordinate of this glyph in the containing texture
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 x2; /// Right x coordinate of this glyph in the containing texture float y1; ///< Left y 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
float y2; /// Right y coordinate of this glyph in the containing texture int w; ///< Width of the glyph in pixels
int w; /// Width of the glyph in pixels int h; ///< Height 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; void Draw(int x,int y) const;
}; };
@ -68,14 +71,13 @@ public:
/// @class OpenGLTextTexture /// @class OpenGLTextTexture
/// @brief OpenGL texture which stores one or more glyphs as sprites /// @brief OpenGL texture which stores one or more glyphs as sprites
class OpenGLTextTexture { class OpenGLTextTexture {
private: int x; ///< Next x coordinate at which a glyph can be inserted
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 y; /// Next y coordinate at which a glyph can be inserted int nextY; ///< Y coordinate of the next line; tracked due to that lines
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
/// are only as tall as needed to fit the glyphs in them int width; ///< Width of the texture
int width; /// Width of the texture int height; ///< Height of the texture
int height; /// Height of the texture GLuint tex; ///< The texture
GLuint tex; /// The texture
/// Insert the glyph into this texture at the current coordinates /// Insert the glyph into this texture at the current coordinates
void Insert(OpenGLTextGlyph &glyph); void Insert(OpenGLTextGlyph &glyph);
@ -106,12 +108,7 @@ OpenGLText::OpenGLText()
OpenGLText::~OpenGLText() { OpenGLText::~OpenGLText() {
} }
OpenGLText& OpenGLText::GetInstance() { void OpenGLText::SetFont(wxString face,int size,bool bold,bool italics) {
static OpenGLText instance;
return instance;
}
void OpenGLText::DoSetFont(wxString face,int size,bool bold,bool italics) {
// No change required // No change required
if (size == fontSize && face == fontFace && bold == fontBold && italics == fontItalics) return; 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(); glyphs.clear();
} }
void OpenGLText::DoSetColour(wxColour col,float alpha) { void OpenGLText::SetColour(wxColour col,float alpha) {
r = col.Red() / 255.f; r = col.Red() / 255.f;
g = col.Green() / 255.f; g = col.Green() / 255.f;
b = col.Blue() / 255.f; b = col.Blue() / 255.f;
a = alpha; 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_BLEND);
glEnable(GL_TEXTURE_2D); glEnable(GL_TEXTURE_2D);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); 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(); size_t len = text.Length();
lineHeight = 0; lineHeight = 0;
int dx=0,dy=0; int dx=0,dy=0;
@ -222,7 +219,7 @@ OpenGLTextGlyph const& OpenGLText::GetGlyph(int i) {
} }
OpenGLTextGlyph const& OpenGLText::CreateGlyph(int n) { 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 // Insert into some texture
bool ok = false; bool ok = false;
@ -235,7 +232,7 @@ OpenGLTextGlyph const& OpenGLText::CreateGlyph(int n) {
// No texture could fit it, create a new one // No texture could fit it, create a new one
if (!ok) { if (!ok) {
textures.push_back(boost::shared_ptr<OpenGLTextTexture>(new OpenGLTextTexture(glyph))); textures.push_back(std::tr1::shared_ptr<OpenGLTextTexture>(new OpenGLTextTexture(glyph)));
} }
return glyph; return glyph;
@ -245,12 +242,10 @@ OpenGLTextGlyph const& OpenGLText::CreateGlyph(int n) {
/// @param w /// @param w
/// @param h /// @param h
OpenGLTextTexture::OpenGLTextTexture(OpenGLTextGlyph &glyph) { OpenGLTextTexture::OpenGLTextTexture(OpenGLTextGlyph &glyph) {
using std::max;
x = y = nextY = 0; x = y = nextY = 0;
width = max(SmallestPowerOf2(glyph.w), 64); width = std::max(SmallestPowerOf2(glyph.w), 64);
height = max(SmallestPowerOf2(glyph.h), 64); height = std::max(SmallestPowerOf2(glyph.h), 64);
width = height = max(width, height); width = height = std::max(width, height);
tex = 0; tex = 0;
// Generate and bind // Generate and bind
@ -322,7 +317,7 @@ void OpenGLTextTexture::Insert(OpenGLTextGlyph &glyph) {
// Draw text and convert to image // Draw text and convert to image
dc.SetBackground(wxBrush(wxColour(0,0,0))); dc.SetBackground(wxBrush(wxColour(0,0,0)));
dc.Clear(); dc.Clear();
dc.SetFont(OpenGLText::GetFont()); dc.SetFont(glyph.font);
dc.SetTextForeground(wxColour(255,255,255)); dc.SetTextForeground(wxColour(255,255,255));
dc.DrawText(glyph.str,0,0); dc.DrawText(glyph.str,0,0);
wxImage img = bmp.ConvertToImage(); wxImage img = bmp.ConvertToImage();
@ -366,13 +361,14 @@ void OpenGLTextGlyph::Draw(int x,int y) const {
} }
/// @brief DOCME /// @brief DOCME
OpenGLTextGlyph::OpenGLTextGlyph(int value) OpenGLTextGlyph::OpenGLTextGlyph(int value, wxFont const& font)
: str(wxChar(value)) : str(wxChar(value))
, font(font)
{ {
wxCoord desc,lead; wxCoord desc,lead;
wxBitmap tempBmp(32, 32, 24); wxBitmap tempBmp(32, 32, 24);
wxMemoryDC dc(tempBmp); wxMemoryDC dc(tempBmp);
dc.SetFont(OpenGLText::GetFont()); dc.SetFont(font);
dc.GetTextExtent(str,&w,&h,&desc,&lead); dc.GetTextExtent(str,&w,&h,&desc,&lead);
} }

View file

@ -36,8 +36,8 @@
#ifndef AGI_PRE #ifndef AGI_PRE
#include <map> #include <map>
#include <tr1/memory>
#include <vector> #include <vector>
#include "boost/shared_ptr.hpp"
#include <wx/colour.h> #include <wx/colour.h>
#include <wx/font.h> #include <wx/font.h>
@ -49,7 +49,7 @@
#include <GL/gl.h> #include <GL/gl.h>
#endif #endif
class OpenGLTextGlyph; struct OpenGLTextGlyph;
class OpenGLTextTexture; class OpenGLTextTexture;
/// DOCME /// DOCME
@ -61,8 +61,6 @@ typedef std::map<int,OpenGLTextGlyph> glyphMap;
/// ///
/// DOCME /// DOCME
class OpenGLText { class OpenGLText {
private:
/// DOCME /// DOCME
/// DOCME /// DOCME
@ -95,10 +93,8 @@ private:
glyphMap glyphs; glyphMap glyphs;
/// DOCME /// DOCME
std::vector<boost::shared_ptr<OpenGLTextTexture> > textures; std::vector<std::tr1::shared_ptr<OpenGLTextTexture> > textures;
OpenGLText();
~OpenGLText();
OpenGLText(OpenGLText const&); OpenGLText(OpenGLText const&);
OpenGLText& operator=(OpenGLText const&); OpenGLText& operator=(OpenGLText const&);
@ -109,55 +105,32 @@ private:
/// @brief Create a new glyph /// @brief Create a new glyph
OpenGLTextGlyph const& CreateGlyph(int chr); OpenGLTextGlyph const& CreateGlyph(int chr);
/// @brief Get the singleton OpenGLText instance void DrawString(const wxString &text,int x,int y);
static OpenGLText& GetInstance(); public:
/// @brief Get the currently active font
wxFont GetFont() const { return font; }
/// @brief Set the currently active font /// @brief Set the currently active font
/// @param face Name of the desired font /// @param face Name of the desired font
/// @param size Size in points of the desired font /// @param size Size in points of the desired font
/// @param bold Should the font be bold? /// @param bold Should the font be bold?
/// @param italics Should the font be italic? /// @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 /// @brief Set the text color
/// @param col Color /// @param col Color
/// @param alpha Alpha value from 0.f-1.f /// @param alpha Alpha value from 0.f-1.f
void DoSetColour(wxColour col,float alpha); void SetColour(wxColour col,float alpha);
/// @brief Print a string onscreen /// @brief Print a string on screen
/// @param text String to print /// @param text String to print
/// @param x x coordinate /// @param x x coordinate
/// @param y y coordinate /// @param y y coordinate
void DoPrint(const wxString &text,int x,int y); void Print(const wxString &text,int x,int y);
void DrawString(const wxString &text,int x,int y);
/// @brief Get the extents of a string printed with the current font in pixels /// @brief Get the extents of a string printed with the current font in pixels
/// @param text String to get extends of /// @param text String to get extends of
/// @param[out] w Width /// @param[out] w Width
/// @param[out] h Height /// @param[out] h Height
void DoGetExtent(const wxString &text,int &w,int &h); void GetExtent(const wxString &text,int &w,int &h);
public: OpenGLText();
/// @brief Get the currently active font ~OpenGLText();
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); }
}; };

View file

@ -226,13 +226,13 @@ void VideoContext::UpdateDisplays(bool full, bool seek) {
for (std::list<VideoDisplay*>::iterator cur=displayList.begin();cur!=displayList.end();cur++) { for (std::list<VideoDisplay*>::iterator cur=displayList.begin();cur!=displayList.end();cur++) {
VideoDisplay *display = *cur; VideoDisplay *display = *cur;
if (!seek) {
display->Refresh();
}
if (full) { if (full) {
display->UpdateSize(); display->UpdateSize();
display->SetFrameRange(0,GetLength()-1); display->SetFrameRange(0,GetLength()-1);
} }
if (!seek) {
display->Refresh();
}
if (seek || full) { if (seek || full) {
display->SetFrame(GetFrameN()); display->SetFrame(GetFrameN());
} }

View file

@ -437,7 +437,7 @@ void VideoDisplay::UpdateSize() {
video.w = w; video.w = w;
video.h = h; video.h = h;
tool->Refresh(); if (tool.get()) tool->Refresh();
wxGLCanvas::Refresh(false); wxGLCanvas::Refresh(false);
} }

View file

@ -44,6 +44,7 @@
VisualToolCross::VisualToolCross(VideoDisplay *parent, VideoState const& video, wxToolBar *) VisualToolCross::VisualToolCross(VideoDisplay *parent, VideoState const& video, wxToolBar *)
: VisualTool<VisualDraggableFeature>(parent, video) : VisualTool<VisualDraggableFeature>(parent, video)
, glText(new OpenGLText)
{ {
} }
@ -100,9 +101,9 @@ void VisualToolCross::Draw() {
wxString mouseText = wxString::Format(L"%i,%i", tx, ty); wxString mouseText = wxString::Format(L"%i,%i", tx, ty);
int tw,th; int tw,th;
OpenGLText::SetFont(L"Verdana", 12, true); glText->SetFont(L"Verdana", 12, true, false);
OpenGLText::SetColour(wxColour(255, 255, 255)); glText->SetColour(wxColour(255, 255, 255), 1.f);
OpenGLText::GetExtent(mouseText, tw, th); glText->GetExtent(mouseText, tw, th);
// Calculate draw position // Calculate draw position
int dx = video.x; int dx = video.x;
@ -117,5 +118,5 @@ void VisualToolCross::Draw() {
else dy -= th + 3; else dy -= th + 3;
// Draw text // Draw text
OpenGLText::Print(mouseText, dx, dy); glText->Print(mouseText, dx, dy);
} }

View file

@ -34,9 +34,14 @@
/// @ingroup visual_ts /// @ingroup visual_ts
/// ///
#ifndef AGI_PRE
#include <tr1/memory>
#endif
#include "visual_feature.h" #include "visual_feature.h"
#include "visual_tool.h" #include "visual_tool.h"
class OpenGLText;
/// DOCME /// DOCME
/// @class VisualToolCross /// @class VisualToolCross
@ -46,6 +51,7 @@
class VisualToolCross : public VisualTool<VisualDraggableFeature> { class VisualToolCross : public VisualTool<VisualDraggableFeature> {
bool Update(); bool Update();
void Draw(); void Draw();
std::tr1::shared_ptr<OpenGLText> glText;
public: public:
VisualToolCross(VideoDisplay *parent, VideoState const& video, wxToolBar *); VisualToolCross(VideoDisplay *parent, VideoState const& video, wxToolBar *);
}; };