Problem following Nick Gravelyn's tile engine tutorial.

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 3299 Views - Last Post: 18 October 2012 - 07:45 AM

#16 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1356
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: Problem following Nick Gravelyn's tile engine tutorial.

Posted 04 October 2012 - 10:36 AM

        public Texture2D Texture2DFromFile(string fileName)
        {
            Texture2D texture;

            FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
            texture = Texture2D.FromStream(GraphicsDevice, stream);

            stream.Flush();
            stream.Close();
            return texture;
        }



Above is the code that I wrote to get around the missing FromFile method in the Texture2D class. It's just a simple method that opens a file into a FileStream, which is basically just a section of memory to use for reading and writing to files, it then sets the texture object to the data collected in the FromStream method, then flushes the stream to clear it all out, and then closes the stream which basically just closes the file and stops using it. There are a few arguments in the FromStream method call, but they're pretty simple to understand. We just use them to let the compiler know that we're going to open a file with the FileMode.Open argument, and read data from that stream with the FileAccess.Read argument. This also limits the current file to being read only while it is being used. The FileShare argument is an argument that allows the file to be opened in another place in the code before it is closed here. We add ".Read" after the FileShare to ensure that the file, if accessed anywhere else before this closes, is read only and cannot be overwritten.

EDIT: Forgot to mention how to use it. All you have to do is add this method to the Form1.cs class and call this method just like you would the Texture2D.FromFile in the code that Nick gives. You just remove the period from the line of code and it will call this method and do exactly the same thing as the FromFile method used to.

This post has been edited by Kilorn: 04 October 2012 - 10:38 AM

Was This Post Helpful? 0
  • +
  • -

#17 ArchColossus  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 22-February 12

Re: Problem following Nick Gravelyn's tile engine tutorial.

Posted 05 October 2012 - 06:45 AM

Thank you, it makes a lot more sense now. Although I've hit yet another bump in the road. Also a minor thing. In the very beginning of 4D 3/3, he is debugging the filename of loaded textures. I've been looking at it for a bit, and while Nick's correctly removes the parts of the file path he doesn't need, mine only removes the double backslashes. Leaving the rest of the path just sitting. It's not a huge issue, but I'd like to know why it's happening. Thank you guys so much for being so helpful.

private void addTextureButton_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "JPG Image|*.jpg|PNG Image|*.png|TGA Image|*.tga";
            openFileDialog1.InitialDirectory = contentPathTextBox.Text;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string filename = openFileDialog1.FileName;

                Texture2D texture = Texture2DFromFile(filename);
                Image image = Image.FromFile(filename);

                filename = filename.Replace(contentPathTextBox.Text + "\\", "");
                filename = filename.Remove(filename.LastIndexOf("."));

                textureListBox.Items.Add(filename);
                textureDict.Add(filename, texture);
                previewDict.Add(filename, image);
                
            }
        }

Was This Post Helpful? 0
  • +
  • -

#18 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1356
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: Problem following Nick Gravelyn's tile engine tutorial.

Posted 05 October 2012 - 08:21 AM

            // Find the index of the last slash in the path
            int index = filename.LastIndexOf("\\") + 1;
            // Create a substring from that point on
            string assetName = filename.Substring(index);
            // Remove extension from the string
            assetName = assetName.Remove(assetName.LastIndexOf("."));

            // Display the name of the tileset in a textbox without the file extension
            tileSetTextBox.Text = assetName;



Try the above code for stripping the excess pieces of the path. The first line finds the index of the first letter after the final \, the next line creates a new string from that index to the end of the path, the second line will remove anything after the . which will essentially just remove the file extension from it. The final line is just a way for me to display the current tileset that is being used into a textbox on the form. This is what I use to display the tileset that I use on my tile editor.

My tile editor is vastly different from what you'll have by following his tutorials since I decided after about 4A to do my own thing and use tilesets instead of each tile being in a separate image. Hopefully this code can help you get the name of the file without the extension for displaying.
Was This Post Helpful? 0
  • +
  • -

#19 ArchColossus  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 22-February 12

Re: Problem following Nick Gravelyn's tile engine tutorial.

Posted 12 October 2012 - 08:00 AM

Thank you, I've been relatively error free for a bit. But now I've gotten myself stuck. Again. I'm getting an IndexOutOfRange exception in my FrameAnimation class. Here's the code:
using System;
using Microsoft.Xna.Framework;

namespace TileEngine
{
    public class FrameAnimation : ICloneable
    {
        Rectangle[] frames;
        int currentFrame = 0;

        float frameLength = .5f;
        float timer = 0;

        public int FramesPerSecond
        {
            get
            {
                return (int) (1f / frameLength);
            }

            set
            {
                frameLength = (float)Math.Max(1f / (float)value, .01f);
            }
        }

        public Rectangle CurrentRect
        {
            get { return frames[currentFrame]; }
        }

        public int CurrentFrame
        {
            get { return currentFrame; }
            set
            {
                currentFrame = (int)MathHelper.Clamp(value, 0, frames.Length - 1);
            }
        }

        public FrameAnimation(
            int numberOfFrames,
            int frameWidth,
            int frameHeight,
            int xOffset,
            int yOffset)
        {
            frames = new Rectangle[numberOfFrames];

            for (int i = 0; i < numberOfFrames; i++)
            {
                Rectangle rect = new Rectangle();
                rect.Width = frameWidth;
                rect.Height = frameHeight;
                rect.X = xOffset + (1 * frameWidth);
                rect.Y = yOffset;

                frames[i] = rect;
            }
        }

        private FrameAnimation()
        {

        }

        public void Update(GameTime gameTime)
        {
            timer += (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (timer >= frameLength)
            {
                timer = 0f;

                currentFrame = currentFrame + 1 % frames.Length;
            }
        }

        public object Clone()
        {
            FrameAnimation anim = new FrameAnimation();

            anim.frameLength = frameLength;
            anim.frames = frames;

            return anim;
        }
    }
}


The exeption is shown in public Rectangle CurrentRect inside the get { return frames[currentFrame]; } I'm not sure how to go about fixing this, as I'm umfamiliar with the error. Apologies if I'm becoming a burden, but this place is quite helpful. Please let me know if you need any other parts of the project. Google has proved most unfruitful. I must have messed up something simple somewhere, as Nick doesn't get the exception. But I'm not sure what to try to fix with this one. '~'

This post has been edited by ArchColossus: 12 October 2012 - 08:04 AM

Was This Post Helpful? 0
  • +
  • -

#20 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1356
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: Problem following Nick Gravelyn's tile engine tutorial.

Posted 12 October 2012 - 08:16 AM

Where in your code are you actually giving a value to the rectangle array that you've named 'frames'? I don't see anything in the code you posted where you're adding adding any elements to the array, and with the way you've declared it, it has no elements. I didn't follow his tutorials that far, because as I stated before, I decided to vary from the tutorials at around the 4th part of the videos, so I don't have any source code for the animation stuff that he did.

Presumably you're creating an instance of the FrameAnimation class elsewhere, but possibly using the wrong constructor or the Clone method, neither of which define a number of elements for the array. Once you try to return the values of the array, you will hit that error because the compiler can't find any elements.

You've got a for loop in one of the constructors for this class, but my suspicion is that you're never using this loop because you're using the empty constructor or using the Clone method to create instances of this class.

One suggestion would be to extract that for loop out of the constructor and make it part of a new method in the class. Call this method from somewhere in the code where you're creating the instance or instances and it should populate the array.
Was This Post Helpful? 0
  • +
  • -

#21 ArchColossus  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 22-February 12

Re: Problem following Nick Gravelyn's tile engine tutorial.

Posted 18 October 2012 - 07:45 AM

It actually turned out to be a simple mistake of forgetting parenthesis around part of the update method. Whoops. Although now it isn't animating. I can't find anything missing, but there has to be something missing somewhere. Thank you for your help.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2