{"id":12,"date":"2008-03-02T03:53:00","date_gmt":"2008-03-02T08:53:00","guid":{"rendered":"http:\/\/lukerymarz.com\/alearningexperience\/2008\/03\/displaying-a-mouse-pointer-in-xna.html"},"modified":"2008-03-02T03:53:00","modified_gmt":"2008-03-02T08:53:00","slug":"displaying-a-mouse-pointer-in-xna","status":"publish","type":"post","link":"http:\/\/lukerymarz.com\/alearningexperience\/2008\/03\/displaying-a-mouse-pointer-in-xna.html","title":{"rendered":"Displaying a Mouse Pointer in XNA"},"content":{"rendered":"<p>XNA doesn&#8217;t automatically show you a mouse pointer within the game window.  Solving this is easy.  First, you need a position for your mouse.  You&#8217;ll also need a 2d texture to display as your mouse pointer.  Declare this within your game class:<\/p>\n<p><\/p>\n<div style=\"font-family: Courier New; font-size: 10pt; color: black; background: white;\">\n<p style=\"margin: 0px;\"><span style=\"color: #2b91af;\">Texture2D<\/span> m_mouseTexture;<\/p>\n<p style=\"margin: 0px;\"><span style=\"color: #2b91af;\">Vector2<\/span> m_mousePos;<\/p>\n<\/div>\n<p>Be sure to initialize it to zero. For simplicity, I also generate a simple red square texture to use as the mouse pointer.  I&#8217;m doing all of this in the Initialize() function.  In my (very short) experience with XNA, you&#8217;d want to initialize textures in the LoadContent() function, but this is not loading from a file, so it&#8217;s safe to do it all in the Initialize() function like this:<\/p>\n<p><\/p>\n<div style=\"font-family: Courier New; font-size: 10pt; color: black; background: white;\">\n<p style=\"margin: 0px;\">m_mousePos = <span style=\"color: #2b91af;\">Vector2<\/span>.Zero;<\/p>\n<p style=\"margin: 0px;\">&nbsp;<\/p>\n<p style=\"margin: 0px;\">m_mouseTexture = <span style=\"color: blue;\">new<\/span> <span style=\"color: #2b91af;\">Texture2D<\/span>(graphics.GraphicsDevice, (<span style=\"color: blue;\">int<\/span>)POINTSIZE, (<span style=\"color: blue;\">int<\/span>)POINTSIZE, 1, <span style=\"color: #2b91af;\">TextureUsage<\/span>.None, <span style=\"color: #2b91af;\">SurfaceFormat<\/span>.Color);<\/p>\n<p style=\"margin: 0px;\">&nbsp;<\/p>\n<p style=\"margin: 0px;\"><span style=\"color: #2b91af;\">Color<\/span>[] texture = <span style=\"color: blue;\">new<\/span> <span style=\"color: #2b91af;\">Color<\/span>[POINTSIZE * POINTSIZE];<\/p>\n<p style=\"margin: 0px;\"><span style=\"color: blue;\">for<\/span> (<span style=\"color: blue;\">int<\/span> i = 0; i &lt; texture.Length; ++i)<\/p>\n<p style=\"margin: 0px;\">{<\/p>\n<p style=\"margin: 0px;\">&nbsp;&nbsp;&nbsp; texture[i] = <span style=\"color: #2b91af;\">Color<\/span>.Red;<\/p>\n<p style=\"margin: 0px;\">}<\/p>\n<p style=\"margin: 0px;\">m_mouseTexture.SetData(texture);<\/p>\n<\/div>\n<p>Now, in the Update() function, update your mouse position:<\/p>\n<p><\/p>\n<div style=\"font-family: Courier New; font-size: 10pt; color: black; background: white;\">\n<p style=\"margin: 0px;\"><span style=\"color: #2b91af;\">MouseState<\/span> mouseState = <span style=\"color: #2b91af;\">Mouse<\/span>.GetState();<\/p>\n<p style=\"margin: 0px;\">m_mousePos.X = mouseState.X;<\/p>\n<p style=\"margin: 0px;\">m_mousePos.Y = mouseState.Y;<\/p>\n<\/div>\n<p>And finally, in the Draw() function, draw your sprite in a spritebatch:<\/p>\n<p><\/p>\n<div style=\"font-family: Courier New; font-size: 10pt; color: black; background: white;\">\n<p style=\"margin: 0px;\">spriteBatch.Begin();<\/p>\n<p style=\"margin: 0px;\">spriteBatch.Draw(m_mouseTexture, m_mousePos, <span style=\"color: blue;\">null<\/span>, <span style=\"color: #2b91af;\">Color<\/span>.White, 0f,<\/p>\n<p style=\"margin: 0px;\">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style=\"color: #2b91af;\">Vector2<\/span>.Zero, 1.0f, <span style=\"color: #2b91af;\">SpriteEffects<\/span>.None, 0f);<\/p>\n<p style=\"margin: 0px;\">spriteBatch.End();<\/p>\n<\/div>\n<p>Grab the sample project for this <a href=\"http:\/\/www.lukerymarz.com\/blog\/MouseDisplay.zip\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>XNA doesn&#8217;t automatically show you a mouse pointer within the game window. Solving this is easy. First, you need a position for your mouse. You&#8217;ll also need a 2d texture to display as your mouse pointer. Declare this within your game class: Texture2D m_mouseTexture; Vector2 m_mousePos; Be sure to initialize it to zero. For simplicity, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[6,3],"tags":[],"class_list":["post-12","post","type-post","status-publish","format-standard","hentry","category-graphics","category-xna"],"_links":{"self":[{"href":"http:\/\/lukerymarz.com\/alearningexperience\/wp-json\/wp\/v2\/posts\/12","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/lukerymarz.com\/alearningexperience\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/lukerymarz.com\/alearningexperience\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/lukerymarz.com\/alearningexperience\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/lukerymarz.com\/alearningexperience\/wp-json\/wp\/v2\/comments?post=12"}],"version-history":[{"count":0,"href":"http:\/\/lukerymarz.com\/alearningexperience\/wp-json\/wp\/v2\/posts\/12\/revisions"}],"wp:attachment":[{"href":"http:\/\/lukerymarz.com\/alearningexperience\/wp-json\/wp\/v2\/media?parent=12"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/lukerymarz.com\/alearningexperience\/wp-json\/wp\/v2\/categories?post=12"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/lukerymarz.com\/alearningexperience\/wp-json\/wp\/v2\/tags?post=12"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}