Slick Forums

Discuss the Slick 2D Library
It is currently Sun May 26, 2013 9:34 am

All times are UTC




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Saved State on Android
PostPosted: Sun Jul 24, 2011 7:33 pm 
Offline
Oldbie
User avatar

Joined: Thu Jan 13, 2011 4:42 pm
Posts: 349
I've ported an android game that uses the SavedState class to save the game. On android, this does not work. Anyone know why, and if so, anyone know how to solve it?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 28, 2011 9:10 am 
Offline
Slick Zombie

Joined: Wed Apr 02, 2008 1:32 pm
Posts: 1315
Location: Italy
It throws and exception ?

_________________
Blog | Last game Gravity Duck tribute | In progress Gravity Duck tribute


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 28, 2011 3:58 pm 
Offline
Oldbie
User avatar

Joined: Thu Jan 13, 2011 4:42 pm
Posts: 349
No actually. It doesn't complain at all. That's part of why it's so confused. It claims to be saving to be using the local filesystem in the output, but the game won't save like It does on desktop. I'll keep looking though. Mabye there is an error somewhere, and I just haven't noticed it.


Top
 Profile  
 
 Post subject: Fix.
PostPosted: Thu Aug 04, 2011 7:16 pm 
Offline

Joined: Sun Jul 24, 2011 3:52 pm
Posts: 19
Android doesn't do file output the same way a desktop does(it's more secure) which is why SavedState doesn't work unless you specify it to save your files to an SD-Card.

I implemented SavedState in my code to test out an AndroidMuffin class I made (just for you lol) and it's working fine for me. You need to add the following to your Slick-AE sources:

In the package org.newdawn.slick.muffin, you need to add a class called AndroidMuffin. The contents are displayed below:



Code:
package org.newdawn.slick.muffin;

import android.content.Context;
import android.os.Environment;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import org.newdawn.slick.util.Log;

/**
*
* @author mangelok
*/
public class AndroidMuffin implements Muffin {

    public static Context app;

    @Override
    public void saveFile(HashMap data, String fileName) throws IOException {
        String userHome = Environment.getDataDirectory().getPath();
        File file = new File(userHome);
        file = new File(file, ".java");
        if (!file.exists()) {
            file.mkdir();
        }

        file = new File(file, fileName);
        FileOutputStream fos = app.openFileOutput(file.getName(), Context.MODE_PRIVATE);
        ObjectOutputStream oos = new ObjectOutputStream(fos);

        // save hashMap
        oos.writeObject(data);

        oos.close();
    }

    @Override
    public HashMap loadFile(String fileName) throws IOException {
        HashMap hashMap = new HashMap();
        FileInputStream fis = null;
        try {
            fis = app.openFileInput(fileName);
        } catch (FileNotFoundException fe) {
            Log.error("SavedState files not found!");
            return hashMap;
        }

        try {
            ObjectInputStream ois = new ObjectInputStream(fis);
            hashMap = (HashMap) ois.readObject();
            ois.close();
        } catch (EOFException e) {
            // End of the file reached, do nothing
        } catch (ClassNotFoundException e) {
            Log.error(e);
            throw new IOException("Failed to pull state from store - class not found");
        }

    return hashMap;
    }

   

}


In the SlickActivity class you need to add
Code:
org.newdawn.slick.muffin.AndroidMuffin.app = this.getApplicationContext();

to the beginning of your onCreate method (just after super.onCreate(sis))

and lastly, in the SavedState class, the constructor looks like this:

Code:
public SavedState(String fileName) throws SlickException {
      this.fileName = fileName;
      
                if (isAndroidAvailable()){
                    muffin = new AndroidMuffin();
                }
                else if (isWebstartAvailable()) {
         muffin = new WebstartMuffin();
      }
      else {
         muffin = new FileMuffin();
      }
      
      try {
         load();
      } catch (IOException e) {
         throw new SlickException("Failed to load state on startup",e);
      }
   }


and you need to add the method isAndroidAvailable() which looks like this:

Code:
       
private boolean isAndroidAvailable() {
                //my pathetic android checking method
      try {
         Class.forName("android.app.Activity");
         Log.info("Android detected using Muffins");
      } catch (Exception e) {
         Log.info("Using Local File System");
         return false;
      }
      return true;
   }


Hope this helps and is not too confusing!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 04, 2011 7:55 pm 
Offline
Oldbie
User avatar

Joined: Thu Jan 13, 2011 4:42 pm
Posts: 349
WoW! that's a lot of code. I haven't tested it yet, but I think I've gotten it all. The only place I'm unsure about is the SavedState class changes. I assumed that I make a copy in the Slick-AE project to add the changes too and then add <include name="org/newdawn/slick/SavedState.class"/> to the ant build after the <echo>Removing Replaced Classes</echo> line. I don't know ant, and I don't have access to the android device I test on, and my emulators don't work, so I could be wrong.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 04, 2011 8:07 pm 
Offline

Joined: Sun Jul 24, 2011 3:52 pm
Posts: 19
That seems right to me. Personally, I've merged my Slick-AE with Slick (by simply copying AE's sources and libraries into slick and overwriting everything) and then in slick's build.xml I tell it to copy the compiled jar into my game's libs folder. This way, I can make any changes I want to Slick, compile it, then build my game and it will have the revised jars instantly.

There isn't that much edited code actually. It's based on the originals just edited again.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 06, 2011 3:03 am 
Offline
Oldbie
User avatar

Joined: Thu Jan 13, 2011 4:42 pm
Posts: 349
I tested, and it works! I agree that having the source for both Slick and Slick-AE is extremely useful, but I would still like to keep slick & slick-AE separate (somehow this seems cleaner). I import slick into slick-AE and build from there. I don't know much about ant build, but I think I've managed to get it to overwrite whatever slick classes necessary. Thanks anyway, and of course, thanknyou very much for sharing your new SavedState code!


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