[英]Creating a portable opengl engine for swing and android Java based application
I'm working on a Java
based game for desktop using JOGL
. 我正在使用
JOGL
基于Java
的桌面游戏。 But I'm considering porting the core part of game to Android
. 但是我正在考虑将游戏的核心部分移植到
Android
。
Currently I have Swing
(UI) related code in 1 project called desktop-game
, and that project has a dependency on another project called core-game
, which has basic functionality and is, for the most part, portable without any changes. 目前,我在1个名为
desktop-game
项目中具有与Swing
(UI)相关的代码,并且该项目依赖于另一个名为core-game
项目,该项目具有基本功能,并且在大多数情况下都可移植而无需任何更改。
Most notably however, the OpenGL
drawing context GL2
is different on desktop
and android
ie 然而,最值得注意的是,
OpenGL
绘图上下文GL2
在desktop
和android
上是不同的,即
desktop = import com.jogamp.opengl.GL2;
android = import android.opengl.GLES20;
Is there any way I could resuse the same core-game
as a dependency for a new project, lets say android-game
.. which has android
specific UI? 有什么办法可以重用相同的
core-game
作为新项目的依赖项,比如说具有android
特定UI的android-game
..?
Or do I need to create a totally separate project called android-core
to be dependency for android-game
? 还是我需要创建一个完全独立的项目
android-core
来依赖android-game
?
I'm trying to resuse the same core project for CI
based building. 我试图将相同的核心项目重用于基于
CI
的构建。
Snippet from core-game
which would not work with android-game
.. core-game
,不适用于android-game
..
import com.jogamp.opengl.GL2;
public abstract class Shape{
public abstract void draw(GL2 gl2, Vec3 position, float angle);
}
You need to make sure that core-game
has no references anywhere to GL2
. 您需要确保
core-game
在任何地方都没有对GL2
引用。 That might also mean moving the draw
method out of shape
. 这也可能意味着使
draw
方法shape
。
One possible solution is to make a ShapeDrawer
class, which doesn't refer to GL2
, which is implemented in desktop-game
: 一种可能的解决方案是制作一个
ShapeDrawer
类,该类不引用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);
}
and then use it in the shape 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 ...
}
then in desktop-game
you could implement ShapeDrawer
: 然后在
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 ...
}
}
This way, core-game
never directly sees a GL2
object. 这样,
core-game
永远不会直接看到GL2
对象。 It does mean that you need to get desktop-game
to pass the shape drawer to core-game
- for example, if you have a GameEngine
class, then maybe its constructor should take the ShapeDrawer
as an argument. 这确实意味着您需要获得
desktop-game
才能将形状抽屉传递给core-game
-例如,如果您具有GameEngine
类,则其构造函数可能应该将ShapeDrawer
作为参数。 When the desktop launcher creates the GameEngine
, it can pass a new GL2ShapeDrawer
. 当桌面启动器创建
GameEngine
,它可以传递新的GL2ShapeDrawer
。
This is only one possible solution. 这只是一种可能的解决方案。 Use it as a starting point (if you want), but don't treat it as gospel.
将其用作起点(如果需要),但不要将其视为福音。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.