Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  Abstract input management - need feedback  (Read 2125 times)
0 Members and 2 Guests are viewing this topic.
Offline aldacron

Full Member
**

Posts: 200


Java games rock!


« on: 2004-02-09 18:27:43 »

With the Simplicity project I'm trying to work out an input abstraction layer which should be able to support multiple input APIs (JInput, AWT, LWJGL). What's killing me is how to handle translation of key constants.

Mapping Simplicity's constants to the API constants is not the issue. The problem lies in going the other way. For example, I could number the Simplicity contants (0...n), then the concrete implementation would load an array which could be used for mapping. Since I can't rely on API key constants being sequential, or even within a sane range (wouldn't want an array of 1000 ints if I'm only going to use 100 of them), then that method is out for the reverse mapping.

A few ideas I've thought of:

1) The obvious switch statement. This is ugly, bulky, and not something I favor.

2) A hashmap - API constants as keys, Simplicity constants as values. I could still use the array for mapping Simplicity to API constants. This would require an Integer object to be created for each constant (both keys and values) at startup, then the cost for searching the hashmap, fetching the Integer and pulling the int value each time I want to lookup a keycode.

3) Simplicity constants could be non-final statics. Each concrete implementation would set them at load time to the appropriate API constant value. The drawback is that they could be accidently modified during runtime causing the input handling to break.

4) No Simplicity constants. Instead, define an interface with methods such as getKeyA(), getKeyUp(), etc... Implementations could simply return the appropriate API constant. The only drawbacks I see here are that the class created to implement the interface would likely take up more memory than constants would (nothing major), and implementations would require a lot more typing!

Performance doesn't really concern me as Simplicity's input system is event based, so there won't be needless polling of every key each frame. I'm also not concerned about memory overhead, as it is miniscule in the grand scheme of things (although it does just feel icky redefining constants that are already defined elsewhere - the price of abstraction).

So, with the exception of number 1, all of these techniques are viable for me. I really prefer #3, and it's currently how I plan to implement it. This is often done in C++ (externed constants in a header initialized in the source file of a concrete implementation, for example). I'm curious if anyone has any other suggestions/comments on how to go about it.
Offline elias

JGO Ninja
***

Posts: 638



« Reply #1 on: 2004-02-09 23:31:59 »

I wouldn't worry about performance either, so the HashMap option sounds just about right from where I sit.

- elias

Offline princec
« League of Dukes »

JGO Kernel
*****

Posts: 8074
Medals: 91


Eh? Who? What? ... Me?


« Reply #2 on: 2004-02-10 04:07:04 »

Hashmap, for sure. Very easy to implement, and we're talking an unmeasurably small performance hit on the rare occasions a key is typed.

Cas Smiley

Games published by our own members! Go get 'em!
Offline swpalmer

JGO Kernel
*****

Posts: 3438
Medals: 4


Where's the Kaboom?


« Reply #3 on: 2004-02-10 13:55:00 »

Simplicity constants should be object references - using the typesafe enum pattern.  (Or 1.5 enums)
Then when you look them up using the HashMap you don't have to convert the value to an int at all.

Offline aldacron

Full Member
**

Posts: 200


Java games rock!


« Reply #4 on: 2004-02-10 18:10:08 »

THanks for the input guys. I often have this horrid fear of instantiating many small objects, which leads me to struggle with decisions like this one. I do some wacky stuff sometimes to avoid it.
Offline princec
« League of Dukes »

JGO Kernel
*****

Posts: 8074
Medals: 91


Eh? Who? What? ... Me?


« Reply #5 on: 2004-02-11 02:36:28 »

Instantiating small objects should never be a worry. Until you have to make hundreds of thousands of them.

Cas Smiley

Offline William Denniss

JGO Kernel
*****

Posts: 1837
Medals: 5


Fire at will


« Reply #6 on: 2004-02-11 22:21:26 »

Will this input abstraction layer be able to be used independently?  This is exactly the sort of thing which we need for Xith3D and I'm guessing other projects too.

What license will you be using?

Will.

Offline aldacron

Full Member
**

Posts: 200


Java games rock!


« Reply #7 on: 2004-02-12 04:23:08 »

Yes, it could be used independently. The only ties it has to the rest of the framework are the custom exceptions I have in the top-level package.

The entire Simplicity project is being released under the same BSD-style license as Xith3D.
Offline William Denniss

JGO Kernel
*****

Posts: 1837
Medals: 5


Fire at will


« Reply #8 on: 2004-02-13 16:55:16 »

great Smiley

Offline tgeorge

JGO n00b
*

Posts: 18


Java games rock!


« Reply #9 on: 2004-02-13 17:36:48 »

Is there any code or anything released yet ?

I'm in the middle of implementing something very similar for
cross-toolkit SWT.  Can I take a gander at what you're doing
for this?  If so, how, where is the CVS or file store ?

                                           -T
Games published by our own members! Go get 'em!
Offline aldacron

Full Member
**

Posts: 200


Java games rock!


« Reply #10 on: 2004-02-15 04:44:22 »

I don't have any of the input packaged checked in yet. Working on it piecemeal, so it will be a few more days. simplicity.dev.java.net is the project the package is a part of.
Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.113 seconds with 19 queries.