C#MonoGame OpenGL读取文件并将纹理加载到框架上

时间:2014-08-15 18:05:05

标签: c# opengl monogame

我在C#上使用OpenGL应用程序使用MonoGame。

我正在进行塔防游戏,我想加载一张如下所示的地图:

++++++++++++++++++++
++++++++++++++++++++
$###++++++++++++++++
+++#+++++++######+++
+++#+++++++#++++#+++
+++#++++####++++##++
+++#++++#++++++++#++
+++#++++#####++++#++
+++#++++++++#++++#++
+++#++++++++#++++#++
+++#++++#####++++#++
+++#++++#++++++++#++
+++#++++#++++++++#++
+++#++++#++++++++#++
+++######++++++++#++
+++++++++++++++++#++
+++++++++++++++++#++
+++++++++#########++
+++++++++#++++++++++
+++++++++&++++++++++

Key:
$  = start
# = path
& = finish
+ = grass area

通过读入文件,我想读入这些键并将一个瓦片放在指定的位置。 我在C#Visual Studio中做了以下工作

public partial class Game : System.Windows.Controls.UserControl
{
    #region Variables
    private Wave _GameWave;
    public Wave GameWave
    {
        get { return _GameWave; }
        set { _GameWave = value; }
    }

    private Map _GameMap;
    public Map GameMap
    {
        get { return _GameMap; }
        set { _GameMap = value; }
    }

    private System.Timers.Timer _GameLoop;
    public System.Timers.Timer GameLoop
    {
        get { return _GameLoop; }
        set { _GameLoop = value; }
    }
    #endregion

    public Game()
    {
        InitializeComponent();
        GameLoop = new System.Timers.Timer(1000);
        GameWave = new Wave();
        CreateMap();
        Image I = new Image();
        I.Source = new BitmapImage(new Uri(@"..\Pictures\RawModels\GreenMonster.png", UriKind.Relative));
        Grid.SetColumn(I, 0);
        Grid.SetRow(I, 2);
        GameGrid.Children.Add(I);
    }

private void CreateMap()
    {
        GameGrid.RowDefinitions.Clear();
        GameGrid.ColumnDefinitions.Clear();
        GameMap = new Map();
        for (int x = 0; x < GameMap.ActualWidth; x++)
        {
            GameGrid.ColumnDefinitions.Add(new ColumnDefinition());
        }
        for (int y = 0; y < GameMap.ActualHeight; y++)
        {
            GameGrid.RowDefinitions.Add(new RowDefinition());
        }
        for (int y = 0; y < GameMap.ActualHeight; y++)
        {
            for (int x = 0; x < GameMap.ActualWidth; x++)
            {

                Image I = new Image();
                if (GameMap.MapArray[x, y].CanPlaceTower)
                {  
                    I.Source = new BitmapImage(new Uri(@"..\Pictures\Squares\Grass.jpg", UriKind.Relative));
                }
                else
                {
                    I.Source = new BitmapImage(new Uri(@"..\Pictures\Squares\Path.jpg", UriKind.Relative));
                }
                I.Stretch = Stretch.Fill;
                Grid.SetColumn(I, x);
                Grid.SetRow(I, y);
                GameGrid.Children.Add(I);
            }
        }
    }

我想在MonoGame中做同样的事情或类似事情,我无法找到方法。

到目前为止,我有这个     base.Draw方法(gameTime);

        //Drawing and rendering out the image
        spriteBatch.Begin();

        for (int y = 0; y < WindowWidth; y+=64)
        {
            for (int x = 0; x < WindowWidth; x+=64)
            {
                spriteBatch.Draw(grass, new Rectangle(x, y, 64, 64), Color.White);
            }
        }
        spriteBatch.End();

1 个答案:

答案 0 :(得分:2)

我认为你基本上想要使用TitleContainer.OpenStream来读取文本文件。

这样的事情:

var path = @"Content\YourData.txt";

using (var stream = TitleContainer.OpenStream(path))
using (var reader = new StreamReader(stream))
{
    while (!reader.EndOfStream)
    {
        var line = reader.ReadLine();

        // do your thing
    }
}