// 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 }
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 }