Skip to content

Commit

Permalink
use image.Image instead of a specific implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
anaseto committed Feb 9, 2021
1 parent ecab109 commit 783ca25
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 8 deletions.
13 changes: 11 additions & 2 deletions drivers/js/js.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package js
import (
"context"
"image"
"image/draw"
"log"
"time"
"unicode/utf8"
Expand All @@ -16,7 +17,7 @@ import (
// TileManager manages tiles fetching.
type TileManager interface {
// GetImage returns the image to be used for a given cell style.
GetImage(gruid.Cell) *image.RGBA
GetImage(gruid.Cell) image.Image

// TileSize returns the (width, height) in pixels of the tiles. Both
// should be positive and non-zero.
Expand Down Expand Up @@ -358,7 +359,15 @@ func (dr *Driver) draw(cell gruid.Cell, x, y int) {
log.Printf("no tile for %+v", cell)
return
}
buf := img.Pix
var rgbaimg *image.RGBA
switch img := img.(type) {
case *image.RGBA:
rgbaimg = img
default:
rect := img.Bounds()
draw.Draw(rgbaimg, rect, img, rect.Min, draw.Src)
}
buf := rgbaimg.Pix
ua := js.Global().Get("Uint8Array").New(js.ValueOf(len(buf)))
js.CopyBytesToJS(ua, buf)
ca := js.Global().Get("Uint8ClampedArray").New(ua)
Expand Down
2 changes: 1 addition & 1 deletion drivers/sdl/sdl.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
// TileManager manages tiles fetching.
type TileManager interface {
// GetImage returns the image to be used for a given cell style.
GetImage(gruid.Cell) *image.RGBA
GetImage(gruid.Cell) image.Image

// TileSize returns the (width, height) in pixels of the tiles. Both
// should be positive and non-zero.
Expand Down
2 changes: 1 addition & 1 deletion examples/messages/tiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type TileDrawer struct {
drawer *tiles.Drawer
}

func (t *TileDrawer) GetImage(c gruid.Cell) *image.RGBA {
func (t *TileDrawer) GetImage(c gruid.Cell) image.Image {
// we use some selenized colors
fg := image.NewUniform(color.RGBA{0xad, 0xbc, 0xbc, 255})
switch c.Style.Fg {
Expand Down
2 changes: 1 addition & 1 deletion examples/movement/tiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type TileDrawer struct {
drawer *tiles.Drawer
}

func (t *TileDrawer) GetImage(c gruid.Cell) *image.RGBA {
func (t *TileDrawer) GetImage(c gruid.Cell) image.Image {
// we use some selenized colors
fg := image.NewUniform(color.RGBA{0xad, 0xbc, 0xbc, 255})
bg := image.NewUniform(color.RGBA{0x18, 0x49, 0x56, 255})
Expand Down
2 changes: 1 addition & 1 deletion examples/pager/tiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type TileDrawer struct {
drawer *tiles.Drawer
}

func (t *TileDrawer) GetImage(c gruid.Cell) *image.RGBA {
func (t *TileDrawer) GetImage(c gruid.Cell) image.Image {
// we use some selenized colors
fg := image.NewUniform(color.RGBA{0xad, 0xbc, 0xbc, 255})
switch c.Style.Fg {
Expand Down
4 changes: 2 additions & 2 deletions tiles/drawer.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ func NewDrawer(face font.Face) (*Drawer, error) {

// Draw draws a rune and returns the produced image with foreground and
// background colors given by images fg and bg.
func (d *Drawer) Draw(r rune, fg, bg image.Image) *image.RGBA {
func (d *Drawer) Draw(r rune, fg, bg image.Image) image.Image {
d.drawer.Dot = d.dot
d.drawer.Src = fg
d.drawer.Dst = image.NewRGBA(d.rect)
rect := d.drawer.Dst.Bounds()
draw.Draw(d.drawer.Dst, rect, bg, rect.Min, draw.Src)
d.drawer.DrawString(string(r))
img := d.drawer.Dst.(*image.RGBA)
img := d.drawer.Dst
return img
}

Expand Down

0 comments on commit 783ca25

Please sign in to comment.