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  
  I see all these samples that poll one component, has anyone made a listener  (Read 2257 times)
0 Members and 1 Guest are viewing this topic.
Offline CJM3407

Senior Newbie





« Posted 2008-04-06 16:53:27 »

A listener that polls all components.  Because I have never seen a game that uses only one button, they use multiple buttons all at the same time.
Offline CJM3407

Senior Newbie





« Reply #1 - Posted 2008-04-06 17:02:17 »

Sorry, I was looking at the api and I saw an Event class.  I am not a hundred percent on how to make my own ActionListener.  Can I just create an Action Listener with the Event from JInput.  Would that work?
Offline endolf

JGO Coder


Medals: 7


Current project release date: sometime in 3003


« Reply #2 - Posted 2008-04-06 21:53:07 »

Nope. JInput is a polled API, you always have to poll it. If you want to do what you say, you could write a thread that polls the API and then calls in to your code on a listener interface, but it seems odd. Most games want control over the threading model, so polled APIs seem more acceptable to developers. It also reflects the underlying OS interface for input.

HTH

Endolf

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline CJM3407

Senior Newbie





« Reply #3 - Posted 2008-04-07 15:08:07 »

ok what is the setup for polling all buttons? Do I need to make a component for every button and then each one?>
Offline endolf

JGO Coder


Medals: 7


Current project release date: sometime in 3003


« Reply #4 - Posted 2008-04-07 17:54:47 »

You have 2 options.
1) The old way. Get the array of components from the controller, then poll the device and read each components data.
2) The new way. Poll the device, get the event queue, and in each event it has the component the event relates too.

It's all in the getting started information.

HTH

Endolf

Offline flib

Innocent Bystander





« Reply #5 - Posted 2008-04-11 13:24:30 »

Here's how I do it, using a thread and creating a homemade event from the polled data.
(code is a snippet, it wont compile as is)
   private final class ControllerEventThread extends Object implements Runnable {
      
      private Thread myThread = null;            
      private int mySleepTime;
      
      private long myLastEventTimeStamp;
      private boolean myIDLE;
      
      public ControllerEventThread() {
         this(EVENT_FETCH_WAIT_MS);
      }
      
      public ControllerEventThread(int sleepTimeInMillis) {
         super();         
         mySleepTime = sleepTimeInMillis;      
         myLastEventTimeStamp = 0;
      }
      
        public boolean isRunning() {
            return (null != myThread);
        }
       
      public boolean start() {
         if (null == myThread) {            
             myIDLE = false;
            myThread = new Thread(this);
            myController.setEventQueueSize(EVENT_FETCH_SIZE);
            myLastEventTimeStamp = System.nanoTime();
            myThread.start();
            if (log.isDebugEnabled()) {
               log.debug("ControllerEventThread started");
            }
            return true;
         }
         log.warn("ControllerEventThread already started.");
         return false;
      }
      
      public boolean stop() {
         if (null == myThread) {
            log.warn("ControllerEventThread not started.");
            return false;
         }
         myThread = null;
         myController.setEventQueueSize(0);
         myLastEventTimeStamp = 0;
         log.debug("ControllerEventThread stopped");
         return true;
      }
      
      public void run() {         
         while (myThread != null) {            
            if (myController.poll()) {
                Event event = new Event();
               while (myController.getEventQueue().getNextEvent(event)) {
                   myIDLE = false;
                   myLastEventTimeStamp = System.nanoTime();
                        try {                           
                            GameController.this.fireGameControllerEvent(event);                                                     
                        }
                        catch (Exception e) {
                            log.error(e.getLocalizedMessage(), e);
                        }
               }//end while
                long delay = System.nanoTime() - myLastEventTimeStamp;
                if ((delay > EVENT_IDLE_NS) && (!myIDLE)) {                       
                    myIDLE = true;
                        GameController.this.fireGameControllerIDLE();                       
                    }
               try {
                       if (mySleepTime > 0) {
                           myThread.sleep(mySleepTime);
                       }
                   }
                   catch (InterruptedException e) {                   
                       log.error(e.getLocalizedMessage(), e);
                       stop();
                   }
            }
            else {
               //not polled
               stop();               
               log.warn("Stopped because of loss of polled data. Controller must have been unplugged.");
            }            
         }//while
      }      
   }
   
The drawback to this approach however is that
-- There's a lot of created event objects since each call to  GameController.this.fireGameControllerEvent(event) creates a new one and some controllers don't stop spamming events (the way you implement this method is up to you - in my case I create a new EventObject 90% of the time).
-- The thread.wait() may introduce some lag in some cases and bursts of events. It may be a problem if you need something hyper-responsive but I'm happy with the results so far.


Hope this helps


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 (23 views)
2014-09-12 09:08:26

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

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

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

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

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

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

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

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

mitcheeb (38 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!