示例#1
0
文件: game.go 项目: genbattle/Go2D
//Internal initalization (executes when game starts)
func (game *Game) initialize() {
	// Initialize SDL
	err := sdl.Init(sdl.INIT_VIDEO)
	if err != nil {
		fmt.Println(err.Error())
	}
	//Create the window
	game.window, err = sdl.CreateWindow(game.title, sdl.WINDOWPOS_CENTERED, sdl.WINDOWPOS_CENTERED, game.width, game.height, sdl.WINDOW_SHOWN|sdl.WINDOW_OPENGL)
	if err != nil {
		panic(fmt.Sprintf("Go2D Error: Creating window: %s", err.Error()))
	}

	//Create the renderer

	//Find our available renderers
	openglIndex := 0
	d3dIndex := -1
	numRenderers := sdl.GetNumRenderDrivers()
	for i := 0; i < numRenderers; i++ {
		rendererName := sdl.GetRenderDriverName(i)
		if rendererName == "opengl" {
			openglIndex = i
		} else if rendererName == "direct3d" {
			d3dIndex = i
		}
	}

	//Default renderer is OpenGL
	rendererIndex := openglIndex

	//If we want to use Direct3D and we found it, use it
	if game.d3d && d3dIndex != -1 {
		rendererIndex = d3dIndex
	}

	var errString string
	game.renderer, errString = sdl.CreateRenderer(game.window, rendererIndex)
	if errString != "" {
		panic(fmt.Sprintf("Go2D Error: Creating renderer: %s", errString))
	}

	//initialize font rendering
	sdl.InitTTF()

	//Call the user-defined init function
	if game.initFun != nil {
		game.initFun()
	}

	g_running = true
}
示例#2
0
func main() {
	fmt.Println("OpenGL Test!!!")
	// Init SDL
	err := sdl.Init(sdl.INIT_VIDEO)
	if err != nil {
		fmt.Println("Init error:", err.Error())
		Quit(1)
	}
	// Init OpenGL
	sdl.GL_SetAttribute(sdl.GL_CONTEXT_MAJOR_VERSION, 3)
	sdl.GL_SetAttribute(sdl.GL_CONTEXT_MINOR_VERSION, 3)
	sdl.GL_SetAttribute(sdl.GL_DOUBLEBUFFER, 1)
	sdl.GL_SetAttribute(sdl.GL_DEPTH_SIZE, 24)

	// Create SDL Window
	window, err := sdl.CreateWindow("OpenGL Test Nick", sdl.WINDOWPOS_CENTRED, sdl.WINDOWPOS_CENTRED, 512, 512, sdl.WINDOW_OPENGL|sdl.WINDOW_SHOWN)
	if err != nil {
		fmt.Println("Error while creating window:", err.Error())
		Quit(2)
	}

	context := sdl.GL_CreateContext(window)

	err = sdl.GL_SetSwapInterval(1)
	if err != nil {
		fmt.Println("Error while setting swap interval:", err.Error())
		Quit(3)
	}

	//show red
	gl.ClearColor(1.0, 0.0, 0.0, 1.0)
	gl.Clear(gl.COLOR_BUFFER_BIT)
	sdl.GL_SwapWindow(window)
	sdl.Delay(2000)
	//show green
	gl.ClearColor(0.0, 1.0, 0.0, 1.0)
	gl.Clear(gl.COLOR_BUFFER_BIT)
	sdl.GL_SwapWindow(window)
	sdl.Delay(2000)
	//show blue
	gl.ClearColor(0.0, 0.0, 1.0, 1.0)
	gl.Clear(gl.COLOR_BUFFER_BIT)
	sdl.GL_SwapWindow(window)
	sdl.Delay(2000)

	sdl.GL_DeleteContext(context)
	sdl.DestroyWindow(window)
	Quit(0)
}