Example #1
0
func (i *IO) Init(title string, screenSize int, onCloseHandler func()) error {
	i.KeyHandler = new(KeyHandler)
	i.Display = new(Display)
	i.ScreenOutputChannel = make(chan *types.Screen)
	i.AudioOutputChannel = make(chan int)

	if err := glfw.Init(); err != nil {
		return err
	} else if err = i.Display.init(title, screenSize); err != nil {
		return err
	}

	i.KeyHandler.Init(DefaultControlScheme) //TODO: allow user to define controlscheme
	glfw.SetKeyCallback(func(key, state int) {
		if state == glfw.KeyPress {
			i.KeyHandler.KeyDown(key)
		} else {
			i.KeyHandler.KeyUp(key)
		}
	})

	glfw.SetWindowCloseCallback(func() int {
		glfw.CloseWindow()
		glfw.Terminate()
		onCloseHandler()
		return 0
	})

	return nil
}
func Init() {

	runtime.LockOSThread()

	// Initialize GLFW

	var err error

	if err = glfw.Init(); err != nil {
		log.Fatalf("%v\n", err)
		return
	}

	err = glfw.OpenWindow(SCREEN_WIDTH, SCREEN_HEIGHT,
		0, 0, 0, 0, 0, 0, glfw.Windowed)

	if err != nil {
		log.Fatalf("%v\n", err)
		return
	}

	glfw.SetWindowTitle("Mandelbrot")
	glfw.SetSwapInterval(1)

	glfw.SetWindowSizeCallback(onResize)
	glfw.SetWindowCloseCallback(onClose)
	glfw.SetMouseButtonCallback(onMouseBtn)
	glfw.SetMouseWheelCallback(onMouseWheel)
	glfw.SetKeyCallback(onKey)
	glfw.SetCharCallback(onChar)

	// Initialize OpenGL
	gl.Disable(gl.DEPTH_TEST)
	gl.ClearColor(0, 0, 0, 0)
}
Example #3
0
func (i *IO) Init(title string, screenSize int, onCloseHandler func()) error {
	var err error

	err = glfw.Init()
	if err != nil {
		return err
	}

	err = i.Display.init(title, screenSize)
	if err != nil {
		return err
	}

	i.KeyHandler.Init(DefaultControlScheme) //TODO: allow user to define controlscheme
	glfw.SetKeyCallback(func(key, state int) {
		if state == glfw.KeyPress {
			i.KeyHandler.KeyDown(key)
		} else {
			i.KeyHandler.KeyUp(key)
		}
	})

	glfw.SetWindowCloseCallback(func() int {
		glfw.CloseWindow()
		glfw.Terminate()
		onCloseHandler()
		return 0
	})

	return nil
}
Example #4
0
func WindowCloseChan() chan bool {
	out := make(chan bool)
	glfw.SetWindowCloseCallback(func() int {
		go func() { out <- true }()
		return 1
	})
	return out
}
Example #5
0
// Wait for an event to be passed into the Events channel and execute the
// corresponding registered actions.
func PullEvents() {
	glfw.SetKeyCallback(keyHandler)
	glfw.SetWindowCloseCallback(func() int { CloseWindow(); return 0 })
	for {
		e := <-Events
		for r, fn := range registered {
			// TODO change this (maps are supposed to work?)
			if e.Code() == r.Code() && e.State() == r.State() {
				fn()
			}
		}
	}
}
Example #6
0
func main() {
	var err error
	if err = glfw.Init(); err != nil {
		fmt.Fprintf(os.Stderr, "[e] %v\n", err)
		return
	}

	// Ensure glfw is cleanly terminated on exit.
	defer glfw.Terminate()

	if err = glfw.OpenWindow(256, 256, 8, 8, 8, 0, 0, 0, glfw.Windowed); err != nil {
		fmt.Fprintf(os.Stderr, "[e] %v\n", err)
		return
	}

	// Ensure window is cleanly closed on exit.
	defer glfw.CloseWindow()

	// Enable vertical sync on cards that support it.
	glfw.SetSwapInterval(1)

	// Set window title
	glfw.SetWindowTitle("Simple GLFW window")

	// Hook some events to demonstrate use of callbacks.
	// These are not necessary if you don't need them.
	glfw.SetWindowSizeCallback(onResize)
	glfw.SetWindowCloseCallback(onClose)
	glfw.SetMouseButtonCallback(onMouseBtn)
	glfw.SetMouseWheelCallback(onMouseWheel)
	glfw.SetKeyCallback(onKey)
	glfw.SetCharCallback(onChar)

	// Start loop
	running := true
	for running {
		// OpenGL rendering goes here.

		// Swap front and back rendering buffers. This also implicitly calls
		// glfw.PollEvents(), so we have valid key/mouse/joystick states after
		// this. This behavior can be disabled by calling glfw.Disable with the
		// argument glfw.AutoPollEvents. You must be sure to manually call
		// PollEvents() or WaitEvents() in this case.
		glfw.SwapBuffers()

		// Break out of loop when Escape key is pressed, or window is closed.
		running = glfw.Key(glfw.KeyEsc) == 0 && glfw.WindowParam(glfw.Opened) == 1
	}
}
Example #7
0
func (_ *NgUserIO) recreateWin() (err error) {
	winInit := &Options.Initialization.Window
	if UserIO.Window.isCreated {
		glfw.CloseWindow()
	}
	if UserIO.Window.isCreated, err = false, glfw.OpenWindow(UserIO.Window.width, UserIO.Window.height, winInit.Rbits, winInit.Gbits, winInit.Bbits, winInit.Abits, winInit.DepthBits, winInit.StencilBits, ugo.Ifi(UserIO.Window.fullscreen, glfw.Fullscreen, glfw.Windowed)); err == nil {
		UserIO.Window.width, UserIO.Window.height = glfw.WindowSize()
		UserIO.Window.isCreated = true
		UserIO.Window.SetTitle(UserIO.Window.title)
		UserIO.Window.SetSwapInterval(UserIO.Window.swap)
		glfw.SetWindowCloseCallback(glfwOnWindowClose)
		glfw.SetWindowSizeCallback(glfwOnWindowResize)
		// glfw.Disable(glfw.MouseCursor)
		glfw.Disable(glfw.AutoPollEvents)
		glfw.Disable(glfw.StickyKeys)
	}
	return
}
Example #8
0
// Specify a function to call when the window is closed.
func (c *Controller) SetCloseCallback(handler CloseHandler) {
	glfw.SetWindowCloseCallback(glfw.WindowCloseHandler(handler))
}
Example #9
0
func New(width int, height int, title string) *Window {
	var err error
	window := &Window{width: width, height: height, title: title}
	// initialize logger
	logf, err := os.OpenFile("window.log", os.O_WRONLY|os.O_CREATE, 0640)
	logger := log.New(logf, "", log.Ldate|log.Ltime)

	if err = glfw.Init(); err != nil {
		fmt.Fprintf(os.Stderr, "[e] %v\n", err)
		return nil
	}

	// width, hieght, r,g,b,a (color depth bits), depth, stencil, mode
	if err = glfw.OpenWindow(window.width, window.height, 8, 8, 8, 0, 0, 0, glfw.Windowed); err != nil {
		fmt.Fprintf(os.Stderr, "[e] %v\n", err)
		return nil
	}

	onResize := func(w, h int) {
		logger.Printf("resized: %dx%d\n", w, h)
	}

	onClose := func() int {
		logger.Println("window closed\n")
		return 1 // return 0 to keep window open.
	}

	// list callback generators
	createBtnList := func(btnNext func() interface{}, device string) func(button, state int) {
		btnList := list.New()
		btnNext = func() interface{} { return btnList.Remove(btnList.Back()) }
		return func(button, state int) {
			btnList.PushFront(btnEvent{button: button, state: state})
			logger.Printf("%s button: %d, %d\n", device, button, state)
		}
	}

	createPosList := func(posNext func() interface{}, device string) func(x, y int) {
		posList := list.New()
		posNext = func() interface{} { return posList.Remove(posList.Back()) }
		return func(x, y int) {
			posList.PushFront(posEvent{x: x, y: y})
			logger.Printf("%s pos: %d, %d\n", device, x, y)
		}
	}

	createDeltaList := func(deltaNext func() interface{}, device string) func(delta int) {
		deltaList := list.New()
		deltaNext = func() interface{} { return deltaList.Remove(deltaList.Back()) }
		return func(delta int) {
			deltaList.PushFront(deltaEvent{delta: delta})
			logger.Printf("%s delta: %d\n", device, delta)
		}
	}

	glfw.SetSwapInterval(1)
	glfw.SetWindowTitle(title)

	glfw.SetWindowSizeCallback(onResize)
	glfw.SetWindowCloseCallback(onClose)
	glfw.SetMousePosCallback(createPosList(window.MousePos, "mouse pos"))
	glfw.SetMouseButtonCallback(createBtnList(window.MouseBtn, "mouse"))
	glfw.SetMouseWheelCallback(createDeltaList(window.MouseWheel, "mouse wheel"))
	glfw.SetKeyCallback(createBtnList(window.KeyEvt, "keyboard"))
	glfw.SetCharCallback(createBtnList(window.CharEvt, "char"))

	window.Start = func() {
		defer glfw.Terminate()
		defer glfw.CloseWindow()
		running := true
		for running {
			window.Render()
			glfw.SwapBuffers()
			// Break out of loop when Escape key is pressed, or window is closed.
			running = glfw.Key(glfw.KeyEsc) == 0 && glfw.WindowParam(glfw.Opened) == 1
		}
	}

	return window
}
Example #10
0
func (me *window) CallbackWindowClose(f func()) {
	glfw.SetWindowCloseCallback(func() int {
		f()
		return 0
	})
}
Example #11
0
func (w *glfwBackend) Open(width, height, zoom int, fs bool, font *FontData) {
	if err := glfw.Init(); err != nil {
		panic(err)
	}

	w.font = font
	w.zoom = zoom
	w.width = width
	w.height = height

	var fwidth = width * font.CellWidth * zoom
	var fheight = height * font.CellHeight * zoom
	var twidth = fwidth
	var theight = fheight

	flag := glfw.Windowed
	if fs {
		flag = glfw.Fullscreen
		dm := glfw.DesktopMode()
		twidth = dm.W
		theight = dm.H
	}

	glfw.OpenWindowHint(glfw.WindowNoResize, gl.TRUE)
	err := glfw.OpenWindow(twidth, theight, 8, 8, 8, 8, 0, 0, flag)
	if err != nil {
		panic(err)
	}

	w.key = NOKEY
	glfw.SetWindowCloseCallback(func() int { w.Close(); return 0 })
	glfw.SetKeyCallback(func(key, state int) { w.setKey(key, state) })
	glfw.SetCharCallback(func(key, state int) { w.setKey(key, state) })
	glfw.Enable(glfw.KeyRepeat)

	w.mouse = new(MouseData)
	glfw.Enable(glfw.MouseCursor)
	glfw.SetMousePosCallback(func(x, y int) { w.mouseMove(x, y) })
	glfw.SetMouseButtonCallback(func(but, state int) { w.mousePress(but, state) })
	glfw.Enable(glfw.MouseCursor)

	xoff := float32(twidth-fwidth) / 2.0
	yoff := float32(theight-fheight) / 2.0

	fc := float32(font.CellWidth * zoom)
	fch := float32(font.CellHeight * zoom)
	for y := 0; y < height; y++ {
		for x := 0; x < width; x++ {
			cx := xoff + float32(x)*fc
			cy := yoff + float32(y)*fch
			w.verts = append(w.verts, cx, cy, cx, cy+fch, cx+fc, cy+fch, cx+fc, cy)
		}
	}

	runtime.LockOSThread()
	glInit(twidth, theight)

	m := font.Image.(*image.RGBA)
	w.s = float32(font.CellWidth) / float32(m.Bounds().Max.X)
	w.t = float32(font.CellHeight) / float32(m.Bounds().Max.Y)
	textures = make([]gl.Uint, 2)
	gl.GenTextures(2, &textures[0])

	gl.BindTexture(gl.TEXTURE_2D, textures[0])
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
	gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.Sizei(m.Bounds().Max.X), gl.Sizei(m.Bounds().Max.Y), 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Pointer(&m.Pix[0]))

	m = image.NewRGBA(image.Rect(0, 0, font.CellWidth, font.CellHeight))
	draw.Draw(m, m.Bounds(), &image.Uniform{White}, image.ZP, draw.Src)
	gl.BindTexture(gl.TEXTURE_2D, textures[1])
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
	gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.Sizei(m.Bounds().Max.X), gl.Sizei(m.Bounds().Max.Y), 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Pointer(&m.Pix[0]))

	w.open = true
}