func (a *app) Run() { // If updates start const maxMultipleUpdates = 6 lastTime := time.Now().UnixNano() for len(a.states) > 0 { currentTime := time.Now().UnixNano() if currentTime-lastTime < a.nanosecondsPerFrame { // Avoid busy waiting and take short naps if ahead of schedule. // XXX: Is this a good thing? time.Sleep(10e6) continue } // If things get slow, tell Update multiple frames have elapsed. nUpdates := (currentTime - lastTime) / a.nanosecondsPerFrame if nUpdates < 1 { nUpdates = 1 } if nUpdates > maxMultipleUpdates { nUpdates = maxMultipleUpdates } a.TopState().Draw() gfx.BlitX2(sdl.Frame(), sdl.Video()) sdl.Flip() a.TopState().Update(nUpdates * a.nanosecondsPerFrame) lastTime += nUpdates * a.nanosecondsPerFrame } sdl.Stop() }
func (h *Hud) Draw(bounds image.Rectangle) { h.update() sdl.Frame().SetClipRect(bounds) defer sdl.Frame().ClearClipRect() style := util.TextStyle().ForeColor(gfx.Khaki).Edge(typography.Round) lines := []string{} for _, str := range h.msgs { for _, line := range typography.SplitToLines(util.TextStyle(), str, float64(bounds.Dx())) { lines = append(lines, line) } } for lineY, str := range lines { pos := bounds.Min.Add(image.Pt(0, (lineY+1)*int(style.LineHeight()))) style.Render(str, pos) } h.drawHealth(image.Rectangle{image.Pt(bounds.Min.X, bounds.Max.Y-8), bounds.Max}) }
func BenchmarkTruecolorBlit(b *testing.B) { b.StopTimer() sdl.Run(320, 240) defer sdl.Stop() surf := unpaletted() b.StartTimer() for i := 0; i < b.N; i++ { for y := 0; y < 30; y++ { for x := 0; x < 40; x++ { surf.Blit(surf.Bounds(), x*8, y*8, sdl.Frame()) } } sdl.Flip() } b.StopTimer() }
// Beam generates a projectile beam effect in the game world. func (f *Fx) Beam(origin space.Location, dir image.Point, length int, kind BeamKind) { // Make a footprint for the beam shape. shape := []image.Point{image.Pt(0, 0)} for i := 0; i < length; i++ { shape = append(shape, shape[len(shape)-1].Add(dir)) } footprint := space.FootprintFromPoints(f.world.Manifold, origin, shape) screenVec := util.ChartToScreen(shape[len(shape)-1]) // TODO: Different beam types. f.anim.Add( anim.Func(func(t int64, offset image.Point) { gfx.Line( sdl.Frame(), offset.Add(util.HalfTile), offset.Add(util.HalfTile).Add(screenVec), gfx.LerpCol(gfx.Gold, gfx.Black, float64(t)/float64(.5e9))) }), footprint, .2e9) }
func (s *Style) Render(line string, pos image.Point) { s.RenderOn(sdl.Frame(), line, pos) }
func (gs *game) Draw() { sdl.Frame().Clear(gfx.Black) gs.view.Draw(image.Rect(0, 0, 320, 240)) gs.hud.Draw(image.Rect(0, 0, 320, 240)) }