I'm working on a Java
based game for desktop using JOGL
. But I'm considering porting the core part of game to 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.
Most notably however, the OpenGL
drawing context GL2
is different on desktop
and android
ie
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?
Or do I need to create a totally separate project called android-core
to be dependency for android-game
?
I'm trying to resuse the same core project for CI
based building.
Snippet from core-game
which would not work with 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
. That might also mean moving the draw
method out of shape
.
One possible solution is to make a ShapeDrawer
class, which doesn't refer to GL2
, which is implemented in 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
:
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. 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. When the desktop launcher creates the GameEngine
, it can pass a new GL2ShapeDrawer
.
This is only one possible solution. Use it as a starting point (if you want), but don't treat it as gospel.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.