more documentation; renamed W/H to Width/Height on Image type

This commit is contained in:
Thomas Friedel 2018-03-27 15:57:01 +02:00
parent a9aa15d1d6
commit 37767fa86f
4 changed files with 34 additions and 3 deletions

View file

@ -157,7 +157,8 @@ var (
// LoadGL needs to be called once per GL context to load the GL assets
// that canvas needs. The parameter is an implementation of the GL interface
// in this package that should make this package neutral to GL implementations
// in this package that should make this package neutral to GL implementations.
// The goglimpl subpackage contains an implementation based on Go-GL v3.2
func LoadGL(glimpl GL) (err error) {
gli = glimpl

View file

@ -9,6 +9,8 @@ import (
var imageBufTex uint32
var imageBuf []byte
// GetImageData returns an RGBA image of the currently displayed image. The
// alpha channel is always opaque
func (cv *Canvas) GetImageData(x, y, w, h int) *image.RGBA {
cv.activate()
@ -42,6 +44,7 @@ func (cv *Canvas) GetImageData(x, y, w, h int) *image.RGBA {
return rgba
}
// PutImageData puts the given image at the given x/y coordinates
func (cv *Canvas) PutImageData(img *image.RGBA, x, y int) {
cv.activate()

View file

@ -9,6 +9,7 @@ import (
"unsafe"
)
// Image represents a loaded image that can be used in various drawing functions
type Image struct {
w, h int
tex uint32
@ -17,6 +18,11 @@ type Image struct {
var images = make(map[string]*Image)
// LoadImage loads an image. The src parameter can be either an image from the
// standard image package, a byte slice that will be loaded, or a file name
// string. If you want the canvas package to load the image, make sure you
// import the required format packages. Name is an optional name that can also
// be used in draw functions
func LoadImage(src interface{}, name string) (*Image, error) {
var img *Image
var err error
@ -163,15 +169,33 @@ func loadImageConverted(src image.Image) (*Image, error) {
return img, nil
}
func (img *Image) W() int { return img.w }
func (img *Image) H() int { return img.h }
// Width returns the width of the image
func (img *Image) Width() int { return img.w }
// Height returns the height of the image
func (img *Image) Height() int { return img.h }
// Size returns the width and height of the image
func (img *Image) Size() (int, int) { return img.w, img.h }
// Delete deletes the image from memory. Any draw calls with a deleted image
// will not do anything
func (img *Image) Delete() {
gli.DeleteTextures(1, &img.tex)
img.deleted = true
}
// Draw image draws the given image to the given coordinates. The image
// parameter can be an Image loaded by LoadImage, a file name string that will
// be loaded and cached, or a name string that corresponds to a previously
// loaded image with the same name parameter.
//
// The coordinates must be one of the following:
// DrawImage("image", dx, dy)
// DrawImage("image", dx, dy, dw, dh)
// DrawImage("image", sx, sy, sw, sh, dx, dy, dw, dh)
// Where dx/dy/dw/dh are the destination coordinates and sx/sy/sw/sh are the
// source coordinates
func (cv *Canvas) DrawImage(image interface{}, coords ...float64) {
var img *Image
switch v := image.(type) {

View file

@ -789,6 +789,9 @@ const (
gl_ZERO = 0
)
// The GL interface is used to make this package independent of any particular
// OpenGL implementation. The goglimpl subpackage conatins an implementation of
// this interface based on Go-GL v3.2
type GL interface {
Ptr(data interface{}) unsafe.Pointer
PtrOffset(offset int) unsafe.Pointer