Esempio n. 1
0
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main(string[] args)
        {
            GameEngine game = null;

            //*
            // Normal operations
            try
            {
                game = new GameEngine();
                game.Run();     // NORMAL OPERATION: Iridel's exception reporting active
                // Comment out this line to bypass Iridel's exception reporting
                // (When doing internal Iridel development)
            }
            catch (Exception e)
            {
                Report.Error(e.Message, e);
            }
            finally
            {
                if (game != null) game.Dispose();
            }
            //*/

            // WARNING: READ COMMENTS!!!
            /*
            game = new GameEngine();
            game.Run();       // ABNORMAL OPERATION: Iridel's Exception reporting bypassed
            // The above two lines should ALWAYS be commented out UNLESS
            // you are doing internal Iridel developemnt
            //*/
        }
Esempio n. 2
0
        public GameEngine()
        {
            graphics = new GraphicsDeviceManager(this);

            content = new ContentManager(Services);
            content.RootDirectory = "Content";

            Instance = this;
        }
        public ResourceManager()
        {
            game = GameEngine.Instance;

            mTextures = new Dictionary<string, Texture2D>();
            mModels = new Dictionary<string, Model>();
            mSpriteFonts = new Dictionary<string, SpriteFont>();
            mFontSprites = new Dictionary<string, SpriteWriter>();
            mNumberFonts = new Dictionary<string, NumberIconWriter>();
            mPaths = new Dictionary<string, IridelPath>();
            mTerrains = new Dictionary<string, TerrainRAW>();
            mVideos = new Dictionary<string, Video>();
        }
Esempio n. 4
0
        public Visualize()
            : base()
        {
            thegame = theGame;

            // Init the shader.
            //BEshader = new BasicEffect(theGame.GraphicsDevice, null);
            BEshader = new BasicEffect(theGame.GraphicsDevice);
            BEshader.VertexColorEnabled = true;

            // Sets up the basic data for the UnitSphere (too painful to do manually)
            UnitCircleDataInit();
            UnitSphereDataInit();

            ClearAll();
        }
        /// <summary>
        /// Loads game objects to use as static scenery.
        /// </summary>
        /// <param name="g">GameEngine</param>
        /// <param name="s">Scene</param>
        /// <param name="sceneryConfigFile">The text file from which we read the scenery info.</param>
        public static void LoadScenery(GameEngine g, Scene s, string sceneConfigFile)
        {
            string sceneryConfigFile = g.GameContent.RootDirectory + "/" + sceneConfigFile;

            Stream sceneInfoFile;
            try
            {
                sceneInfoFile = new FileStream(sceneryConfigFile, FileMode.Open, FileAccess.Read);
            }
            catch (Exception e)
            {
                throw new Exception("Problems opening file '" + sceneConfigFile + "'", e);
            }

            StreamReader sceneInfo = new StreamReader(sceneInfoFile);

            string line;
            int linenum = 0;
            string[] parameterList;
            float[] valueList = { 0, 0, 0, 0, 0, 0, 1, 1, 1 };

            // Find the namespace of the user's objects
            Assembly asm = Assembly.GetCallingAssembly();
            Type[] allTypes = asm.GetTypes();
            string objectNameSpace = "";
            string objectName = "";

            foreach (Type t in allTypes)
            {
                if (t.ToString().EndsWith("Program"))
                {
                    objectNameSpace = t.ToString().Substring(0, t.ToString().IndexOf('.'));
                }
            }

            try
            {
                // Read in all of the lines from the text file.
                do
                {
                    linenum++;
                    line = sceneInfo.ReadLine().Trim();

                    if (!line.StartsWith("#") && line.Length != 0)
                    {
                        parameterList = line.Split(',');
                        int paramCount = parameterList.Length;

                        // Validate the nubmer of params we got
                        if (paramCount != 4 && paramCount != 7 && paramCount != 10)
                            throw new Exception(
                                   "Incorrect format on line " + linenum + " in " + sceneConfigFile + "\n\n"
                                   + "\tline format must be on of the following:\n"
                                   + "\t\tname, posx, posy, posz, rotx, roty, rotz, scx, scy, scz\n"
                                   + "\t\tname, posx, posy, posz, rotx, roty, rotz\n"
                                   + "\t\tname, posx, posy, posz");

                        // validate the paramaters we have (using default values for those missing
                        for (int i = 1; i < paramCount; i++)
                        {
                            try
                            {
                                valueList[i - 1] = float.Parse(parameterList[i]);
                            }
                            catch (Exception e)
                            {
                                throw new Exception(
                                    "Problem reading parameter '" + parameterList[i] + "' (#" + (i + 1) + ") "
                                    + " on line " + linenum + " in " + sceneConfigFile, e
                                    );
                            }
                        }

                        // Build the object name: namespace.objectName
                        objectName = objectNameSpace + "." + parameterList[0].Trim();

                        try
                        {
                            GameObject.InstantiateDerivedGameObject(objectName,
                                            Matrix.CreateScale(valueList[6], valueList[7], valueList[8])							//  }   Scale
                                            * Matrix.CreateRotationX(valueList[3])		//  \
                                            * Matrix.CreateRotationY(valueList[4])		//  |   Rotation
                                            * Matrix.CreateRotationZ(valueList[5])		//  /
                                            * Matrix.CreateTranslation(
                                                    new Vector3(valueList[0], valueList[1], valueList[2])));	//  }   Translation
                        }
                        catch (Exception e)
                        {
                            throw new Exception(
                                 "'" + parameterList[0].Trim() + "' is not a known object.\n"
                                + "\tline " + linenum + "\t" + sceneConfigFile + "\n"
                               , e);
                        }
                    }
                } while (!sceneInfo.EndOfStream);
            }
            catch (IOException e)
            {
                throw new IOException("Error reading file '" + sceneryConfigFile + "'", e);
            }
            finally
            {
                // Close the files we were using
                sceneInfoFile.Close();
                sceneInfo.Close();
            }
        }
        /// <summary>
        /// Allows the user to print numbers (or icons that represent numbers) on the screen.
        /// Bryan Patzke, 2008
        /// </summary>
        /// <remarks>
        /// Prints numbers, and numerically indexed icons.
        /// Handles positive and negative longs and doubles.
        /// Graphic file must have no more than 10 icons per row.  Less is ok.
        /// </remarks>
        /// <param name="g">GameEngine reference</param>
        /// <param name="fileName">The path to the sprite file.</param>
        /// <returns></returns>
        public NumberIconWriter(ref GameEngine g, string fileName)
        {
            mIconFile = fileName;
            mSprites = new SpriteBatch(g.Graphics.GraphicsDevice);

            FileStream iconInfoFile;	// These are for reading
            StreamReader inputStream;	// the text file.
            ArrayList lines = new ArrayList();

            mTex = g.GameContent.Load<Texture2D>(mIconFile);

            // Open the font info file
            // Content project name can't be hard coded!!!
            // This needs to be fixed!!!
            string iconInfoFileName = g.GameContent.RootDirectory + "/" + fileName + ".txt";
            if (File.Exists(iconInfoFileName))
            {
                iconInfoFile = new FileStream(iconInfoFileName, FileMode.Open, FileAccess.Read);

                inputStream = new StreamReader(iconInfoFile);

                string nextLine = null;
                try
                {
                    // Skip blank and comment lines
                    do
                    {
                        nextLine = inputStream.ReadLine().Trim();
                    } while (nextLine.StartsWith("#") || nextLine.Length == 0);

                    mSrcHeight = Convert.ToInt32(nextLine);

                    // Skip blank and comment lines
                    do
                    {
                        nextLine = inputStream.ReadLine().Trim();
                    } while (nextLine.StartsWith("#") || nextLine.Length == 0);

                    // Read in the remaining lines from the text file.
                    do
                    {
                        lines.Add(nextLine);
                        nextLine = inputStream.ReadLine().Trim();
                    } while (!inputStream.EndOfStream);
                }
                catch (IOException)
                {
                    throw new IOException("Error reading file " + iconInfoFileName);
                }
                catch (FormatException)
                {
                    throw new FormatException("Error reading value: " + nextLine + "\nin file: " + iconInfoFileName);
                }
                catch (OverflowException)
                {
                    throw new OverflowException("Error reading value: " + nextLine + "\nin file: " + iconInfoFileName);
                }
                finally
                {
                    iconInfoFile.Close();
                }

                mRows = lines.Count;	// Store the number of rows in the file.

                // Set up a 2-dimensional array of rectangles.
                // We'll populate this with the information from the icon info file.
                mSrcCharRectangle = new Rectangle[mRows, 10];

                // When we read in the sizes, they're strings.
                // Here is where we iterate through the list and convert them to integers.
                int index, offset, iconWidth;
                string[] fields;
                for (int row = 0; row < mRows; row++)
                {
                    index = 0;
                    offset = 0;
                    fields = lines[row].ToString().Split(',');
                    foreach (string field in fields)
                    {
                        iconWidth = Convert.ToInt32(field);

                        try
                        {
                            mSrcCharRectangle[row, index++] = new Rectangle(offset,
                                row * mSrcHeight, iconWidth, mSrcHeight);

                            offset += iconWidth;
                        }
                        catch (IndexOutOfRangeException)
                        {
                            // If there are too many numbers on a given line,
                            // handle it gracefully.
                            break;
                        }
                    }
                }
            }
            else
            {
                throw new IOException("Icon info file " + iconInfoFileName + " not found.");
            }
        }
        /// <summary>
        /// Allows the user to print text on the screen.
        /// Bryan Patzke, 2008
        /// </summary>
        /// <remarks>
        /// Covers the full printable ASCII character set
        /// from 33 through 126
        /// </remarks>
        /// <param name="g">GameEngine</param>
        /// <param name="fileName">The path to the sprite file.</param>
        /// <returns></returns>
        public SpriteWriter(ref GameEngine g, string fileName)
        {
            mIconFile = fileName;
            mSprites = new SpriteBatch(g.Graphics.GraphicsDevice);

            mTex = g.GameContent.Load<Texture2D>(mIconFile);

            // Open the font info file
            string iconInfoFileName = g.GameContent.RootDirectory + "/" + fileName + ".txt";
            if (File.Exists(iconInfoFileName))
            {
                FileStream iconInfoFile = new FileStream(iconInfoFileName, FileMode.Open, FileAccess.Read);
                StreamReader inputStream = new StreamReader(iconInfoFile);

                // Set up an array of rectangles.
                // We'll populate this with the information from the icon info file.
                mSrcCharRectangle = new Rectangle[mNumGlyphs];

                string line = null;
                string nextLine = null;
                try
                {
                    // Read in all of the lines from the text file.
                    // This should probably be revised at some point.
                    do
                    {
                        line = inputStream.ReadLine().Trim();

                        if (!line.StartsWith("#") && line.Length != 0)
                        {
                            // Handle fixed-width fonts
                            if (line.StartsWith("fixed"))
                            {
                                // Skip blank and comment lines
                                do
                                {
                                    nextLine = inputStream.ReadLine().Trim();
                                } while (nextLine.StartsWith("#") || nextLine.Length == 0);

                                // Set the font height
                                mSrcHeight = Convert.ToInt32(nextLine);

                                // Skip blank and comment lines
                                do
                                {
                                    nextLine = inputStream.ReadLine().Trim();
                                } while (nextLine.StartsWith("#") || nextLine.Length == 0);

                                // Set the font width
                                int fixedWidth = Convert.ToInt32(nextLine);
                                // Use font width for space width as well
                                mSrcWidthSpace = fixedWidth;

                                int rowShift = 20;
                                int xOffset = 0;
                                int yOffset = 0;
                                for (int i = 0; i < mNumGlyphs; i++)
                                {
                                    mSrcCharRectangle[i] = new Rectangle(xOffset, yOffset, fixedWidth, mSrcHeight);
                                    xOffset += fixedWidth;

                                    if (((i + 1) % rowShift) == 0)
                                    {
                                        xOffset = 0;
                                        yOffset += mSrcHeight;
                                    }
                                }
                            }

                            // Handle proportional fonts
                            else if (line.StartsWith("prop"))
                            {
                                // Skip blank and comment lines
                                do
                                {
                                    nextLine = inputStream.ReadLine().Trim();
                                } while (nextLine.StartsWith("#") || nextLine.Length == 0);

                                // Read in the font height.
                                mSrcHeight = Convert.ToInt32(nextLine);

                                // Skip blank and comment lines
                                do
                                {
                                    nextLine = inputStream.ReadLine().Trim();
                                } while (nextLine.StartsWith("#") || nextLine.Length == 0);

                                // Read in the 5 lines of widths
                                int iconWidth;
                                int xOffset = 0;
                                int yOffset = 0;
                                int index = 0;
                                for (int i = 0; i < 5; i++)
                                {
                                    string[] fields = nextLine.Split(',');
                                    foreach (string field in fields)
                                    {
                                        iconWidth = Convert.ToInt32(field);

                                        mSrcCharRectangle[index++] = new Rectangle(xOffset,
                                            yOffset, iconWidth, mSrcHeight);

                                        // Use the width of the lower-case 'n'
                                        // as the width of the space character.
                                        if (index == 78)
                                        {
                                            mSrcWidthSpace = iconWidth;
                                        }

                                        xOffset += iconWidth;
                                    }
                                    xOffset = 0;
                                    yOffset += mSrcHeight;
                                    if (!inputStream.EndOfStream)
                                    {
                                        nextLine = inputStream.ReadLine().Trim();
                                    }
                                }
                            }
                        }
                    } while (!inputStream.EndOfStream);
                }
                catch (IOException)
                {
                    throw new IOException("Error reading file " + iconInfoFileName);
                }
                catch (FormatException)
                {
                    throw new FormatException("Error reading value: " + nextLine + "\nin file: " + iconInfoFileName);
                }
                catch (OverflowException)
                {
                    throw new OverflowException("Error reading value: " + nextLine + "\nin file: " + iconInfoFileName);
                }

                iconInfoFile.Close();
                inputStream.Close();
            }
            else
            {
                throw new IOException("Icon info file " + iconInfoFileName + " not found.");
            }
        }
Esempio n. 8
0
            public HeightGridDataGeneral(GameEngine g, string heightFld, ColorType chan)
                : base(g, heightFld, chan)
            {
                myterrain = g.resources.GetTexture2D(heightFld);

                if (myterrain.Width != myterrain.Height)
                    throw new Exception("Terrain file resource '" + heightFld + "' is not a square image: its dimensions are " + myterrain.Width + " by " + myterrain.Height + ".");

                heightColors = new Color[myterrain.Width * myterrain.Height];
                Data = new float[heightColors.Length];

                try
                {
                    // Get all the color values out of a texture.
                    myterrain.GetData<Color>(heightColors);
                }
                catch (ArgumentNullException)
                {
                    throw new Exception("The color array contains insufficient room to hold all the colors.");
                }

                size = myterrain.Height;

                // Process data based on the color channel
                Color temp;
                float data;
                int index;
                for (int i = 0; i < size; i++)
                    for (int j = 0; j < size; j++)
                    {
                        index = i * size + (size - j - 1);
                        temp = heightColors[index];
                        switch (chan)
                        {
                            case ColorType.Red:
                                data = (float)temp.R;
                                break;
                            case ColorType.Blue:
                                data = (float)temp.B;
                                break;
                            case ColorType.Green:
                                data = (float)temp.G;
                                break;
                            default:
                                data = (float)((temp.B + temp.G + temp.R) / 3.0);
                                break;
                        }

                        Data[index] = data;
                    }
            }
Esempio n. 9
0
 public HeightGridData(GameEngine g, string heightFld, ColorType chan)
 {
 }
Esempio n. 10
0
 public HeighGridDataRaw(GameEngine g, string heightFld, ColorType chan)
     : base(g, heightFld, chan)
 {
     myterrain = g.resources.GetTerrain(heightFld);
     size = (int)Math.Sqrt(myterrain.miSize);
     // We ignore 'chan'for now as we can't select a channel for RAW files using the current TerrImproter.
     // This might change in the future.
 }
Esempio n. 11
0
        /// <summary>
        /// Internal parameter setting... 
        /// </summary>
        /// <param name="g"></param>
        /// <param name="s"></param>
        /// <param name="TerrainFileName"></param>
        /// <param name="heightFld"></param>
        /// <param name="heightFactor"></param>
        /// <param name="SideLength"></param>
        /// <param name="RepeatsU"></param>
        /// <param name="RepeatsV"></param>
        /// <param name="SubDivX"></param>
        /// <param name="SubDivY"></param>
        void SetUpParams(GameEngine g, Scene s, string TerrainFileName, string heightFld, ColorType chan, float heightFactor, float SideLength, float RepeatsU, float RepeatsV, int SubDivX, int SubDivY)
        {
            game = g;
            scene = s;
            mTexGrass = game.resources.GetTexture2D(TerrainFileName);

            HeightField = heightFld;

            if (HeightField != null)
            {
                // Used for constructor, where a texture heightmap is given.
                if (game.resources.IsTextureKey(heightFld))
                {
                    myTerrainData = new HeightGridDataGeneral(g, heightFld, chan);
                    SubDivX = myTerrainData.Size();
                    SubDivY = SubDivX;
                }
                // Used for constructor 2, where a RAW heightmap is given.
                else
                {
                    myTerrainData = new HeighGridDataRaw(g, heightFld, chan);
                    SubDivX = myTerrainData.Size();
                    SubDivY = SubDivX;
                }
            }

            HeightFactor = heightFactor;

            BOUNDARY = SideLength;
            NUM_COLS = SubDivX;
            NUM_ROWS = SubDivY;
            BaseHeight = 0;
            URepeats = RepeatsU;
            VRepeats = RepeatsV;

            mCellHeight = 2.0f * BOUNDARY / (NUM_ROWS - 1);
            mCellWidth = 2.0f * BOUNDARY / (NUM_COLS - 1);

            Initialize();
        }
Esempio n. 12
0
 /// <summary>
 /// Terrain with Texture heightfield.
 /// </summary>
 /// <param name="g"></param>
 /// <param name="s"></param>
 /// <param name="heightfield"></param>
 /// <param name="Channel"></param>
 /// <param name="heightFactor"></param>
 /// <param name="TerrainFileName"></param>
 /// <param name="SideLength"></param>
 /// <param name="RepeatsU"></param>
 /// <param name="RepeatsV"></param>
 public Terrain(GameEngine g, Scene s, string heightfield, ColorType Channel, float heightFactor, string TerrainFileName, float SideLength, float RepeatsU, float RepeatsV)
 {
     SetUpParams(g, s, TerrainFileName, heightfield, Channel, heightFactor, SideLength, RepeatsU, RepeatsV, 0, 0);
 }
Esempio n. 13
0
 /// <summary>
 /// Flat terrain with texture
 /// </summary>
 /// <param name="g"></param>
 /// <param name="s"></param>
 /// <param name="TerrainFileName"></param>
 /// <param name="SideLength"></param>
 /// <param name="RepeatsU"></param>
 /// <param name="RepeatsV"></param>
 public Terrain(GameEngine g, Scene s, string TerrainFileName, float SideLength, float RepeatsU, float RepeatsV)
 {
     SetUpParams(g, s, TerrainFileName, null, ColorType.Average, 0, SideLength, RepeatsU, RepeatsV, 2, 2);
 }