Exemplo n.º 1
0
func Init() *MissionControl {
	mc := new(MissionControl)

	var err error

	// Create a new window with a default size
	mc.sdl_window, err = sdl.CreateWindow("Mission Control", sdl.WINDOWPOS_CENTERED, sdl.WINDOWPOS_CENTERED,
		640, 480, sdl.WINDOW_SHOWN)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to create window: %s\n", err)
		os.Exit(1)
	}

	// Set window to fullscreen
	var disp_rect sdl.Rect
	sdl.GetDisplayBounds(mc.Display, &disp_rect)
	mc.sdl_window.SetPosition(int(disp_rect.X), int(disp_rect.Y))
	mc.sdl_window.SetSize(int(disp_rect.W), int(disp_rect.H))
	mc.sdl_window.SetFullscreen(sdl.WINDOW_FULLSCREEN)

	mc.renderer, err = sdl.CreateRenderer(mc.sdl_window, -1, sdl.RENDERER_ACCELERATED)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to create renderer: %s\n", err)
		os.Exit(2)
	}
	mc.renderer.Clear()

	mc.SetBackgroundColor(0, 0, 0, 255)

	return mc
}
Exemplo n.º 2
0
// NewWindow will create a new sdl window with a gl context. It will also destroy
// an old one if there is one already. Errors returned come straight from SDL so
// errors will be indicitive of SDL errors
func NewWindow() (*window, error) {
	if current_window != nil || initError != nil {
		return current_window, initError
	}

	var config *windowConfig
	var err error

	if err = sdl.InitSubSystem(sdl.INIT_VIDEO); err != nil {
		panic(err)
	}

	if config, err = loadConfig(); err != nil {
		panic(err)
	}

	config.Minwidth = int32(math.Max(float64(config.Minwidth), 1.0))
	config.Minheight = int32(math.Max(float64(config.Minheight), 1.0))
	config.Display = int(math.Min(math.Max(float64(config.Display), 0.0), float64(GetDisplayCount()-1)))

	if config.Width == 0 || config.Height == 0 {
		var mode sdl.DisplayMode
		sdl.GetDesktopDisplayMode(config.Display, &mode)
		config.Width = mode.W
		config.Height = mode.H
	}

	sdlflags := uint32(sdl.WINDOW_OPENGL)

	if config.Fullscreen {
		if config.Fstype == "desktop" {
			sdlflags |= sdl.WINDOW_FULLSCREEN_DESKTOP
		} else {
			sdlflags |= sdl.WINDOW_FULLSCREEN

			mode := sdl.DisplayMode{W: int32(config.Width), H: int32(config.Height)}

			// Fullscreen window creation will bug out if no mode can be used.
			if _, err := sdl.GetClosestDisplayMode(config.Display, &mode, &mode); err != nil {
				// GetClosestDisplayMode will fail if we request a size larger
				// than the largest available display mode, so we'll try to use
				// the largest (first) mode in that case.
				if err := sdl.GetDisplayMode(config.Display, 0, &mode); err != nil {
					return nil, err
				}
			}

			config.Width = mode.W
			config.Height = mode.H
		}
	}

	if config.Resizable {
		sdlflags |= sdl.WINDOW_RESIZABLE
	}

	if config.Borderless {
		sdlflags |= sdl.WINDOW_BORDERLESS
	}

	if config.Highdpi {
		sdlflags |= sdl.WINDOW_ALLOW_HIGHDPI
	}

	if config.Fullscreen {
		// The position needs to be in the global coordinate space.
		var displaybounds sdl.Rect
		sdl.GetDisplayBounds(config.Display, &displaybounds)
		config.X += displaybounds.X
		config.Y += displaybounds.Y
	} else {
		if config.Centered {
			config.X = sdl.WINDOWPOS_CENTERED
			config.Y = sdl.WINDOWPOS_CENTERED
		} else {
			config.X = sdl.WINDOWPOS_UNDEFINED
			config.Y = sdl.WINDOWPOS_UNDEFINED
		}
	}

	if current_window != nil {
		Destroy()
	}

	created = false
	new_window, err := createWindowAndContext(config, sdlflags)
	if err != nil {
		return nil, err
	}
	created = true

	if new_window.Config.Icon != "" {
		SetIcon(config.Icon)
	}

	SetMouseGrab(false)
	SetMinimumSize(config.Minwidth, config.Minheight)
	SetTitle(config.Title)

	if config.Centered && !config.Fullscreen {
		SetPosition(config.X, config.Y)
	}

	getCurrent().sdl_window.Raise()

	if config.Vsync {
		sdl.GL_SetSwapInterval(1)
	} else {
		sdl.GL_SetSwapInterval(0)
	}

	new_window.open = true
	return new_window, nil
}