stregasgate / raylib Goto Github PK
View Code? Open in Web Editor NEWA Swift package for Raylib. Builds Raylib from source so no need to fiddle with libraries. Just add as a dependency in you game package and go!
License: MIT License
A Swift package for Raylib. Builds Raylib from source so no need to fiddle with libraries. Just add as a dependency in you game package and go!
License: MIT License
I tried to follow the README, but it didn't make sense. Steps 1 and 2 were fine (and I could build the project with the dependency, without any code of my own), but Step 3 was completely confusing:
- Add the following to the
static func main()
inMyGame.swift
.
The swift package init --type executable
command does not create a file named MyGame.swift
. There's only main.swift
(inside ~/Projects/MyGame/Sources
). And, I cannot add the provided code "to the static func main()
", as that construct doesn't exist in Swift. A static func
must be a method.
Note: The main.swift
file only contains print("hello world")
by default. There's no class or struct or anything to add to.
It looks like a really fun project. I just couldn't follow the tutorial. :(
This library is great, thanks!
Am I correct that the physics library isn't included in this wrap?
I'm using the 'STREGAsGate/Raylib' library, which provides a method called loadFontEx(...) to load font files into the VRAM. However, it seems that the current implementation of this method is using .utf8 to convert characters to code points, which results in incorrect behavior. Instead, the method should be using .unicodeScalars for accurate code point conversion.
/// Load font from file with extended parameters
@inlinable
static func loadFontEx(_ fileName: String, _ fontSize: Int32, _ fontChars: [Character]? = nil) -> Font {
return fileName.withCString { cString in
if fontChars == nil {
return RaylibC.LoadFontEx(cString, fontSize, nil, 0)
} else {
var chars: [Int32] = fontChars!.compactMap({$0.utf8.first}).map({Int32($0)})
return chars.withUnsafeMutableBufferPointer { bufferPointer in
return RaylibC.LoadFontEx(cString, fontSize, bufferPointer.baseAddress, Int32(bufferPointer.count))
}
}
}
}
To fix this bug, I suggest modifying the implementation to use .unicodeScalars for character to code point conversion. Here is the updated implementation:
/// Load font from file with extended parameters (fixed bug in character conversion)
@inlinable
static func loadFontEx(_ fileName: String, _ fontSize: Int32, _ fontChars: [Character]? = nil) -> Font {
return fileName.withCString { cString in
if fontChars == nil {
return RaylibC.LoadFontEx(cString, fontSize, nil, 0)
} else {
var chars: [Int32] = fontChars!.map({ Int32($0.unicodeScalars.first?.value ?? 0 )})
return chars.withUnsafeMutableBufferPointer { bufferPointer in
return RaylibC.LoadFontEx(cString, fontSize, bufferPointer.baseAddress, Int32(bufferPointer.count))
}
}
}
}
By making this change, the font loading method should now handle character to code point conversion correctly, resolving the issue with incorrect font loading.
For further information/reproducing take a look into this discord conversation on the Raylib server. If you have any questions or need further assistance, feel free to reach out to me. I'd be happy to help!
Hello! Thank you very much for creating this Swift package to streamline working with Raylib.
While testing out your library, I tried using from: "4.0.0"
for the package and encountered this error:
error: the target '_RaylibC' in product 'Raylib' contains unsafe build flags
Then I discovered this thread regarding the use of .unsafeFlags()
and .branch()
/ from
- https://forums.swift.org/t/question-about-contains-unsafe-build-flags-in-spm/47677
I understand that since your library needs to use .unsafeFlags()
to compile Raylib and GLFW together via clang -x objective-c -fno-obj-arc
, could you please document that users of this library must use .branch("master")
?
The shader stuff appears to be wrapped but it silently halts the program for me when I try to load one:
let rscFS = "/home/arckex/Raylib/Sources/Example/resources/shaders/glsl330/bloom.fs"
let rscFSTXT = try String(contentsOfFile: rscFS)
let rscVS = "/home/arckex/Raylib/Sources/Example/resources/shaders/glsl330/base.vs"
let rscVSTXT = try String(contentsOfFile: rscVS)
let shader = Raylib.loadShaderFromMemory(rscVSTXT, rscFSTXT)
Can you make a min example of loading a shader? Thanks so much
When using async/await, The program quits. I have tried only using Raylib commands on the Main Actor and main thread. Still crashes if there is an await.
I got it working with the following work around; however, this ties up the main thread with a semaphore.
@MainActor static func main() {
Raylib.initWindow(800, 450, "test")
Raylib.setTargetFPS(60)
while Raylib.windowShouldClose == false {
let semaphore = DispatchSemaphore(value: 0)
DispatchQueue.global().async {
Task {
await update()
semaphore.signal()
}
}
semaphore.wait()
draw()
}
Raylib.closeWindow()
}
I was able to get this running in xcode 13.1, but I had to modify the package file slightly.
I had to modify the package and target thusly:
`
.package(name: "Raylib", url: "https://github.com/STREGAsGate/Raylib.git", .branch("master"))
...
.target(
name: "",
dependencies: ["Raylib"]),
`
without this, I would receive the error "No such module 'Raylib'" when I tried to import "Raylib" in my swift files. Not sure if it's an update with newer versions of Swift or not.
This is a fantastic project. Thanks for putting this work together.
Description
Since the .getRandomValue(min: , max:)
method seems to be deprecated, I tried using Int32.random(in:)
/Int.random(in:)
as stated in the deprecation message. However, these methods do not seem to be affected by the .setRandomSeed()
method. I even attempted to set the seed via srand()
and srandom()
, but this did not work either.
Steps to Reproduce
.setRandomSeed()
once (I did it in the beginning immediately after window initialization).Int32.random(in:)
.Current Behavior
The generated values are not identical, nor are their sequences anywhere near identical. When using .getRandomValue(min: , max:)
, everything works fine.
Relevant Screenshots/Logs
-
Environment
Operating System: Ubuntu 20.04.6 LTS
Project Version: master
Swift Version: 5.6.2
Target: x86_64-unknown-linux-gnu
Troubleshooting Steps Taken
I tried only setting the seed once at the very beginning. I used a hardcoded seed of '123'. I tried setting the seed via .setRandomSeed()
as well as srand()
and srandom()
. For getting a pseudo-random value, I tried Int.random(in:)
, Int(random())
, Int32.random(in:)
, as well as UInt32.random(in:)
. After getting a value from the PRNG, I restarted the application and did the exact same thing as before, expecting to get the exact same value. But the value I received was always different. I also tried replicating this in a playground environment, getting different values/sequences there as well.
Possible Solutions
Maybe the problem is with my usage, but I'm uncertain.
Additional Information
You can use .getRandomValue(min: , max:)
as a workaround, regardless of its deprecation.
Hello!
It's most likely I'm doing something wrong...
Let's say I'm trying to load image:
public var fish1 = Raylib.loadImage(Bundle.module.path(forResource: "fish1", ofType: "png", inDirectory: "Resources") ?? "")
And it's loaded successfully (output from terminal):
File loaded successfully
INFO: IMAGE: Data loaded successfully (256x256 | R8G8B8A8 | 1 mipmaps)
But then I'm trying to load texture:
public var fishTexture1 = Raylib.loadTextureFromImage(fish1)
And it bring back to _Sources/RaylibC/UnmodifiedRaylibCSrc/rlgl and rlLoadTexture (line number 2688) with EXC_BAD_ACCESS
Thanks in advance for your help!
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.