electronstudio / jaylib Goto Github PK
View Code? Open in Web Editor NEWJava JNI bindings for Raylib
License: Other
Java JNI bindings for Raylib
License: Other
Javacpp creates noarg constructors and expects you use the fluent methods to initialize, e.g.:
new Vector3().x(16).y(8).z(16)
It would be nicer for Java programmers if we could make a constructor so you could do:
new Vector3(16,8,16)
I know how to do this as a helper method, but I don't know how to insert it as a constructor into the generated Raylib.java source.
As we all know, FFI in Java is a mess. From Sun you have JNI which requires you to write C code, you have JNA which doesn't require C but is slower and still needs a generator program (long since abandoned) to avoid writing it by hand. Then there are third party solutions, like JavaCPP which we are currently using. It's the best I have found so far but still not easy to configure.
The latest attempt from Oracle to fix this is Project Panama, which is available now in Early Access and may perhaps be in Java 17 or 18. https://jdk.java.net/panama/
So should we switch to this?
I've tried the EA and it works, but the generator currently doesn't seem to generate anything to automate access to data structures. For example, to print the mouse coordinates you would do this:
MemorySegment vector2 = GetMousePosition(newImplicitScope());
System.out.println(Vector2.x$get(vector2)+" "+Vector2.y$get(vector2));
To clear the screen with a colour:
BeginDrawing();
MemorySegment color = MemorySegment.allocateNative(Color.$LAYOUT(),newImplicitScope());
Color.r$set(color, (byte) 255);
Color.g$set(color, (byte) 255);
Color.b$set(color, (byte) 255);
Color.a$set(color, (byte) 255);
ClearBackground(color);
EndDrawing();
I have not read every page of the docs, so I don't know if they have implemented or are planning to implement automatic generator of helpers to do all this. If you know the answer please comment. But my feeling at the moment is we would have to write so much wrapper code to make an API that is easy to use we may as well use JNI.
Greetings, I'm very interested in this Java binding for "raylib". I would like to know if there are any future plans for adding support to many platforms, especially for Android and HTML5. If not, then at least explain how can I try to do it on my own, please. I realise it's easy to say, but much harder to do.
when i use and compile:
this.teste = new Rectangle(0.0f, 0.0f, 64.0f, 64.0f);
I receive this:
BattleTank.java:54: error: no suitable constructor found for Rectangle(float,float,float,float)
this.teste = new Rectangle(0.0f, 0.0f, 64.0f, 64.0f);
^
constructor Rectangle.Rectangle() is not applicable
(actual and formal argument lists differ in length)
constructor Rectangle.Rectangle(long) is not applicable
(actual and formal argument lists differ in length)
constructor Rectangle.Rectangle(Pointer) is not applicable
(actual and formal argument lists differ in length)
1 error
i didn't build jaylib and i running it in windows 7 x64
RLGL works on Linux. On Windows, it requires raymath.h as well, and then compilation fails due to unexpected brackets around line 1030. Only way to fix this seems to be to delete the code it doesn't like (I can't understand why it doesn't like code) which may or may not break raymath and/or RLGL.
So risk including broken versions, or dont include them on windows, or dont include them on any platform?
This would make it easier for developers to use the binding.
The Javacpp build process is a bit unusual and the docs are not great, so I wrote bash scripts to get it working. Would be preferable to replace them with gradle scripts.
it shows in read me that it includes raygui but i can't import Label, Button how to do it
For some reason, the Raylib.Color class is treated as a resource, and thus needs to be closed.
Why is that?
Shouldn't it be treated by the garbage collector, just like any other class?
Dear @electronstudio , as I said I wrote a wrapper that allows to load Textures and Images from within the jarfile.
Without shipping the resources elsewhere.
Without extracting them somewhere to a temporary location.
Without using libGDX.
All I done (but it took me two days of research) was figuring out how RL loads images, and replacing all C-specific code with Java-specific code so that it does work.
Library: https://codeberg.org/glowiak/librlimg
I am not proposing a pull request, as you would probably f*ck it away as being bad code.
Maybe it is bad code, but it works.
I have finished my wrapper (hope it works), and I want to test it on some image, but all images I have are too big to fit in the window.
Obvious solution would be to modify the .width and .height values of the Texture2D struct, but in this binding these are not variables, but getters (confirmed with a decompiler).
How to access the variables of the structs? I looked into the code and there seem to be no setters.
Convert more of the Raylib examples from C to Java and test they work.
Investigate why Texture works but Texture2D doesn't.
I need compatibility for aarch64 macOS for my project for a school assignment, is there a way that you can add that?
GraalVM can call code in any supported language from any supported language, and one of the 'languages' is LLVM bitcode.
Compiling Raylib to LLVM bitcode works fine. However we then have some problems:
Context polyglot = Context.newBuilder().
allowAllAccess(true).build();
File file = new File("libraylib.so.3.7.0");
Source source = Source.newBuilder("llvm", file).build();
Value cpart = polyglot.eval(source);
cpart.invokeMember("InitWindow", 300, 300, "foo");
You can also do 'execute' rather than 'invokeMember' but the result is the same:
Exception in thread "main" org.graalvm.polyglot.PolyglotException: com.oracle.truffle.api.dsl.UnsupportedSpecializationException: Unexpected values provided for <core.c:664:29>:664 LLVMI8LoadNodeGen#1: [foo], [String]
I'm writing a game engine using Jaylib, and some of the image manipulation functions, such as:
void ImageFlipVertical(Image image);
void ImageFlipHorizontal(Image image);
void ImageRotateCW(Image image);
void ImageRotateCCW(Image image);
void ImageColorTint(Image image, Color color);
void ImageColorInvert(Image image);
void ImageColorGrayscale(Image image);
Don't work. The image resizing functions do work, but these ones don't. I checked the docs, and im using them correctly, so im not exactly sure why it isnt working. This is my code:
public class Sprite {
@Getter
private String path;
@Getter
private Image img;
@Getter
private Texture rawTex;
public Sprite(String path) {
this.path = path;
this.img = LoadImage(path);
this.rawTex = LoadTextureFromImage(img);
}
public void flip() {
ImageFlipVertical(img);
}
public void resize(Vector2f newSize) {
// Use nearest neighbour scaling algorithm instead of the default bicubic scaling algorithm
// https://en.wikipedia.org/wiki/Nearest-neighbor_interpolation
ImageResizeNN(this.img, (int) newSize.x, (int) newSize.y);
// reload the texture after resizing
this.rawTex = LoadTextureFromImage(this.img);
}
public void resizeBicubic(Vector2f newSize) {
// Raylib uses the bicubic resizing algorithm by default
// https://en.wikipedia.org/wiki/Bicubic_interpolation
ImageResize(this.img, (int) newSize.x, (int) newSize.y);
// reload the texture after resizing
this.rawTex = LoadTextureFromImage(this.img);
}
public void dispose() {
UnloadImage(img);
UnloadTexture(rawTex);
}
}
and the sprite.flip() function isn't changing anything in the image.
Publishing on Jitpack is quite easy, but since I'm not using this project for anything yet I haven't done it. JCentral would be better but is perhaps more work.
It's been released a while back.
Javacpp accesses C arrays via pointers, e.g.
model.materials().position(1).maps().position(2)
This is weird for a Java programmer because it isn't just an accessor; it modifies the position field of the object, and is obviously highly unsafe. It would be better if we could convert the C arrays into Java arrays, or at least wrap a List interface around them.
when you use loadTexture or LoadTextureFromImage it crashes the program instantly
error:
# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x0000000000000000, pid=11408, tid=17568
then it prints out a file containing heap and stack info
It seems that the Rectangle() four arguments constructor is not working...
new Jaylib.Rectangle((float)0, (float)0, (float)16, (float)16);
That gives me an error...
Just wondering if there are plans to upgrade this awesome library to the new Raylib 4.0 base?
When testing the Jaylib jar, I realized that the "physac" in not in jar, and this is included in the RayLib 3.0 as its "default" 2D physics engine/component. I think it would quite useful if it (physac.h) can be included in the Jaylib jar.
similar reason for the "raygui.h"
many thanks
In all of the examples of Jaylib I see all the Texture2Ds being loaded from local files.
Is there any way to use this.getClass().getResource() with this?
Because distributing a game in a single jarfile is much more convinient than having to have a resources directory.
I can't get raylib_static.lib to link on Windows. Therefore we have a runtime dependency on raylib.dll being in the library path.
Possible solutions:
Raylib function names conflict with Windows function names. Raylib have declined the request to add a prefix or change their function names: raysan5/raylib#896
As a work around I have hacked the Windows headers to remove the conflicting funtions: https://github.com/electronstudio/jaylib/tree/master/raylib_bug_896_hack
Less hacky solutions welcome!
Im trying to draw some text with a custom font, but it looks like DrawTextEx takes in a vector2 where it should take a font.
There are two ways to do this:
Problem: All the JNI method calls need to be listed in a file META-INF/native-image/jni-config.json
. This file can be generated by the 'native-image-agent' but it's very incomplete. So someone would have to manually add every JNI call which seems a pretty big job.
There's a nice example of this here: https://github.com/praj-foss/opengl-graal-examples
Seems like it should be very fast. But it will only ever work with GraalVM in nativeimage mode which might limit how many people would want to use it. Is there a tool to autogenerate these or are we back to having to do it manually?
As jaylib is almost same as raylib in C,I managed to fix HighDPI causes text blurring issue in raylib by the line that raysan5 mentioned in this issue
raysan5/raylib#1111 (comment)
SetTextureFilter(GetFontDefault().texture, FILTER_POINT);
I think it should works as same as in raylib C but it gives error in the following image
Here is the full file source code,Called Game.java
import com.raylib.Raylib;
import static com.raylib.Jaylib.*;
public class Game {
public static void main(String[] args) {
SetConfigFlags(FLAG_VSYNC_HINT);
SetConfigFlags(FLAG_MSAA_4X_HINT);
InitWindow(800, 600, "Game");
SetTargetFPS(60);
SetTextureFilter(GetFontDefault().texture, FILTER_POINT);
while (!WindowShouldClose()) {
BeginDrawing();
ClearBackground(WHITE);
DrawRectangleLines(100, 100, 100, 50, BLUE);
DrawCircleLines(200, 100, 25, RED);
DrawLine(300, 100, 600, 100, BLACK);
DrawTriangleLines(new Vector2(500, 300), new Vector2(600, 400), new Vector2(500, 200), BROWN);
DrawPolyLines(new Vector2(500, 600), 6, 50, 0, GOLD);
DrawEllipseLines(500, 400, 100, 50, PURPLE);
EndDrawing();
}
Raylib.CloseWindow();
}
}
I don't know until now what causes this,Thanks for helping me if you can!
System Information:
M2 Macbook Air
Sonoma 14.3.1
Trying to compile my project which depends on Jaylib results in the following error:
Exception in thread "main" java.lang.UnsatisfiedLinkError: Platform "macosx-arm64" not supported by class com.raylib.Raylib
at org.bytedeco.javacpp.Loader.load(Loader.java:1184)
at org.bytedeco.javacpp.Loader.load(Loader.java:1157)
at org.bytedeco.javacpp.Loader.load(Loader.java:1133)
at com.raylib.Raylib.<clinit>(Raylib.java:10)
at dev.yeff.orbital.graphics.Window.start(Window.java:39)
at dev.yeff.orbital.Game.start(Game.java:45)
at dev.yeff.Main.main(Main.java:21)
Does Jaylib not support MacOS, or is there something I need to add myself, like an extra dependency, etc.?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.