Hi, I am making this little library for fun. It uses a separate script with its own class and it includes a method to make a blank texture and to make a rectangle. I have come across a problem where i don’t know how I can use the graphics device in this new class.
Here is my code for Game1:
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace Test
{
public class Game1 : Game
{
private GraphicsDeviceManager graphics;
private SpriteBatch sb;
MonoHelp m;
public Game1()
{
m = new MonoHelp(graphics, sb); //library is called mono help
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
}
protected override void Initialize()
{
// start
base.Initialize();
}
protected override void LoadContent()
{
sb = new SpriteBatch(GraphicsDevice);
// loads content{}
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
// update{}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
sb.Begin();
m.rect(50, 50, 50, 100, Color.Red); //i use one of the class's methods to make a rectangle
sb.End();
base.Draw(gameTime);
}
}
}
And here is the library code:
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace Test
{
class MonoHelp : Game1 // if i inherit from game1 i crate two game1 classes so i get a "Cannot access a disposed object." error.
{
GraphicsDeviceManager graphics;
SpriteBatch sb;
public MonoHelp(GraphicsDeviceManager _graphics, SpriteBatch _spriteBatch)// constructor
{
graphics = _graphics;
sb = _spriteBatch;
}
public void size(int width, int height)
{
graphics.PreferredBackBufferWidth = width;
graphics.PreferredBackBufferHeight = height;
}
public Texture2D blankTexture() //generates a blank texture
{
Texture2D _blankTexture;
_blankTexture = new Texture2D(GraphicsDevice, 1, 1); //---------------Here is my problem. I get a 'No Graphics Device Service' error.
_blankTexture.SetData(new Color[] { Color.White });
return _blankTexture;
}
public void rect(int x, int y, int width, int height, Color color)
{
sb.Draw(blankTexture(), new Rectangle(x, y, width, height), color);
}
}
}
I am new to monogame so pardon me if this is a simple issue. Sorry for this being long but if anyone can help please do.