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 »
Fondusi’s Dev Blog – Week 4 – Lighting and Shaders

This week we focused on getting the lighting system working in the tile engine. In order to do this, I had to change the way the tile engine was being rendered. Read on to see how we did it and how it looks.

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 »

Fondusi’s Dev Blog – Week 3 – Tile Behaviours

This week we got some of the extra mapping tools working like the paint bucket and the eye dropper, which allows you to select a tile from the map (so you don’t have to find it on the tilesets). We also set up “tile behaviours” (read the full post for more info), added the ability to switch between player mode and camera control, added some validation functions for mouse clicks on the game from (see here for more details), added an outline for the player name font, and did an overhaul of the particle engine system to make it more generic and useful.

Read the rest of this entry »
Fondusi’s Dev Blog – Week 2 – Pixel Shaders and Moving Players

The second week of development was spent reorganizing the map editor and fleshing out more-or-less how we wanted it to look and function. We added a basic pixel shader effect file and changed how players move across tiles, which I discuss in more detail in the full post.

Read the rest of this entry »
Fondusi’s Dev Blog – Week 1 – Tile Engine and Map Structure

Assuming you’ve read up on the first post, it’s time for the first (back-logged) Fondusi’s Development Blog weekly post.

Week 1 – August 1st – 7th

Note: We decided to start out making the map editor so we could get the tile engine up and running quickly before we needed the server, client or networking code. So, until further notice, all posts relate to developing the map editor.

This week I set up the solution’s main projects and began work on the tile engine. I based the map layout on the old game’s map objects and added new capabilities as I saw fit. The major new addition was using map layers. Basically, this is like having two maps stacked on each other. There are special tiles that allow the character to switch between layers. This means we can do things like allow players to walk over other players on bridges or have a house with two floors on the same map.

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

float myAngleInRadians = Math.PI;
Vector2 angleVector = new Vector2(
    (float)Math.Cos(myAngleInRadians),
    -(float)Math.Sin(myAngleInRadians));

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 »