From 0c3d868088f6294ff203bed1c98770a18125912d Mon Sep 17 00:00:00 2001 From: Matt Kimber Date: Tue, 8 Oct 2024 19:39:12 +0100 Subject: [PATCH] clean up and recommendations from IDE --- cmd/renderobject.go | 16 +++++++++------- internal/colour/palette.go | 15 +++++++-------- internal/colour/rgb.go | 4 ++-- internal/geometry/point.go | 4 ++-- internal/manifest/manifest.go | 5 ++--- internal/manifest/manifest_test.go | 4 +++- internal/raycaster/raycaster.go | 2 +- internal/sprite/shader.go | 6 +++--- internal/spritesheet/spritesheet.go | 2 +- internal/utils/fileutils/io.go | 2 +- internal/utils/fileutils/io_test.go | 3 +-- internal/utils/fileutils/writer.go | 2 +- internal/utils/imageutils/imageutils.go | 6 ------ 13 files changed, 33 insertions(+), 38 deletions(-) diff --git a/cmd/renderobject.go b/cmd/renderobject.go index 31efcc3..cff14f9 100644 --- a/cmd/renderobject.go +++ b/cmd/renderobject.go @@ -129,15 +129,15 @@ func processFile(inputFilename string) { log.Fatal(err) } - manifest, err := getManifest(flags.ManifestFilename) + renderManifest, err := getManifest(flags.ManifestFilename) if err != nil { log.Fatal(err) } if flags.Fast { - manifest.Sampler = "square" - manifest.Accuracy = 1 - manifest.Overlap = 0 + renderManifest.Sampler = "square" + renderManifest.Accuracy = 1 + renderManifest.Overlap = 0 } object, err := magica.FromFile(inputFilename) @@ -150,7 +150,9 @@ func processFile(inputFilename string) { if err != nil { log.Fatal("could not create CPU profile: ", err) } - defer f.Close() // error handling omitted for example + defer func(f *os.File) { + _ = f.Close() + }(f) if err := pprof.StartCPUProfile(f); err != nil { log.Fatal("could not start CPU profile: ", err) } @@ -159,13 +161,13 @@ func processFile(inputFilename string) { var processedObject voxelobject.ProcessedVoxelObject timingutils.Time("Voxel processing", flags.OutputTime, func() { - processedObject = voxelobject.GetProcessedVoxelObject(object, &palette, manifest.TiledNormals, manifest.TilingMode, manifest.SolidBase) + processedObject = voxelobject.GetProcessedVoxelObject(object, &palette, renderManifest.TiledNormals, renderManifest.TilingMode, renderManifest.SolidBase) }) // Check if there are files to output for _, scale := range splitScales { timingutils.Time(fmt.Sprintf("Total (%sx)", scale), flags.OutputTime, func() { - renderScale(inputFilename, scale, manifest, processedObject, palette, numScales) + renderScale(inputFilename, scale, renderManifest, processedObject, palette, numScales) }) } diff --git a/internal/colour/palette.go b/internal/colour/palette.go index 74c735f..f81239d 100644 --- a/internal/colour/palette.go +++ b/internal/colour/palette.go @@ -5,7 +5,6 @@ import ( "fmt" "image/color" "io" - "io/ioutil" "math" ) @@ -187,15 +186,15 @@ func (p Palette) GetLitIndexed(index byte, l float64) (idx byte) { if int(index) < len(p.Entries) { rng := p.Entries[index].Range if rng != nil { - min, max := rng.Start, rng.End - spread := max - min + mn, mx := rng.Start, rng.End + spread := mx - mn offsetIndex := float64(index) + math.Round(float64(spread)*(l/2)) - if offsetIndex < float64(min) { - return min + if offsetIndex < float64(mn) { + return mn } - if offsetIndex > float64(max) { - return max + if offsetIndex > float64(mx) { + return mx } return byte(offsetIndex) @@ -259,7 +258,7 @@ func (pe *PaletteEntry) UnmarshalJSON(data []byte) error { } func FromJson(handle io.Reader) (p Palette, err error) { - data, err := ioutil.ReadAll(handle) + data, err := io.ReadAll(handle) if err != nil { return Palette{}, err diff --git a/internal/colour/rgb.go b/internal/colour/rgb.go index 44ba224..f391126 100644 --- a/internal/colour/rgb.go +++ b/internal/colour/rgb.go @@ -8,7 +8,7 @@ type RGB struct { B float64 } -func (rgb *RGB) DivideAndClamp(divisor float64) { +func (rgb RGB) DivideAndClamp(divisor float64) { rgb.R = Clamp(rgb.R/divisor, 256, 65535-256) rgb.G = Clamp(rgb.G/divisor, 256, 65535-256) rgb.B = Clamp(rgb.B/divisor, 256, 65535-256) @@ -44,7 +44,7 @@ func Clamp(input float64, min, max float64) float64 { return input } -func (rgb *RGB) GetRGBA(alpha float64) color.NRGBA64 { +func (rgb RGB) GetRGBA(alpha float64) color.NRGBA64 { return color.NRGBA64{ R: uint16(rgb.R), G: uint16(rgb.G), diff --git a/internal/geometry/point.go b/internal/geometry/point.go index 4b09279..2f6c3a2 100644 --- a/internal/geometry/point.go +++ b/internal/geometry/point.go @@ -1,8 +1,8 @@ package geometry -import gandalf_geo "github.com/mattkimber/gandalf/geometry" +import gandalfgeo "github.com/mattkimber/gandalf/geometry" -func FromGandalfPoint(point gandalf_geo.Point) Point { +func FromGandalfPoint(point gandalfgeo.Point) Point { return Point{ X: point.X, Y: point.Y, diff --git a/internal/manifest/manifest.go b/internal/manifest/manifest.go index c96168c..c6d8072 100644 --- a/internal/manifest/manifest.go +++ b/internal/manifest/manifest.go @@ -6,7 +6,6 @@ import ( "github.com/mattkimber/gorender/internal/geometry" "github.com/mattkimber/gorender/internal/voxelobject" "io" - "io/ioutil" "math" ) @@ -74,7 +73,7 @@ func FromJson(handle io.Reader) (manifest Manifest, err error) { manifest.EdgeThreshold = 0.5 manifest.TilingMode = "normal" - data, err := ioutil.ReadAll(handle) + data, err := io.ReadAll(handle) if err != nil { return @@ -105,7 +104,7 @@ func (d *Definition) SoftenEdges() bool { func (m *Manifest) SetSpriteSizes() { // Set any auto-height sprites - for i, _ := range m.Sprites { + for i := range m.Sprites { // 0 means "auto" if m.Sprites[i].Height == 0 { height, delta := getCalculatedSpriteHeight(m, m.Sprites[i]) diff --git a/internal/manifest/manifest_test.go b/internal/manifest/manifest_test.go index e3dbd9f..7bc70f7 100644 --- a/internal/manifest/manifest_test.go +++ b/internal/manifest/manifest_test.go @@ -28,7 +28,9 @@ func TestFromJson(t *testing.T) { t.Fatalf("Could not open test data: %v", err) } - defer file.Close() + defer func(file *os.File) { + _ = file.Close() + }(file) actual, err := FromJson(file) if err != nil { diff --git a/internal/raycaster/raycaster.go b/internal/raycaster/raycaster.go index 4f12ba8..9bb3c64 100644 --- a/internal/raycaster/raycaster.go +++ b/internal/raycaster/raycaster.go @@ -106,7 +106,7 @@ func raycastSamples( px, py, pz, pi := 0, 0, 0, 0 - for i, _ := range *samples { + for i := range *samples { result[thisX][y][i].Count = 1 } diff --git a/internal/sprite/shader.go b/internal/sprite/shader.go index ea1810f..0463306 100644 --- a/internal/sprite/shader.go +++ b/internal/sprite/shader.go @@ -554,12 +554,12 @@ func shade(info raycaster.RenderInfo, def *manifest.Definition, prevIndex byte) totalSamples = totalSamples + s.Count } - max := 0.0 + mx := 0.0 alternateModal := byte(0) for k, v := range values { - if v > max { - max = v + if v > mx { + mx = v // Store the previous modal alternateModal = output.ModalIndex output.ModalIndex = k diff --git a/internal/spritesheet/spritesheet.go b/internal/spritesheet/spritesheet.go index 0b37200..5151e59 100644 --- a/internal/spritesheet/spritesheet.go +++ b/internal/spritesheet/spritesheet.go @@ -212,7 +212,7 @@ func (sheets *Spritesheets) SaveAll(baseFilename string) (err error) { for i, sheet := range sheets.Data { filename := baseFilename + "_" + i + ".png" thisSheet := sheet - go func() { fileutils.WriteToFile(filename, thisSheet); wg.Done() }() + go func() { _ = fileutils.WriteToFile(filename, thisSheet); wg.Done() }() } wg.Wait() diff --git a/internal/utils/fileutils/io.go b/internal/utils/fileutils/io.go index 193b67d..2a9e533 100644 --- a/internal/utils/fileutils/io.go +++ b/internal/utils/fileutils/io.go @@ -30,7 +30,7 @@ func doFileIO(filename string, handler fileIOHandler) (err error) { err = handler.DoIO(file) if err != nil { - file.Close() + _ = file.Close() return } diff --git a/internal/utils/fileutils/io_test.go b/internal/utils/fileutils/io_test.go index 3402a5b..45b44d2 100644 --- a/internal/utils/fileutils/io_test.go +++ b/internal/utils/fileutils/io_test.go @@ -2,7 +2,6 @@ package fileutils import ( "io" - "io/ioutil" "math/rand" "os" "strconv" @@ -14,7 +13,7 @@ type testData struct { } func (d *testData) GetFromReader(r io.Reader) error { - bytes, err := ioutil.ReadAll(r) + bytes, err := io.ReadAll(r) if err != nil { return err } diff --git a/internal/utils/fileutils/writer.go b/internal/utils/fileutils/writer.go index 6df304c..32f7794 100644 --- a/internal/utils/fileutils/writer.go +++ b/internal/utils/fileutils/writer.go @@ -24,6 +24,6 @@ func (w writer) GetFileHandle(filename string) (f *os.File, err error) { func (w writer) DoIO(f *os.File) (err error) { buf := bufio.NewWriterSize(f, writerSize) err = w.fileWriter.OutputToWriter(buf) - buf.Flush() + _ = buf.Flush() return } diff --git a/internal/utils/imageutils/imageutils.go b/internal/utils/imageutils/imageutils.go index 63889cf..64017bf 100644 --- a/internal/utils/imageutils/imageutils.go +++ b/internal/utils/imageutils/imageutils.go @@ -12,12 +12,6 @@ func GetUniformImage(bounds image.Rectangle, colour color.Color) *image.RGBA { return img } -func GetUniformPalettedImage(bounds image.Rectangle, pal color.Palette, index byte) *image.Paletted { - img := image.NewPaletted(bounds, pal) - ClearToColourIndex(img, index) - return img -} - func ClearToColourIndex(img *image.Paletted, index byte) { for x := img.Bounds().Min.X; x < img.Bounds().Max.X; x++ { for y := img.Bounds().Min.Y; y < img.Bounds().Max.Y; y++ {