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:
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