sharpdx / SharpDX-Samples

Official repository for all SharpDX Samples
346 stars 223 forks source link

gamesystem #12

Closed PedroAlvesq closed 9 years ago

PedroAlvesq commented 9 years ago

i can´t change the screen en click in the button

this is my code

using System;
using SharpDX;
using SharpDX.Toolkit;
using System.Collections.Generic;
using SharpDX.Toolkit.Input;
using TomShane.Neoforce.Controls;
using SharpDX.Direct3D;
using SharpDX.Direct3D11;
using SharpDX.Toolkit.Content;

namespace TomShane.Neoforce.Central
{

    // Use this namespace here in case we need to use Direct3D11 namespace as well, as this
    // namespace will override the Direct3D11.
    using SharpDX.Toolkit.Graphics;

    /// <summary>
    /// Simple MiniCube application using SharpDX.Toolkit.
    /// The purpose of this application is to show a rotating cube using <see cref="BasicEffect"/>.
    /// </summary>
    public class Gamer : Game
    {

        private GraphicsDeviceManager graphicsDeviceManager;
       private IScreen correntscreen;
       private List<IScreen> listscreen;
        private SpriteBatch spriteBatch;
     //   private SpriteBatch _sprite;
        private SpriteFont arial16BMFont;
   //     ScreenManager screenManager;

        // By preloading any assets used by UI rendering, we avoid framerate glitches
        // when they suddenly need to be loaded in the middle of a menu transition.
        static readonly string[] preloadAssets =
        {
            "gradient",
        };
        private PointerManager pointer;

        private Model model;

        private List<Model> models;

        private BoundingSphere modelBounds;
        private Matrix world;
        private Matrix view;
        private Matrix projection;

        #region Neoforce Controls

        private Manager NeoManager;

        private Window window;
        private Button virtualkeyboard = null;
        public Button LoginButton { get { return btnApply; } }
        public Button ExitButton { get { return btnExit; } }
        public string Username { get { return txusername.Text; } }
        public string Password { get { return txpassword.Text; } }
        private Button options { get { return option; } }
        private SideBarPanel pnlRes = null;
        private Button btnApply = null;
        private Button btnExit = null;
        private Button option = null;

        //private SideBarPanel pnlSkin = null;

        private ComboBox Face = null;
        //private SideBarPanel pnlStats = null;
        public Label lblObjects = null;
        public Label lblAvgFps = null;
        public Label lblFps = null;
        private TextBox txusername = null;
        private TextBox txpassword = null;
        private Label lbusername = null;
        private Label lbpassword = null;
        private SharpDX.Toolkit.Graphics.Texture2D defaultbg;
        #endregion

        /// <summary>
        /// Initializes a new instance of the <see cref="MiniCubeGame" /> class.
        /// </summary>
        public Gamer()
        {
            // Creates a graphics manager. This is mandatory.
            graphicsDeviceManager = new GraphicsDeviceManager(this);
            graphicsDeviceManager.PreferredGraphicsProfile = new FeatureLevel[] { FeatureLevel.Level_9_1, };
         //     NeoManager = new Manager(this, graphicsDeviceManager);
            pointer = new PointerManager(this);
          // screenManager = new ScreenManager(this);
            // Activate the first screens.

            // Setup the relative directory to the executable directory
            // for loading contents with the ContentManager

            Content.RootDirectory = "Content";
            IsMouseVisible = true;

        //   Components.Add(screenManager);

        }

        protected override void LoadContent()
        {
            //foreach (string asset in preloadAssets)
            //{
            //    Content.Load<object>(asset);
            //}
            arial16BMFont = Content.Load<SpriteFont>("Arial16");

            // Load the model (by default the model is loaded with a BasicEffect. Use ModelContentReaderOptions to change the behavior at loading time.
            models = new List<Model>();
            foreach (var modelName in new[] { "Dude", "Duck", "Car", "Happy", "Knot", "Skull", "Sphere", "Teapot", "helmet" })
            {
                model = Content.Load<Model>(modelName);

                // Enable default lighting  on model.
                BasicEffect.EnableDefaultLighting(model, true);

                models.Add(model);
            }
            model = models[0];

            // Instantiate a SpriteBatch
            spriteBatch = ToDisposeContent(new SpriteBatch(GraphicsDevice));
            // Creates a basic effect

            base.LoadContent();

        }

        protected override void Initialize()
        {

           listscreen = new List<IScreen>();
           listscreen.Add(new Login());
        //   listscreen.Add(new Server());
            Window.Title = "Firstgame";

             ChangeScreen("Server");
            base.Initialize();

        }

        protected override void Update(GameTime gameTime)
        {
          correntscreen.HandleEvent();
            correntscreen.Update(gameTime);
              /*  #region Neoforce Controls
             NeoManager.Update(gameTime);
            #endregion 
            */
            base.Update(gameTime);
            var pointerState = pointer.GetState();
            if (pointerState.Points.Count > 0 && pointerState.Points[0].EventType == PointerEventType.Released)
            {
                // Go to next model when pressing key space
                model = models[(models.IndexOf(model) + 1) % models.Count];
            }

            // Calculate the bounds of this model
            modelBounds = model.CalculateBounds();

            // Calculates the world and the view based on the model size
            const float MaxModelSize = 10.0f;
            var scaling = MaxModelSize / modelBounds.Radius;
            view = Matrix.LookAtRH(new Vector3(0, 0, MaxModelSize * 2.5f), new Vector3(0, 0, 0), Vector3.UnitY);
            projection = Matrix.PerspectiveFovRH(0.9f, (float)GraphicsDevice.BackBuffer.Width / GraphicsDevice.BackBuffer.Height, 0.1f, MaxModelSize * 10.0f);
            world = Matrix.Translation(-modelBounds.Center.X, -modelBounds.Center.Y, -modelBounds.Center.Z) * Matrix.Scaling(scaling) * Matrix.RotationY((float)gameTime.TotalGameTime.TotalSeconds);

        }

        protected override void Draw(GameTime gameTime)
        {

           //   NeoManager.BeginDraw(gameTime);
            GraphicsDevice.Clear(SharpDX.Color.CornflowerBlue);
         model.Draw(GraphicsDevice, world, view, projection);
            // Render the text
           spriteBatch.Begin();

       //    spriteBatch.Begin(SpriteSortMode.Deferred,GraphicsDevice.BlendStates.NonPremultiplied);

            //correntscreen.Draw(gameTime);
            spriteBatch.DrawString(arial16BMFont, "Press the pointer to switch models...\r\nCurrent Model: " + model.Name, new Vector2(16, 16), Color.White);
           spriteBatch.End();
            // NeoManager.EndDraw();

           correntscreen.Draw(gameTime);

            base.Draw(gameTime);

        }
        public void ChangeScreen(string screenName)
        {
            try
            {
                correntscreen = listscreen.Find(screen => (screen.name == screenName));
                correntscreen.Initialize(this);
                correntscreen.LoadContent(Content);
            }
            catch (Exception)
            {
                throw new NotImplementedException();

            }
        }
    }
}
public class Login:IScreen
    {
       private Gamer _game;
       private Manager NeoManager;
       private Window window;
       private LoginClient cliente;
       string user = null;
       string userPassword = null;
       private Button virtualkeyboard = null;
     //  private IScreen correntscreen;
     //  private List<IScreen> listscreen;
       // private ImageBox image = null;

       // public Texture2D ImageBox { get { return image.Image; } set { image.Image = value; } }
       ////////////////////////////////////////////////////////////////////////////       
       //private SharpDX.Toolkit.Graphics.Texture2D defaultbg;
      // private SharpDX.Toolkit.Graphics.Texture2D greenbg;

       //  private SideBar sidebar = null;
       public Button LoginButton { get { return btnApply; } }
       public Button ExitButton { get { return btnExit; } }
       public string Username { get { return txusername.Text; } }
       public string Password { get { return txpassword.Text; } }
       private Button options { get { return option; } }
       private SideBarPanel pnlRes = null;
       private Button btnApply = null;
       private Button btnExit = null;
       private Button option = null;

       //private SideBarPanel pnlSkin = null;

       //private SideBarPanel pnlStats = null;
       public Label lblObjects = null;
       public Label lblAvgFps = null;
       public Label lblFps = null;
       private TextBox txusername = null;
       private TextBox txpassword = null;
       private Label lbusername = null;
       private Label lbpassword = null;
      // private Gamer ola;
        public string name
        {
            get { return "Login"; }
        }

        public void Initialize(Gamer game)
        {

        //    ola.ChangeScreen("server");
            _game = game;
            NeoManager = new Manager(_game);
            NeoManager.Initialize();
            cliente = new LoginClient();
          ////  listscreen = new List<IScreen>();
           // listscreen.Add(new Server());

        }

        public void LoadContent(ContentManager manager)
        {
           window = new Window(NeoManager);
            window.Init();
            window.Text = "Login";
            window.Width = 420;
            window.Height = 243;
            window.Center();
            window.Visible = true;
            window.CloseButtonVisible = false;
            window.Resizable = false;
            window.Movable = false;
            NeoManager.Add(window);
            window.IconVisible = false;
            InitRes();

          }

        public void HandleEvent()
        {

        }

        public void Update(GameTime gametime)
        {
          //  correntscreen.HandleEvent();
          //  correntscreen.Update(gametime);

            NeoManager.Update(gametime);

        }

        public void Draw(GameTime gametime)
        {

        NeoManager.BeginDraw(gametime);
           _game.GraphicsDevice.Clear(SharpDX.Color.AliceBlue);

          NeoManager.EndDraw();
       //   correntscreen.Draw(gametime);
        }

    ////////////////////////////////////////////////////////////////////////////   

    ////////////////////////////////////////////////////////////////////////////
        private void InitRes()
        {

            pnlRes = new SideBarPanel(NeoManager);
            pnlRes.Init();
            pnlRes.Passive = true;
            pnlRes.Parent = window;
            pnlRes.Left = 0;
            pnlRes.Top = 0;
            pnlRes.Width = 410;
            pnlRes.Height = 210;
            pnlRes.CanFocus = false;
            //   Manager.Add(pnlRes);
            virtualkeyboard = new Button(NeoManager);
            virtualkeyboard.Init();
            virtualkeyboard.Width = 40;
            virtualkeyboard.Height = 30;
            virtualkeyboard.Parent = pnlRes;
            virtualkeyboard.Top = pnlRes.Left + 10;
            virtualkeyboard.Left = pnlRes.Top + 283;
            //virtualkeyboard.TextColor = new SharpDX.Color(20, 22, 23);
            //  virtualkeyboard.Text = "Teclado";

            //    virtualkeyboard.Glyph = new Glyph()
            virtualkeyboard.Glyph = new Glyph(NeoManager.Content.Load<SharpDX.Toolkit.Graphics.Texture2D>("Content\\Images\\teclado"));
            lbusername = new Label(NeoManager);
            lbusername.Init();

            lbusername.Width = 100;
            lbusername.Parent = pnlRes;
            lbusername.Top = pnlRes.Left + 10;
            lbusername.Left = pnlRes.Top + 8;

            lbusername.Text = "USERNAME";
            lbusername.TextColor = new SharpDX.Color(250, 250, 250);
            //  Manager.Graphics.ApplyChanges();
            //
            //  Manager.Add(lbusername);

            txusername = new TextBox(NeoManager);
            txusername.Init();
            txusername.Width = 200;
            txusername.Parent = pnlRes;
            txusername.Top = pnlRes.Left + 8;
            txusername.Left = pnlRes.Top + 80;
            txusername.Text.Trim();
            txusername.TextColor = new SharpDX.Color(250, 250, 250);
            lbpassword = new Label(NeoManager);
            lbpassword.Init();
            lbpassword.Width = 100;
            lbpassword.Parent = pnlRes;
            lbpassword.Top = pnlRes.Left + 30;
            lbpassword.Left = pnlRes.Top + 8;

            lbpassword.Text = "PASSWORD";
            lbpassword.TextColor = new SharpDX.Color(250, 250, 250);
            txpassword = new TextBox(NeoManager);
            txpassword.Init();
            txpassword.Width = 200;
            txpassword.Parent = pnlRes;
            txpassword.Top = pnlRes.Left + 30;
            txpassword.Left = pnlRes.Top + 80;
            txpassword.Mode = TextBoxMode.Password;

            txpassword.PasswordChar = '*';

            txpassword.Text.Trim();
            txpassword.TextColor = new SharpDX.Color(250, 250, 250);

            btnApply = new Button(NeoManager);
            btnApply.Init();
            btnApply.Width = 80;
            btnApply.Parent = pnlRes;
            btnApply.Left = pnlRes.Left + 50;
            btnApply.Top = pnlRes.Top + 80;
            btnApply.Text = "Login";
            btnApply.TextColor = new SharpDX.Color(250, 250, 250);
            btnApply.Click += new Controls.EventHandler(btnApply_Click);
            option = new Button(NeoManager);
            option.Init();
            option.Width = 80;
            option.Parent = pnlRes;
            option.Left = btnApply.Left + btnApply.Width + 8;
            option.Top = pnlRes.Top + 80;
            option.Text = "Options";
            option.TextColor = new SharpDX.Color(250, 250, 250);
            option.Click += new Controls.EventHandler(options_click);
            btnExit = new Button(NeoManager);
            btnExit.Init();
            btnExit.Width = 80;
            btnExit.Parent = pnlRes;
            btnExit.Left = option.Left + option.Width + 8;
            btnExit.Top = pnlRes.Top + 80;
            btnExit.Text = "Exit";
            btnExit.TextColor = new SharpDX.Color(250, 250, 250);
            btnExit.Click += new Controls.EventHandler(btnExit_Click1);

        }
    ////////////////////////////////////////////////////////////////////////////

    ////////////////////////////////////////////////////////////////////////////    

    void btnExit_Click1(object sender, Controls.EventArgs e)
    {

        System.Windows.Forms.DialogResult dialogResult = System.Windows.Forms.MessageBox.Show("Do you really want to exit ", "", System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Question);
        if (dialogResult == System.Windows.Forms.DialogResult.Yes)
        {

            System.Windows.Forms.Application.Exit();
        }

    }

   void options_click(object sender, Controls.EventArgs e)
    {

        Option ola = new Option(NeoManager);

        ola.Init();
        ola.Show();
        ola.Width = 800;
        ola.Height = 600;
        ola.Text = "Options";
        ola.Center();
        ola.CloseButtonVisible = false;
        ola.Movable = false;
        ola.Visible = true;
        ola.Resizable = false;
        NeoManager.Add(ola);
    }

    public void ProcessComplete1(Headers header, Rank rank)
    {
        string msg = "";
        //  System.Diagnostics.Debug.Write(Erro);
        if (header != Headers.Rank)
        {

            return;
        }

        else if (rank == Rank.Administrador)
        {

            txusername.Text = "";
            txpassword.Text = "";
            msg = "Entrou como Admiministrador";
            System.Windows.Forms.MessageBox.Show(msg, "", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
        }

        else if (rank == Rank.player)
        {
            msg = "Entrou com Player";
            System.Windows.Forms.MessageBox.Show(msg, "", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
        }

        else if (rank == Rank.Moderador)
        {
            msg = "Entrou Como Moderador";
            System.Windows.Forms.MessageBox.Show(msg, "", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
        }
        else if (rank == Rank.Vip)
        {

            msg = "Entrou Como VIP";
            System.Windows.Forms.MessageBox.Show(msg, "", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);

        }
        //else if (rank == Rank.Banned)
        //{
        //    // txusername.Text = "";
        //    //txpassword.Text = "";
        //    msg = "Jogador Banido";
        //    System.Windows.Forms.MessageBox.Show(msg, "", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);

        //}

    }

    public void ProcessComplete(Headers header, ErrorCodes Erro)
    {
        string msg = "";
        System.Diagnostics.Debug.Write(Erro);
        if (header != Headers.Login)
        {

            return;
        }

        else if (Erro == ErrorCodes.Sucess)
        {

            txusername.Text = "";
            txpassword.Text = "";
            window.Hide();
            World ola = new World(NeoManager, user);

            ola.Init();
            ola.Show();
            ola.Width = 800;
            ola.Height = 600;
            ola.Center();
            ola.CloseButtonVisible = false;
            ola.Movable = false;
            ola.Visible = true;
            ola.Resizable = false;
            cliente.Nivel(user, ProcessComplete1);

        }

        else if (Erro == ErrorCodes.InvalidLogin)
        {
            msg = "Password ou username is incorret";
            System.Windows.Forms.MessageBox.Show(msg, "", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
            txusername.Text = "";
            txpassword.Text = "";

        }

        else if (Erro == ErrorCodes.Error)
        {
            txusername.Text = "";
            txpassword.Text = "";
            msg = "Server Is Offline";
            System.Windows.Forms.MessageBox.Show(msg, "", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);

        }

    }

    public static String Create(String passwd)
    {
        var rnd = new Random();
        var saltBuf = new StringBuilder();
        const string seedList = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

        for (int i = 0; i < 32; i++)
        {
            int rndIndex = rnd.Next(62);
            saltBuf.Append(seedList.Substring(rndIndex, 1));
        }

        String salt = saltBuf.ToString();

        return CreateMd5Hash(passwd);
    }

    private static String CreateMd5Hash(String data)
    {
        byte[] bdata = new byte[data.Length];
        byte[] hash;
        for (int i = 0; i < data.Length; i++)
        {
            bdata[i] = (byte)(data[i] & 0xff);
        }
        try
        {
            MD5CryptoServiceProvider md5Provider = new MD5CryptoServiceProvider();
            hash = md5Provider.ComputeHash(bdata);
        }
        catch (SecurityException e)
        {
            throw new ApplicationException("A security encryption error occured.", e);
        }
        var result = new StringBuilder(32);
        foreach (byte t in hash)
        {
            String x = (t & 0xff).ToString("X").ToLowerInvariant();
            if (x.Length < 2)
            {
                result.Append("0");
            }
            result.Append(x);
        }
        System.Console.WriteLine(result);
        return result.ToString();
    }
    private static String CalculateHash(string password, string salt)
    {
        byte[] data = System.Text.Encoding.ASCII.GetBytes(salt + password);
        data = System.Security.Cryptography.MD5.Create().ComputeHash(data);

        return BitConverter.ToString(data).Replace("-", "") + ":" + salt;
    }

    void btnApply_Click(object sender, Controls.EventArgs e)
    {

        user = txusername.Text.Trim();
        userPassword = txpassword.Text.Trim();

        if ((user.Equals(String.Empty)) || (userPassword.Equals(String.Empty)))
        {
            System.Windows.Forms.MessageBox.Show("Username Ou Password is empty ", "", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);

        }
        else
        {
            window.Hide();
          // _game.GraphicsDevice.Clear(SharpDX.Color.CornflowerBlue);

            Gamer ola = new Gamer();
            ola.ChangeScreen("Server");

         /*   World ola = new World(NeoManager,user);

            ola.Init();
            ola.Show();
            ola.Width = 800;
            ola.Height = 600;

            ola.Center();
            ola.CloseButtonVisible = false;
            ola.Movable = false;
            ola.Visible = true;
            ola.Resizable = false;
            */
       //     cliente.Login(user, userPassword, ProcessComplete);

        }
        ////////////////////////////////////////////////////////////////////////////

        ////////////////////////////////////////////////////////////////////////////    

    }
    }
}
using SharpDX.Toolkit;
using SharpDX.Toolkit.Content;
using SharpDX.Toolkit.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TomShane.Neoforce.Central
{

    interface IScreen
    {
        string name { get; }

        void Initialize(Gamer game);

          void LoadContent(ContentManager manager);

          void HandleEvent();
          void Update(GameTime gametime);
          void Draw(GameTime gametime);
    }
}
PedroAlvesq commented 9 years ago

i find i away to change the screen but when i load a model i have this result i using neoforce controls to make my game r

xoofx commented 9 years ago

Sorry, we don't have time to help debug or dig into your code.