2018-07-22 00:12:22 +00:00
|
|
|
package render
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-07-24 03:10:53 +00:00
|
|
|
"math"
|
2018-07-22 00:12:22 +00:00
|
|
|
|
|
|
|
"git.kirsle.net/apps/doodle/events"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Engine is the interface for the rendering engine, keeping SDL-specific stuff
|
|
|
|
// far away from the core of Doodle.
|
|
|
|
type Engine interface {
|
|
|
|
Setup() error
|
|
|
|
|
|
|
|
// Poll for events like keypresses and mouse clicks.
|
|
|
|
Poll() (*events.State, error)
|
|
|
|
GetTicks() uint32
|
|
|
|
|
2018-07-22 03:43:01 +00:00
|
|
|
// Present presents the current state to the screen.
|
|
|
|
Present() error
|
2018-07-22 00:12:22 +00:00
|
|
|
|
|
|
|
// Clear the full canvas and set this color.
|
|
|
|
Clear(Color)
|
|
|
|
DrawPoint(Color, Point)
|
|
|
|
DrawLine(Color, Point, Point)
|
|
|
|
DrawRect(Color, Rect)
|
2018-07-22 03:43:01 +00:00
|
|
|
DrawBox(Color, Rect)
|
|
|
|
DrawText(Text, Point) error
|
2018-07-25 16:03:49 +00:00
|
|
|
ComputeTextRect(Text) (Rect, error)
|
2018-07-22 00:12:22 +00:00
|
|
|
|
|
|
|
// Delay for a moment using the render engine's delay method,
|
|
|
|
// implemented by sdl.Delay(uint32)
|
|
|
|
Delay(uint32)
|
|
|
|
|
|
|
|
// Tasks that the Setup function should defer until tear-down.
|
|
|
|
Teardown()
|
|
|
|
|
|
|
|
Loop() error // maybe?
|
|
|
|
}
|
|
|
|
|
|
|
|
// Point holds an X,Y coordinate value.
|
|
|
|
type Point struct {
|
|
|
|
X int32
|
|
|
|
Y int32
|
|
|
|
}
|
|
|
|
|
2018-07-25 16:03:49 +00:00
|
|
|
// NewPoint makes a new Point at an X,Y coordinate.
|
|
|
|
func NewPoint(x, y int32) Point {
|
|
|
|
return Point{
|
|
|
|
X: x,
|
|
|
|
Y: y,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-22 00:12:22 +00:00
|
|
|
func (p Point) String() string {
|
|
|
|
return fmt.Sprintf("Point<%d,%d>", p.X, p.Y)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rect has a coordinate and a width and height.
|
|
|
|
type Rect struct {
|
|
|
|
X int32
|
|
|
|
Y int32
|
|
|
|
W int32
|
|
|
|
H int32
|
|
|
|
}
|
|
|
|
|
2018-08-02 01:52:52 +00:00
|
|
|
// NewRect creates a rectangle of size `width` and `height`. The X,Y values
|
|
|
|
// are initialized to zero.
|
|
|
|
func NewRect(width, height int32) Rect {
|
|
|
|
return Rect{
|
|
|
|
W: width,
|
|
|
|
H: height,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-22 00:12:22 +00:00
|
|
|
func (r Rect) String() string {
|
|
|
|
return fmt.Sprintf("Rect<%d,%d,%d,%d>",
|
|
|
|
r.X, r.Y, r.W, r.H,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-08-02 01:52:52 +00:00
|
|
|
// Bigger returns if the given rect is larger than the current one.
|
|
|
|
func (r Rect) Bigger(other Rect) bool {
|
|
|
|
// TODO: don't know why this is !
|
|
|
|
return !(other.X < r.X || // Lefter
|
|
|
|
other.Y < r.Y || // Higher
|
|
|
|
other.W > r.W || // Wider
|
|
|
|
other.H > r.H) // Taller
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsZero returns if the Rect is uninitialized.
|
|
|
|
func (r Rect) IsZero() bool {
|
|
|
|
return r.X == 0 && r.Y == 0 && r.W == 0 && r.H == 0
|
|
|
|
}
|
|
|
|
|
2018-07-22 00:12:22 +00:00
|
|
|
// Text holds information for drawing text.
|
|
|
|
type Text struct {
|
2018-08-05 19:54:57 +00:00
|
|
|
Text string
|
|
|
|
Size int
|
|
|
|
Color Color
|
|
|
|
Padding int32
|
|
|
|
Stroke Color // Stroke color (if not zero)
|
|
|
|
Shadow Color // Drop shadow color (if not zero)
|
2018-07-22 00:12:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t Text) String() string {
|
2018-08-11 00:19:47 +00:00
|
|
|
return fmt.Sprintf(`Text<"%s" %dpx %s>`, t.Text, t.Size, t.Color)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsZero returns if the Text is the zero value.
|
|
|
|
func (t Text) IsZero() bool {
|
|
|
|
return t.Text == "" && t.Size == 0 && t.Color == Invisible && t.Padding == 0 && t.Stroke == Invisible && t.Shadow == Invisible
|
2018-07-22 00:12:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Common color names.
|
|
|
|
var (
|
2018-07-25 03:57:22 +00:00
|
|
|
Invisible = Color{}
|
|
|
|
White = RGBA(255, 255, 255, 255)
|
|
|
|
Grey = RGBA(153, 153, 153, 255)
|
|
|
|
Black = RGBA(0, 0, 0, 255)
|
|
|
|
SkyBlue = RGBA(0, 153, 255, 255)
|
|
|
|
Blue = RGBA(0, 0, 255, 255)
|
|
|
|
DarkBlue = RGBA(0, 0, 153, 255)
|
|
|
|
Red = RGBA(255, 0, 0, 255)
|
|
|
|
DarkRed = RGBA(153, 0, 0, 255)
|
|
|
|
Green = RGBA(0, 255, 0, 255)
|
|
|
|
DarkGreen = RGBA(0, 153, 0, 255)
|
|
|
|
Cyan = RGBA(0, 255, 255, 255)
|
|
|
|
DarkCyan = RGBA(0, 153, 153, 255)
|
|
|
|
Yellow = RGBA(255, 255, 0, 255)
|
|
|
|
DarkYellow = RGBA(153, 153, 0, 255)
|
|
|
|
Magenta = RGBA(255, 0, 255, 255)
|
|
|
|
Purple = RGBA(153, 0, 153, 255)
|
|
|
|
Pink = RGBA(255, 153, 255, 255)
|
2018-07-22 00:12:22 +00:00
|
|
|
)
|
2018-07-24 03:10:53 +00:00
|
|
|
|
|
|
|
// IterLine is a generator that returns the X,Y coordinates to draw a line.
|
|
|
|
// https://en.wikipedia.org/wiki/Digital_differential_analyzer_(graphics_algorithm)
|
|
|
|
func IterLine(x1, y1, x2, y2 int32) chan Point {
|
|
|
|
generator := make(chan Point)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
var (
|
|
|
|
dx = float64(x2 - x1)
|
|
|
|
dy = float64(y2 - y1)
|
|
|
|
)
|
|
|
|
var step float64
|
|
|
|
if math.Abs(dx) >= math.Abs(dy) {
|
|
|
|
step = math.Abs(dx)
|
|
|
|
} else {
|
|
|
|
step = math.Abs(dy)
|
|
|
|
}
|
|
|
|
|
|
|
|
dx = dx / step
|
|
|
|
dy = dy / step
|
|
|
|
x := float64(x1)
|
|
|
|
y := float64(y1)
|
|
|
|
for i := 0; i <= int(step); i++ {
|
|
|
|
generator <- Point{
|
|
|
|
X: int32(x),
|
|
|
|
Y: int32(y),
|
|
|
|
}
|
|
|
|
x += dx
|
|
|
|
y += dy
|
|
|
|
}
|
|
|
|
|
|
|
|
close(generator)
|
|
|
|
}()
|
|
|
|
|
|
|
|
return generator
|
|
|
|
}
|
|
|
|
|
2018-07-25 00:44:32 +00:00
|
|
|
// IterLine2 works with two Points rather than four coordinates.
|
2018-07-24 03:10:53 +00:00
|
|
|
func IterLine2(p1 Point, p2 Point) chan Point {
|
|
|
|
return IterLine(p1.X, p1.Y, p2.X, p2.Y)
|
|
|
|
}
|