Ubuntu下编译golang的GXUI

Ubuntu下编译golang的GXUI

取得资源 ,安装GXUI

sudo apt-get install libxi-dev libxcursor-dev libxrandr-dev libxinerama-dev mesa-common-dev libgl1-mesa-dev libxxf86vm-dev

go get -u github.com/google/gxui/

下载了这些,你运行程序的时候还是会有各种提示,安装问题。下面是我go get的文件。

go get golang.org/x/image
go get github.com/goxjs/glfw 
go get github.com/goxjs/gl
go get github.com/golang/freetype/truetype 
go get -u github.com/go-gl/glfw/
go get  github.com/go-gl/glfw/
go get  github.com/go-gl/

这个时候你随便从samples里面找一个源程序,我找的是samples/hello_world

// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
    "time"

    "github.com/google/gxui"
    "github.com/google/gxui/drivers/gl"
    "github.com/google/gxui/gxfont"
    "github.com/google/gxui/math"
    "github.com/google/gxui/samples/flags"
)

func appMain(driver gxui.Driver) {
    theme := flags.CreateTheme(driver)

    font, err := driver.CreateFont(gxfont.Default, 75)
    if err != nil {
        panic(err)
    }

    window := theme.CreateWindow(380, 100, "Hi")
    window.SetBackgroundBrush(gxui.CreateBrush(gxui.Gray50))

    label := theme.CreateLabel()
    label.SetFont(font)
    label.SetText("Hello world")

    window.AddChild(label)

    ticker := time.NewTicker(time.Millisecond * 30)
    go func() {
        phase := float32(0)
        for _ = range ticker.C {
            c := gxui.Color{
                R: 0.75 + 0.25*math.Cosf((phase+0.000)*math.TwoPi),
                G: 0.75 + 0.25*math.Cosf((phase+0.333)*math.TwoPi),
                B: 0.75 + 0.25*math.Cosf((phase+0.666)*math.TwoPi),
                A: 0.50 + 0.50*math.Cosf(phase*10),
            }
            phase += 0.01
            driver.Call(func() {
                label.SetColor(c)
            })
        }
    }()

    window.OnClose(ticker.Stop)
    window.OnClose(driver.Terminate)
}

func main() {
    gl.StartDriver(appMain)
}

执行的时候会提示有错误,大概是下面的样子:

PlatformError: X11: Failed to open display :0
panic: NotInitialized: The GLFW library is not initialized

goroutine 1 [running, locked to thread]:
github.com/go-gl/glfw/v3.2/glfw.acceptError(0x0, 0x0, 0x0, 0xc400000000, 0xc420057e88)
    /var/www/go/src/github.com/go-gl/glfw/v3.2/glfw/error.go:173 +0x1d4
github.com/go-gl/glfw/v3.2/glfw.panicError()
    /var/www/go/src/github.com/go-gl/glfw/v3.2/glfw/error.go:184 +0x32

从字面上看是没有安装glfw,下面安装glfw

第一步,下载glfw

到地址www.glfw.org下载glfw source code:glfw-3.2.1.zip,解压

$unzip glfw-3.2.1.zip 

第二步,编译安装glfw

1. 安装依赖库,
$sudo apt-get install cmake xorg-dev libglu1-mesa-dev
2. 进入CMake,使用cmake命令生成Makefile
$sudo cmake ../
4. make && make install
$sudo make
$sudo make install

现在运行go build ,然后执行生成的./hello_world就可以看到界面了。

已邀请:

要回复问题请先登录注册