Search code examples
dictionarymatrixxna2dtile

how to make a map in xna 4 with matrix from text file


I am trying to make a map by reading a text file line by line (because i cant find how to do that word by word). So I make a map00.txt that looks like "33000000111" (every number is one row, first 2 rows are number of columns and rows so matrix that I load it into looks like 000 000 111 ). Now I am supposed to draw 3 tiles at the bottom (1=draw tile). I do so by drawing tile at its position in matrix * window height(width) / matrix number of rows(columns). PROBLEM: i cant get the right parameters for current window width and height.

Code for loading tiles:

    public int[,] LoadMatrix(string path) 
    {
        StreamReader sr = new StreamReader(path);
        int[,] a = new int[int.Parse(sr.ReadLine().ToString()), 
                           int.Parse(sr.ReadLine().ToString())];

        for(int i = 0; i < a.GetLength(0); i++)
            for (int j = 0; j < a.GetLength(1); j++)
            { a[i, j] =int.Parse(sr.ReadLine().ToString()); }

        sr.Close();
        return a;
    }

Code for drawing tiles:

    public void DrawTiles(SpriteBatch sp, GraphicsDeviceManager gdm)
    {
        for(int i = 0; i < matrix.GetLength(0); i++)
            for(int j = 0; j < matrix.GetLength(1); j++)
            {
                if (i == 1)
                {
                    sp.Draw(tile, 
                            new Rectangle(j * (gdm.PreferredBackBufferWidth / 3),//matrix.GetLength(1),
                                          i * (gdm.PreferredBackBufferWidth / 3),//matrix.GetLength(0),
                                          gdm.PreferredBackBufferWidth / matrix.GetLength(1),
                                          gdm.PreferredBackBufferHeight / matrix.GetLength(0)),
                            Color.White);
                }
            }
    }

but the result is that they are drawn about 40 pixels above the bottom of the screen!

and i tried with GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height(Width) but i get the same result. And when i put calculated numbers that should (in theory) be width/columns and heigth/rows i get what i want. So any suggestions would be VERY appriciated because i am stuck at this for a long time on google and Stack Overflow.


Solution

  • Here is a reworked version of your Draw code, which should work:

    public void DrawTiles(SpriteBatch sp, GraphicsDeviceManager gdm)
    { 
        //You would typically pre-compute these in a load function
        int tileWidth = gdm.PreferredBackBufferWidth / matrix.GetLength(0);
        int tileHeight = gdm.PreferredBackBufferWidth / matrix.GetLength(1);
    
        //Loop through all tiles
        for(int i = 0; i < matrix.GetLength(0); i++)
        {
            for(int j = 0; j < matrix.GetLength(1); j++)
            {
                //If tile value is not 0
                if (matrix[i,j] != 0)
                {
                     sp.Draw(tile, new Rectangle(i * tileWidth, j * tileHeight, tileWidth, tileHeight), Color.White);
                }
            }
      }
    }