Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7741759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:06:33+00:00 2026-06-01T09:06:33+00:00

public void Draw(SpriteBatch spriteBatch) { for (int x = 0; x < width; x++)

  • 0
public void Draw(SpriteBatch spriteBatch)
    {
        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {
                Vector2 mouseCoord = GetMouseTilePosition();
                if (mouseCoord.X > 0)
                {
                    spriteBatch.Draw(selection, tileRect = new Rectangle((int)mouseCoord.X * 64, (int)mouseCoord.Y * 64, 64, 64),
                                    Color.White);
                }
                spriteBatch.Draw(tiles[index[x,y]].texture, tileRect = new Rectangle(x * 64, y * 64, 64, 64), 
                    Color.White);          
            }
        }  
    }

    public Vector2 GetMouseTilePosition()
    {
        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {
                if (IsMouseInsideTile(x, y))
                {
                    return new Vector2(x, y);
                }
            }
        }

        return new Vector2(-1, -1);
    }

    public bool IsMouseInsideTile(int x, int y)
    {
        MouseState MS = Mouse.GetState();
        return (MS.X >= x * 64 && MS.X <= (x + 1) * 64 &&
            MS.Y >= y * 64 && MS.Y <= (y + 1) * 64);
    }

It is very cpu intensive. Is there a better way to do this code? Also I have a camera how can I change this to factor that in so I get the actual mouse position

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T09:06:35+00:00Added an answer on June 1, 2026 at 9:06 am

    Jon Skeet is right, you could directly call IsMouseInsideTile() instead of looping through your array several times. (Currently, you’re checking where the mouse is in the whole tile array, for each tile, instead of only checking the current tile you’re in).

    public void Draw(SpriteBatch spriteBatch)
    {
        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {
                if (IsMouseInsideTile(x, y))
                {
                    spriteBatch.Draw(selection, tileRect = new Rectangle(x * 64, y * 64, 64, 64),
                                        Color.White);
                }
                spriteBatch.Draw(tiles[index[x,y]].texture, tileRect = new Rectangle(x * 64, y * 64, 64, 64), 
                        Color.White);          
            }
        }  
    }
    

    I’m sorry, it’s all my fault, I submitted this code quickly earlier without double checking. This new version should improve your performance drastically.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public void Draw(SpriteBatch theSpriteBatch) { Random rand = new Random(); for(int y = 0;
I'm working on a tile/sprite-based game, calling my Draws via public void Draw(SpriteBatch spriteBatch)
I don't understand what I'm doing wrong here: public void Draw(GameTime gameTime) // in
I draw an arc using onDraw(canvas) : @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
public void Save() { XmlSerializer Serializer = new XmlSerializer(typeof(DatabaseInformation)); /* A first chance exception
public void openReport() { try { HashMap params = new HashMap(); params.put(aapor, 19); JasperReport
private Mesh mesh; private Texture texture; private SpriteBatch batch; @Override public void create() {
base class class Drawer { public abstract void Draw<T>(T type); } derived class #1
Shape.h #includeCircle.h #includeSquare.h class Shape { public: virtual void Draw() = 0; static Shape*
class Game { public: void draw_string(const char *text, int x, int y, TTF_Font *font,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.