Slick Forums

Discuss the Slick 2D Library
It is currently Sun Oct 20, 2019 10:19 pm

All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Some ideas
PostPosted: Sun Jul 10, 2011 1:41 pm 
Offline

Joined: Tue Jun 14, 2011 5:09 pm
Posts: 6
Hi,

as I recently posted, I need to subclass some of the original Slick classes, StateBasedGame is the most important one. In this class, I had to override the update and render methods, which are declared as final - why?
To circumvent this problem, I had to copy the files from the SVN and midified them accordingly - a pretty ugly solution as I think : D

Additionally, some methods in Input like these would be nice:

Returns the controller id for a device name
Code:
public int getControllerID(final String name) {
      int id = 0;
      for (final Controller c : Input.controllers) {
         if (c.getName().equals(name)) {
            return id;
         }
         ++id;
      }
      return Integer.MIN_VALUE;
   }


Returns the name of the controller which has this ID
Code:
public String getControllerName(final int controller) {
      if (controller > Input.controllers.size()) {
         throw new IllegalArgumentException("Controller " + controller + " does not exist!");
      }
      return (Input.controllers.get(controller)).getName();
   }


Returns true if there is such a controller
Code:
public boolean hasController(final String name) {
      for (final Controller c : Input.controllers) {
         if (c.getName().equals(name)) {
            return true;
         }
      }
      return false;
   }


Returns all controllers
Code:
public List<Controller> getControllers() {
      return Input.controllers;
   }


Gets the controller which has this name
Code:
public Controller getController(final String deviceName) {
      for (final Controller c : Input.controllers) {
         if (c.getName().equals(deviceName)) {
            return c;
         }
      }
      throw new IllegalArgumentException("This controller does not exist!");
   }


Reloads the controllers, the first if clause was removed
Code:
public void reinitControllers() throws SlickException {
      controllersInited = true;
      try {
         Controllers.create();
         int count = Controllers.getControllerCount();
         
         for (int i = 0; i < count; i++) {
            Controller controller = Controllers.getController(i);

            if ((controller.getButtonCount() >= 3) && (controller.getButtonCount() < MAX_BUTTONS)) {
               controllers.add(controller);
            }
         }
         
         Log.info("Found "+controllers.size()+" controllers");
         for (int i=0;i<controllers.size();i++) {
            Log.info(i+" : "+((Controller) controllers.get(i)).getName());
         }
      } catch (LWJGLException e) {
         if (e.getCause() instanceof ClassNotFoundException) {
            throw new SlickException("Unable to create controller - no jinput found - add jinput.jar to your classpath");
         }
         throw new SlickException("Unable to create controllers");
      } catch (NoClassDefFoundError e) {
         // forget it, no jinput availble
      }
   }


KM


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 22, 2011 10:49 pm 
Offline
Site Admin
User avatar

Joined: Thu Jan 01, 1970 12:00 am
Posts: 3143
They're final because overriding them will prevent the super class doing it's work and hence you have to remember to call the super which is bad API design. Instead there are strategy methods

Kev


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 10 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group