为 swing 和基于 android Java 的应用程序创建一个可移植的 opengl 引擎

Creating a portable opengl engine for swing and android Java based application

我正在使用 JOGL 开发基于 Java 的桌面游戏。但我正在考虑将游戏的核心部分移植到 Android

目前我在名为 desktop-game 的 1 个项目中有 Swing (UI) 相关代码,并且该项目依赖于另一个名为 core-game 的项目,该项目具有基本功能,并且在大多数情况下无需任何更改即可移植。

然而,最值得注意的是,OpenGL 绘图上下文 GL2desktopandroid 上是不同的,即

desktop = import com.jogamp.opengl.GL2;
android = import android.opengl.GLES20;

有什么方法可以重新使用相同的 core-game 作为新项目的依赖项,比方说 android-game .. 其中有 android 特定的 UI?

或者我是否需要创建一个名为 android-core 的完全独立的项目作为 android-game 的依赖项?

我正在尝试为基于 CI 的建筑重新使用相同的核心项目。

来自 core-game 的片段不适用于 android-game ..

import com.jogamp.opengl.GL2;

public abstract class Shape{

    public abstract void draw(GL2 gl2, Vec3 position, float angle);
}

您需要确保 core-game 没有对 GL2 的任何引用。这也可能意味着将 draw 方法移出 shape.

一个可能的解决方案是制作一个 ShapeDrawer class,它不引用 GL2,它在 desktop-game:

中实现
public abstract class ShapeDrawer {
    public abstract void drawRectangle(Rectangle r, Vec3 position, float angle);
    public abstract void drawCircle(Circle c, Vec3 position, float angle);
}

然后在形状 classes:

中使用它
public abstract class Shape{
    public abstract void draw(ShapeDrawer drawer, Vec3 position, float angle);
}

public class Rectangle extends Shape {
    ... other stuff ...
    public void draw(ShapeDrawer drawer, Vec3 position, float angle) {
        drawer.drawRectangle(this, position, angle);
    }
    ... other stuff ...
}

public class Circle extends Shape {
    ... other stuff ...
    public void draw(ShapeDrawer drawer, Vec3 position, float angle) {
        drawer.drawCircle(this, position, angle);
    }
    ... other stuff ...
}

然后在 desktop-game 你可以实现 ShapeDrawer:

public class GL2ShapeDrawer extends ShapeDrawer {
    private GL2 gl;
    public GL2ShapeDrawer(GL2 gl) {
        this.gl = gl;
    }
    public void drawRectangle(Rectangle r, Vec3 position, float angle) {
        ... drawing code ...
    }
    public void drawCircle(Circle c, Vec3 position, float angle) {
        ... drawing code ...
    }
}

这样,core-game 永远不会直接看到 GL2 对象。这确实意味着您需要获取 desktop-game 才能将形状抽屉传递给 core-game - 例如,如果您有一个 GameEngine class,那么它的构造函数可能应该采用ShapeDrawer 作为参数。当桌面启动器创建 GameEngine 时,它可以传递一个新的 GL2ShapeDrawer.

这只是一种可能的解决方案。将它作为一个起点(如果你愿意的话),但不要把它当作福音。