Xbox LIVE Indie Games
Sort Discussions: Previous Discussion Next Discussion
Page 1 of 1 (8 posts)

Possible to make addons with XNA? -Ressources?

Last post 10/23/2012 2:46 PM by Jakob Krarup. 7 replies.
  • 10/6/2012 3:10 PM

    Possible to make addons with XNA? -Ressources?

    Hey,

    I am planning to write an application for Windows with different modules. There should be a basic version and different addons which should extend this basic version. Imagine it as a collection of games. You'll have the basic pack with 5 games and should be able to extend it with more games.
    Can this be realized with XNA? Does anybody know any ressources on this, because I don't know where to start from.

    Best, Jan
  • 10/6/2012 8:39 PM In reply to

    Re: Possible to make addons with XNA? -Ressources?

    Hi Jan :)

    This sounds like a good place to use the Strategy pattern.

    Basically - make an abstract class ("AbstractGame" for example) which has a Update and a Draw method (just like the Game class), and whatever else you need (probably SpriteBatch, ContentManager, GraphicsDevice and GraphicsDeviceManager) as properties.

    Something like this:
    /// <summary> 
    /// This class is the basis for all the games that can be loaded in the main game 
    /// You have to implement Draw and Update like in a normal Game class. 
    /// </summary> 
        public abstract class AbstractGame  //abstract means it isn't finished yet, and can't be "new'ed up" :)  
        //helperproperties, to store stuff you need to make a game 
        public GraphicsDeviceManager GraphicsDeviceManager { getprotected set; } 
        public GraphicsDevice Graphics { getprotected set; } 
        public SpriteBatch SpriteBatch { getprotected set; } 
        public ContentManager Content { getprotected set; } 
        public Game Game { getprotected set; } 
        public SpriteFont Font { getprotected set; } 
        public bool GameOver { getprotected set; } 
     
        //initializes a new game 
        public void Initialize(Game game, GraphicsDeviceManager manager, SpriteBatch batch) 
        { 
            this.GraphicsDeviceManager = manager; 
            this.Graphics = game.GraphicsDevice; 
            this.SpriteBatch = batch; 
            this.Content = game.Content; 
            Font = Content.Load<SpriteFont>("DefaultFont"); 
        } 
     
        //these methods must be implemented by your game 
        public abstract void Draw(GameTime gameTime); 
        public abstract void Update(GameTime gameTime); 
     


    Then you can make concrete implementations of games as subclasses (e.g. Pong:)
    public class PongGame : AbstractGame 
        public override void Draw(GameTime gameTime) 
        { 
            Graphics.Clear(Color.Blue); 
            SpriteBatch.Begin(); 
            //draw Pong stuff 
            SpriteBatch.End(); 
        } 
     
        public override void Update(GameTime gameTime) 
        { 
            //update Pong stuff 
        } 


    In your Game class, make a variable of this type 

    AbstractGame _currentGame; 

    In your main game's Update and Draw methods you check whether the _currentGame variable is null. If it is you present a game selection screen where a user can select the possible games.
    When a game is selected, you instantiate a concrete implementation (subclass) of the AbstractClass (Pong/Pacman/etc.), assign it the variables it needs and set the _currentGame to this.

    //if game was selected (in this case Pong): 
    AbstractGame game = new PongGame(); 
    game.Initialize(this, graphics, spriteBatch); 
    _currentGame = game; 


    In your main game's Update and Draw you call the Update and Draw of the game the player has chosen.

    protected override void Update(GameTime gameTime) 
        //if we're playing a specific game 
        if (_currentGame != null
        { 
            //...call the update method of the game being played 
            _currentGame.Update(gameTime); 
        } 
        else 
        { 
            //take input in gameselectionmenu 
            //... 
     
            //if game was selected (in this case Pong): 
            AbstractGame game = new PongGame(); 
            game.Initialize(this, graphics, spriteBatch); 
            _currentGame = game; 
        } 
     
        base.Update(gameTime); 
     
    protected override void Draw(GameTime gameTime) 
        //if we're playing a specific game 
        if (_currentGame != null
        { 
            //...call the update method of the game being played 
            _currentGame.Draw(gameTime); 
        } 
        else 
        { 
            //draw gameselection menu 
        } 
     
        base.Draw(gameTime); 
     


    After writing the short explanation above, I put together a little barebones working demo for you:


    screenshot of the demo

    Let me know if it is of any use... :)

    Latest edit: I've included a GamesPackManager class, which lets you drop a DLL with new games in a subfolder of your main game and they will then show up in the game selection menu.

    Here's a small version of the class diagram which may help some people:

    classdiagram
    (Click the image for a larger version)


    Kind regards - Jakob

    P.S. I figured this was now almost a complete blogpost - so I've added a bit more info and posted it here
  • 10/7/2012 11:45 AM In reply to

    Re: Possible to make addons with XNA? -Ressources?

    Wow, that Looks like a hell of a solution. Thank you for all your work, I will definetly take a deeper look in it.

    Best, Jan
  • 10/7/2012 3:03 PM In reply to

    Re: Possible to make addons with XNA? -Ressources?

    You're welcome :)

    Kind regards - Jakob
  • 10/7/2012 3:56 PM In reply to

    Re: Possible to make addons with XNA? -Ressources?

    Wow, you really went over the top on this. I like it. Good work!
  • 10/7/2012 5:30 PM In reply to

    Re: Possible to make addons with XNA? -Ressources?

    Thanks! :-D

    /Jake
  • 10/23/2012 1:04 PM In reply to

    Re: Possible to make addons with XNA? -Ressources?

    I'm trying to use this system for use a selector for games.
    My idea is to create a "framework" for some kind of game (that i 'm developing).
    The problem is "CONTENT".

    How i manage the content with this system for every game?
    I will to load and unload content (for specific game) direcly in the game.

    I means everytime i make a new game, i make new solution now, with same namespace, include reference to "GamePack library" with abstract class.
    Every new game, is a new Solution and new Project, that use and exted that abstract class.. and give me DLL file, that i put in "GAMES" folder.

    Now all work good, but i can't load and manage for every game different Content.

    How i can manage this?
    if i create empty content project in solution of the game, i got:

    "Avviso 5 mancata corrispondenza tra l'architettura del processore di questo progetto "MSIL" da compilare e l'architettura del processore del riferimento "Microsoft.Xna.Framework.Content.Pipeline.XImporter, Version=4.0.0.0, Culture=neutral, PublicKeyToken=842cf8be1de50553, processorArchitecture=MSIL", "x86". La mancata corrispondenza può causare errori di runtime. Modificare l'architettura del processore di destinazione del progetto tramite Gestione configurazione in modo da allineare le architetture del processore tra il progetto e i riferimenti o specificare una dipendenza sui riferimenti con un'architettura del processore corrispondente all'architettura del processore di destinazione del progetto. TheEscapeContent"

    Francesco
  • 10/23/2012 2:46 PM In reply to

    Re: Possible to make addons with XNA? -Ressources?

    Hi Azhoral :)

    I have actually come across the same problem, and am in the process of creating a tutorial which addresses this :)

    the sample project


    The short version is: it is possible to embed your XNA resources inside the DLL by first creating the resources in a separate Content Library, and then adding them to your Game's XNA Game Library project as a resource in an Application Resource file.
    Then you need a special ContentManager which is a ResourceContentManager to read directly from the DLL.
    I've created a sample project where everything is working, and a videopresentation (see above), but I am only halfway on the tutorial.

    Get the code and play around with it, you may be able to figure out what to do.
    You won't see any games in the complete solution, as you need to copy the compiled SampleGamesPack.dll to the folder of the XnaArcadeTutorial.exe.

    I am hoping to finish the tutorial series during next week :)

    Kind regards - Jakob
Page 1 of 1 (8 posts) Previous Discussion Next Discussion