Example #1
0
func main() {
	if sdl.Init(sdl.INIT_VIDEO) < 0 {
		fmt.Printf(
			"Video initialization failed: %s\n",
			sdl.GetError())
		quit_tutorial(1)
	}
	info := sdl.GetVideoInfo()
	if info == nil {
		fmt.Printf(
			"Video query failed: %s\n",
			sdl.GetError())
		quit_tutorial(1)
	}
	width := 640
	height := 480
	bpp := int(info.Vfmt.BitsPerPixel)
	sdl.GL_SetAttribute(sdl.GL_RED_SIZE, 5)
	sdl.GL_SetAttribute(sdl.GL_GREEN_SIZE, 5)
	sdl.GL_SetAttribute(sdl.GL_BLUE_SIZE, 5)
	sdl.GL_SetAttribute(sdl.GL_DEPTH_SIZE, 16)
	sdl.GL_SetAttribute(sdl.GL_DOUBLEBUFFER, 1)
	flags := uint32(sdl.OPENGL | sdl.FULLSCREEN)
	if sdl.SetVideoMode(width, height, bpp, flags) == nil {
		fmt.Printf("Video mode set failed: %s\n", sdl.GetError())
		quit_tutorial(1)
	}
	setup_opengl(width, height)
	for {
		process_events()
		draw_screen()
	}
	os.Exit(0)
}
Example #2
0
File: gotris.go Project: nsf/gotris
func main() {
	runtime.LockOSThread()
	flag.Parse()
	sdl.Init(sdl.INIT_VIDEO)
	defer sdl.Quit()

	sdl.GL_SetAttribute(sdl.GL_SWAP_CONTROL, 1)

	if sdl.SetVideoMode(640, 480, 32, sdl.OPENGL) == nil {
		panic("sdl error")
	}

	sdl.WM_SetCaption("Gotris", "Gotris")
	sdl.EnableKeyRepeat(250, 45)

	gl.Enable(gl.TEXTURE_2D)
	gl.Enable(gl.BLEND)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
	gl.Viewport(0, 0, 640, 480)
	gl.MatrixMode(gl.PROJECTION)
	gl.LoadIdentity()
	gl.Ortho(0, 640, 480, 0, -1, 1)

	gl.ClearColor(0, 0, 0, 0)

	//-----------------------------------------------------------------------------

	font, err := LoadFontFromFile("dejavu.font")
	if err != nil {
		panic(err)
	}

	rand.Seed(int64(sdl.GetTicks()))

	gs := NewGameSession(*initLevel, font)
	lastTime := sdl.GetTicks()
	ticker := time.NewTicker(10 * time.Millisecond)

	stop := make(chan byte, 1)
	go func() {
		for {
			switch e := (<-sdl.Events).(type) {
			case sdl.QuitEvent:
				stop <- 0
			case sdl.KeyboardEvent:
				if e.Type == sdl.KEYDOWN {
					running := gs.HandleKey(e.Keysym.Sym)
					if !running {
						stop <- 0
					}
					gs.update <- 0
				}
			}
		}
	}()
loop:
	for {
		select {
		case <-ticker.C:
			gs.update <- 0

		case <-gs.update:
			now := sdl.GetTicks()
			delta := now - lastTime
			lastTime = now

			gs.Update(delta)

			gl.Clear(gl.COLOR_BUFFER_BIT)
			font.Draw(5, 5, fmt.Sprintf("Level: %d | Score: %d", gs.Level, gs.Score))
			gs.Draw()
			gl.Color3ub(255, 255, 255)
			sdl.GL_SwapBuffers()

		case <-stop:
			break loop
		}
	}
}