• Advertisement
Sign in to follow this  

Newbie needing help with C#/MDX2

This topic is 4257 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

First off, hello everyone! I'm new to posting here, but I've been lurking about for a while taking in as much information as I can before trying to tackle a project, which brings me to my question. I'm working on learning C#/MDX2 by slowly developing an engine so I can learn how the DirectX functions all work and how to implement it. I've been following along with The Hazy Mind's tutorial, however I decided to go off on my own when he left 2D drawing behind. I modified alot of how his code was run, and can get a window rendered (whoopie!), but when I try to put in a sprite I get NullException error. Here's the code in question:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Text;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace BNETEngine.BNVideo
{
    /// <summary>
    /// Base class for Objects. Used for polymorphy.
    /// </summary>
    public class BNObject
    {
        protected Vector3 myPosition = new Vector3(0, 0, 0); //These Vectors are used for
        protected Vector3 myRotation = new Vector3(0, 0, 0); //building transforms for the
        protected Vector3 myScaling  = new Vector3(1, 1, 1); //camera

        public virtual void ReloadResources(Device myDevice) {} //This will be called on device resets
        public virtual void Render(Device myDevice) {}          //Renders the object
    }

    /// <summary>
    /// Sprite class. Contains key information regarding an element on the screen.
    /// </summary>
    public class BNSprite : BNObject
    {
        private Dictionary<string, BNAnim> Animations;  //Dictionary of all the sprite's animations
        private Vector3 initPos;                          //Position of the sprite at load
        private Vector3 curPos;                           //Current position of the sprite
        private string anim;                            //Current playing animation
        private Sprite mySprite;

        /// <summary>
        /// Default Constructor. Creates an empty sprite
        /// </summary>
        public BNSprite()
        {//Default position information
            initPos    = new Vector3(0, 0, 0);
            curPos     = new Vector3(0, 0, 0);
            Animations = new Dictionary<string, BNAnim>();
        }

        /// <summary>
        /// Create a sprite with a default location
        /// </summary>
        /// <param name="initPos">
        /// Initial location of sprite in floats
        /// </param>
        public BNSprite(Vector3 initPos)
        {//Initialize the sprite at a specific location
            this.initPos = initPos;
            curPos       = initPos;
            Animations   = new Dictionary<string, BNAnim>();
        }

        /// <summary>
        /// Create a sprite with a default location and animations
        /// </summary>
        /// <param name="initPos">
        /// Initial location of sprite in floats
        /// </param>
        /// <param name="Animations">
        /// Dictionary containing all animations for the sprite
        /// </param>
        public BNSprite(Vector3 initPos, Dictionary<string, BNAnim> Animations)
        {//Initialize the sprite at a specific location with all animations
            this.initPos    = initPos;
            curPos          = initPos;
            this.Animations = Animations;
        }

        /// <summary>
        /// Create a sprite with a set of animations
        /// </summary>
        /// <param name="Animations">
        /// Dictionary containing all animations for the sprite
        /// </param>
        public BNSprite(Dictionary<string, BNAnim> Animations)
        {//Initialize the sprite at default location, with all animations
            initPos         = new Vector3(0,0,0);
            curPos          = new Vector3(0,0,0);
            this.Animations = Animations;
        }
        /// <summary>
        /// Adds an animation to the list of animations for the sprite
        /// </summary>
        /// <param name="key">Name of the animation</param>
        /// <param name="Animation">Animation to be played</param>
        public void addAnim(string key, BNAnim Animation)
        {
            try
            {
                Animations.Add(key, Animation);
            }
            catch (ArgumentException)
            {
                MessageBox.Show("An animation with this key already exists.", "Argument Exception!");
            }
        }

        /// <summary>
        /// Instructs the sprite to start playing the specified animation on the next frame
        /// </summary>
        /// <param name="anim">
        /// Animation to play
        /// </param>
        /// <returns>
        /// True if the animation can be played
        /// </returns>
        public bool doAnim(string anim)
        {
            if (Animations.ContainsKey(anim))
            {
                this.anim = anim;
            }else
            {
                return false;
            }
            return true;
        }

        public override void Render(Device myDevice)
        {
            Texture toDisplay = Animations[anim].curTexture;
            Vector3 textureCenter = new Vector3(
                toDisplay.GetSurfaceLevel(0).Description.Width / 2,
                toDisplay.GetSurfaceLevel(0).Description.Height / 2,
                0);

            mySprite.Transform = Matrix.Transformation(
                new Vector3(),
                new Quaternion(),
                new Vector3(),
                textureCenter,
                new Quaternion(),
                new Vector3());

            mySprite.Begin(SpriteFlags.AlphaBlend);
            mySprite.Draw(toDisplay, null, null, curPos, System.Drawing.Color.White.ToArgb());
            mySprite.End();
        }
    }

    /// <summary>
    /// Animation Class. Contains a series of images and methods to control their playback.
    /// </summary>
    public class BNAnim
    {
        private Texture[] Frames;   //An array to hold the images of the animation
        private bool Repeat;        //Whether or not to repeat the animation after reaching the final frame
        private int curFrame;       //Which frame is currently being displayed

        /// <summary>
        /// Create an empty Animation
        /// </summary>
        public BNAnim()
        {//Defaults to not repeat
            Repeat = false;
            curFrame = 0;

        }

        /// <summary>
        /// Create an empty Animation
        /// </summary>
        /// <param name="Repeat">
        /// Repeat upon reaching the end of the animation
        /// </param>
        public BNAnim(bool Repeat)
        {//Will repeat once reaching the end of the animation
            this.Repeat = Repeat;
            curFrame = 0;
        }

        /// <summary>
        /// Add a single frame to the animation
        /// </summary>
        /// <param name="myPath">
        /// The location of the image
        /// </param>
        /// <param name="myDevice">
        /// The device the frame will be drawn on
        /// </param>
        /// <returns>
        /// True if the frame added successfully
        /// </returns>
        public bool addFrameFromFile(string myPath, Device myDevice)
        {
            try
            {
                Frames[Frames.Length + 1] = new Texture(myDevice, myPath);
            }
            catch (DirectXException)
            {
                return false;
            }
            return true;
        }

        public Texture curTexture
        {
            get
            {
                Texture temp;
                if (curFrame == Frames.Length)
                {
                    temp = Frames[curFrame];
                    curFrame = 0;
                    return temp;
                }
                else
                {
                    temp = Frames[curFrame];
                    curFrame++;
                    return temp;
                }
            }
        }
    }
}

I appoligize for the excesive length. The part throwing the exception is:
Frames[Frames.Length + 1] = new Texture(myDevice, myPath);

And here is how the sprite is being added:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using BNETEngine;
using BNETEngine.BNVideo;

namespace BNETDemo
{
    class Demo
    {
        static BNEntry demo1 = new BNEntry(800, 600, "BNET Engine!");

        [STAThread]
        public static void Main()
        {
            BNAnim anims = new BNAnim();
            anims.addFrameFromFile("Images\\ani\\ani1.gif", demo1.Device);
            Dictionary<string, BNAnim> animation = new Dictionary<string,BNAnim>();
            animation.Add("first", anims);
            BNSprite sprite1 = new BNSprite(animation);
            demo1.Scene.AddObject(sprite1);
            demo1.Show();
            Application.Run(demo1);
        }
    }
}

The thing is, the compiler gives me a warning about Frames being null, and I can't figure out a good way to initialize it without putting a blank frame at the begining. I also have a question regarding my BNAnim class. I'm not quite sure how to go about getting it to actually play the animations. Now, I was working on this at about 4AM last night (and I have a cold), so my logic may not be all there at the moment, so please excuse any silly mistakes. If anyone can point me in the right direction, I'd be very appreciative!

Share this post


Link to post
Share on other sites
Advertisement
Have you used the debugger to determine that Frames, myDevice, and myPath are all not null? Where do you initialize them?

Share this post


Link to post
Share on other sites
All but Frames are initialized prior to the call to add to the Frames array. My big problem is in trying to figure out how to get Frames initialized with usefull data, without putting a placeholder in there on class instatiation.

I'd post the full code listing but it's kinda big for one post. I'll try and find a place to host an archive with my solution and I'll update this post to link to it.

EDIT: Here's a download of my solution: http://home.earthlink.net/~roweju/data/BNETEngine.rar

Share this post


Link to post
Share on other sites
Aside from the data that Frames contains, it itself is an Array and therefore you most intialize it to

Frames=new Texture[100];

or however many items you want. After that you can set individula array items the way you are trying to now.

Share this post


Link to post
Share on other sites
Quote:

hehe

Frames[Frames.Length + 1] = new Texture(myDevice, myPath);

Shouldn't that be - 1?


Should it? I'm still learning C# as I go, and most of my programming experience is in VB.NET (with some minor C++ there aswell). So I wasn't sure if Length would return the amount of elements, or the position of the last one. *laughs*

As you can probably see, I can use all the help I can get!

Speaking of help... Would anyone know of a good reference for all of the MDX classes and methods (other than the SDK, as I find the documentation for it [and the Intellisense] is lacking compared to the core .NET framework docs).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement