Ejemplo n.º 1
0
Archivo: ctx.go Proyecto: go3d/go-ngine
func (me *context) Window(winf *ngctx.WinProfile, bufSize *ngctx.BufferBits, ctxProf *ngctx.CtxProfile) (window ngctx.Window, err error) {
	glfw.WindowHint(glfw.Samples, winf.MultiSampling)
	glfw.WindowHint(glfw.RedBits, bufSize.Color.R)
	glfw.WindowHint(glfw.GreenBits, bufSize.Color.G)
	glfw.WindowHint(glfw.BlueBits, bufSize.Color.B)
	glfw.WindowHint(glfw.AlphaBits, bufSize.Color.A)
	glfw.WindowHint(glfw.DepthBits, bufSize.Depth)
	glfw.WindowHint(glfw.StencilBits, bufSize.Stencil)
	glfw.WindowHint(glfw.ContextVersionMajor, ctxProf.Version.Major)
	glfw.WindowHint(glfw.ContextVersionMinor, ctxProf.Version.Minor)
	glfw.WindowHint(glfw.OpenglProfile, glfw.OpenglCoreProfile)
	if ctxProf.ForwardCompat {
		glfw.WindowHint(glfw.OpenglForwardCompatible, 1)
	}
	var mon *glfw.Monitor
	if winf.FullScreen {
		mon, err = glfw.GetPrimaryMonitor()
	}
	if err == nil {
		var win *glfw.Window
		if win, err = glfw.CreateWindow(winf.Width, winf.Height, winf.Title, mon, nil); win != nil {
			window = newWindow(win)
			if winf.FullScreen {
				win.SetInputMode(glfw.Cursor, glfw.CursorHidden)
			}
		}
	}
	return
}
Ejemplo n.º 2
0
func (self *OpenGLWindow) Open() {
	var err error
	if !glfw.Init() {
		panic(errors.New("Unable to initialize GLFW"))
	}

	glfw.SetErrorCallback(func(code glfw.ErrorCode, desc string) {
		log.Printf("[GLFW Error] (%d) %s", code, desc)
	})

	var monitor *glfw.Monitor
	if self.config.Fullscreen {
		monitor, err = glfw.GetPrimaryMonitor()

		if err != nil {
			panic(err)
		}
	}

	glfw.WindowHint(glfw.ContextVersionMajor, 4)
	glfw.WindowHint(glfw.ContextVersionMinor, 1)
	glfw.WindowHint(glfw.OpenglProfile, glfw.OpenglCoreProfile)
	glfw.WindowHint(glfw.OpenglForwardCompatible, glfw.True)

	// Default buffer sizes
	glfw.WindowHint(glfw.DepthBits, 32)
	glfw.WindowHint(glfw.StencilBits, 0)

	// Toggle VSync. Turning VSync off aparently doesn't work via glfw through
	// some ATI cards and drivers
	if self.config.VSync {
		glfw.SwapInterval(1)
	} else {
		glfw.SwapInterval(0)
	}

	self.window, err = glfw.CreateWindow(
		int(self.config.Width), int(self.config.Height),
		"Project Slartibartfast",
		monitor,
		nil)

	if err != nil {
		panic(err)
	}

	self.window.MakeContextCurrent()

	if glewError := gl.Init(); glewError != 0 {
		panic(errors.New("Unable to initialize OpenGL"))
	}
}
Ejemplo n.º 3
0
func initWindow() (window *glfw.Window, err error) {
	monitor, err := glfw.GetPrimaryMonitor()
	if err != nil {
		return nil, err
	}
	videomode, err := monitor.GetVideoMode()
	if err != nil {
		return nil, err
	}
	if videomode.Height < 480 || videomode.Width < 640 {
		return nil, errors.New("unsupported resolution!")
	}

	ratio := float64(videomode.Width) / float64(videomode.Height)

	if fullscreen {
		glfw.WindowHint(glfw.Decorated, 0)
		window, err = glfw.CreateWindow(videomode.Width, videomode.Height, "Golang Asteroids!", nil, nil)
		if err != nil {
			return nil, err
		}
		window.SetPosition(0, 0)
	} else {
		glfw.WindowHint(glfw.Decorated, 1)
		window, err = glfw.CreateWindow(int(ratio*480), 480, "Golang Asteroids!", nil, nil)
		if err != nil {
			return nil, err
		}
		window.SetPosition(videomode.Width/2-320, videomode.Height/2-240)
	}

	window.SetKeyCallback(keyCallback)
	window.SetFramebufferSizeCallback(reshapeWindow)
	window.MakeContextCurrent()

	gl.Init()
	gl.ClearColor(gl.GLclampf(Colorize(0)), gl.GLclampf(Colorize(0)), gl.GLclampf(Colorize(0)), 0.0)
	gl.Clear(gl.COLOR_BUFFER_BIT)

	width, height := window.GetFramebufferSize()
	reshapeWindow(window, width, height)

	return window, nil
}
Ejemplo n.º 4
0
// NewWindow creates a new window for the given dimensions and title.
// The window is created via GLFW.
func NewWindow(settings WindowSettings) (*Window, error) {
	// Error callback
	glfw.SetErrorCallback(errorCallback)

	// Init glfw
	logger.Debug("Initializing GLFW")
	if !glfw.Init() {
		return nil, errors.New("Could not initialise GLFW.")
	}

	glfw.WindowHint(glfw.Samples, 4)
	glfw.WindowHint(glfw.ContextVersionMajor, 3)
	glfw.WindowHint(glfw.ContextVersionMinor, 3)
	glfw.WindowHint(glfw.OpenglForwardCompatible, glfw.True)
	glfw.WindowHint(glfw.OpenglProfile, glfw.OpenglCoreProfile)
	glfw.WindowHint(glfw.OpenglDebugContext, 1)

	var monitor *glfw.Monitor
	var err error
	if settings.Fullscreen {
		logger.Debug("Get primary monitor to create fullscreen window.")
		monitor, err = glfw.GetPrimaryMonitor()
		if err != nil {
			return nil, err
		}

		logger.Debug("Checking available video modes:")
		videoModes, err := monitor.GetVideoModes()
		if err != nil {
			return nil, err
		}

		for _, videoMode := range videoModes {
			logger.Debug(fmt.Sprintf("-- %+v", videoMode))
		}

		idealVideoMode := videoModes[len(videoModes)-1]

		settings.Width = idealVideoMode.Width
		settings.Height = idealVideoMode.Height
	}

	// Create window
	logger.Info("Creating new window")
	window, err := glfw.CreateWindow(settings.Width, settings.Height, settings.Title, monitor, nil)
	if err != nil {
		return nil, err
	}
	window.SetKeyCallback(keyCallback)
	window.MakeContextCurrent()
	window.SetInputMode(glfw.StickyKeys, 1)

	// Use vsync
	glfw.SwapInterval(1)

	w := &Window{
		window:   window,
		Settings: settings,
	}

	currentWindow = w

	return w, nil
}
Ejemplo n.º 5
0
func CreateWindow(width, height int, name string, fullscreen bool, delegate WindowDelegate, legacy bool) error {
	if !glfw.Init() {
		return errors.New("Failed to initialize GLFW")
	}
	defer glfw.Terminate()

	glfw.WindowHint(glfw.DepthBits, 16)
	if !legacy {
		glfw.WindowHint(glfw.ContextVersionMajor, 3)
		glfw.WindowHint(glfw.ContextVersionMinor, 2)
		glfw.WindowHint(glfw.OpenglProfile, glfw.OpenglCoreProfile)
		glfw.WindowHint(glfw.OpenglForwardCompatible, 1)
	}

	var monitor *glfw.Monitor = nil
	var err error = nil
	if fullscreen {
		monitor, err = glfw.GetPrimaryMonitor()
		if err != nil {
			return err
		}

		vidModes, _ := monitor.GetVideoModes()
		maxResolution := vidModes[len(vidModes)-1]
		width = maxResolution.Width
		height = maxResolution.Height
	}
	window, err := glfw.CreateWindow(width, height, name, monitor, nil)
	if err != nil {
		return err
	}

	if fullscreen {
		window.SetInputMode(glfw.Cursor, glfw.CursorDisabled)
	}

	start := time.Now()
	last := start
	doSimulation := func() {
		now := time.Now()
		gameTime := GameTime{
			now,
			now.Sub(start),
			now.Sub(last),
		}
		delegate.Simulate(gameTime)
		last = now
	}

	bindEvents(window, &IdleSimulatorWindowDelegator{
		WindowDelegator{delegate},
		doSimulation,
	})
	window.MakeContextCurrent()
	glfw.SwapInterval(1)
	delegate.Init(window)
	frameWidth, frameHeight := window.GetFramebufferSize()
	delegate.Reshape(window, frameWidth, frameHeight)
	for !window.ShouldClose() {
		doSimulation()
		if delegate.NeedsRender() {
			delegate.Draw(window)
		}
		window.SwapBuffers()
		if delegate.IsIdle() {
			glfw.WaitEvents()
		} else {
			glfw.PollEvents()
		}
	}
	return nil
}