Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 6 additions & 35 deletions examples/new-api/systems/render-overlay.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"gomp/stdcomponents"
"gomp/vectors"
"image/color"
"slices"
"time"
)

Expand Down Expand Up @@ -65,7 +66,7 @@
fpsSampleSum int
fpsSampleIdx int
avgFPS float64
percentileFPS int
lowestFps int
lastFrameDuration time.Duration
msHistory []float64 // ms per frame, ring buffer
msHistoryIdx int
Expand Down Expand Up @@ -135,7 +136,7 @@
s.avgFPS = float64(s.fpsSampleSum) / float64(len(s.fpsSamples))

// Calculate 1% FPS (lowest 1% frame in the sample window)
s.percentileFPS = s.calcPercentileFPS(0.01)
s.lowestFps = slices.Min(s.fpsSamples)

// Update frame time history (ms) on every frame
// Use average of last two frames for smoother graph
Expand Down Expand Up @@ -316,7 +317,7 @@
return true
}

// Draws FPS stats: 1% low, current frame, average, and current FPS
// Draws FPS stats: low, current frame, average, and current FPS
func (s *RenderOverlaySystem) drawCustomFPS(x, y int32) {
fps := int32(s.currentFPS)

Expand All @@ -327,7 +328,7 @@
}

avgFPS := int32(s.avgFPS)
percentileFPS := int32(s.percentileFPS)
percentileFPS := int32(s.lowestFps)

Check failure

Code scanning / gosec

integer overflow conversion uint64 -> uint32 Error

integer overflow conversion int -> int32

// Colors
fontColor := rl.Lime
Expand All @@ -349,7 +350,7 @@
rl.DrawText(fmt.Sprintf("FPS: %d", fps), x, y, fontSize, fontColor)
rl.DrawText(fmt.Sprintf("Frame: %.2f ms", frameTimeMs), x, y+fontSize, fontSize, frameTimeColor)
rl.DrawText(fmt.Sprintf("Avg %d: %d", fpsAvgSamples, avgFPS), x, y+fontSize*2, fontSize, fontColor)
rl.DrawText(fmt.Sprintf("1%% Low: %d", percentileFPS), x, y+fontSize*3, fontSize, fontColor)
rl.DrawText(fmt.Sprintf("Low: %d", percentileFPS), x, y+fontSize*3, fontSize, fontColor)

// Draw ms graph
s.drawMsGraph(x+180, y)
Expand Down Expand Up @@ -447,36 +448,6 @@
)
}

// Calculates the given percentile FPS (e.g., 0.01 for 1% low)
func (s *RenderOverlaySystem) calcPercentileFPS(percentile float64) int {
n := len(s.fpsSamples)
if n == 0 {
return 0
}
// Copy and sort samples
sorted := make([]int, n)
copy(sorted, s.fpsSamples)
for i := 1; i < n; i++ {
key := sorted[i]
j := i - 1
for j >= 0 && sorted[j] > key {
sorted[j+1] = sorted[j]
j--
}
sorted[j+1] = key
}

// For 1% low, we want the 1st percentile (lowest values)
idx := int(float64(n) * percentile)
if idx < 0 {
idx = 0
}
if idx >= n {
idx = n - 1
}
return sorted[idx]
}

func (s *RenderOverlaySystem) intersects(rect1, rect2 vectors.Rectangle) bool {
return rect1.X < rect2.X+rect2.Width &&
rect1.X+rect1.Width > rect2.X &&
Expand Down
Loading