From fae918a1964b8caf4f773bd7d837ebfd7b7914f8 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Mon, 21 Aug 2023 00:35:55 -0400 Subject: [PATCH] Added textures to backend --- texture.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 texture.go diff --git a/texture.go b/texture.go new file mode 100644 index 0000000..346f62b --- /dev/null +++ b/texture.go @@ -0,0 +1,43 @@ +package x + +import "image" +import "git.tebibyte.media/tomo/tomo" + +type texture struct { + pix []uint8 + stride int + rect image.Rectangle +} + +func (backend *Backend) NewTexture (source image.Image) tomo.Texture { + bounds := source.Bounds() + texture := &texture { + pix: make([]uint8, bounds.Dx() * bounds.Dy() * 4), + stride: bounds.Dx(), + rect: bounds.Sub(bounds.Min), + } + + index := 0 + var point image.Point + for point.Y = bounds.Min.Y; point.Y < bounds.Max.Y; point.Y ++ { + for point.X = bounds.Min.X; point.X < bounds.Max.X; point.X ++ { + r, g, b, a := source.At(point.X, point.Y).RGBA() + texture.pix[index + 0] = uint8(b >> 8) + texture.pix[index + 1] = uint8(g >> 8) + texture.pix[index + 2] = uint8(r >> 8) + texture.pix[index + 3] = uint8(a >> 8) + index += 4 + }} + + return texture +} + +func (this *texture) Clip (bounds image.Rectangle) tomo.Texture { + clipped := *this + clipped.rect = bounds + return &clipped +} + +func (this *texture) Close () error { + return nil +}