Esempio n. 1
0
// GetShaderInfoLog returns the information log for a shader.
//
// http://www.khronos.org/opengles/sdk/docs/man3/html/glGetShaderInfoLog.xhtml
func GetShaderInfoLog(s Shader) string {
	var logLength int32
	gl.GetShaderiv(s.Value, gl.INFO_LOG_LENGTH, &logLength)
	if logLength == 0 {
		return ""
	}

	logBuffer := make([]uint8, logLength)
	gl.GetShaderInfoLog(s.Value, logLength, nil, &logBuffer[0])
	return gl.GoStr(&logBuffer[0])
}
Esempio n. 2
0
// GetProgramInfoLog returns the information log for a program.
//
// http://www.khronos.org/opengles/sdk/docs/man3/html/glGetProgramInfoLog.xhtml
func GetProgramInfoLog(p Program) string {
	var logLength int32
	gl.GetProgramiv(p.Value, gl.INFO_LOG_LENGTH, &logLength)
	if logLength == 0 {
		return ""
	}

	logBuffer := make([]uint8, logLength)
	gl.GetProgramInfoLog(p.Value, logLength, nil, &logBuffer[0])
	return gl.GoStr(&logBuffer[0])
}
Esempio n. 3
0
// GetString reports current GL state.
//
// Valid name values:
//	EXTENSIONS
//	RENDERER
//	SHADING_LANGUAGE_VERSION
//	VENDOR
//	VERSION
//
// http://www.khronos.org/opengles/sdk/docs/man3/html/glGetString.xhtml
func GetString(pname Enum) string {
	return gl.GoStr(gl.GetString(uint32(pname)))
}
Esempio n. 4
0
func main() {
	fmt.Printf(`WolfenGo v%s, Copyright (C) 2016 gdm85
https://github.com/gdm85/wolfengo
WolfenGo comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under GNU/GPLv2 license.`+"\n", version)

	if err := glfw.Init(); err != nil {
		fatalError(err)
	}
	defer glfw.Terminate()

	// create a context and activate it
	glfw.WindowHint(glfw.ContextVersionMajor, 2)
	glfw.WindowHint(glfw.ContextVersionMinor, 1)
	glfw.WindowHint(glfw.Resizable, glfw.False)
	glfw.WindowHint(glfw.DoubleBuffer, glfw.True)
	var err error
	Window, err = glfw.CreateWindow(800, 600, "WolfenGo", nil, nil)
	if err != nil {
		fatalError(err)
	}
	Window.MakeContextCurrent()

	// gl.Init() should be called after context is current
	if err := gl.Init(); err != nil {
		fatalError(err)
	}

	glfw.SwapInterval(1) // enable vertical refresh

	fmt.Println(gl.GoStr(gl.GetString(gl.VERSION)))

	if debugGL {
		gl.DebugMessageCallback(debugCb, unsafe.Pointer(nil))
		gl.Enable(gl.DEBUG_OUTPUT)
	}

	// init graphics
	gl.ClearColor(0.0, 0.0, 0.0, 0.0)

	gl.FrontFace(gl.CW)
	gl.CullFace(gl.BACK)
	gl.Enable(gl.CULL_FACE)
	gl.Enable(gl.DEPTH_TEST)

	gl.Enable(gl.BLEND)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)

	gl.Enable(gl.DEPTH_CLAMP)
	gl.Enable(gl.TEXTURE_2D)

	isRunning := false

	// load all assets
	_, err = getBasicShader()
	if err != nil {
		fatalError(err)
	}
	err = _defaultMedkit.initMedkit()
	if err != nil {
		fatalError(err)
	}
	err = _defaultMonster.initMonster()
	if err != nil {
		fatalError(err)
	}
	getDoorMesh()
	initPlayer()
	err = initGun()
	if err != nil {
		fatalError(err)
	}

	G, err = NewGame()
	if err != nil {
		fatalError(err)
	}

	// GAME STARTS
	isRunning = true

	var frames uint64
	var frameCounter time.Duration

	frameTime := time.Duration(1000/frameCap) * time.Millisecond
	lastTime := time.Now()
	var unprocessedTime time.Duration

	for isRunning {
		var render bool

		startTime := time.Now()
		passedTime := startTime.Sub(lastTime)
		lastTime = startTime

		unprocessedTime += passedTime
		frameCounter += passedTime

		for unprocessedTime > frameTime {
			render = true

			unprocessedTime -= frameTime
			if Window.ShouldClose() {
				isRunning = false
			}
			G.timeDelta = frameTime.Seconds()

			glfw.PollEvents()
			err := G.input()
			if err != nil {
				fatalError(err)
			}
			err = G.update()
			if err != nil {
				fatalError(err)
			}

			if frameCounter >= time.Second {
				if printFPS {
					fmt.Printf("%d FPS\n", frames)
				}
				frames = 0
				frameCounter -= time.Second
			}
		}

		if render {
			//TODO: Stencil Buffer
			gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)

			G.render()
			frames++
			Window.SwapBuffers()
		} else {
			time.Sleep(time.Millisecond)
		}
	}

	Window.Destroy()
}
Esempio n. 5
0
// GoStr takes a null-terminated string returned by OpenGL and constructs a
// corresponding Go string.
func (c *Context) GoStr(cstr *uint8) string {
	return gl.GoStr(cstr)
}
Esempio n. 6
0
func (w *Window) Show() {
	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.Resizable, glfw.True)

	window, err := glfw.CreateWindow(w.W, w.H, w.Title, nil, nil)
	if err != nil {
		panic(err)
	}

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

	window.MakeContextCurrent()

	if len(w.Icon) == 1 {
		window.SetIcon(w.Icon)

	}

	if w.RestrictionsEnabled {
		window.SetSizeLimits(w.MinW, w.MinH, w.MaxW, w.MaxH)
	}

	if w.Blend {
		gl.Enable(gl.BLEND)
		gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
	} else if w.Smooth {
		gl.Enable(gl.SMOOTH)
		gl.Enable(gl.POINT_SMOOTH)
		gl.Enable(gl.LINE_SMOOTH)
		gl.Enable(gl.POLYGON_SMOOTH)
		gl.Enable(gl.RELATIVE_SMOOTH_CUBIC_CURVE_TO_NV)
		gl.Enable(gl.RELATIVE_SMOOTH_QUADRATIC_CURVE_TO_NV)
	} else if w.Multisample {
		gl.Enable(gl.MULTISAMPLE)
	}

	reshape(w.W, w.H)
	window.SetRefreshCallback(func(g *glfw.Window) { reshape(w.W, w.H) })
	window.SetSizeCallback(func(g *glfw.Window, width int, height int) {
		reshape(width, height)
		w.W = width
		w.H = height
	})
	version := gl.GoStr(gl.GetString(gl.VERSION))
	if !silentMode {
		fmt.Println("OpenGL version", version)
	}

	for !window.ShouldClose() {
		//Reset?
		gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)

		if w.DrawFunc != nil {
			w.DrawFunc()
		}
		// Maintenance
		gl.Flush()
		window.SwapBuffers()
		glfw.PollEvents()
	}
}