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

Commit 07f1bf3

Browse files
committed
Added FontBaker class
1 parent 9e3e5d4 commit 07f1bf3

26 files changed

+242
-41
lines changed
Binary file not shown.
Binary file not shown.

Samples/StbSharp.MonoGame.WindowsDX.Test/OpenSans/Apache License.txt renamed to Samples/StbSharp.MonoGame.Test/Fonts/LICENSE.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
Apache License
1+
2+
Apache License
23
Version 2.0, January 2004
34
http://www.apache.org/licenses/
45

@@ -198,4 +199,4 @@ Apache License
198199
distributed under the License is distributed on an "AS IS" BASIS,
199200
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200201
See the License for the specific language governing permissions and
201-
limitations under the License.
202+
limitations under the License.

Samples/StbSharp.MonoGame.WindowsDX.Test/Game1.cs renamed to Samples/StbSharp.MonoGame.Test/Game1.cs

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ namespace StbSharp.MonoGame.WindowsDX.Test
1313
/// </summary>
1414
public class Game1 : Game
1515
{
16-
private const int FontBitmapWidth = 512;
17-
private const int FontBitmapHeight = 512;
16+
private const int FontBitmapWidth = 1024;
17+
private const int FontBitmapHeight = 1024;
1818

1919
GraphicsDeviceManager _graphics;
2020
SpriteBatch _spriteBatch;
@@ -23,14 +23,15 @@ public class Game1 : Game
2323
private Texture2D _fontTexture;
2424
private DynamicSoundEffectInstance _effect;
2525
private bool _startedPlaying;
26-
private readonly Dictionary<char, StbTrueType.stbtt_bakedchar> _charData = new Dictionary<char, StbTrueType.stbtt_bakedchar>();
26+
private Dictionary<char, StbTrueType.stbtt_packedchar> _charData = new Dictionary<char, StbTrueType.stbtt_packedchar>();
27+
private Texture2D _white;
2728

2829
public Game1()
2930
{
3031
_graphics = new GraphicsDeviceManager(this)
3132
{
32-
PreferredBackBufferWidth = 1280,
33-
PreferredBackBufferHeight = 800
33+
PreferredBackBufferWidth = 1400,
34+
PreferredBackBufferHeight = 960
3435
};
3536

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

5051
// TODO: use this.Content to load your game content here
52+
// Create white texture
53+
_white = new Texture2D(GraphicsDevice, 1, 1);
54+
_white.SetData(new[] {Color.White});
55+
5156
// Load image data into memory
5257
var path = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
5358
path = Path.Combine(path, "image.jpg");
@@ -58,18 +63,29 @@ protected override void LoadContent()
5863
_image.SetData(image.Data);
5964

6065
// Load ttf
61-
buffer = File.ReadAllBytes("OpenSans/OpenSans-Regular.ttf");
66+
buffer = File.ReadAllBytes("Fonts/DroidSans.ttf");
67+
var buffer2 = File.ReadAllBytes("Fonts/DroidSansJapanese.ttf");
68+
6269
var tempBitmap = new byte[FontBitmapWidth * FontBitmapHeight];
63-
var charData = new StbTrueType.stbtt_bakedchar[256];
6470

65-
StbTrueType.stbtt_BakeFontBitmap(buffer, 0, 48, tempBitmap, FontBitmapWidth, FontBitmapHeight, 32, 96, charData);
66-
67-
var c = 32;
68-
foreach (var cd in charData)
71+
var fontBaker = new FontBaker();
72+
73+
fontBaker.Begin(tempBitmap, FontBitmapWidth, FontBitmapHeight);
74+
fontBaker.Add(buffer, 32, new []
6975
{
70-
_charData[(char)c] = cd;
71-
++c;
72-
}
76+
FontBakerCharacterRange.BasicLatin,
77+
FontBakerCharacterRange.Latin1Supplement,
78+
FontBakerCharacterRange.LatinExtendedA,
79+
FontBakerCharacterRange.Cyrillic,
80+
});
81+
82+
fontBaker.Add(buffer2, 32, new []
83+
{
84+
FontBakerCharacterRange.Hiragana,
85+
FontBakerCharacterRange.Katakana
86+
});
87+
88+
_charData = fontBaker.End();
7389

7490
var rgb = new Color[FontBitmapWidth * FontBitmapHeight];
7591
for (var i = 0; i < tempBitmap.Length; ++i)
@@ -159,13 +175,21 @@ private void DrawTTFString(SpriteBatch batch, string str, Vector2 location, Colo
159175
for (var i = 0; i < str.Length; ++i)
160176
{
161177
var c = str[i];
162-
StbTrueType.stbtt_bakedchar cd;
178+
StbTrueType.stbtt_packedchar cd;
179+
var pos = location;
180+
163181
if (!_charData.TryGetValue(c, out cd))
164182
{
183+
// Draw red rectangle
184+
batch.Draw(_white,
185+
new Rectangle((int)pos.X + 2, (int)pos.Y - 20, 20, 20),
186+
new Rectangle(0, 0, 1, 1),
187+
Color.Red);
188+
189+
location.X += 24;
165190
continue;
166191
}
167192

168-
var pos = location;
169193
pos.X += cd.xoff;
170194
pos.Y += cd.yoff;
171195

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

194218
DrawTTFString(_spriteBatch, string.Format("Sichem Allocated: {0}", Pointer.AllocatedTotal),
195219
new Vector2(0, _image.Height + 30), Color.White);
196-
DrawTTFString(_spriteBatch, "Hello, World!",
197-
new Vector2(0, _image.Height + 60), Color.White);
220+
221+
DrawTTFString(_spriteBatch, "E: The quick brown fox jumps over the lazy dog",
222+
new Vector2(0, _image.Height + 160), Color.White);
223+
DrawTTFString(_spriteBatch, "G: Üben quält finſteren Jagdſchloß höfliche Bäcker größeren, N: Blåbærsyltetøy",
224+
new Vector2(0, _image.Height + 190), Color.White);
225+
DrawTTFString(_spriteBatch, "D: Høj bly gom vandt fræk sexquiz på wc, S: bäckasiner söka",
226+
new Vector2(0, _image.Height + 220), Color.White);
227+
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",
228+
new Vector2(0, _image.Height + 250), Color.White);
229+
DrawTTFString(_spriteBatch, "C: Příliš žluťoučký kůň úpěl ďábelské kódy, R: В чащах юга жил-был цитрус? Да, но фальшивый экземпляр! ёъ.",
230+
new Vector2(0, _image.Height + 280), Color.White);
231+
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ù",
232+
new Vector2(0, _image.Height + 310), Color.White);
233+
DrawTTFString(_spriteBatch, "J: いろはにほへど",
234+
new Vector2(0, _image.Height + 340), Color.White);
198235

199236
_spriteBatch.End();
200237

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3-
<Import Project="$(MSBuildExtensionsPath)\MonoGame\v3.0\MonoGame.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\MonoGame\v3.0\MonoGame.Common.props')" />
43
<PropertyGroup>
54
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
65
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
@@ -46,34 +45,81 @@
4645
<Compile Include="Properties\AssemblyInfo.cs" />
4746
</ItemGroup>
4847
<ItemGroup>
49-
<Reference Include="MonoGame.Framework">
50-
<HintPath>$(MonoGameInstallDirectory)\MonoGame\v3.0\Assemblies\Windows\MonoGame.Framework.dll</HintPath>
48+
<Reference Include="MonoGame.Framework, Version=3.6.0.1625, Culture=neutral, PublicKeyToken=null">
49+
<HintPath>..\ThirdParty\MonoGame.Framework.DesktopGL\MonoGame.Framework.dll</HintPath>
5150
</Reference>
5251
<Reference Include="System" />
5352
<Reference Include="System.Drawing" />
5453
<Reference Include="System.Xml" />
5554
</ItemGroup>
5655
<ItemGroup>
56+
<None Include="..\ThirdParty\MonoGame.Framework.DesktopGL\libopenal.1.dylib">
57+
<Link>libopenal.1.dylib</Link>
58+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
59+
</None>
60+
<None Include="..\ThirdParty\MonoGame.Framework.DesktopGL\libSDL2-2.0.0.dylib">
61+
<Link>libSDL2-2.0.0.dylib</Link>
62+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
63+
</None>
64+
<None Include="..\ThirdParty\MonoGame.Framework.DesktopGL\MonoGame.Framework.dll.config">
65+
<Link>MonoGame.Framework.dll.config</Link>
66+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
67+
</None>
68+
<None Include="..\ThirdParty\MonoGame.Framework.DesktopGL\x64\libopenal.so.1">
69+
<Link>x64/libopenal.so.1</Link>
70+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
71+
</None>
72+
<None Include="..\ThirdParty\MonoGame.Framework.DesktopGL\x64\libSDL2-2.0.so.0">
73+
<Link>x64/libSDL2-2.0.so.0</Link>
74+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
75+
</None>
76+
<None Include="..\ThirdParty\MonoGame.Framework.DesktopGL\x64\SDL2.dll">
77+
<Link>x64/SDL2.dll</Link>
78+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
79+
</None>
80+
<None Include="..\ThirdParty\MonoGame.Framework.DesktopGL\x64\soft_oal.dll">
81+
<Link>x64/soft_oal.dll</Link>
82+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
83+
</None>
84+
<None Include="..\ThirdParty\MonoGame.Framework.DesktopGL\x86\libopenal.so.1">
85+
<Link>x86/libopenal.so.1</Link>
86+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
87+
</None>
88+
<None Include="..\ThirdParty\MonoGame.Framework.DesktopGL\x86\libSDL2-2.0.so.0">
89+
<Link>x86/libSDL2-2.0.so.0</Link>
90+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
91+
</None>
92+
<None Include="..\ThirdParty\MonoGame.Framework.DesktopGL\x86\SDL2.dll">
93+
<Link>x86/SDL2.dll</Link>
94+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
95+
</None>
96+
<None Include="..\ThirdParty\MonoGame.Framework.DesktopGL\x86\soft_oal.dll">
97+
<Link>x86/soft_oal.dll</Link>
98+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
99+
</None>
100+
<None Include="Fonts\DroidSans.ttf">
101+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
102+
</None>
103+
<None Include="Fonts\DroidSansJapanese.ttf">
104+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
105+
</None>
106+
<Content Include="Fonts\LICENSE.txt" />
57107
<Content Include="Icon.ico" />
58108
<Content Include="image.jpg">
59109
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
60110
</Content>
61-
<Content Include="OpenSans\Apache License.txt" />
62-
</ItemGroup>
63-
<ItemGroup>
64-
<ProjectReference Include="..\..\StbSharp\StbSharp.csproj">
65-
<Project>{5a331dad-b629-41e7-9131-70e538cf0b42}</Project>
66-
<Name>StbSharp</Name>
67-
</ProjectReference>
68111
</ItemGroup>
69112
<ItemGroup>
70113
<None Include="..\..\Testing\TestOggs\Adeste_Fideles.ogg">
71114
<Link>Adeste_Fideles.ogg</Link>
72115
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
73116
</None>
74-
<None Include="OpenSans\OpenSans-Regular.ttf">
75-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
76-
</None>
117+
</ItemGroup>
118+
<ItemGroup>
119+
<ProjectReference Include="..\..\StbSharp\StbSharp.csproj">
120+
<Project>{5a331dad-b629-41e7-9131-70e538cf0b42}</Project>
121+
<Name>StbSharp</Name>
122+
</ProjectReference>
77123
</ItemGroup>
78124
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
79125
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Binary file not shown.
Binary file not shown.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<dllmap dll="SDL2.dll" os="osx" target="./libSDL2-2.0.0.dylib"/>
4+
<dllmap dll="soft_oal.dll" os="osx" target="./libopenal.1.dylib" />
5+
<dllmap dll="SDL2.dll" os="linux" cpu="x86" target="./x86/libSDL2-2.0.so.0"/>
6+
<dllmap dll="soft_oal.dll" os="linux" cpu="x86" target="./x86/libopenal.so.1" />
7+
<dllmap dll="SDL2.dll" os="linux" cpu="x86-64" target="./x64/libSDL2-2.0.so.0"/>
8+
<dllmap dll="soft_oal.dll" os="linux" cpu="x86-64" target="./x64/libopenal.so.1" />
9+
</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.

StbSharp.sln

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Testing", "Testing", "{519A
1111
EndProject
1212
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StbSharp.ImageConverter", "Samples\StbSharp.ImageConverter\StbSharp.ImageConverter.csproj", "{87A1E8BA-4F87-4BE7-84DD-BECD47E9944B}"
1313
EndProject
14-
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}"
15-
EndProject
1614
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StbSharp.OggPlayer", "Samples\StbSharp.OggPlayer\StbSharp.OggPlayer.csproj", "{5A2992B2-E042-4DD4-A5DA-B032373A219D}"
1715
EndProject
1816
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Stb.Native", "Testing\Stb.Native\Stb.Native.vcxproj", "{EA7B8121-942A-437D-85D5-E4D55B94E88A}"
1917
EndProject
2018
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StbSharp.Tests", "Testing\StbSharp.Tests\StbSharp.Tests.csproj", "{3E2A8A80-CE3E-4ADE-81FF-518CE804383C}"
2119
EndProject
20+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StbSharp.MonoGame.Test", "Samples\StbSharp.MonoGame.Test\StbSharp.MonoGame.Test.csproj", "{5528B473-7EEA-43EF-8DE2-6338C5F157D2}"
21+
EndProject
2222
Global
2323
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2424
Debug|Mixed Platforms = Debug|Mixed Platforms
@@ -33,10 +33,6 @@ Global
3333
{87A1E8BA-4F87-4BE7-84DD-BECD47E9944B}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
3434
{87A1E8BA-4F87-4BE7-84DD-BECD47E9944B}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
3535
{87A1E8BA-4F87-4BE7-84DD-BECD47E9944B}.Release|Mixed Platforms.Build.0 = Release|Any CPU
36-
{5528B473-7EEA-43EF-8DE2-6338C5F157D2}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
37-
{5528B473-7EEA-43EF-8DE2-6338C5F157D2}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
38-
{5528B473-7EEA-43EF-8DE2-6338C5F157D2}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
39-
{5528B473-7EEA-43EF-8DE2-6338C5F157D2}.Release|Mixed Platforms.Build.0 = Release|Any CPU
4036
{5A2992B2-E042-4DD4-A5DA-B032373A219D}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
4137
{5A2992B2-E042-4DD4-A5DA-B032373A219D}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
4238
{5A2992B2-E042-4DD4-A5DA-B032373A219D}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
@@ -49,15 +45,19 @@ Global
4945
{3E2A8A80-CE3E-4ADE-81FF-518CE804383C}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
5046
{3E2A8A80-CE3E-4ADE-81FF-518CE804383C}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
5147
{3E2A8A80-CE3E-4ADE-81FF-518CE804383C}.Release|Mixed Platforms.Build.0 = Release|Any CPU
48+
{5528B473-7EEA-43EF-8DE2-6338C5F157D2}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
49+
{5528B473-7EEA-43EF-8DE2-6338C5F157D2}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
50+
{5528B473-7EEA-43EF-8DE2-6338C5F157D2}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
51+
{5528B473-7EEA-43EF-8DE2-6338C5F157D2}.Release|Mixed Platforms.Build.0 = Release|Any CPU
5252
EndGlobalSection
5353
GlobalSection(SolutionProperties) = preSolution
5454
HideSolutionNode = FALSE
5555
EndGlobalSection
5656
GlobalSection(NestedProjects) = preSolution
5757
{87A1E8BA-4F87-4BE7-84DD-BECD47E9944B} = {14A355A8-A827-4D09-B3A4-4C7A0EF983EC}
58-
{5528B473-7EEA-43EF-8DE2-6338C5F157D2} = {14A355A8-A827-4D09-B3A4-4C7A0EF983EC}
5958
{5A2992B2-E042-4DD4-A5DA-B032373A219D} = {14A355A8-A827-4D09-B3A4-4C7A0EF983EC}
6059
{EA7B8121-942A-437D-85D5-E4D55B94E88A} = {519A6B9C-7460-47E9-9030-F30D86D08DA8}
6160
{3E2A8A80-CE3E-4ADE-81FF-518CE804383C} = {519A6B9C-7460-47E9-9030-F30D86D08DA8}
61+
{5528B473-7EEA-43EF-8DE2-6338C5F157D2} = {14A355A8-A827-4D09-B3A4-4C7A0EF983EC}
6262
EndGlobalSection
6363
EndGlobal

0 commit comments

Comments
 (0)