From 2b7ca75ff8daed93fe7c11d81adc05a3f02e3991 Mon Sep 17 00:00:00 2001 From: bitver Date: Tue, 6 May 2025 22:51:14 +0500 Subject: [PATCH] resize --- examples/new-api/systems/camera-main.go | 20 ++-- examples/new-api/systems/camera-minimap.go | 11 +++ examples/new-api/systems/render-overlay.go | 102 +++++++++++++-------- pkg/ecs/component-bit-table.go | 4 +- stdsystems/os-handler-system.go | 1 + stdsystems/render.go | 5 - 6 files changed, 87 insertions(+), 56 deletions(-) diff --git a/examples/new-api/systems/camera-main.go b/examples/new-api/systems/camera-main.go index df59d3dd..ca067983 100644 --- a/examples/new-api/systems/camera-main.go +++ b/examples/new-api/systems/camera-main.go @@ -111,15 +111,17 @@ func (s *MainCameraSystem) Run(dt time.Duration) { 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)) + 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 diff --git a/examples/new-api/systems/camera-minimap.go b/examples/new-api/systems/camera-minimap.go index 55e3b432..fc5cc07f 100644 --- a/examples/new-api/systems/camera-minimap.go +++ b/examples/new-api/systems/camera-minimap.go @@ -90,6 +90,17 @@ func (s *MinimapSystem) Run(dt time.Duration) bool { 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)) + 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 } diff --git a/examples/new-api/systems/render-overlay.go b/examples/new-api/systems/render-overlay.go index 5f193099..88e43e50 100644 --- a/examples/new-api/systems/render-overlay.go +++ b/examples/new-api/systems/render-overlay.go @@ -81,7 +81,7 @@ func (s *RenderOverlaySystem) Init() { 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)}, }) @@ -121,42 +121,43 @@ func (s *RenderOverlaySystem) Run(dt time.Duration) bool { 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) @@ -165,8 +166,8 @@ func (s *RenderOverlaySystem) Run(dt time.Duration) bool { // 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) @@ -266,18 +267,30 @@ func (s *RenderOverlaySystem) Run(dt time.Duration) bool { }) 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() } @@ -307,13 +320,23 @@ func (s *RenderOverlaySystem) Run(dt time.Duration) bool { 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)) + 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 } @@ -328,7 +351,6 @@ func (s *RenderOverlaySystem) drawCustomFPS(x, y int32) { } avgFPS := int32(s.avgFPS) - percentileFPS := int32(s.lowestFps) // Colors fontColor := rl.Lime @@ -350,7 +372,7 @@ func (s *RenderOverlaySystem) drawCustomFPS(x, y int32) { 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) diff --git a/pkg/ecs/component-bit-table.go b/pkg/ecs/component-bit-table.go index 2802663f..ef7ab94a 100644 --- a/pkg/ecs/component-bit-table.go +++ b/pkg/ecs/component-bit-table.go @@ -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 { diff --git a/stdsystems/os-handler-system.go b/stdsystems/os-handler-system.go index 4a99dd42..eecb8fdc 100644 --- a/stdsystems/os-handler-system.go +++ b/stdsystems/os-handler-system.go @@ -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") } diff --git a/stdsystems/render.go b/stdsystems/render.go index 281a9810..496acb36 100644 --- a/stdsystems/render.go +++ b/stdsystems/render.go @@ -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()) }