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

Author Topic: SFML on Custom WinForms User Control?  (Read 3723 times)

0 Members and 1 Guest are viewing this topic.

Shadowblitz16

  • Newbie
  • *
  • Posts: 13
    • View Profile
SFML on Custom WinForms User Control?
« on: May 06, 2017, 01:08:25 am »
can someone explain how to render a sfml canvas on a custom winforms user control?

I am trying to make so that it renders correctly when scaling, resizing, and positioning inside the vs winforms editor.

of course I want it to have a 1x, 2x, 3x, and 4x scaling options inside the editor.

this is my current code but it doesn't draw properly
Code: [Select]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using SFML;
using SFML.System;
using SFML.Window;
using SFML.Graphics;


namespace TileEditor1.Classes
{
    public partial class TileMap : Control
    {

        private VideoMode mode;
        private RenderWindow window;
        protected uint tileScale = 1;
        protected uint tileWidth = 16;
        protected uint tileHeight = 16;
        protected uint tileCols = 10;
        protected uint tileRows = 9;

        public TileMap()
        {
            InitializeComponent();

            mode = new VideoMode(tileCols * (tileWidth * tileScale), tileRows * (tileHeight * tileScale));
            window = new RenderWindow(mode, "SFML Editor");

            TileMap_Resize();
            TileMap_Update();

        }

        #region Properties
        public uint TileScale
        {
            get { return tileScale; }
            set { tileScale = value; TileMap_Resize(); }
        }

        public uint TileWidth
        {
            get { return tileWidth; }
            set { tileWidth= value; TileMap_Resize(); }
        }

        public uint TileHeight
        {
            get { return tileHeight ; }
            set { tileHeight = value; TileMap_Resize(); }
        }
        public uint TileColumns
        {
            get { return tileCols; }
            set { tileCols = value; TileMap_Resize(); }
        }
        public uint TileRows
        {
            get { return tileRows; }
            set { tileRows = value; TileMap_Resize(); }
        }
        #endregion

        #region Methods

        public void TileMap_Update()
        {
            while(Visible)
            {
                System.Windows.Forms.Application.DoEvents();
                window.DispatchEvents();
                window.Clear(SFML.Graphics.Color.Black);
                window.Display();
            }
        }

        public void TileMap_Resize()
        {
            Size = new Size((int)(tileCols * (tileWidth * TileScale)), (int)(tileRows * (tileHeight * tileScale)));
            window.Size = new Vector2u((tileCols * (tileWidth * TileScale)), (tileRows * (tileHeight * tileScale)));
        }

        #endregion

        #region Events

        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            // don't call base.OnPaint(e) to prevent forground painting
            // base.OnPaint(e);
        }
        protected override void OnPaintBackground(System.Windows.Forms.PaintEventArgs pevent)
        {
            // don't call base.OnPaintBackground(e) to prevent background painting
            //base.OnPaintBackground(pevent);
        }
    }

    #endregion
}