{"id":646,"date":"2013-11-22T23:40:28","date_gmt":"2013-11-22T22:40:28","guid":{"rendered":"http:\/\/labalec.fr\/erwan\/?p=646"},"modified":"2013-11-22T23:40:28","modified_gmt":"2013-11-22T22:40:28","slug":"vb-net-and-xna-article-10-a-simple-progressbar","status":"publish","type":"post","link":"https:\/\/labalec.fr\/erwan\/?p=646","title":{"rendered":"VB.Net and XNA : Article 10 \u2013 A simple progressbar"},"content":{"rendered":"<p>Sometimes you need a simple progressbar in your XNA game to display game points, life, etc.<\/p>\n<p>Lets do it quick and simple with rectangles only:<br \/>\n-One gray rectangle for the fix bar, and one red rectangle for the moving bar (use +\/- keys).<br \/>\n-a very simple math formula to draw the current value against the width of the bar : progressbar_width * progressbar_value \/ progressbar_max<\/p>\n<p>The sourcecode : <a href=\"https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/XNA_DEMO_15.zip\">Here<\/a><\/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    Dim spritebatch As SpriteBatch<br \/>\n    Dim dummyTexture As Texture2D<br \/>\n    Dim progressbar_max = 100<br \/>\n    Dim progressbar_value As Integer = progressbar_max<br \/>\n    Dim progressbar_width As Integer = 250<br \/>\n    Dim font As SpriteFont<\/p>\n<p>    Public Sub New()<br \/>\n        graphics = New GraphicsDeviceManager(Me)<br \/>\n        Window.Title = \"Test\"<br \/>\n        graphics.PreferredBackBufferWidth = 400<br \/>\n        graphics.PreferredBackBufferHeight = 200<br \/>\n    End Sub<\/p>\n<p>    Protected Overrides Sub Initialize()<br \/>\n        MyBase.Initialize()<br \/>\n    End Sub<br \/>\n    Protected Overrides Sub LoadContent()<br \/>\n        spritebatch = New SpriteBatch(GraphicsDevice)<br \/>\n        dummyTexture = New Texture2D(GraphicsDevice, 1, 1)<br \/>\n        Dim c(0) As Color<br \/>\n        c(0) = Color.White<br \/>\n        dummyTexture.SetData(Of Color)(c)<br \/>\n        '<br \/>\n        font = Content.Load(Of SpriteFont)(\"xnb\\myfont\")<br \/>\n        MyBase.LoadContent()<br \/>\n    End Sub<br \/>\n    Protected Overrides Sub UnloadContent()<br \/>\n        MyBase.UnloadContent()<br \/>\n    End Sub<\/p>\n<p>    Protected Overrides Sub Update(ByVal gameTime As Microsoft.Xna.Framework.GameTime)<br \/>\n        If Keyboard.GetState.IsKeyDown(Keys.Add) Then progressbar_value += 1<br \/>\n        If Keyboard.GetState.IsKeyDown(Keys.Subtract) Then progressbar_value -= 1<br \/>\n        If progressbar_value >= progressbar_max Then progressbar_value = progressbar_max<br \/>\n        If progressbar_value <= 0 Then progressbar_value = 0\n        MyBase.Update(gameTime)\n    End Sub\n\n    \n    Protected Overrides Sub Draw(ByVal gameTime As Microsoft.Xna.Framework.GameTime)\n        GraphicsDevice.Clear(Color.Blue)\n        spritebatch.Begin()\n        spritebatch.DrawString(font, progressbar_value, New Vector2(10, 10), Color.White)\n        spritebatch.Draw(dummyTexture, New Rectangle(100, 10, progressbar_width, 10), Color.Gray)\n        spritebatch.Draw(dummyTexture, New Rectangle(100, 10, progressbar_width * progressbar_value \/ progressbar_max, 10), Color.Red)\n        spritebatch.End()\n        MyBase.Draw(gameTime)\n    End Sub\n\nEnd Class\n<\/code><\/p>\n<div style=\"width: 695px;\" class=\"wp-video\"><video class=\"wp-video-shortcode\" id=\"video-646-1\" width=\"695\" height=\"348\" preload=\"metadata\" controls=\"controls\"><source type=\"video\/mp4\" src=\"https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/xna10.mp4?_=1\" \/><a href=\"https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/xna10.mp4\">https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/xna10.mp4<\/a><\/video><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes you need a simple progressbar in your XNA game to display game points, life, etc. Lets do it quick and simple with rectangles only: -One gray rectangle for the fix bar, and one red rectangle for the moving bar (use +\/- keys). -a very simple math formula to draw the current value against the <a href='https:\/\/labalec.fr\/erwan\/?p=646' 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-646","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\/646","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=646"}],"version-history":[{"count":1,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=\/wp\/v2\/posts\/646\/revisions"}],"predecessor-version":[{"id":649,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=\/wp\/v2\/posts\/646\/revisions\/649"}],"wp:attachment":[{"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=646"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=646"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=646"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}