SFML community forums

Help => Window => Topic started by: Mad Engineer on January 19, 2014, 04:17:40 pm

Title: .Net resize window [Solved]
Post by: Mad Engineer on January 19, 2014, 04:17:40 pm
I want to know how to re-size view when i re-size pictureBox.

This is my code so far, but it doesn't work well:

private RenderWindow window = null;

SFML.Graphics.View view = new SFML.Graphics.View();

window = new RenderWindow(pictureBox1.Handle);

view.Reset(new FloatRect(0f, 0f, pictureBox1.Width, pictureBox1.Height));
           
window.SetView(view);

private void Form1_Resize(object sender, EventArgs e)
{
            view.Reset(new FloatRect(0f, 0f, pictureBox1.Width, pictureBox1.Height));
            window.SetView(view);
 
}

When i start program everything is great, but when i re-size window(pictureBox) all Sprites start to get wider when i reduce size and smaller when i increase size.
Title: Re: .Net resize window
Post by: zsbzsb on January 19, 2014, 05:17:20 pm
Are you sure the resized event handler gets called?

And also, maybe you should handle the picture box resized instead of the form resized (since SFML is hosted in the picturebox and not the form).
Title: Re: .Net resize window
Post by: Mad Engineer on January 19, 2014, 05:25:06 pm
Yes it is called i tested it with changing text in label. And i have tried to put it in pictureBox_Resize but still get same result;
Title: Re: .Net resize window
Post by: zsbzsb on January 19, 2014, 05:45:12 pm
Can you post a complete and minimal example (something that I can copy and paste) that shows the problem? I have a few ideas what may be wrong, but first I need to see how you handle your form/drawing/events.
Title: Re: .Net resize window
Post by: Mad Engineer on January 19, 2014, 05:54:49 pm
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SFML.Window;
using SFML.Graphics;

namespace Test
{
    public partial class Form1 : Form
    {
        private RenderWindow window = null;

        SFML.Graphics.View view = new SFML.Graphics.View();

        float mousePositionXpictureBox;
        float mousePositionYpictureBox;

        float mousePositionXMap;
        float mousePositionYMap;

        bool mouseInRender = false;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            window = new RenderWindow(pictureBox1.Handle);

            view.Reset(new FloatRect(0f, 0f, pictureBox1.Width, pictureBox1.Height));
            window.SetView(view);

            timer1.Enabled = true;

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            window.Clear(SFML.Graphics.Color.Red);

            //DRAW BEGIN

           

            //DRAW END
            window.Display();

            if (mouseInRender == true)
            {
                if (mousePositionXpictureBox < 100)
                {
                    view.Move(new Vector2f(-5f, 0f));
                    window.SetView(view);
                   
                }
                if (mousePositionXpictureBox > pictureBox1.Width - 100f)
                {
                    view.Move(new Vector2f(5f, 0f));
                    window.SetView(view);
                   
                }

                if (mousePositionYpictureBox < 100)
                {
                    view.Move(new Vector2f(0f, -5f));
                    window.SetView(view);
                   
                }
                if (mousePositionYpictureBox > pictureBox1.Height - 100f)
                {
                    view.Move(new Vector2f(0, 5f));
                    window.SetView(view);
                   
                }
            }

           

        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            mousePositionXpictureBox = e.X;
            mousePositionYpictureBox = e.Y;

            mousePositionXMap = e.X + view.Center.X - pictureBox1.Width / 2;
            mousePositionYMap = e.Y + view.Center.Y - pictureBox1.Height / 2;

        }

        private void pictureBox1_MouseLeave(object sender, EventArgs e)
        {
            mouseInRender = false;
        }

        private void pictureBox1_MouseEnter(object sender, EventArgs e)
        {
            mouseInRender = true;
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            view.Reset(new FloatRect(0f, 0f, pictureBox1.Width, pictureBox1.Height));
            window.SetView(view);
        }

    }
}
Title: Re: .Net resize window
Post by: zsbzsb on January 19, 2014, 06:11:05 pm
In your timer1_Tick you need to call window.DispatchEvents().

Also, I don't recommend using a timer to do your SFML drawing. I recommend you initialize SFML from outside the form as in the example I posted here (http://en.sfml-dev.org/forums/index.php?topic=12466.msg87073#msg87073). This can have a standard drawing loop with WinForms.  ;)
Title: Re: .Net resize window
Post by: Mad Engineer on January 19, 2014, 06:13:52 pm
omg , dat line of code XD . Ty so much for help :D