Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Tuesday, June 28, 2011

XNA Game Design: Abstract Sprite (part 2)

In the last part I said our constructor will look a little bit funky. Here it is:


public ASprite(string path, Vector2? loc = null)
{
    pos = loc ?? Vector2.Zero;
    image = content.Load<Texture2D>("Graphics/" + path);
}

If you're baffled by what is going on, I don't blame you. It certainly looks quite funky, so let me explain what we're trying to do.

When the user wants to create a new sprite, they must provide the path name to the Texture2D resource they want to use, and they may also provide the Vector2 position of their sprite. C# takes in optional arguments by specifying their default values in the parameters (for example, method(int x, int y = 5) requires an int for x, and an optional value for y that defaults to 5 when unspecified).

The question mark after the Vector2 specifies the value we're taking in is nullable. A nullable type is like it's original type, but that it's range of values can also acceptably include null. In our case, we're saying that if the developer does not specify the optional value for our vector2, it will default to null.

Inside the constructor, the first line uses a special operator (??) that sets pos equal to loc if it has a value (is not null), or otherwise sets it to the value of Vector2.Zero (0.0, 0.0). This ensures that if the developer specified a starting Vector2, it's position is set. Otherwise, if it's not set, or set to null, it defaults to the top-left corner (0.0, 0.0).

If you're paying attention, you might be thinking, "Why don't we just say (... Vector2 loc = Vector2.Zero) and avoid the nullable thing all together?"

The problem here is rather asinine; to specify optional parameters you need compile-time constants (nothing that requires extra computation to determine). This means you can't specify it as (... Vector2 loc = new Vector2(0.0, 0.0) ) either.


Wow. That's a lot of explanation for a simple constructor. Lets finish this bad boy up. All we need now is just a draw method. As it should, it will take our spriteBatch and use that to draw our sprites.


public void Draw(SpriteBatch sb)
{
sb.Draw(image, pos, src_rect, blend_color, angle, origin, scale, effect, z);
}

That's a lot of parameter's, innit?
Now, we just need to make an actual Sprite Class that we can make instances of. In a separate class, add the following code:
class Sprite : ASprite
{
    public Sprite(string path, Vector2? v = null) : base(path, v)
    {
    }       
    public new void Draw(SpriteBatch s_Batch)
    {
        base.Draw(s_Batch);
    }
}


The Sprite class now extends ASprite, and holds all it's properties and methods. These members can be overwritten with the new keyword (as we did with the Draw method) and the inherited members can be called with the base keyword.


To draw a new image, in the SCENE.Update(), just create a new instance of Sprite. initiate and tweak your Sprite instances as you see fit (Sprite.Rotation = 45). They can be easily rotated, flipped, and moved. There just simply needs to be a call somewhere in the SCENE.Draw() to actually draw the sprite.


Not shabby at all?


So why did we want to make an abstract class in the first place? After all, having only one class to shape after an abstract class is kinda pointless. Unless of course, we want to create more classes that would extend our abstract class. Such as a class to cut the image into pieces and draw only one piece at a time. Like, say for example, in a sprite sheet...


Slots animation by Mia. Unsure of how to link to her work, but this sprite sheet serves a good example of what we will be tackling.


Homework: Opacity in XNA images is calculated strangely. You would think it would be derived by the Alpha value used in the blending color, but it is in fact much different. Alpha values are imparted onto drawn graphics by multiplying a float value to the blending color, with 0.0f being full transparency, and 1.0f being fully opaque. So to draw an image at 50% opacity, you would blend with blend_color * 0.5f .


Create one extra public integer value for Sprite called Opacity. When the developer changes the value of opacity (from 0-255), change the draw method so the image blends with the set opacity.

Saturday, June 11, 2011

XNA Game Design: Implementing SCENE flow

Sorry I'm not updating as frequently. It's mostly because writing these things out is long, and I'm busy myself. Still, this chapter should conclude Part 1 of the guide!

By now, you might have noticed everything is coming back to the Game1 class. Because everything must actually run from this class, our code in it will certainly get a bit bogged down, and keeping our code organized will be more difficult.

Games, no matter how simple, should not be built entirely within the "Main" class (which is essentially what our Game1 is). It's not only impractical, but it makes for code that's both hard to read and keep organized. Not to mention, it makes us loose sight of what we need to focus on.


My solution to this may not be the best, but I'd say it's pretty darn good. If you remember from the chapter "The Basics", I showed you the simple game loop. While this is the loop that all games follow, it's not the loop an XNA game will follow.


And I'm not mentioning our Input!


Our XNA loop is a bit more complicated. And as we've already done, we've thrown our call to update our inputs in the Update() method. That works just fine.


Still, if we don't do something about organizing our code, we will wind up with a huge Game1 class. While there are a number of ways of engineering around this problem, I can't guarantee the solution I'll provide is the best (truthfully, the best solution depends on the game YOU'RE designing. What I'm showing you is best for something with lots of screens or minigames, such as a RPG). Still, I think it's pretty good, as it keeps the code organized and it allows the developer to focus on the situation at hand.


The solution we will use to overcome this obstacle is to create an interface that will handle our Update and Draw methods depending on what we want our situation to focus on. As such, our game flow will look more like this;


The flow may seem slightly more complicated, but because we have static input, all we'll have to focus on from here on out is our Draw() and Update() methods.

I call this interface a SCENE. The concept is that a different object that implements the SCENE interface will take over the update and draw methods depending on the situation, and in turn that will let us code objectively to each SCENario (get it? huh? Err, let me better explain...)



When you start a game, you are often confronted with a few splash screens and maybe a intro movie before encountering a title screen. From the title screen, you can start a new game, go to the options menu, or to the load menu. From the load menu and selecting 'new game', you go to the world map. From the world map, you can go to the main menu, to many submenus, or to a battle screen. Occasionally you might have a minigame, or something else. When you're coding each of these scenarios and screens, you should only be concerned with that scene, and any transitioning to any related screen. What happened on the main menu should stay on the main menu, and so forth.


All images Courtesy of Nintendo and Gamefreak. This game serves as a good example of how the SCENE interface should work. The scope of a game such as pokemon may be daunting, but when you break it down into it's individual components it's not as complex as it seems.

Each of these would be a different SCENE object. The SCENE Interface needs to only contain the method signatures of two methods; Update and Draw .
Alright, lets do this! Create a new interface object (similar to how you would create a new class, but select 'interface' instead of class). Name is IScene.cs (good habit; prefix all interfaces you create with a capital 'I'), and add the code;
public interface IScene
{
    void Update();
    void Draw(SpriteBatch s_batch);
}


Wow. Smallest .cs file ever or what?


While this whole process is all fairly painless, but we're not finished yet. We still have to make things work in our Game1 class. By the time this is over, it should be one of the last things we will ever do in the Game1 class. In the Game1 properties, add 
public static ISCENE SCENE;

Now in our Update, add:
//Check to see if Game1.SCENE = null. 
//If so, exit. Otherwise, update.

if (SCENE == null)
    this.Exit();
else SCENE.Update();


And in our Draw method, add (between our spriteBatch.Begin() and spriteBatch.End() calls) :
SCENE.Draw(spriteBatch);

And we're done! Build, fix any errors and run. What happens?
If the screens pops up and closes itself, it's working. Why? Because of our check in update. We never set the Game1.SCENE to anything, so it's value is null. When Game1.Update() runs, it trips the if statement, and it exits with this.Exit() (alas, another non-static member hosted in Microsoft.Xna.Framework.Game). This is good for two reasons;

Firstly, if somehow our code runs in a manner to nuke the current SCENE by setting it to null, or otherwise leave us without a SCENE, it will close itself rather than throw an error (if your code is that bad though, you have more important things to worry about ). Secondly, it gives us a static method to quit the game. With the single line of code Game1.SCENE = null, we can end things on a dime.

Alright, so we at least want the game window to show up when we run. From here on out, when creating a new broad component to your game (a menu, a minigame, a title screen, etc.), make sure it will implement IScene!

    class Scene_TEMPIScene
    {

  //List your local variables here!
        public Scene_TEMP()
        {
//Use the constructor to initialize
//any local variables to your scene!

        }

        public void Update()
        {
//It's our update method!
        }

        public void Draw(SpriteBatch s_batch)
        {
  //Because the SpriteBatch passes by
//reference (it sends the pointer to 
//Game1.Spritebatch, not the contents of
//Spritebatch), we can just use s_batch
//send any calls to draw (and it's in-
//between the begin() / end() methods!
//No need to worry about that ever again!

        }
    }


This is just a basic template of a scene object we would need to create. In our Game1.Initialize() method, we simply need to specify what our initial SCENE object should be before the game officially runs. Because all input is ubiquitous and static, all components of our game are now broken down into two methods; Update and Draw. Game design could not be more straightforward.



If you can't already tell, the underlying philosophy of this guide has been "everything in one line of code". By coding objectively and creating a framework to handle more abstract tasks, we give ourselves more tools to work with. This concludes the first part of this guide. I hope you had fun, and please recommend this blog to your friends. Facebook it. Tweet it. Reddit it, so on. I'm a sucker for statistics, and the more people reading this the more inclined I am to update it.

In the next part of this guide, we will learn how to beat the dreaded Spritebatch, and display our graphics in a single line of code. Prepare for Part 2; Thinking Abstractly.

Homework: I'll soon provide the entirety of the code we should have, so the solution for this homework will be found there. In the Game1.Update() method, set the SCENE to the same SCENE object the initialize method sets whenever the F12 button is pressed (Think of this as a reset button. When F12 is pushed, the game jumps back to the title screen. The actual operations for a reset button should be more complex than this, but for now this will do).

Yesterdays Homework Solution: The code wasn't hard, just repetitive.

public static void BGM_play(string name)
{
    if(BGM != null) BGM.Stop(); 
    s_bgm = Player.Load<SoundEffect>(@"BGM\" + name);
    BGM = s_bgm.CreateInstance();
    BGM.IsLooped = true;
    BGM.Play();
}


public static void BGS_play(string name)
{
    if(BGS != null) BGS.Stop(); 
    s_bgs = Player.Load<SoundEffect>(@"BGS\" + name);
    BGS = s_bgs.CreateInstance();
    BGS.Volume = 0.8f;
    BGS.IsLooped = true;
    BGS.Play();
}


public static void SE_play(string name)
{
    if(SE != null) SE.Stop();
    s_se = Player.Load<SoundEffect>(@"SE\" + name);
    SE = s_se.CreateInstance();
    SE.Volume = 0.8f;
    SE.Play();
}


public static void ME_play(string name)
{
    if(ME != null) ME.Stop(); 
    s_me = Player.Load<SoundEffect>(@"ME\" + name);
    ME = s_me.CreateInstance();
    BGM.Pause();
    BGS.Pause();
    ME.Play();
    while(ME.state != stopped)
    {
    //this will resume the BGM and BGS
    //once the ME has completed.
    //Avoid using while statements like this though;
    //They're bad design, and they slow down the game
    //And in theory, this shouldn't even work at all.
    }
    BGM.Resume();
    BGS.Resume();
}


Wednesday, June 8, 2011

XNA Game Design: ContentManager and Audio

Unlike the SpriteBatch, the ContentManager is actually a lot easier to spawn instances of, and just requires a variable we can make a static in our game1 class. In our Game1 properties, add the following;

public static GameServiceContainer services;
and in the constructor, initialize it with

services = Services;
What is a GameServiceContainer? It seems like one of those generally random variables we seem to be needing here and there (and it is). A ContentManager needs a particular service that can import and read the files it's loading.*  GameServiceContainer objects simply contain a collection of all Game.Services so that we can pass it in lieu of picking out each service from each service provider and passing that. It's like passing a box of different cookies to children in a classroom instead of asking each child what they want, running to the factory and getting it for them. Nifty huh?


Our game will have a significant number of services, most if not all of whom are default initialized in the Microsoft.Xna.Framework.Game class. These are contained in the private Services object, which is not static and we would like public static access to. With this done, from here on out we can make ContentManagers anywhere using Game1.services .
*(Contentmanagers really only need a service for loading texture files, so this is the equivalent of giving a lumberjack an ax to trim a bonsai tree. He just needs the ax in case he's suddenly asked to chop a tree, which shouldn't happen. God these metaphors are getting weird...)


Let's create a class to play audio now! This audio playing class will contain methods designed to do the following:
  • In a single line of code, take in a String that will correspond to an asset name in our "Content/Audio" folder, and play that sound at default settings (full volume, normal pitch, no pan).
  • Allow us to change the volume, pitch or pan of the sound in a single line of code.
  • Allow us to pause the sound in a single line of code.
  • Allow us to resume the sound in a single line of code.

static class Audio
{
    public static SoundEffect s_bgm;
    public static SoundEffectInstance BGM;
    //Our Audio ContentManager will be called Player
    public static ContentManager Player = 
    new ContentManager(Game1.services, "Content/Audio");
}

We see our ContentManager is initialized, but hold on a second; there's a class... called SoundEffectInstance? What's the difference between a SoundEffect and a SoundEffectInstance? Is an instance of a SoundEffect a SoundEffectInstance? What is this I don't even-


Chillax. A SoundEffect is the direct output from the ContentManager. SoundEffect objects contain mostly metadata and no methods actually related to playing the sound. This metadata includes length, channels of audio, kbps, and other things related to sound we wouldn't want to touch with a ten foot pole. SoundEffectInstance, on the other hand, contains all our fun methods for playing, pausing, resuming, looping, paning, pitching and general tinkering with our sound. SoundEffect has a method called CreateInstance() which, as you can imagine, returns a SoundEffectInstance object. With our content manager, we can load the SoundEffect, and then use CreateInstance() to create our playable SoundEffectInstance. It's just a two step process.


So, here is our method to take in the string and play the audio:



public static void bgm_play(string name)
{
    if(BGM != null) BGM.Stop(); 
    //Without this the current sound would not stop playing
    s_bgm = Player.Load<SoundEffect>(@"BGM\" + name);
    //The @ before the string indicates that escape codes 
    //should be ignored. It should be noted that both / and \
    //would be read as a sign to look into that folder. 
    //Therefore, ("BGM/" + name) would work just as well.
    BGM = s_bgm.CreateInstance();
    BGM.IsLooped = true;
    BGM.Play();
}

That's all there is to it. From here on out, we just have to say Audio.BGM.Pause();,  Audio.BGM.Stop()Audio.BGM.Volume = x, etc. just to change things. Everything from one line of code- that's how to design a game.


Still, there is a slight problem; with this design, we only get one channel to play sound on. I named our SoundEffect s_bgm and our SoundEffectInstance BGM. BGM is designed to be background music that will loop upon completion (notice the BGM.IsLooped = true;). But because this is our only channel of audio, we would not be able to play other noises, such as spoken voices, running water, explosions off in the distance and so forth. As such, this is the homework:


Homework: In your Audio folder, create four new subfolders named BGM, BGS, SE, and ME. Initialize and create the variables for these four audio channels (BGM is already there), and create methods so that following occurs:
  • Play_BGM() is looped (Background Music)
  • Play_BGS() is looped and will play at 80% volume. (Background Sound)
  • Play_SE() is not looped, will play at 80% volume (Sound Effect)
  • Play_ME() is not looped, and when called will pause BGM and BGS. (Music Effect)
  • Each method will look for it's audio file in the respective audio subfolder