Skip to content

Readme example code is out of date #6

@koliyo

Description

@koliyo

Here is updated code that actually compiles against main:

  • MainActor
  • Renamed error handler
  • Case changes in hint fields
  • Explicit cast to GLbitfield
  • Remove missing underlyingError
import GLFW
import OpenGL // Or whatever other library you use


@MainActor
func main() {
    do {
        try GLFWSession.initialize()
        GLFWSession.onReceiveError = { error in
          print("GLFW error: \(error)")
        }

        /* macOS's OpenGL implementation requires some extra tweaking */
        GLFWWindow.hints.contextVersion = (4, 1)
        GLFWWindow.hints.openGLProfile = .core
        GLFWWindow.hints.openGLCompatibility = .forward

        /* Create a windowed mode window and its OpenGL context */
        let window = try GLFWWindow(width: 640, height: 480, title: "Hello World")

        /* Make the window's context current */
        window.context.makeCurrent()

        /* Loop until the user closes the window */
        while !window.shouldClose {
            /* Render here */
            glClear(GLbitfield(GL_COLOR_BUFFER_BIT))
            someRenderFunctionDefinedElsewhere()

            /* Swap front and back buffers */
            window.swapBuffers()

            /* Poll for and process events */
            GLFWSession.pollInputEvents()
        }
    } catch let error as GLFWError {
        print(error.description ?? "Unknown error")
        // print(error.underlyingError.description)
    } catch {
        print(error)
    }
}

Also, to avoid deprecation warnings on glClear, you should add the following to the Package.swift target:

            cSettings: [
                .define("GL_SILENCE_DEPRECATION",
                    .when(platforms: [.macOS])),
                .define("GLES_SILENCE_DEPRECATION",
                    .when(platforms: [.iOS, .tvOS])),
            ]

Also the package dependency import has the wrong name:

                // This does not work:
                // .product(name: "GLFW", package: "SwiftGLFW"),
                // This works:
                .product(name: "SwiftGLFW", package: "SwiftGLFW"),

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions