Giter Club home page Giter Club logo

cglfw3's Introduction

CGLFW3

Builds GLFW as a library to add to your Swift Package. As of writing, it's currently updated to the latest commit for GLFW 3.4.

This package can work on its own, but it was created as a base for SwiftGLFW.

Getting Started

SwiftPM doesn't support unsafe flags with semantic versioned packages, so add this to your dependecies in Package.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:

.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:

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
    }

    glfwMakeCurrentContext(window)
    while glfwWindowShouldClose(window) == GLFW_FALSE {
        glfwSwapBuffers(window)
        glfwPollEvents()
    }
    
    glfwTerminate()
}

cglfw3's People

Contributors

thepotatoking55 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.