Exemplo n.º 1
0
func draw() {
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

	glPushMatrix()
	glRotatef(view_rotx, 1.0, 0.0, 0.0)
	glRotatef(view_roty, 0.0, 1.0, 0.0)
	glRotatef(view_rotz, 0.0, 0.0, 1.0)

	glPushMatrix()
	glTranslatef(-3.0, -2.0, 0.0)
	glRotatef(angle, 0.0, 0.0, 1.0)
	glCallList(gear1)
	glPopMatrix()

	glPushMatrix()
	glTranslatef(3.1, -2.0, 0.0)
	glRotatef(-2.0*angle-9.0, 0.0, 0.0, 1.0)
	glCallList(gear2)
	glPopMatrix()

	glPushMatrix()
	glTranslatef(-3.1, 4.2, 0.0)
	glRotatef(-2.0*angle-25.0, 0.0, 0.0, 1.0)
	glCallList(gear3)
	glPopMatrix()

	glPopMatrix()

	sdl.GL_SwapBuffers()

	frames++
	{
		t := sdl.GetTicks()
		if t-t0 >= 5000 {
			seconds := (t - t0) / 1000.0
			fps := frames / seconds
			printf("%d frames in %g seconds = %g FPS\n", frames, seconds, fps)
			t0 = t
			frames = 0
		}
	}
}
Exemplo n.º 2
0
Arquivo: gotris.go Projeto: 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
		}
	}
}