Пример #1
0
func CreateRenderer(windowWidth, windowHeight int) *Renderer {

	if err := glfw.Init(); err != nil {
		log.Fatalln("failed to initialize glfw:", err)
	}
	// defer glfw.Terminate()

	glfw.WindowHint(glfw.Resizable, glfw.False)
	glfw.WindowHint(glfw.ContextVersionMajor, 4)
	glfw.WindowHint(glfw.ContextVersionMinor, 1)
	glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
	glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)
	if windowWidth == 0 && windowHeight == 0 {
		windowWidth, windowHeight = glfw.GetPrimaryMonitor().GetPhysicalSize()
	}
	fmt.Println("Window Width -", windowWidth, "Window Height -", windowHeight)
	window, err := glfw.CreateWindow(windowWidth, windowHeight, "Karma", glfw.GetPrimaryMonitor(), nil)
	if err != nil {
		panic(err)
	}
	window.MakeContextCurrent()
	window.SetKeyCallback(testCallback)

	// Initialize Glow
	if err := gl.Init(); err != nil {
		panic(err)
	}

	version := gl.GoStr(gl.GetString(gl.VERSION))
	fmt.Println("OpenGL version", version)

	shaders := Shaders{}

	textureFlatUniforms := []string{"projection", "camera", "modelView", "tex"}
	textureFlatAttributes := []string{"vert", "vertTexCoord"}

	fmt.Println(textureFlatUniforms)
	fmt.Println(textureFlatAttributes)

	shader, err := createProgram("./assets/shaders/texture_flat.vs", "./assets/shaders/texture_flat.fs", textureFlatUniforms, textureFlatAttributes)
	if err != nil {
		panic(err)
	}
	shaders.textureFlat = shader

	meshes := []*Mesh{}

	previousTime := glfw.GetTime()

	return &Renderer{
		PreviousTime: previousTime,
		Window:       window,
		Shaders:      &shaders,
		Meshes:       meshes,
	}
}
Пример #2
0
func (u *userInterface) start(width, height, scale int, title string) (actualScale int, err error) {
	videoMode := glfw.GetPrimaryMonitor().GetVideoMode()
	x := (videoMode.Width - width*scale) / 2
	y := (videoMode.Height - height*scale) / 3

	u.setScreenSize(width, height, scale)
	u.window.SetTitle(title)
	u.window.SetPos(x, y)
	u.window.Show()

	return u.actualScale, nil
}
Пример #3
0
//CreateWindow creates a new glfw window. If fullscreen will place the screen on primary monitor.
func CreateWindow(width, height int, title string, fullscreen bool) (window *glfw.Window) {
	SetContext()
	var x *glfw.Monitor
	if fullscreen {
		x = glfw.GetPrimaryMonitor()
	}
	window, err := glfw.CreateWindow(width, height, title, x, nil)
	if err != nil {
		panic(err)
	}
	window.MakeContextCurrent()
	letsglowLETSGLOW()
	glbs()
	QueryExtentions()
	return
}
Пример #4
0
func (c *Context) createWindow() (err error) {
	var (
		monitor *glfw.Monitor = nil
	)
	if c.window != nil {
		win := c.window
		c.window = nil
		win.Destroy()
	}
	if c.fullscreen == true {
		monitor = glfw.GetPrimaryMonitor()
	}
	if c.window, err = glfw.CreateWindow(c.w, c.h, c.name, monitor, nil); err != nil {
		return
	}
	c.Events = newEvents(c.window)
	c.window.MakeContextCurrent()
	return
}
Пример #5
0
func (u *userInterface) Start(width, height, scale int, title string) error {
	var err error
	u.runOnMainThread(func() {
		m := glfw.GetPrimaryMonitor()
		v := m.GetVideoMode()
		u.deviceScale = deviceScale()
		u.framebufferScale = 1

		if !u.setScreenSize(width, height, scale) {
			err = errors.New("ui: Fail to set the screen size")
			return
		}
		u.window.SetTitle(title)
		u.window.Show()

		x := (v.Width - width*u.windowScale()) / 2
		y := (v.Height - height*u.windowScale()) / 3
		u.window.SetPos(x, y)
	})
	return err
}
Пример #6
0
func (c *Context) CreateWindow(w, h int, name string) (err error) {
	c.w = w
	c.h = h
	c.name = name
	var monitor *glfw.Monitor
	if c.fullscreen == true {
		monitor = glfw.GetPrimaryMonitor()
	}
	if c.window, err = glfw.CreateWindow(c.w, c.h, c.name, monitor, nil); err != nil {
		return
	}
	if c.cursor == false {
		c.window.SetInputMode(glfw.CursorMode, glfw.CursorHidden)
	}
	c.window.MakeContextCurrent()
	gl.Init()
	if e := gl.GetError(); e != 0 {
		if e == gl.INVALID_ENUM {
			fmt.Printf("GL_INVALID_ENUM when calling glInit\n")
		} else {
			err = fmt.Errorf("OpenGL glInit error: %X\n", e)
			return
		}
	}
	c.OpenGLVersion = glfw.GetVersionString()
	c.ShaderVersion = gl.GoStr(gl.GetString(gl.SHADING_LANGUAGE_VERSION))
	gl.Enable(gl.BLEND)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
	gl.ClearColor(0.0, 0.0, 0.0, 1.0)
	gl.Disable(gl.CULL_FACE)
	glfw.SwapInterval(1)
	if c.VAO, err = CreateVAO(); err != nil {
		return
	}
	gl.BindVertexArray(c.VAO)
	c.Events = NewEventHandler(c.window)
	return
}
Пример #7
0
func InitDisplay(title string) error {
	defer tlog.FuncLog(tlog.Func("InitDisplay"))

	runtime.LockOSThread()

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

	monitor := glfw.GetPrimaryMonitor()
	mode := monitor.GetVideoMode()
	tlog.Println("monitor mode", mode)

	if DEBUG {
		glfw.WindowHint(glfw.OpenGLDebugContext, gl.TRUE)
	}

	glfw.WindowHint(glfw.ContextVersionMajor, 3)
	glfw.WindowHint(glfw.ContextVersionMinor, 3)
	glfw.WindowHint(glfw.OpenGLForwardCompatible, 1)
	glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)

	window, err = glfw.CreateWindow(mode.Width, mode.Height, title, nil, nil)
	if err != nil {
		return err
	}

	window.SetKeyCallback(onKey)
	// glfw.SetMouseButtonCallback(onMouseBtn)

	window.MakeContextCurrent()
	glfw.SwapInterval(1)

	return nil
}
Пример #8
0
func CreateWindow(title string, width, height int, fullscreen bool) {
	err := glfw.Init()
	fatalErr(err)

	Arrow = glfw.CreateStandardCursor(int(glfw.ArrowCursor))
	Hand = glfw.CreateStandardCursor(int(glfw.HandCursor))
	IBeam = glfw.CreateStandardCursor(int(glfw.IBeamCursor))
	Crosshair = glfw.CreateStandardCursor(int(glfw.CrosshairCursor))

	monitor := glfw.GetPrimaryMonitor()
	mode := monitor.GetVideoMode()

	gameWidth = float32(width)
	gameHeight = float32(height)

	if fullscreen {
		width = mode.Width
		height = mode.Height
		glfw.WindowHint(glfw.Decorated, 0)
	} else {
		monitor = nil
	}

	glfw.WindowHint(glfw.ContextVersionMajor, 2)
	glfw.WindowHint(glfw.ContextVersionMinor, 1)

	window, err = glfw.CreateWindow(width, height, title, nil, nil)
	fatalErr(err)

	window.MakeContextCurrent()

	if !fullscreen {
		window.SetPos((mode.Width-width)/2, (mode.Height-height)/2)
	}

	width, height = window.GetFramebufferSize()
	windowWidth, windowHeight = float32(width), float32(height)

	SetVSync(vsync)

	Gl = webgl.NewContext()
	Gl.Viewport(0, 0, width, height)

	window.SetFramebufferSizeCallback(func(window *glfw.Window, w, h int) {
		width, height = window.GetFramebufferSize()
		Gl.Viewport(0, 0, width, height)

		// TODO: when do we want to handle resizing? and who should deal with it?
		// responder.Resize(w, h)
	})

	window.SetCursorPosCallback(func(window *glfw.Window, x, y float64) {
		Mouse.X, Mouse.Y = float32(x), float32(y)
		Mouse.Action = MOVE
	})

	window.SetMouseButtonCallback(func(window *glfw.Window, b glfw.MouseButton, a glfw.Action, m glfw.ModifierKey) {
		x, y := window.GetCursorPos()
		Mouse.X, Mouse.Y = float32(x), float32(y)
		// this is only valid because we use an internal structure that is
		// 100% compatible with glfw3.h
		Mouse.Button = MouseButton(b)
		Mouse.Modifer = Modifier(m)

		if a == glfw.Press {
			Mouse.Action = PRESS
		} else {
			Mouse.Action = RELEASE
		}
	})

	window.SetScrollCallback(func(window *glfw.Window, xoff, yoff float64) {
		Mouse.ScrollX = float32(xoff)
		Mouse.ScrollY = float32(yoff)
	})

	window.SetKeyCallback(func(window *glfw.Window, k glfw.Key, s int, a glfw.Action, m glfw.ModifierKey) {
		key := Key(k)
		if a == glfw.Press {
			keyStates[key] = true
		} else if a == glfw.Release {
			keyStates[key] = false
		}
	})

	window.SetSizeCallback(func(w *glfw.Window, widthInt int, heightInt int) {
		windowWidth = float32(widthInt)
		windowHeight = float32(heightInt)

		if !scaleOnResize {
			gameWidth, gameHeight = float32(widthInt), float32(heightInt)

			// Update default batch
			for _, scene := range scenes {
				if scene.world == nil {
					continue // with other scenes
				}

				for _, s := range scene.world.Systems() {
					if _, ok := s.(*RenderSystem); ok {
						Shaders.def.SetProjection(gameWidth, gameHeight)
					}
				}
			}
		}

		// Update HUD batch
		for _, scene := range scenes {
			if scene.world == nil {
				continue // with other scenes
			}

			for _, s := range scene.world.Systems() {
				if _, ok := s.(*RenderSystem); ok {
					// TODO: don't call it directly, but let HUD listen for it
					//Shaders.HUD.SetProjection(windowWidth, windowHeight)
				}
			}
		}
	})

	window.SetCharCallback(func(window *glfw.Window, char rune) {
		// TODO: what does this do, when can we use it?
		// it's like KeyCallback, but for specific characters instead of keys...?
		// responder.Type(char)
	})
}
Пример #9
0
func run(title string, width, height int, fullscreen bool) {
	defer runtime.UnlockOSThread()
	runtime.GOMAXPROCS(runtime.NumCPU())
	fatalErr(glfw.Init())

	monitor := glfw.GetPrimaryMonitor()
	mode := monitor.GetVideoMode()

	// ideally we want a "windowless full screen"
	// but using normal full screen and disabling minimizing on OS X (which is broken)
	// is the best we can do for now...
	if fullscreen {
		if runtime.GOOS == "darwin" {
			glfw.WindowHint(glfw.AutoIconify, glfw.False)
		}
		glfw.WindowHint(glfw.Decorated, glfw.False)
	} else {
		monitor = nil
	}

	glfw.WindowHint(glfw.ContextVersionMajor, 2)
	glfw.WindowHint(glfw.ContextVersionMinor, 1)

	var err error
	window, err = glfw.CreateWindow(width, height, title, monitor, nil)
	fatalErr(err)
	window.MakeContextCurrent()

	if !fullscreen {
		window.SetPos((mode.Width-width)/2, (mode.Height-height)/2)
	}

	width, height = window.GetFramebufferSize()

	glfw.SwapInterval(1)

	gl = webgl.NewContext()

	gl.Viewport(0, 0, width, height)
	window.SetFramebufferSizeCallback(func(window *glfw.Window, w, h int) {
		width, height = window.GetFramebufferSize()
		gl.Viewport(0, 0, width, height)
		responder.Resize(w, h)
	})

	window.SetCursorPosCallback(func(window *glfw.Window, x, y float64) {
		responder.Mouse(float32(x), float32(y), MouseMove, MOVE)
	})

	window.SetMouseButtonCallback(func(window *glfw.Window, b glfw.MouseButton, a glfw.Action, m glfw.ModifierKey) {
		x, y := window.GetCursorPos()
		if a == glfw.Press {
			responder.Mouse(float32(x), float32(y), MouseKey(b), PRESS)
		} else {
			responder.Mouse(float32(x), float32(y), MouseKey(b), RELEASE)
		}
	})

	window.SetScrollCallback(func(window *glfw.Window, xoff, yoff float64) {
		responder.Scroll(float32(yoff))
	})

	window.SetKeyCallback(func(window *glfw.Window, k glfw.Key, s int, a glfw.Action, m glfw.ModifierKey) {
		if a == glfw.Press {
			responder.Key(Key(k), Modifier(m), PRESS)
		} else if a == glfw.Release {
			responder.Key(Key(k), Modifier(m), RELEASE)
		}
	})

	window.SetCharCallback(func(window *glfw.Window, char rune) {
		responder.Type(char)
	})

	setupAudio()

	responder.Preload()
	Files.Load(func() {})
	responder.Setup()

	shouldClose := window.ShouldClose()
	for !shouldClose {
		responder.Update(Time.Delta())
		gl.Clear(gl.COLOR_BUFFER_BIT)
		responder.Render()
		window.SwapBuffers()
		glfw.PollEvents()
		Time.Tick()

		shouldClose = window.ShouldClose()
	}
	window.Destroy()
	glfw.Terminate()
	responder.Close()
}
Пример #10
0
//Start -
func (glRenderer *OpenglRenderer) Start() {
	if err := glfw.Init(); err != nil {
		log.Fatalln("failed to initialize glfw:", err)
	}
	defer glfw.Terminate()

	glfw.WindowHint(glfw.Resizable, glfw.False)
	glfw.WindowHint(glfw.ContextVersionMajor, 4)
	glfw.WindowHint(glfw.ContextVersionMinor, 1)
	glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
	glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)

	if glRenderer.FullScreen {
		glRenderer.WindowWidth = glfw.GetPrimaryMonitor().GetVideoMode().Width
	} else if glRenderer.WindowWidth == 0 {
		glRenderer.WindowWidth = glfw.GetPrimaryMonitor().GetVideoMode().Width * 95 / 100
	}
	if glRenderer.FullScreen {
		glRenderer.WindowHeight = glfw.GetPrimaryMonitor().GetVideoMode().Height
	} else if glRenderer.WindowHeight == 0 {
		glRenderer.WindowHeight = glfw.GetPrimaryMonitor().GetVideoMode().Height * 95 / 100
	}

	var monitor *glfw.Monitor
	if glRenderer.FullScreen {
		monitor = glfw.GetPrimaryMonitor()
	}
	window, err := glfw.CreateWindow(glRenderer.WindowWidth, glRenderer.WindowHeight, glRenderer.WindowTitle, monitor, nil)
	if err != nil {
		panic(err)
	}
	window.MakeContextCurrent()
	glRenderer.Window = window

	// Initialize Glow
	if err := gl.Init(); err != nil {
		panic(err)
	}

	var vao uint32
	gl.GenVertexArrays(1, &vao)
	gl.BindVertexArray(vao)

	gl.Enable(gl.TEXTURE_CUBE_MAP_SEAMLESS)
	gl.Enable(gl.BLEND)
	gl.DepthFunc(gl.LEQUAL)
	gl.CullFace(gl.BACK)

	glRenderer.initPostEffects()

	glRenderer.onInit()

	window.SetRefreshCallback(func(w *glfw.Window) {
		glRenderer.mainLoop()
		window.SwapBuffers()
	})

	//Main loop
	for !window.ShouldClose() {
		glRenderer.mainLoop()
		window.SwapBuffers()
		glfw.PollEvents()
	}
}
Пример #11
0
func run(customGame CustomGame, title string, width, height int, fullscreen bool) {

	err := glfw.Init()
	fatalErr(err)

	defer glfw.Terminate()

	Arrow = glfw.CreateStandardCursor(int(glfw.ArrowCursor))
	Hand = glfw.CreateStandardCursor(int(glfw.HandCursor))

	monitor := glfw.GetPrimaryMonitor()
	mode := monitor.GetVideoMode()

	if fullscreen {
		width = mode.Width
		height = mode.Height
		glfw.WindowHint(glfw.Decorated, 0)
	} else {
		monitor = nil
	}

	glfw.WindowHint(glfw.ContextVersionMajor, 2)
	glfw.WindowHint(glfw.ContextVersionMinor, 1)

	window, err = glfw.CreateWindow(width, height, title, nil, nil)
	fatalErr(err)

	window.MakeContextCurrent()

	if !fullscreen {
		window.SetPos((mode.Width-width)/2, (mode.Height-height)/2)
	}

	width, height = window.GetFramebufferSize()

	glfw.SwapInterval(1)

	Gl = webgl.NewContext()
	Gl.Viewport(0, 0, width, height)
	window.SetFramebufferSizeCallback(func(window *glfw.Window, w, h int) {
		width, height = window.GetFramebufferSize()
		Gl.Viewport(0, 0, width, height)
		// TODO: when do we want to handle resizing? and who should deal with it?
		// responder.Resize(w, h)
	})

	window.SetCursorPosCallback(func(window *glfw.Window, x, y float64) {
		Mouse.X, Mouse.Y = float32(x), float32(y)
		Mouse.Action = MOVE
	})

	window.SetMouseButtonCallback(func(window *glfw.Window, b glfw.MouseButton, a glfw.Action, m glfw.ModifierKey) {
		x, y := window.GetCursorPos()
		Mouse.X, Mouse.Y = float32(x), float32(y)

		if a == glfw.Press {
			Mouse.Action = PRESS
		} else {
			Mouse.Action = RELEASE
		}
	})

	window.SetScrollCallback(func(window *glfw.Window, xoff, yoff float64) {
		Mouse.ScrollX = float32(xoff)
		Mouse.ScrollY = float32(yoff)
	})

	window.SetKeyCallback(func(window *glfw.Window, k glfw.Key, s int, a glfw.Action, m glfw.ModifierKey) {
		key := Key(k)
		if a == glfw.Press {
			keyStates[key] = true
		} else if a == glfw.Release {
			keyStates[key] = false
		}
	})

	window.SetCharCallback(func(window *glfw.Window, char rune) {
		// TODO: what does this do, when can we use it?
		// it's like KeyCallback, but for specific characters instead of keys...?
		// responder.Type(char)
	})

	runLoop(customGame, false)
}
Пример #12
0
func CreateWindow(title string, width, height int, fullscreen bool, msaa int) {
	err := glfw.Init()
	fatalErr(err)

	cursorArrow = glfw.CreateStandardCursor(int(glfw.ArrowCursor))
	cursorIBeam = glfw.CreateStandardCursor(int(glfw.IBeamCursor))
	cursorCrosshair = glfw.CreateStandardCursor(int(glfw.CrosshairCursor))
	cursorHand = glfw.CreateStandardCursor(int(glfw.HandCursor))
	cursorHResize = glfw.CreateStandardCursor(int(glfw.HResizeCursor))
	cursorVResize = glfw.CreateStandardCursor(int(glfw.VResizeCursor))

	monitor := glfw.GetPrimaryMonitor()
	mode := monitor.GetVideoMode()

	gameWidth = float32(width)
	gameHeight = float32(height)

	if fullscreen {
		width = mode.Width
		height = mode.Height
		glfw.WindowHint(glfw.Decorated, 0)
	} else {
		monitor = nil
	}

	glfw.WindowHint(glfw.ContextVersionMajor, 2)
	glfw.WindowHint(glfw.ContextVersionMinor, 1)

	glfw.WindowHint(glfw.Samples, msaa)

	window, err = glfw.CreateWindow(width, height, title, nil, nil)
	fatalErr(err)

	window.MakeContextCurrent()

	if !fullscreen {
		window.SetPos((mode.Width-width)/2, (mode.Height-height)/2)
	}

	width, height = window.GetFramebufferSize()
	windowWidth, windowHeight = float32(width), float32(height)

	SetVSync(vsync)

	Gl = gl.NewContext()
	Gl.Viewport(0, 0, width, height)

	window.SetFramebufferSizeCallback(func(window *glfw.Window, w, h int) {
		width, height = window.GetFramebufferSize()
		Gl.Viewport(0, 0, width, height)

		// TODO: when do we want to handle resizing? and who should deal with it?
		// responder.Resize(w, h)
	})

	window.SetCursorPosCallback(func(window *glfw.Window, x, y float64) {
		Mouse.X, Mouse.Y = float32(x), float32(y)
		Mouse.Action = MOVE
	})

	window.SetMouseButtonCallback(func(window *glfw.Window, b glfw.MouseButton, a glfw.Action, m glfw.ModifierKey) {
		x, y := window.GetCursorPos()
		Mouse.X, Mouse.Y = float32(x), float32(y)
		// this is only valid because we use an internal structure that is
		// 100% compatible with glfw3.h
		Mouse.Button = MouseButton(b)
		Mouse.Modifer = Modifier(m)

		if a == glfw.Press {
			Mouse.Action = PRESS
		} else {
			Mouse.Action = RELEASE
		}
	})

	window.SetScrollCallback(func(window *glfw.Window, xoff, yoff float64) {
		Mouse.ScrollX = float32(xoff)
		Mouse.ScrollY = float32(yoff)
	})

	window.SetKeyCallback(func(window *glfw.Window, k glfw.Key, s int, a glfw.Action, m glfw.ModifierKey) {
		key := Key(k)
		if a == glfw.Press {
			Input.keys.Set(key, true)
		} else if a == glfw.Release {
			Input.keys.Set(key, false)
		}
	})

	window.SetSizeCallback(func(w *glfw.Window, widthInt int, heightInt int) {
		message := WindowResizeMessage{
			OldWidth:  int(windowWidth),
			OldHeight: int(windowHeight),
			NewWidth:  widthInt,
			NewHeight: heightInt,
		}

		windowWidth = float32(widthInt)
		windowHeight = float32(heightInt)

		if !scaleOnResize {
			gameWidth, gameHeight = float32(widthInt), float32(heightInt)
		}

		Mailbox.Dispatch(message)
	})

	window.SetCharCallback(func(window *glfw.Window, char rune) {
		// TODO: what does this do, when can we use it?
		// it's like KeyCallback, but for specific characters instead of keys...?
		// responder.Type(char)
	})
}