Skip to content

Instantly share code, notes, and snippets.

@slimsag
Created March 2, 2015 23:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slimsag/43d8232a7231168ac466 to your computer and use it in GitHub Desktop.
Save slimsag/43d8232a7231168ac466 to your computer and use it in GitHub Desktop.
// Copyright 2014 The Azul3D Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Example - Demonstrates receiving window events.
package main
import (
"azul3d.org/gfx.v1"
"azul3d.org/gfx/window.v2"
"azul3d.org/keyboard.v1"
"azul3d.org/mouse.v1"
"image"
)
// gfxLoop is responsible for drawing things to the window.
func gfxLoop(w window.Window, r gfx.Renderer) {
r.Clock().SetMaxFrameRate(0) // Disable frame rate limit (BAD!)
var cursorX int
var cursorY int
// You can handle window events in a seperate goroutine!
go func() {
// Create our events channel with sufficient buffer size.
events := make(chan window.Event, 256)
// Notify our channel anytime any event occurs.
w.Notify(events, window.AllEvents)
// Wait for events.
for event := range events {
switch e := event.(type) {
case window.CursorMoved:
cursorX = int(e.X)
cursorY = int(e.Y)
}
}
}()
for {
// Clear the entire area (empty rectangle means "the whole area").
r.Clear(image.Rect(0, 0, 0, 0), gfx.Color{1, 1, 1, 1})
// The keyboard is monitored for you, simply check if a key is down:
if w.Keyboard().Down(keyboard.Space) {
// Clear a red rectangle.
r.Clear(image.Rect(0, 0, 100, 100), gfx.Color{1, 0, 0, 1})
}
// And the same thing with the mouse, check if a mouse button is down:
if w.Mouse().Down(mouse.Left) {
// Clear a blue rectangle.
r.Clear(image.Rect(cursorX-50, cursorY-50, 50+cursorX, 50+cursorY), gfx.Color{0, 0, 1, 1})
}
// Render the whole frame.
r.Render()
}
}
func main() {
props := window.NewProps()
props.SetVSync(false) // Disable vsync (BAD!)
window.Run(gfxLoop, props)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment