Skip to content

Instantly share code, notes, and snippets.

@hajimehoshi
Created March 27, 2021 19:22
Show Gist options
  • Save hajimehoshi/1c441ae8a72c7c87b7374b04fb638117 to your computer and use it in GitHub Desktop.
Save hajimehoshi/1c441ae8a72c7c87b7374b04fb638117 to your computer and use it in GitHub Desktop.
diff --git a/renderer/replacepixels.go b/renderer/replacepixels.go
index 2de8786..72c9dd6 100644
--- a/renderer/replacepixels.go
+++ b/renderer/replacepixels.go
@@ -8,11 +8,17 @@ import (
"github.com/hajimehoshi/ebiten/v2"
)
-var screenBuffer *image.RGBA
+var (
+ screenBuffer *image.RGBA
+ screenBufferImg *ebiten.Image
+)
func ReplacePixels(screen *ebiten.Image, data []particles.Particle) {
if screenBuffer == nil {
- screenBuffer = image.NewRGBA(image.Rect(0, 0, settings.ResolutionWidth, settings.ResolutionHeight))
+ screenBuffer = image.NewRGBA(image.Rect(0, 0, settings.ScreenWidth, settings.ScreenHeight))
+ }
+ if screenBufferImg == nil {
+ screenBufferImg = ebiten.NewImage(settings.ScreenWidth, settings.ScreenHeight)
}
const l = settings.ScreenWidth * settings.ScreenHeight
@@ -20,16 +26,11 @@ func ReplacePixels(screen *ebiten.Image, data []particles.Particle) {
for x := 0; x < settings.ScreenWidth; x++ {
for y := 0; y < settings.ScreenHeight; y++ {
particleData := particles.GetDataXY(x, y)
- for px := x * settings.Scale; px < (x+1)*settings.Scale; px++ {
- for py := y * settings.Scale; py < (y+1)*settings.Scale; py++ {
- pxIdx := (px + py*settings.ResolutionWidth) * 4
- screenBuffer.Pix[pxIdx] = particleData.Color.R
- screenBuffer.Pix[pxIdx+1] = particleData.Color.G
- screenBuffer.Pix[pxIdx+2] = particleData.Color.B
- screenBuffer.Pix[pxIdx+3] = particleData.Color.A
- }
- }
-
+ pxIdx := (x + y*settings.ScreenWidth) * 4
+ screenBuffer.Pix[pxIdx] = particleData.Color.R
+ screenBuffer.Pix[pxIdx+1] = particleData.Color.G
+ screenBuffer.Pix[pxIdx+2] = particleData.Color.B
+ screenBuffer.Pix[pxIdx+3] = particleData.Color.A
}
}
@@ -58,5 +59,8 @@ func ReplacePixels(screen *ebiten.Image, data []particles.Particle) {
}
*/
- screen.ReplacePixels(screenBuffer.Pix)
+ screenBufferImg.ReplacePixels(screenBuffer.Pix)
+ op := &ebiten.DrawImageOptions{}
+ op.GeoM.Scale(settings.Factor, settings.Factor)
+ screen.DrawImage(screenBufferImg, op)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment