iPhone + kev g

Hi,

I’m not sure if you follow Kev Glass’s blog, but he’s been doing some amazing things with the iPhone (and Android and applets). I hope he’s ok with me posting this, it’s just that I know that there’s people here who are busting to use java on the iPhone, and that’s sort of what kev has managed to do:

http://www.cokeandcode.com/kevsblog

yup its pretty awesome stuff, been following it too, you simply write code with the simple api and it’ll work as an applet, android and the iphone app (no jailbreak needed), very cool stuff.

That’s cool! It’s sort of too bad that I’ve already ported my Java 2D game API to iPhone (manually) and then written an entire game straight in Objective-C, but maybe I can port some of my other Java games over this way. The problem is whether any of my games are fun enough to be worth the time… :stuck_out_tongue:

Sounds like you’d be an excellent candidate for porting the remaining library code abstraction layer into Objective-C =)

It’s a noble pursuit; but i’m pretty sure there are other automated solutions in existance for doing it that are in a much more mature state. (though they’ll obviously cost $$$)

Innaworks’ alcheMo is one for sure.

I’m curious how Java’s more complex language constructs are translated into objective-c; finally blocks, threading & synchronization, etc.
From what I remember, that’s what made J2ME -> Brew more complicated than one would have first imagined.

Perhaps. :stuck_out_tongue:

I really don’t have the time to do something like this, though. I don’t even have time to do my own games anymore. :frowning:

Sounds all very interesting. Would be great if you could easy port Java code to iPhone.
I wouldn’t mind seeing the results for myself.

I’m fine with you posting it.

Kev

This image describes most of what I’ve been doing.

Kev

Welcome back Kev :slight_smile:

I knew I had never seen the two of you in the same room at the same time. How was Krypton?

Welcome back dude.

Hi Kev.

didnt know you long, but nice to have you back.

It’s definitely an intriguing project! I ported my game Aevum Obscurum to both the Android & iPhone platform and I noted that Objective C is actually a lot closer to Java than I initially though. I ended up converting Android Java code directly to Objective C. However, I guess the crux lies in the detail.

Hey nice to hear from you again Kev! I always knew you’d be drawn back! 8)

For anyone interested, I got some OpenGL ES stuff working. I’ve used NeHe lesson 4 (a rotating triangle and quad) as a reference since it’s a nice simple proof of concept.

Running in the Java base simulator in windows - this is using JOGL currently cause I felt like a change. Hopefully use DGLES+JNA at some point soon:

http://www.cokeandcode.com/aboid/nehe4_sim.png

And this is the same java code, passed through XMLVM into objc and compiled for the iphone. This is the iphone emulator on OSX:

http://www.cokeandcode.com/aboid/nehe4_emu.png

Here’s the code thats running it which I think looks reasonably like normal Java, except maybe the buffer usage. Thats simply because of my limited objc implementation of java.nio.*Buffer.


package org.xmlvm.iphone.gl.nehelesson4;

import java.nio.FloatBuffer;

import org.xmlvm.iphone.CGRect;
import org.xmlvm.iphone.gl.BufferUtils;
import org.xmlvm.iphone.gl.GL;
import org.xmlvm.iphone.gl.GLView;

public class NeHeLesson4View extends GLView {
	private static final float TO_RADIANS = (3.14159f / 180.0f);
	
	private boolean viewSetup = false;
	private float zFar;
	private float fieldOfView;
	
	private float rtri = 0;
	private float rquad = 0;
	
	private FloatBuffer triVertices;
	private FloatBuffer triVertexColors;
	private FloatBuffer quadVertices;
	
	private float viewWidth;
	private float viewHeight;
	
	public NeHeLesson4View(CGRect rect) {
		super(rect);
	
		viewWidth = rect.size.width;
		viewHeight = rect.size.height;
		System.out.println("View: "+viewWidth+","+viewHeight);
		
               setOpaque(true);
               setClearsContextBeforeDrawing(false);
	}

	private void initViewGL() {
		System.out.println("Init view");
		triVertices = BufferUtils.createFloatBuffer(9);
		triVertexColors = BufferUtils.createFloatBuffer(12);
		quadVertices = BufferUtils.createFloatBuffer(12);
		
		triVertices.put(-1).put(-1).put(0.0f)
		.put(1).put(-1.0f).put(0.0f)
		.put(-1.0f).put(1.0f).put(0.0f);
		
		triVertexColors.put(1).put(0).put(0).put(1)
		.put(0).put(1).put(0).put(1)
		.put(0).put(0).put(1).put(1);
		
		quadVertices.put(-1).put(1).put(0)
		.put(1).put(1).put(0)
		.put(1).put(-1).put(0)
		.put(-1).put(-1).put(0);
		
		float zNear = 0.1f, 
		zFar = 1000.0f, 
		fieldOfView = 60.0f; 
		float size; 
		
		GL.glMatrixMode(GL.GL_PROJECTION); 
		size = zNear * (float) Math.tan((TO_RADIANS * fieldOfView) / 2.0); 
		GL.glLoadIdentity();
		GL.glFrustumf(-size, size, -size / (viewWidth / viewHeight), size / 
				   (viewWidth / viewHeight), zNear, zFar); 
		
		GL.glViewport(0, 0, (int) viewWidth, (int) viewHeight); 
		GL.glMatrixMode(GL.GL_MODELVIEW); 
		GL.glLoadIdentity(); 
		GL.glClearColor(0.0f, 0.0f, 0.0f, 1.0f); 
	}
	
	@Override
	public void renderView() {
		if (!viewSetup) {
			viewSetup = true;
			initViewGL();
		}
		
		GL.glClear(GL.GL_COLOR_BUFFER_BIT);

		GL.glLoadIdentity(); 
		// Triangle
		GL.glTranslatef(-2.0f,1.0f,-6.0f);
		GL.glRotatef(rtri,0.0f,1.0f,0.0f);	
		GL.glEnableClientState(GL.GL_VERTEX_ARRAY);
		GL.glEnableClientState(GL.GL_COLOR_ARRAY);
		GL.glColorPointer (4, GL.GL_FLOAT, 0, triVertexColors);
		GL.glVertexPointer(3, GL.GL_FLOAT, 0, triVertices); 
		GL.glDrawArrays(GL.GL_TRIANGLE_STRIP, 0, 3);
		GL.glDisableClientState (GL.GL_COLOR_ARRAY);
		
		// Square
		GL.glLoadIdentity();
		GL.glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
		GL.glTranslatef(2.0f,1.0f,-6.0f);
		GL.glRotatef(rquad,1.0f,0.0f,0.0f);
		GL.glVertexPointer(3, GL.GL_FLOAT, 0, quadVertices);
		GL.glDrawArrays(GL.GL_TRIANGLE_FAN, 0, 4); 
		GL.glDisableClientState(GL.GL_VERTEX_ARRAY);
		
		rtri += 1.5f;
		rquad -= 2.5f;
	}

}

I still think this has quite a lot of potential.

Kev

“Java base simulator in windows”? Did you write this as well? So effectively you can go from Java -> ObjC -> Java?

The simulator is part of the XMLVM project. It’s not java -> objc -> java. It’s simulator rather than an emulator.

When you’re writing the Java code to be compiled to objc you have to code against an API written in Java. This is what XMLVM call the compatibility library. They could have just left that API as exactly that, empty API methods for each of the iPhone SDK functions. However, instead they implemented the methods in terms of Java2D - so you end up with a way to test you Java code before you go to objc.

Kev

Ah, yes. That makes sense. Very impressive stuff btw!

Hi!

It is nice to see that you can write some source code in Java for IPhone even though it has to be converted into Objective-C, nice work, keep it up.

What is wrong with using JOGL? JNA relies on Platform Invoke, doesn’t it? Therefore, DGLES+JNA should be noticeably slower than JOGL 2. Some C# bindings for OpenGL rely on the mechanism of platform invoke and they are twice slower than JOGL, I think about Tao Framework for example.