Swiftpack.co -  ThePotatoKing55/SwiftGLFW as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
ThePotatoKing55/SwiftGLFW
A Swift library that makes GLFW a bit more manageable.
.package(url: "https://github.com/ThePotatoKing55/SwiftGLFW.git", from: "3.3.5")

SwiftGLFW

A Swift library that makes GLFW a bit more manageable. It's a bit of a deviation from the original C library, as not only does it clear away non-Swifty things like pointers and properly wrap callback closures, but it also has a much more object-oriented architecture.

Installation

Installing is pretty standard for the Swift Package Manager. Add this to your dependencies:

.package(url: "https://github.com/thepotatoking55/SwiftGLFW.git", .upToNextMajor(from: "3.3.4"))

How to Use

Basic Setup

Setup is pretty painless, requiring just a few lines of code:

import SwiftGLFW
import glfw3 // Not strictly necessary, but nicely imports OpenGL

...

func main() {
    GLSession.initialize()
    
    GLWindow.hints.add(.openGLVersion(.v4_6))
    GLWindow.hints.add(.openGLProfile(.core))
    GLWindow.hints.add(.openGLCompatibility(.forward))
    
    let window = GLWindow.create(size: GLSize(width: 860, height: 480), title: "SwiftGLFW")
    window.context.makeCurrent()
    
    window.keyInputHandler = { key, scancode, state, modifiers in
        if key == .escape && state == .pressed {
            window.close()
        }
    }
    
    glClearColor(0.2, 0.3, 0.3, 1.0) // OpenGL function imported through glfw3
    
    while !window.shouldClose {
        GLSession.pollInputEvents()
        someRenderFunctionDeclaredEarlier()
        window.swapBuffers()
    }
    
    GLSession.terminate()
}

Error Handling

While there are no direct errors thrown, you may come across some weirdness (namely, windows coming up nil) when something goes wrong. To check on the latest error from GLFW, call this:

let error = GLSession.getError()

Or, you can assign an error handler to catch them as soon as they come up:

GLSession.errorHandler = { error in
    /* do something with it here */
}

Attributes

Attributes are no longer accessed or modified through functions like glfwGetWindowAttrib()/glfwSetWindowAttrib(). Instead, the library handles all of the C functions and constants to create Swifty objects. For example:

let window = GLWindow.create(...)
window.resizable = true
window.maxmize()

window.mouse.useRawMotionInput = true
window.mouse.cursorMode = .disabled
window.scrollInputHandler = { offset in ... }

let monitor = GLMonitor.primary
monitor.setGamma(2.2)

Gamepad Support

GLFW supports gamepads, so this does too!

let movementSpeed = GLGamepad[0]?.input.thumbstick.left.y ?? 0.0

GitHub

link
Stars: 0
Last commit: 4 days ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Dependencies

Release Notes

1 week ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API