XNA – How to check if mouse or keyboard button has been clicked, pressed or released


I was reviewing some of the search terms that brought people to my website. It seems like a lot of them are related to using the mouse in XNA, so I figured I’d write a quick post about determining the mouse or keyboard state in XNA, though this applies to most anything that polls input and has a main loop.

Read the rest of this entry »

Creating Render Targets in XNA 4.0


While working on the game engine for Fondusi’s, I had to change how my tile engine was rendered in order to add the lighting. At first, all tiles were just rendered directly to the screen (back buffer, actually) but with the lighting, I was tweaking pixel alpha values and it ended up causing issues with stacked tiles. So, what I needed to do was to draw my tiles on one surface, draw the lighting on a second surface, and then draw the lighting surface onto the tiled surface with it’s own pixel shader effect. (Really, I just sample the lighting surface in the pixel shader while drawing the tile surface to the back-buffer.)

In order to draw it like that, I had to add new render targets. Now, I was following a blog post on Shawn Hargreaves’ Blog which was (quite!) out of date. So needless to say, I had a few issues implementing it the same way. Fortunately, they’ve made it a lot easier to use render targets in XNA 4.0 and that what I’m going to show you below.

Read the rest of this entry »

XNA – Converting from angle to Vector2 and back


I was recently working on an XNA project and needed to convert from an angle to a Vector2. There are a lot of resources showing how to go one way (angle->Vector2) or the other (Vector2->angle) but not many showing both. So, I figured it might be helpful to put it up here, even if I only use it for reference.

Angle -> Vector2

Now, the reason I make the Y value negative is because that’s how the screen coordinate system works. If you didn’t do this, the angle in radians would go clock-wise instead of counter-clockwise.

Vector2 -> Angle
Read the rest of this entry »

XNA – Check if a click is on the game form/window


One of the annoying things with XNA, happens when handling mouse clicks. If you’ve ever worked on an XNA windows game (note: this only really applies to a windows game) in windowed mode and tried switching applications, you may have noticed that XNA still captures the clicks even when the window is not active! It also reports the mouse position when it’s outside of the window which can cause more issues in and of itself. Unfortunately, the fix for the first problem doesn’t work if you’re using multiple forms in the same application.

So, we have three issues:

  • XNA captures clicks while the game is not active (i.e. from other applications)
  • and it reports the mouse position when it’s outside of the viewable area
  • and the fix for the first item above doesn’t work when you have multiple forms in your application.

Fortunately, there’s an easy way to resolve these! “Great!”, you say, “now how do I do it?”. Click on, my friend.

Read the rest of this entry »

Latest Posts



Post Calendar

March 2024
M T W T F S S
 123
45678910
11121314151617
18192021222324
25262728293031