Click here to Skip to main content
Click here to Skip to main content

Learning XNA 2D Engine IceCream With 1945 Demo Project

By , 8 Aug 2012
 
1945_Code_Package.zip
1945 Code Package
IceCream
XNA4
IceCream
Attributes
Background.png
Components
Content
BloomCombineEffect.fx
BloomExtractEffect.fx
Content.contentproj.vspscc
DefaultParticleTexture.png
DefaultTileGridTexture.png
diagnosticFont.spritefont
GaussianBlurEffect.fx
IceCreamContent.contentproj
IceCreamContent.contentproj.user
RefractionLayerEffect.fx
Debug
Console
Drawing
IceEffects
IceCream.csproj.user
IceCreamMono.pidb
Input
Properties
QuadTree
Resources
WIN
Arial.xnb
blank.xnb
BloomCombineEffect.xnb
BloomExtractEffect.xnb
DefaultFont.xnb
DefaultParticleTexture.xnb
DefaultTexture.xnb
DefaultTileGridTexture.xnb
diagnosticFont.xnb
GaussianBlurEffect.xnb
RefractionLayerEffect.xnb
XBOX
BloomCombineEffect.xnb
BloomExtractEffect.xnb
DefaultFont.xnb
DefaultParticleTexture.xnb
DefaultTileGridTexture.xnb
diagnosticFont.xnb
GaussianBlurEffect.xnb
RefractionLayerEffect.xnb
SceneItems
AnimationClasses
CompositeEntityClasses
ParticlesClasses
TileGridClasses
TypeConverters
Serialization
Utility
Milkshake
Editors
AnimatedSprites
Components
CompositeEntities
Materials
Particles
PostProcessAnimations
Sprites
TileGrids
TileSheet
Graphics
GraphicsDeviceControls
IceCream_Icon_128px.ico
Lib
XPTable.dll
Milkshake_TemporaryKey.pfx
Properties
app.manifest
Resources
car.png
checker.png
color_swatch.png
cross.png
font.png
pivot.png
plugin.png
ppBackground.jpg
sport_soccer.png
user.png
weather_lightning.png
weather_sun.png
Selectors
Tools
Wizards
MilkshakeLibrary
Properties
Templates
IceComponent.zip
IceCreamWindowsGame.zip
IceCream1945
IceCream1945
IceCream1945
Components
Game.ico
Game.icproj
GameScenes
GameThumbnail.png
Global
Properties
IceCream1945Content
1945.png
Ending.icescene
EnemyBulletsAndIslands.png
global.ice
IceCream1945Content.contentproj
Level1.icescene
Music
Sounds
ExplosionBomb.wav
ExplosionHit.wav
Laser_Shoot_Player.wav
Laser_Shoot_Turret.wav
Laser_Shoot3.wav
Quick_Hit.wav
Splash.icescene
1945_Demo_Package-noexe.zip
1945_Demo_Package
Game.icproj
IceCream1945Content
1945.xnb
Ending.icescene
EnemyBulletsAndIslands.xnb
global.ice
Level1.icescene
Sounds
ExplosionBomb.xnb
ExplosionHit.xnb
Laser_Shoot_Player.xnb
Laser_Shoot_Turret.xnb
Laser_Shoot3.xnb
Quick_Hit.xnb
Splash.icescene
1945_Demo_Package.zip
Game.icproj
IceCream.dll
IceCream1945.exe
1945.xnb
Ending.icescene
EnemyBulletsAndIslands.xnb
global.ice
Level1.icescene
Music
ExplosionBomb.xnb
ExplosionHit.xnb
Laser_Shoot_Player.xnb
Laser_Shoot_Turret.xnb
Laser_Shoot3.xnb
Quick_Hit.xnb
Splash.icescene
#if XNATOUCH
using XnaTouch.Framework;
using XnaTouch.Framework.Audio;
using XnaTouch.Framework.Content;
using XnaTouch.Framework.GamerServices;
using XnaTouch.Framework.Graphics;
using XnaTouch.Framework.Input;
using XnaTouch.Framework.Media;
using XnaTouch.Framework.Net;
using XnaTouch.Framework.Storage;
#else
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;

using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


#endif

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Reflection;
using System.Xml;
using System.Globalization;
using IceCream.Attributes;
using IceCream.Components;
using IceCream.Drawing;
using IceCream.Serialization;
using IceCream.SceneItems;

namespace IceCream
{
    public static class SceneManager
    {
        #region Static Fields

        internal static List<Assembly> _assemblies;
        private static IceScene _activeScene;
        private static List<IceScene> _scenes;
        private static ContentManager _globalcontent;
        private static GlobalDataHolder _globalDataHolder;
        private static List<Material> _embeddedMaterials = new List<Material>();
        private static List<IceFont> _embeddedFonts = new List<IceFont>();

        #endregion

        #region Properties
        /// <summary>
        /// Trial property for networking code | dont remove 
        /// </summary>
        public static bool IsNetworkOwner { get; set; }

        public static GlobalDataHolder GlobalDataHolder
        {
            get { return _globalDataHolder; }
            set { _globalDataHolder = value; }
        }

        public static ContentManager GlobalContent
        {
            get { return _globalcontent; }
        }

        public static List<Material> EmbeddedMaterials
        {
            get { return _embeddedMaterials; }
            set { _embeddedMaterials = value; }
        }

        public static List<IceFont> EmbeddedFonts
        {
            get { return _embeddedFonts; }
            set { _embeddedFonts = value; }
        }

        public static List<IceScene> Scenes
        {
            get { return _scenes; }
        }

        public static IceScene ActiveScene
        {
            get { return _activeScene; }
            set
            {
                if (_activeScene != null)
                {
                    _activeScene.Enabled = false;
                }
                _activeScene = value;
                if (value != null)
                {
                    _activeScene.Enabled = true;
                }
                IceCore.activeSceneChanged = true;
            }
        }

        #endregion

        #region Constructor

        static SceneManager()
        {
            _scenes = new List<IceScene>();
            _assemblies = new List<Assembly>();
            _globalDataHolder = new GlobalDataHolder();

            //LoadAssemblies();
        }

        #endregion

        #region Methods

        private static void LoadAssemblies()
        {
            string _path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().ManifestModule.FullyQualifiedName);
            foreach (string _file in Directory.GetFiles(_path))
            {
                try
                {

                    if (_file.EndsWith(".exe")) //|| _file.EndsWith(".dll"))
                    {
                        TraceLogger.TraceInfo("Reading Assembly " + Path.GetFileName(_file));
                        #if(WINDOWS)
                        Assembly _asm = Assembly.LoadFile(_file);
                        #else
                        Assembly _asm = Assembly.LoadFrom(_file);
                        #endif
                        if (HasComponents(_asm))
                        {
                            _assemblies.Add(_asm);
                        }
                    }
                }
                catch { }
            }
        }

        static string _tempRoot;
        public static void LoadAssemblies(string rootpath)
        {
            string _path = rootpath; // Path.GetDirectoryName(Assembly.GetExecutingAssembly().ManifestModule.FullyQualifiedName);
            _tempRoot = rootpath;
            #if(WINDOWS)
            AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
            #endif
            foreach (string _file in Directory.GetFiles(_path))
            {
                try
                {
                    if (_file.EndsWith(".exe"))// || _file.EndsWith(".dll"))
                    {
                        //                        #if WINDOWS
                        //Assembly _asm = Assembly.LoadFile(_file);
                        #if !WINDOWS
                        Assembly _asm = Assembly.LoadFrom(_file);
                        #else
                        Assembly _asm = null;
                        _asm = Assembly.LoadFile(_file);
                        #endif
                        if (HasComponents(_asm) == true)
                        {
                            _assemblies.Add(_asm);
                        }                        
                    }
                }
                catch (Exception err)
                {
                    TraceLogger.TraceError("Error in SceneManager Load Assemblies\r\n" + err.ToString());
                }
            }
            #if(WINDOWS)
            AppDomain.CurrentDomain.AssemblyResolve -= new ResolveEventHandler(CurrentDomain_AssemblyResolve);
            #endif
        }

        #if(WINDOWS)
        static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
        {
            foreach (var item in Directory.GetFiles(_tempRoot))
            {
                if (item.EndsWith(".exe") || item.EndsWith(".dll"))
                {
                    Assembly tmp = Assembly.LoadFile(item);
                    if (tmp.FullName == args.Name)
                        return tmp;
                }
            }
            return null;
        }
        #endif

        private static bool HasComponents(Assembly asm)
        {
            foreach (Type t in asm.GetTypes())
            {
                var _attribs = t.GetCustomAttributes(true);
                foreach (var item in _attribs)
                {
                    Attribute b = (Attribute)item;
                    if (b.GetType().Equals(typeof(IceComponentAttribute)))
                    {
                        TraceLogger.TraceInfo("Assembly Has Components To Read");
                        return true;
                    }
                }
            }
            return false;
        }

        /// <summary>
        /// Loads the given filename into a scene and returns it. 
        /// </summary>
        /// <param name="filename">The filename of the scene</param>
        /// <returns>The loaded Scene</returns>
        /// <remarks>Automatically loads Materials</remarks>
        public static IceScene LoadScene(string filename)
        {
            return LoadScene(filename, true);
        }

        /// <summary>
        /// Loads the given filename into a scene and returns it. 
        /// </summary>
        /// <param name="filename">The filename of the scene</param>
        /// <param name="loadMaterials">Should load materials</param>
        /// <remarks>If false is passed to load materials they are not loaded from the content manager into the texture object. This is for the editor</remarks>
        public static IceScene LoadScene(string filename, bool loadMaterials)
        {
            if (!filename.EndsWith(".icescene"))
            {
                filename += ".icescene";
            }
            string _filename = Path.GetFullPath(filename);
            if (!File.Exists(_filename))
            {
                throw new FileNotFoundException("File Not Found: " + filename);
            }
            IceScene scene = Serialization.SceneSerializer.DeSerializeScene(filename, GlobalDataHolder.ContentFolderPath);

            scene.WillRenderNotActive = false;
            Scenes.Add(scene);
            if (_activeScene == null)
            {
                _activeScene = scene;
            }

            if (loadMaterials == true)
            {
                InitializeScene(scene);
            }
            scene.Enabled = true;
            scene.WillRenderNotActive = true;

            return scene;
        }

        public static void UnLoadScene(IceScene oldScene)
        {
            oldScene.Unload();
            oldScene.ContentManager.Unload();
            oldScene.ContentManager.Dispose();

            Scenes.Remove(oldScene);
            if (oldScene == ActiveScene)
                ActiveScene = null;
            if (ActiveScene == null && Scenes.Count > 0) //Pick the top scene
                ActiveScene = Scenes[Scenes.Count - 1];

        }

        private static void InitializeScene(IceScene _scene)
        {
            _scene.InitializeContent(Game.Instance.Services);
            _scene.ContentManager.RootDirectory = GlobalDataHolder.ContentFolderPath;

            // Create a default full sized camera            
            Camera camera = new Camera();
            camera.Position = Vector2.Zero;
            camera.Update(1/60f);
            _scene.ActiveCameras.Add(camera);

            // Load Materials
            foreach (Material _material in _scene.Materials)
            {
                _material.Texture = _scene._content.Load<Texture2D>(_material.Filename.Substring(0, _material.Filename.Length - 4));
                _material.Scope = AssetScope.Local;
            }
            // Load Fonts
            foreach (IceFont _font in _scene.Fonts)
            {
                _font.Font = _scene._content.Load<SpriteFont>(_font.Filename.Replace(".spritefont", ""));
            }
			#if !XNATOUCH
            foreach (IceEffect _effect in _scene.Effects)
            {
                if (_effect.Effects != null)
                {
                    continue;
                }
                // removing ".fx" from the name
                string name = _effect.Filename.Substring(0, _effect.Filename.Length - 3);
                _effect.Load(GlobalDataHolder.ContentManager, new string[] { name });
            }
            #endif
            //Load Scene Components
            foreach (IceSceneComponent _comp in _scene.SceneComponents)
            {
                _comp.SetOwner(_scene);
                _comp.OnRegister();
            }
            //Register Scene Items 
            for (int i = 0; i < _scene.SceneItems.Count; i++)
            {
                SceneItem item = _scene.SceneItems[i];
                item.SceneParent = _scene;
                if (item.IsRegistered == false)
                {
                    item.OnRegister();
                }
            }
        }

        public static IceScene AddBlankScene()
        {
            IceScene _newScene = new IceScene();
            Camera _camera = new Camera();
            _newScene.ActiveCameras.Add(_camera);

            Scenes.Add(_newScene);
            _newScene._content = new ContentManager(Game.Instance.Services);
            if (_activeScene == null)
            {
                _activeScene = _newScene;
            }
            return _newScene;
        }

        public static void InitializeGlobal()
        {
            InitializeGlobal(IceCream.Game.Instance.Services);
        }

        public static void LoadGlobalData(string contentPath)
        {
            String filename = Path.Combine(contentPath, "global.ice");
            SceneSerializer.RootPath = contentPath;
            if (File.Exists(filename))
            {
                Serialization.SceneSerializer.DeSerializeScene(filename, "", SceneManager.GlobalDataHolder);
            }
            else
            {
                throw new Exception(filename + " was not found, unable to load global data");
            }
            SceneManager.GlobalDataHolder.ContentFolderPath = contentPath;
        }

        public static void InitializeGlobal(IServiceProvider services)
        {
            InitializeGlobal(services, true);
        }

        public static void InitializeEmbedded(IServiceProvider services, bool loadMaterials)
        {
            #if XBOX360
            _globalcontent = new ResourceContentManager(services,XBOXIceResources.ResourceManager); //Add root directory ?
            #elif !REACH
            _globalcontent = new ResourceContentManager(services, WINIceCreamResources.ResourceManager); //Add root directory ?                          
            String[] bloomShaders = { "BloomExtractEffect", "GaussianBlurEffect", "BloomCombineEffect" };
            DrawingManager.EmbeddedIceEffects[(int)EmbeddedIceEffectType.Bloom]
                .Load(SceneManager.GlobalContent, bloomShaders);
            String[] gaussianShaders = { "GaussianBlurEffect" };
            DrawingManager.EmbeddedIceEffects[(int)EmbeddedIceEffectType.GaussianBlur]
                .Load(SceneManager.GlobalContent, gaussianShaders);
            DrawingManager.RefractionLayerEffect = _globalcontent.Load<Effect>("RefractionLayerEffect");

            Texture2D _pixelTexture = new Texture2D(DrawingManager.GraphicsDevice, 1, 1, false, SurfaceFormat.Color);
            Color[] pixelColor = new Color[1];
            pixelColor[0] = Color.White;
            _pixelTexture.SetData<Color>(pixelColor);
            EmbeddedMaterials.Add(new Material("Pixel", _pixelTexture, AssetScope.Embedded));
            EmbeddedMaterials.Add(new Material("DefaultParticleMaterial",
                _globalcontent.Load<Texture2D>("DefaultParticleTexture"), AssetScope.Embedded));
            Material defaultTileGridMat = new Material("DefaultTileGridMaterial",
                _globalcontent.Load<Texture2D>("DefaultTileGridTexture"), AssetScope.Embedded);            
            // load default areas of the texture            
            defaultTileGridMat.Areas.Add("1", new Rectangle(0, 0, 32, 32));
            defaultTileGridMat.Areas.Add("2", new Rectangle(34, 0, 32, 32));
            defaultTileGridMat.Areas.Add("3", new Rectangle(0, 34, 32, 32));
            defaultTileGridMat.Areas.Add("4", new Rectangle(34, 34, 32, 32));
            EmbeddedMaterials.Add(defaultTileGridMat);

            EmbeddedFonts.Add(new IceFont("DefaultFont",
                _globalcontent.Load<SpriteFont>("DefaultFont"), AssetScope.Embedded));
            EmbeddedFonts.Add(new IceFont("DiagnosticFont",
                _globalcontent.Load<SpriteFont>("DefaultFont"), AssetScope.Embedded));
			#else
			Texture2D _pixelTexture = Texture2D.FromFile(DrawingManager.GraphicsDevice, "Content/pixel.png");
			EmbeddedMaterials.Add(new Material("Pixel", _pixelTexture, AssetScope.Embedded));
			#endif
        }

        static String GetContentFilename(String fileName)
        {
            return Path.GetFileNameWithoutExtension(fileName);
        }

        public static void InitializeGlobal(IServiceProvider services, bool loadMaterials)
        {
            SceneManager.GlobalDataHolder._content = new ContentManager(services);
            SceneManager.GlobalDataHolder.ContentManager.RootDirectory = SceneManager.GlobalDataHolder.ContentFolderPath;
            if (SceneManager.GlobalDataHolder.NativeResolution == Point.Zero)
            {
                SceneManager.GlobalDataHolder.NativeResolution = new Point(1280, 720);
            }
            if (loadMaterials == true)
            {
                foreach (Material _mat in SceneManager.GlobalDataHolder.Materials)
                {
                    _mat.Scope = AssetScope.Global;
                    if (_mat.Texture != null)
                    {
                        continue;
                    }                    
                    _mat.Texture = SceneManager.GlobalDataHolder.ContentManager.Load<Texture2D>(GetContentFilename(_mat.Filename));
                }

                foreach (IceFont _mat in SceneManager.GlobalDataHolder.Fonts)
                {
                    _mat.Scope = AssetScope.Global;
                    if (_mat.Font != null)
                    {
                        continue;
                    }
                    _mat.Font = SceneManager.GlobalDataHolder.ContentManager.Load<SpriteFont>(GetContentFilename(_mat.Filename));
                }
				#if !XNATOUCH
                foreach (IceEffect _effect in SceneManager.GlobalDataHolder.Effects)
                {
                    _effect.Scope = AssetScope.Global;
                    if (_effect.Effects != null)
                    {
                        continue;
                    }
                    String[] names = new String[] { GetContentFilename(_effect.Filename) };
                    _effect.Load(SceneManager.GlobalDataHolder.ContentManager, names);
                }
                #endif
            }
        }

        public static Material GetEmbeddedMaterial(string name)
        {
            foreach (Material _material in EmbeddedMaterials)
            {
                if (_material.Name == name)
                {
                    return _material;
                }
            }
            return null;
        }

        public static IceFont GetEmbeddedFont(string name)
        {
            foreach (IceFont _font in EmbeddedFonts)
            {
                if (_font.Name == name)
                {
                    return _font;
                }
            }
            return null;
        }

        public static Material GetEmbeddedParticleMaterial()
        {
            foreach (Material _material in EmbeddedMaterials)
            {
                if (_material.Name == "DefaultParticleMaterial")
                {
                    return _material;
                }
            }
            return null;
        }

        public static Material GetEmbeddedTileGridMaterial()
        {
            foreach (Material _material in EmbeddedMaterials)
            {
                if (_material.Name == "DefaultTileGridMaterial")
                {
                    return _material;
                }
            }
            return null;
        }

        public static Material GetEmbeddedPixelMaterial()
        {
            foreach (Material _material in EmbeddedMaterials)
            {
                if (_material.Name == "Pixel")
                {
                    return _material;
                }
            }
            return null;
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of use and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Tyler Forsythe
Software Developer (Senior)
United States United States
Member
No Biography provided

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 8 Aug 2012
Article Copyright 2012 by Tyler Forsythe
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid