Exemple #1
0
func (c *runContext) render(g GraphicsContext) error {
	fps := c.fps
	n := now()
	defer func() {
		// Calc the current FPS.
		if time.Second > time.Duration(n-c.lastFPSUpdated) {
			return
		}
		currentFPS := float64(c.frames) * float64(time.Second) / float64(n-c.lastFPSUpdated)
		c.updateFPS(currentFPS)
		c.lastFPSUpdated = n
		c.frames = 0
	}()

	// If lastUpdated is too old, we assume that screen is not shown.
	if 10*int64(time.Second)/int64(fps) < n-c.lastUpdated {
		c.lastUpdated = n
		return nil
	}

	// Note that generally t is a little different from 1/60[sec].
	t := n - c.lastUpdated
	tt := int(t * int64(fps) / int64(time.Second))

	// As t is not accurate 1/60[sec], errors are accumulated.
	// To make the FPS stable, set tt 1 if t is a little less than 1/60[sec].
	if tt == 0 && (int64(time.Second)/int64(fps)-int64(5*time.Millisecond)) < t {
		tt = 1
	}
	if err := g.UpdateAndDraw(ui.GLContext(), tt); err != nil {
		return err
	}
	c.lastUpdated += int64(tt) * int64(time.Second) / int64(fps)
	c.frames++
	return nil
}
func (c *graphicsContext) GLContext() *opengl.Context {
	if atomic.LoadInt32(&c.initialized) == 0 {
		return nil
	}
	return ui.GLContext()
}