Skip to content

Commit

Permalink
clean up and recommendations from IDE
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkimber committed Oct 8, 2024
1 parent 79b8aca commit 0c3d868
Show file tree
Hide file tree
Showing 13 changed files with 33 additions and 38 deletions.
16 changes: 9 additions & 7 deletions cmd/renderobject.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
Expand All @@ -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)
})
}

Expand Down
15 changes: 7 additions & 8 deletions internal/colour/palette.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"image/color"
"io"
"io/ioutil"
"math"
)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions internal/colour/rgb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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),
Expand Down
4 changes: 2 additions & 2 deletions internal/geometry/point.go
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
5 changes: 2 additions & 3 deletions internal/manifest/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/mattkimber/gorender/internal/geometry"
"github.com/mattkimber/gorender/internal/voxelobject"
"io"
"io/ioutil"
"math"
)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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])
Expand Down
4 changes: 3 additions & 1 deletion internal/manifest/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion internal/raycaster/raycaster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
6 changes: 3 additions & 3 deletions internal/sprite/shader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/spritesheet/spritesheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion internal/utils/fileutils/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func doFileIO(filename string, handler fileIOHandler) (err error) {
err = handler.DoIO(file)

if err != nil {
file.Close()
_ = file.Close()
return
}

Expand Down
3 changes: 1 addition & 2 deletions internal/utils/fileutils/io_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package fileutils

import (
"io"
"io/ioutil"
"math/rand"
"os"
"strconv"
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion internal/utils/fileutils/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
6 changes: 0 additions & 6 deletions internal/utils/imageutils/imageutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++ {
Expand Down

0 comments on commit 0c3d868

Please sign in to comment.