1

I was following the Book "Beginning Android 4 Games Development" by Mario Zechner.

If you want to see the Android Framework developed by reading the Book look here:

Android Game Framework

Now, before diving further into the book and starting OpenGl, I decided to improve my fundal Java Knowledge by trying to port the Framework to a Java Version of it.

I think of having a Game written which uses the Framework, and when copying those GameClasses to my Java Version,it would simply use the same Framework with different Implementations of the Interfaces.

So when trying to implement the top interfaces in java I had to stumble along problems such as - what is the equivalent of an AssetsManager etc:

The Part solved in the Book for the Android GameFramework:

________INTERFACE______

public interface FileIO {
  // Load assets from apk package
  InputStream readAsset(String fileName) throws IOException;

  // Load files from storage (SD)
  InputStream readFile(String fileName) throws IOException;
  OutputStream writeFile(String fileName) throws IOException;

}

_______ANDROID IMPLEMENTATION_______

public class AndroidFileIO implements FileIO {

  Context context;
  AssetManager assets;
  String externalStoragePath;


  // Constructor
  public AndroidFileIO(Context context) {

    this.context = context;
    this.assets = context.getAssets();
    this.externalStoragePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator;

  }

  @override
  public InputStream readAsset(String fileName) throws IOException {
    return assets.open(fileName);
  }

  @override
  public InputStream readFile(String fileName) throws IOException {
    return new FileInputStream(externalStoragePath + fileName);
  }


  @override
  public OutputStream writeFile(String fileName) throws IOException {
    return new FileOutputStream(externalStoragePath + fileName);
  }


}

And me not knowing of how to do the same in Java, regarding that the above used AssetsManager also appears in another Android Implementation of the Framework.

I used to load files in Java like this:

public void load(String filename){

  BufferedReader in = null;
  File file1 = new File("/Users/jesjjes/AndroidStudioProjects/MapWindowTest/app/src/main/assets/"+filename+".text");

  try {
    in = new BufferedReader(new InputStreamReader(new FileInputStream(file1)));

  } catch (IOException e) {
    // :( It's ok we have defaults
  } catch (NumberFormatException e) {
    // :/ It's ok, defaults save our day
  }
  finally {

    try {
      if (in != null)
      in.close();
    }
    catch (IOException e) {
    }
  }
}

How would you solve it? The Framework is not too complex, u can get a good guess at looking into the linked GitHub Rep. above.

I don't need exact Code Solution, but maybe you can share some tips on how to perform this.

Should i simply extend my interfaces? And later somehow using a top Interface which tells the Game which one is used? So the called Methods like : game.getInput().getTouchedX() know if they need to call the Android GameFramework or the JavaFrameworks input x version? :P

Thanks in advance

Yesyoor
  • 157
  • 1
  • 18

1 Answers1

1
public class DesktopFileIO implements FileIO {

  private final File externalStoragePath;
  private final File assetPath;


  // Constructor
  public DesktopFileIO(String externalStoragePath, String assetPath) {
    this(new File(externalStoragePath), new File(assetPath));
  }

  // Constructor
  public DesktopFileIO(File externalStoragePath, File assetPath) {
    super();
    this.externalStoragePath = externalStoragePath;
    this.assetPath = assetPath;
  }

  @override
  public InputStream readAsset(String fileName) throws IOException {
    return new FileInputStream( new File(assetPath, fileName) );
  }

  @override
  public InputStream readFile(String fileName) throws IOException {
    return new FileInputStream( new File(externalStoragePath, fileName) );
  }

  @override
  public OutputStream writeFile(String fileName) throws IOException {
    return new FileOutputStream( new File( externalStoragePath, fileName ) );
  }


}
JimmyB
  • 12,101
  • 2
  • 28
  • 44
  • ok there is one error in the second constructor saying that this.externalStoragePath = externalStoragePath needs a String, as defined above the Constructor. How is this assignment supposed to be? I guess its a typing mistake in the second Constructor and the first parameter should be of String type also? Or shall i extract the String of the passed File? – Yesyoor Apr 13 '16 at 09:16
  • one last question, in the second constructor you call super(). Why is this not needed in the first Constructer? – Yesyoor Apr 13 '16 at 09:26
  • Nevermind, the second constructor is called by the first. but still i wonder why super() is called. I posted below the changes in the second Constructor ( extract path String of the file) – Yesyoor Apr 13 '16 at 09:32
  • Re: calling `super()` - not required but [arguably serves the clarity of the code](http://stackoverflow.com/questions/10508107/why-call-super-in-a-constructor). – JimmyB Apr 13 '16 at 11:05
  • ok i kind of got it, but in this case what is the superclass of DesktopFileIO? – Yesyoor Apr 13 '16 at 12:05
  • 1
    Without an `extends`, the superclass is always implicitly `java.lang.Object`. – JimmyB Apr 13 '16 at 12:19
  • okay sir :) now i got it, thanks for the answers ! My posted changes are wrong, I took over your example wrong, having changed the externalStoragePath of the file type to String myself somehow :D sorry for my embarrassing questions that followed. you helped me out very much! – Yesyoor Apr 13 '16 at 12:45