-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Introduced support for sprites * Updated readme * cleanup * Added unit test * Refactoring
- Loading branch information
Showing
6 changed files
with
260 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,8 @@ Go Graphics library for use in a text terminal. Only 1bit graphics can be used w | |
* [Fill](https://pkg.go.dev/github.com/msoap/tcg#Buffer.Fill) area with a different [options](https://pkg.go.dev/github.com/msoap/tcg#FillOpt), for example fill with [patterns](https://pkg.go.dev/github.com/msoap/tcg#WithPattern) | ||
* Buffer manipulating: [cut](https://pkg.go.dev/github.com/msoap/tcg#Buffer.Cut), [clone](https://pkg.go.dev/github.com/msoap/tcg#Buffer.Clone), convert to/from stdlib [Image](https://pkg.go.dev/github.com/msoap/tcg#Buffer.ToImage) or text | ||
* Buffer transform: [BitBlt](https://pkg.go.dev/github.com/msoap/tcg#Buffer.BitBlt) with [options](https://pkg.go.dev/github.com/msoap/tcg#BitBltOpt), [clear](https://pkg.go.dev/github.com/msoap/tcg#Buffer.Clear), [flip](https://pkg.go.dev/github.com/msoap/tcg#Buffer.HFlip), [invert](https://pkg.go.dev/github.com/msoap/tcg#Buffer.Invert), scroll ([vertical](https://pkg.go.dev/github.com/msoap/tcg#Buffer.VScroll), [horizontal](https://pkg.go.dev/github.com/msoap/tcg#Buffer.HScroll)) | ||
* Sub-package for [turtle graphics](https://pkg.go.dev/github.com/msoap/tcg/turtle), also available [drawing](https://pkg.go.dev/github.com/msoap/[email protected]/turtle#Turtle.DrawScript) by text script | ||
* Sub-package for [turtle graphics](https://pkg.go.dev/github.com/msoap/tcg/turtle), also available [drawing](https://pkg.go.dev/github.com/msoap/tcg/turtle#Turtle.DrawScript) by text script | ||
* [Sprite](https://pkg.go.dev/github.com/msoap/tcg/sprite) support with [Put](https://pkg.go.dev/github.com/msoap/tcg/sprite#Sprite.Put), [Withdraw](https://pkg.go.dev/github.com/msoap/tcg/sprite#Sprite.Withdraw), [Move](https://pkg.go.dev/github.com/msoap/tcg/sprite#Sprite.Move), etc methods | ||
|
||
## Install | ||
|
||
|
@@ -69,7 +70,8 @@ See more [screenshots](https://github.com/msoap/tcg/wiki/Screenshots). | |
## TODO | ||
|
||
* [ ] fonts support | ||
* [ ] sprites, maybe with animation | ||
* [x] sprites | ||
* [ ] animation in sprite | ||
|
||
## See also | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package sprite | ||
|
||
import ( | ||
"github.com/msoap/tcg" | ||
) | ||
|
||
// Sprite - sprite object | ||
type Sprite struct { | ||
Buf tcg.Buffer // sprite image | ||
x, y int // position on buffer, can be negative | ||
width int | ||
height int | ||
mask *tcg.Buffer // mask for sprite | ||
bg tcg.Buffer // saved | ||
isDrawn bool // is sprite drawn on buffer | ||
} | ||
|
||
// New - get new sprite object from tcg.Buffer | ||
func New(buf tcg.Buffer) *Sprite { | ||
return &Sprite{ | ||
width: buf.Width, | ||
height: buf.Height, | ||
Buf: buf, | ||
bg: tcg.NewBuffer(buf.Width, buf.Height), | ||
} | ||
} | ||
|
||
// WithMask - add mask to sprite | ||
func (s *Sprite) WithMask(mask tcg.Buffer) *Sprite { | ||
s.mask = &mask | ||
return s | ||
} | ||
|
||
// Put - put sprite on buffer, save background under sprite, change state of sprite to drawn | ||
func (s *Sprite) Put(buf tcg.Buffer) *Sprite { | ||
s.draw(buf) | ||
s.isDrawn = true | ||
|
||
return s | ||
} | ||
|
||
// draw - draw sprite on buffer, save background under sprite | ||
func (s *Sprite) draw(buf tcg.Buffer) { | ||
// copy background | ||
s.bg.BitBlt(0, 0, s.width, s.height, buf, s.x, s.y) | ||
|
||
// draw sprite | ||
var opts []tcg.BitBltOpt | ||
if s.mask != nil { | ||
opts = append(opts, tcg.BBMask(s.mask)) | ||
} | ||
buf.BitBlt(s.x, s.y, s.width, s.height, s.Buf, 0, 0, opts...) | ||
} | ||
|
||
// Withdraw - withdraw sprite from buffer | ||
func (s *Sprite) Withdraw(buf tcg.Buffer) *Sprite { | ||
if s.isDrawn { | ||
s.clear(buf) | ||
s.isDrawn = false | ||
} | ||
|
||
return s | ||
} | ||
|
||
// clear sprite on buffer | ||
func (s *Sprite) clear(buf tcg.Buffer) { | ||
buf.BitBlt(s.x, s.y, s.width, s.height, s.bg, 0, 0) | ||
} | ||
|
||
// MoveAbs - move sprite on buffer to absolute position, coordinates can be negative | ||
func (s *Sprite) MoveAbs(buf tcg.Buffer, x, y int) *Sprite { | ||
if s.isDrawn { | ||
s.clear(buf) | ||
} | ||
|
||
s.x = x | ||
s.y = y | ||
|
||
if s.isDrawn { | ||
s.draw(buf) | ||
} | ||
|
||
return s | ||
} | ||
|
||
// Move - move sprite on buffer to relative position | ||
func (s *Sprite) Move(buf tcg.Buffer, x, y int) *Sprite { | ||
return s.MoveAbs(buf, s.x+x, s.y+y) | ||
} |
Oops, something went wrong.