Slick Forums

Discuss the Slick 2D Library
It is currently Fri May 24, 2013 3:48 pm

All times are UTC




Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: Wed Apr 25, 2012 7:02 pm 
Offline

Joined: Tue Feb 21, 2012 3:46 pm
Posts: 26
Hy !

I have a little problem.
I wanted to port my game to an applet. In my game, I am using the packedspritesheet.
Normally I load my resources with the class.getResource() method which returns an URL. But the packedspritesheet takes only String parameters!?

Has somebody got an idea how to handle this? If there is no way to do that, I will have to modify the packedspritesheet class....

thanks!


Top
 Profile  
 
PostPosted: Thu Apr 26, 2012 1:22 am 
Offline
Slick Zombie

Joined: Sat Jan 27, 2007 7:10 pm
Posts: 1469
All of the string parameters for Slick (i.e. Image/Sound loading too) should forward to getResource/getResourceAsStream, so it should be fine for applets/JARs.


Top
 Profile  
 
PostPosted: Thu Apr 26, 2012 4:07 pm 
Offline
Slick Zombie

Joined: Fri Jan 29, 2010 7:02 pm
Posts: 1173
This is only true for simple classpath setups. In more complex setups the Classloader which loaded Slick might not be able to access the application classpath / resources. The only reliable way to access resources for a library is to use the URL passed in by the application.

_________________
TWL - The Themable Widget Library


Top
 Profile  
 
PostPosted: Thu Apr 26, 2012 6:29 pm 
Offline

Joined: Tue Feb 21, 2012 3:46 pm
Posts: 26
If I want to use the PackedSpriteSheet , I have to pass a String. There is no constructor that takes an URL.

So I only can use it that way

Code:
spriteSheet = new PackedSpriteSheet(GameOne.class.getResource(defPath)
            .getPath());


But when I use it like that, there comes an error prompt "Resource not found"?!

UPDATE:

I found an really ugly workaround.
I modified the PacketSpriteSheet.class and hardcoded the resource loading part. But it doesn't matter for me. I only wanted to fix it as fast as possible.

Code:
private void loadDefinition(String def, Color trans) throws SlickException {
      BufferedReader reader = new BufferedReader(new InputStreamReader(
            PackedSpriteSheet.class
                  .getResourceAsStream("game_one_spritesheet.def")));

      try {

         image = new Image(
               PackedSpriteSheet.class
                     .getResourceAsStream("game_one_spritesheet.png"),
               "", false);
         reader.readLine();
         while (reader.ready()) {
            if (reader.readLine() == null) {
               break;
            }

            Section sect = new Section(reader);
            sections.put(sect.name, sect);

            if (reader.readLine() == null) {
               break;
            }
         }
      } catch (Exception e) {
         Log.error(e);
         throw new SlickException(
               "Failed to process definitions file - invalid format?", e);
      }
   }


And I copied the files I had to load in the same package as the modified PacketSpriteSheet.class.

Now it loads without errors :roll:


Top
 Profile  
 
PostPosted: Thu Apr 26, 2012 7:04 pm 
Offline
Slick Zombie

Joined: Sat Jan 27, 2007 7:10 pm
Posts: 1469
Slick's resource loading depends on String, which for more complex applications (what MatthiasM is talking about) might be a bit limiting.

But in your case a string will work just fine. Like I said, slick's resource loaders will handle the string as if you were calling Class.getResource. So your code should look like this:
Code:
new PackedSpriteSheet(defPath);


EDIT: The same applies to Image, Sound, etc.


Top
 Profile  
 
PostPosted: Fri Apr 27, 2012 1:16 pm 
Offline

Joined: Tue Feb 21, 2012 3:46 pm
Posts: 26
Ok. But what about the source of the string variable?

I only know the relative path not the full path!

e.g. res/file.def (where res is a package and file.def the definition file for the packedspritesheet)


Top
 Profile  
 
PostPosted: Fri Apr 27, 2012 3:55 pm 
Offline
Slick Zombie

Joined: Sat Jan 27, 2007 7:10 pm
Posts: 1469
Yeah, that's the idea. e.g.
Code:
new PackedSpriteSheet("res/sheet.def");


I'd recommend looking at some of the slick tests to get a better feel for the library. :)


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


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:  
Powered by phpBB® Forum Software © phpBB Group