Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

11991 Posts in 1587 Topics- by 3508 Members - Latest Member: NevilleKemp

26. May 2012, 06:59:39 pm
Xith3D CommunityGeneral CategorySupport (Moderator: Marvin Fröhlich)HUD + RunMode.RUN_IN_SEPARATE_THREAD
Pages: [1]
Print
Author Topic: HUD + RunMode.RUN_IN_SEPARATE_THREAD  (Read 190 times)
jeffhoye
developers
Enjoying the stay
***
Offline Offline

Posts: 32


View Profile Email
« on: 26. December 2011, 01:14:15 pm »

How do you setup input for the HUD in RunMode.RUN_IN_SEPARATE_THREAD?  You can reproduce the problem by adding the argument in this line at the bottom of XIN Chapter15a:

        rl.begin(RunMode.RUN_IN_SEPARATE_THREAD);

I believe that I must call this on the proper thread, or with different args: 
InputSystem.getInstance().registerNewKeyboardAndMouse( canvas.getPeer() );

Thanks!
-Jeff
Logged
jeffhoye
developers
Enjoying the stay
***
Offline Offline

Posts: 32


View Profile Email
« Reply #1 on: 26. December 2011, 06:01:20 pm »

BTW, OS is Windows XP, JVM Sun 1.6.0_29.

Thanks!
-Jeff
Logged
Marvin Fröhlich
Xith Lord
Administrator
Guru
*****
Offline Offline

Posts: 4381


May the 4th, be with you...


View Profile
« Reply #2 on: 26. December 2011, 06:05:07 pm »

You gave the answer yourself Wink.
Logged
jeffhoye
developers
Enjoying the stay
***
Offline Offline

Posts: 32


View Profile Email
« Reply #3 on: 27. December 2011, 10:03:20 pm »

It appears that you must also create the display on the same thread.
Logged
jeffhoye
developers
Enjoying the stay
***
Offline Offline

Posts: 32


View Profile Email
« Reply #4 on: 27. December 2011, 10:30:23 pm »

I don't understand how to use the RUN_IN_SEPARATE_THREAD and the HUD?  How should I organize the code to call InputSystem.getInstance().registerNewKeyboardAndMouse( canvas.getPeer() ); on the other thread?  Would you normally do it in a ScheduledOperation that is only called once?  From my testing you must also create the Display on the thread.  Do people commonly use the RUN_IN_SEPARATE_THREAD?  Any exmaples?

Thanks!
-Jeff

Logged
Marvin Fröhlich
Xith Lord
Administrator
Guru
*****
Offline Offline

Posts: 4381


May the 4th, be with you...


View Profile
« Reply #5 on: 28. December 2011, 06:07:34 pm »

I am not sure, what the separate thread has to do with the HUD.

I think, it is more common to use SAME_THREAD. In the end it doesn't make much of a difference. You could simply create everything on the same thread, but don't use the application's main thread, but a separate one.
Logged
jeffhoye
developers
Enjoying the stay
***
Offline Offline

Posts: 32


View Profile Email
« Reply #6 on: 29. December 2011, 12:16:29 pm »

Here's a solution that works on XP.  I'll try to update it. Sorry about the logger, I'm using another library for that.  Basically call start()/stop() rather than begin()/end()

-Jeff

Code:
/**
 * Xith3D on another thread for windows (and hopefully everything else).
 *
 * @author Jeff Hoye
 *
 */
public abstract class DisplayThread extends RenderLoop {

 
  public Canvas3DPanel canvas3D;
  public Xith3DEnvironment xithEnv;

  Thread thread;
  Logger logger;
  private Object lock;
  boolean initialized = false;
  boolean running = false;

  /**
   * Runs init() on own thread, but blocks unitl done.
   *
   * @param name
   * @param logger
   */
  public DisplayThread(String name, Logger logger) {
    this.logger = logger;
    logger.log("DisplayThread.ctor()");
    lock = new Object();
   
    thread = new Thread(new Runnable() {

      @Override
      public void run() {
        runMe();
      }
     
    }, name);
    thread.start();

    synchronized(lock) {
      if (initialized) return;
      try {
        lock.wait(); //  block the ctor until done with init() ... can take a while
      } catch (InterruptedException ie) {
        throw new RuntimeException(ie);
      }
    }
  }

  public void runMe() {
    init();
    synchronized(lock) {
      initialized = true;
      lock.notify(); // this lets the ctor continue
    }
   
    while(true) {
      try {
        synchronized(lock) {
          if (!running) {
            lock.wait(); // if not running block
          }
        }
      } catch (InterruptedException ie) {
        throw new RuntimeException(ie);
      }
      logger.log(this+".beginning()");
      begin();
    }
  }

  Runnable task;
 
  public void init() {
    xithEnv = new Xith3DEnvironment();
    setXith3DEnvironment(xithEnv);
    canvas3D = new Canvas3DPanel();
   
    xithEnv.addCanvas( canvas3D.getCanvas() );   
   
    try {
      InputSystem.getInstance().registerNewKeyboardAndMouse( canvas3D.getCanvas().getPeer() );   
    } catch (Exception e) {
      logger.logException("Error in "+toString()+".init()", e);
    }
  }
 
  public String toString() {
    return thread.toString();
  }
 
  public void start() {
    logger.log(this+".start()");
    synchronized(lock) {
      running = true;
      lock.notify();
    }
  }
 
  public void stop() {
    logger.log("stopping");
    synchronized(lock) {
      running = false;
    }
    end();
  }
 
 
}


Then to use it:
Code:
    renderLoop = new DisplayThread("Render",logger) {
      @Override
      protected void prepareNextFrame( long gameTime, long frameTime, TimingMode tm ) {
        super.prepareNextFrame(gameTime, frameTime, tm);       
        render();
      }
    };

    // set up geometry etc

    renderLoop.start();
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic