News for hackers. Stuff that matters.
Re:How to Use the Foreign Function API in Java 22 to Call C ... by xyproto on Wednesday May 08, 2024 @02:17PM (#40301315)
Does this mean that one can use SDL2 together with Java without bending over backwards?
Re:How to Use the Foreign Function API in Java 22 to Call C ... by maksut on Wednesday May 08, 2024 @03:21PM (#40301898)
I have played with raylib bindings for clojure by using the new foreign function api. It was a lot of fun. SDL might be a better fit because it prefers pass by reference arguments [0].

[0] https://gist.github.com/raysan5/17392498d40e2cb281f5d09c0a4b...

Re:How to Use the Foreign Function API in Java 22 to Call C ... by marginalia_nu on Wednesday May 08, 2024 @02:54PM (#40301649)
Oh man that's a cool idea.

Might just build a SDL2-wrapper for ffi just as an FFI and FMI-exercise.

Re:How to Use the Foreign Function API in Java 22 to Call C ... by neonsunset on Wednesday May 08, 2024 @02:40PM (#40301515)
It seems it will make it somewhat easier.

But if you want to use SDL2 from something higher-level, you will be much better served by C# which will give you minimal FFI cost and most data structures you want to express in C as-is.

Re:How to Use the Foreign Function API in Java 22 to Call C ... by maksut on Wednesday May 08, 2024 @03:48PM (#40302135)
I don't know much about C#. It certainly looks more popular in gamedev circles.

When I played with this new java api. I wasn't worried about the FFI cost. It seemed fast enough to me. My toy application was performing about 0.77x of pure C equivalent. I think Java's memory model and heavy heap use might hurt more. Hopefully Java will catch up when it gets value objects with Project Valhalla. Next decade or so :)

Re:How to Use the Foreign Function API in Java 22 to Call C ... by neonsunset on Wednesday May 08, 2024 @04:04PM (#40302273)
Genuine curiosity - what would be your motivation to use Java over C# here aside from familiarity (which is perfectly understandable)? The latter takes heavy focus on making sure to provide features like structs and pointers with little to no friction, you can even AOT compile it and statically link SDL2 into a single executable.

In improbable case you may want to try it out, then all it needs is

- SDK from https://dot.net/download (or package manager of your choice if you are on Linux e.g. `sudo apt-get install dotnet-sdk-8.0`, !do not! use Homebrew if you are on macOS however, use .pkg installer)

- C# extension for VS Code (DevKit is not needed)

- SDL2 abstraction: https://github.com/dotnet/Silk.NET (there are all sorts of alternate bindings depending on your preferences)

Re:How to Use the Foreign Function API in Java 22 to Call C ... by lazide on Wednesday May 08, 2024 @06:30PM (#40303388)
Not the original poster, but most folks have little choice what ecosystem they’re using.

And once you have enough momentum, switching isn’t usually worth it.

(As someone who has done Perl, C, Java, C#, Kotlin, JS, and Python professionally - god help me. Maybe a million lines of code all in now?)

Re:How to Use the Foreign Function API in Java 22 to Call C ... by neonsunset on Thursday May 09, 2024 @01:52AM (#40305648)
Fair enough
Re:How to Use the Foreign Function API in Java 22 to Call C ... by spullara on Wednesday May 08, 2024 @09:50PM (#40304509)
Java now has <1ms max pause time garbage collectors with TB heaps. If you are writing a game GC matters a lot.
Re:How to Use the Foreign Function API in Java 22 to Call C ... by neonsunset on Thursday May 09, 2024 @01:46AM (#40305617)
Interesting! What games did you write code for?
Re:How to Use the Foreign Function API in Java 22 to Call C ... by stoperaticless on Thursday May 09, 2024 @01:21AM (#40305510)
Not the op, but at some point I did choose between the two paths/jobs assuming I will get more proficient in only one of them each year (which is true, I stayed junior in C#).

Why I chose Java boils down to two reasons:

- runs on linux (I know there is some version of c# that eventually opened up, but I kind of expect it to have lot of conditions for being cross platform, I assume that standard c# code is not crossplatform due to some reason (e.g. Com usage might be standard way of doing stuff), which would make finding crossplatform answers tedious)

- whole ecosystem is more open source and more involved parties (which I interpreted as abit less controlled by the corporate overlord, so if corporate overlord went rogue, greater chance that language would survive somehow)

Never needed to call into C though..

Re:How to Use the Foreign Function API in Java 22 to Call C ... by 5e92cb50239222b on Thursday May 09, 2024 @03:49AM (#40306173)
Despite what some fanatics may claim, operating systems other than Windows are still second class citizens (saying this after five years of doing .NET development almost exclusively on Linux), especially for dev, and operating systems other than the big three are not supported at all. So no BSDs (even FreeBSD) or Solaris if you ever need it.

Since the open .NET is pretty young, and they still have trouble with community perception due to their past actions, finding high quality FOSS libraries may pose a problem depending on what you're doing. Pretty much everything from MS is open and high quality, but they don't provide everything under the sun.

And with Java you always have alternative runtimes in case this Oracle deal goes sideways for any reason.

So you're all good, don't worry about it.

Re:How to Use the Foreign Function API in Java 22 to Call C ... by neonsunset on Thursday May 09, 2024 @04:50AM (#40306383)
FreeBSD: `pkg install lang/dotnet` (from https://www.freshports.org/lang/dotnet)

GObject (GTK4 and similar): https://github.com/gircore/gir.core (significantly better and faster than Java alternatives, this is just one example among many)

Young: first OSS version was released 8 years ago

Solaris: might as well say "it runs COBOL but not .NET"

It's funny that everyone missed the initial context of the question and jumped onto parroting the same arguments as years ago, without actually addressing the matter at hand or saying anything of substance. Unsurprising show of ignorance by Java community. Please stay this way - will help the industry move on faster.

The premise is always the same - if something is missing in {technology I don't like}, it's a deal-breaker, and when it's not or was always there - it never mattered, or is harmful actually, that is, until {technology I like} gets it as well.

Re:How to Use the Foreign Function API in Java 22 to Call C ... by pooya72 on Thursday May 09, 2024 @06:41AM (#40306852)
Interesting, in what ways is it a second class citizen? I tried googling it, but didn't find much.
Re:How to Use the Foreign Function API in Java 22 to Call C ... by neonsunset on Thursday May 09, 2024 @01:45AM (#40305616)
Neither point is true today FWIW.

Neither point was ever true in the last ~10 years when it comes to gamedev (or where you want to use SDL) where Java was and continues to be a much weaker choice.

Re:How to Use the Foreign Function API in Java 22 to Call C ... by kaba0 on Thursday May 09, 2024 @02:38AM (#40305850)
Java’s ecosystem is just vastly bigger. In many categories, Java has multiple open-source offerings vs .NET’s single, proprietary one that is often just a bad copy of one of the former libraries.
Re:How to Use the Foreign Function API in Java 22 to Call C ... by maksut on Thursday May 09, 2024 @02:42PM (#40311516)
It was a learning exercise. Just playing around with clojure, raylib and this new api. I know all these can also be done with C# with some pros & cons.

I wasn't advocating java for gamedev. Just pointing that, this new api is a nice addition. And I am glad that jvm ecosystem is improving.

To be fair, if I was starting a game project I wouldn't stay in Java/C# level. Depending on the project, something like C, C++, zig might be more practical. Ironically I believe they would be easier for iterating ideas and deploy into different platforms (mobile, wasm etc.).

Re:How to Use the Foreign Function API in Java 22 to Call C ... by neonsunset on Thursday May 09, 2024 @03:17PM (#40311926)
A little bit sad* but understandable, thank you.

*C/C++ tooling and verbosity pains, what PL dev progress is for? C# feels more modern than some of the "modern" alternatives but eh.

Re:How to Use the Foreign Function API in Java 22 to Call C ... by p0w3n3d on Wednesday May 08, 2024 @05:50PM (#40303043)
You're are (or were) right. Java has (had) an awful performance of a foreign API call, and I wonder was this fixed in this release, because as I heard, fixing it was the main reason of the upcoming functionality
Re:How to Use the Foreign Function API in Java 22 to Call C ... by neonsunset on Wednesday May 08, 2024 @06:03PM (#40303172)
It could bring Java closer in FFI overhead but not necessarily match. There are still missing features like structs, C pointers (though in C# they are superseded quite a bit by byrefs aka `ref T` syntax, e.g. used by Span<T>), stack allocated buffers, etc.

C# also has function pointers (managed/unmanaged) and C exports with NativeAOT.