From b58976527a0899ec7e25c12351df55307f556fe6 Mon Sep 17 00:00:00 2001 From: Noah Petherbridge Date: Sun, 30 Dec 2018 13:50:24 -0800 Subject: [PATCH] Add test wallpaper and minor code tweaks --- assets/wallpapers/test-128x128.png | Bin 0 -> 375 bytes balance/debug.go | 10 +++++----- balance/numbers.go | 5 +++-- commands.go | 11 +++++++++++ doodads/actor.go | 2 ++ doodads/collision.go | 23 +++++++++++++++-------- doodads/drawing.go | 11 +++++++++++ doodle.go | 14 ++++++++------ fps.go | 10 ++++++++-- pkg/wallpaper/wallpaper_test.go | 2 +- play_scene.go | 2 +- render/color.go | 27 +++++++++++++++++++-------- render/interface.go | 14 +++++++++++++- render/point.go | 11 ++++++++++- render/sdl/fps.go | 22 ---------------------- ui/theme/theme.go | 2 +- uix/canvas_scrolling.go | 2 +- 17 files changed, 109 insertions(+), 59 deletions(-) create mode 100644 assets/wallpapers/test-128x128.png delete mode 100644 render/sdl/fps.go diff --git a/assets/wallpapers/test-128x128.png b/assets/wallpapers/test-128x128.png new file mode 100644 index 0000000000000000000000000000000000000000..736302d2ad79b8780d96a4a1cd77ed80463cb6f2 GIT binary patch literal 375 zcmeAS@N?(olHy`uVBq!ia0vp^4Is?H1SEZ8zRh7^V6^jeaSW-L^XAS)&I1NKM;(@z z{GWQ;kWct7i+;zSUw_lKZ~nvBFITXeDS`C>SA!^nHbWT08iokQ4fGdybLNfRf&JCB k_6-B*2)N(>0mJbB|HiK@|7}b% 0 { // make sure it won't roll under - delay = uint32(TargetFPS - int(tmp)) + if !fpsDoNotCap { + elapsed := time.Now().Sub(start) + tmp := elapsed / time.Millisecond + if TargetFPS-int(tmp) > 0 { // make sure it won't roll under + delay = uint32(TargetFPS - int(tmp)) + } + d.Engine.Delay(delay) } - d.Engine.Delay(delay) // Track how long this frame took to measure FPS over time. d.TrackFPS(delay) diff --git a/fps.go b/fps.go index 57f2193..4de911a 100644 --- a/fps.go +++ b/fps.go @@ -33,6 +33,7 @@ var ( fpsFrames int fpsSkipped uint32 fpsInterval uint32 = 1000 + fpsDoNotCap bool // remove the FPS delay cap in Main Loop // Custom labels for scenes to add to the debug overlay view. customDebugLabels []debugLabel @@ -49,17 +50,22 @@ func (d *Doodle) DrawDebugOverlay() { return } + var framesSkipped = fmt.Sprintf("(skip: %dms)", fpsSkipped) + if fpsDoNotCap { + framesSkipped = "uncapped" + } + var ( darken = balance.DebugStrokeDarken Yoffset int32 = 20 // leave room for the menu bar - Xoffset int32 = 5 + Xoffset int32 = 20 keys = []string{ "FPS:", "Scene:", "Mouse:", } values = []string{ - fmt.Sprintf("%d (skip: %dms)", fpsCurrent, fpsSkipped), + fmt.Sprintf("%d %s", fpsCurrent, framesSkipped), d.Scene.Name(), fmt.Sprintf("%d,%d", d.event.CursorX.Now, d.event.CursorY.Now), } diff --git a/pkg/wallpaper/wallpaper_test.go b/pkg/wallpaper/wallpaper_test.go index 72d3c1e..4efcecf 100644 --- a/pkg/wallpaper/wallpaper_test.go +++ b/pkg/wallpaper/wallpaper_test.go @@ -28,7 +28,7 @@ func TestWallpaper(t *testing.T) { img := image.NewRGBA(image.Rect(0, 0, width, height)) draw.Draw( // Corner: red - img, // dst Image + img, // dst Image image.Rect(0, 0, qWidth, qHeight), // r Rectangle image.NewUniform(red), // src Image image.Point{0, 0}, // sp Point diff --git a/play_scene.go b/play_scene.go index bf6546c..c1cf8bb 100644 --- a/play_scene.go +++ b/play_scene.go @@ -173,7 +173,7 @@ func (s *PlayScene) movePlayer(ev *events.State) { // Apply gravity. // var onFloor bool - info, ok := doodads.CollidesWithGrid(s.Player, s.Level.Chunker, delta) + info, ok := doodads.CollidesWithGrid(s.Player, s.Level.Chunker, delta, s.drawing.Scroll) if ok { // Collision happened with world. } diff --git a/render/color.go b/render/color.go index c16cda2..f953e49 100644 --- a/render/color.go +++ b/render/color.go @@ -156,15 +156,15 @@ func (c *Color) UnmarshalJSON(b []byte) error { } // Add a relative color value to the color. -func (c Color) Add(r, g, b, a int32) Color { +func (c Color) Add(r, g, b, a int) Color { var ( - R = int32(c.Red) + r - G = int32(c.Green) + g - B = int32(c.Blue) + b - A = int32(c.Alpha) + a + R = int(c.Red) + r + G = int(c.Green) + g + B = int(c.Blue) + b + A = int(c.Alpha) + a ) - cap8 := func(v int32) uint8 { + cap8 := func(v int) uint8 { if v > 255 { v = 255 } else if v < 0 { @@ -182,11 +182,22 @@ func (c Color) Add(r, g, b, a int32) Color { } // Lighten a color value. -func (c Color) Lighten(v int32) Color { +func (c Color) Lighten(v int) Color { return c.Add(v, v, v, 0) } // Darken a color value. -func (c Color) Darken(v int32) Color { +func (c Color) Darken(v int) Color { return c.Add(-v, -v, -v, 0) } + +// Transparentize adjusts the alpha level of a color. +func (c Color) Transparentize(v int) Color { + return c.Add(0, 0, 0, int(v)) +} + +// SetAlpha sets the alpha value to a specific setting. +func (c Color) SetAlpha(v uint8) Color { + c.Alpha = v + return c +} diff --git a/render/interface.go b/render/interface.go index 1a30ad9..a64d498 100644 --- a/render/interface.go +++ b/render/interface.go @@ -125,7 +125,8 @@ func (r Rect) Add(other Rect) Rect { } } -// Add a point to move the rect. +// Add (or subtract) a point to move the rect. This is usually the method you +// want to use: negative Point values will subtract the rect's position. func (r Rect) AddPoint(other Point) Rect { return Rect{ X: r.X + other.X, @@ -135,6 +136,17 @@ func (r Rect) AddPoint(other Point) Rect { } } +// SubtractPoint is the inverse of AddPoint. Use this only if you need to +// invert the Point being added. +func (r Rect) SubtractPoint(other Point) Rect { + return Rect{ + X: r.X - other.X, + Y: r.Y - other.Y, + W: r.W, + H: r.H, + } +} + // Text holds information for drawing text. type Text struct { Text string diff --git a/render/point.go b/render/point.go index 272238c..49f8576 100644 --- a/render/point.go +++ b/render/point.go @@ -69,12 +69,21 @@ func (p Point) Inside(r Rect) bool { (p.Y >= y1 && p.Y <= y2)) } -// Add (or subtract) the other point to your current point. +// Add (or subtract) the other point to your current point. This is usually +// the one you want: if the other Point has negative values it will subtract +// them from this Point, or if they are positive it will add them. func (p *Point) Add(other Point) { p.X += other.X p.Y += other.Y } +// Subtract is the inverse of Add. Use this if you want to force a subtraction +// operation (i.e. to invert a Point before adding it). +func (p *Point) Subtract(other Point) { + p.X -= other.X + p.Y -= other.Y +} + // MarshalText to convert the point into text so that a render.Point may be used // as a map key and serialized to JSON. func (p *Point) MarshalText() ([]byte, error) { diff --git a/render/sdl/fps.go b/render/sdl/fps.go deleted file mode 100644 index 4c625c9..0000000 --- a/render/sdl/fps.go +++ /dev/null @@ -1,22 +0,0 @@ -package sdl - -import ( - "git.kirsle.net/apps/doodle/level" -) - -// Frames to cache for FPS calculation. -const ( - maxSamples = 100 - TargetFPS = 1000 / 60 -) - -var ( - fpsCurrentTicks uint32 // current time we get sdl.GetTicks() - fpsLastTime uint32 // last time we printed the fpsCurrentTicks - fpsCurrent int - fpsFrames int - fpsSkipped uint32 - fpsInterval uint32 = 1000 -) - -var pixelHistory []level.Pixel diff --git a/ui/theme/theme.go b/ui/theme/theme.go index 23c3186..1994128 100644 --- a/ui/theme/theme.go +++ b/ui/theme/theme.go @@ -8,5 +8,5 @@ var ( ButtonHoverColor = render.RGBA(200, 255, 255, 255) ButtonOutlineColor = render.Black - BorderColorOffset int32 = 40 + BorderColorOffset = 40 ) diff --git a/uix/canvas_scrolling.go b/uix/canvas_scrolling.go index 3c5f506..7c13aff 100644 --- a/uix/canvas_scrolling.go +++ b/uix/canvas_scrolling.go @@ -127,7 +127,7 @@ func (w *Canvas) loopFollowActor(ev *events.State) error { var ( APosition = actor.Position() // relative to screen APoint = actor.Drawing.Position() - ASize = actor.Canvas.Size() + ASize = actor.Drawing.Size() scrollBy render.Point )