-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Allen Ray
committed
Jul 30, 2020
1 parent
4b409b5
commit edc0f4f
Showing
3 changed files
with
217 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package pixelutils | ||
|
||
import "sync" | ||
|
||
type IDGen struct { | ||
nextID int | ||
idLock sync.Mutex | ||
} | ||
|
||
func NewIDGen() *IDGen { | ||
return &IDGen{} | ||
} | ||
|
||
func (gen *IDGen) Gen() int { | ||
gen.idLock.Lock() | ||
defer gen.idLock.Unlock() | ||
|
||
id := gen.nextID | ||
gen.nextID++ | ||
|
||
return id | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package pixelutils | ||
|
||
import "time" | ||
|
||
type Ticker struct { | ||
frames int | ||
frametime float64 | ||
last time.Time | ||
deltat float64 | ||
framerate float64 | ||
targetFPS *time.Ticker | ||
targetFrametime time.Duration | ||
|
||
totalFrames int64 | ||
totalFrametime float64 | ||
avgFramerate float64 | ||
prevFPS []float32 | ||
prevFPSOffset int | ||
} | ||
|
||
const ( | ||
MAX_PREV_FPS = 500 | ||
) | ||
|
||
func NewTicker(targetFPS int64) *Ticker { | ||
ticker := &Ticker{ | ||
last: time.Now(), | ||
} | ||
|
||
ticker.SetTargetFPS(targetFPS) | ||
ticker.prevFPS = make([]float32, MAX_PREV_FPS) | ||
|
||
return ticker | ||
} | ||
|
||
func (this *Ticker) SetTargetFPS(target int64) { | ||
this.targetFrametime = time.Second / time.Duration(target) | ||
this.targetFPS = time.NewTicker(this.targetFrametime) | ||
} | ||
|
||
func (this *Ticker) Tick() (deltat, framerate float64) { | ||
this.deltat = time.Since(this.last).Seconds() | ||
this.last = time.Now() | ||
|
||
if this.frametime >= 1 { | ||
this.totalFrames += int64(this.frames) | ||
this.totalFrametime += this.frametime | ||
|
||
this.frametime = 0 | ||
this.frames = 0 | ||
} else { | ||
this.frames++ | ||
this.frametime += this.deltat | ||
} | ||
|
||
if this.frametime == 0 { | ||
this.avgFramerate = float64(this.totalFrames) / this.totalFrametime | ||
this.framerate = this.avgFramerate | ||
} else { | ||
this.framerate = float64(this.frames) / this.frametime | ||
} | ||
|
||
if this.prevFPSOffset == MAX_PREV_FPS { | ||
this.prevFPSOffset = 0 | ||
} | ||
|
||
this.prevFPS[this.prevFPSOffset] = float32(this.framerate) | ||
this.prevFPSOffset++ | ||
|
||
return this.deltat, this.framerate | ||
} | ||
|
||
func (this *Ticker) Reset() { | ||
this.last = time.Now() | ||
} | ||
|
||
func (this *Ticker) Wait() { | ||
<-this.targetFPS.C | ||
} |