Saturday, June 29, 2013

II. Reading from a txt file. Lord of strings part II



As a programmer, you need to learn this too. There are many possible functions for getting data from a txt file. For example , you can use it to read dialogues and draw it in the game screen, reading game settings etc.

So create a new project and name it whatever you want.
The content we  need is a spritefont and a text file. Create a new spritefont and save it as font.

Copy and paste this to a new textFile, name it anim.txt


16
Year 1982 in Philippines |
" " |
The Country is currently ruled by the Spanish Empire.|
Jose Rizal, a physician, scholar, scientist, and writer founded the La Liga Filipina at Ilaya Street, Tondo Manila in 1982.|
La Liga Filipina is a progressive organization ought to build a new group sought |
to involve the people directly in the reform movement.|
July 6 1982 |
Dr. Jose Rizal was arrested and exiled because the La Liga Filipina became a threat to the Spanish Authorities.|
During the exile of Dr. Jose Rizal, The organization became inactive and soon to fall.|
With the efforts of Domingo Franco and Andres Bonifacio, |
the La Liga Filipina was reorganized and decided to support  La Solidaridad and the reforms it advocated.|
Eventually after some disarray in the leadership of the group, |
the Supreme Council of the League dissolved the society.|
The Liga membership split into two groups: |
the conservatives formed the Cuerpo de Compromisarios which pledged to continue supporting the La Solidaridad while |
the radicals led by Bonifacio devoted themselves to a new and secret society, the Katipunan.|

The first line of the text is the number of sentences. Import it to game content and copy the following properties.






The Using Statements:
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;
using System.IO;

We need to add the System.IO since we are going to use streamreader.
Next : Declaring Variables.
        SpriteFont font; 
        StreamReader sr; //Use a member of System.IO, enables us to read the contents of the file
        string[] line; //Array to store the sentences we read
        int  numberOfLines=0; // The number of sentences


At your load content method, declare and add the following lines and variables.


string fullPath = "the path of the txt file, see image below";
[Copy the full path generated by the game and paste it to the full path. Replace "\" by "/"]

string name ="anim.txt";

sr = new StreamReader(fullPath + name  ); //FIND THE FILE TO READ
numberOfLines = Int32.Parse(sr.ReadLine());// READ THE FIRST LINE TO GET THE No. of sentences

line=  sr.ReadToEnd().Split('|'); //read the txt file till the end, seperate each sentences when a '|' character is read.


And draw it into the screen, at your draw method..
  for (int x = numberOfLines; x > 0; x--) //set up a loop and read the lines until number of lines to read = 0
            {                      
               spriteBatch.DrawString(font, line[x], new Vector2(1, 20 * (x)), Color.White);
           }  
            spriteBatch.End();


RUN THE PROJECT



SOURCE CODE


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;
using System.IO;
namespace sounds
{
  
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        SpriteFont font;
        StreamReader sr;
        string[] line;
        int  numberOfLines=0;


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

       
        protected override void Initialize()
        {
   

            base.Initialize();
        }

        protected override void LoadContent()
        {
            string fullPath = "D:/sounds/soundsContent/";
            string name ="anim.txt";
            spriteBatch = new SpriteBatch(GraphicsDevice);
            sr = new StreamReader(fullPath + name  );
            numberOfLines = Int32.Parse(sr.ReadLine());
           line=  sr.ReadToEnd().Split('|');

        }
        protected override void UnloadContent()
        {
         }

     
        protected override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin();
            for (int x = numberOfLines; x > 0; x--)
            {
                                    
               spriteBatch.DrawString(font, line[x], new Vector2(1, 20 * (x)), Color.White);
              
           }  
        
            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}