将 Android 输入移植到 Java 桌面应用程序

Porting Android Input to Java Desktop Application

我正在关注 Mario Zechner 的书 "Beginning Android 4 Games Development"。

如果您想查看通过阅读本书开发的 Android 框架,请看这里:

Android Game Framework

现在,在深入阅读本书并开始使用 OpenGl 之前,我决定通过尝试将框架移植到它的 Java 版本来提高我的基础 Java 知识。

我想编写一个使用框架的游戏,当将这些游戏类复制到我的 Java 版本时,它会简单地使用具有不同接口实现的相同框架。

因此,当尝试在 java 中实现顶级接口时,我不得不遇到一些问题,例如 - 什么是 AssetsManager 等价物:

书中为 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);
  }


}

而且我不知道如何在 Java 中做同样的事情,关于上面使用的 AssetsManager 也出现在另一个 Android 框架的实现中。

我曾经像这样在 Java 中加载文件:

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) {
    }
  }
}

你会怎么解决?该框架并不太复杂,您可以通过查看上面链接的 GitHub Rep. 得到很好的猜测。

我不需要确切的代码解决方案,但也许您可以分享一些有关如何执行此操作的技巧。

我应该简单地扩展我的接口吗?后来以某种方式使用顶级界面告诉游戏使用了哪个界面?所以被调用的方法如:game.getInput().getTouchedX() 知道他们是否需要调用 Android GameFramework 或 JavaFrameworks 输入 x 版本? :P

提前致谢

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 ) );
  }


}