Skip to content

Instantly share code, notes, and snippets.

@sinistersnare
Created September 27, 2013 18:25
Show Gist options
  • Save sinistersnare/6732960 to your computer and use it in GitHub Desktop.
Save sinistersnare/6732960 to your computer and use it in GitHub Desktop.
JyGDX
from com.badlogic.gdx import ApplicationListener, Gdx
from com.badlogic.gdx.graphics.g2d import SpriteBatch
from com.badlogic.gdx.graphics import Texture, OrthographicCamera, GL10
class GdxJython(ApplicationListener):
def __init__(self):
self.camera = None
self.batch = None
self.texture = None
self.sprite = None
self.taps = 0
def create(self):
self.camera = OrthographicCamera()
self.camera.setToOrtho(False, 800, 480)
self.batch = SpriteBatch()
self.texture = Texture(Gdx.files.internal("data/libgdx.png"))
def dispose(self):
self.batch.dispose()
self.texture.dispose()
def render(self):
Gdx.gl.glClearColor(0,1,0,0)
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT)
if Gdx.input.justTouched():
self.taps += 1
print "TAPS: " + str(self.taps)
self.batch.setProjectionMatrix(self.camera.combined)
self.batch.begin()
self.batch.draw(self.texture,0,0, 800, 480)
self.batch.end()
def resize(self, width, height):
pass
def pause(self):
pass
def resume(self):
pass
package com.badlogic.GdxJython;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.plyjy.exceptions.ModuleNotFoundException;
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;
public class JythonObjectFactory {
private JythonObjectFactory() {}
// todo: implement args.
// would need to coerce each arg into a java object...hmmm
public static Object createObject(Object interfaceType, String moduleName,
String className) {
Object javaObj = null;
PyObject pyObject = null;
PythonInterpreter interpreter = new PythonInterpreter();
try {
try {
interpreter.exec("from " + moduleName + " import " + className);
pyObject = interpreter.get(className);
} catch (Exception ie) {
throw new ModuleNotFoundException("The module '" + moduleName
+ "' is not found.");
}
} catch (ModuleNotFoundException ex) {
Logger.getLogger(JythonObjectFactory.class.getName()).log(
Level.SEVERE, null, ex);
ex.printStackTrace();
}
try {
PyObject newObj = pyObject.__call__();
javaObj = newObj.__tojava__(Class.forName(interfaceType.toString()
.substring(interfaceType.toString().indexOf(" ") + 1,
interfaceType.toString().length())));
} catch (ClassNotFoundException ex) {
Logger.getLogger(JythonObjectFactory.class.getName()).log(
Level.SEVERE, null, ex);
}
return javaObj;
}
public static Object createObject(Object interfaceType, String moduleName) {
return createObject(interfaceType, moduleName, moduleName);
}
}
package com.badlogic.GdxJython;
import org.python.util.PythonInterpreter;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
public class Main {
public static void main(String[] args) {
LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
cfg.title = "GdxJython";
cfg.useGL20 = false;
cfg.width = 480;
cfg.height = 320;
String pathToPyFiles = "/home/sinistersnare/Development/testing/jython/GdxJython-desktop/py";
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.exec("import sys");
interpreter.exec(String.format("sys.path.append('%s'); print sys.path", pathToPyFiles));
ApplicationListener app = (ApplicationListener) JythonObjectFactory
.createObject(ApplicationListener.class, "Gdx", "GdxJython");
new LwjglApplication(app, cfg);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment