jbang-examples
jbang-examples copied to clipboard
feat: game of Pong with JBang, Java and LWJGL
$ jbang run Pong.java
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.
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.
we'll need to figure out how to do the deps in a portable way.
It should work now on macOS (arm64) as well.
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?
btw. i'm surprised the fix worked ...trying to grok if bug or feature :)
remarkably fatjar of that is "only 3.7mb...
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.
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?
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?
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.
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.*;
...
Why? You plan to share that Config.java between multiple games in this repo or how?
Why? You plan to share that Config.java between multiple games in this repo or how?
It was an experiment. Reverted the change again.