using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
namespace TileEngine
{
class Renderer
{
GraphicsDevice mydevice;
void doit()
{
int width = 400;
int height = 400;
Texture2D TEX = new Texture2D(mydevice, 400, 400);
RenderTarget2D Mine = new RenderTarget2D(mydevice, width, height);
GraphicsDevice.SetRenderTarget(Mine);
}
}
}
the problems I'm getting are as follows:
Error 2 The name 'Mine' does not exist in the current context C:\Users\Programming\Desktop\Tengine\TileEngine\TileEngine\Game1.cs 149 30 TileEngine
Error 1 An object reference is required for the non-static field, method, or property 'Microsoft.Xna.Framework.Graphics.GraphicsDevice.SetRenderTarget(Microsoft.Xna.Framework.Graphics.RenderTarget2D)' C:\Users\Programming\Desktop\Tengine\TileEngine\TileEngine\Renderer.cs 18 13 TileEngine
The way I understand object oriented programming. This should technically work. Maybe I don't understand XNA at all. I'm still learning so yeah there's that.
The reason I want to have a class that renders outside the main class is because I'm going to be having a few functions to do the heavy lifting for me and clipping will change depending on what frame of reference I'm getting. I want to throw the Texture2D object new instances based on these settings of size and such, each time I do a render (each frame) or possibly each time something changes. There will also be zooming, and contorting of the 2d image, and scaling as well as lighting effects. This is all stuff I want to handle through a SEPARATE class.
If you can't provide me a solution to what I'm doing wrong and how to fix it, please teach me or show me the RIGHT way.
I've now fixed the issue. Hmm..
It seems like I had to include everything from Microsoft.xna.Framework Even though i'm within the namespace TileEngine anyways.. shouldn't it just inheret it all? Also for some reason... at first it didn't like me using 'mydevice' inside those lines at the bottom Then.. the errors began to go away again? Maybe changing the error inside of Game1.cs which btw was a reference of something I removed, fixed the error inside of Renderer? THat doesn't make any sense though... renderer shouldn't say that mydevice doesn't exist in the current context if there's an error concerning an entirely DIFFERENT object reference inside of Game1.cs
Maybe I don't understand how interpreters work but in my honest opinion that should NEVER EVER EVER happen.
Here's the fixed code for anyone with a similiar issue (needs cleanup)
Since I haven't seperated the functionality yet. LOL.
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
namespace TileEngine
{
class Renderer
{
GraphicsDevice mydevice;
SpriteBatch spriteBatch;
void doit()
{
int width = 400;
int height = 400;
Texture2D TEX = new Texture2D(mydevice, 400, 400);
RenderTarget2D Mine = new RenderTarget2D(mydevice, width, height);
mydevice.SetRenderTarget(Mine);
mydevice.SetRenderTarget(null);
Rectangle drawrect = new Rectangle(0, 0, 400, 400);
spriteBatch.Begin();
spriteBatch.Draw(Mine, drawrect, Color.White);
spriteBatch.End();
}
}
}
ANyways maybe my compiler or ide was just being silly. It does have some recompiling glitches with my hd i think it's windows permissions. Mehh.. thanks to everyone who read this anyways.
=================NEW ISSUE same code sources kind of starts here ====================================== Okay now I'm having a new issue along the same lines. Microsoft xna is trying to instantiate these objects at the start of the function life span. I can't seem to create an instance of this class that passes a device object back to the main class. This solution isn't working, I wanted to basically say Hey give me a target rendering object. Oh here you go. catches thanks buddy. use of render in second function
Here are the two functions when they last worked... last worked huge statement.
Only issue is the Game.Run() function crashes or the code in drawing of my main class crashes due to an issue with the graphics device being null??
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
namespace TileEngine
{
class Renderer
{
GraphicsDevice mydevice;
public SpriteBatch spriteBatch;
public RenderTarget2D new_texture(int width, int height)
{
RenderTarget2D Mine = new RenderTarget2D(mydevice, width, height);
Texture2D TEX = new Texture2D(mydevice, width, height); //create the texture to render to
mydevice.SetRenderTarget(Mine); //set the render device to the reference provided
//maybe base.draw can be used with spritebatch. Idk. We'll see if the order of operation
//works out. Wish I could call base.draw here.
return Mine; //I'm hoping that this returns the same instance and not a copy.
}
public void draw_texture(int width, int height, RenderTarget2D Mine)
{
mydevice.SetRenderTarget(null); //Set the renderer to render to the backbuffer again
Rectangle drawrect = new Rectangle(0, 0, width, height); //Set the rendering size to what we want
spriteBatch.Begin(); //This uses spritebatch to draw the texture directly to the screen
spriteBatch.Draw(Mine, drawrect, Color.White); //This uses the color white
spriteBatch.End(); //ends the spritebatch
//Call base.draw after this since it doesn't seem to recognize inside the function
//maybe base.draw can be used with spritebatch. Idk. We'll see if the order of operation
//works out. Wish I could call base.draw here.
}
}
}
That is the code when it was working. This is the code when the class was working with this 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;
namespace TileEngine
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
public GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Renderer tileclipping = new Renderer();
TileMap myMap = new TileMap();
int squaresAcross = 12;
int squaresDown = 12;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
Tile.TileSetTexture = Content.Load<Texture2D>(@"Textures\TileSets\part1_tileset");
// TODO: use this.Content to load your game content here
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
KeyboardState ks = Keyboard.GetState();
if (ks.IsKeyDown(Keys.Left))
{
Camera.Location.X = MathHelper.Clamp(Camera.Location.X - 8, 0, (myMap.MapWidth - squaresAcross) * 32);
}
if (ks.IsKeyDown(Keys.Right))
{
Camera.Location.X = MathHelper.Clamp(Camera.Location.X + 8, 0, (myMap.MapWidth - squaresAcross) * 32);
}
if (ks.IsKeyDown(Keys.Up))
{
Camera.Location.Y = MathHelper.Clamp(Camera.Location.Y - 8, 0, (myMap.MapHeight - squaresDown) * 32);
}
if (ks.IsKeyDown(Keys.Down))
{
Camera.Location.Y = MathHelper.Clamp(Camera.Location.Y + 8, 0, (myMap.MapHeight - squaresDown) * 32);
}
// TODO: Add your update logic here
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
//use the instance of renderer called tileclipping to generate a new texture of a specified size for tiles
//this surface is 200 pixels by 200 pixels for the reason that it's the same as the clipping i'll choose
Texture2D mytexture = new Texture2D(GraphicsDevice, 200, 200);
RenderTarget2D Mine = new RenderTarget2D(graphics.GraphicsDevice, 200, 200);
Mine = tileclipping.new_texture(200, 200);
spriteBatch.Begin();
Vector2 firstSquare = new Vector2(Camera.Location.X / 32, Camera.Location.Y / 32);
int firstX = (int)firstSquare.X;
int firstY = (int)firstSquare.Y;
Vector2 squareOffset = new Vector2(Camera.Location.X % 32, Camera.Location.Y % 32);
int offsetX = (int)squareOffset.X;
int offsetY = (int)squareOffset.Y;
for (int y = 0; y < squaresDown; y++)
{
for (int x = 0; x < squaresAcross; x++)
{
spriteBatch.Draw(
Tile.TileSetTexture,
new Rectangle((x * 32) - offsetX, (y * 32) - offsetY, 32, 32),
Tile.GetSourceRectangle(myMap.Rows[y + firstY].Columns[x + firstX].TileID),
Color.White);
}
}
spriteBatch.End();
// TODO: Add your drawing code here
//There are two instances of mine
//A new one is made each time tileclipping.new_texture is called
//This function can re use the copy created by new texture
//hopefully this saves on memory
tileclipping.draw_texture(200, 200, Mine);
base.Draw(gameTime);
}
public CubeMapFace Tex2d { get; set; }
}
}
Please note: I haven't included those other classes since their functionality isn't causing any issues. if this question is too confusing I can give a link to the sources that are causing issues via pastebin. But basically this thing is saying
Here are the two errors with this issue I'm getting:
Warning 2 Field 'TileEngine.Renderer.device' is never assigned to, and will always have its default value null C:\Users\Programming\Desktop\Tengine\TileEngine\TileEngine\Renderer.cs 11 24 TileEngine
Warning 1 Field 'TileEngine.Renderer.spriteBatch' is never assigned to, and will always have its default value null C:\Users\Programming\Desktop\Tengine\TileEngine\TileEngine\Renderer.cs 10 28 TileEngine
And the crash message is: The GraphicsDevice must not be null when creating new resources. Parameter name: graphicsDevice
I'm going to leave my work in this state since it was the last state that properly compiled. Waiting for help now thanks. :3