调整大小均匀的在XNA一个窗口 [英] Uniformly Resizing a Window in XNA

查看:108
本文介绍了调整大小均匀的在XNA一个窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我想给我的游戏窗口,以调整均匀的能力。我已签到处,但我似乎无法找到任何事情。

Well, I am trying to give my game's window the ability to resize uniformly. I have checked everywhere but I can't seem to find anything about it.

任何想法?

我不能代码后,由于字符的限制。这将不胜感激,如果有人可以请帮我,看看我做错了:)

I can't post the code due to the character limit. It would be much appreciated if someone could please help me out and see what I am doing wrong :)

也有利于将如何调整后备缓冲当这种情况发生,因为我不认为比赛会是可玩只精灵一半可见:)

Also helpful would be how to resize the backbuffer when this happens, as I don't think the game would be playable with only half the sprites visible :)

   void Window_ClientSizeChanged( object sender, EventArgs e )
   {
       int new_width = graphics.GraphicsDevice.Viewport.Width;
       int new_height = graphics.GraphicsDevice.Viewport.Height;

       if (new_width != Variables.SCREEN_WIDTH)
       {
           Variables.SCREEN_HEIGHT = (int)(new_width * ascept_ratio);
           Variables.SCREEN_WIDTH = new_width;
       }
       if (new_height != Variables.SCREEN_HEIGHT)
       {
           Variables.SCREEN_WIDTH = (int)(new_height / ascept_ratio);
           Variables.SCREEN_HEIGHT = new_height;
       }

       UpdateParameters();
   }



...

...

   public void UpdateParameters()
   {
       graphics.PreferredBackBufferWidth = Variables.SCREEN_WIDTH;
       graphics.PreferredBackBufferHeight = Variables.SCREEN_HEIGHT;
       graphics.ApplyChanges();
   }



谢谢,

Thanks,

亲切的问候,Darestium

Kind Regards, Darestium

推荐答案

要保持纵横比,你是什么意思?

To keep the aspect ratio you mean?

您会做这一样的WinForms项目:

You'd do this the same as any WinForms project:

在表单负荷高达存储方面单选:(浮点)宽度/(浮点)高度。在XNA,这可能是在 LoadContent 你的游戏(因为窗口会被再创造)。

When the form loads up store the aspect radio: (float)Width/(float)Height. In XNA, this could be in the LoadContent of your game (since the window would be created by then).

然后,处理表单的时,SizeChanged 事件。你需要跟踪用户是否正在改变高度,宽度或两者兼而有之。如果它的高度,然后设置宽度=身高/ ASPECTRATIO ,如果宽度改变设置高度=宽度* ASPECTRATIO

Then, handle the form's sizechanged event. You'll need to keep track of whether the user is changing height, width or both. If it's height, then set the Width = Height / AspectRatio, if the width changes set Height = Width * AspectRatio.

如果这两个变化,那么无论是宽度还是高度决定的,(我的意思是挑选任何一个设计,而不是每个调整大小),并与上述事情。

If both change, then decide on either width or height, (i mean pick either one in design, not each resize) and do as above.

您可能会不得不做的事情XNA特定一旦你做到了这一点,如调整后备缓冲等,但这个ISN'具体到这个问题牛逼,所以我会离开它(问另外一个问题如果需要的话)。

You'll probably have to do things XNA-specific once you've done this, such as resize the backbuffer etc. but this isn't specific to this question so i'll leave it out (ask another question if need be).

编辑。下面是一个最小的,工作样品:

它保持高宽比,以及通过绘制到调整大小的图形渲染目标窗口的原始尺寸,然后绘图缩放到适合新窗口。如果你不希望此取下覆盖 BeginDraw EndDraw 方法。

It maintains aspect ratio, as well as resizes the graphics by drawing to a render target the original size of the window, then drawing that scaled to fit the new window. If you don't want this remove the overridden BeginDraw and EndDraw methods.

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace WindowsGame1
{
    public class Game1 : Game
    {
        GraphicsDeviceManager Graphics;
        float AspectRatio;
        Point OldWindowSize;
        Texture2D BlankTexture;
        RenderTarget2D OffScreenRenderTarget;
        SpriteBatch SpriteBatch;

        public Game1()
        {
            Graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            Graphics.IsFullScreen = false;
            Window.AllowUserResizing = true;
            Window.ClientSizeChanged += new EventHandler<EventArgs>(Window_ClientSizeChanged);
        }

        void Window_ClientSizeChanged(object sender, EventArgs e)
        {
            // Remove this event handler, so we don't call it when we change the window size in here
            Window.ClientSizeChanged -= new EventHandler<EventArgs>(Window_ClientSizeChanged);

            if (Window.ClientBounds.Width != OldWindowSize.X)
            { // We're changing the width
                // Set the new backbuffer size
                Graphics.PreferredBackBufferWidth = Window.ClientBounds.Width;
                Graphics.PreferredBackBufferHeight = (int)(Window.ClientBounds.Width / AspectRatio);
            }
            else if (Window.ClientBounds.Height != OldWindowSize.Y)
            { // we're changing the height
                // Set the new backbuffer size
                Graphics.PreferredBackBufferWidth = (int)(Window.ClientBounds.Height * AspectRatio);
                Graphics.PreferredBackBufferHeight = Window.ClientBounds.Height;
            }

            Graphics.ApplyChanges();

            // Update the old window size with what it is currently
            OldWindowSize = new Point(Window.ClientBounds.Width, Window.ClientBounds.Height);

            // add this event handler back
            Window.ClientSizeChanged += new EventHandler<EventArgs>(Window_ClientSizeChanged);
        }

        protected override void LoadContent()
        {
            // Set up initial values
            AspectRatio = GraphicsDevice.Viewport.AspectRatio;
            OldWindowSize = new Point(Window.ClientBounds.Width, Window.ClientBounds.Height);

            BlankTexture = new Texture2D(GraphicsDevice, 1, 1);
            BlankTexture.SetData(new Color[] { Color.FromNonPremultiplied(255, 255, 255, 255) });
            SpriteBatch = new SpriteBatch(GraphicsDevice);

            OffScreenRenderTarget = new RenderTarget2D(GraphicsDevice, Window.ClientBounds.Width, Window.ClientBounds.Height);
        }

        protected override void UnloadContent()
        {
            if (OffScreenRenderTarget != null)
                OffScreenRenderTarget.Dispose();

            if (BlankTexture != null)
                BlankTexture.Dispose();

            if (SpriteBatch != null)
                SpriteBatch.Dispose();

            base.UnloadContent();
        }

        protected override bool BeginDraw()
        {
            GraphicsDevice.SetRenderTarget(OffScreenRenderTarget);
            return base.BeginDraw();
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            SpriteBatch.Begin();
            SpriteBatch.Draw(BlankTexture, new Rectangle(100, 100, 100, 100), Color.White);
            SpriteBatch.End();
            base.Draw(gameTime);
        }

        protected override void EndDraw()
        {
            GraphicsDevice.SetRenderTarget(null);
            SpriteBatch.Begin();
            SpriteBatch.Draw(OffScreenRenderTarget, GraphicsDevice.Viewport.Bounds, Color.White);
            SpriteBatch.End();
            base.EndDraw();
        }
    }
}

这篇关于调整大小均匀的在XNA一个窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆