Is this an efficient way to handle this?

Hi,
In my MouseInputHandler I’m checking each mouse movement which tile is getting hovered.
Now I was wondering… Is this a good way to handle this? (I’m talking about the threading)


@Override
    public void mouseMoved(MouseEvent e) {
        mousePosition = e.getPoint();
        Thread t = new Thread() {
            @Override
            public void run() {
                switch (Core.getGameState()) {
                    case INGAME: // Only perform when we're ingame so we know for sure that there are tiles..
                        if (activeTile != null) {
                            /**
                             * Get active tile, and unset if mouse is out of it
                             */
                            if (!activeTile.getEdges().contains(mousePosition)) {
                                activeTile.setBorderColor(activeTile.getDarkPixelBorderColor());
                                activeTile = null;
                            }
                        } else {
                            /**
                             * Loop trough tiles Cecks if mouse is hovered If
                             * hovered, border goes green. If not, border goes
                             * black.
                             */
                            for (int x = 0; x < Core.getTiles().length; x++) {
                                for (int y = 0; y < Core.getTiles()[x].length; y++) {
                                    Tile t = Core.getTiles()[x][y];
                                    if (t.getEdges().contains(mousePosition)) {
                                        t.setBorderColor(t.getLightPixelBorderColor());
                                    } else {
                                        t.setBorderColor(t.getDarkPixelBorderColor());
                                    }
                                }
                            }
                        }
                        break;
                }
            }
        };
        t.start();
    }

Thanks in advance!

No, it is possibly the worst way you could conceivably imagine doing it. Do not use a Thread.

Cas :slight_smile:

Ok, but what am I supposed to do as an alternative then?
If I just run it, without using the thread, my game gets too laggy…

Seperate grabbing the mouseposition, from using it.
Else you do many unnessecary calculations, one for each mouseevent, probably more than 1 per gameframe.

You sould store the current mouseposition in a variable, array or vector

int mouse_x;
int mouse_y;
public void mouseMoved(MouseEvent e) {

store mouseposition into

mouse_x and mouse_y

}

But in the gameloop you check if the mouseposition was changed

gameloop:

if(mouse_x_old!=mouse_x || mouse_y_old!=mouse_y)
{
mouse_x_old=mouse_y;
mouse_y_old=mouse_y;

…do tile search

}

No need to use a thread here,

Also you probably want to optimise how your tile search works. Don’t scroll through every single tile and compare if the mouse is on it, its a highly inefficient brutefore way to do it (hence the lag).

Can’t you convert mouse position mathematically to detect which tile the mouse is over? then you can just go straight to the tile array entry and highlight it. As for blackening the tile, just save the tile location somewhere when mouse is over it and once another tile is selected then blacken it and save the new tile there.

A better way to do it, like kappa suggested, is to use math. If you are using a grid, and saving the tiles in a 2d array, try this:


int mouse_x;
int mouse_y;
Tile mouseTile;
public void mouseMoved(MouseEvent e) {
    //Other stuff
    int xx = e.getX(); //
    int yy = e.getY();
    //Save the mouse x and y to temporary values
    mouse_x = xx-(xx%gridSize)/16; // The modulus is probably unnecessary, I just like it
    mouse_y = yy-(yy%gridSize)/16;

    if(mouseTile != null) {mouseTile.setSelected(false);}
    if(/*Check if in window*/) {
        mouseTile = Core.getTiles()[x][y];
    }
}

There are several things that need to be changed, but these are the basics.