Skip to content
Merged
Show file tree
Hide file tree
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
20 changes: 11 additions & 9 deletions examples/new-api/systems/camera-main.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,17 @@
return false
})

//if rl.IsWindowResized() {
// width, height := rl.GetScreenWidth(), rl.GetScreenHeight()
// main := s.Cameras.GetUnsafe(s.mainCamera)
// main.Dst = vectors.Rectangle{X: 0, Y: 0, Width: float32(width), Height: float32(height)}
//
// mini := s.Cameras.GetUnsafe(s.minimapCamera)
// mini.Dst = vectors.Rectangle{X: float32(width) - mini.Dst.Width, Y: float32(height) - mini.Dst.Height, Width: mini.Dst.Width, Height: mini.Dst.Height}
//}

if rl.IsWindowResized() {
width, height := rl.GetScreenWidth(), rl.GetScreenHeight()
c := s.Cameras.GetUnsafe(s.mainCamera)
c.Dst = vectors.Rectangle{X: 0, Y: 0, Width: float32(width), Height: float32(height)}
c.Camera2D.Offset = rl.Vector2(vectors.Vec2{X: float32(width), Y: float32(height)}.Scale(0.5))
fb := s.FrameBuffer2D.GetUnsafe(s.mainCamera)
rl.UnloadRenderTexture(fb.Texture)
fb.Texture = rl.LoadRenderTexture(int32(width), int32(height))

Check failure

Code scanning / gosec

integer overflow conversion uint64 -> uint32 Error

integer overflow conversion int -> int32

Check failure

Code scanning / gosec

integer overflow conversion uint64 -> uint32 Error

integer overflow conversion int -> int32
fb.Frame = rl.Rectangle{X: 0, Y: 0, Width: float32(width), Height: float32(height)}
fb.Dst = rl.Rectangle{Width: float32(width), Height: float32(height)}
}
}

// TODO: check and do better
Expand Down
11 changes: 11 additions & 0 deletions examples/new-api/systems/camera-minimap.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@
c.Camera2D.Rotation = -float32(rotation.Degrees())
return false
})
if rl.IsWindowResized() {
width, height := rl.GetScreenWidth(), rl.GetScreenHeight()
c := s.Cameras.GetUnsafe(s.minimapCamera)
c.Dst = vectors.Rectangle{X: 0, Y: float32(width) - float32(height)*0.1666666666666667, Width: float32(width) * 0.1666666666666667, Height: float32(height) * 0.1666666666666667}
c.Offset = rl.Vector2(vectors.Vec2{X: float32(width), Y: float32(height)}.Scale(0.5))
fb := s.FrameBuffer2D.GetUnsafe(s.minimapCamera)
rl.UnloadRenderTexture(fb.Texture)
fb.Texture = rl.LoadRenderTexture(int32(width), int32(height))

Check failure

Code scanning / gosec

integer overflow conversion uint64 -> uint32 Error

integer overflow conversion int -> int32

Check failure

Code scanning / gosec

integer overflow conversion uint64 -> uint32 Error

integer overflow conversion int -> int32
fb.Frame = rl.NewRectangle(0, 0, float32(width), float32(height))
fb.Dst = rl.Rectangle{Y: float32(height) - float32(height)*0.1666666666666667, Width: float32(width) * 0.1666666666666667, Height: float32(height) * 0.1666666666666667}
}
return false
}

Expand Down
102 changes: 62 additions & 40 deletions examples/new-api/systems/render-overlay.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
Frame: rl.Rectangle{X: 0, Y: 0, Width: float32(s.monitorWidth), Height: float32(s.monitorHeight)},
Texture: rl.LoadRenderTexture(int32(s.monitorWidth), int32(s.monitorHeight)),
Layer: config.DebugLayer,
BlendMode: rl.BlendAlpha,
BlendMode: rl.BlendAlphaPremultiply,
Tint: rl.White,
Dst: rl.Rectangle{Width: float32(s.monitorWidth), Height: float32(s.monitorHeight)},
})
Expand Down Expand Up @@ -121,42 +121,43 @@
s.frameCount++
s.lastFrameDuration = dt

// Store current frame FPS in samples
frameFPS := 0
if dt > 0 {
// Correct calculation: convert duration to frames per second
frameFPS = int(time.Second / dt)
}
s.fpsSampleSum -= s.fpsSamples[s.fpsSampleIdx]
s.fpsSamples[s.fpsSampleIdx] = frameFPS
s.fpsSampleSum += frameFPS
s.fpsSampleIdx = (s.fpsSampleIdx + 1) % len(s.fpsSamples)

// Calculate average FPS over samples
s.avgFPS = float64(s.fpsSampleSum) / float64(len(s.fpsSamples))

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

// Update frame time history (ms) on every frame
// Use average of last two frames for smoother graph
var ms float64
if s.lastFrameDuration > 0 {
ms = float64(s.lastFrameDuration.Microseconds()) / 1000.0
s.msHistory[s.msHistoryIdx] = ms
s.msHistoryIdx = (s.msHistoryIdx + 1) % len(s.msHistory)
}
{ // Store current frame FPS in samples
frameFPS := 0
if dt > 0 {
// Correct calculation: convert duration to frames per second
frameFPS = int(time.Second / dt)
}
s.fpsSampleSum -= s.fpsSamples[s.fpsSampleIdx]
s.fpsSamples[s.fpsSampleIdx] = frameFPS
s.fpsSampleSum += frameFPS
s.fpsSampleIdx = (s.fpsSampleIdx + 1) % len(s.fpsSamples)

// Calculate average FPS over samples
s.avgFPS = float64(s.fpsSampleSum) / float64(len(s.fpsSamples))

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

// Update frame time history (ms) on every frame
// Use average of last two frames for smoother graph
var ms float64
if s.lastFrameDuration > 0 {
ms = float64(s.lastFrameDuration.Microseconds()) / 1000.0
s.msHistory[s.msHistoryIdx] = ms
s.msHistoryIdx = (s.msHistoryIdx + 1) % len(s.msHistory)
}

if now.Sub(s.lastFPSTime) >= time.Second {
s.currentFPS = s.frameCount
s.frameCount = 0
s.lastFPSTime = now
if now.Sub(s.lastFPSTime) >= time.Second {
s.currentFPS = s.frameCount
s.frameCount = 0
s.lastFPSTime = now
}
}

s.Cameras.EachEntity()(func(entity ecs.Entity) bool {
camera := s.Cameras.GetUnsafe(entity)
frame := s.FrameBuffer2D.GetUnsafe(entity)
switch frame.Layer {
fb := s.FrameBuffer2D.GetUnsafe(entity)
switch fb.Layer {
case config.MainCameraLayer:
overlayFrame := s.FrameBuffer2D.GetUnsafe(s.frameBuffer)
rl.BeginTextureMode(overlayFrame.Texture)
Expand All @@ -165,8 +166,8 @@
// Debug mode: BVH tree and dots
if s.debug {
rl.BeginMode2D(camera.Camera2D)

cameraRect := camera.Rect()

s.CollisionCells.EachEntity()(func(e ecs.Entity) bool {
cell := s.CollisionCells.GetUnsafe(e)
assert.NotNil(cell)
Expand Down Expand Up @@ -266,18 +267,30 @@
})
s.Collisions.EachEntity()(func(entity ecs.Entity) bool {
pos := s.Positions.GetUnsafe(entity)
rl.DrawRectangle(int32(pos.XY.X-8), int32(pos.XY.Y-8), 16, 16, rl.Red)
rec := vectors.Rectangle{
X: pos.XY.X - 8,
Y: pos.XY.Y - 8,
Width: 16,
Height: 16,
}
if s.intersects(cameraRect, rec) {
rl.DrawRectangleRec(rl.Rectangle(rec), rl.Red)
}
return true
})
s.Textures.EachComponent()(func(r *stdcomponents.RLTexturePro) bool {
rl.DrawRectanglePro(rl.Rectangle{
rec := vectors.Rectangle{
X: r.Dest.X - 2,
Y: r.Dest.Y - 2,
Width: 4,
Height: 4,
}, rl.Vector2{}, r.Rotation, rl.Red)
}
if s.intersects(cameraRect, rec) {
rl.DrawRectanglePro(rl.Rectangle(rec), rl.Vector2{}, r.Rotation, rl.Red)
}
return true
})

rl.EndMode2D()
}

Expand Down Expand Up @@ -307,13 +320,23 @@
rl.EndTextureMode()

case config.MinimapCameraLayer:
rl.BeginTextureMode(frame.Texture)
rl.DrawRectangleLines(1, 1, frame.Texture.Texture.Width-1, frame.Texture.Texture.Height-1, rl.Green)
rl.BeginTextureMode(fb.Texture)
rl.DrawRectangleLines(2, 2, fb.Texture.Texture.Width-2, fb.Texture.Texture.Height-2, rl.Green)
rl.EndTextureMode()
}

return true
})

if rl.IsWindowResized() {
s.monitorWidth = rl.GetScreenWidth()
s.monitorHeight = rl.GetScreenHeight()
fb := s.FrameBuffer2D.GetUnsafe(s.frameBuffer)
rl.UnloadRenderTexture(fb.Texture)
fb.Texture = rl.LoadRenderTexture(int32(s.monitorWidth), int32(s.monitorHeight))

Check failure

Code scanning / gosec

integer overflow conversion uint64 -> uint32 Error

integer overflow conversion int -> int32

Check failure

Code scanning / gosec

integer overflow conversion uint64 -> uint32 Error

integer overflow conversion int -> int32
fb.Frame = rl.Rectangle{X: 0, Y: 0, Width: float32(s.monitorWidth), Height: float32(s.monitorHeight)}
fb.Dst = rl.Rectangle{Width: float32(s.monitorWidth), Height: float32(s.monitorHeight)}
}
return true
}

Expand All @@ -328,7 +351,6 @@
}

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

// Colors
fontColor := rl.Lime
Expand All @@ -350,7 +372,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("Low: %d", percentileFPS), x, y+fontSize*3, fontSize, fontColor)
rl.DrawText(fmt.Sprintf("Low: %d", s.lowestFps), x, y+fontSize*3, fontSize, fontColor)

// Draw ms graph
s.drawMsGraph(x+180, y)
Expand Down
4 changes: 2 additions & 2 deletions pkg/ecs/component-bit-table.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ func (b *ComponentBitTable) AllSet(entity Entity, yield func(ComponentId) bool)
return
}
pageId, bitsetId := b.getPageIDAndBitsetIndex(bitsId)
for i := 0; i < b.bitsetSize; i++ {
set := b.bitsetsBook[pageId][bitsetId+i]
bitset := b.bitsetsBook[pageId][bitsetId : bitsetId+b.bitsetSize]
for i, set := range bitset {
j := 0
for set != 0 {
if set&1 == 1 {
Expand Down
1 change: 1 addition & 0 deletions stdsystems/os-handler-system.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type OSHandlerSystem struct{}

func (s *OSHandlerSystem) Init() {
// TODO: pass parameters, resize or reinit.
rl.SetConfigFlags(rl.FlagWindowResizable)
rl.InitWindow(1280, 720, "GOMP")
}

Expand Down
5 changes: 0 additions & 5 deletions stdsystems/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,9 @@ type RenderSystem struct {

renderTextures []rl.RenderTexture2D
frames []stdcomponents.FrameBuffer2D

monitorWidth int
monitorHeight int
}

func (s *RenderSystem) Init() {
s.monitorWidth = rl.GetScreenWidth()
s.monitorHeight = rl.GetScreenHeight()
s.renderTextures = make([]rl.RenderTexture2D, 0, s.FrameBuffer2D.Len())
s.frames = make([]stdcomponents.FrameBuffer2D, 0, s.FrameBuffer2D.Len())
}
Expand Down
Loading