Home > Back-end >  How should I return a list that was previously set without setting it again?
How should I return a list that was previously set without setting it again?

Time:01-03

I have two classes, Main and Grid. Grid simply makes a grid of square pixels. In my Main class, I want to get the list that was create in the Grid class. I managed to figure it out, but I'm wondering if there's a way to optimize the code.

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Diagnostics;
using System.Collections.Generic;

namespace teeeest
{
    public class Grid
    {

        Texture2D image;

        Color color;

        int rows;
        int columns;

        float outerThickness;
        float innerThickness;

        Vector2 size;
        Vector2 origin;
        Vector2 dotSize;

        List<Pixel> pixels = new List<Pixel>(0);

        public Grid(Texture2D image, int rows, int columns, float outerThickness, float innerThickness, Vector2 size, Vector2 origin, Vector2 dotSize, Color color)
        {
            this.dotSize = dotSize;
            this.origin = origin;
            this.color = color;
            this.image = image;
            this.rows = rows;
            this.columns = columns;
            this.outerThickness = outerThickness;
            this.innerThickness = innerThickness;
            this.size = size;
        }

     

        public void Update()
        {
            
            float sizeX = size.X / (columns - 1);
            float sizeY = size.Y / (rows - 1);

            for (int i = 0; i < rows; i  )
            {
                for (int g = 0; g < columns; g  )
                {
                    Pixel p = new Pixel(image, 3, new Vector2((g * sizeX)   origin.X, sizeY * i   origin.Y), new Vector2(image.Width / 2, image.Height / 2), color);
                    pixels.Add(p);

                }
            }
        }


        public virtual void Draw(SpriteBatch hspritebatch, List<Grid> grids)
        {
            foreach (Pixel p in pixels)
            {
                hspritebatch.Draw(
                texture: p.getImage(), 
                position: p.getPosition(), 
                sourceRectangle: null, 
                p.getColor(), 
                rotation: 0, 
                origin: new Vector2(image.Width / 2, image.Height), 
                scale: new Vector2(dotSize.X * .02f, dotSize.Y * .02f), 
                SpriteEffects.None, 
                0); 
            }
        }

        public Texture2D getImage()
        {
            return image;
        }

        public Vector2 getPosition()
        {
            return origin;
        }

        public Vector2 getOrigin()
        {
            return new Vector2(image.Width / 2, image.Height);   
        }

        public Color getColor()
        {
            return color;
        }

        public List<Pixel> getList()
        {
            Update(); # This seems unnecessary. Is it?
            return pixels;
        }

        
        



    }
}

The problem lies in the getList() function. In order to return the correct pixel list that was edited in the Update function, my solution there is to call that function right before returning the list. However, this seems costly for no reason. Is there a way around this without calling the Update function, or is this the only way?

I realize there's been posts similar to this, but I just don't understand them. I'm very much a beginner at coding. Here is my Main class.

using System;
using System.Diagnostics;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;




namespace teeeest
{
    public class Game1 : Game
    {
        private GraphicsDeviceManager _graphics;
        
        private SpriteBatch _spriteBatch;
        
        private SpriteFont font;
        
        private Texture2D ball;

        private Texture2D square;
        
        private Color color = Color.White * .1f;
        
        private Vector2 MouseCoords;
        private Vector2 winMiddle;
        private Vector2 ballOrigin;
        
        private bool leftDown;
        private bool eDown;
        
        private int winWidth;
        private int winHeight;

        List<Line> lines = new List<Line>(0);
        List<Grid> grids = new List<Grid>(0);
        List<Pixel> pixels = new List<Pixel>(0);
        List<Pixel> test = new List<Pixel>(0);

        public Game1()
        {
            _graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            IsMouseVisible = true;
            _graphics.PreferredBackBufferWidth = 800;
            _graphics.PreferredBackBufferHeight = 600;
        }

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

        protected override void LoadContent()
        {
            _spriteBatch = new SpriteBatch(GraphicsDevice);
            ball = Content.Load<Texture2D>("ball");
            square = Content.Load<Texture2D>("square");
            font = Content.Load<SpriteFont>("File");
            
            ballOrigin = new Vector2(ball.Width / 2, ball.Height / 2);
            
            
            
            winWidth = _graphics.PreferredBackBufferWidth;
            winHeight = _graphics.PreferredBackBufferHeight;
            winMiddle = new Vector2(winWidth / 2, winHeight / 2);
            
        }

        protected override void Update(GameTime gameTime)
        {
            lines.Clear();
            grids.Clear();
            pixels.Clear();

            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
                Exit();
                
            if (Mouse.GetState().LeftButton == ButtonState.Released)
            {
                
                leftDown = false;
            }
            if (Mouse.GetState().LeftButton == ButtonState.Pressed && !leftDown)
            {
                Pixel g = new Pixel(ball, 5, MouseCoords, new Vector2(ball.Width / 2, ball.Height / 2), Color.Blue);
                pixels.Add(g);
            }
            if (Keyboard.GetState().IsKeyUp(Keys.E))
            {
                eDown = false;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.E) && !eDown)
            {
                
                color *= 1.1f;
                eDown = true;
            }
            
            MouseCoords = new Vector2(Mouse.GetState().X, Mouse.GetState().Y);

            





       

            Grid q = new Grid(ball, 10, 10, 7, 3, new Vector2(500, 500), new Vector2(30, 30), new Vector2(.2f, .2f), Color.White);
            grids.Add(q);


            # Here is where I'm calling the getList() function. 

            System.Console.WriteLine(q.getList()[7].getPosition()); 

            # Here is where I'm calling the getList() function. 
        








            foreach (Line s in lines)
            {
         
                s.Update();   
            }

            foreach (Grid gh in grids)
            {
                gh.Update();
            }


            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);
            _spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
            
            
     

            foreach (Line l in lines)
            {
                l.Draw(_spriteBatch, lines);
            }

            foreach (Grid g in grids)
            {
                g.Draw(_spriteBatch, grids);
            }

            foreach (Pixel p in pixels)
            {
                _spriteBatch.Draw(
                texture: p.getImage(), 
                position: p.getPosition(), 
                sourceRectangle: null, 
                p.getColor(), 
                rotation: 0, 
                origin: p.getOrigin(), 
                scale: new Vector2(.02f, .02f), 
                SpriteEffects.None, 
                0); 
            }
            
        
            
            _spriteBatch.DrawString(font, 
            MouseCoords.ToString(), 
            new Vector2 (winWidth - 100, 10), 
            Color.White, 
            rotation: 0, 
            origin: new Vector2(0, 0), 
            scale: new Vector2(1, 1), 
            SpriteEffects.None, 
            0);
            
            
            
            
            _spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}

CodePudding user response:

You could only call Update() when the List is EMPTY?

public List<Pixel> getList()
{
    if (pixels.Count == 0)
    {
        Update(); // now it only gets called when pixels is EMPTY
    }            
    return pixels;
}

This type of check may need to be done in Update() as well if it can be called directly from other places so you don't end up with more Pixel instances in it than you were expecting.

  •  Tags:  
  • c#
  • Related