https://github.com/thepotatoking55/cglfw3
Swift bindings for GLFW.
https://github.com/thepotatoking55/cglfw3
glfw glfw-bindings glfw3 macos opengl swift
Last synced: 5 months ago
JSON representation
Swift bindings for GLFW.
- Host: GitHub
- URL: https://github.com/thepotatoking55/cglfw3
- Owner: ThePotatoKing55
- Created: 2021-04-28T19:19:26.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2024-06-02T17:24:16.000Z (about 2 years ago)
- Last Synced: 2026-02-16T22:42:47.929Z (5 months ago)
- Topics: glfw, glfw-bindings, glfw3, macos, opengl, swift
- Language: Swift
- Homepage:
- Size: 1.07 MB
- Stars: 8
- Watchers: 1
- Forks: 10
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# CGLFW3
Builds [GLFW](https://www.glfw.org) as a library to add to your Swift Package. As of writing, it's currently updated to the [latest commit](https://github.com/glfw/glfw/commit/955fbd9d265fa95adf9cb94896eb9a516aa50420) for GLFW 3.4.
This package can work on its own, but it was created as a base for [SwiftGLFW](https://github.com/thepotatoking55/SwiftGLFW).
## Getting Started
SwiftPM doesn't support unsafe flags with semantic versioned packages, so add this to your dependecies in `Package.swift`:
```swift
.package(url: "https://github.com/thepotatoking55/CGLFW3.git", branch: "main")
```
From there, you can just import it with `import CGLFW3` and use it like normal.
## Cross-Platform Support
To expose platform-native functions such as `glfwGetCocoaWindow`, add the following C settings to your target:
```swift
.target(
name: "ExampleTarget",
dependencies: ["CGLFW3"],
cSettings: [
.define("GLFW_EXPOSE_NATIVE_WIN32", .when(platforms: [.windows])),
.define("GLFW_EXPOSE_NATIVE_WGL", .when(platforms: [.windows])),
.define("GLFW_EXPOSE_NATIVE_COCOA", .when(platforms: [.macOS])),
.define("GLFW_EXPOSE_NATIVE_NSGL", .when(platforms: [.macOS])),
.define("GLFW_EXPOSE_NATIVE_X11", .when(platforms: [.linux]))
]
)
```
I don't have a computer running Linux and Windows support for SwiftPM is rudimentary, so this will probably still take some work to get ported to non-Mac platforms.
## Hello World
A Swift translation of the "hello world" program in [GLFW's documentation](https://www.glfw.org/documentation):
```swift
import CGLFW3
func main() {
glfwInit()
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4)
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1)
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE)
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE)
guard let window = glfwCreateWindow(800, 600, "Hello World", nil, nil) else {
let error = glfwGetError(nil)
print(error)
return
}
glfwMakeContextCurrent(window)
while glfwWindowShouldClose(window) == GLFW_FALSE {
glfwSwapBuffers(window)
glfwPollEvents()
}
glfwTerminate()
}
```