{"id":564,"date":"2013-11-03T18:12:19","date_gmt":"2013-11-03T17:12:19","guid":{"rendered":"http:\/\/labalec.fr\/erwan\/?p=564"},"modified":"2013-11-16T14:09:07","modified_gmt":"2013-11-16T13:09:07","slug":"vb-net-and-xna-article-3","status":"publish","type":"post","link":"https:\/\/labalec.fr\/erwan\/?p=564","title":{"rendered":"VB.NET and XNA : Article 3 (draw text)"},"content":{"rendered":"<p>In previous article, we have seen a basic XNA application illustrating how to load a texture and how to move it around.<\/p>\n<p>Lets see now how to write a text : load a spritefont as content, and use the drawstring method.<\/p>\n<p>First lets look at a spritefont (in the zip file).<br \/>\nThis is a simple XML file where you describe the font you want to use : font name, size, etc.<\/p>\n<p>But you cannot use use as is in your vb.net XNA code.<br \/>\nYou need to turn it into a XNA content (XNB file -> myfont.xnb next to exe file).<br \/>\nAs my VS2013 does not manage XNA projects and contents, I use <a href=\"http:\/\/xnacontentcompiler.codeplex.com\/\" target=\"_blank\">XNA Content Compiler<\/a>.<br \/>\nChoose content type, select myfont.spritefont, input the destination folder and press compile !<\/p>\n<p>Attached to this post the vb.net project files. <a href=\"https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/xna_demo_2.zip\">xna_demo_2<\/a><\/p>\n<p>Below the code for a quick review.<\/p>\n<p><code><br \/>\nImports Microsoft.Xna.Framework<br \/>\nImports Microsoft.Xna.Framework.Graphics<br \/>\nImports Microsoft.Xna.Framework.Audio<br \/>\nImports Microsoft.Xna.Framework.Content<br \/>\nImports Microsoft.Xna.Framework.Media<br \/>\nImports Microsoft.Xna.Framework.Input<\/p>\n<p>Public Class Game<br \/>\n    Inherits Microsoft.Xna.Framework.Game<br \/>\n    'Fields in our game graphic manager etc'<br \/>\n    Dim graphics As GraphicsDeviceManager<br \/>\n    '<br \/>\n    Dim SpriteBatch As SpriteBatch<br \/>\n    Dim font1 As SpriteFont<br \/>\n    '<br \/>\n    Public Sub New()<br \/>\n        graphics = New GraphicsDeviceManager(Me)<br \/>\n    End Sub<\/p>\n<p>    Protected Overrides Sub Initialize()<br \/>\n        'TODO: Add your initialization logic here'<br \/>\n        MyBase.Initialize()<br \/>\n    End Sub<br \/>\n    Protected Overrides Sub LoadContent()<br \/>\n        ' TODO: use this.Content to load your game content here'<br \/>\n        MyBase.LoadContent()<br \/>\n        ' Create a new SpriteBatch, which can be used to draw textures.'<br \/>\n        SpriteBatch = New SpriteBatch(GraphicsDevice)<br \/>\n        'lets create a font<br \/>\n        font1 = Content.Load(Of SpriteFont)(\"myfont\")<br \/>\n    End Sub<br \/>\n    Protected Overrides Sub UnloadContent()<br \/>\n        MyBase.UnloadContent()<br \/>\n        'TODO: Unload any non ContentManager content here'<br \/>\n    End Sub<br \/>\n    Protected Overrides Sub Update(ByVal gameTime As Microsoft.Xna.Framework.GameTime)<br \/>\n        'Allows the game to exit'<br \/>\n        If GamePad.GetState(PlayerIndex.One).Buttons.Back = ButtonState.Pressed Then<br \/>\n            Me.Exit()<br \/>\n        End If<br \/>\n        'TODO: Add your update logic here'<br \/>\n        MyBase.Update(gameTime)<br \/>\n    End Sub<br \/>\n    Protected Overrides Sub Draw(ByVal gameTime As Microsoft.Xna.Framework.GameTime)<br \/>\n        GraphicsDevice.Clear(Color.CornflowerBlue)<br \/>\n        'TODO: Add your drawing code here'<br \/>\n        'write text<br \/>\n        SpriteBatch.Begin()<br \/>\n        SpriteBatch.DrawString(font1, \"Ceci est un Test !!!\", New Vector2(20.0F, 20.0F), Color.White)<br \/>\n        SpriteBatch.End()<br \/>\n        '<br \/>\n        MyBase.Draw(gameTime)<br \/>\n    End Sub<br \/>\nEnd Class<br \/>\n<\/code><\/p>\n<p><a href=\"https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/xna03.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/xna03-300x190.png\" alt=\"xna03\" width=\"300\" height=\"190\" class=\"alignnone size-medium wp-image-601\" srcset=\"https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/xna03-300x190.png 300w, https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/xna03.png 816w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In previous article, we have seen a basic XNA application illustrating how to load a texture and how to move it around. Lets see now how to write a text : load a spritefont as content, and use the drawstring method. First lets look at a spritefont (in the zip file). This is a simple <a href='https:\/\/labalec.fr\/erwan\/?p=564' class='excerpt-more'>[&#8230;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[34,35],"tags":[],"class_list":["post-564","post","type-post","status-publish","format-standard","hentry","category-dotnet","category-xna","category-34-id","category-35-id","post-seq-1","post-parity-odd","meta-position-corners","fix"],"_links":{"self":[{"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=\/wp\/v2\/posts\/564","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=564"}],"version-history":[{"count":5,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=\/wp\/v2\/posts\/564\/revisions"}],"predecessor-version":[{"id":602,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=\/wp\/v2\/posts\/564\/revisions\/602"}],"wp:attachment":[{"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=564"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=564"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=564"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}