# [XNA] Sprite map squised

This topic is 2603 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

So i figured i could make a map on my own and further build on that but i'm already running into problems.

My tile sprites are 16*16, and within my map class i'm building a 3*3 array for the map (yeah i start off really simple ).
--
I put my backbuffer width/height to that array maps x/y * 16.

Load in the content and sending the textures to the map class that puts 'm into a list.

finally i draw the map.
--
Now when i run the program the window is 24 heigh and 48 wide and the textures are all squised in the Y axis. I rechecked everything a couple of times but i can't find the source. It's weird as the backbuffer uses a get field in the map class time 16. And in the draw mathod i'm just hardcoding the width and height.

Game1.cs
 using System; using System.Collections.Generic; using System.Linq; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.GamerServices; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Media; namespace MyMapTest { /// <summary> /// This is the main type for your game /// </summary> public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; Map map = new Map(); public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; graphics.PreferredBackBufferWidth = map.Width * 16; graphics.PreferredBackBufferHeight = map.Height * 16; graphics.ApplyChanges(); IsMouseVisible = true; } /// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { // TODO: Add your initialization logic here base.Initialize(); } /// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); Texture2D jungle01 = Content.Load<Texture2D>(@"Tiles\Jungle01"); Texture2D jungle02 = Content.Load<Texture2D>(@"Tiles\Jungle02"); Texture2D jungle03 = Content.Load<Texture2D>(@"Tiles\Jungle03"); Texture2D jungle04 = Content.Load<Texture2D>(@"Tiles\Jungle04"); map.AddTexture(jungle01); map.AddTexture(jungle02); map.AddTexture(jungle03); map.AddTexture(jungle04); // TODO: use this.Content to load your game content here } /// <summary> /// UnloadContent will be called once per game and is the place to unload /// all content. /// </summary> protected override void UnloadContent() { // TODO: Unload any non ContentManager content here } /// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); // TODO: Add your update logic here base.Update(gameTime); } /// <summary> /// This is called when the game should draw itself. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); // TODO: Add your drawing code here spriteBatch.Begin(); map.Draw(spriteBatch); spriteBatch.End(); base.Draw(gameTime); } } } 

Map.cs
 using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; namespace MyMapTest { public class Map { private List<Texture2D> tileTextures = new List<Texture2D>(); int[,] mapArray = new int[,] { {0,1,2,}, {2,1,3,}, {1,0,2,}, }; public int Width { get { return mapArray.GetLength(1); } } public int Height { get { return mapArray.GetLength(0); } } public void AddTexture(Texture2D texture) { tileTextures.Add(texture); } public void Draw(SpriteBatch spriteBatch) { for (int x = 0; x < Width; x++) { for (int y = 0; y < Height; y++) { int textureIndex = mapArray[x, y]; Texture2D texture = tileTextures[textureIndex]; spriteBatch.Draw(texture, new Rectangle(x * 16, y * 16, 16, 16), Color.White); } } } } } 

Hope someone can figure this out.....

Tx,

##### Share on other sites
O...M...G....

After getting stuck for an hour or 2, getting demotivated as the code is really simple and short and i could just not work it out i fixed it.

The damn backbufferwidth has a minimum width and everything gets stretched tot hat if the backbuffer width is set to less then it's minimum width. Now obviously a 48*48 windows is pretty small but they could have made something to let me know where i went wrong... like giving the backbuffer a value of 0 will give an error. I always have stuff like this happening in my life .

Sick, sick, sick... but i'm finally good to go again.

• 36
• 15
• 9
• 23
• 10