Jump to content

  • Log In with Google      Sign In   
  • Create Account

#Actualsatsujin

Posted 14 November 2013 - 01:01 PM

Okay I'm back already. I was trying to implement a ScreenManager class so I tried to load a texture from another class but I get a ContentLoad exeception: GraphicsDevice component not found. I am trying to load the texture in the LoadContent of the class. It wont work there but it does work if I put the Content.Load in the Update method of the class. However, thats probably not a good place to load the texture. I also realize it's not good practice to have all these members as public but that was just a temporary fix until I created accessors for them. Here is the code, hope you can help. 

 

MainMenu.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace RoomGame
{
    class MainMenu : Screen
    {
        GameController gameController;
        Texture2D texture;
        Vector2 position;

        public MainMenu(GameController gc)
        {
            this.gameController = gc;
            this.isUpdating = true;
            this.isDrawn = true;
            //this.graphics = this.gameController.game.graphics;
        }

        public override void LoadContent()
        {
            texture = this.gameController.game.Content.Load<Texture2D>("Textures/Start");
            
        }

        public override void Update(GameTime gameTime)
        {
            //texture = this.gameController.content.Load<Texture2D>("Textures/Start");
            position = new Vector2(100, 100);
        }

        public override void Draw(GameTime gameTime,SpriteBatch sb)
        {
            sb.Draw(texture, position, Color.White);
        }
    }
}

Screen.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace RoomGame
{
    public class Screen
    {
        public ScreenController screenController;
        public bool isUpdating=true;
        public bool isDrawn=true;
        public GraphicsDeviceManager graphics;

        public virtual void Initialize()
        { }

        public virtual void LoadContent()
        { }

        public virtual void Update(GameTime gameTime)
        { }

        public virtual void Draw(GameTime gameTime,SpriteBatch sb)
        { }
    }
}

ScreenController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace RoomGame
{
    
    public class ScreenController
    {
        List<Screen> screens = new List<Screen>();
        GameController gameController;
        SpriteBatch spriteBatch;

        public ScreenController(GameController gc)
        {
            this.gameController = gc;
        }

        public void Initialize()
        {
            
            spriteBatch = new SpriteBatch(this.gameController.GraphicsDevice);
        }

        public void LoadContent()
        {


        }

        public void Update(GameTime gameTime)
        {
            foreach (Screen screen in screens)
            {
                if (screen.isUpdating)
                    screen.Update(gameTime);
            }
        }

        public void Draw(GameTime gameTime)
        {
            foreach (Screen screen in screens)
            {
                if (screen.isDrawn)
                {
                    spriteBatch.Begin();
                    screen.Draw(gameTime,spriteBatch);
                    spriteBatch.End();
                }
            }
        }

        public void AddScreen(Screen screen)
        {
            if (!screens.Contains(screen))
            {
                screens.Add(screen);
                screen.LoadContent();
            }
        }


        

    }
}

GameController.cs-> I realize MainMenu should be referenced in a variable so that it can be called on a ScreenCollections.Remove function which is not yet implemented

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;

namespace RoomGame
{
    public class GameController : DrawableGameComponent
    {
        public Game game;
        public ContentManager content;
        public ScreenController screenController;
        public GameController(Game g,ContentManager c)
            : base(g)
        {
            game = g;
            content = c;
            screenController = new ScreenController(this);
            screenController.AddScreen(new MainMenu(this));
        }

        public override void Initialize()
        {
            base.Initialize();
            screenController.Initialize();
        }

        protected override void LoadContent()
        {
            screenController.LoadContent();
            base.LoadContent();
        }

        protected override void UnloadContent()
        {
            base.UnloadContent();
        }

        public override void Update(GameTime gameTime)
        {
            screenController.Update(gameTime);
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
            screenController.Draw(gameTime);
        }
    }
}

Game1.cs

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;

namespace RoomGame
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        public GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        GameController gameController;

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

            gameController = new GameController(this,this.Content);
            this.Components.Add(gameController);
        }

    }
}

#1satsujin

Posted 14 November 2013 - 12:37 PM

Okay I'm back already. I was trying to implement a ScreenManager class so I tried to load a texture from another class but I get a ContentLoad exeception: GraphicsDevice component not found. I am trying to load the texture in the LoadContent of the class. It wont work there but it does work if I put the Content.Load in the Update method of the class. However, thats probably not a good place to load the texture. I also realize it's not good practice to have all these members as public but that was just a temporary fix until I created accessors for them. Here is the code, hope you can help. 

 

MainMenu.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace RoomGame
{
    class MainMenu : Screen
    {
        GameController gameController;
        Texture2D texture;
        Vector2 position;

        public MainMenu(GameController gc)
        {
            this.gameController = gc;
            this.isUpdating = true;
            this.isDrawn = true;
            //this.graphics = this.gameController.game.graphics;
        }

        public override void LoadContent()
        {
            texture = this.gameController.game.Content.Load<Texture2D>("Textures/Start");
            
        }

        public override void Update(GameTime gameTime)
        {
            //texture = this.gameController.content.Load<Texture2D>("Textures/Start");
            position = new Vector2(100, 100);
        }

        public override void Draw(GameTime gameTime,SpriteBatch sb)
        {
            sb.Draw(texture, position, Color.White);
        }
    }
}

Screen.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace RoomGame
{
    public class Screen
    {
        public ScreenController screenController;
        public bool isUpdating=true;
        public bool isDrawn=true;
        public GraphicsDeviceManager graphics;

        public virtual void Initialize()
        { }

        public virtual void LoadContent()
        { }

        public virtual void Update(GameTime gameTime)
        { }

        public virtual void Draw(GameTime gameTime,SpriteBatch sb)
        { }
    }
}

ScreenController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace RoomGame
{
    
    public class ScreenController
    {
        List<Screen> screens = new List<Screen>();
        GameController gameController;
        SpriteBatch spriteBatch;

        public ScreenController(GameController gc)
        {
            this.gameController = gc;
        }

        public void Initialize()
        {
            
            spriteBatch = new SpriteBatch(this.gameController.GraphicsDevice);
        }

        public void LoadContent()
        {


        }

        public void Update(GameTime gameTime)
        {
            foreach (Screen screen in screens)
            {
                if (screen.isUpdating)
                    screen.Update(gameTime);
            }
        }

        public void Draw(GameTime gameTime)
        {
            foreach (Screen screen in screens)
            {
                if (screen.isDrawn)
                {
                    spriteBatch.Begin();
                    screen.Draw(gameTime,spriteBatch);
                    spriteBatch.End();
                }
            }
        }

        public void AddScreen(Screen screen)
        {
            if (!screens.Contains(screen))
            {
                screens.Add(screen);
                screen.LoadContent();
            }
        }


        

    }
}

GameController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;

namespace RoomGame
{
    public class GameController : DrawableGameComponent
    {
        public Game game;
        public ContentManager content;
        public ScreenController screenController;
        public GameController(Game g,ContentManager c)
            : base(g)
        {
            game = g;
            content = c;
            screenController = new ScreenController(this);
            screenController.AddScreen(new MainMenu(this));
        }

        public override void Initialize()
        {
            base.Initialize();
            screenController.Initialize();
        }

        protected override void LoadContent()
        {
            screenController.LoadContent();
            base.LoadContent();
        }

        protected override void UnloadContent()
        {
            base.UnloadContent();
        }

        public override void Update(GameTime gameTime)
        {
            screenController.Update(gameTime);
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
            screenController.Draw(gameTime);
        }
    }
}

Game1.cs

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;

namespace RoomGame
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        public GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        GameController gameController;

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

            gameController = new GameController(this,this.Content);
            this.Components.Add(gameController);
        }

    }
}

PARTNERS