Skip to content

Commit

Permalink
Text caching (not really)
Browse files Browse the repository at this point in the history
In this commit I removed creating new instance of Text class everytime
when text was drawn. Now, when drawing begins, new instance of Text
class is created, and then drawing text is just changing properties of
it and at end of drawing this text is disposed. Simple enought, and I
think it speeded up things a bit. Also, removed empty try of getting
font kerning, what was not used anywhere in MeasureText sub.
  • Loading branch information
deathbeam committed Apr 27, 2014
1 parent c9897ce commit 4120a84
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
13 changes: 13 additions & 0 deletions Gwen.SFML.userprefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Properties>
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" />
<MonoDevelop.Ide.Workbench ActiveDocument="Gwen.SFML\Renderer.cs">
<Files>
<File FileName="Gwen.SFML\Input.cs" Line="1" Column="1" />
<File FileName="Gwen.SFML\Renderer.cs" Line="142" Column="22" />
</Files>
</MonoDevelop.Ide.Workbench>
<MonoDevelop.Ide.DebuggingService.Breakpoints>
<BreakpointStore />
</MonoDevelop.Ide.DebuggingService.Breakpoints>
<MonoDevelop.Ide.DebuggingService.PinnedWatches />
</Properties>
23 changes: 10 additions & 13 deletions Gwen.SFML/Renderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ public class GuiRenderer : Base, ICacheToTexture
private RenderTarget _target;
private Color _color;
private Vector2f _viewScale;
//private SFMLTexture m_LastSampled;
//private Image m_SampleCache;
private RenderStates _renderState;
private uint _cacheSize;
private readonly Vertex[] _vertices;

//Some simple caching for text to draw
private Text _str;

public const uint CacheSize = 1024;

/// <summary>
Expand All @@ -46,11 +47,13 @@ public override void Begin()
var scaled = _target.MapPixelToCoords(new Vector2i(port.Width, port.Height));
_viewScale.X = (port.Width/scaled.X)*Scale;
_viewScale.Y = (port.Height/scaled.Y)*Scale;
_str = new Text ();
}

public override void End()
{
FlushCache();
_str.Dispose ();
base.End();
}

Expand Down Expand Up @@ -124,7 +127,6 @@ public override void FreeFont(Font font)
/// </returns>
public override Point MeasureText(Font font, string text)
{
// todo: cache results, this is slow
var sfFont = font.RendererData as SFML.Graphics.Font;

// If the font doesn't exist, or the font size should be changed
Expand All @@ -148,7 +150,6 @@ public override Point MeasureText(Font font, string text)

foreach (var cur in text)
{
sfFont.GetKerning(prev, cur, (uint)font.RealSize);
prev = cur;
if (cur == '\n' || cur == '\v')
continue;
Expand Down Expand Up @@ -177,15 +178,11 @@ public override void RenderText(Font font, Point pos, string text)
text += '\0';
}

var sfText = new Text(text, sfFont)
{
Font = sfFont,
Position = new Vector2f(pos.X, pos.Y),
CharacterSize = (uint) font.RealSize,
Color = _color
};
_target.Draw(sfText);
sfText.Dispose();
_str.Font = sfFont;
_str.Position = new Vector2f (pos.X, pos.Y);
_str.CharacterSize = (uint) font.RealSize;
_str.Color = _color;
_target.Draw(_str);
}

public override void DrawLine(int x1, int y1, int x2, int y2)
Expand Down

0 comments on commit 4120a84

Please sign in to comment.