Make it a Go module + minor tweaks

This commit is contained in:
Noah 2020-03-09 17:28:49 -07:00
parent 4d008b5c46
commit 87fabf6a8d
9 changed files with 93 additions and 9 deletions

View File

@ -13,10 +13,11 @@ import (
// Texture can hold on to cached image textures.
type Texture struct {
data string // data:image/png URI
image js.Value // DOM image element
canvas js.Value // Warmed up canvas element
ctx2d js.Value // 2D drawing context for the canvas.
data string // data:image/png URI
img image.Image // underlying Go image data
image js.Value // DOM image element
canvas js.Value // Warmed up canvas element
ctx2d js.Value // 2D drawing context for the canvas.
width int
height int
}
@ -39,6 +40,7 @@ func (e *Engine) StoreTexture(name string, img image.Image) (render.Texturer, er
tex := &Texture{
data: dataURI,
img: img,
width: width,
height: height,
}

View File

@ -53,6 +53,16 @@ func FromColor(from color.Color) Color {
)
}
// ToRGBA converts to a standard Go color.Color
func (c Color) ToRGBA() color.RGBA {
return color.RGBA{
R: c.Red,
G: c.Green,
B: c.Blue,
A: c.Alpha,
}
}
// MustHexColor parses a color from hex code or panics.
func MustHexColor(hex string) Color {
color, err := HexColor(hex)

8
go.mod Normal file
View File

@ -0,0 +1,8 @@
module git.kirsle.net/go/render
go 1.13
require (
github.com/veandco/go-sdl2 v0.4.1
golang.org/x/image v0.0.0-20200119044424-58c23975cae1
)

5
go.sum Normal file
View File

@ -0,0 +1,5 @@
github.com/veandco/go-sdl2 v0.4.1 h1:HmSBvVmKWI8LAOeCfTTM8R33rMyPcs6U3o8n325c9Qg=
github.com/veandco/go-sdl2 v0.4.1/go.mod h1:FB+kTpX9YTE+urhYiClnRzpOXbiWgaU3+5F2AB78DPg=
golang.org/x/image v0.0.0-20200119044424-58c23975cae1 h1:5h3ngYt7+vXCDZCup/HkCQgW5XwmSvR/nA2JmJ0RErg=
golang.org/x/image v0.0.0-20200119044424-58c23975cae1/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

55
image.go Normal file
View File

@ -0,0 +1,55 @@
package render
import (
"errors"
"image"
"image/gif"
"image/jpeg"
"image/png"
"os"
"path/filepath"
"golang.org/x/image/bmp"
)
// OpenImage opens an image file from disk.
//
// Supported file types are: jpeg, gif, png, bmp.
func OpenImage(filename string) (image.Image, error) {
fh, err := os.Open(filename)
if err != nil {
return nil, err
}
var img image.Image
switch filepath.Ext(filename) {
case ".jpg":
fallthrough
case ".jpeg":
img, err = jpeg.Decode(fh)
case ".png":
img, err = png.Decode(fh)
case ".bmp":
img, err = bmp.Decode(fh)
case ".gif":
img, err = gif.Decode(fh)
default:
return nil, errors.New("unsupported file type")
}
return img, err
}
// ImageToRGBA converts a Go image.Image into an image.RGBA.
func ImageToRGBA(input image.Image) *image.RGBA {
var bounds = input.Bounds()
var rgba = image.NewRGBA(bounds)
for x := bounds.Min.X; x < bounds.Max.X; x++ {
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
color := input.At(x, y)
rgba.Set(x, y, color)
}
}
return rgba
}

View File

@ -24,10 +24,9 @@ func (r *Renderer) DrawPoint(color render.Color, point render.Point) {
// DrawLine draws a line between two points.
func (r *Renderer) DrawLine(color render.Color, a, b render.Point) {
if color != r.lastColor {
r.renderer.SetDrawColor(color.Red, color.Green, color.Blue, color.Alpha)
for pt := range render.IterLine(a, b) {
r.DrawPoint(color, pt)
}
r.renderer.DrawLine(int32(a.X), int32(a.Y), int32(b.X), int32(b.Y))
}
// DrawRect draws a rectangle.

View File

@ -156,6 +156,7 @@ func (r *Renderer) Poll() (*event.State, error) {
case sdl.SCANCODE_DOWN:
s.Down = t.State == 1
case sdl.SCANCODE_LSHIFT:
fallthrough
case sdl.SCANCODE_RSHIFT:
s.Shift = t.State == 1
case sdl.SCANCODE_LALT:

View File

@ -10,7 +10,7 @@ import (
)
// TODO: font filenames
var defaultFontFilename = "DejaVuSans.ttf"
var DefaultFontFilename = "DejaVuSans.ttf"
// Font holds cached SDL_TTF structures for loaded fonts. They are created
// automatically when fonts are either preinstalled (InstallFont) or loaded for
@ -37,7 +37,7 @@ func InstallFont(filename string, binary []byte) {
// LoadFont loads and caches the font at a given size.
func LoadFont(filename string, size int) (*ttf.Font, error) {
if filename == "" {
filename = defaultFontFilename
filename = DefaultFontFilename
}
// Cached font available?

4
version.go Normal file
View File

@ -0,0 +1,4 @@
package render
// Version number of the render library.
const Version = "0.1.0"