Skip to content

Commit 74b76d2

Browse files
committed
Improved performance rendering textured floor
- using image.WritePixels instead of NewImageFromImage during Draw
1 parent 7edc66d commit 74b76d2

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

camera.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ func (c *Camera) SetLightRGB(min, max color.NRGBA) {
223223
// Update - updates the camera view
224224
func (c *Camera) Update(sprites []Sprite) {
225225
// clear horizontal buffer by making a new one
226-
c.floorLvl.clear(c.w, c.h)
226+
c.floorLvl.initialize(c.w, c.h)
227227

228228
// reset convergence point
229229
c.convergenceDistance = -1
@@ -761,7 +761,7 @@ func (c *Camera) createLevels(numLevels int) []*level {
761761
// creates floor slices for raycasting floor
762762
func (c *Camera) createFloorLevel() *horLevel {
763763
horizontalLevel := new(horLevel)
764-
horizontalLevel.clear(c.w, c.h)
764+
horizontalLevel.initialize(c.w, c.h)
765765
return horizontalLevel
766766
}
767767

level.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,13 @@ func sliceView(width, height int) []*image.Rectangle {
3838
type horLevel struct {
3939
// horBuffer is the image representing the pixels to render during the update
4040
horBuffer *image.RGBA
41+
// image is the ebitengine image object rendering the horBuffer during draw
42+
image *ebiten.Image
4143
}
4244

43-
func (h *horLevel) clear(width, height int) {
45+
func (h *horLevel) initialize(width, height int) {
4446
h.horBuffer = image.NewRGBA(image.Rect(0, 0, width, height))
47+
if h.image == nil {
48+
h.image = ebiten.NewImage(width, height)
49+
}
4550
}

render.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package raycaster
33
import (
44
"image"
55
"image/color"
6-
"log"
76

87
"github.com/hajimehoshi/ebiten/v2"
98
)
@@ -31,13 +30,12 @@ func (c *Camera) Draw(screen *ebiten.Image) {
3130
}
3231

3332
// draw textured floor
34-
floorImg := ebiten.NewImageFromImage(c.floorLvl.horBuffer)
35-
if floorImg == nil {
36-
log.Fatal("floorImg is nil")
37-
} else {
33+
if c.floorLvl != nil && c.floorLvl.image != nil {
34+
c.floorLvl.image.WritePixels(c.floorLvl.horBuffer.Pix)
35+
3836
op := &ebiten.DrawImageOptions{}
3937
op.Filter = ebiten.FilterNearest
40-
screen.DrawImage(floorImg, op)
38+
screen.DrawImage(c.floorLvl.image, op)
4139
}
4240

4341
// draw sprites

0 commit comments

Comments
 (0)