Mouse for software mode and opengl? [Solved]

Started by efaj, December 23, 2011, 08:00:44 AM

Previous topic - Next topic

efaj

The advanced example uses a MouseMapper, but it needs an opengl window. How would I do a mouse mapper for both software mode and opengl? (My idea involves setting the game like the car example: Being able to change between both renderers in midgame)

While I'm at it, looking around the forum I found: reproject2D3DWS, from reading the javadocs, does it mean it allows you to move with the mouse like World of Warcraft, Runescape and other MMORPGs do?

Thanks

EgonOlsen

I can offer you a MouseMapper-class that does both...however, it's pretty old and tailored for a specific game, so it has some references that can't be resolved, but maybe it helps to get the basics. It has two constructors. The one that takes a Frame is the one that can be used for the software rendererm the other one is for hardware. Here it is:


package naroth.util;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;

import org.lwjgl.input.*;
import com.threed.jpct.*;
import naroth.config.*;
import java.nio.*;

public class MouseMapper extends MouseMotionAdapter implements MouseListener {

   public final static int BUTTON1=0;
   public final static int BUTTON2=1;

   private boolean lwjgl;
   private Frame frame=null;
   private java.awt.Cursor oldCursor=null;
   private org.lwjgl.input.Cursor oldCursorGL=null;
   private Robot robot=null;
   private int mouseX=0;
   private int mouseY=0;
   private boolean button1=false;
   private boolean button2=false;
   private boolean button3=false;
   private boolean hidden=false;


   public MouseMapper() {
      init(null);
   }

   public MouseMapper(Frame comp) {
      try {
         robot=new Robot();
      } catch (Exception e) {
          Logger.log("Error initializing awt.Robot: "+e.getMessage(), Logger.ERROR);
      }
      init(comp);
   }

   public void mouseMoved(MouseEvent e) {
     mouseX=e.getX()-frame.getInsets().left;
     mouseY=e.getY()-frame.getInsets().top;
   }

   public void mouseDragged(MouseEvent e) {
      mouseX=e.getX()-frame.getInsets().left;
      mouseY=e.getY()-frame.getInsets().top;
   }

   public void mouseClicked(MouseEvent e) {}

   public void mousePressed(MouseEvent e) {
      if (e.getButton()==MouseEvent.BUTTON1) {
         button1=true;
      }
      if (e.getButton()==MouseEvent.BUTTON2) {
         button2=true;
      }
      if (e.getButton()==MouseEvent.BUTTON3) {
         button3=true;
      }
   }

   public void mouseReleased(MouseEvent e) {
     if (e.getButton()==MouseEvent.BUTTON1) {
        button1=false;
     }
     if (e.getButton()==MouseEvent.BUTTON2) {
        button2=false;
     }
     if (e.getButton()==MouseEvent.BUTTON3) {
       button3=false;
    }

  }

  public void mouseEntered(MouseEvent e) {}

  public void mouseExited(MouseEvent e) {}

   public void center() {
      if (frame!=null) {
         int x=frame.getX();
         int y=frame.getY();
         int width=frame.getWidth();
         int height=frame.getHeight();
         width=width>>1;
         height=height>>1;
         x+=width;
         y+=height;
         robot.mouseMove(x,y);
      } else {
         // already done by Mouse.setGrabbed().
      }
   }

   public void hide() {
     if (frame!=null) {
        oldCursor=frame.getCursor();
        int[] pixels=new int[16*16];
        Image image=Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(16, 16, pixels, 0, 16));
        java.awt.Cursor transparentCursor=Toolkit.getDefaultToolkit().createCustomCursor(image, new Point(0, 0),"hidden");
        frame.setCursor(transparentCursor);
      } else {
         Mouse.setGrabbed(true);
      }
      hidden=true;
   }

   public void show() {
      if (frame!=null) {
         if (oldCursor!=null) {
            frame.setCursor(oldCursor);
            oldCursor=null;
         }
      } else {
         Mouse.setGrabbed(false);
      }
      hidden=false;
   }

   public boolean isVisible() {
      return !hidden;
   }

   public void destroy() {
      if (frame!=null) {
         frame.setCursor(oldCursor);
         frame.removeMouseMotionListener(this);
         frame.removeMouseListener(this);
      } else {
         if (Mouse.isCreated()) {
            try {
              Mouse.setNativeCursor(oldCursorGL);
           } catch (Exception e) {
              Logger.log("Error restoring cursor!", Logger.WARNING);
           }
           Mouse.destroy();
         }
      }
      show();
   }

   public boolean buttonDown(int button) {
      if (!lwjgl) {
         if (button==BUTTON1) return button1;
         if (button==BUTTON2) return button2|button3;
      }
      return Mouse.isButtonDown(button);
   }

   public int getMouseX() {
      if (!lwjgl) {
         return mouseX;
      } else {
         return Mouse.getX();
      }
   }

   public int getMouseY() {
      if (!lwjgl) {
         return mouseY;
      } else {
         return Configuration.height-Mouse.getY();
      }
   }

   public int getDeltaX() {
      if (!lwjgl) {
         int mouseXCenter=(frame.getWidth()>>1)-frame.getInsets().left; // Correct the value
         return mouseX-mouseXCenter;
      } else {
         if (Mouse.isGrabbed()) {
            return Mouse.getDX();
         } else {
            return 0;
         }
      }
   }

   public int getDeltaY() {
      if (!lwjgl) {
         int mouseYCenter=(frame.getHeight()>>1)-frame.getInsets().top; // Correct the value
         return mouseY-mouseYCenter;
      } else {
         if (Mouse.isGrabbed()) {
            return Mouse.getDY();
         } else {
            return 0;
         }
      }
   }

   private Image loadCursor() {
      // Yikes!! There has to be a cleaner solution than this...but no time now to investigate....:-)
      int bufferSize=100000;
      int anzAkt=0;
      int anz=0;
      int aktsize=bufferSize;
      byte[] puffer=new byte[bufferSize];
      SimpleStream ss=new SimpleStream("textures/crosshair.gif");
      InputStream is=ss.getStream();
      Image image=null;
      try {
         while ((anzAkt!=-1)) {
            anzAkt=is.read(puffer, anz, aktsize-anz);
            anz+=anzAkt;
            if ((anzAkt!=-1)&&(anz>=aktsize)) {
               aktsize+=100000;
               byte npuffer[]=new byte[aktsize];
               System.arraycopy(puffer, 0, npuffer, 0, aktsize-100000);
               puffer=npuffer;
               Logger.log("Expanding buffers..."+aktsize+" bytes", Logger.MESSAGE);
            }
         }
         anz++;
         ss.close();
         image=Toolkit.getDefaultToolkit().createImage(puffer, 0, anz);
      } catch (Exception e) {
         Logger.log("Couldn't get data from InputStream: "+e.toString(), Logger.ERROR);
      }

      MediaTracker myTracker=new MediaTracker(new Canvas());
      myTracker.addImage(image, 0);
      try {
         myTracker.waitForAll();
      } catch (Exception e) {
         Logger.log("Error loading cursor: "+e, Logger.ERROR);
      }
      return image;
   }

   private IntBuffer createBuffer(Image img) {
      int len=img.getHeight(null)*img.getWidth(null);
      ByteBuffer temp=ByteBuffer.allocateDirect(len<<2);;
      temp.order(ByteOrder.LITTLE_ENDIAN);

      int[] pixels=new int[len];

      PixelGrabber pg=new PixelGrabber(img, 0, 0, img.getWidth(null), img.getHeight(null), pixels, 0, img.getWidth(null));

      try {
         pg.grabPixels();
      } catch (InterruptedException e) {
         Logger.log("Could not grab pixels from image!", Logger.ERROR);
      }

      for (int i=0; i<len; i++) {
         int pos=i<<2;
         int texel=pixels[i];
         if (texel!=0) {
            texel|=0xff000000;
         }
         temp.putInt(pos, texel);
      }

      return temp.asIntBuffer();
   }

   private void init(Frame comp) {
      Image cursor=loadCursor();
      if (comp!=null) {
         this.frame=comp;
         comp.addMouseMotionListener(this);
         comp.addMouseListener(this);
         lwjgl=false;
         center();
         java.awt.Cursor cur=Toolkit.getDefaultToolkit().createCustomCursor(cursor, new Point(15, 15),"crosshair");
         frame.setCursor(cur);
      } else {
         lwjgl=true;
         try {
            if (!Mouse.isCreated()) {
               Mouse.create();
            }
            try {
               oldCursorGL=Mouse.getNativeCursor();
               int maxSize=org.lwjgl.input.Cursor.getMaxCursorSize();
               if (maxSize<=cursor.getWidth(null)) {
                  org.lwjgl.input.Cursor cur=new org.lwjgl.input.Cursor(32, 32, 15, 15, 1, createBuffer(cursor), null);
                  Mouse.setNativeCursor(cur);
               }
            } catch (Exception e) {
               Logger.log("Error setting cursor!", Logger.WARNING);
            }
         } catch (Exception e) {
            //e.printStackTrace();
            Logger.log("Couldn't create LWJGL-mouse - initialize the OpenGL renderer first!", Logger.WARNING);
         }
      }
   }
}


efaj

Thank you. I tweaked and got it running wonderfully.