jbang-examples icon indicating copy to clipboard operation
jbang-examples copied to clipboard

feat: game of Pong with JBang, Java and LWJGL

Open wfouche opened this issue 3 months ago • 13 comments

$ jbang run Pong.java

image

wfouche avatar Aug 17 '25 16:08 wfouche

love it - did you make the game or copied from somewhere? should make ref if from somewhere else.

we'll need to figure out how to do the deps in a portable way.

maxandersen avatar Aug 18 '25 10:08 maxandersen

This game was developed from scratch using Google Gemini AI.

I tested the game on Linux and Windows, had to a make a small timing change so that Linux was properly supported.

wfouche avatar Aug 19 '25 09:08 wfouche

we'll need to figure out how to do the deps in a portable way.

It should work now on macOS (arm64) as well.

wfouche avatar Aug 19 '25 13:08 wfouche

i gets further but errors with:

jbang examples/game/Pong.java 
[jbang] Resolving dependencies...
[jbang]    org.lwjgl:lwjgl:3.3.6
[jbang]    org.lwjgl:lwjgl-glfw:3.3.6
[jbang]    org.lwjgl:lwjgl-opengl:3.3.6
[jbang] Dependencies resolved
[jbang] Building jar for Pong.java...
Exception in thread "main" java.lang.IllegalStateException: GLFW may only be used on the main thread and that thread must be the first thread in the process. Please run the JVM with -XstartOnFirstThread. This check may be disabled with Configuration.GLFW_CHECK_THREAD0.
	at org.lwjgl.glfw.EventLoop.check(EventLoop.java:33)
	at org.lwjgl.glfw.GLFW.glfwInit(GLFW.java:1108)
	at Pong.init(Pong.java:100)
	at Pong.run(Pong.java:82)
	at Pong.main(Pong.java:298) 

i can make it work using this:

jbang -R=-XstartOnFirstThread examples/game/Pong.java

not sure if adding //RUNTIME_OPTIONS -XstartOnFirstThread` to it will have bad impact on windows/linux?

maxandersen avatar Aug 19 '25 14:08 maxandersen

btw. i'm surprised the fix worked ...trying to grok if bug or feature :)

maxandersen avatar Aug 19 '25 14:08 maxandersen

remarkably fatjar of that is "only 3.7mb...

maxandersen avatar Aug 19 '25 14:08 maxandersen

I've disabled the main thread check using runtime option -Dorg.lwjgl.glfw.checkThread0=false.

@maxandersen , please check if this resolves the macOS issue. -XstartOnFirstThread does not work on Windows, so cannot be used.

wfouche avatar Aug 19 '25 16:08 wfouche

I no longer get an error but now it just hangs/doing nothing.

jbang -R=-XstartOnFirstThread examples/game/Pong.java still works

what error are you getting on windows?

maxandersen avatar Aug 19 '25 16:08 maxandersen

There currently is no way to get this to work without explicitly specifying the -XstartOnFirstThread option when running the program on macOS. I think we need a way of specifying OS specific runtime options that will only be used if a particular OS is detected. For example

//RUNTIME_OPTIONS  -Xmx2g
//RUNTIME_OPTIONS:MACOS -XstartOnFirstThread

will set the heap size to 2GB for all platforms and only on MacOS also add option -XstartOnFirstThread

One could extend this to also allow specifying an optional ARCH_TYPE as well, but it I'm not quite sure if that's really needed.

  • //RUNTIME_OPTIONS:MACOS:ARM64

Comments?

wfouche avatar Aug 19 '25 19:08 wfouche

See https://github.com/jbangdev/jbang/issues/2186

Similar issue for //DEPS we might want to consider {key=value} being applicable to directives ... At least to things like runtime options.

maxandersen avatar Aug 20 '25 05:08 maxandersen

Moved the JBang directives to file Config.java.

//usr/bin/env jbang "$0" "$@" ; exit $?

// Game developed using Google Gemini AI (2.5 Pro)

//SOURCES Config.java

import org.lwjgl.glfw.*;
import org.lwjgl.opengl.*;
import org.lwjgl.system.*;

...

wfouche avatar Aug 25 '25 09:08 wfouche

Why? You plan to share that Config.java between multiple games in this repo or how?

maxandersen avatar Aug 25 '25 11:08 maxandersen

Why? You plan to share that Config.java between multiple games in this repo or how?

It was an experiment. Reverted the change again.

wfouche avatar Aug 26 '25 03:08 wfouche