Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: View not moving with sprite for some reason  (Read 4452 times)

0 Members and 1 Guest are viewing this topic.

StormWingDelta

  • Sr. Member
  • ****
  • Posts: 365
    • View Profile
View not moving with sprite for some reason
« on: April 04, 2014, 03:45:44 pm »
Not sure what I missed but whatever it is it has to be so simple that it is laughing at me from some dark hole in the universe. >.>




class SFMLMovingViewTest
    {
        RenderWindow window;
        View movingview;
        Font exfont;
        Text extext2, extext3;
        MovingSprite movingspriteplayer;
        public SFMLMovingViewTest()
        {
            exfont = new Font("Fonts/00Starmap/00TT.TTF");
            extext2 = new Text("Key Pressed: ", exfont, 15);
            extext3 = new Text("Key Released: ", exfont, 15);
           
            Image image = new Image((uint)TestImages.TestShip.Width,
                (uint)TestImages.TestShip.Height,
                GetRGBValues(TestImages.TestShip));
            movingspriteplayer = new MovingSprite(new Texture(image));
        }

        public void StartSFMLProgram3()
        {
            VideoMode vm = new VideoMode(800, 600);
            window = new RenderWindow(vm , "SFML C# Example");
            movingview = new View(new FloatRect(0, 0, vm.Width, vm.Height));
            window.SetView(movingview);
            Image image = new Image((uint)TestImages.OpenGridFourWay.Width,
                (uint)TestImages.OpenGridFourWay.Height,
                GetRGBValues(TestImages.OpenGridFourWay));
           
            Sprite exsprite = new Sprite(new Texture(image));
            movingspriteplayer.Position = new Vector2f(vm.Width / 2, vm.Height / 2);
           
           

            Text extext = new Text("My Font!!!", exfont, 15);
           
            extext2.Position = new Vector2f(0,50);
            extext3.Position = new Vector2f(0, 70);
           

            window.Closed += new EventHandler(OnClosed);
            window.KeyPressed += new EventHandler<KeyEventArgs>(KeyEventPressed);
            window.KeyReleased += new EventHandler<KeyEventArgs>(KeyEventReleased);
           

            while (window.IsOpen())
            {
               
               
                window.DispatchEvents();

                movingspriteplayer.MoveSprite(1,1);
                //movingview.Move(movingspriteplayer.Origin);
                movingview.Center = movingspriteplayer.Position;
                window.SetView(window.GetView());
                extext.DisplayedString = "" + movingspriteplayer.Position.ToString();
                window.Clear();
                window.Draw(exsprite);
                window.Draw(movingspriteplayer);
                window.Draw(extext);
                //window.Draw(extext2);
                //window.Draw(extext3);

                window.Display();
            }

        }

        void OnClosed(object sender, EventArgs e)
        {
            window.Close();
        }

        void KeyEventPressed(object sender, KeyEventArgs e)
        {
            extext2.DisplayedString = "Key Pressed: " + e.Code;

        }
        void KeyEventReleased(object sender, KeyEventArgs e)
        {
            extext3.DisplayedString = "Key Released: " + e.Code;

        }
       

        //http://stackoverflow.com/questions/7350679/convert-a-bitmap-into-a-byte-array-in-c
        private static byte[] ImageToByteArray(System.Drawing.Image resourceimage)
        {
            System.Drawing.ImageConverter imgcon = new System.Drawing.ImageConverter();
            return (byte[])imgcon.ConvertTo(resourceimage, typeof(byte[]));
        }

        // Yes Straight up C&P from here:
        //http://social.msdn.microsoft.com/Forums/vstudio/en-US/47c5a003-1d26-4213-9370-fba3aa170c21/fastest-method-to-convert-bitmap-object-to-byte-array?forum=csharpgeneral
        // A Case of either use full quantified names for SFML or C#. >.>
        private static byte[] GetRGBValues(System.Drawing.Bitmap bmp)
        {

            // Lock the bitmap's bits.
            System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height);
            System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, bmp.PixelFormat);

            // Get the address of the first line.
            IntPtr ptr = bmpData.Scan0;

            // Declare an array to hold the bytes of the bitmap.
            int bytes = bmpData.Stride * bmp.Height;
            byte[] rgbValues = new byte[bytes];

            // Copy the RGB values into the array.
            System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
            bmp.UnlockBits(bmpData);

            return rgbValues;
        }
    }
   
    public class MovingSprite : Sprite
    {

        public MovingSprite() : base()
        {}
        public MovingSprite(Texture texture)
            : base(texture)
        {

            CenterOrigin();
        }
        public MovingSprite(Texture texture, IntRect intrect)
            : base(texture, intrect)
        {
            CenterOrigin();
        }
        public MovingSprite(MovingSprite copy)
            : base(copy)
        { }

        public void CenterOrigin()
        {
            if(this.Texture != null && !this.TextureRect.Equals(null))
            {
                this.Origin = new Vector2f(this.TextureRect.Width / 2, this.TextureRect.Height / 2);
               
            }
        }

        public void MoveSprite(float xvec, float yvec)
        {
            this.Position += new Vector2f(xvec,yvec);
        }
        public void MoveSpriteByDirection(float angledirection, float speed)
        {
            //MoveSprite();
        }

    }

 

No clue what I'm missing.  Been trying to get the view to move with the sprite in question but this is my latest attempt at it and it isn't working. >.>
I have many ideas but need the help of others to find way to make use of them.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: View not moving with sprite for some reason
« Reply #1 on: April 04, 2014, 04:21:29 pm »
window.SetView(window.GetView());

And the purpose of setting the view to the exact same current view is what?
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

StormWingDelta

  • Sr. Member
  • ****
  • Posts: 365
    • View Profile
Re: View not moving with sprite for some reason
« Reply #2 on: April 04, 2014, 06:08:18 pm »
window.SetView(window.GetView());

And the purpose of setting the view to the exact same current view is what?
lol forgot to remove that line.  Still doesn't work though.  >.>
I have many ideas but need the help of others to find way to make use of them.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: View not moving with sprite for some reason
« Reply #3 on: April 04, 2014, 06:10:28 pm »
You removed and replaced with what?
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

StormWingDelta

  • Sr. Member
  • ****
  • Posts: 365
    • View Profile
Re: View not moving with sprite for some reason
« Reply #4 on: April 04, 2014, 06:15:36 pm »
Nothing right now since I don't know what I'm missing to make the view move with the sprite. 

Here's what my while loop looks like now.

while (window.IsOpen())
            {
               
               
                window.DispatchEvents();

                movingspriteplayer.MoveSprite(0,-0.25f);

                movingview.Move(new Vector2f(movingspriteplayer.Origin.X, movingspriteplayer.Origin.Y));//movingspriteplayer.Origin
                //movingview.Center = movingspriteplayer.Position;
                //window.SetView(window.GetView());
                extext.DisplayedString = "" + movingspriteplayer.Position.ToString();
                window.Clear();
                window.Draw(exsprite);
                window.Draw(movingspriteplayer);
                window.Draw(extext);
                //window.Draw(extext2);
                //window.Draw(extext3);


                window.Display();
            }

 
I have many ideas but need the help of others to find way to make use of them.

StormWingDelta

  • Sr. Member
  • ****
  • Posts: 365
    • View Profile
Re: View not moving with sprite for some reason
« Reply #5 on: April 04, 2014, 06:24:21 pm »
hmm just noticed something.  When I grab the view's coords it shows that it is moving but the area being seen by the screen isn't changing.   ??? ??? ???
I have many ideas but need the help of others to find way to make use of them.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: View not moving with sprite for some reason
« Reply #6 on: April 04, 2014, 06:34:01 pm »
Why don't people just read the tutorials....

And yes it is in red in the tutorial
Quote
When you call setView, the render-target keeps a copy of the view, not a pointer to the original one. So whenever you update your view, you need to call setView again to apply the modifications.
Don't be afraid to copy views or to create them on the fly, they are lightweight objects (they just hold a few floats).

Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

StormWingDelta

  • Sr. Member
  • ****
  • Posts: 365
    • View Profile
Re: View not moving with sprite for some reason
« Reply #7 on: April 04, 2014, 06:42:22 pm »
Guess it would help if I fully read that.  Was looking through that and forgot to read that.  >.>
I have many ideas but need the help of others to find way to make use of them.