Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (494)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Is this an efficient way to handle this?  (Read 1519 times)
0 Members and 1 Guest are viewing this topic.
Offline toktzza

Junior Newbie





« Posted 2012-01-28 10:14:44 »

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)

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
@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!
Offline princec

JGO Kernel


Medals: 378
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #1 - Posted 2012-01-28 10:29:00 »

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

Cas Smiley

Offline toktzza

Junior Newbie





« Reply #2 - Posted 2012-01-28 10:30:57 »

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

Cas Smiley
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..
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Damocles
« Reply #3 - Posted 2012-01-28 11:27:18 »

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,

Offline kappa
« League of Dukes »

JGO Kernel


Medals: 77
Projects: 15


★★★★★


« Reply #4 - Posted 2012-01-28 11:32:08 »

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.
Offline Geemili

Senior Member


Medals: 9
Projects: 1
Exp: 2 years


No Games Finished


« Reply #5 - Posted 2012-02-15 02:26:36 »

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:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
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.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Dwinin (19 views)
2014-09-12 09:08:26

Norakomi (54 views)
2014-09-10 13:57:51

TehJavaDev (63 views)
2014-09-10 06:39:09

Tekkerue (31 views)
2014-09-09 02:24:56

mitcheeb (53 views)
2014-09-08 06:06:29

BurntPizza (37 views)
2014-09-07 01:13:42

Longarmx (23 views)
2014-09-07 01:12:14

Longarmx (27 views)
2014-09-07 01:11:22

Longarmx (26 views)
2014-09-07 01:10:19

mitcheeb (34 views)
2014-09-04 23:08:59
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!