Skip to content

Commit

Permalink
Move Renderer interface to generator/tile
Browse files Browse the repository at this point in the history
  • Loading branch information
weqqr committed Oct 30, 2024
1 parent 8c31048 commit e00cb3b
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 36 deletions.
3 changes: 1 addition & 2 deletions cmd/panorama/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/alexflint/go-arg"
"github.com/lord-server/panorama/internal/config"
"github.com/lord-server/panorama/internal/game"
"github.com/lord-server/panorama/internal/generator"
"github.com/lord-server/panorama/internal/generator/flat"
"github.com/lord-server/panorama/internal/generator/tile"
"github.com/lord-server/panorama/internal/server"
Expand Down Expand Up @@ -83,7 +82,7 @@ func fullrender(config config.Config) error {

slog.Info("performing a full render", "workers", config.Renderer.Workers, "region", config.Region)

tiler.FullRender(&game, &wd, config.Renderer.Workers, config.Region, func() generator.Renderer {
tiler.FullRender(&game, &wd, config.Renderer.Workers, config.Region, func() tile.Renderer {
return flat.NewRenderer(config.Region, &game)
})

Expand Down
4 changes: 2 additions & 2 deletions internal/generator/flat/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"log/slog"

"github.com/lord-server/panorama/internal/game"
"github.com/lord-server/panorama/internal/generator"
"github.com/lord-server/panorama/internal/generator/rasterizer"
"github.com/lord-server/panorama/internal/generator/tile"
"github.com/lord-server/panorama/internal/world"
"github.com/lord-server/panorama/pkg/geom"
"github.com/lord-server/panorama/pkg/lm"
Expand All @@ -28,7 +28,7 @@ func NewRenderer(region geom.Region, game *game.Game) *FlatRenderer {
}

func (r *FlatRenderer) RenderTile(
tilePos generator.TilePosition,
tilePos tile.TilePosition,
wd *world.World,
game *game.Game,
) *rasterizer.RenderBuffer {
Expand Down
4 changes: 2 additions & 2 deletions internal/generator/isometric/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"math"

"github.com/lord-server/panorama/internal/game"
"github.com/lord-server/panorama/internal/generator"
"github.com/lord-server/panorama/internal/generator/light"
"github.com/lord-server/panorama/internal/generator/nn"
"github.com/lord-server/panorama/internal/generator/rasterizer"
"github.com/lord-server/panorama/internal/generator/tile"
"github.com/lord-server/panorama/internal/world"
"github.com/lord-server/panorama/pkg/geom"
"github.com/lord-server/panorama/pkg/lm"
Expand Down Expand Up @@ -160,7 +160,7 @@ func (r *IsometricRenderer) renderBlock(
}

func (r *IsometricRenderer) RenderTile(
tilePos generator.TilePosition,
tilePos tile.TilePosition,
world *world.World,
game *game.Game,
) *rasterizer.RenderBuffer {
Expand Down
18 changes: 0 additions & 18 deletions internal/generator/renderer.go
Original file line number Diff line number Diff line change
@@ -1,19 +1 @@
package generator

import (
"github.com/lord-server/panorama/internal/game"
"github.com/lord-server/panorama/internal/generator/rasterizer"
"github.com/lord-server/panorama/internal/world"
"github.com/lord-server/panorama/pkg/geom"
)

type TilePosition struct {
X, Y int
}

type Renderer interface {
RenderTile(pos TilePosition, w *world.World, game *game.Game) *rasterizer.RenderBuffer
ProjectRegion(region geom.Region) geom.ProjectedRegion
// ListTilesWithBlock(x, y, z int) []TilePosition
// ListTilesInsideRegion(region config.Region) []TilePosition
}
9 changes: 4 additions & 5 deletions internal/generator/tile/downscale.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ import (

"github.com/nfnt/resize"

"github.com/lord-server/panorama/internal/generator"
"github.com/lord-server/panorama/pkg/imageutil"
"github.com/lord-server/panorama/pkg/lm"
)

func uniquePositions(input []generator.TilePosition) []generator.TilePosition {
func uniquePositions(input []TilePosition) []TilePosition {
// Slices with zero or one element always contain unique elements
if len(input) < 2 {
return input
Expand Down Expand Up @@ -58,10 +57,10 @@ func uniquePositions(input []generator.TilePosition) []generator.TilePosition {
}

// downscalePositions produces downscaled images for given zoom level and returns a list of produced tile positions
func (t *Tiler) downscalePositions(zoom int, positions []generator.TilePosition) []generator.TilePosition {
func (t *Tiler) downscalePositions(zoom int, positions []TilePosition) []TilePosition {
const quadrantSize = 128

var nextPositions []generator.TilePosition
var nextPositions []TilePosition

for _, pos := range positions {
target := image.NewNRGBA(image.Rect(0, 0, 256, 256))
Expand All @@ -88,7 +87,7 @@ func (t *Tiler) downscalePositions(zoom int, positions []generator.TilePosition)
slog.Error("unable to save image", "err", err, "path", imagePath)
}

nextPositions = append(nextPositions, generator.TilePosition{
nextPositions = append(nextPositions, TilePosition{
X: lm.FloorDiv(pos.X, 2),
Y: lm.FloorDiv(pos.Y, 2),
})
Expand Down
23 changes: 16 additions & 7 deletions internal/generator/tile/tiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,22 @@ import (
"sync"

"github.com/lord-server/panorama/internal/game"
"github.com/lord-server/panorama/internal/generator"
"github.com/lord-server/panorama/internal/generator/rasterizer"
"github.com/lord-server/panorama/internal/world"
"github.com/lord-server/panorama/pkg/geom"
"github.com/lord-server/panorama/pkg/imageutil"
"github.com/lord-server/panorama/pkg/lm"
)

type TilePosition struct {
X, Y int
}

type Renderer interface {
RenderTile(pos TilePosition, w *world.World, game *game.Game) *rasterizer.RenderBuffer
ProjectRegion(region geom.Region) geom.ProjectedRegion
}

type Tiler struct {
region geom.Region
zoomLevels int
Expand All @@ -37,7 +46,7 @@ func (t *Tiler) tilePath(x, y, zoom int) string {
return fmt.Sprintf("%v/%v/%v/%v.png", t.tilesPath, -zoom, x, y)
}

func (t *Tiler) worker(wg *sync.WaitGroup, game *game.Game, world *world.World, renderer generator.Renderer, positions <-chan generator.TilePosition) {
func (t *Tiler) worker(wg *sync.WaitGroup, game *game.Game, world *world.World, renderer Renderer, positions <-chan TilePosition) {
for position := range positions {
output := renderer.RenderTile(position, world, game)
// Don't save empty tiles
Expand All @@ -58,12 +67,12 @@ func (t *Tiler) worker(wg *sync.WaitGroup, game *game.Game, world *world.World,
wg.Done()
}

type CreateRendererFunc func() generator.Renderer
type CreateRendererFunc func() Renderer

func (t *Tiler) FullRender(game *game.Game, world *world.World, workers int, region geom.Region, createRenderer CreateRendererFunc) {
var wg sync.WaitGroup

positions := make(chan generator.TilePosition)
positions := make(chan TilePosition)
projectedRegion := geom.ProjectedRegion{}

for i := 0; i < workers; i++ {
Expand All @@ -82,7 +91,7 @@ func (t *Tiler) FullRender(game *game.Game, world *world.World, workers int, reg
}

for y := projectedRegion.YBounds.Min; y < projectedRegion.YBounds.Max; y++ {
positions <- generator.TilePosition{X: x, Y: y}
positions <- TilePosition{X: x, Y: y}
}
}

Expand All @@ -101,7 +110,7 @@ func (t *Tiler) DownscaleTiles() {
}

// Collect tile positions
var positions []generator.TilePosition
var positions []TilePosition

err = filepath.WalkDir(tileDir, func(path string, d fs.DirEntry, _ error) error {
if d == nil || d.IsDir() {
Expand All @@ -124,7 +133,7 @@ func (t *Tiler) DownscaleTiles() {
return nil
}

positions = append(positions, generator.TilePosition{
positions = append(positions, TilePosition{
X: lm.FloorDiv(x, 2),
Y: lm.FloorDiv(y, 2),
})
Expand Down

0 comments on commit e00cb3b

Please sign in to comment.