Skip to content
This repository has been archived by the owner on Jul 16, 2019. It is now read-only.

Commit

Permalink
Added FontBaker class
Browse files Browse the repository at this point in the history
  • Loading branch information
rds1983 committed Feb 20, 2018
1 parent 9e3e5d4 commit 07f1bf3
Show file tree
Hide file tree
Showing 26 changed files with 242 additions and 41 deletions.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Apache License

Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/

Expand Down Expand Up @@ -198,4 +199,4 @@ Apache License
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
limitations under the License.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ namespace StbSharp.MonoGame.WindowsDX.Test
/// </summary>
public class Game1 : Game
{
private const int FontBitmapWidth = 512;
private const int FontBitmapHeight = 512;
private const int FontBitmapWidth = 1024;
private const int FontBitmapHeight = 1024;

GraphicsDeviceManager _graphics;
SpriteBatch _spriteBatch;
Expand All @@ -23,14 +23,15 @@ public class Game1 : Game
private Texture2D _fontTexture;
private DynamicSoundEffectInstance _effect;
private bool _startedPlaying;
private readonly Dictionary<char, StbTrueType.stbtt_bakedchar> _charData = new Dictionary<char, StbTrueType.stbtt_bakedchar>();
private Dictionary<char, StbTrueType.stbtt_packedchar> _charData = new Dictionary<char, StbTrueType.stbtt_packedchar>();
private Texture2D _white;

public Game1()
{
_graphics = new GraphicsDeviceManager(this)
{
PreferredBackBufferWidth = 1280,
PreferredBackBufferHeight = 800
PreferredBackBufferWidth = 1400,
PreferredBackBufferHeight = 960
};

Content.RootDirectory = "Content";
Expand All @@ -48,6 +49,10 @@ protected override void LoadContent()
_spriteBatch = new SpriteBatch(GraphicsDevice);

// TODO: use this.Content to load your game content here
// Create white texture
_white = new Texture2D(GraphicsDevice, 1, 1);
_white.SetData(new[] {Color.White});

// Load image data into memory
var path = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
path = Path.Combine(path, "image.jpg");
Expand All @@ -58,18 +63,29 @@ protected override void LoadContent()
_image.SetData(image.Data);

// Load ttf
buffer = File.ReadAllBytes("OpenSans/OpenSans-Regular.ttf");
buffer = File.ReadAllBytes("Fonts/DroidSans.ttf");
var buffer2 = File.ReadAllBytes("Fonts/DroidSansJapanese.ttf");

var tempBitmap = new byte[FontBitmapWidth * FontBitmapHeight];
var charData = new StbTrueType.stbtt_bakedchar[256];

StbTrueType.stbtt_BakeFontBitmap(buffer, 0, 48, tempBitmap, FontBitmapWidth, FontBitmapHeight, 32, 96, charData);

var c = 32;
foreach (var cd in charData)
var fontBaker = new FontBaker();
fontBaker.Begin(tempBitmap, FontBitmapWidth, FontBitmapHeight);
fontBaker.Add(buffer, 32, new []
{
_charData[(char)c] = cd;
++c;
}
FontBakerCharacterRange.BasicLatin,
FontBakerCharacterRange.Latin1Supplement,
FontBakerCharacterRange.LatinExtendedA,
FontBakerCharacterRange.Cyrillic,
});

fontBaker.Add(buffer2, 32, new []
{
FontBakerCharacterRange.Hiragana,
FontBakerCharacterRange.Katakana
});

_charData = fontBaker.End();

var rgb = new Color[FontBitmapWidth * FontBitmapHeight];
for (var i = 0; i < tempBitmap.Length; ++i)
Expand Down Expand Up @@ -159,13 +175,21 @@ private void DrawTTFString(SpriteBatch batch, string str, Vector2 location, Colo
for (var i = 0; i < str.Length; ++i)
{
var c = str[i];
StbTrueType.stbtt_bakedchar cd;
StbTrueType.stbtt_packedchar cd;
var pos = location;

if (!_charData.TryGetValue(c, out cd))
{
// Draw red rectangle
batch.Draw(_white,
new Rectangle((int)pos.X + 2, (int)pos.Y - 20, 20, 20),
new Rectangle(0, 0, 1, 1),
Color.Red);

location.X += 24;
continue;
}

var pos = location;
pos.X += cd.xoff;
pos.Y += cd.yoff;

Expand Down Expand Up @@ -193,8 +217,21 @@ protected override void Draw(GameTime gameTime)

DrawTTFString(_spriteBatch, string.Format("Sichem Allocated: {0}", Pointer.AllocatedTotal),
new Vector2(0, _image.Height + 30), Color.White);
DrawTTFString(_spriteBatch, "Hello, World!",
new Vector2(0, _image.Height + 60), Color.White);

DrawTTFString(_spriteBatch, "E: The quick brown fox jumps over the lazy dog",
new Vector2(0, _image.Height + 160), Color.White);
DrawTTFString(_spriteBatch, "G: Üben quält finſteren Jagdſchloß höfliche Bäcker größeren, N: Blåbærsyltetøy",
new Vector2(0, _image.Height + 190), Color.White);
DrawTTFString(_spriteBatch, "D: Høj bly gom vandt fræk sexquiz på wc, S: bäckasiner söka",
new Vector2(0, _image.Height + 220), Color.White);
DrawTTFString(_spriteBatch, "I: Sævör grét áðan því úlpan var ónýt, P: Pchnąć w tę łódź jeża lub osiem skrzyń fig",
new Vector2(0, _image.Height + 250), Color.White);
DrawTTFString(_spriteBatch, "C: Příliš žluťoučký kůň úpěl ďábelské kódy, R: В чащах юга жил-был цитрус? Да, но фальшивый экземпляр! ёъ.",
new Vector2(0, _image.Height + 280), Color.White);
DrawTTFString(_spriteBatch, "S: kilómetros y frío, añoraba, P: vôo à noite, F: Les naïfs ægithales hâtifs pondant à Noël où",
new Vector2(0, _image.Height + 310), Color.White);
DrawTTFString(_spriteBatch, "J: いろはにほへど",
new Vector2(0, _image.Height + 340), Color.White);

_spriteBatch.End();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\MonoGame\v3.0\MonoGame.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\MonoGame\v3.0\MonoGame.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
Expand Down Expand Up @@ -46,34 +45,81 @@
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="MonoGame.Framework">
<HintPath>$(MonoGameInstallDirectory)\MonoGame\v3.0\Assemblies\Windows\MonoGame.Framework.dll</HintPath>
<Reference Include="MonoGame.Framework, Version=3.6.0.1625, Culture=neutral, PublicKeyToken=null">
<HintPath>..\ThirdParty\MonoGame.Framework.DesktopGL\MonoGame.Framework.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Drawing" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<None Include="..\ThirdParty\MonoGame.Framework.DesktopGL\libopenal.1.dylib">
<Link>libopenal.1.dylib</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="..\ThirdParty\MonoGame.Framework.DesktopGL\libSDL2-2.0.0.dylib">
<Link>libSDL2-2.0.0.dylib</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="..\ThirdParty\MonoGame.Framework.DesktopGL\MonoGame.Framework.dll.config">
<Link>MonoGame.Framework.dll.config</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="..\ThirdParty\MonoGame.Framework.DesktopGL\x64\libopenal.so.1">
<Link>x64/libopenal.so.1</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="..\ThirdParty\MonoGame.Framework.DesktopGL\x64\libSDL2-2.0.so.0">
<Link>x64/libSDL2-2.0.so.0</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="..\ThirdParty\MonoGame.Framework.DesktopGL\x64\SDL2.dll">
<Link>x64/SDL2.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="..\ThirdParty\MonoGame.Framework.DesktopGL\x64\soft_oal.dll">
<Link>x64/soft_oal.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="..\ThirdParty\MonoGame.Framework.DesktopGL\x86\libopenal.so.1">
<Link>x86/libopenal.so.1</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="..\ThirdParty\MonoGame.Framework.DesktopGL\x86\libSDL2-2.0.so.0">
<Link>x86/libSDL2-2.0.so.0</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="..\ThirdParty\MonoGame.Framework.DesktopGL\x86\SDL2.dll">
<Link>x86/SDL2.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="..\ThirdParty\MonoGame.Framework.DesktopGL\x86\soft_oal.dll">
<Link>x86/soft_oal.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="Fonts\DroidSans.ttf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="Fonts\DroidSansJapanese.ttf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<Content Include="Fonts\LICENSE.txt" />
<Content Include="Icon.ico" />
<Content Include="image.jpg">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="OpenSans\Apache License.txt" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\StbSharp\StbSharp.csproj">
<Project>{5a331dad-b629-41e7-9131-70e538cf0b42}</Project>
<Name>StbSharp</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="..\..\Testing\TestOggs\Adeste_Fideles.ogg">
<Link>Adeste_Fideles.ogg</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="OpenSans\OpenSans-Regular.ttf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\StbSharp\StbSharp.csproj">
<Project>{5a331dad-b629-41e7-9131-70e538cf0b42}</Project>
<Name>StbSharp</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<dllmap dll="SDL2.dll" os="osx" target="./libSDL2-2.0.0.dylib"/>
<dllmap dll="soft_oal.dll" os="osx" target="./libopenal.1.dylib" />
<dllmap dll="SDL2.dll" os="linux" cpu="x86" target="./x86/libSDL2-2.0.so.0"/>
<dllmap dll="soft_oal.dll" os="linux" cpu="x86" target="./x86/libopenal.so.1" />
<dllmap dll="SDL2.dll" os="linux" cpu="x86-64" target="./x64/libSDL2-2.0.so.0"/>
<dllmap dll="soft_oal.dll" os="linux" cpu="x86-64" target="./x64/libopenal.so.1" />
</configuration>
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
14 changes: 7 additions & 7 deletions StbSharp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Testing", "Testing", "{519A
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StbSharp.ImageConverter", "Samples\StbSharp.ImageConverter\StbSharp.ImageConverter.csproj", "{87A1E8BA-4F87-4BE7-84DD-BECD47E9944B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StbSharp.MonoGame.WindowsDX.Test", "Samples\StbSharp.MonoGame.WindowsDX.Test\StbSharp.MonoGame.WindowsDX.Test.csproj", "{5528B473-7EEA-43EF-8DE2-6338C5F157D2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StbSharp.OggPlayer", "Samples\StbSharp.OggPlayer\StbSharp.OggPlayer.csproj", "{5A2992B2-E042-4DD4-A5DA-B032373A219D}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Stb.Native", "Testing\Stb.Native\Stb.Native.vcxproj", "{EA7B8121-942A-437D-85D5-E4D55B94E88A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StbSharp.Tests", "Testing\StbSharp.Tests\StbSharp.Tests.csproj", "{3E2A8A80-CE3E-4ADE-81FF-518CE804383C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StbSharp.MonoGame.Test", "Samples\StbSharp.MonoGame.Test\StbSharp.MonoGame.Test.csproj", "{5528B473-7EEA-43EF-8DE2-6338C5F157D2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Mixed Platforms = Debug|Mixed Platforms
Expand All @@ -33,10 +33,6 @@ Global
{87A1E8BA-4F87-4BE7-84DD-BECD47E9944B}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{87A1E8BA-4F87-4BE7-84DD-BECD47E9944B}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{87A1E8BA-4F87-4BE7-84DD-BECD47E9944B}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{5528B473-7EEA-43EF-8DE2-6338C5F157D2}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{5528B473-7EEA-43EF-8DE2-6338C5F157D2}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{5528B473-7EEA-43EF-8DE2-6338C5F157D2}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{5528B473-7EEA-43EF-8DE2-6338C5F157D2}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{5A2992B2-E042-4DD4-A5DA-B032373A219D}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{5A2992B2-E042-4DD4-A5DA-B032373A219D}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{5A2992B2-E042-4DD4-A5DA-B032373A219D}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
Expand All @@ -49,15 +45,19 @@ Global
{3E2A8A80-CE3E-4ADE-81FF-518CE804383C}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{3E2A8A80-CE3E-4ADE-81FF-518CE804383C}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{3E2A8A80-CE3E-4ADE-81FF-518CE804383C}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{5528B473-7EEA-43EF-8DE2-6338C5F157D2}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{5528B473-7EEA-43EF-8DE2-6338C5F157D2}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{5528B473-7EEA-43EF-8DE2-6338C5F157D2}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{5528B473-7EEA-43EF-8DE2-6338C5F157D2}.Release|Mixed Platforms.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{87A1E8BA-4F87-4BE7-84DD-BECD47E9944B} = {14A355A8-A827-4D09-B3A4-4C7A0EF983EC}
{5528B473-7EEA-43EF-8DE2-6338C5F157D2} = {14A355A8-A827-4D09-B3A4-4C7A0EF983EC}
{5A2992B2-E042-4DD4-A5DA-B032373A219D} = {14A355A8-A827-4D09-B3A4-4C7A0EF983EC}
{EA7B8121-942A-437D-85D5-E4D55B94E88A} = {519A6B9C-7460-47E9-9030-F30D86D08DA8}
{3E2A8A80-CE3E-4ADE-81FF-518CE804383C} = {519A6B9C-7460-47E9-9030-F30D86D08DA8}
{5528B473-7EEA-43EF-8DE2-6338C5F157D2} = {14A355A8-A827-4D09-B3A4-4C7A0EF983EC}
EndGlobalSection
EndGlobal
Loading

0 comments on commit 07f1bf3

Please sign in to comment.