Convert Figma logo to code with AI

gen2brain logoraylib-go

Go bindings for raylib, a simple and easy-to-use library to enjoy videogames programming.

1,540
156
1,540
3

Top Related Projects

21,434

A simple and easy-to-use library to enjoy videogames programming

3,722

Multi-Language Vulkan/GL/GLES/EGL/GLX/WGL Loader-Generator based on the official specs.

12,827

A multi-platform library for OpenGL, OpenGL ES, Vulkan, window and input

59,344

Dear ImGui: Bloat-free Graphical User interface for C++ with minimal dependencies

11,581

Hazel Engine

6,830

minimal cross-platform standalone C headers

Quick Overview

Raylib-go is a Go binding for raylib, a simple and easy-to-use library for video game programming. It provides a comprehensive set of functions for creating 2D and 3D games, with a focus on simplicity and performance. The project aims to make game development accessible to Go programmers while leveraging the power of raylib.

Pros

  • Easy to use and learn, especially for developers familiar with Go
  • Provides access to a wide range of raylib features, including graphics, audio, and input handling
  • Cross-platform support, allowing development for multiple operating systems
  • Active development and community support

Cons

  • May have performance overhead compared to using raylib directly in C
  • Documentation could be more comprehensive, especially for Go-specific features
  • Dependency on the underlying raylib library, which may require separate installation
  • Limited compared to more feature-rich game engines for complex projects

Code Examples

  1. Creating a window and drawing a circle:
package main

import (
    "github.com/gen2brain/raylib-go/raylib"
)

func main() {
    rl.InitWindow(800, 450, "raylib [core] example - basic window")
    defer rl.CloseWindow()

    for !rl.WindowShouldClose() {
        rl.BeginDrawing()
        rl.ClearBackground(rl.RayWhite)
        rl.DrawCircle(400, 225, 100, rl.Maroon)
        rl.EndDrawing()
    }
}
  1. Playing a sound:
package main

import (
    "github.com/gen2brain/raylib-go/raylib"
)

func main() {
    rl.InitWindow(800, 450, "raylib [audio] example - sound playing")
    defer rl.CloseWindow()

    rl.InitAudioDevice()
    defer rl.CloseAudioDevice()

    sound := rl.LoadSound("resources/sound.wav")
    defer rl.UnloadSound(sound)

    for !rl.WindowShouldClose() {
        if rl.IsKeyPressed(rl.KeySpace) {
            rl.PlaySound(sound)
        }

        rl.BeginDrawing()
        rl.ClearBackground(rl.RayWhite)
        rl.DrawText("Press SPACE to play sound!", 200, 180, 20, rl.LightGray)
        rl.EndDrawing()
    }
}
  1. Creating a 3D cube:
package main

import (
    "github.com/gen2brain/raylib-go/raylib"
)

func main() {
    rl.InitWindow(800, 450, "raylib [3d] example - 3d cube")
    defer rl.CloseWindow()

    camera := rl.Camera3D{}
    camera.Position = rl.Vector3{X: 10.0, Y: 10.0, Z: 10.0}
    camera.Target = rl.Vector3{X: 0.0, Y: 0.0, Z: 0.0}
    camera.Up = rl.Vector3{X: 0.0, Y: 1.0, Z: 0.0}
    camera.Fovy = 45.0
    camera.Projection = rl.CameraPerspective

    rl.SetCameraMode(camera, rl.CameraFree)

    for !rl.WindowShouldClose() {
        rl.UpdateCamera(&camera)

        rl.BeginDrawing()
        rl.ClearBackground(rl.RayWhite)
        rl.BeginMode3D(camera)
        rl.DrawCube(rl.Vector3{X: 0, Y: 0, Z: 0}, 2.0, 2.0, 2.0, rl.Red)
        rl.DrawCubeWires(rl.Vector3{X: 0, Y: 0, Z: 0}, 2.

Competitor Comparisons

21,434

A simple and easy-to-use library to enjoy videogames programming

Pros of raylib

  • Written in C, offering better performance and lower-level control
  • More comprehensive and mature library with a wider range of features
  • Larger community and ecosystem, with more examples and resources available

Cons of raylib

  • Requires C knowledge, which may be less accessible for some developers
  • Potentially more complex setup and compilation process
  • Less idiomatic for Go developers who prefer working with native Go code

Code Comparison

raylib (C):

#include "raylib.h"

int main(void)
{
    InitWindow(800, 450, "raylib [core] example - basic window");
    while (!WindowShouldClose())
    {
        BeginDrawing();
            ClearBackground(RAYWHITE);
            DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY);
        EndDrawing();
    }
    CloseWindow();
    return 0;
}

raylib-go (Go):

package main

import rl "github.com/gen2brain/raylib-go/raylib"

func main() {
    rl.InitWindow(800, 450, "raylib [core] example - basic window")
    for !rl.WindowShouldClose() {
        rl.BeginDrawing()
        rl.ClearBackground(rl.RayWhite)
        rl.DrawText("Congrats! You created your first window!", 190, 200, 20, rl.LightGray)
        rl.EndDrawing()
    }
    rl.CloseWindow()
}
3,722

Multi-Language Vulkan/GL/GLES/EGL/GLX/WGL Loader-Generator based on the official specs.

Pros of glad

  • Focused on OpenGL loading, providing a lightweight solution for graphics programming
  • Supports multiple languages (C, C++, D, Nim, Pascal) for broader compatibility
  • Allows fine-grained control over OpenGL function loading

Cons of glad

  • Limited to OpenGL functionality, lacking the comprehensive features of a full game framework
  • Requires more setup and boilerplate code for basic rendering tasks
  • May need additional libraries for window management, input handling, and audio

Code Comparison

glad (OpenGL function loading):

if (!gladLoadGL()) {
    printf("Failed to initialize GLAD\n");
    return -1;
}

raylib-go (Window creation and rendering):

rl.InitWindow(800, 450, "raylib [core] example - basic window")
defer rl.CloseWindow()
for !rl.WindowShouldClose() {
    rl.BeginDrawing()
    rl.ClearBackground(rl.RayWhite)
    rl.EndDrawing()
}

Summary

glad is a specialized OpenGL loader library, while raylib-go is a comprehensive game development framework. glad offers more flexibility for low-level graphics programming across multiple languages, but requires additional setup. raylib-go provides a higher-level abstraction with built-in window management, input handling, and rendering utilities, making it easier to get started with game development in Go.

12,827

A multi-platform library for OpenGL, OpenGL ES, Vulkan, window and input

Pros of GLFW

  • More mature and widely adopted in the industry
  • Offers greater flexibility and lower-level control
  • Extensive documentation and community support

Cons of GLFW

  • Steeper learning curve for beginners
  • Requires more boilerplate code for basic functionality
  • Limited to window creation and input handling

Code Comparison

GLFW (C):

#include <GLFW/glfw3.h>

int main(void) {
    GLFWwindow* window = glfwCreateWindow(640, 480, "GLFW Window", NULL, NULL);
    while (!glfwWindowShouldClose(window)) {
        glfwSwapBuffers(window);
        glfwPollEvents();
    }
}

Raylib-go (Go):

package main

import rl "github.com/gen2brain/raylib-go/raylib"

func main() {
    rl.InitWindow(800, 450, "Raylib Window")
    for !rl.WindowShouldClose() {
        rl.BeginDrawing()
        rl.EndDrawing()
    }
}

GLFW focuses on window creation and input handling, while Raylib-go provides a higher-level API with built-in graphics functions. GLFW offers more control but requires additional libraries for graphics, while Raylib-go simplifies game development with an all-in-one solution.

59,344

Dear ImGui: Bloat-free Graphical User interface for C++ with minimal dependencies

Pros of ImGui

  • Lightweight and easy to integrate into existing projects
  • Highly customizable with a wide range of UI elements
  • Supports multiple backends (OpenGL, DirectX, Vulkan, etc.)

Cons of ImGui

  • Requires more manual setup and rendering code
  • Less beginner-friendly compared to Raylib's simplicity
  • Limited built-in support for advanced graphics features

Code Comparison

ImGui (C++):

ImGui::Begin("Hello, ImGui!");
ImGui::Text("This is a simple window.");
if (ImGui::Button("Click me!"))
    // Handle button click
ImGui::End();

Raylib-go (Go):

rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
rl.DrawText("Hello, Raylib!", 190, 200, 20, rl.LightGray)
rl.EndDrawing()

Key Differences

  • ImGui focuses on immediate mode GUI, while Raylib is a more comprehensive game development framework
  • Raylib-go provides Go bindings for Raylib, making it easier to use in Go projects
  • ImGui requires more manual rendering and event handling, whereas Raylib abstracts much of this complexity

Use Cases

  • ImGui: Best for adding debug interfaces or tools to existing applications
  • Raylib-go: Ideal for creating 2D and 3D games or multimedia applications in Go

Both libraries have their strengths, and the choice depends on the specific project requirements and the developer's preferred language and workflow.

11,581

Hazel Engine

Pros of Hazel

  • More comprehensive game engine with built-in scene management and entity-component system
  • Designed for modern C++ development, leveraging newer language features
  • Includes a robust event system and input handling

Cons of Hazel

  • Steeper learning curve due to its more complex architecture
  • Less cross-platform support compared to raylib-go
  • Still in development, with potential for breaking changes

Code Comparison

Hazel (C++):

#include "Hazel.h"

class ExampleLayer : public Hazel::Layer
{
public:
    void OnUpdate() override
    {
        HZ_INFO("ExampleLayer::Update");
    }
};

raylib-go (Go):

package main

import "github.com/gen2brain/raylib-go/raylib"

func main() {
    rl.InitWindow(800, 450, "raylib [core] example - basic window")
    defer rl.CloseWindow()

    for !rl.WindowShouldClose() {
        rl.BeginDrawing()
        rl.ClearBackground(rl.RayWhite)
        rl.DrawText("Congrats! You created your first window!", 190, 200, 20, rl.LightGray)
        rl.EndDrawing()
    }
}

The code comparison shows that Hazel uses a more object-oriented approach with layers and events, while raylib-go provides a simpler, more procedural API for game development.

6,830

minimal cross-platform standalone C headers

Pros of sokol

  • Multi-language support: C, C++, Zig, Nim, and more
  • Modular design with separate headers for different functionalities
  • Lightweight and minimal dependencies

Cons of sokol

  • Less comprehensive than raylib-go for game development
  • Steeper learning curve for beginners
  • Limited built-in high-level game development features

Code Comparison

raylib-go:

package main

import "github.com/gen2brain/raylib-go/raylib"

func main() {
    rl.InitWindow(800, 450, "raylib [core] example - basic window")
    defer rl.CloseWindow()

    for !rl.WindowShouldClose() {
        rl.BeginDrawing()
        rl.ClearBackground(rl.RayWhite)
        rl.DrawText("Congrats! You created your first window!", 190, 200, 20, rl.LightGray)
        rl.EndDrawing()
    }
}

sokol:

#include "sokol_app.h"
#include "sokol_gfx.h"

sapp_desc sokol_main(int argc, char* argv[]) {
    return (sapp_desc){
        .init_cb = init,
        .frame_cb = frame,
        .cleanup_cb = cleanup,
        .width = 800,
        .height = 450,
        .window_title = "sokol example",
    };
}

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

logo

raylib-go

Build Status GoDoc Go Report Card Examples

Golang bindings for raylib, a simple and easy-to-use library to enjoy videogames programming.

raylib C source code is included and compiled together with bindings. Note that the first build can take a few minutes.

It is also possible to use raylib-go without cgo (Windows only; see requirements below).

Requirements

Ubuntu
apt-get install libgl1-mesa-dev libxi-dev libxcursor-dev libxrandr-dev libxinerama-dev libwayland-dev libxkbcommon-dev
Fedora
dnf install mesa-libGL-devel libXi-devel libXcursor-devel libXrandr-devel libXinerama-devel wayland-devel libxkbcommon-devel
macOS

On macOS you need Xcode or Command Line Tools for Xcode.

Windows
cgo

On Windows you need C compiler, like Mingw-w64 or TDM-GCC. You can also build binary in MSYS2 shell.

To remove console window, build with -ldflags "-H=windowsgui".

purego (without cgo, i.e. CGO_ENABLED=0)

Download the raylib.dll from the assets on the releases page. It is contained in the raylib-*_win64_msvc*.zip. Put the raylib.dll into the root folder of your project or copy it into C:\Windows\System32 for a system-wide installation.

As of November 15, 2023, raylib 5.0 is the required version.

It is also possible to build the DLL yourself. You can find more info at raylib's wiki.

Android

Android example.

Installation

go get -v -u github.com/gen2brain/raylib-go/raylib

Build tags

  • drm - build for Linux native DRM mode, including Raspberry Pi 4 and other devices (PLATFORM_DRM)
  • sdl - build for SDL backend (PLATFORM_DESKTOP_SDL)
  • rgfw - build for RGFW backend (PLATFORM_DESKTOP_RGFW)
  • noaudio - disables audio functions
  • opengl43 - uses OpenGL 4.3 backend
  • opengl21 - uses OpenGL 2.1 backend (default is 3.3 on desktop)
  • opengl11 - uses OpenGL 1.1 backend (pseudo OpenGL 1.1 style)
  • es2 - uses OpenGL ES 2.0 backend (can be used to link against Google's ANGLE)
  • es3 - experimental support for OpenGL ES 3.0
  • x11 - force X11 compatibility mode on Wayland (PLATFORM_DESKTOP/GLFW)

Documentation

Documentation on GoDoc. Also check raylib cheatsheet. If you have problems or need assistance there is an active community in the #raylib-go channel of the Raylib Discord Server that can help.

Example

package main

import rl "github.com/gen2brain/raylib-go/raylib"

func main() {
	rl.InitWindow(800, 450, "raylib [core] example - basic window")
	defer rl.CloseWindow()

	rl.SetTargetFPS(60)

	for !rl.WindowShouldClose() {
		rl.BeginDrawing()

		rl.ClearBackground(rl.RayWhite)
		rl.DrawText("Congrats! You created your first window!", 190, 200, 20, rl.LightGray)

		rl.EndDrawing()
	}
}

Check more examples organized by raylib modules.

Cross-compile (Linux)

To cross-compile for Windows install MinGW toolchain.

$ CGO_ENABLED=1 CC=x86_64-w64-mingw32-gcc GOOS=windows GOARCH=amd64 go build -ldflags "-s -w"
$ file basic_window.exe
basic_window.exe: PE32+ executable (console) x86-64 (stripped to external PDB), for MS Windows, 11 sections

$ CGO_ENABLED=1 CC=i686-w64-mingw32-gcc GOOS=windows GOARCH=386 go build -ldflags "-s -w"
$ file basic_window.exe
basic_window.exe: PE32 executable (console) Intel 80386 (stripped to external PDB), for MS Windows, 9 sections

To cross-compile for macOS install OSXCross toolchain.

$ CGO_ENABLED=1 CC=x86_64-apple-darwin21.1-clang GOOS=darwin GOARCH=amd64 go build -ldflags "-linkmode external -s -w '-extldflags=-mmacosx-version-min=10.15'"
$ file basic_window
basic_window: Mach-O 64-bit x86_64 executable, flags:<NOUNDEFS|DYLDLINK|TWOLEVEL>

$ CGO_ENABLED=1 CC=aarch64-apple-darwin21.1-clang GOOS=darwin GOARCH=arm64 go build -ldflags "-linkmode external -s -w '-extldflags=-mmacosx-version-min=12.0.0'"
$ file basic_window
basic_window: Mach-O 64-bit arm64 executable, flags:<NOUNDEFS|DYLDLINK|TWOLEVEL|PIE>

License

raylib-go is licensed under an unmodified zlib/libpng license. View LICENSE.