Noah Petherbridge
a316bafb12
Move all Doodle source code into the src/ subpackage and move the publicly shareable stuff into lib/, for example lib/ui and lib/render. This cleans up the git root and helps make the Doodle UI library more easily publishable as a separate open source project. Currently both lib/ui and lib/render import one or two things from doodle/src that need to be broken apart.
56 lines
1.2 KiB
Go
56 lines
1.2 KiB
Go
package sdl
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"git.kirsle.net/apps/doodle/lib/render"
|
|
"github.com/veandco/go-sdl2/sdl"
|
|
)
|
|
|
|
// Copy a texture into the renderer.
|
|
func (r *Renderer) Copy(t render.Texturer, src, dst render.Rect) {
|
|
if tex, ok := t.(*Texture); ok {
|
|
var (
|
|
a = RectToSDL(src)
|
|
b = RectToSDL(dst)
|
|
)
|
|
r.renderer.Copy(tex.tex, &a, &b)
|
|
}
|
|
}
|
|
|
|
// Texture can hold on to SDL textures for caching and optimization.
|
|
type Texture struct {
|
|
tex *sdl.Texture
|
|
width int32
|
|
height int32
|
|
}
|
|
|
|
// Size returns the dimensions of the texture.
|
|
func (t *Texture) Size() render.Rect {
|
|
return render.NewRect(t.width, t.height)
|
|
}
|
|
|
|
// NewBitmap initializes a texture from a bitmap image.
|
|
func (r *Renderer) NewBitmap(filename string) (render.Texturer, error) {
|
|
surface, err := sdl.LoadBMP(filename)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("NewBitmap: LoadBMP: %s", err)
|
|
}
|
|
defer surface.Free()
|
|
|
|
// TODO: chroma key color hardcoded to white here
|
|
key := sdl.MapRGB(surface.Format, 255, 255, 255)
|
|
surface.SetColorKey(true, key)
|
|
|
|
tex, err := r.renderer.CreateTextureFromSurface(surface)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("NewBitmap: create texture: %s", err)
|
|
}
|
|
|
|
return &Texture{
|
|
width: surface.W,
|
|
height: surface.H,
|
|
tex: tex,
|
|
}, nil
|
|
}
|