我在C#和XNA 4.0中制作游戏。它已经完成了,但现在我想添加设置,以便玩家可以根据需要更改窗口大小。目前的设置是这样的:如何缩放游戏的图形?
void Initialize()
{
//The window size is initally 800x480
graphics.PreferredBackBufferWidth = 800;
graphics.PreferredBackBufferHeight = 480;
graphics.ApplyChanges();
}
void Update()
{
//If the player completes an action, the window size is changed
if (windowSizeChanged)
{
graphics.PreferredBackBufferWidth = 1024;
graphics.PreferredBackBufferHeight = 720;
graphics.ApplyChanges();
}
}
使用此代码,这是本场比赛的样子在具体决议:
800X480
1024x720
,你可以希望看到,当窗口大小发生变化时,它不会影响游戏的实际图形。所有对象的精灵和hitbox保持相同的大小,所以他们在屏幕的角落里填满了一个小盒子,而不是整个窗口。任何人都可以告诉我如何缩放精灵以填满窗口?我认为我需要使用某种矩阵,但任何人都可以指出我的方向?
编辑:
这里是绘制代码。
void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
base.Draw(gameTime);
spriteBatch.Begin();
//Background texture drawn at default window size
spriteBatch.Draw(background, new Rectangle(0, 0, 800, 480), Color.White);
//Each object in the level (player, block, etc.) is drawn with a specific texture and a rectangle variable specifying the size and coordinates
//E.g. Each block is a size of 64x64 pixels and in this level they are placed at X-coordinates 0, 64, 128 and so on
//Each Y-coordinate for the blocks in this level is '480 - 64' (Window height minus block height)
foreach (/*Object in level*/)
{
spriteBatch.Draw(object.Texture, object.TextureSize, Color.White);
}
spriteBatch.End();
}
发布您的视口代码并绘制调用。 – RadioSpace
您可能会喜欢这篇文章:http://www.david-amador.com/2010/03/xna-2d-independent-resolution-rendering/ –