Created
October 21, 2017 00:14
-
-
Save tbogdala/f071bc12ec824cee5b8f517b77d969ba to your computer and use it in GitHub Desktop.
a quick test importing multiple GL versions with golang-ui/nuklear using 3.2 and creating a 3.3 context...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"runtime" | |
_ "github.com/go-gl/gl/v3.2-core/gl" | |
"github.com/go-gl/gl/v3.3-core/gl" | |
"github.com/go-gl/glfw/v3.2/glfw" | |
"github.com/golang-ui/nuklear/nk" | |
) | |
const ( | |
winWidth = 400 | |
winHeight = 500 | |
maxVertexBuffer = 512 * 1024 | |
maxElementBuffer = 128 * 1024 | |
) | |
func init() { | |
runtime.LockOSThread() | |
} | |
func main() { | |
/////////////////////////////////////////////////////////////////////////// | |
// Setup OpenGL / GLFW | |
if err := glfw.Init(); err != nil { | |
fmt.Println(err) | |
return | |
} | |
glfw.WindowHint(glfw.Samples, 4) | |
glfw.WindowHint(glfw.ContextVersionMajor, 3) | |
glfw.WindowHint(glfw.ContextVersionMinor, 3) | |
glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile) | |
glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True) | |
win, err := glfw.CreateWindow(winWidth, winHeight, "Nuklear Demo", nil, nil) | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
win.SetSizeCallback(onWindowResize) | |
win.MakeContextCurrent() | |
glfw.SwapInterval(1) | |
width, height := win.GetSize() | |
log.Printf("glfw: created window %dx%d", width, height) | |
if err := gl.Init(); err != nil { | |
fmt.Printf("opengl: init failed: %v", err) | |
return | |
} | |
gl.Viewport(0, 0, int32(width), int32(height)) | |
/////////////////////////////////////////////////////////////////////////// | |
// Setup Nuklear | |
ctx := nk.NkPlatformInit(win, nk.PlatformInstallCallbacks) | |
atlas := nk.NewFontAtlas() | |
nk.NkFontStashBegin(&atlas) | |
sansFont := nk.NkFontAtlasAddFromBytes(atlas, MustAsset("assets/FreeSans.ttf"), 16, nil) | |
// sansFont := nk.NkFontAtlasAddDefault(atlas, 16, nil) | |
nk.NkFontStashEnd() | |
if sansFont != nil { | |
nk.NkStyleSetFont(ctx, sansFont.Handle()) | |
} | |
for !win.ShouldClose() { | |
glfw.PollEvents() | |
uiRender(win, ctx) | |
win.SwapBuffers() | |
} | |
} | |
func onWindowResize(w *glfw.Window, width int, height int) { | |
// renderer.ChangeResolution(int32(width), int32(height)) | |
} | |
func uiRender(win *glfw.Window, ctx *nk.Context) { | |
var bgColor nk.Color | |
nk.NkPlatformNewFrame() | |
// Layout | |
bounds := nk.NkRect(50, 50, 230, 250) | |
update := nk.NkBegin(ctx, "Demo", bounds, | |
nk.WindowBorder|nk.WindowMovable|nk.WindowScalable|nk.WindowMinimizable|nk.WindowTitle) | |
if update > 0 { | |
nk.NkLayoutRowStatic(ctx, 30, 80, 1) | |
{ | |
if nk.NkButtonLabel(ctx, "button") > 0 { | |
log.Println("[INFO] button pressed!") | |
} | |
} | |
nk.NkLayoutRowDynamic(ctx, 25, 1) | |
{ | |
size := nk.NkVec2(nk.NkWidgetWidth(ctx), 400) | |
if nk.NkComboBeginColor(ctx, bgColor, size) > 0 { | |
nk.NkLayoutRowDynamic(ctx, 120, 1) | |
bgColor = nk.NkColorPicker(ctx, bgColor, nk.ColorFormatRGBA) | |
nk.NkLayoutRowDynamic(ctx, 25, 1) | |
r, g, b, a := bgColor.RGBAi() | |
r = nk.NkPropertyi(ctx, "#R:", 0, r, 255, 1, 1) | |
g = nk.NkPropertyi(ctx, "#G:", 0, g, 255, 1, 1) | |
b = nk.NkPropertyi(ctx, "#B:", 0, b, 255, 1, 1) | |
a = nk.NkPropertyi(ctx, "#A:", 0, a, 255, 1, 1) | |
bgColor.SetRGBAi(r, g, b, a) | |
nk.NkComboEnd(ctx) | |
} | |
} | |
} | |
nk.NkEnd(ctx) | |
bg := make([]float32, 4) | |
nk.NkColorFv(bg, bgColor) | |
width, height := win.GetSize() | |
gl.Viewport(0, 0, int32(width), int32(height)) | |
gl.Clear(gl.COLOR_BUFFER_BIT) | |
gl.ClearColor(bg[0], bg[1], bg[2], bg[3]) | |
nk.NkPlatformRender(nk.AntiAliasingOn, maxVertexBuffer, maxElementBuffer) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment