// RenderLinesWithEvents renders multiple channels of samples to screen, and draws events. func (s *Screen) RenderLinesWithEvents(lines []Line, events <-chan interface{}, sampleRate int) { s.lines = lines runtime.LockOSThread() // NOTE: It appears that glfw 3.1 uses its own internal error callback. // glfw.SetErrorCallback(func(err glfw.ErrorCode, desc string) { // log.Fatalf("%v: %s\n", err, desc) // }) if err := glfw.Init(); err != nil { log.Fatalf("Can't init glfw: %v!", err) } defer glfw.Terminate() window, err := glfw.CreateWindow(s.width, s.height, "Muse", nil, nil) if err != nil { log.Fatalf("CreateWindow failed: %s", err) } if aw, ah := window.GetSize(); aw != s.width || ah != s.height { log.Fatalf("Window doesn't have the requested size: want %d,%d got %d,%d", s.width, s.height, aw, ah) } window.MakeContextCurrent() // Must gl.Init() *after* MakeContextCurrent if err := gl.Init(); err != nil { log.Fatalf("Can't init gl: %v!", err) } // Set window up to be [0, -1.0] -> [width, 1.0], black. gl.MatrixMode(gl.MODELVIEW) gl.LoadIdentity() gl.Translated(-1, 0, 0) gl.Scaled(2/float64(s.width), 1.0, 1.0) gl.ClearColor(0.0, 0.0, 0.0, 0.0) // Actually start writing data to the buffer for i, _ := range s.lines { s.lines[i].valueBuffer = types.NewBuffer(int(float64(s.width) / s.pixelsPerSample)) s.lines[i].valueBuffer.GoPushChannel(s.lines[i].Values, sampleRate) } if events != nil { s.eventBuffer.GoPushChannel(events, sampleRate) } // Keep drawing while we still can (and should). for !window.ShouldClose() && !s.bufferFinished() { if window.GetKey(glfw.KeyEscape) == glfw.Press { break } gl.Clear(gl.COLOR_BUFFER_BIT) s.drawSignal() window.SwapBuffers() glfw.PollEvents() } // Keep window around, only close on esc. for !window.ShouldClose() && window.GetKey(glfw.KeyEscape) != glfw.Press { glfw.PollEvents() } }
func main() { err := glfw.Init() if err != nil { panic(err) } defer glfw.Terminate() width, height = 800, 800 window, err := glfw.CreateWindow(width, height, "Show RoundedRect", nil, nil) if err != nil { panic(err) } window.MakeContextCurrent() window.SetSizeCallback(reshape) window.SetKeyCallback(onKey) window.SetCharCallback(onChar) glfw.SwapInterval(1) err = gl.Init() if err != nil { panic(err) } reshape(window, width, height) for !window.ShouldClose() { if redraw { display() window.SwapBuffers() redraw = false } glfw.PollEvents() // time.Sleep(2 * time.Second) } }
// StartFrame sets everything up to start rendering a new frame. // This includes swapping in last rendered buffer, polling for window events, // checkpointing cursor tracking, and updating the time since last frame. func (w *Window) StartFrame() { // swap in the previous rendered buffer w.glfw.SwapBuffers() // poll for UI window events glfw.PollEvents() if w.inputManager.IsActive(PROGRAM_QUIT) { w.glfw.SetShouldClose(true) } // base calculations of time since last frame (basic program loop idea) // For better advanced impl, read: http://gafferongames.com/game-physics/fix-your-timestep/ curFrameTime := glfw.GetTime() if w.firstFrame { w.lastFrameTime = curFrameTime w.firstFrame = false } w.dTime = curFrameTime - w.lastFrameTime w.lastFrameTime = curFrameTime w.inputManager.CheckpointCursorChange() }
// Update must be called from within the main thread as often as possible. func (window *OpenGlWindow) Update() { glfw.PollEvents() window.glfwWindow.MakeContextCurrent() window.CallRender() window.glfwWindow.SwapBuffers() }
// // Start Loop // this starts the event loop which runs until the program ends // func (glw *Glw) StartLoop() { optimalTime := 1000.0 / float64(glw.fps) previousTime := glfw.GetTime() // If the Window is open keep looping for !glw.GetWindow().ShouldClose() { // Update time := glfw.GetTime() elapsed := time - previousTime delta := elapsed / optimalTime previousTime = time // Calls the Render Callback glw.renderer(glw, delta) // Triggers window refresh glw.GetWindow().SwapBuffers() // Triggers events glfw.PollEvents() } // Called at the end of the program, and terminates the window system glw.Terminate() }
func main() { err := glfw.Init() if err != nil { panic(err) } defer glfw.Terminate() window, err := glfw.CreateWindow(640, 480, "Testing", nil, nil) if err != nil { panic(err) } window.MakeContextCurrent() if err := gl.Init(); err != nil { panic(err) } // int width, height // glfw.GetFramebufferSize(window, &width, &height) for !window.ShouldClose() { // Do OpenGL stuff. gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) gl.Color3f(1, 1, 0) gl.Rectf(-0.75, 0.75, 0.75, -0.75) gl.Color3f(1, 0, 1) gl.Rectf(-0.5, 0.5, 0.5, -0.5) window.SwapBuffers() glfw.PollEvents() } }
func (self *Application) run() error { if err := glfw.Init(); err != nil { return err } defer glfw.Terminate() glfw.WindowHint(glfw.Resizable, glfw.False) glfw.WindowHint(glfw.ContextVersionMajor, 4) glfw.WindowHint(glfw.ContextVersionMinor, 1) glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile) glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True) window, err := glfw.CreateWindow(512, 512, "GL", nil, nil) if err != nil { return err } window.MakeContextCurrent() if err := gl.Init(); err != nil { return err } if err := self.setup(); err != nil { return err } for !window.ShouldClose() { self.display() window.SwapBuffers() glfw.PollEvents() } return nil }
// RenderLoop is the main render loop for the application func (app *ExampleApp) RenderLoop() { lastRenderTime = time.Now() for !app.MainWindow.ShouldClose() { // get the time delta loopTime := time.Now() deltaNano := loopTime.Sub(lastRenderTime).Nanoseconds() deltaF := float64(deltaNano) * (1.0 / float64(time.Second)) // call the Update callback if app.OnUpdate != nil { app.OnUpdate(deltaF) } // call the Render callback if app.OnRender != nil { app.OnRender(deltaF) } // draw the screen and get any input app.MainWindow.SwapBuffers() glfw.PollEvents() // update the last render time lastRenderTime = loopTime } }
func (u *userInterface) setScreenSize(width, height, scale int) bool { if u.width == width && u.height == height && u.scale == scale { return false } ch := make(chan struct{}) window := u.window window.SetFramebufferSizeCallback(func(w *glfw.Window, width, height int) { window.SetFramebufferSizeCallback(nil) close(ch) }) window.SetSize(width*scale, height*scale) for { done := false glfw.PollEvents() select { case <-ch: done = true default: } if done { break } } u.width = width u.height = height u.scale = scale // For retina displays, recalculate the scale with the framebuffer size. windowWidth, _ := window.GetFramebufferSize() u.actualScale = windowWidth / width return true }
func main() { if err := glfw.Init(); err != nil { log.Fatalln("failed to initialize glfw:", err) } defer glfw.Terminate() glfw.WindowHint(glfw.Resizable, glfw.False) glfw.WindowHint(glfw.ContextVersionMajor, 2) glfw.WindowHint(glfw.ContextVersionMinor, 1) window, err := glfw.CreateWindow(800, 600, "Create Window Example", nil, nil) if err != nil { panic(err) } window.MakeContextCurrent() if err := gl.Init(); err != nil { panic(err) } window.SetKeyCallback(keypress) window.SetMouseButtonCallback(mousepress) gl.Viewport(0, 0, 800, 600) for !window.ShouldClose() { draw() window.SwapBuffers() glfw.PollEvents() } }
func main() { width := 1024 height := 768 title := "Simple Triangle" if err := glfw.Init(); err != nil { log.Fatalln("failed to initialize glfw:", err) } defer glfw.Terminate() glfw.WindowHint(glfw.Samples, 4) // 4x antialiasing glfw.WindowHint(glfw.ContextVersionMajor, 3) // We want OpenGL 3.3 glfw.WindowHint(glfw.ContextVersionMinor, 3) glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True) // To make MacOS happy; should not be needed glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile) //We don't want the old OpenGL window, err := glfw.CreateWindow(width, height, title, nil, nil) if err != nil { panic(err) } window.MakeContextCurrent() if err := gl.Init(); err != nil { panic(err) } window.SetKeyCallback(keypress) window.SetMouseButtonCallback(mousepress) gl.Viewport(0, 0, 1024, 768) for !window.ShouldClose() { draw() window.SwapBuffers() glfw.PollEvents() } }
func main() { err := glfw.Init() if err != nil { panic(err) } defer glfw.Terminate() window, err := glfw.CreateWindow(640, 480, "Testing", nil, nil) if err != nil { panic(err) } window.MakeContextCurrent() if err := gl.Init(); err != nil { panic(err) } window.SetKeyCallback(game.OnKey) game.Init() for !window.ShouldClose() { game.Render() window.SwapBuffers() glfw.PollEvents() } }
// Runs the visualization with a set of DelayedNoteData. The DelayedNote data is // used to push information to the synth as well as represent the data visually. func RunVisualization(notes *synth.NoteArrangement, oNoteChannel chan synth.DelayedNoteData) error { window, assets, err := initialize() if err != nil { return err } defer destroy(window, assets) // The main update loop. ct, lt, dt := 0.0, 0.0, 0.0 for !window.ShouldClose() { // Keeping the current time. lt = ct ct = glfw.GetTime() dt = ct - lt // Real render loop. gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) if config.DebugMode { glErr := gl.GetError() if glErr != gl.NO_ERROR { fmt.Printf("OpenGL error: %d\n", glErr) } } window.SwapBuffers() glfw.PollEvents() if dt < 1/1000.0 { // Delay the thread to keep up w/ updating? } } return nil }
func main() { if err := glfw.Init(); err != nil { log.Fatalln("failed to initialize glfw:", err) } defer glfw.Terminate() glfw.WindowHint(glfw.Resizable, glfw.False) glfw.WindowHint(glfw.ContextVersionMajor, 2) glfw.WindowHint(glfw.ContextVersionMinor, 1) window, err := glfw.CreateWindow(640, 480, "tut03", nil, nil) if err != nil { panic(err) } window.MakeContextCurrent() if err := gl.Init(); err != nil { panic(err) } t0 := time.Now() program := initResources() for { curFade = float32(math.Sin(time.Now().Sub(t0).Seconds()*2*math.Pi/5)/2 + 0.5) onDisplay(program) window.SwapBuffers() glfw.PollEvents() } gl.DeleteProgram(program) gl.DeleteBuffers(1, &vboTriangle) }
func main() { if err := glfw.Init(); err != nil { log.Fatalln("failed to initialize glfw:", err) } defer glfw.Terminate() glfw.WindowHint(glfw.Resizable, glfw.False) glfw.WindowHint(glfw.ContextVersionMajor, 2) glfw.WindowHint(glfw.ContextVersionMinor, 1) window, err := glfw.CreateWindow(800, 600, "Cube", nil, nil) if err != nil { panic(err) } window.MakeContextCurrent() if err := gl.Init(); err != nil { panic(err) } texture = newTexture("square.png") defer gl.DeleteTextures(1, &texture) setupScene() for !window.ShouldClose() { drawScene() window.SwapBuffers() glfw.PollEvents() } }
func programLoop(window *glfw.Window) { // the linked shader program determines how the data will be rendered shaders := compileShaders() shaderProgram := linkShaders(shaders) // VAO contains all the information about the data to be rendered VAO := createTriangleVAO() for !window.ShouldClose() { // poll events and call their registered callbacks glfw.PollEvents() // perform rendering gl.ClearColor(0.2, 0.5, 0.5, 1.0) gl.Clear(gl.COLOR_BUFFER_BIT) // draw loop gl.UseProgram(shaderProgram) // ensure the right shader program is being used gl.BindVertexArray(VAO) // bind data gl.DrawArrays(gl.TRIANGLES, 0, 3) // perform draw call gl.BindVertexArray(0) // unbind data (so we don't mistakenly use/modify it) // end of draw loop // swap in the rendered buffer window.SwapBuffers() } }
func Run(g *Game) error { if g.Scene == nil { return fmt.Errorf("Scene property of given Game struct is empty") } // GLFW event handling must run on the main OS thread runtime.LockOSThread() if err := glfw.Init(); err != nil { return fmt.Errorf("glfw.Init failed: %s", err) } defer glfw.Terminate() glfw.WindowHint(glfw.ContextVersionMajor, OpenGLVerMinor) glfw.WindowHint(glfw.ContextVersionMinor, OpenGLVerMinor) glfw.WindowHint(glfw.Resizable, glfw.False) if g.Resizable { glfw.WindowHint(glfw.Resizable, glfw.True) } glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile) glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True) window, err := glfw.CreateWindow(int(g.Width), int(g.Height), g.Title, nil, nil) if err != nil { return fmt.Errorf("glfw.CreateWindow failed: %s", err) } window.MakeContextCurrent() // Initialize Glow if err := gl.Init(); err != nil { return fmt.Errorf("gl.Init failed:", err) } version := gl.GoStr(gl.GetString(gl.VERSION)) log.Println("gl.Init successful, OpenGL version:", version) var previousTime, deltaTime, time float32 previousTime = float32(glfw.GetTime()) - 1.0/60.0 g.Scene.Setup() for !window.ShouldClose() { time = float32(glfw.GetTime()) deltaTime = time - previousTime glfw.PollEvents() gl.ClearColor(0.2, 0.3, 0.3, 0.5) gl.Clear(gl.COLOR_BUFFER_BIT) g.Scene.Update(deltaTime) g.Scene.Draw(deltaTime) previousTime = time window.SwapBuffers() } return nil }
func main() { err := glfw.Init() if err != nil { panic(err) } defer glfw.Terminate() w, err := glfw.CreateWindow(640, 480, "Testing", nil, nil) if err != nil { panic(err) } w.MakeContextCurrent() glfw.SwapInterval(1) w.SetCharCallback(charCallBack) if err := gl.Init(); err != nil { panic(err) } setupScene(w) fmt.Println("Press 'q' to quit") for !w.ShouldClose() { // Do OpenGL stuff. time.Sleep(10 * time.Millisecond) drawScene(w) w.SwapBuffers() glfw.PollEvents() } }
func main() { flag.Parse() go func() { http.ListenAndServe("localhost:6060", nil) }() if err := glfw.Init(); err != nil { log.Fatalln("failed to initialize glfw:", err) } defer glfw.Terminate() glfw.WindowHint(glfw.Resizable, glfw.True) glfw.WindowHint(glfw.Visible, glfw.False) // do not steal focus glfw.WindowHint(glfw.ContextVersionMajor, 2) glfw.WindowHint(glfw.ContextVersionMinor, 1) window, err := glfw.CreateWindow(800, 600, "Spector", nil, nil) if err != nil { panic(err) } window.MakeContextCurrent() window.Restore() window.SetPos(32, 64) if err := gl.Init(); err != nil { panic(err) } state := NewState() for !window.ShouldClose() { if window.GetKey(glfw.KeyEscape) == glfw.Press { return } if window.GetKey(glfw.KeyR) == glfw.Press { state.Stop() state = NewState() } start := time.Now() state.Update(1.0 / 60.0) updateTime := time.Since(start) start = time.Now() state.Render(window) renderTime := time.Since(start) text := fmt.Sprintf("update: %.2fms render: %.2fms", float32(updateTime)/float32(time.Millisecond), float32(renderTime)/float32(time.Millisecond)) w, h := window.GetSize() state.Backend.SetFontColor(ui.ColorHex(0xFF0000FF)) size := state.Backend.Measure(text) state.Backend.Text(text, ui.Block(float32(w)-size.X, float32(h)-size.Y, size.X, size.Y)) window.SwapBuffers() glfw.PollEvents() } }
func EventLoop() bool { glfw.PollEvents() if window.ShouldClose() { return false } return running }
func main() { err := glfw.Init() if err != nil { panic(err) } defer glfw.Terminate() w, err := glfw.CreateWindow(640, 480, "Testing 3+", nil, nil) if err != nil { panic(err) } w.MakeContextCurrent() glfw.SwapInterval(1) w.SetCharCallback(charCallBack) if err := gl.Init(); err != nil { panic(err) } r := makeResources() gl.ClearColor(.5, .5, .5, 0) fmt.Println("Press 'q' to quit") for !w.ShouldClose() { time.Sleep(10 * time.Millisecond) render(w, r) w.SwapBuffers() glfw.PollEvents() } }
func (a *Application) Continue() bool { glfw.PollEvents() if a.window.ShouldClose() { return false } a.update() return true }
func (d *Director) Run() { for !d.window.ShouldClose() { d.Step() d.window.SwapBuffers() glfw.PollEvents() } d.SetView(nil) }
func main() { err := glfw.Init() if err != nil { panic(err) } defer glfw.Terminate() fp, err := os.Open("example.tmx") if err != nil { panic(err) } m, err := tmx.NewMap(fp) if err != nil { panic(err) } var monitor *glfw.Monitor window, err := glfw.CreateWindow(screenWidth, screenHeight, "Map Renderer", monitor, nil) if err != nil { panic(err) } window.MakeContextCurrent() if err := gl.Init(); err != nil { panic(err) } width, height := window.GetFramebufferSize() gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) gl.ClearColor(1.0, 1.0, 1.0, 1.0) gl.Viewport(0, 0, int32(width), int32(height)) gl.MatrixMode(gl.PROJECTION) gl.LoadIdentity() gl.Ortho(0, float64(width), float64(height), 0, -1, 1) gl.Enable(gl.BLEND) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) canvas := newOpenGLCanvas(width, height, float32(width)/float32(screenWidth), float32(height)/float32(screenHeight)) renderer := tmx.NewRenderer(*m, canvas) fps := 0 startTime := time.Now().UnixNano() timer := tmx.CreateTimer() timer.Start() for !window.ShouldClose() { elapsed := float64(timer.GetElapsedTime()) / (1000 * 1000) renderer.Render(int64(math.Ceil(elapsed))) fps++ if time.Now().UnixNano()-startTime > 1000*1000*1000 { log.Println(fps) startTime = time.Now().UnixNano() fps = 0 } window.SwapBuffers() glfw.PollEvents() timer.UpdateTime() } }
func Run(window *Window) { setupScene(window.width, window.height) for !window.handle.ShouldClose() { drawScene() window.Render() window.handle.SwapBuffers() glfw.PollEvents() } }
func (me *Core) Play(g Game) { for !me.Win.ShouldClose() { gl.Clear(me.ClearOpt) g.Play() g.Display(me) glfw.PollEvents() me.Win.SwapBuffers() me.Exit() } }
func main() { runtime.LockOSThread() if err := glfw.Init(); err != nil { log.Fatal(err) } defer glfw.Terminate() if err := gl.Init(); err != nil { log.Fatal(err) } window, err := glfw.CreateWindow(WindowWidth, WindowHeight, "Texture Demo", nil, nil) if err != nil { log.Fatal(err) } window.MakeContextCurrent() const vertShader = `#version 120 uniform mat4 proj; attribute vec3 vertex_position; attribute vec2 vertex_texture; varying vec2 texture_coordinates; void main() { gl_Position = proj * vec4(vertex_position, 1); texture_coordinates = vertex_texture; } ` const fragShader = `#version 120 uniform sampler2D tex_loc; varying vec2 texture_coordinates; void main() { gl_FragColor = texture2D(tex_loc, texture_coordinates); } ` texture, err := newImageTexture("sq.png") if err != nil { log.Fatal(err) } scene, err := NewScene(vertShader, fragShader, texture) if err != nil { log.Fatal(err) } for !window.ShouldClose() { scene.Draw() glfw.PollEvents() window.SwapBuffers() } }
func main() { runtime.LockOSThread() if err := glfw.Init(); err != nil { panic(err) } defer glfw.Terminate() window, err := glfw.CreateWindow(800, 600, "fontstash example", nil, nil) if err != nil { panic(err) } window.MakeContextCurrent() glfw.SwapInterval(1) gl.Init() data, err := ioutil.ReadFile(filepath.Join("..", "ClearSans-Regular.ttf")) if err != nil { panic(err) } gl.Enable(gl.TEXTURE_2D) tmpBitmap := make([]byte, 512*512) cdata, err, _, tmpBitmap := truetype.BakeFontBitmap(data, 0, 32, tmpBitmap, 512, 512, 32, 96) var ftex uint32 gl.GenTextures(1, &ftex) gl.BindTexture(gl.TEXTURE_2D, ftex) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR) gl.TexImage2D(gl.TEXTURE_2D, 0, gl.ALPHA, 512, 512, 0, gl.ALPHA, gl.UNSIGNED_BYTE, unsafe.Pointer(&tmpBitmap[0])) gl.ClearColor(0.3, 0.3, 0.32, 1.) for !window.ShouldClose() { gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) gl.MatrixMode(gl.PROJECTION) gl.LoadIdentity() gl.Ortho(0, 800, 600, 0, 0, 1) gl.MatrixMode(gl.MODELVIEW) gl.LoadIdentity() gl.Disable(gl.DEPTH_TEST) gl.Color4ub(255, 255, 255, 255) gl.Enable(gl.BLEND) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) my_print(100, 100, "The quick brown fox jumps over the fence", ftex, cdata) window.SwapBuffers() glfw.PollEvents() } }
func drawLoop(win *glfw.Window, vao uint32, shader uint32) { for !win.ShouldClose() { gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) gl.BindVertexArray(vao) gl.UseProgram(shader) gl.DrawArrays(gl.TRIANGLES, 0, 3) glfw.PollEvents() win.SwapBuffers() } }
func Run() { if err := glfw.Init(); err != nil { glog.Fatalln("failed to initialize glfw", err) } defer glfw.Terminate() setupWindowOptions() window, err := glfw.CreateWindow(WindowWidth, WindowHeight, "Game", nil, nil) if err != nil { panic(err) } window.MakeContextCurrent() //initilize Glow if err := gl.Init(); err != nil { panic(err) } version := gl.GoStr(gl.GetString(gl.VERSION)) fmt.Println("OpenGL version", version) shaderSource, err := ReadShaders("colorShader") if err != nil { panic(err) } program, err := NewProgram(shaderSource) if err != nil { panic(err) } program.Use() sprite := &SpriteComponent{-.5, -.5, 1, 1} sprite.ReloadGraphics() vertAttrib := uint32(gl.GetAttribLocation(program.program, CStr("vertPosition"))) gl.EnableVertexAttribArray(vertAttrib) gl.VertexAttribPointer(vertAttrib, 3, gl.FLOAT, false, 0, gl.PtrOffset(0)) gl.Enable(gl.DEPTH_TEST) gl.DepthFunc(gl.LESS) gl.ClearColor(1.0, 1.0, 1.0, 1.0) for !window.ShouldClose() { gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) program.Use() gl.BindVertexArray(sprite.vaoID) gl.DrawArrays(gl.TRIANGLES, 0, 2*3) window.SwapBuffers() glfw.PollEvents() } }