mirror of https://github.com/liamg/aminal.git
relocated font package and tidied up font rendering
This commit is contained in:
parent
dab508e6e2
commit
e5e7d09656
|
@ -28,11 +28,7 @@
|
|||
[[projects]]
|
||||
branch = "master"
|
||||
name = "github.com/golang/freetype"
|
||||
packages = [
|
||||
".",
|
||||
"raster",
|
||||
"truetype"
|
||||
]
|
||||
packages = [".","raster","truetype"]
|
||||
revision = "e2365dfdc4a05e4b8299a783240d4a7d5a65d4e4"
|
||||
|
||||
[[projects]]
|
||||
|
@ -41,12 +37,6 @@
|
|||
revision = "fa756f09eeb418bf1cc6268c66ceaad9bb98f598"
|
||||
version = "v1.1.2"
|
||||
|
||||
[[projects]]
|
||||
branch = "master"
|
||||
name = "github.com/liamg/glfont"
|
||||
packages = ["."]
|
||||
revision = "d24783da55c81fd75c8e7c0897ee3f7d336f8b44"
|
||||
|
||||
[[projects]]
|
||||
name = "github.com/pmezard/go-difflib"
|
||||
packages = ["difflib"]
|
||||
|
@ -61,10 +51,7 @@
|
|||
|
||||
[[projects]]
|
||||
name = "github.com/stretchr/testify"
|
||||
packages = [
|
||||
"assert",
|
||||
"require"
|
||||
]
|
||||
packages = ["assert","require"]
|
||||
revision = "f35b8ab0b5a2cef36673838d662e249dd9c94686"
|
||||
version = "v1.2.2"
|
||||
|
||||
|
@ -82,29 +69,19 @@
|
|||
|
||||
[[projects]]
|
||||
name = "go.uber.org/zap"
|
||||
packages = [
|
||||
".",
|
||||
"buffer",
|
||||
"internal/bufferpool",
|
||||
"internal/color",
|
||||
"internal/exit",
|
||||
"zapcore"
|
||||
]
|
||||
packages = [".","buffer","internal/bufferpool","internal/color","internal/exit","zapcore"]
|
||||
revision = "ff33455a0e382e8a81d14dd7c922020b6b5e7982"
|
||||
version = "v1.9.1"
|
||||
|
||||
[[projects]]
|
||||
branch = "master"
|
||||
name = "golang.org/x/image"
|
||||
packages = [
|
||||
"font",
|
||||
"math/fixed"
|
||||
]
|
||||
packages = ["font","math/fixed"]
|
||||
revision = "c73c2afc3b812cdd6385de5a50616511c4a3d458"
|
||||
|
||||
[solve-meta]
|
||||
analyzer-name = "dep"
|
||||
analyzer-version = 1
|
||||
inputs-digest = "18e6b85742181970b4dbe1aae2d74b033fbca809df825b8a03c8e3ba6b5ba29c"
|
||||
inputs-digest = "511d10ed41125fb4859199ab90d9779aaa5a5134f6ca26eca1d517d200404124"
|
||||
solver-name = "gps-cdcl"
|
||||
solver-version = 1
|
||||
|
|
220
glfont/font.go
220
glfont/font.go
|
@ -1,29 +1,31 @@
|
|||
package glfont
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"image"
|
||||
"image/draw"
|
||||
"math"
|
||||
"os"
|
||||
|
||||
"github.com/go-gl/gl/all-core/gl"
|
||||
)
|
||||
|
||||
// Direction represents the direction in which strings should be rendered.
|
||||
type Direction uint8
|
||||
|
||||
// Known directions.
|
||||
const (
|
||||
LeftToRight Direction = iota // E.g.: Latin
|
||||
RightToLeft // E.g.: Arabic
|
||||
TopToBottom // E.g.: Chinese
|
||||
"github.com/golang/freetype"
|
||||
"github.com/golang/freetype/truetype"
|
||||
"golang.org/x/image/font"
|
||||
"golang.org/x/image/math/fixed"
|
||||
)
|
||||
|
||||
// A Font allows rendering of text to an OpenGL context.
|
||||
type Font struct {
|
||||
fontChar []*character
|
||||
characters map[rune]*character
|
||||
vao uint32
|
||||
vbo uint32
|
||||
program uint32
|
||||
texture uint32 // Holds the glyph texture id.
|
||||
color color
|
||||
ttf *truetype.Font
|
||||
scale float32
|
||||
linePadding float32
|
||||
lineHeight float32
|
||||
}
|
||||
|
||||
type color struct {
|
||||
|
@ -34,7 +36,7 @@ type color struct {
|
|||
}
|
||||
|
||||
//LoadFont loads the specified font at the given scale.
|
||||
func LoadFont(file string, scale int32, windowWidth int, windowHeight int) (*Font, error) {
|
||||
func LoadFont(file string, scale float32, windowWidth int, windowHeight int) (*Font, error) {
|
||||
fd, err := os.Open(file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -54,7 +56,7 @@ func LoadFont(file string, scale int32, windowWidth int, windowHeight int) (*Fon
|
|||
resUniform := gl.GetUniformLocation(program, gl.Str("resolution\x00"))
|
||||
gl.Uniform2f(resUniform, float32(windowWidth), float32(windowHeight))
|
||||
|
||||
return LoadTrueTypeFont(program, fd, scale, 32, 127, LeftToRight)
|
||||
return LoadTrueTypeFont(program, fd, scale)
|
||||
}
|
||||
|
||||
//SetColor allows you to set the text color to be used when you draw the text
|
||||
|
@ -70,10 +72,22 @@ func (f *Font) UpdateResolution(windowWidth int, windowHeight int) {
|
|||
resUniform := gl.GetUniformLocation(f.program, gl.Str("resolution\x00"))
|
||||
gl.Uniform2f(resUniform, float32(windowWidth), float32(windowHeight))
|
||||
gl.UseProgram(0)
|
||||
//f.characters = map[rune]*character{}
|
||||
}
|
||||
|
||||
func (f *Font) LineHeight() float32 {
|
||||
return f.lineHeight
|
||||
}
|
||||
|
||||
func (f *Font) LinePadding() float32 {
|
||||
return f.linePadding
|
||||
}
|
||||
|
||||
//Printf draws a string to the screen, takes a list of arguments like printf
|
||||
func (f *Font) Print(x, y float32, scale float32, text string) error {
|
||||
func (f *Font) Print(x, y float32, text string) error {
|
||||
|
||||
x = float32(math.Round(float64(x)))
|
||||
y = float32(math.Round(float64(y)))
|
||||
|
||||
indices := []rune(text)
|
||||
|
||||
|
@ -81,8 +95,6 @@ func (f *Font) Print(x, y float32, scale float32, text string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
lowChar := rune(32)
|
||||
|
||||
//setup blending mode
|
||||
gl.Enable(gl.BLEND)
|
||||
gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
|
||||
|
@ -104,19 +116,17 @@ func (f *Font) Print(x, y float32, scale float32, text string) error {
|
|||
//get rune
|
||||
runeIndex := indices[i]
|
||||
|
||||
//skip runes that are not in font chacter range
|
||||
if int(runeIndex)-int(lowChar) > len(f.fontChar) || runeIndex < lowChar {
|
||||
continue
|
||||
//find rune in fontChar list
|
||||
ch, err := f.GetRune(runeIndex)
|
||||
if err != nil {
|
||||
return err // @todo ignore errors?
|
||||
}
|
||||
|
||||
//find rune in fontChar list
|
||||
ch := f.fontChar[runeIndex-lowChar]
|
||||
|
||||
//calculate position and size for current rune
|
||||
xpos := x + float32(ch.bearingH)*scale
|
||||
ypos := y - float32(ch.height-ch.bearingV)*scale
|
||||
w := float32(ch.width) * scale
|
||||
h := float32(ch.height) * scale
|
||||
xpos := x + float32(ch.bearingH)
|
||||
ypos := y - float32(+ch.height-ch.bearingV)
|
||||
w := float32(ch.width)
|
||||
h := float32(ch.height)
|
||||
|
||||
//set quad positions
|
||||
var x1 = xpos
|
||||
|
@ -147,7 +157,7 @@ func (f *Font) Print(x, y float32, scale float32, text string) error {
|
|||
|
||||
gl.BindBuffer(gl.ARRAY_BUFFER, 0)
|
||||
// Now advance cursors for next glyph (note that advance is number of 1/64 pixels)
|
||||
x += float32((ch.advance >> 6)) * scale // Bitshift by 6 to get value in pixels (2^6 = 64 (divide amount of 1/64th pixels by 64 to get amount of pixels))
|
||||
x += float32((ch.advance >> 6)) // Bitshift by 6 to get value in pixels (2^6 = 64 (divide amount of 1/64th pixels by 64 to get amount of pixels))
|
||||
|
||||
}
|
||||
|
||||
|
@ -161,79 +171,129 @@ func (f *Font) Print(x, y float32, scale float32, text string) error {
|
|||
}
|
||||
|
||||
//Width returns the width of a piece of text in pixels
|
||||
func (f *Font) Width(scale float32, text string) float32 {
|
||||
func (f *Font) Size(text string) (float32, float32) {
|
||||
|
||||
var width float32
|
||||
|
||||
indices := []rune(text)
|
||||
|
||||
if len(indices) == 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
lowChar := rune(32)
|
||||
|
||||
// Iterate through all characters in string
|
||||
for i := range indices {
|
||||
|
||||
//get rune
|
||||
runeIndex := indices[i]
|
||||
|
||||
//skip runes that are not in font chacter range
|
||||
if int(runeIndex)-int(lowChar) > len(f.fontChar) || runeIndex < lowChar {
|
||||
continue
|
||||
}
|
||||
|
||||
//find rune in fontChar list
|
||||
ch := f.fontChar[runeIndex-lowChar]
|
||||
|
||||
// Now advance cursors for next glyph (note that advance is number of 1/64 pixels)
|
||||
width += float32((ch.advance >> 6)) * scale // Bitshift by 6 to get value in pixels (2^6 = 64 (divide amount of 1/64th pixels by 64 to get amount of pixels))
|
||||
|
||||
}
|
||||
|
||||
return width
|
||||
}
|
||||
|
||||
//Height returns the height of a piece of text in pixels
|
||||
func (f *Font) Height(scale float32, text string) float32 {
|
||||
|
||||
var baseHeight float32
|
||||
var height float32
|
||||
|
||||
indices := []rune(text)
|
||||
|
||||
if len(indices) == 0 {
|
||||
return 0
|
||||
return 0, 0
|
||||
}
|
||||
|
||||
lowChar := rune(32)
|
||||
|
||||
// Iterate through all characters in string
|
||||
for i := range indices {
|
||||
|
||||
//get rune
|
||||
runeIndex := indices[i]
|
||||
|
||||
if int(runeIndex) == 0x0a {
|
||||
baseHeight = height
|
||||
height = 0
|
||||
}
|
||||
|
||||
//skip runes that are not in font chacter range
|
||||
if int(runeIndex)-int(lowChar) > len(f.fontChar) || runeIndex < lowChar {
|
||||
//find rune in fontChar list
|
||||
ch, err := f.GetRune(runeIndex)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
//find rune in fontChar list
|
||||
ch := f.fontChar[runeIndex-lowChar]
|
||||
// Now advance cursors for next glyph (note that advance is number of 1/64 pixels)
|
||||
width += float32((ch.advance >> 6)) // Bitshift by 6 to get value in pixels (2^6 = 64 (divide amount of 1/64th pixels by 64 to get amount of pixels))
|
||||
|
||||
// Now advance cursors for next glyph (note that advance is number of 1/64 pixels)
|
||||
if float32(ch.height)*scale > height {
|
||||
height = float32(ch.height) * scale
|
||||
if float32(ch.height)*f.scale > height {
|
||||
height = float32(ch.height)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return baseHeight + height
|
||||
return width, height
|
||||
}
|
||||
|
||||
func (f *Font) GetRune(r rune) (*character, error) {
|
||||
|
||||
cc, ok := f.characters[r]
|
||||
if ok {
|
||||
return cc, nil
|
||||
}
|
||||
|
||||
char := new(character)
|
||||
|
||||
//create new face to measure glyph diamensions
|
||||
ttfFace := truetype.NewFace(f.ttf, &truetype.Options{
|
||||
Size: float64(f.scale),
|
||||
DPI: 72,
|
||||
Hinting: font.HintingFull,
|
||||
})
|
||||
|
||||
gBnd, gAdv, ok := ttfFace.GlyphBounds(r)
|
||||
if ok != true {
|
||||
return nil, fmt.Errorf("ttf face glyphBounds error")
|
||||
}
|
||||
|
||||
gh := int32((gBnd.Max.Y - gBnd.Min.Y) >> 6)
|
||||
gw := int32((gBnd.Max.X - gBnd.Min.X) >> 6)
|
||||
|
||||
//if gylph has no diamensions set to a max value
|
||||
if gw == 0 || gh == 0 {
|
||||
gBnd = f.ttf.Bounds(fixed.Int26_6(f.scale))
|
||||
gw = int32((gBnd.Max.X - gBnd.Min.X) >> 6)
|
||||
gh = int32((gBnd.Max.Y - gBnd.Min.Y) >> 6)
|
||||
|
||||
//above can sometimes yield 0 for font smaller than 48pt, 1 is minimum
|
||||
if gw == 0 || gh == 0 {
|
||||
gw = 1
|
||||
gh = 1
|
||||
}
|
||||
}
|
||||
|
||||
//The glyph's ascent and descent equal -bounds.Min.Y and +bounds.Max.Y.
|
||||
gAscent := int(-gBnd.Min.Y) >> 6
|
||||
gdescent := int(gBnd.Max.Y) >> 6
|
||||
|
||||
//set w,h and adv, bearing V and bearing H in char
|
||||
char.width = int(gw)
|
||||
char.height = int(gh)
|
||||
char.advance = int(gAdv)
|
||||
char.bearingV = gdescent
|
||||
char.bearingH = (int(gBnd.Min.X) >> 6)
|
||||
|
||||
//create image to draw glyph
|
||||
fg, bg := image.White, image.Black
|
||||
rect := image.Rect(0, 0, int(gw), int(gh))
|
||||
rgba := image.NewRGBA(rect)
|
||||
draw.Draw(rgba, rgba.Bounds(), bg, image.ZP, draw.Src)
|
||||
|
||||
//create a freetype context for drawing
|
||||
c := freetype.NewContext()
|
||||
c.SetDPI(72)
|
||||
c.SetFont(f.ttf)
|
||||
c.SetFontSize(float64(f.scale))
|
||||
c.SetClip(rgba.Bounds())
|
||||
c.SetDst(rgba)
|
||||
c.SetSrc(fg)
|
||||
c.SetHinting(font.HintingFull)
|
||||
|
||||
//set the glyph dot
|
||||
px := 0 - (int(gBnd.Min.X) >> 6)
|
||||
py := (gAscent)
|
||||
pt := freetype.Pt(px, py)
|
||||
|
||||
// Draw the text from mask to image
|
||||
if _, err := c.DrawString(string(r), pt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Generate texture
|
||||
var texture uint32
|
||||
gl.GenTextures(1, &texture)
|
||||
gl.BindTexture(gl.TEXTURE_2D, texture)
|
||||
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
|
||||
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
|
||||
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
|
||||
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
|
||||
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, int32(rgba.Rect.Dx()), int32(rgba.Rect.Dy()), 0,
|
||||
gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(rgba.Pix))
|
||||
|
||||
char.textureID = texture
|
||||
|
||||
f.characters[r] = char
|
||||
|
||||
return char, nil
|
||||
}
|
||||
|
|
|
@ -2,15 +2,12 @@ package glfont
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/go-gl/gl/all-core/gl"
|
||||
"github.com/golang/freetype"
|
||||
"github.com/golang/freetype/truetype"
|
||||
"golang.org/x/image/font"
|
||||
"golang.org/x/image/math/fixed"
|
||||
"image"
|
||||
"image/draw"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
|
||||
"github.com/go-gl/gl/all-core/gl"
|
||||
"github.com/golang/freetype/truetype"
|
||||
"golang.org/x/image/font"
|
||||
)
|
||||
|
||||
type character struct {
|
||||
|
@ -23,112 +20,37 @@ type character struct {
|
|||
}
|
||||
|
||||
//LoadTrueTypeFont builds a set of textures based on a ttf files gylphs
|
||||
func LoadTrueTypeFont(program uint32, r io.Reader, scale int32, low, high rune, dir Direction) (*Font, error) {
|
||||
func LoadTrueTypeFont(program uint32, r io.Reader, scale float32) (*Font, error) {
|
||||
data, err := ioutil.ReadAll(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Read the truetype font.
|
||||
ttf, err := truetype.Parse(data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
//make Font stuct type
|
||||
f := new(Font)
|
||||
f.fontChar = make([]*character, 0, high-low+1)
|
||||
f.scale = scale
|
||||
f.characters = map[rune]*character{}
|
||||
f.program = program //set shader program
|
||||
// Read the truetype font.
|
||||
f.ttf, err = truetype.Parse(data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
f.SetColor(1.0, 1.0, 1.0, 1.0) //set default white
|
||||
|
||||
//make each gylph
|
||||
for ch := low; ch <= high; ch++ {
|
||||
sample := "abcdefghijklmnopqrstuvqxyzABCDEFGHIJKLMNOPQRSTUVWXYZ█"
|
||||
|
||||
char := new(character)
|
||||
|
||||
//create new face to measure glyph diamensions
|
||||
ttfFace := truetype.NewFace(ttf, &truetype.Options{
|
||||
Size: float64(scale),
|
||||
ttfFace := truetype.NewFace(f.ttf, &truetype.Options{
|
||||
Size: float64(f.scale),
|
||||
DPI: 72,
|
||||
Hinting: font.HintingFull,
|
||||
})
|
||||
|
||||
gBnd, gAdv, ok := ttfFace.GlyphBounds(ch)
|
||||
if ok != true {
|
||||
return nil, fmt.Errorf("ttf face glyphBounds error")
|
||||
}
|
||||
_, h := f.Size(sample)
|
||||
f.linePadding = h - float32(ttfFace.Metrics().Height>>6)
|
||||
f.lineHeight = h
|
||||
|
||||
gh := int32((gBnd.Max.Y - gBnd.Min.Y) >> 6)
|
||||
gw := int32((gBnd.Max.X - gBnd.Min.X) >> 6)
|
||||
|
||||
//if gylph has no diamensions set to a max value
|
||||
if gw == 0 || gh == 0 {
|
||||
gBnd = ttf.Bounds(fixed.Int26_6(scale))
|
||||
gw = int32((gBnd.Max.X - gBnd.Min.X) >> 6)
|
||||
gh = int32((gBnd.Max.Y - gBnd.Min.Y) >> 6)
|
||||
|
||||
//above can sometimes yield 0 for font smaller than 48pt, 1 is minimum
|
||||
if gw == 0 || gh == 0 {
|
||||
gw = 1
|
||||
gh = 1
|
||||
}
|
||||
}
|
||||
|
||||
//The glyph's ascent and descent equal -bounds.Min.Y and +bounds.Max.Y.
|
||||
gAscent := int(-gBnd.Min.Y) >> 6
|
||||
gdescent := int(gBnd.Max.Y) >> 6
|
||||
|
||||
//set w,h and adv, bearing V and bearing H in char
|
||||
char.width = int(gw)
|
||||
char.height = int(gh)
|
||||
char.advance = int(gAdv)
|
||||
char.bearingV = gdescent
|
||||
char.bearingH = (int(gBnd.Min.X) >> 6)
|
||||
|
||||
//create image to draw glyph
|
||||
fg, bg := image.White, image.Black
|
||||
rect := image.Rect(0, 0, int(gw), int(gh))
|
||||
rgba := image.NewRGBA(rect)
|
||||
draw.Draw(rgba, rgba.Bounds(), bg, image.ZP, draw.Src)
|
||||
|
||||
//create a freetype context for drawing
|
||||
c := freetype.NewContext()
|
||||
c.SetDPI(72)
|
||||
c.SetFont(ttf)
|
||||
c.SetFontSize(float64(scale))
|
||||
c.SetClip(rgba.Bounds())
|
||||
c.SetDst(rgba)
|
||||
c.SetSrc(fg)
|
||||
c.SetHinting(font.HintingFull)
|
||||
|
||||
//set the glyph dot
|
||||
px := 0 - (int(gBnd.Min.X) >> 6)
|
||||
py := (gAscent)
|
||||
pt := freetype.Pt(px, py)
|
||||
|
||||
// Draw the text from mask to image
|
||||
_, err = c.DrawString(string(ch), pt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Generate texture
|
||||
var texture uint32
|
||||
gl.GenTextures(1, &texture)
|
||||
gl.BindTexture(gl.TEXTURE_2D, texture)
|
||||
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
|
||||
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
|
||||
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
|
||||
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
|
||||
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, int32(rgba.Rect.Dx()), int32(rgba.Rect.Dy()), 0,
|
||||
gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(rgba.Pix))
|
||||
|
||||
char.textureID = texture
|
||||
|
||||
//add char to fontChar list
|
||||
f.fontChar = append(f.fontChar, char)
|
||||
|
||||
}
|
||||
fmt.Printf("Line height: %f px, padding: %f px", f.lineHeight, f.linePadding)
|
||||
|
||||
gl.BindTexture(gl.TEXTURE_2D, 0)
|
||||
|
||||
|
|
|
@ -23,7 +23,7 @@ type GUI struct {
|
|||
width int //window width in pixels
|
||||
height int //window height in pixels
|
||||
font *glfont.Font
|
||||
fontScale int32
|
||||
fontScale float32
|
||||
renderer Renderer
|
||||
colourAttr uint32
|
||||
}
|
||||
|
@ -122,14 +122,14 @@ func (gui *GUI) Render() error {
|
|||
|
||||
gui.logger.Debugf("Loading font...")
|
||||
if err := gui.loadFont("./fonts/Hack-Regular.ttf"); err != nil {
|
||||
//if err := gui.loadFont("./fonts/Roboto.ttf"); err != nil {
|
||||
//if err := gui.loadFont("./fonts/envypn-15.ttf"); err != nil {
|
||||
return fmt.Errorf("Failed to load font: %s", err)
|
||||
}
|
||||
|
||||
changeChan := make(chan bool, 1)
|
||||
titleChan := make(chan bool, 1)
|
||||
|
||||
gui.renderer = NewOpenGLRenderer(gui.config, gui.font, gui.fontScale, 0, 0, gui.width, gui.height, gui.colourAttr, program)
|
||||
gui.renderer = NewOpenGLRenderer(gui.config, gui.font, 0, 0, gui.width, gui.height, gui.colourAttr, program)
|
||||
|
||||
gui.window.SetFramebufferSizeCallback(gui.resize)
|
||||
gui.window.SetKeyCallback(gui.key)
|
||||
|
|
|
@ -41,7 +41,7 @@ func (gui *GUI) mouseButtonCallback(w *glfw.Window, button glfw.MouseButton, act
|
|||
b := rune(byte(button & 0xff))
|
||||
x := rune(byte((gui.terminal.ActiveBuffer().CursorColumn() + 31) & 0xff))
|
||||
y := rune(byte((gui.terminal.ActiveBuffer().CursorLine() + 31) & 0xff))
|
||||
packet := fmt.Sprintf("\x1b[%c%c%cM", b, x, y)
|
||||
packet := fmt.Sprintf("\x1b[M%c%c%c", b, x, y)
|
||||
gui.terminal.Write([]byte(packet))
|
||||
}
|
||||
case terminal.MouseModeVT200: // normal
|
||||
|
@ -95,7 +95,7 @@ func (gui *GUI) mouseButtonCallback(w *glfw.Window, button glfw.MouseButton, act
|
|||
}
|
||||
x := rune(byte((gui.terminal.ActiveBuffer().CursorColumn() + 31) & 0xff))
|
||||
y := rune(byte((gui.terminal.ActiveBuffer().CursorLine() + 31) & 0xff))
|
||||
packet := fmt.Sprintf("\x1b[%c%c%cM", b, x, y)
|
||||
packet := fmt.Sprintf("\x1b[M%c%c%c", b, x, y)
|
||||
gui.terminal.Write([]byte(packet))
|
||||
|
||||
case terminal.MouseModeVT200Highlight:
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package gui
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
|
||||
"github.com/go-gl/gl/all-core/gl"
|
||||
|
@ -24,10 +23,8 @@ type OpenGLRenderer struct {
|
|||
areaHeight int
|
||||
areaX int
|
||||
areaY int
|
||||
fontScale int32
|
||||
cellWidth float32
|
||||
cellHeight float32
|
||||
verticalCellPadding float32
|
||||
termCols uint
|
||||
termRows uint
|
||||
cellPositions map[[2]uint][2]float32
|
||||
|
@ -49,10 +46,13 @@ type rectangle struct {
|
|||
|
||||
func (r *OpenGLRenderer) newRectangle(x float32, y float32, colourAttr uint32) *rectangle {
|
||||
|
||||
x = (x - float32(r.areaWidth/2)) / float32(r.areaWidth/2)
|
||||
y = -(y - float32(r.areaHeight/2)) / float32(r.areaHeight/2)
|
||||
w := r.cellWidth / float32(r.areaWidth/2)
|
||||
h := r.cellHeight / float32(r.areaHeight/2)
|
||||
halfAreaWidth := float32(r.areaWidth / 2)
|
||||
halfAreaHeight := float32(r.areaHeight / 2)
|
||||
|
||||
x = (x - halfAreaWidth) / halfAreaWidth
|
||||
y = -(y - halfAreaHeight) / halfAreaHeight
|
||||
w := r.cellWidth / halfAreaWidth
|
||||
h := r.cellHeight / halfAreaHeight
|
||||
|
||||
rect := &rectangle{
|
||||
points: []float32{
|
||||
|
@ -126,13 +126,12 @@ func (rect *rectangle) Free() {
|
|||
gl.DeleteBuffers(1, &rect.cv)
|
||||
}
|
||||
|
||||
func NewOpenGLRenderer(config *config.Config, font *glfont.Font, fontScale int32, areaX int, areaY int, areaWidth int, areaHeight int, colourAttr uint32, program uint32) *OpenGLRenderer {
|
||||
func NewOpenGLRenderer(config *config.Config, font *glfont.Font, areaX int, areaY int, areaWidth int, areaHeight int, colourAttr uint32, program uint32) *OpenGLRenderer {
|
||||
r := &OpenGLRenderer{
|
||||
areaWidth: areaWidth,
|
||||
areaHeight: areaHeight,
|
||||
areaX: areaX,
|
||||
areaY: areaY,
|
||||
fontScale: fontScale,
|
||||
cellPositions: map[[2]uint][2]float32{},
|
||||
rectangles: map[[2]uint]*rectangle{},
|
||||
config: config,
|
||||
|
@ -155,35 +154,15 @@ func (r *OpenGLRenderer) SetArea(areaX int, areaY int, areaWidth int, areaHeight
|
|||
r.SetFont(r.font)
|
||||
}
|
||||
|
||||
func (r *OpenGLRenderer) SetFontScale(fontScale int32) {
|
||||
r.fontScale = fontScale
|
||||
r.SetFont(r.font)
|
||||
}
|
||||
|
||||
func (r *OpenGLRenderer) SetFont(font *glfont.Font) { // @todo check for monospace and return error if not?
|
||||
r.font = font
|
||||
r.verticalCellPadding = (0.25 * float32(r.fontScale))
|
||||
r.cellWidth = font.Width(1, "X")
|
||||
r.cellHeight = font.Height(1, "X") + (r.verticalCellPadding * 2) // vertical padding
|
||||
r.cellWidth, _ = font.Size("X")
|
||||
r.cellHeight = font.LineHeight() // vertical padding
|
||||
r.termCols = uint(math.Floor(float64(float32(r.areaWidth) / r.cellWidth)))
|
||||
r.termRows = uint(math.Floor(float64(float32(r.areaHeight) / r.cellHeight)))
|
||||
r.calculatePositions()
|
||||
r.rectangles = map[[2]uint]*rectangle{}
|
||||
}
|
||||
|
||||
func (r *OpenGLRenderer) calculatePositions() {
|
||||
for line := uint(0); line < r.termRows; line++ {
|
||||
for col := uint(0); col < r.termCols; col++ {
|
||||
// rounding to whole pixels makes everything nice
|
||||
x := float32(math.Round(float64((float32(col) * r.cellWidth))))
|
||||
y := float32(math.Round(float64(
|
||||
(float32(line) * r.cellHeight) + (r.cellHeight / 2) + r.verticalCellPadding,
|
||||
)))
|
||||
r.cellPositions[[2]uint{col, line}] = [2]float32{x, y}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (r *OpenGLRenderer) getRectangle(col uint, row uint) *rectangle {
|
||||
if rect, ok := r.rectangles[[2]uint{col, row}]; ok {
|
||||
return rect
|
||||
|
@ -200,7 +179,7 @@ func (r *OpenGLRenderer) generateRectangle(col uint, line uint) *rectangle {
|
|||
|
||||
// rounding to whole pixels makes everything nice
|
||||
x := float32(float32(col) * r.cellWidth)
|
||||
y := float32((float32(line) * r.cellHeight) + (r.cellHeight))
|
||||
y := float32((float32(line) * r.cellHeight)) + r.cellHeight + (r.font.LinePadding() / 2)
|
||||
r.rectangles[[2]uint{col, line}] = r.newRectangle(x, y, r.colourAttr)
|
||||
return r.rectangles[[2]uint{col, line}]
|
||||
}
|
||||
|
@ -239,20 +218,18 @@ func (r *OpenGLRenderer) DrawCellText(cell buffer.Cell, col uint, row uint) {
|
|||
fg = cell.Fg()
|
||||
}
|
||||
|
||||
pos, ok := r.cellPositions[[2]uint{col, row}]
|
||||
if !ok {
|
||||
panic(fmt.Sprintf("Missing position data for cell at %d,%d", col, row))
|
||||
}
|
||||
|
||||
var alpha float32 = 1
|
||||
if cell.Attr().Dim {
|
||||
alpha = 0.5
|
||||
}
|
||||
r.font.SetColor(fg[0], fg[1], fg[2], alpha)
|
||||
|
||||
x := float32(col) * r.cellWidth
|
||||
y := (float32(row+1) * r.cellHeight) - (r.font.LinePadding() / 2)
|
||||
|
||||
if cell.Attr().Bold { // bold means draw text again one pixel to right, so it's fatter
|
||||
r.font.Print(pos[0]+1, pos[1], 1, string(cell.Rune()))
|
||||
r.font.Print(x+1, y, string(cell.Rune()))
|
||||
}
|
||||
r.font.Print(pos[0], pos[1], 1, string(cell.Rune()))
|
||||
r.font.Print(x, y, string(cell.Rune()))
|
||||
|
||||
}
|
||||
|
|
|
@ -103,8 +103,10 @@ func csiSetMode(modeStr string, enabled bool, terminal *Terminal) error {
|
|||
terminal.ActiveBuffer().SetAutoWrap(enabled)
|
||||
case "?9":
|
||||
if enabled {
|
||||
terminal.logger.Debugf("Turning on X10 mouse mode")
|
||||
terminal.SetMouseMode(MouseModeX10)
|
||||
} else {
|
||||
terminal.logger.Debugf("Turning off X10 mouse mode")
|
||||
terminal.SetMouseMode(MouseModeNone)
|
||||
}
|
||||
case "?12", "?13":
|
||||
|
@ -119,9 +121,12 @@ func csiSetMode(modeStr string, enabled bool, terminal *Terminal) error {
|
|||
}
|
||||
case "?1000", "?1006;1000", "?10061000": // ?10061000 seen from htop
|
||||
// enable mouse tracking
|
||||
// 1000 refers to ext mode for extended mouse click area - otherwise only x <= 255-31
|
||||
if enabled {
|
||||
terminal.logger.Debugf("Turning on VT200 mouse mode")
|
||||
terminal.SetMouseMode(MouseModeVT200)
|
||||
} else {
|
||||
terminal.logger.Debugf("Turning off VT200 mouse mode")
|
||||
terminal.SetMouseMode(MouseModeNone)
|
||||
}
|
||||
case "?1048":
|
||||
|
|
|
@ -0,0 +1,61 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
)
|
||||
|
||||
type tomlConfig struct {
|
||||
Title string
|
||||
Owner ownerInfo
|
||||
DB database `toml:"database"`
|
||||
Servers map[string]server
|
||||
Clients clients
|
||||
}
|
||||
|
||||
type ownerInfo struct {
|
||||
Name string
|
||||
Org string `toml:"organization"`
|
||||
Bio string
|
||||
DOB time.Time
|
||||
}
|
||||
|
||||
type database struct {
|
||||
Server string
|
||||
Ports []int
|
||||
ConnMax int `toml:"connection_max"`
|
||||
Enabled bool
|
||||
}
|
||||
|
||||
type server struct {
|
||||
IP string
|
||||
DC string
|
||||
}
|
||||
|
||||
type clients struct {
|
||||
Data [][]interface{}
|
||||
Hosts []string
|
||||
}
|
||||
|
||||
func main() {
|
||||
var config tomlConfig
|
||||
if _, err := toml.DecodeFile("example.toml", &config); err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("Title: %s\n", config.Title)
|
||||
fmt.Printf("Owner: %s (%s, %s), Born: %s\n",
|
||||
config.Owner.Name, config.Owner.Org, config.Owner.Bio,
|
||||
config.Owner.DOB)
|
||||
fmt.Printf("Database: %s %v (Max conn. %d), Enabled? %v\n",
|
||||
config.DB.Server, config.DB.Ports, config.DB.ConnMax,
|
||||
config.DB.Enabled)
|
||||
for serverName, server := range config.Servers {
|
||||
fmt.Printf("Server: %s (%s, %s)\n", serverName, server.IP, server.DC)
|
||||
}
|
||||
fmt.Printf("Client data: %v\n", config.Clients.Data)
|
||||
fmt.Printf("Client hosts: %v\n", config.Clients.Hosts)
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
# This is a TOML document. Boom.
|
||||
|
||||
title = "TOML Example"
|
||||
|
||||
[owner]
|
||||
name = "Tom Preston-Werner"
|
||||
organization = "GitHub"
|
||||
bio = "GitHub Cofounder & CEO\nLikes tater tots and beer."
|
||||
dob = 1979-05-27T07:32:00Z # First class dates? Why not?
|
||||
|
||||
[database]
|
||||
server = "192.168.1.1"
|
||||
ports = [ 8001, 8001, 8002 ]
|
||||
connection_max = 5000
|
||||
enabled = true
|
||||
|
||||
[servers]
|
||||
|
||||
# You can indent as you please. Tabs or spaces. TOML don't care.
|
||||
[servers.alpha]
|
||||
ip = "10.0.0.1"
|
||||
dc = "eqdc10"
|
||||
|
||||
[servers.beta]
|
||||
ip = "10.0.0.2"
|
||||
dc = "eqdc10"
|
||||
|
||||
[clients]
|
||||
data = [ ["gamma", "delta"], [1, 2] ] # just an update to make sure parsers support it
|
||||
|
||||
# Line breaks are OK when inside arrays
|
||||
hosts = [
|
||||
"alpha",
|
||||
"omega"
|
||||
]
|
|
@ -0,0 +1,22 @@
|
|||
# Test file for TOML
|
||||
# Only this one tries to emulate a TOML file written by a user of the kind of parser writers probably hate
|
||||
# This part you'll really hate
|
||||
|
||||
[the]
|
||||
test_string = "You'll hate me after this - #" # " Annoying, isn't it?
|
||||
|
||||
[the.hard]
|
||||
test_array = [ "] ", " # "] # ] There you go, parse this!
|
||||
test_array2 = [ "Test #11 ]proved that", "Experiment #9 was a success" ]
|
||||
# You didn't think it'd as easy as chucking out the last #, did you?
|
||||
another_test_string = " Same thing, but with a string #"
|
||||
harder_test_string = " And when \"'s are in the string, along with # \"" # "and comments are there too"
|
||||
# Things will get harder
|
||||
|
||||
[the.hard.bit#]
|
||||
what? = "You don't think some user won't do that?"
|
||||
multi_line_array = [
|
||||
"]",
|
||||
# ] Oh yes I did
|
||||
]
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
# [x] you
|
||||
# [x.y] don't
|
||||
# [x.y.z] need these
|
||||
[x.y.z.w] # for this to work
|
|
@ -0,0 +1,6 @@
|
|||
# DO NOT WANT
|
||||
[fruit]
|
||||
type = "apple"
|
||||
|
||||
[fruit.type]
|
||||
apple = "yes"
|
|
@ -0,0 +1,35 @@
|
|||
# This is an INVALID TOML document. Boom.
|
||||
# Can you spot the error without help?
|
||||
|
||||
title = "TOML Example"
|
||||
|
||||
[owner]
|
||||
name = "Tom Preston-Werner"
|
||||
organization = "GitHub"
|
||||
bio = "GitHub Cofounder & CEO\nLikes tater tots and beer."
|
||||
dob = 1979-05-27T7:32:00Z # First class dates? Why not?
|
||||
|
||||
[database]
|
||||
server = "192.168.1.1"
|
||||
ports = [ 8001, 8001, 8002 ]
|
||||
connection_max = 5000
|
||||
enabled = true
|
||||
|
||||
[servers]
|
||||
# You can indent as you please. Tabs or spaces. TOML don't care.
|
||||
[servers.alpha]
|
||||
ip = "10.0.0.1"
|
||||
dc = "eqdc10"
|
||||
|
||||
[servers.beta]
|
||||
ip = "10.0.0.2"
|
||||
dc = "eqdc10"
|
||||
|
||||
[clients]
|
||||
data = [ ["gamma", "delta"], [1, 2] ] # just an update to make sure parsers support it
|
||||
|
||||
# Line breaks are OK when inside arrays
|
||||
hosts = [
|
||||
"alpha",
|
||||
"omega"
|
||||
]
|
|
@ -0,0 +1,5 @@
|
|||
Age = 25
|
||||
Cats = [ "Cauchy", "Plato" ]
|
||||
Pi = 3.14
|
||||
Perfection = [ 6, 28, 496, 8128 ]
|
||||
DOB = 1987-07-05T05:45:00Z
|
|
@ -0,0 +1 @@
|
|||
some_key_NAME = "wat"
|
|
@ -0,0 +1,13 @@
|
|||
# Implements the TOML test suite interface
|
||||
|
||||
This is an implementation of the interface expected by
|
||||
[toml-test](https://github.com/BurntSushi/toml-test) for my
|
||||
[toml parser written in Go](https://github.com/BurntSushi/toml).
|
||||
In particular, it maps TOML data on `stdin` to a JSON format on `stdout`.
|
||||
|
||||
|
||||
Compatible with TOML version
|
||||
[v0.4.0](https://github.com/toml-lang/toml/blob/master/versions/en/toml-v0.4.0.md)
|
||||
|
||||
Compatible with `toml-test` version
|
||||
[v0.2.0](https://github.com/BurntSushi/toml-test/tree/v0.2.0)
|
|
@ -0,0 +1,90 @@
|
|||
// Command toml-test-decoder satisfies the toml-test interface for testing
|
||||
// TOML decoders. Namely, it accepts TOML on stdin and outputs JSON on stdout.
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
"time"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
)
|
||||
|
||||
func init() {
|
||||
log.SetFlags(0)
|
||||
|
||||
flag.Usage = usage
|
||||
flag.Parse()
|
||||
}
|
||||
|
||||
func usage() {
|
||||
log.Printf("Usage: %s < toml-file\n", path.Base(os.Args[0]))
|
||||
flag.PrintDefaults()
|
||||
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
func main() {
|
||||
if flag.NArg() != 0 {
|
||||
flag.Usage()
|
||||
}
|
||||
|
||||
var tmp interface{}
|
||||
if _, err := toml.DecodeReader(os.Stdin, &tmp); err != nil {
|
||||
log.Fatalf("Error decoding TOML: %s", err)
|
||||
}
|
||||
|
||||
typedTmp := translate(tmp)
|
||||
if err := json.NewEncoder(os.Stdout).Encode(typedTmp); err != nil {
|
||||
log.Fatalf("Error encoding JSON: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func translate(tomlData interface{}) interface{} {
|
||||
switch orig := tomlData.(type) {
|
||||
case map[string]interface{}:
|
||||
typed := make(map[string]interface{}, len(orig))
|
||||
for k, v := range orig {
|
||||
typed[k] = translate(v)
|
||||
}
|
||||
return typed
|
||||
case []map[string]interface{}:
|
||||
typed := make([]map[string]interface{}, len(orig))
|
||||
for i, v := range orig {
|
||||
typed[i] = translate(v).(map[string]interface{})
|
||||
}
|
||||
return typed
|
||||
case []interface{}:
|
||||
typed := make([]interface{}, len(orig))
|
||||
for i, v := range orig {
|
||||
typed[i] = translate(v)
|
||||
}
|
||||
|
||||
// We don't really need to tag arrays, but let's be future proof.
|
||||
// (If TOML ever supports tuples, we'll need this.)
|
||||
return tag("array", typed)
|
||||
case time.Time:
|
||||
return tag("datetime", orig.Format("2006-01-02T15:04:05Z"))
|
||||
case bool:
|
||||
return tag("bool", fmt.Sprintf("%v", orig))
|
||||
case int64:
|
||||
return tag("integer", fmt.Sprintf("%d", orig))
|
||||
case float64:
|
||||
return tag("float", fmt.Sprintf("%v", orig))
|
||||
case string:
|
||||
return tag("string", orig)
|
||||
}
|
||||
|
||||
panic(fmt.Sprintf("Unknown type: %T", tomlData))
|
||||
}
|
||||
|
||||
func tag(typeName string, data interface{}) map[string]interface{} {
|
||||
return map[string]interface{}{
|
||||
"type": typeName,
|
||||
"value": data,
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
# Implements the TOML test suite interface for TOML encoders
|
||||
|
||||
This is an implementation of the interface expected by
|
||||
[toml-test](https://github.com/BurntSushi/toml-test) for the
|
||||
[TOML encoder](https://github.com/BurntSushi/toml).
|
||||
In particular, it maps JSON data on `stdin` to a TOML format on `stdout`.
|
||||
|
||||
|
||||
Compatible with TOML version
|
||||
[v0.4.0](https://github.com/toml-lang/toml/blob/master/versions/en/toml-v0.4.0.md)
|
||||
|
||||
Compatible with `toml-test` version
|
||||
[v0.2.0](https://github.com/BurntSushi/toml-test/tree/v0.2.0)
|
|
@ -0,0 +1,131 @@
|
|||
// Command toml-test-encoder satisfies the toml-test interface for testing
|
||||
// TOML encoders. Namely, it accepts JSON on stdin and outputs TOML on stdout.
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
)
|
||||
|
||||
func init() {
|
||||
log.SetFlags(0)
|
||||
|
||||
flag.Usage = usage
|
||||
flag.Parse()
|
||||
}
|
||||
|
||||
func usage() {
|
||||
log.Printf("Usage: %s < json-file\n", path.Base(os.Args[0]))
|
||||
flag.PrintDefaults()
|
||||
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
func main() {
|
||||
if flag.NArg() != 0 {
|
||||
flag.Usage()
|
||||
}
|
||||
|
||||
var tmp interface{}
|
||||
if err := json.NewDecoder(os.Stdin).Decode(&tmp); err != nil {
|
||||
log.Fatalf("Error decoding JSON: %s", err)
|
||||
}
|
||||
|
||||
tomlData := translate(tmp)
|
||||
if err := toml.NewEncoder(os.Stdout).Encode(tomlData); err != nil {
|
||||
log.Fatalf("Error encoding TOML: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func translate(typedJson interface{}) interface{} {
|
||||
switch v := typedJson.(type) {
|
||||
case map[string]interface{}:
|
||||
if len(v) == 2 && in("type", v) && in("value", v) {
|
||||
return untag(v)
|
||||
}
|
||||
m := make(map[string]interface{}, len(v))
|
||||
for k, v2 := range v {
|
||||
m[k] = translate(v2)
|
||||
}
|
||||
return m
|
||||
case []interface{}:
|
||||
tabArray := make([]map[string]interface{}, len(v))
|
||||
for i := range v {
|
||||
if m, ok := translate(v[i]).(map[string]interface{}); ok {
|
||||
tabArray[i] = m
|
||||
} else {
|
||||
log.Fatalf("JSON arrays may only contain objects. This " +
|
||||
"corresponds to only tables being allowed in " +
|
||||
"TOML table arrays.")
|
||||
}
|
||||
}
|
||||
return tabArray
|
||||
}
|
||||
log.Fatalf("Unrecognized JSON format '%T'.", typedJson)
|
||||
panic("unreachable")
|
||||
}
|
||||
|
||||
func untag(typed map[string]interface{}) interface{} {
|
||||
t := typed["type"].(string)
|
||||
v := typed["value"]
|
||||
switch t {
|
||||
case "string":
|
||||
return v.(string)
|
||||
case "integer":
|
||||
v := v.(string)
|
||||
n, err := strconv.Atoi(v)
|
||||
if err != nil {
|
||||
log.Fatalf("Could not parse '%s' as integer: %s", v, err)
|
||||
}
|
||||
return n
|
||||
case "float":
|
||||
v := v.(string)
|
||||
f, err := strconv.ParseFloat(v, 64)
|
||||
if err != nil {
|
||||
log.Fatalf("Could not parse '%s' as float64: %s", v, err)
|
||||
}
|
||||
return f
|
||||
case "datetime":
|
||||
v := v.(string)
|
||||
t, err := time.Parse("2006-01-02T15:04:05Z", v)
|
||||
if err != nil {
|
||||
log.Fatalf("Could not parse '%s' as a datetime: %s", v, err)
|
||||
}
|
||||
return t
|
||||
case "bool":
|
||||
v := v.(string)
|
||||
switch v {
|
||||
case "true":
|
||||
return true
|
||||
case "false":
|
||||
return false
|
||||
}
|
||||
log.Fatalf("Could not parse '%s' as a boolean.", v)
|
||||
case "array":
|
||||
v := v.([]interface{})
|
||||
array := make([]interface{}, len(v))
|
||||
for i := range v {
|
||||
if m, ok := v[i].(map[string]interface{}); ok {
|
||||
array[i] = untag(m)
|
||||
} else {
|
||||
log.Fatalf("Arrays may only contain other arrays or "+
|
||||
"primitive values, but found a '%T'.", m)
|
||||
}
|
||||
}
|
||||
return array
|
||||
}
|
||||
log.Fatalf("Unrecognized tag type '%s'.", t)
|
||||
panic("unreachable")
|
||||
}
|
||||
|
||||
func in(key string, m map[string]interface{}) bool {
|
||||
_, ok := m[key]
|
||||
return ok
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
# TOML Validator
|
||||
|
||||
If Go is installed, it's simple to try it out:
|
||||
|
||||
```bash
|
||||
go get github.com/BurntSushi/toml/cmd/tomlv
|
||||
tomlv some-toml-file.toml
|
||||
```
|
||||
|
||||
You can see the types of every key in a TOML file with:
|
||||
|
||||
```bash
|
||||
tomlv -types some-toml-file.toml
|
||||
```
|
||||
|
||||
At the moment, only one error message is reported at a time. Error messages
|
||||
include line numbers. No output means that the files given are valid TOML, or
|
||||
there is a bug in `tomlv`.
|
||||
|
||||
Compatible with TOML version
|
||||
[v0.4.0](https://github.com/toml-lang/toml/blob/master/versions/en/toml-v0.4.0.md)
|
|
@ -0,0 +1,61 @@
|
|||
// Command tomlv validates TOML documents and prints each key's type.
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
"text/tabwriter"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
)
|
||||
|
||||
var (
|
||||
flagTypes = false
|
||||
)
|
||||
|
||||
func init() {
|
||||
log.SetFlags(0)
|
||||
|
||||
flag.BoolVar(&flagTypes, "types", flagTypes,
|
||||
"When set, the types of every defined key will be shown.")
|
||||
|
||||
flag.Usage = usage
|
||||
flag.Parse()
|
||||
}
|
||||
|
||||
func usage() {
|
||||
log.Printf("Usage: %s toml-file [ toml-file ... ]\n",
|
||||
path.Base(os.Args[0]))
|
||||
flag.PrintDefaults()
|
||||
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
func main() {
|
||||
if flag.NArg() < 1 {
|
||||
flag.Usage()
|
||||
}
|
||||
for _, f := range flag.Args() {
|
||||
var tmp interface{}
|
||||
md, err := toml.DecodeFile(f, &tmp)
|
||||
if err != nil {
|
||||
log.Fatalf("Error in '%s': %s", f, err)
|
||||
}
|
||||
if flagTypes {
|
||||
printTypes(md)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func printTypes(md toml.MetaData) {
|
||||
tabw := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
|
||||
for _, key := range md.Keys() {
|
||||
fmt.Fprintf(tabw, "%s%s\t%s\n",
|
||||
strings.Repeat(" ", len(key)-1), key, md.Type(key...))
|
||||
}
|
||||
tabw.Flush()
|
||||
}
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,615 @@
|
|||
package toml
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestEncodeRoundTrip(t *testing.T) {
|
||||
type Config struct {
|
||||
Age int
|
||||
Cats []string
|
||||
Pi float64
|
||||
Perfection []int
|
||||
DOB time.Time
|
||||
Ipaddress net.IP
|
||||
}
|
||||
|
||||
var inputs = Config{
|
||||
13,
|
||||
[]string{"one", "two", "three"},
|
||||
3.145,
|
||||
[]int{11, 2, 3, 4},
|
||||
time.Now(),
|
||||
net.ParseIP("192.168.59.254"),
|
||||
}
|
||||
|
||||
var firstBuffer bytes.Buffer
|
||||
e := NewEncoder(&firstBuffer)
|
||||
err := e.Encode(inputs)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var outputs Config
|
||||
if _, err := Decode(firstBuffer.String(), &outputs); err != nil {
|
||||
t.Logf("Could not decode:\n-----\n%s\n-----\n",
|
||||
firstBuffer.String())
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// could test each value individually, but I'm lazy
|
||||
var secondBuffer bytes.Buffer
|
||||
e2 := NewEncoder(&secondBuffer)
|
||||
err = e2.Encode(outputs)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if firstBuffer.String() != secondBuffer.String() {
|
||||
t.Error(
|
||||
firstBuffer.String(),
|
||||
"\n\n is not identical to\n\n",
|
||||
secondBuffer.String())
|
||||
}
|
||||
}
|
||||
|
||||
// XXX(burntsushi)
|
||||
// I think these tests probably should be removed. They are good, but they
|
||||
// ought to be obsolete by toml-test.
|
||||
func TestEncode(t *testing.T) {
|
||||
type Embedded struct {
|
||||
Int int `toml:"_int"`
|
||||
}
|
||||
type NonStruct int
|
||||
|
||||
date := time.Date(2014, 5, 11, 20, 30, 40, 0, time.FixedZone("IST", 3600))
|
||||
dateStr := "2014-05-11T19:30:40Z"
|
||||
|
||||
tests := map[string]struct {
|
||||
input interface{}
|
||||
wantOutput string
|
||||
wantError error
|
||||
}{
|
||||
"bool field": {
|
||||
input: struct {
|
||||
BoolTrue bool
|
||||
BoolFalse bool
|
||||
}{true, false},
|
||||
wantOutput: "BoolTrue = true\nBoolFalse = false\n",
|
||||
},
|
||||
"int fields": {
|
||||
input: struct {
|
||||
Int int
|
||||
Int8 int8
|
||||
Int16 int16
|
||||
Int32 int32
|
||||
Int64 int64
|
||||
}{1, 2, 3, 4, 5},
|
||||
wantOutput: "Int = 1\nInt8 = 2\nInt16 = 3\nInt32 = 4\nInt64 = 5\n",
|
||||
},
|
||||
"uint fields": {
|
||||
input: struct {
|
||||
Uint uint
|
||||
Uint8 uint8
|
||||
Uint16 uint16
|
||||
Uint32 uint32
|
||||
Uint64 uint64
|
||||
}{1, 2, 3, 4, 5},
|
||||
wantOutput: "Uint = 1\nUint8 = 2\nUint16 = 3\nUint32 = 4" +
|
||||
"\nUint64 = 5\n",
|
||||
},
|
||||
"float fields": {
|
||||
input: struct {
|
||||
Float32 float32
|
||||
Float64 float64
|
||||
}{1.5, 2.5},
|
||||
wantOutput: "Float32 = 1.5\nFloat64 = 2.5\n",
|
||||
},
|
||||
"string field": {
|
||||
input: struct{ String string }{"foo"},
|
||||
wantOutput: "String = \"foo\"\n",
|
||||
},
|
||||
"string field and unexported field": {
|
||||
input: struct {
|
||||
String string
|
||||
unexported int
|
||||
}{"foo", 0},
|
||||
wantOutput: "String = \"foo\"\n",
|
||||
},
|
||||
"datetime field in UTC": {
|
||||
input: struct{ Date time.Time }{date},
|
||||
wantOutput: fmt.Sprintf("Date = %s\n", dateStr),
|
||||
},
|
||||
"datetime field as primitive": {
|
||||
// Using a map here to fail if isStructOrMap() returns true for
|
||||
// time.Time.
|
||||
input: map[string]interface{}{
|
||||
"Date": date,
|
||||
"Int": 1,
|
||||
},
|
||||
wantOutput: fmt.Sprintf("Date = %s\nInt = 1\n", dateStr),
|
||||
},
|
||||
"array fields": {
|
||||
input: struct {
|
||||
IntArray0 [0]int
|
||||
IntArray3 [3]int
|
||||
}{[0]int{}, [3]int{1, 2, 3}},
|
||||
wantOutput: "IntArray0 = []\nIntArray3 = [1, 2, 3]\n",
|
||||
},
|
||||
"slice fields": {
|
||||
input: struct{ IntSliceNil, IntSlice0, IntSlice3 []int }{
|
||||
nil, []int{}, []int{1, 2, 3},
|
||||
},
|
||||
wantOutput: "IntSlice0 = []\nIntSlice3 = [1, 2, 3]\n",
|
||||
},
|
||||
"datetime slices": {
|
||||
input: struct{ DatetimeSlice []time.Time }{
|
||||
[]time.Time{date, date},
|
||||
},
|
||||
wantOutput: fmt.Sprintf("DatetimeSlice = [%s, %s]\n",
|
||||
dateStr, dateStr),
|
||||
},
|
||||
"nested arrays and slices": {
|
||||
input: struct {
|
||||
SliceOfArrays [][2]int
|
||||
ArrayOfSlices [2][]int
|
||||
SliceOfArraysOfSlices [][2][]int
|
||||
ArrayOfSlicesOfArrays [2][][2]int
|
||||
SliceOfMixedArrays [][2]interface{}
|
||||
ArrayOfMixedSlices [2][]interface{}
|
||||
}{
|
||||
[][2]int{{1, 2}, {3, 4}},
|
||||
[2][]int{{1, 2}, {3, 4}},
|
||||
[][2][]int{
|
||||
{
|
||||
{1, 2}, {3, 4},
|
||||
},
|
||||
{
|
||||
{5, 6}, {7, 8},
|
||||
},
|
||||
},
|
||||
[2][][2]int{
|
||||
{
|
||||
{1, 2}, {3, 4},
|
||||
},
|
||||
{
|
||||
{5, 6}, {7, 8},
|
||||
},
|
||||
},
|
||||
[][2]interface{}{
|
||||
{1, 2}, {"a", "b"},
|
||||
},
|
||||
[2][]interface{}{
|
||||
{1, 2}, {"a", "b"},
|
||||
},
|
||||
},
|
||||
wantOutput: `SliceOfArrays = [[1, 2], [3, 4]]
|
||||
ArrayOfSlices = [[1, 2], [3, 4]]
|
||||
SliceOfArraysOfSlices = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
|
||||
ArrayOfSlicesOfArrays = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
|
||||
SliceOfMixedArrays = [[1, 2], ["a", "b"]]
|
||||
ArrayOfMixedSlices = [[1, 2], ["a", "b"]]
|
||||
`,
|
||||
},
|
||||
"empty slice": {
|
||||
input: struct{ Empty []interface{} }{[]interface{}{}},
|
||||
wantOutput: "Empty = []\n",
|
||||
},
|
||||
"(error) slice with element type mismatch (string and integer)": {
|
||||
input: struct{ Mixed []interface{} }{[]interface{}{1, "a"}},
|
||||
wantError: errArrayMixedElementTypes,
|
||||
},
|
||||
"(error) slice with element type mismatch (integer and float)": {
|
||||
input: struct{ Mixed []interface{} }{[]interface{}{1, 2.5}},
|
||||
wantError: errArrayMixedElementTypes,
|
||||
},
|
||||
"slice with elems of differing Go types, same TOML types": {
|
||||
input: struct {
|
||||
MixedInts []interface{}
|
||||
MixedFloats []interface{}
|
||||
}{
|
||||
[]interface{}{
|
||||
int(1), int8(2), int16(3), int32(4), int64(5),
|
||||
uint(1), uint8(2), uint16(3), uint32(4), uint64(5),
|
||||
},
|
||||
[]interface{}{float32(1.5), float64(2.5)},
|
||||
},
|
||||
wantOutput: "MixedInts = [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]\n" +
|
||||
"MixedFloats = [1.5, 2.5]\n",
|
||||
},
|
||||
"(error) slice w/ element type mismatch (one is nested array)": {
|
||||
input: struct{ Mixed []interface{} }{
|
||||
[]interface{}{1, []interface{}{2}},
|
||||
},
|
||||
wantError: errArrayMixedElementTypes,
|
||||
},
|
||||
"(error) slice with 1 nil element": {
|
||||
input: struct{ NilElement1 []interface{} }{[]interface{}{nil}},
|
||||
wantError: errArrayNilElement,
|
||||
},
|
||||
"(error) slice with 1 nil element (and other non-nil elements)": {
|
||||
input: struct{ NilElement []interface{} }{
|
||||
[]interface{}{1, nil},
|
||||
},
|
||||
wantError: errArrayNilElement,
|
||||
},
|
||||
"simple map": {
|
||||
input: map[string]int{"a": 1, "b": 2},
|
||||
wantOutput: "a = 1\nb = 2\n",
|
||||
},
|
||||
"map with interface{} value type": {
|
||||
input: map[string]interface{}{"a": 1, "b": "c"},
|
||||
wantOutput: "a = 1\nb = \"c\"\n",
|
||||
},
|
||||
"map with interface{} value type, some of which are structs": {
|
||||
input: map[string]interface{}{
|
||||
"a": struct{ Int int }{2},
|
||||
"b": 1,
|
||||
},
|
||||
wantOutput: "b = 1\n\n[a]\n Int = 2\n",
|
||||
},
|
||||
"nested map": {
|
||||
input: map[string]map[string]int{
|
||||
"a": {"b": 1},
|
||||
"c": {"d": 2},
|
||||
},
|
||||
wantOutput: "[a]\n b = 1\n\n[c]\n d = 2\n",
|
||||
},
|
||||
"nested struct": {
|
||||
input: struct{ Struct struct{ Int int } }{
|
||||
struct{ Int int }{1},
|
||||
},
|
||||
wantOutput: "[Struct]\n Int = 1\n",
|
||||
},
|
||||
"nested struct and non-struct field": {
|
||||
input: struct {
|
||||
Struct struct{ Int int }
|
||||
Bool bool
|
||||
}{struct{ Int int }{1}, true},
|
||||
wantOutput: "Bool = true\n\n[Struct]\n Int = 1\n",
|
||||
},
|
||||
"2 nested structs": {
|
||||
input: struct{ Struct1, Struct2 struct{ Int int } }{
|
||||
struct{ Int int }{1}, struct{ Int int }{2},
|
||||
},
|
||||
wantOutput: "[Struct1]\n Int = 1\n\n[Struct2]\n Int = 2\n",
|
||||
},
|
||||
"deeply nested structs": {
|
||||
input: struct {
|
||||
Struct1, Struct2 struct{ Struct3 *struct{ Int int } }
|
||||
}{
|
||||
struct{ Struct3 *struct{ Int int } }{&struct{ Int int }{1}},
|
||||
struct{ Struct3 *struct{ Int int } }{nil},
|
||||
},
|
||||
wantOutput: "[Struct1]\n [Struct1.Struct3]\n Int = 1" +
|
||||
"\n\n[Struct2]\n",
|
||||
},
|
||||
"nested struct with nil struct elem": {
|
||||
input: struct {
|
||||
Struct struct{ Inner *struct{ Int int } }
|
||||
}{
|
||||
struct{ Inner *struct{ Int int } }{nil},
|
||||
},
|
||||
wantOutput: "[Struct]\n",
|
||||
},
|
||||
"nested struct with no fields": {
|
||||
input: struct {
|
||||
Struct struct{ Inner struct{} }
|
||||
}{
|
||||
struct{ Inner struct{} }{struct{}{}},
|
||||
},
|
||||
wantOutput: "[Struct]\n [Struct.Inner]\n",
|
||||
},
|
||||
"struct with tags": {
|
||||
input: struct {
|
||||
Struct struct {
|
||||
Int int `toml:"_int"`
|
||||
} `toml:"_struct"`
|
||||
Bool bool `toml:"_bool"`
|
||||
}{
|
||||
struct {
|
||||
Int int `toml:"_int"`
|
||||
}{1}, true,
|
||||
},
|
||||
wantOutput: "_bool = true\n\n[_struct]\n _int = 1\n",
|
||||
},
|
||||
"embedded struct": {
|
||||
input: struct{ Embedded }{Embedded{1}},
|
||||
wantOutput: "_int = 1\n",
|
||||
},
|
||||
"embedded *struct": {
|
||||
input: struct{ *Embedded }{&Embedded{1}},
|
||||
wantOutput: "_int = 1\n",
|
||||
},
|
||||
"nested embedded struct": {
|
||||
input: struct {
|
||||
Struct struct{ Embedded } `toml:"_struct"`
|
||||
}{struct{ Embedded }{Embedded{1}}},
|
||||
wantOutput: "[_struct]\n _int = 1\n",
|
||||
},
|
||||
"nested embedded *struct": {
|
||||
input: struct {
|
||||
Struct struct{ *Embedded } `toml:"_struct"`
|
||||
}{struct{ *Embedded }{&Embedded{1}}},
|
||||
wantOutput: "[_struct]\n _int = 1\n",
|
||||
},
|
||||
"embedded non-struct": {
|
||||
input: struct{ NonStruct }{5},
|
||||
wantOutput: "NonStruct = 5\n",
|
||||
},
|
||||
"array of tables": {
|
||||
input: struct {
|
||||
Structs []*struct{ Int int } `toml:"struct"`
|
||||
}{
|
||||
[]*struct{ Int int }{{1}, {3}},
|
||||
},
|
||||
wantOutput: "[[struct]]\n Int = 1\n\n[[struct]]\n Int = 3\n",
|
||||
},
|
||||
"array of tables order": {
|
||||
input: map[string]interface{}{
|
||||
"map": map[string]interface{}{
|
||||
"zero": 5,
|
||||
"arr": []map[string]int{
|
||||
{
|
||||
"friend": 5,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
wantOutput: "[map]\n zero = 5\n\n [[map.arr]]\n friend = 5\n",
|
||||
},
|
||||
"(error) top-level slice": {
|
||||
input: []struct{ Int int }{{1}, {2}, {3}},
|
||||
wantError: errNoKey,
|
||||
},
|
||||
"(error) slice of slice": {
|
||||
input: struct {
|
||||
Slices [][]struct{ Int int }
|
||||
}{
|
||||
[][]struct{ Int int }{{{1}}, {{2}}, {{3}}},
|
||||
},
|
||||
wantError: errArrayNoTable,
|
||||
},
|
||||
"(error) map no string key": {
|
||||
input: map[int]string{1: ""},
|
||||
wantError: errNonString,
|
||||
},
|
||||
"(error) empty key name": {
|
||||
input: map[string]int{"": 1},
|
||||
wantError: errAnything,
|
||||
},
|
||||
"(error) empty map name": {
|
||||
input: map[string]interface{}{
|
||||
"": map[string]int{"v": 1},
|
||||
},
|
||||
wantError: errAnything,
|
||||
},
|
||||
}
|
||||
for label, test := range tests {
|
||||
encodeExpected(t, label, test.input, test.wantOutput, test.wantError)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncodeNestedTableArrays(t *testing.T) {
|
||||
type song struct {
|
||||
Name string `toml:"name"`
|
||||
}
|
||||
type album struct {
|
||||
Name string `toml:"name"`
|
||||
Songs []song `toml:"songs"`
|
||||
}
|
||||
type springsteen struct {
|
||||
Albums []album `toml:"albums"`
|
||||
}
|
||||
value := springsteen{
|
||||
[]album{
|
||||
{"Born to Run",
|
||||
[]song{{"Jungleland"}, {"Meeting Across the River"}}},
|
||||
{"Born in the USA",
|
||||
[]song{{"Glory Days"}, {"Dancing in the Dark"}}},
|
||||
},
|
||||
}
|
||||
expected := `[[albums]]
|
||||
name = "Born to Run"
|
||||
|
||||
[[albums.songs]]
|
||||
name = "Jungleland"
|
||||
|
||||
[[albums.songs]]
|
||||
name = "Meeting Across the River"
|
||||
|
||||
[[albums]]
|
||||
name = "Born in the USA"
|
||||
|
||||
[[albums.songs]]
|
||||
name = "Glory Days"
|
||||
|
||||
[[albums.songs]]
|
||||
name = "Dancing in the Dark"
|
||||
`
|
||||
encodeExpected(t, "nested table arrays", value, expected, nil)
|
||||
}
|
||||
|
||||
func TestEncodeArrayHashWithNormalHashOrder(t *testing.T) {
|
||||
type Alpha struct {
|
||||
V int
|
||||
}
|
||||
type Beta struct {
|
||||
V int
|
||||
}
|
||||
type Conf struct {
|
||||
V int
|
||||
A Alpha
|
||||
B []Beta
|
||||
}
|
||||
|
||||
val := Conf{
|
||||
V: 1,
|
||||
A: Alpha{2},
|
||||
B: []Beta{{3}},
|
||||
}
|
||||
expected := "V = 1\n\n[A]\n V = 2\n\n[[B]]\n V = 3\n"
|
||||
encodeExpected(t, "array hash with normal hash order", val, expected, nil)
|
||||
}
|
||||
|
||||
func TestEncodeWithOmitEmpty(t *testing.T) {
|
||||
type simple struct {
|
||||
Bool bool `toml:"bool,omitempty"`
|
||||
String string `toml:"string,omitempty"`
|
||||
Array [0]byte `toml:"array,omitempty"`
|
||||
Slice []int `toml:"slice,omitempty"`
|
||||
Map map[string]string `toml:"map,omitempty"`
|
||||
}
|
||||
|
||||
var v simple
|
||||
encodeExpected(t, "fields with omitempty are omitted when empty", v, "", nil)
|
||||
v = simple{
|
||||
Bool: true,
|
||||
String: " ",
|
||||
Slice: []int{2, 3, 4},
|
||||
Map: map[string]string{"foo": "bar"},
|
||||
}
|
||||
expected := `bool = true
|
||||
string = " "
|
||||
slice = [2, 3, 4]
|
||||
|
||||
[map]
|
||||
foo = "bar"
|
||||
`
|
||||
encodeExpected(t, "fields with omitempty are not omitted when non-empty",
|
||||
v, expected, nil)
|
||||
}
|
||||
|
||||
func TestEncodeWithOmitZero(t *testing.T) {
|
||||
type simple struct {
|
||||
Number int `toml:"number,omitzero"`
|
||||
Real float64 `toml:"real,omitzero"`
|
||||
Unsigned uint `toml:"unsigned,omitzero"`
|
||||
}
|
||||
|
||||
value := simple{0, 0.0, uint(0)}
|
||||
expected := ""
|
||||
|
||||
encodeExpected(t, "simple with omitzero, all zero", value, expected, nil)
|
||||
|
||||
value.Number = 10
|
||||
value.Real = 20
|
||||
value.Unsigned = 5
|
||||
expected = `number = 10
|
||||
real = 20.0
|
||||
unsigned = 5
|
||||
`
|
||||
encodeExpected(t, "simple with omitzero, non-zero", value, expected, nil)
|
||||
}
|
||||
|
||||
func TestEncodeOmitemptyWithEmptyName(t *testing.T) {
|
||||
type simple struct {
|
||||
S []int `toml:",omitempty"`
|
||||
}
|
||||
v := simple{[]int{1, 2, 3}}
|
||||
expected := "S = [1, 2, 3]\n"
|
||||
encodeExpected(t, "simple with omitempty, no name, non-empty field",
|
||||
v, expected, nil)
|
||||
}
|
||||
|
||||
func TestEncodeAnonymousStruct(t *testing.T) {
|
||||
type Inner struct{ N int }
|
||||
type Outer0 struct{ Inner }
|
||||
type Outer1 struct {
|
||||
Inner `toml:"inner"`
|
||||
}
|
||||
|
||||
v0 := Outer0{Inner{3}}
|
||||
expected := "N = 3\n"
|
||||
encodeExpected(t, "embedded anonymous untagged struct", v0, expected, nil)
|
||||
|
||||
v1 := Outer1{Inner{3}}
|
||||
expected = "[inner]\n N = 3\n"
|
||||
encodeExpected(t, "embedded anonymous tagged struct", v1, expected, nil)
|
||||
}
|
||||
|
||||
func TestEncodeAnonymousStructPointerField(t *testing.T) {
|
||||
type Inner struct{ N int }
|
||||
type Outer0 struct{ *Inner }
|
||||
type Outer1 struct {
|
||||
*Inner `toml:"inner"`
|
||||
}
|
||||
|
||||
v0 := Outer0{}
|
||||
expected := ""
|
||||
encodeExpected(t, "nil anonymous untagged struct pointer field", v0, expected, nil)
|
||||
|
||||
v0 = Outer0{&Inner{3}}
|
||||
expected = "N = 3\n"
|
||||
encodeExpected(t, "non-nil anonymous untagged struct pointer field", v0, expected, nil)
|
||||
|
||||
v1 := Outer1{}
|
||||
expected = ""
|
||||
encodeExpected(t, "nil anonymous tagged struct pointer field", v1, expected, nil)
|
||||
|
||||
v1 = Outer1{&Inner{3}}
|
||||
expected = "[inner]\n N = 3\n"
|
||||
encodeExpected(t, "non-nil anonymous tagged struct pointer field", v1, expected, nil)
|
||||
}
|
||||
|
||||
func TestEncodeIgnoredFields(t *testing.T) {
|
||||
type simple struct {
|
||||
Number int `toml:"-"`
|
||||
}
|
||||
value := simple{}
|
||||
expected := ""
|
||||
encodeExpected(t, "ignored field", value, expected, nil)
|
||||
}
|
||||
|
||||
func encodeExpected(
|
||||
t *testing.T, label string, val interface{}, wantStr string, wantErr error,
|
||||
) {
|
||||
var buf bytes.Buffer
|
||||
enc := NewEncoder(&buf)
|
||||
err := enc.Encode(val)
|
||||
if err != wantErr {
|
||||
if wantErr != nil {
|
||||
if wantErr == errAnything && err != nil {
|
||||
return
|
||||
}
|
||||
t.Errorf("%s: want Encode error %v, got %v", label, wantErr, err)
|
||||
} else {
|
||||
t.Errorf("%s: Encode failed: %s", label, err)
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if got := buf.String(); wantStr != got {
|
||||
t.Errorf("%s: want\n-----\n%q\n-----\nbut got\n-----\n%q\n-----\n",
|
||||
label, wantStr, got)
|
||||
}
|
||||
}
|
||||
|
||||
func ExampleEncoder_Encode() {
|
||||
date, _ := time.Parse(time.RFC822, "14 Mar 10 18:00 UTC")
|
||||
var config = map[string]interface{}{
|
||||
"date": date,
|
||||
"counts": []int{1, 1, 2, 3, 5, 8},
|
||||
"hash": map[string]string{
|
||||
"key1": "val1",
|
||||
"key2": "val2",
|
||||
},
|
||||
}
|
||||
buf := new(bytes.Buffer)
|
||||
if err := NewEncoder(buf).Encode(config); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Println(buf.String())
|
||||
|
||||
// Output:
|
||||
// counts = [1, 1, 2, 3, 5, 8]
|
||||
// date = 2010-03-14T18:00:00Z
|
||||
//
|
||||
// [hash]
|
||||
// key1 = "val1"
|
||||
// key2 = "val2"
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
# Compiled Object files, Static and Dynamic libs (Shared Objects)
|
||||
*.o
|
||||
*.a
|
||||
*.so
|
||||
|
||||
# Folders
|
||||
_obj
|
||||
_test
|
||||
|
||||
# Architecture specific extensions/prefixes
|
||||
*.[568vq]
|
||||
[568vq].out
|
||||
|
||||
*.cgo1.go
|
||||
*.cgo2.c
|
||||
_cgo_defun.c
|
||||
_cgo_gotypes.go
|
||||
_cgo_export.*
|
||||
|
||||
_testmain.go
|
||||
|
||||
*.exe
|
|
@ -0,0 +1,14 @@
|
|||
language: go
|
||||
go:
|
||||
- 1.5.4
|
||||
- 1.6.3
|
||||
- 1.7
|
||||
install:
|
||||
- go get -v golang.org/x/tools/cmd/cover
|
||||
script:
|
||||
- go test -v -tags=safe ./spew
|
||||
- go test -v -tags=testcgo ./spew -covermode=count -coverprofile=profile.cov
|
||||
after_success:
|
||||
- go get -v github.com/mattn/goveralls
|
||||
- export PATH=$PATH:$HOME/gopath/bin
|
||||
- goveralls -coverprofile=profile.cov -service=travis-ci
|
|
@ -0,0 +1,205 @@
|
|||
go-spew
|
||||
=======
|
||||
|
||||
[]
|
||||
(https://travis-ci.org/davecgh/go-spew) [![ISC License]
|
||||
(http://img.shields.io/badge/license-ISC-blue.svg)](http://copyfree.org) [![Coverage Status]
|
||||
(https://img.shields.io/coveralls/davecgh/go-spew.svg)]
|
||||
(https://coveralls.io/r/davecgh/go-spew?branch=master)
|
||||
|
||||
|
||||
Go-spew implements a deep pretty printer for Go data structures to aid in
|
||||
debugging. A comprehensive suite of tests with 100% test coverage is provided
|
||||
to ensure proper functionality. See `test_coverage.txt` for the gocov coverage
|
||||
report. Go-spew is licensed under the liberal ISC license, so it may be used in
|
||||
open source or commercial projects.
|
||||
|
||||
If you're interested in reading about how this package came to life and some
|
||||
of the challenges involved in providing a deep pretty printer, there is a blog
|
||||
post about it
|
||||
[here](https://web.archive.org/web/20160304013555/https://blog.cyphertite.com/go-spew-a-journey-into-dumping-go-data-structures/).
|
||||
|
||||
## Documentation
|
||||
|
||||
[]
|
||||
(http://godoc.org/github.com/davecgh/go-spew/spew)
|
||||
|
||||
Full `go doc` style documentation for the project can be viewed online without
|
||||
installing this package by using the excellent GoDoc site here:
|
||||
http://godoc.org/github.com/davecgh/go-spew/spew
|
||||
|
||||
You can also view the documentation locally once the package is installed with
|
||||
the `godoc` tool by running `godoc -http=":6060"` and pointing your browser to
|
||||
http://localhost:6060/pkg/github.com/davecgh/go-spew/spew
|
||||
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
$ go get -u github.com/davecgh/go-spew/spew
|
||||
```
|
||||
|
||||
## Quick Start
|
||||
|
||||
Add this import line to the file you're working in:
|
||||
|
||||
```Go
|
||||
import "github.com/davecgh/go-spew/spew"
|
||||
```
|
||||
|
||||
To dump a variable with full newlines, indentation, type, and pointer
|
||||
information use Dump, Fdump, or Sdump:
|
||||
|
||||
```Go
|
||||
spew.Dump(myVar1, myVar2, ...)
|
||||
spew.Fdump(someWriter, myVar1, myVar2, ...)
|
||||
str := spew.Sdump(myVar1, myVar2, ...)
|
||||
```
|
||||
|
||||
Alternatively, if you would prefer to use format strings with a compacted inline
|
||||
printing style, use the convenience wrappers Printf, Fprintf, etc with %v (most
|
||||
compact), %+v (adds pointer addresses), %#v (adds types), or %#+v (adds types
|
||||
and pointer addresses):
|
||||
|
||||
```Go
|
||||
spew.Printf("myVar1: %v -- myVar2: %+v", myVar1, myVar2)
|
||||
spew.Printf("myVar3: %#v -- myVar4: %#+v", myVar3, myVar4)
|
||||
spew.Fprintf(someWriter, "myVar1: %v -- myVar2: %+v", myVar1, myVar2)
|
||||
spew.Fprintf(someWriter, "myVar3: %#v -- myVar4: %#+v", myVar3, myVar4)
|
||||
```
|
||||
|
||||
## Debugging a Web Application Example
|
||||
|
||||
Here is an example of how you can use `spew.Sdump()` to help debug a web application. Please be sure to wrap your output using the `html.EscapeString()` function for safety reasons. You should also only use this debugging technique in a development environment, never in production.
|
||||
|
||||
```Go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html"
|
||||
"net/http"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
)
|
||||
|
||||
func handler(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/html")
|
||||
fmt.Fprintf(w, "Hi there, %s!", r.URL.Path[1:])
|
||||
fmt.Fprintf(w, "<!--\n" + html.EscapeString(spew.Sdump(w)) + "\n-->")
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/", handler)
|
||||
http.ListenAndServe(":8080", nil)
|
||||
}
|
||||
```
|
||||
|
||||
## Sample Dump Output
|
||||
|
||||
```
|
||||
(main.Foo) {
|
||||
unexportedField: (*main.Bar)(0xf84002e210)({
|
||||
flag: (main.Flag) flagTwo,
|
||||
data: (uintptr) <nil>
|
||||
}),
|
||||
ExportedField: (map[interface {}]interface {}) {
|
||||
(string) "one": (bool) true
|
||||
}
|
||||
}
|
||||
([]uint8) {
|
||||
00000000 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 |............... |
|
||||
00000010 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 |!"#$%&'()*+,-./0|
|
||||
00000020 31 32 |12|
|
||||
}
|
||||
```
|
||||
|
||||
## Sample Formatter Output
|
||||
|
||||
Double pointer to a uint8:
|
||||
```
|
||||
%v: <**>5
|
||||
%+v: <**>(0xf8400420d0->0xf8400420c8)5
|
||||
%#v: (**uint8)5
|
||||
%#+v: (**uint8)(0xf8400420d0->0xf8400420c8)5
|
||||
```
|
||||
|
||||
Pointer to circular struct with a uint8 field and a pointer to itself:
|
||||
```
|
||||
%v: <*>{1 <*><shown>}
|
||||
%+v: <*>(0xf84003e260){ui8:1 c:<*>(0xf84003e260)<shown>}
|
||||
%#v: (*main.circular){ui8:(uint8)1 c:(*main.circular)<shown>}
|
||||
%#+v: (*main.circular)(0xf84003e260){ui8:(uint8)1 c:(*main.circular)(0xf84003e260)<shown>}
|
||||
```
|
||||
|
||||
## Configuration Options
|
||||
|
||||
Configuration of spew is handled by fields in the ConfigState type. For
|
||||
convenience, all of the top-level functions use a global state available via the
|
||||
spew.Config global.
|
||||
|
||||
It is also possible to create a ConfigState instance that provides methods
|
||||
equivalent to the top-level functions. This allows concurrent configuration
|
||||
options. See the ConfigState documentation for more details.
|
||||
|
||||
```
|
||||
* Indent
|
||||
String to use for each indentation level for Dump functions.
|
||||
It is a single space by default. A popular alternative is "\t".
|
||||
|
||||
* MaxDepth
|
||||
Maximum number of levels to descend into nested data structures.
|
||||
There is no limit by default.
|
||||
|
||||
* DisableMethods
|
||||
Disables invocation of error and Stringer interface methods.
|
||||
Method invocation is enabled by default.
|
||||
|
||||
* DisablePointerMethods
|
||||
Disables invocation of error and Stringer interface methods on types
|
||||
which only accept pointer receivers from non-pointer variables. This option
|
||||
relies on access to the unsafe package, so it will not have any effect when
|
||||
running in environments without access to the unsafe package such as Google
|
||||
App Engine or with the "safe" build tag specified.
|
||||
Pointer method invocation is enabled by default.
|
||||
|
||||
* DisablePointerAddresses
|
||||
DisablePointerAddresses specifies whether to disable the printing of
|
||||
pointer addresses. This is useful when diffing data structures in tests.
|
||||
|
||||
* DisableCapacities
|
||||
DisableCapacities specifies whether to disable the printing of capacities
|
||||
for arrays, slices, maps and channels. This is useful when diffing data
|
||||
structures in tests.
|
||||
|
||||
* ContinueOnMethod
|
||||
Enables recursion into types after invoking error and Stringer interface
|
||||
methods. Recursion after method invocation is disabled by default.
|
||||
|
||||
* SortKeys
|
||||
Specifies map keys should be sorted before being printed. Use
|
||||
this to have a more deterministic, diffable output. Note that
|
||||
only native types (bool, int, uint, floats, uintptr and string)
|
||||
and types which implement error or Stringer interfaces are supported,
|
||||
with other types sorted according to the reflect.Value.String() output
|
||||
which guarantees display stability. Natural map order is used by
|
||||
default.
|
||||
|
||||
* SpewKeys
|
||||
SpewKeys specifies that, as a last resort attempt, map keys should be
|
||||
spewed to strings and sorted by those strings. This is only considered
|
||||
if SortKeys is true.
|
||||
|
||||
```
|
||||
|
||||
## Unsafe Package Dependency
|
||||
|
||||
This package relies on the unsafe package to perform some of the more advanced
|
||||
features, however it also supports a "limited" mode which allows it to work in
|
||||
environments where the unsafe package is not available. By default, it will
|
||||
operate in this mode on Google App Engine and when compiled with GopherJS. The
|
||||
"safe" build tag may also be specified to force the package to build without
|
||||
using the unsafe package.
|
||||
|
||||
## License
|
||||
|
||||
Go-spew is licensed under the [copyfree](http://copyfree.org) ISC License.
|
|
@ -0,0 +1,22 @@
|
|||
#!/bin/sh
|
||||
|
||||
# This script uses gocov to generate a test coverage report.
|
||||
# The gocov tool my be obtained with the following command:
|
||||
# go get github.com/axw/gocov/gocov
|
||||
#
|
||||
# It will be installed to $GOPATH/bin, so ensure that location is in your $PATH.
|
||||
|
||||
# Check for gocov.
|
||||
if ! type gocov >/dev/null 2>&1; then
|
||||
echo >&2 "This script requires the gocov tool."
|
||||
echo >&2 "You may obtain it with the following command:"
|
||||
echo >&2 "go get github.com/axw/gocov/gocov"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Only run the cgo tests if gcc is installed.
|
||||
if type gcc >/dev/null 2>&1; then
|
||||
(cd spew && gocov test -tags testcgo | gocov report)
|
||||
else
|
||||
(cd spew && gocov test | gocov report)
|
||||
fi
|
|
@ -0,0 +1,298 @@
|
|||
/*
|
||||
* Copyright (c) 2013-2016 Dave Collins <dave@davec.name>
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
package spew_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
)
|
||||
|
||||
// custom type to test Stinger interface on non-pointer receiver.
|
||||
type stringer string
|
||||
|
||||
// String implements the Stringer interface for testing invocation of custom
|
||||
// stringers on types with non-pointer receivers.
|
||||
func (s stringer) String() string {
|
||||
return "stringer " + string(s)
|
||||
}
|
||||
|
||||
// custom type to test Stinger interface on pointer receiver.
|
||||
type pstringer string
|
||||
|
||||
// String implements the Stringer interface for testing invocation of custom
|
||||
// stringers on types with only pointer receivers.
|
||||
func (s *pstringer) String() string {
|
||||
return "stringer " + string(*s)
|
||||
}
|
||||
|
||||
// xref1 and xref2 are cross referencing structs for testing circular reference
|
||||
// detection.
|
||||
type xref1 struct {
|
||||
ps2 *xref2
|
||||
}
|
||||
type xref2 struct {
|
||||
ps1 *xref1
|
||||
}
|
||||
|
||||
// indirCir1, indirCir2, and indirCir3 are used to generate an indirect circular
|
||||
// reference for testing detection.
|
||||
type indirCir1 struct {
|
||||
ps2 *indirCir2
|
||||
}
|
||||
type indirCir2 struct {
|
||||
ps3 *indirCir3
|
||||
}
|
||||
type indirCir3 struct {
|
||||
ps1 *indirCir1
|
||||
}
|
||||
|
||||
// embed is used to test embedded structures.
|
||||
type embed struct {
|
||||
a string
|
||||
}
|
||||
|
||||
// embedwrap is used to test embedded structures.
|
||||
type embedwrap struct {
|
||||
*embed
|
||||
e *embed
|
||||
}
|
||||
|
||||
// panicer is used to intentionally cause a panic for testing spew properly
|
||||
// handles them
|
||||
type panicer int
|
||||
|
||||
func (p panicer) String() string {
|
||||
panic("test panic")
|
||||
}
|
||||
|
||||
// customError is used to test custom error interface invocation.
|
||||
type customError int
|
||||
|
||||
func (e customError) Error() string {
|
||||
return fmt.Sprintf("error: %d", int(e))
|
||||
}
|
||||
|
||||
// stringizeWants converts a slice of wanted test output into a format suitable
|
||||
// for a test error message.
|
||||
func stringizeWants(wants []string) string {
|
||||
s := ""
|
||||
for i, want := range wants {
|
||||
if i > 0 {
|
||||
s += fmt.Sprintf("want%d: %s", i+1, want)
|
||||
} else {
|
||||
s += "want: " + want
|
||||
}
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// testFailed returns whether or not a test failed by checking if the result
|
||||
// of the test is in the slice of wanted strings.
|
||||
func testFailed(result string, wants []string) bool {
|
||||
for _, want := range wants {
|
||||
if result == want {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
type sortableStruct struct {
|
||||
x int
|
||||
}
|
||||
|
||||
func (ss sortableStruct) String() string {
|
||||
return fmt.Sprintf("ss.%d", ss.x)
|
||||
}
|
||||
|
||||
type unsortableStruct struct {
|
||||
x int
|
||||
}
|
||||
|
||||
type sortTestCase struct {
|
||||
input []reflect.Value
|
||||
expected []reflect.Value
|
||||
}
|
||||
|
||||
func helpTestSortValues(tests []sortTestCase, cs *spew.ConfigState, t *testing.T) {
|
||||
getInterfaces := func(values []reflect.Value) []interface{} {
|
||||
interfaces := []interface{}{}
|
||||
for _, v := range values {
|
||||
interfaces = append(interfaces, v.Interface())
|
||||
}
|
||||
return interfaces
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
spew.SortValues(test.input, cs)
|
||||
// reflect.DeepEqual cannot really make sense of reflect.Value,
|
||||
// probably because of all the pointer tricks. For instance,
|
||||
// v(2.0) != v(2.0) on a 32-bits system. Turn them into interface{}
|
||||
// instead.
|
||||
input := getInterfaces(test.input)
|
||||
expected := getInterfaces(test.expected)
|
||||
if !reflect.DeepEqual(input, expected) {
|
||||
t.Errorf("Sort mismatch:\n %v != %v", input, expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestSortValues ensures the sort functionality for relect.Value based sorting
|
||||
// works as intended.
|
||||
func TestSortValues(t *testing.T) {
|
||||
v := reflect.ValueOf
|
||||
|
||||
a := v("a")
|
||||
b := v("b")
|
||||
c := v("c")
|
||||
embedA := v(embed{"a"})
|
||||
embedB := v(embed{"b"})
|
||||
embedC := v(embed{"c"})
|
||||
tests := []sortTestCase{
|
||||
// No values.
|
||||
{
|
||||
[]reflect.Value{},
|
||||
[]reflect.Value{},
|
||||
},
|
||||
// Bools.
|
||||
{
|
||||
[]reflect.Value{v(false), v(true), v(false)},
|
||||
[]reflect.Value{v(false), v(false), v(true)},
|
||||
},
|
||||
// Ints.
|
||||
{
|
||||
[]reflect.Value{v(2), v(1), v(3)},
|
||||
[]reflect.Value{v(1), v(2), v(3)},
|
||||
},
|
||||
// Uints.
|
||||
{
|
||||
[]reflect.Value{v(uint8(2)), v(uint8(1)), v(uint8(3))},
|
||||
[]reflect.Value{v(uint8(1)), v(uint8(2)), v(uint8(3))},
|
||||
},
|
||||
// Floats.
|
||||
{
|
||||
[]reflect.Value{v(2.0), v(1.0), v(3.0)},
|
||||
[]reflect.Value{v(1.0), v(2.0), v(3.0)},
|
||||
},
|
||||
// Strings.
|
||||
{
|
||||
[]reflect.Value{b, a, c},
|
||||
[]reflect.Value{a, b, c},
|
||||
},
|
||||
// Array
|
||||
{
|
||||
[]reflect.Value{v([3]int{3, 2, 1}), v([3]int{1, 3, 2}), v([3]int{1, 2, 3})},
|
||||
[]reflect.Value{v([3]int{1, 2, 3}), v([3]int{1, 3, 2}), v([3]int{3, 2, 1})},
|
||||
},
|
||||
// Uintptrs.
|
||||
{
|
||||
[]reflect.Value{v(uintptr(2)), v(uintptr(1)), v(uintptr(3))},
|
||||
[]reflect.Value{v(uintptr(1)), v(uintptr(2)), v(uintptr(3))},
|
||||
},
|
||||
// SortableStructs.
|
||||
{
|
||||
// Note: not sorted - DisableMethods is set.
|
||||
[]reflect.Value{v(sortableStruct{2}), v(sortableStruct{1}), v(sortableStruct{3})},
|
||||
[]reflect.Value{v(sortableStruct{2}), v(sortableStruct{1}), v(sortableStruct{3})},
|
||||
},
|
||||
// UnsortableStructs.
|
||||
{
|
||||
// Note: not sorted - SpewKeys is false.
|
||||
[]reflect.Value{v(unsortableStruct{2}), v(unsortableStruct{1}), v(unsortableStruct{3})},
|
||||
[]reflect.Value{v(unsortableStruct{2}), v(unsortableStruct{1}), v(unsortableStruct{3})},
|
||||
},
|
||||
// Invalid.
|
||||
{
|
||||
[]reflect.Value{embedB, embedA, embedC},
|
||||
[]reflect.Value{embedB, embedA, embedC},
|
||||
},
|
||||
}
|
||||
cs := spew.ConfigState{DisableMethods: true, SpewKeys: false}
|
||||
helpTestSortValues(tests, &cs, t)
|
||||
}
|
||||
|
||||
// TestSortValuesWithMethods ensures the sort functionality for relect.Value
|
||||
// based sorting works as intended when using string methods.
|
||||
func TestSortValuesWithMethods(t *testing.T) {
|
||||
v := reflect.ValueOf
|
||||
|
||||
a := v("a")
|
||||
b := v("b")
|
||||
c := v("c")
|
||||
tests := []sortTestCase{
|
||||
// Ints.
|
||||
{
|
||||
[]reflect.Value{v(2), v(1), v(3)},
|
||||
[]reflect.Value{v(1), v(2), v(3)},
|
||||
},
|
||||
// Strings.
|
||||
{
|
||||
[]reflect.Value{b, a, c},
|
||||
[]reflect.Value{a, b, c},
|
||||
},
|
||||
// SortableStructs.
|
||||
{
|
||||
[]reflect.Value{v(sortableStruct{2}), v(sortableStruct{1}), v(sortableStruct{3})},
|
||||
[]reflect.Value{v(sortableStruct{1}), v(sortableStruct{2}), v(sortableStruct{3})},
|
||||
},
|
||||
// UnsortableStructs.
|
||||
{
|
||||
// Note: not sorted - SpewKeys is false.
|
||||
[]reflect.Value{v(unsortableStruct{2}), v(unsortableStruct{1}), v(unsortableStruct{3})},
|
||||
[]reflect.Value{v(unsortableStruct{2}), v(unsortableStruct{1}), v(unsortableStruct{3})},
|
||||
},
|
||||
}
|
||||
cs := spew.ConfigState{DisableMethods: false, SpewKeys: false}
|
||||
helpTestSortValues(tests, &cs, t)
|
||||
}
|
||||
|
||||
// TestSortValuesWithSpew ensures the sort functionality for relect.Value
|
||||
// based sorting works as intended when using spew to stringify keys.
|
||||
func TestSortValuesWithSpew(t *testing.T) {
|
||||
v := reflect.ValueOf
|
||||
|
||||
a := v("a")
|
||||
b := v("b")
|
||||
c := v("c")
|
||||
tests := []sortTestCase{
|
||||
// Ints.
|
||||
{
|
||||
[]reflect.Value{v(2), v(1), v(3)},
|
||||
[]reflect.Value{v(1), v(2), v(3)},
|
||||
},
|
||||
// Strings.
|
||||
{
|
||||
[]reflect.Value{b, a, c},
|
||||
[]reflect.Value{a, b, c},
|
||||
},
|
||||
// SortableStructs.
|
||||
{
|
||||
[]reflect.Value{v(sortableStruct{2}), v(sortableStruct{1}), v(sortableStruct{3})},
|
||||
[]reflect.Value{v(sortableStruct{1}), v(sortableStruct{2}), v(sortableStruct{3})},
|
||||
},
|
||||
// UnsortableStructs.
|
||||
{
|
||||
[]reflect.Value{v(unsortableStruct{2}), v(unsortableStruct{1}), v(unsortableStruct{3})},
|
||||
[]reflect.Value{v(unsortableStruct{1}), v(unsortableStruct{2}), v(unsortableStruct{3})},
|
||||
},
|
||||
}
|
||||
cs := spew.ConfigState{DisableMethods: true, SpewKeys: true}
|
||||
helpTestSortValues(tests, &cs, t)
|
||||
}
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,99 @@
|
|||
// Copyright (c) 2013-2016 Dave Collins <dave@davec.name>
|
||||
//
|
||||
// Permission to use, copy, modify, and distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
// NOTE: Due to the following build constraints, this file will only be compiled
|
||||
// when both cgo is supported and "-tags testcgo" is added to the go test
|
||||
// command line. This means the cgo tests are only added (and hence run) when
|
||||
// specifially requested. This configuration is used because spew itself
|
||||
// does not require cgo to run even though it does handle certain cgo types
|
||||
// specially. Rather than forcing all clients to require cgo and an external
|
||||
// C compiler just to run the tests, this scheme makes them optional.
|
||||
// +build cgo,testcgo
|
||||
|
||||
package spew_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/davecgh/go-spew/spew/testdata"
|
||||
)
|
||||
|
||||
func addCgoDumpTests() {
|
||||
// C char pointer.
|
||||
v := testdata.GetCgoCharPointer()
|
||||
nv := testdata.GetCgoNullCharPointer()
|
||||
pv := &v
|
||||
vcAddr := fmt.Sprintf("%p", v)
|
||||
vAddr := fmt.Sprintf("%p", pv)
|
||||
pvAddr := fmt.Sprintf("%p", &pv)
|
||||
vt := "*testdata._Ctype_char"
|
||||
vs := "116"
|
||||
addDumpTest(v, "("+vt+")("+vcAddr+")("+vs+")\n")
|
||||
addDumpTest(pv, "(*"+vt+")("+vAddr+"->"+vcAddr+")("+vs+")\n")
|
||||
addDumpTest(&pv, "(**"+vt+")("+pvAddr+"->"+vAddr+"->"+vcAddr+")("+vs+")\n")
|
||||
addDumpTest(nv, "("+vt+")(<nil>)\n")
|
||||
|
||||
// C char array.
|
||||
v2, v2l, v2c := testdata.GetCgoCharArray()
|
||||
v2Len := fmt.Sprintf("%d", v2l)
|
||||
v2Cap := fmt.Sprintf("%d", v2c)
|
||||
v2t := "[6]testdata._Ctype_char"
|
||||
v2s := "(len=" + v2Len + " cap=" + v2Cap + ") " +
|
||||
"{\n 00000000 74 65 73 74 32 00 " +
|
||||
" |test2.|\n}"
|
||||
addDumpTest(v2, "("+v2t+") "+v2s+"\n")
|
||||
|
||||
// C unsigned char array.
|
||||
v3, v3l, v3c := testdata.GetCgoUnsignedCharArray()
|
||||
v3Len := fmt.Sprintf("%d", v3l)
|
||||
v3Cap := fmt.Sprintf("%d", v3c)
|
||||
v3t := "[6]testdata._Ctype_unsignedchar"
|
||||
v3t2 := "[6]testdata._Ctype_uchar"
|
||||
v3s := "(len=" + v3Len + " cap=" + v3Cap + ") " +
|
||||
"{\n 00000000 74 65 73 74 33 00 " +
|
||||
" |test3.|\n}"
|
||||
addDumpTest(v3, "("+v3t+") "+v3s+"\n", "("+v3t2+") "+v3s+"\n")
|
||||
|
||||
// C signed char array.
|
||||
v4, v4l, v4c := testdata.GetCgoSignedCharArray()
|
||||
v4Len := fmt.Sprintf("%d", v4l)
|
||||
v4Cap := fmt.Sprintf("%d", v4c)
|
||||
v4t := "[6]testdata._Ctype_schar"
|
||||
v4t2 := "testdata._Ctype_schar"
|
||||
v4s := "(len=" + v4Len + " cap=" + v4Cap + ") " +
|
||||
"{\n (" + v4t2 + ") 116,\n (" + v4t2 + ") 101,\n (" + v4t2 +
|
||||
") 115,\n (" + v4t2 + ") 116,\n (" + v4t2 + ") 52,\n (" + v4t2 +
|
||||
") 0\n}"
|
||||
addDumpTest(v4, "("+v4t+") "+v4s+"\n")
|
||||
|
||||
// C uint8_t array.
|
||||
v5, v5l, v5c := testdata.GetCgoUint8tArray()
|
||||
v5Len := fmt.Sprintf("%d", v5l)
|
||||
v5Cap := fmt.Sprintf("%d", v5c)
|
||||
v5t := "[6]testdata._Ctype_uint8_t"
|
||||
v5s := "(len=" + v5Len + " cap=" + v5Cap + ") " +
|
||||
"{\n 00000000 74 65 73 74 35 00 " +
|
||||
" |test5.|\n}"
|
||||
addDumpTest(v5, "("+v5t+") "+v5s+"\n")
|
||||
|
||||
// C typedefed unsigned char array.
|
||||
v6, v6l, v6c := testdata.GetCgoTypdefedUnsignedCharArray()
|
||||
v6Len := fmt.Sprintf("%d", v6l)
|
||||
v6Cap := fmt.Sprintf("%d", v6c)
|
||||
v6t := "[6]testdata._Ctype_custom_uchar_t"
|
||||
v6s := "(len=" + v6Len + " cap=" + v6Cap + ") " +
|
||||
"{\n 00000000 74 65 73 74 36 00 " +
|
||||
" |test6.|\n}"
|
||||
addDumpTest(v6, "("+v6t+") "+v6s+"\n")
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
// Copyright (c) 2013 Dave Collins <dave@davec.name>
|
||||
//
|
||||
// Permission to use, copy, modify, and distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
// NOTE: Due to the following build constraints, this file will only be compiled
|
||||
// when either cgo is not supported or "-tags testcgo" is not added to the go
|
||||
// test command line. This file intentionally does not setup any cgo tests in
|
||||
// this scenario.
|
||||
// +build !cgo !testcgo
|
||||
|
||||
package spew_test
|
||||
|
||||
func addCgoDumpTests() {
|
||||
// Don't add any tests for cgo since this file is only compiled when
|
||||
// there should not be any cgo tests.
|
||||
}
|
|
@ -0,0 +1,226 @@
|
|||
/*
|
||||
* Copyright (c) 2013-2016 Dave Collins <dave@davec.name>
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
package spew_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
)
|
||||
|
||||
type Flag int
|
||||
|
||||
const (
|
||||
flagOne Flag = iota
|
||||
flagTwo
|
||||
)
|
||||
|
||||
var flagStrings = map[Flag]string{
|
||||
flagOne: "flagOne",
|
||||
flagTwo: "flagTwo",
|
||||
}
|
||||
|
||||
func (f Flag) String() string {
|
||||
if s, ok := flagStrings[f]; ok {
|
||||
return s
|
||||
}
|
||||
return fmt.Sprintf("Unknown flag (%d)", int(f))
|
||||
}
|
||||
|
||||
type Bar struct {
|
||||
data uintptr
|
||||
}
|
||||
|
||||
type Foo struct {
|
||||
unexportedField Bar
|
||||
ExportedField map[interface{}]interface{}
|
||||
}
|
||||
|
||||
// This example demonstrates how to use Dump to dump variables to stdout.
|
||||
func ExampleDump() {
|
||||
// The following package level declarations are assumed for this example:
|
||||
/*
|
||||
type Flag int
|
||||
|
||||
const (
|
||||
flagOne Flag = iota
|
||||
flagTwo
|
||||
)
|
||||
|
||||
var flagStrings = map[Flag]string{
|
||||
flagOne: "flagOne",
|
||||
flagTwo: "flagTwo",
|
||||
}
|
||||
|
||||
func (f Flag) String() string {
|
||||
if s, ok := flagStrings[f]; ok {
|
||||
return s
|
||||
}
|
||||
return fmt.Sprintf("Unknown flag (%d)", int(f))
|
||||
}
|
||||
|
||||
type Bar struct {
|
||||
data uintptr
|
||||
}
|
||||
|
||||
type Foo struct {
|
||||
unexportedField Bar
|
||||
ExportedField map[interface{}]interface{}
|
||||
}
|
||||
*/
|
||||
|
||||
// Setup some sample data structures for the example.
|
||||
bar := Bar{uintptr(0)}
|
||||
s1 := Foo{bar, map[interface{}]interface{}{"one": true}}
|
||||
f := Flag(5)
|
||||
b := []byte{
|
||||
0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
|
||||
0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
|
||||
0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
|
||||
0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
|
||||
0x31, 0x32,
|
||||
}
|
||||
|
||||
// Dump!
|
||||
spew.Dump(s1, f, b)
|
||||
|
||||
// Output:
|
||||
// (spew_test.Foo) {
|
||||
// unexportedField: (spew_test.Bar) {
|
||||
// data: (uintptr) <nil>
|
||||
// },
|
||||
// ExportedField: (map[interface {}]interface {}) (len=1) {
|
||||
// (string) (len=3) "one": (bool) true
|
||||
// }
|
||||
// }
|
||||
// (spew_test.Flag) Unknown flag (5)
|
||||
// ([]uint8) (len=34 cap=34) {
|
||||
// 00000000 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 |............... |
|
||||
// 00000010 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 |!"#$%&'()*+,-./0|
|
||||
// 00000020 31 32 |12|
|
||||
// }
|
||||
//
|
||||
}
|
||||
|
||||
// This example demonstrates how to use Printf to display a variable with a
|
||||
// format string and inline formatting.
|
||||
func ExamplePrintf() {
|
||||
// Create a double pointer to a uint 8.
|
||||
ui8 := uint8(5)
|
||||
pui8 := &ui8
|
||||
ppui8 := &pui8
|
||||
|
||||
// Create a circular data type.
|
||||
type circular struct {
|
||||
ui8 uint8
|
||||
c *circular
|
||||
}
|
||||
c := circular{ui8: 1}
|
||||
c.c = &c
|
||||
|
||||
// Print!
|
||||
spew.Printf("ppui8: %v\n", ppui8)
|
||||
spew.Printf("circular: %v\n", c)
|
||||
|
||||
// Output:
|
||||
// ppui8: <**>5
|
||||
// circular: {1 <*>{1 <*><shown>}}
|
||||
}
|
||||
|
||||
// This example demonstrates how to use a ConfigState.
|
||||
func ExampleConfigState() {
|
||||
// Modify the indent level of the ConfigState only. The global
|
||||
// configuration is not modified.
|
||||
scs := spew.ConfigState{Indent: "\t"}
|
||||
|
||||
// Output using the ConfigState instance.
|
||||
v := map[string]int{"one": 1}
|
||||
scs.Printf("v: %v\n", v)
|
||||
scs.Dump(v)
|
||||
|
||||
// Output:
|
||||
// v: map[one:1]
|
||||
// (map[string]int) (len=1) {
|
||||
// (string) (len=3) "one": (int) 1
|
||||
// }
|
||||
}
|
||||
|
||||
// This example demonstrates how to use ConfigState.Dump to dump variables to
|
||||
// stdout
|
||||
func ExampleConfigState_Dump() {
|
||||
// See the top-level Dump example for details on the types used in this
|
||||
// example.
|
||||
|
||||
// Create two ConfigState instances with different indentation.
|
||||
scs := spew.ConfigState{Indent: "\t"}
|
||||
scs2 := spew.ConfigState{Indent: " "}
|
||||
|
||||
// Setup some sample data structures for the example.
|
||||
bar := Bar{uintptr(0)}
|
||||
s1 := Foo{bar, map[interface{}]interface{}{"one": true}}
|
||||
|
||||
// Dump using the ConfigState instances.
|
||||
scs.Dump(s1)
|
||||
scs2.Dump(s1)
|
||||
|
||||
// Output:
|
||||
// (spew_test.Foo) {
|
||||
// unexportedField: (spew_test.Bar) {
|
||||
// data: (uintptr) <nil>
|
||||
// },
|
||||
// ExportedField: (map[interface {}]interface {}) (len=1) {
|
||||
// (string) (len=3) "one": (bool) true
|
||||
// }
|
||||
// }
|
||||
// (spew_test.Foo) {
|
||||
// unexportedField: (spew_test.Bar) {
|
||||
// data: (uintptr) <nil>
|
||||
// },
|
||||
// ExportedField: (map[interface {}]interface {}) (len=1) {
|
||||
// (string) (len=3) "one": (bool) true
|
||||
// }
|
||||
// }
|
||||
//
|
||||
}
|
||||
|
||||
// This example demonstrates how to use ConfigState.Printf to display a variable
|
||||
// with a format string and inline formatting.
|
||||
func ExampleConfigState_Printf() {
|
||||
// See the top-level Dump example for details on the types used in this
|
||||
// example.
|
||||
|
||||
// Create two ConfigState instances and modify the method handling of the
|
||||
// first ConfigState only.
|
||||
scs := spew.NewDefaultConfig()
|
||||
scs2 := spew.NewDefaultConfig()
|
||||
scs.DisableMethods = true
|
||||
|
||||
// Alternatively
|
||||
// scs := spew.ConfigState{Indent: " ", DisableMethods: true}
|
||||
// scs2 := spew.ConfigState{Indent: " "}
|
||||
|
||||
// This is of type Flag which implements a Stringer and has raw value 1.
|
||||
f := flagTwo
|
||||
|
||||
// Dump using the ConfigState instances.
|
||||
scs.Printf("f: %v\n", f)
|
||||
scs2.Printf("f: %v\n", f)
|
||||
|
||||
// Output:
|
||||
// f: 1
|
||||
// f: flagTwo
|
||||
}
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,87 @@
|
|||
/*
|
||||
* Copyright (c) 2013-2016 Dave Collins <dave@davec.name>
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
/*
|
||||
This test file is part of the spew package rather than than the spew_test
|
||||
package because it needs access to internals to properly test certain cases
|
||||
which are not possible via the public interface since they should never happen.
|
||||
*/
|
||||
|
||||
package spew
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// dummyFmtState implements a fake fmt.State to use for testing invalid
|
||||
// reflect.Value handling. This is necessary because the fmt package catches
|
||||
// invalid values before invoking the formatter on them.
|
||||
type dummyFmtState struct {
|
||||
bytes.Buffer
|
||||
}
|
||||
|
||||
func (dfs *dummyFmtState) Flag(f int) bool {
|
||||
if f == int('+') {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (dfs *dummyFmtState) Precision() (int, bool) {
|
||||
return 0, false
|
||||
}
|
||||
|
||||
func (dfs *dummyFmtState) Width() (int, bool) {
|
||||
return 0, false
|
||||
}
|
||||
|
||||
// TestInvalidReflectValue ensures the dump and formatter code handles an
|
||||
// invalid reflect value properly. This needs access to internal state since it
|
||||
// should never happen in real code and therefore can't be tested via the public
|
||||
// API.
|
||||
func TestInvalidReflectValue(t *testing.T) {
|
||||
i := 1
|
||||
|
||||
// Dump invalid reflect value.
|
||||
v := new(reflect.Value)
|
||||
buf := new(bytes.Buffer)
|
||||
d := dumpState{w: buf, cs: &Config}
|
||||
d.dump(*v)
|
||||
s := buf.String()
|
||||
want := "<invalid>"
|
||||
if s != want {
|
||||
t.Errorf("InvalidReflectValue #%d\n got: %s want: %s", i, s, want)
|
||||
}
|
||||
i++
|
||||
|
||||
// Formatter invalid reflect value.
|
||||
buf2 := new(dummyFmtState)
|
||||
f := formatState{value: *v, cs: &Config, fs: buf2}
|
||||
f.format(*v)
|
||||
s = buf2.String()
|
||||
want = "<invalid>"
|
||||
if s != want {
|
||||
t.Errorf("InvalidReflectValue #%d got: %s want: %s", i, s, want)
|
||||
}
|
||||
}
|
||||
|
||||
// SortValues makes the internal sortValues function available to the test
|
||||
// package.
|
||||
func SortValues(values []reflect.Value, cs *ConfigState) {
|
||||
sortValues(values, cs)
|
||||
}
|
|
@ -0,0 +1,102 @@
|
|||
// Copyright (c) 2013-2016 Dave Collins <dave@davec.name>
|
||||
|
||||
// Permission to use, copy, modify, and distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
// NOTE: Due to the following build constraints, this file will only be compiled
|
||||
// when the code is not running on Google App Engine, compiled by GopherJS, and
|
||||
// "-tags safe" is not added to the go build command line. The "disableunsafe"
|
||||
// tag is deprecated and thus should not be used.
|
||||
// +build !js,!appengine,!safe,!disableunsafe
|
||||
|
||||
/*
|
||||
This test file is part of the spew package rather than than the spew_test
|
||||
package because it needs access to internals to properly test certain cases
|
||||
which are not possible via the public interface since they should never happen.
|
||||
*/
|
||||
|
||||
package spew
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"reflect"
|
||||
"testing"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// changeKind uses unsafe to intentionally change the kind of a reflect.Value to
|
||||
// the maximum kind value which does not exist. This is needed to test the
|
||||
// fallback code which punts to the standard fmt library for new types that
|
||||
// might get added to the language.
|
||||
func changeKind(v *reflect.Value, readOnly bool) {
|
||||
rvf := (*uintptr)(unsafe.Pointer(uintptr(unsafe.Pointer(v)) + offsetFlag))
|
||||
*rvf = *rvf | ((1<<flagKindWidth - 1) << flagKindShift)
|
||||
if readOnly {
|
||||
*rvf |= flagRO
|
||||
} else {
|
||||
*rvf &= ^uintptr(flagRO)
|
||||
}
|
||||
}
|
||||
|
||||
// TestAddedReflectValue tests functionaly of the dump and formatter code which
|
||||
// falls back to the standard fmt library for new types that might get added to
|
||||
// the language.
|
||||
func TestAddedReflectValue(t *testing.T) {
|
||||
i := 1
|
||||
|
||||
// Dump using a reflect.Value that is exported.
|
||||
v := reflect.ValueOf(int8(5))
|
||||
changeKind(&v, false)
|
||||
buf := new(bytes.Buffer)
|
||||
d := dumpState{w: buf, cs: &Config}
|
||||
d.dump(v)
|
||||
s := buf.String()
|
||||
want := "(int8) 5"
|
||||
if s != want {
|
||||
t.Errorf("TestAddedReflectValue #%d\n got: %s want: %s", i, s, want)
|
||||
}
|
||||
i++
|
||||
|
||||
// Dump using a reflect.Value that is not exported.
|
||||
changeKind(&v, true)
|
||||
buf.Reset()
|
||||
d.dump(v)
|
||||
s = buf.String()
|
||||
want = "(int8) <int8 Value>"
|
||||
if s != want {
|
||||
t.Errorf("TestAddedReflectValue #%d\n got: %s want: %s", i, s, want)
|
||||
}
|
||||
i++
|
||||
|
||||
// Formatter using a reflect.Value that is exported.
|
||||
changeKind(&v, false)
|
||||
buf2 := new(dummyFmtState)
|
||||
f := formatState{value: v, cs: &Config, fs: buf2}
|
||||
f.format(v)
|
||||
s = buf2.String()
|
||||
want = "5"
|
||||
if s != want {
|
||||
t.Errorf("TestAddedReflectValue #%d got: %s want: %s", i, s, want)
|
||||
}
|
||||
i++
|
||||
|
||||
// Formatter using a reflect.Value that is not exported.
|
||||
changeKind(&v, true)
|
||||
buf2.Reset()
|
||||
f = formatState{value: v, cs: &Config, fs: buf2}
|
||||
f.format(v)
|
||||
s = buf2.String()
|
||||
want = "<int8 Value>"
|
||||
if s != want {
|
||||
t.Errorf("TestAddedReflectValue #%d got: %s want: %s", i, s, want)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,320 @@
|
|||
/*
|
||||
* Copyright (c) 2013-2016 Dave Collins <dave@davec.name>
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
package spew_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
)
|
||||
|
||||
// spewFunc is used to identify which public function of the spew package or
|
||||
// ConfigState a test applies to.
|
||||
type spewFunc int
|
||||
|
||||
const (
|
||||
fCSFdump spewFunc = iota
|
||||
fCSFprint
|
||||
fCSFprintf
|
||||
fCSFprintln
|
||||
fCSPrint
|
||||
fCSPrintln
|
||||
fCSSdump
|
||||
fCSSprint
|
||||
fCSSprintf
|
||||
fCSSprintln
|
||||
fCSErrorf
|
||||
fCSNewFormatter
|
||||
fErrorf
|
||||
fFprint
|
||||
fFprintln
|
||||
fPrint
|
||||
fPrintln
|
||||
fSdump
|
||||
fSprint
|
||||
fSprintf
|
||||
fSprintln
|
||||
)
|
||||
|
||||
// Map of spewFunc values to names for pretty printing.
|
||||
var spewFuncStrings = map[spewFunc]string{
|
||||
fCSFdump: "ConfigState.Fdump",
|
||||
fCSFprint: "ConfigState.Fprint",
|
||||
fCSFprintf: "ConfigState.Fprintf",
|
||||
fCSFprintln: "ConfigState.Fprintln",
|
||||
fCSSdump: "ConfigState.Sdump",
|
||||
fCSPrint: "ConfigState.Print",
|
||||
fCSPrintln: "ConfigState.Println",
|
||||
fCSSprint: "ConfigState.Sprint",
|
||||
fCSSprintf: "ConfigState.Sprintf",
|
||||
fCSSprintln: "ConfigState.Sprintln",
|
||||
fCSErrorf: "ConfigState.Errorf",
|
||||
fCSNewFormatter: "ConfigState.NewFormatter",
|
||||
fErrorf: "spew.Errorf",
|
||||
fFprint: "spew.Fprint",
|
||||
fFprintln: "spew.Fprintln",
|
||||
fPrint: "spew.Print",
|
||||
fPrintln: "spew.Println",
|
||||
fSdump: "spew.Sdump",
|
||||
fSprint: "spew.Sprint",
|
||||
fSprintf: "spew.Sprintf",
|
||||
fSprintln: "spew.Sprintln",
|
||||
}
|
||||
|
||||
func (f spewFunc) String() string {
|
||||
if s, ok := spewFuncStrings[f]; ok {
|
||||
return s
|
||||
}
|
||||
return fmt.Sprintf("Unknown spewFunc (%d)", int(f))
|
||||
}
|
||||
|
||||
// spewTest is used to describe a test to be performed against the public
|
||||
// functions of the spew package or ConfigState.
|
||||
type spewTest struct {
|
||||
cs *spew.ConfigState
|
||||
f spewFunc
|
||||
format string
|
||||
in interface{}
|
||||
want string
|
||||
}
|
||||
|
||||
// spewTests houses the tests to be performed against the public functions of
|
||||
// the spew package and ConfigState.
|
||||
//
|
||||
// These tests are only intended to ensure the public functions are exercised
|
||||
// and are intentionally not exhaustive of types. The exhaustive type
|
||||
// tests are handled in the dump and format tests.
|
||||
var spewTests []spewTest
|
||||
|
||||
// redirStdout is a helper function to return the standard output from f as a
|
||||
// byte slice.
|
||||
func redirStdout(f func()) ([]byte, error) {
|
||||
tempFile, err := ioutil.TempFile("", "ss-test")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
fileName := tempFile.Name()
|
||||
defer os.Remove(fileName) // Ignore error
|
||||
|
||||
origStdout := os.Stdout
|
||||
os.Stdout = tempFile
|
||||
f()
|
||||
os.Stdout = origStdout
|
||||
tempFile.Close()
|
||||
|
||||
return ioutil.ReadFile(fileName)
|
||||
}
|
||||
|
||||
func initSpewTests() {
|
||||
// Config states with various settings.
|
||||
scsDefault := spew.NewDefaultConfig()
|
||||
scsNoMethods := &spew.ConfigState{Indent: " ", DisableMethods: true}
|
||||
scsNoPmethods := &spew.ConfigState{Indent: " ", DisablePointerMethods: true}
|
||||
scsMaxDepth := &spew.ConfigState{Indent: " ", MaxDepth: 1}
|
||||
scsContinue := &spew.ConfigState{Indent: " ", ContinueOnMethod: true}
|
||||
scsNoPtrAddr := &spew.ConfigState{DisablePointerAddresses: true}
|
||||
scsNoCap := &spew.ConfigState{DisableCapacities: true}
|
||||
|
||||
// Variables for tests on types which implement Stringer interface with and
|
||||
// without a pointer receiver.
|
||||
ts := stringer("test")
|
||||
tps := pstringer("test")
|
||||
|
||||
type ptrTester struct {
|
||||
s *struct{}
|
||||
}
|
||||
tptr := &ptrTester{s: &struct{}{}}
|
||||
|
||||
// depthTester is used to test max depth handling for structs, array, slices
|
||||
// and maps.
|
||||
type depthTester struct {
|
||||
ic indirCir1
|
||||
arr [1]string
|
||||
slice []string
|
||||
m map[string]int
|
||||
}
|
||||
dt := depthTester{indirCir1{nil}, [1]string{"arr"}, []string{"slice"},
|
||||
map[string]int{"one": 1}}
|
||||
|
||||
// Variable for tests on types which implement error interface.
|
||||
te := customError(10)
|
||||
|
||||
spewTests = []spewTest{
|
||||
{scsDefault, fCSFdump, "", int8(127), "(int8) 127\n"},
|
||||
{scsDefault, fCSFprint, "", int16(32767), "32767"},
|
||||
{scsDefault, fCSFprintf, "%v", int32(2147483647), "2147483647"},
|
||||
{scsDefault, fCSFprintln, "", int(2147483647), "2147483647\n"},
|
||||
{scsDefault, fCSPrint, "", int64(9223372036854775807), "9223372036854775807"},
|
||||
{scsDefault, fCSPrintln, "", uint8(255), "255\n"},
|
||||
{scsDefault, fCSSdump, "", uint8(64), "(uint8) 64\n"},
|
||||
{scsDefault, fCSSprint, "", complex(1, 2), "(1+2i)"},
|
||||
{scsDefault, fCSSprintf, "%v", complex(float32(3), 4), "(3+4i)"},
|
||||
{scsDefault, fCSSprintln, "", complex(float64(5), 6), "(5+6i)\n"},
|
||||
{scsDefault, fCSErrorf, "%#v", uint16(65535), "(uint16)65535"},
|
||||
{scsDefault, fCSNewFormatter, "%v", uint32(4294967295), "4294967295"},
|
||||
{scsDefault, fErrorf, "%v", uint64(18446744073709551615), "18446744073709551615"},
|
||||
{scsDefault, fFprint, "", float32(3.14), "3.14"},
|
||||
{scsDefault, fFprintln, "", float64(6.28), "6.28\n"},
|
||||
{scsDefault, fPrint, "", true, "true"},
|
||||
{scsDefault, fPrintln, "", false, "false\n"},
|
||||
{scsDefault, fSdump, "", complex(-10, -20), "(complex128) (-10-20i)\n"},
|
||||
{scsDefault, fSprint, "", complex(-1, -2), "(-1-2i)"},
|
||||
{scsDefault, fSprintf, "%v", complex(float32(-3), -4), "(-3-4i)"},
|
||||
{scsDefault, fSprintln, "", complex(float64(-5), -6), "(-5-6i)\n"},
|
||||
{scsNoMethods, fCSFprint, "", ts, "test"},
|
||||
{scsNoMethods, fCSFprint, "", &ts, "<*>test"},
|
||||
{scsNoMethods, fCSFprint, "", tps, "test"},
|
||||
{scsNoMethods, fCSFprint, "", &tps, "<*>test"},
|
||||
{scsNoPmethods, fCSFprint, "", ts, "stringer test"},
|
||||
{scsNoPmethods, fCSFprint, "", &ts, "<*>stringer test"},
|
||||
{scsNoPmethods, fCSFprint, "", tps, "test"},
|
||||
{scsNoPmethods, fCSFprint, "", &tps, "<*>stringer test"},
|
||||
{scsMaxDepth, fCSFprint, "", dt, "{{<max>} [<max>] [<max>] map[<max>]}"},
|
||||
{scsMaxDepth, fCSFdump, "", dt, "(spew_test.depthTester) {\n" +
|
||||
" ic: (spew_test.indirCir1) {\n <max depth reached>\n },\n" +
|
||||
" arr: ([1]string) (len=1 cap=1) {\n <max depth reached>\n },\n" +
|
||||
" slice: ([]string) (len=1 cap=1) {\n <max depth reached>\n },\n" +
|
||||
" m: (map[string]int) (len=1) {\n <max depth reached>\n }\n}\n"},
|
||||
{scsContinue, fCSFprint, "", ts, "(stringer test) test"},
|
||||
{scsContinue, fCSFdump, "", ts, "(spew_test.stringer) " +
|
||||
"(len=4) (stringer test) \"test\"\n"},
|
||||
{scsContinue, fCSFprint, "", te, "(error: 10) 10"},
|
||||
{scsContinue, fCSFdump, "", te, "(spew_test.customError) " +
|
||||
"(error: 10) 10\n"},
|
||||
{scsNoPtrAddr, fCSFprint, "", tptr, "<*>{<*>{}}"},
|
||||
{scsNoPtrAddr, fCSSdump, "", tptr, "(*spew_test.ptrTester)({\ns: (*struct {})({\n})\n})\n"},
|
||||
{scsNoCap, fCSSdump, "", make([]string, 0, 10), "([]string) {\n}\n"},
|
||||
{scsNoCap, fCSSdump, "", make([]string, 1, 10), "([]string) (len=1) {\n(string) \"\"\n}\n"},
|
||||
}
|
||||
}
|
||||
|
||||
// TestSpew executes all of the tests described by spewTests.
|
||||
func TestSpew(t *testing.T) {
|
||||
initSpewTests()
|
||||
|
||||
t.Logf("Running %d tests", len(spewTests))
|
||||
for i, test := range spewTests {
|
||||
buf := new(bytes.Buffer)
|
||||
switch test.f {
|
||||
case fCSFdump:
|
||||
test.cs.Fdump(buf, test.in)
|
||||
|
||||
case fCSFprint:
|
||||
test.cs.Fprint(buf, test.in)
|
||||
|
||||
case fCSFprintf:
|
||||
test.cs.Fprintf(buf, test.format, test.in)
|
||||
|
||||
case fCSFprintln:
|
||||
test.cs.Fprintln(buf, test.in)
|
||||
|
||||
case fCSPrint:
|
||||
b, err := redirStdout(func() { test.cs.Print(test.in) })
|
||||
if err != nil {
|
||||
t.Errorf("%v #%d %v", test.f, i, err)
|
||||
continue
|
||||
}
|
||||
buf.Write(b)
|
||||
|
||||
case fCSPrintln:
|
||||
b, err := redirStdout(func() { test.cs.Println(test.in) })
|
||||
if err != nil {
|
||||
t.Errorf("%v #%d %v", test.f, i, err)
|
||||
continue
|
||||
}
|
||||
buf.Write(b)
|
||||
|
||||
case fCSSdump:
|
||||
str := test.cs.Sdump(test.in)
|
||||
buf.WriteString(str)
|
||||
|
||||
case fCSSprint:
|
||||
str := test.cs.Sprint(test.in)
|
||||
buf.WriteString(str)
|
||||
|
||||
case fCSSprintf:
|
||||
str := test.cs.Sprintf(test.format, test.in)
|
||||
buf.WriteString(str)
|
||||
|
||||
case fCSSprintln:
|
||||
str := test.cs.Sprintln(test.in)
|
||||
buf.WriteString(str)
|
||||
|
||||
case fCSErrorf:
|
||||
err := test.cs.Errorf(test.format, test.in)
|
||||
buf.WriteString(err.Error())
|
||||
|
||||
case fCSNewFormatter:
|
||||
fmt.Fprintf(buf, test.format, test.cs.NewFormatter(test.in))
|
||||
|
||||
case fErrorf:
|
||||
err := spew.Errorf(test.format, test.in)
|
||||
buf.WriteString(err.Error())
|
||||
|
||||
case fFprint:
|
||||
spew.Fprint(buf, test.in)
|
||||
|
||||
case fFprintln:
|
||||
spew.Fprintln(buf, test.in)
|
||||
|
||||
case fPrint:
|
||||
b, err := redirStdout(func() { spew.Print(test.in) })
|
||||
if err != nil {
|
||||
t.Errorf("%v #%d %v", test.f, i, err)
|
||||
continue
|
||||
}
|
||||
buf.Write(b)
|
||||
|
||||
case fPrintln:
|
||||
b, err := redirStdout(func() { spew.Println(test.in) })
|
||||
if err != nil {
|
||||
t.Errorf("%v #%d %v", test.f, i, err)
|
||||
continue
|
||||
}
|
||||
buf.Write(b)
|
||||
|
||||
case fSdump:
|
||||
str := spew.Sdump(test.in)
|
||||
buf.WriteString(str)
|
||||
|
||||
case fSprint:
|
||||
str := spew.Sprint(test.in)
|
||||
buf.WriteString(str)
|
||||
|
||||
case fSprintf:
|
||||
str := spew.Sprintf(test.format, test.in)
|
||||
buf.WriteString(str)
|
||||
|
||||
case fSprintln:
|
||||
str := spew.Sprintln(test.in)
|
||||
buf.WriteString(str)
|
||||
|
||||
default:
|
||||
t.Errorf("%v #%d unrecognized function", test.f, i)
|
||||
continue
|
||||
}
|
||||
s := buf.String()
|
||||
if test.want != s {
|
||||
t.Errorf("ConfigState #%d\n got: %s want: %s", i, s, test.want)
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,82 @@
|
|||
// Copyright (c) 2013 Dave Collins <dave@davec.name>
|
||||
//
|
||||
// Permission to use, copy, modify, and distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
// NOTE: Due to the following build constraints, this file will only be compiled
|
||||
// when both cgo is supported and "-tags testcgo" is added to the go test
|
||||
// command line. This code should really only be in the dumpcgo_test.go file,
|
||||
// but unfortunately Go will not allow cgo in test files, so this is a
|
||||
// workaround to allow cgo types to be tested. This configuration is used
|
||||
// because spew itself does not require cgo to run even though it does handle
|
||||
// certain cgo types specially. Rather than forcing all clients to require cgo
|
||||
// and an external C compiler just to run the tests, this scheme makes them
|
||||
// optional.
|
||||
// +build cgo,testcgo
|
||||
|
||||
package testdata
|
||||
|
||||
/*
|
||||
#include <stdint.h>
|
||||
typedef unsigned char custom_uchar_t;
|
||||
|
||||
char *ncp = 0;
|
||||
char *cp = "test";
|
||||
char ca[6] = {'t', 'e', 's', 't', '2', '\0'};
|
||||
unsigned char uca[6] = {'t', 'e', 's', 't', '3', '\0'};
|
||||
signed char sca[6] = {'t', 'e', 's', 't', '4', '\0'};
|
||||
uint8_t ui8ta[6] = {'t', 'e', 's', 't', '5', '\0'};
|
||||
custom_uchar_t tuca[6] = {'t', 'e', 's', 't', '6', '\0'};
|
||||
*/
|
||||
import "C"
|
||||
|
||||
// GetCgoNullCharPointer returns a null char pointer via cgo. This is only
|
||||
// used for tests.
|
||||
func GetCgoNullCharPointer() interface{} {
|
||||
return C.ncp
|
||||
}
|
||||
|
||||
// GetCgoCharPointer returns a char pointer via cgo. This is only used for
|
||||
// tests.
|
||||
func GetCgoCharPointer() interface{} {
|
||||
return C.cp
|
||||
}
|
||||
|
||||
// GetCgoCharArray returns a char array via cgo and the array's len and cap.
|
||||
// This is only used for tests.
|
||||
func GetCgoCharArray() (interface{}, int, int) {
|
||||
return C.ca, len(C.ca), cap(C.ca)
|
||||
}
|
||||
|
||||
// GetCgoUnsignedCharArray returns an unsigned char array via cgo and the
|
||||
// array's len and cap. This is only used for tests.
|
||||
func GetCgoUnsignedCharArray() (interface{}, int, int) {
|
||||
return C.uca, len(C.uca), cap(C.uca)
|
||||
}
|
||||
|
||||
// GetCgoSignedCharArray returns a signed char array via cgo and the array's len
|
||||
// and cap. This is only used for tests.
|
||||
func GetCgoSignedCharArray() (interface{}, int, int) {
|
||||
return C.sca, len(C.sca), cap(C.sca)
|
||||
}
|
||||
|
||||
// GetCgoUint8tArray returns a uint8_t array via cgo and the array's len and
|
||||
// cap. This is only used for tests.
|
||||
func GetCgoUint8tArray() (interface{}, int, int) {
|
||||
return C.ui8ta, len(C.ui8ta), cap(C.ui8ta)
|
||||
}
|
||||
|
||||
// GetCgoTypdefedUnsignedCharArray returns a typedefed unsigned char array via
|
||||
// cgo and the array's len and cap. This is only used for tests.
|
||||
func GetCgoTypdefedUnsignedCharArray() (interface{}, int, int) {
|
||||
return C.tuca, len(C.tuca), cap(C.tuca)
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
|
||||
github.com/davecgh/go-spew/spew/dump.go dumpState.dump 100.00% (88/88)
|
||||
github.com/davecgh/go-spew/spew/format.go formatState.format 100.00% (82/82)
|
||||
github.com/davecgh/go-spew/spew/format.go formatState.formatPtr 100.00% (52/52)
|
||||
github.com/davecgh/go-spew/spew/dump.go dumpState.dumpPtr 100.00% (44/44)
|
||||
github.com/davecgh/go-spew/spew/dump.go dumpState.dumpSlice 100.00% (39/39)
|
||||
github.com/davecgh/go-spew/spew/common.go handleMethods 100.00% (30/30)
|
||||
github.com/davecgh/go-spew/spew/common.go printHexPtr 100.00% (18/18)
|
||||
github.com/davecgh/go-spew/spew/common.go unsafeReflectValue 100.00% (13/13)
|
||||
github.com/davecgh/go-spew/spew/format.go formatState.constructOrigFormat 100.00% (12/12)
|
||||
github.com/davecgh/go-spew/spew/dump.go fdump 100.00% (11/11)
|
||||
github.com/davecgh/go-spew/spew/format.go formatState.Format 100.00% (11/11)
|
||||
github.com/davecgh/go-spew/spew/common.go init 100.00% (10/10)
|
||||
github.com/davecgh/go-spew/spew/common.go printComplex 100.00% (9/9)
|
||||
github.com/davecgh/go-spew/spew/common.go valuesSorter.Less 100.00% (8/8)
|
||||
github.com/davecgh/go-spew/spew/format.go formatState.buildDefaultFormat 100.00% (7/7)
|
||||
github.com/davecgh/go-spew/spew/format.go formatState.unpackValue 100.00% (5/5)
|
||||
github.com/davecgh/go-spew/spew/dump.go dumpState.indent 100.00% (4/4)
|
||||
github.com/davecgh/go-spew/spew/common.go catchPanic 100.00% (4/4)
|
||||
github.com/davecgh/go-spew/spew/config.go ConfigState.convertArgs 100.00% (4/4)
|
||||
github.com/davecgh/go-spew/spew/spew.go convertArgs 100.00% (4/4)
|
||||
github.com/davecgh/go-spew/spew/format.go newFormatter 100.00% (3/3)
|
||||
github.com/davecgh/go-spew/spew/dump.go Sdump 100.00% (3/3)
|
||||
github.com/davecgh/go-spew/spew/common.go printBool 100.00% (3/3)
|
||||
github.com/davecgh/go-spew/spew/common.go sortValues 100.00% (3/3)
|
||||
github.com/davecgh/go-spew/spew/config.go ConfigState.Sdump 100.00% (3/3)
|
||||
github.com/davecgh/go-spew/spew/dump.go dumpState.unpackValue 100.00% (3/3)
|
||||
github.com/davecgh/go-spew/spew/spew.go Printf 100.00% (1/1)
|
||||
github.com/davecgh/go-spew/spew/spew.go Println 100.00% (1/1)
|
||||
github.com/davecgh/go-spew/spew/spew.go Sprint 100.00% (1/1)
|
||||
github.com/davecgh/go-spew/spew/spew.go Sprintf 100.00% (1/1)
|
||||
github.com/davecgh/go-spew/spew/spew.go Sprintln 100.00% (1/1)
|
||||
github.com/davecgh/go-spew/spew/common.go printFloat 100.00% (1/1)
|
||||
github.com/davecgh/go-spew/spew/config.go NewDefaultConfig 100.00% (1/1)
|
||||
github.com/davecgh/go-spew/spew/common.go printInt 100.00% (1/1)
|
||||
github.com/davecgh/go-spew/spew/common.go printUint 100.00% (1/1)
|
||||
github.com/davecgh/go-spew/spew/common.go valuesSorter.Len 100.00% (1/1)
|
||||
github.com/davecgh/go-spew/spew/common.go valuesSorter.Swap 100.00% (1/1)
|
||||
github.com/davecgh/go-spew/spew/config.go ConfigState.Errorf 100.00% (1/1)
|
||||
github.com/davecgh/go-spew/spew/config.go ConfigState.Fprint 100.00% (1/1)
|
||||
github.com/davecgh/go-spew/spew/config.go ConfigState.Fprintf 100.00% (1/1)
|
||||
github.com/davecgh/go-spew/spew/config.go ConfigState.Fprintln 100.00% (1/1)
|
||||
github.com/davecgh/go-spew/spew/config.go ConfigState.Print 100.00% (1/1)
|
||||
github.com/davecgh/go-spew/spew/config.go ConfigState.Printf 100.00% (1/1)
|
||||
github.com/davecgh/go-spew/spew/config.go ConfigState.Println 100.00% (1/1)
|
||||
github.com/davecgh/go-spew/spew/config.go ConfigState.Sprint 100.00% (1/1)
|
||||
github.com/davecgh/go-spew/spew/config.go ConfigState.Sprintf 100.00% (1/1)
|
||||
github.com/davecgh/go-spew/spew/config.go ConfigState.Sprintln 100.00% (1/1)
|
||||
github.com/davecgh/go-spew/spew/config.go ConfigState.NewFormatter 100.00% (1/1)
|
||||
github.com/davecgh/go-spew/spew/config.go ConfigState.Fdump 100.00% (1/1)
|
||||
github.com/davecgh/go-spew/spew/config.go ConfigState.Dump 100.00% (1/1)
|
||||
github.com/davecgh/go-spew/spew/dump.go Fdump 100.00% (1/1)
|
||||
github.com/davecgh/go-spew/spew/dump.go Dump 100.00% (1/1)
|
||||
github.com/davecgh/go-spew/spew/spew.go Fprintln 100.00% (1/1)
|
||||
github.com/davecgh/go-spew/spew/format.go NewFormatter 100.00% (1/1)
|
||||
github.com/davecgh/go-spew/spew/spew.go Errorf 100.00% (1/1)
|
||||
github.com/davecgh/go-spew/spew/spew.go Fprint 100.00% (1/1)
|
||||
github.com/davecgh/go-spew/spew/spew.go Fprintf 100.00% (1/1)
|
||||
github.com/davecgh/go-spew/spew/spew.go Print 100.00% (1/1)
|
||||
github.com/davecgh/go-spew/spew ------------------------------- 100.00% (505/505)
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
sudo: false
|
||||
addons:
|
||||
apt_packages:
|
||||
- libgles2-mesa-dev
|
||||
language: go
|
||||
go:
|
||||
- 1.8.x
|
||||
- 1.4.x
|
||||
- master
|
||||
matrix:
|
||||
allow_failures:
|
||||
- go: master
|
||||
fast_finish: true
|
||||
install:
|
||||
- # Do nothing. This is needed to prevent default install action "go get -t -v ./..." from happening here (we want it to happen inside script step).
|
||||
script:
|
||||
- go get -t -v ./...
|
||||
- diff -u <(echo -n) <(gofmt -d -s .)
|
||||
- # vet is reporting "possible misuse of unsafe.Pointer", need to fix that.
|
||||
- # go tool vet .
|
||||
- go test -v -race ./...
|
|
@ -0,0 +1,65 @@
|
|||
# gl [](https://travis-ci.org/go-gl/gl) [](https://godoc.org/github.com/go-gl/gl)
|
||||
|
||||
This repository holds Go bindings to various OpenGL versions. They are auto-generated using [Glow](https://github.com/go-gl/glow).
|
||||
|
||||
Features:
|
||||
- Go functions that mirror the C specification using Go types.
|
||||
- Support for multiple OpenGL APIs (GL/GLES/EGL/WGL/GLX/EGL), versions, and profiles.
|
||||
- Support for extensions (including debug callbacks).
|
||||
|
||||
Requirements:
|
||||
- A cgo compiler (typically gcc).
|
||||
- On Ubuntu/Debian-based systems, the `libgl1-mesa-dev` package.
|
||||
|
||||
Usage
|
||||
-----
|
||||
|
||||
Use `go get -u` to download and install the prebuilt packages. The prebuilt packages support OpenGL versions 2.1, 3.1, 3.2, 3.3, 4.1, 4.2, 4.3, 4.4, 4.5, 4.6 across both the core and compatibility profiles and include all extensions. Pick whichever one(s) you need:
|
||||
|
||||
go get -u github.com/go-gl/gl/v{3.2,3.3,4.1,4.2,4.3,4.4,4.5,4.6}-{core,compatibility}/gl
|
||||
go get -u github.com/go-gl/gl/v3.1/gles2
|
||||
go get -u github.com/go-gl/gl/v2.1/gl
|
||||
|
||||
Once the bindings are installed you can use them with the appropriate import statements.
|
||||
|
||||
```Go
|
||||
import "github.com/go-gl/gl/v3.3-core/gl"
|
||||
|
||||
func main() {
|
||||
window := ... // Open a window.
|
||||
window.MakeContextCurrent()
|
||||
|
||||
// Important! Call gl.Init only under the presence of an active OpenGL context,
|
||||
// i.e., after MakeContextCurrent.
|
||||
if err := gl.Init(); err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
The `gl` package contains the OpenGL functions and enumeration values for the imported version. It also contains helper functions for working with the API. Of note is `gl.Ptr` which takes a Go array or slice or pointer and returns a corresponding `uintptr` to use with functions expecting data pointers. Also of note is `gl.Str` which takes a null-terminated Go string and returns a corresponding `*int8` to use with functions expecting character pointers.
|
||||
|
||||
A note about threading and goroutines. The bindings do not expose a mechanism to make an OpenGL context current on a different thread so you must restrict your usage to the thread on which you called `gl.Init()`. To do so you should use [LockOSThread](https://code.google.com/p/go-wiki/wiki/LockOSThread).
|
||||
|
||||
Examples
|
||||
--------
|
||||
|
||||
Examples illustrating how to use the bindings are available in the [example](https://github.com/go-gl/example) repo. There are examples for [OpenGL 4.1 core](https://github.com/go-gl/example/tree/master/gl41core-cube) and [OpenGL 2.1](https://github.com/go-gl/example/tree/master/gl21-cube).
|
||||
|
||||
Function Loading
|
||||
----------------
|
||||
|
||||
The `procaddr` package contains platform-specific functions for [loading OpenGL functions](https://www.opengl.org/wiki/Load_OpenGL_Functions). Calling `gl.Init()` uses the `auto` subpackage to automatically select an appropriate implementation based on the build environment. If you want to select a specific implementation you can use the `noauto` build tag and the `gl.InitWithProcAddrFunc` initialization function.
|
||||
|
||||
Generating
|
||||
----------
|
||||
|
||||
These gl bindings are generated using the [Glow](https://github.com/go-gl/glow) generator. Only developers of this repository need to do this step.
|
||||
|
||||
It is required to have `glow` source in the same Go workspace (since relative paths are used) and the `glow` binary should be in your `$PATH`. Doable with `go get -u github.com/go-gl/glow` if your `$GOPATH/bin` is in your `$PATH`.
|
||||
|
||||
```bash
|
||||
go generate -tags=gen github.com/go-gl/gl
|
||||
```
|
||||
|
||||
More information about these bindings can be found in the [Glow repository](https://github.com/go-gl/glow).
|
|
@ -0,0 +1,25 @@
|
|||
// +build gen
|
||||
|
||||
//go:generate glow generate -out=./v2.1/gl/ -api=gl -version=2.1 -xml=../glow/xml/
|
||||
//go:generate glow generate -out=./all-core/gl/ -api=gl -version=all -profile=core -lenientInit -xml=../glow/xml/
|
||||
//go:generate glow generate -out=./v3.2-core/gl/ -api=gl -version=3.2 -profile=core -xml=../glow/xml/
|
||||
//go:generate glow generate -out=./v3.3-core/gl/ -api=gl -version=3.3 -profile=core -xml=../glow/xml/
|
||||
//go:generate glow generate -out=./v4.1-core/gl/ -api=gl -version=4.1 -profile=core -xml=../glow/xml/
|
||||
//go:generate glow generate -out=./v4.2-core/gl/ -api=gl -version=4.2 -profile=core -xml=../glow/xml/
|
||||
//go:generate glow generate -out=./v4.3-core/gl/ -api=gl -version=4.3 -profile=core -xml=../glow/xml/
|
||||
//go:generate glow generate -out=./v4.4-core/gl/ -api=gl -version=4.4 -profile=core -xml=../glow/xml/
|
||||
//go:generate glow generate -out=./v4.5-core/gl/ -api=gl -version=4.5 -profile=core -xml=../glow/xml/
|
||||
//go:generate glow generate -out=./v4.6-core/gl/ -api=gl -version=4.6 -profile=core -xml=../glow/xml/
|
||||
//go:generate glow generate -out=./v3.2-compatibility/gl/ -api=gl -version=3.2 -profile=compatibility -xml=../glow/xml/
|
||||
//go:generate glow generate -out=./v3.3-compatibility/gl/ -api=gl -version=3.3 -profile=compatibility -xml=../glow/xml/
|
||||
//go:generate glow generate -out=./v4.1-compatibility/gl/ -api=gl -version=4.1 -profile=compatibility -xml=../glow/xml/
|
||||
//go:generate glow generate -out=./v4.2-compatibility/gl/ -api=gl -version=4.2 -profile=compatibility -xml=../glow/xml/
|
||||
//go:generate glow generate -out=./v4.3-compatibility/gl/ -api=gl -version=4.3 -profile=compatibility -xml=../glow/xml/
|
||||
//go:generate glow generate -out=./v4.4-compatibility/gl/ -api=gl -version=4.4 -profile=compatibility -xml=../glow/xml/
|
||||
//go:generate glow generate -out=./v4.5-compatibility/gl/ -api=gl -version=4.5 -profile=compatibility -xml=../glow/xml/
|
||||
//go:generate glow generate -out=./v4.6-compatibility/gl/ -api=gl -version=4.6 -profile=compatibility -xml=../glow/xml/
|
||||
//go:generate glow generate -out=./v3.1/gles2/ -api=gles2 -version=3.1 -xml=../glow/xml/
|
||||
|
||||
// This is an empty pseudo-package with the sole purpose of containing go generate directives
|
||||
// that generate all gl binding packages inside this repository.
|
||||
package gl
|
|
@ -0,0 +1,109 @@
|
|||
// Glow automatically generated OpenGL binding: http://github.com/go-gl/glow
|
||||
|
||||
package gl
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// #include <stdlib.h>
|
||||
import "C"
|
||||
|
||||
// Ptr takes a slice or pointer (to a singular scalar value or the first
|
||||
// element of an array or slice) and returns its GL-compatible address.
|
||||
//
|
||||
// For example:
|
||||
//
|
||||
// var data []uint8
|
||||
// ...
|
||||
// gl.TexImage2D(gl.TEXTURE_2D, ..., gl.UNSIGNED_BYTE, gl.Ptr(&data[0]))
|
||||
func Ptr(data interface{}) unsafe.Pointer {
|
||||
if data == nil {
|
||||
return unsafe.Pointer(nil)
|
||||
}
|
||||
var addr unsafe.Pointer
|
||||
v := reflect.ValueOf(data)
|
||||
switch v.Type().Kind() {
|
||||
case reflect.Ptr:
|
||||
e := v.Elem()
|
||||
switch e.Kind() {
|
||||
case
|
||||
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
|
||||
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
|
||||
reflect.Float32, reflect.Float64:
|
||||
addr = unsafe.Pointer(e.UnsafeAddr())
|
||||
default:
|
||||
panic(fmt.Errorf("unsupported pointer to type %s; must be a slice or pointer to a singular scalar value or the first element of an array or slice", e.Kind()))
|
||||
}
|
||||
case reflect.Uintptr:
|
||||
addr = unsafe.Pointer(v.Pointer())
|
||||
case reflect.Slice:
|
||||
addr = unsafe.Pointer(v.Index(0).UnsafeAddr())
|
||||
default:
|
||||
panic(fmt.Errorf("unsupported type %s; must be a slice or pointer to a singular scalar value or the first element of an array or slice", v.Type()))
|
||||
}
|
||||
return addr
|
||||
}
|
||||
|
||||
// PtrOffset takes a pointer offset and returns a GL-compatible pointer.
|
||||
// Useful for functions such as glVertexAttribPointer that take pointer
|
||||
// parameters indicating an offset rather than an absolute memory address.
|
||||
func PtrOffset(offset int) unsafe.Pointer {
|
||||
return unsafe.Pointer(uintptr(offset))
|
||||
}
|
||||
|
||||
// Str takes a null-terminated Go string and returns its GL-compatible address.
|
||||
// This function reaches into Go string storage in an unsafe way so the caller
|
||||
// must ensure the string is not garbage collected.
|
||||
func Str(str string) *uint8 {
|
||||
if !strings.HasSuffix(str, "\x00") {
|
||||
panic("str argument missing null terminator: " + str)
|
||||
}
|
||||
header := (*reflect.StringHeader)(unsafe.Pointer(&str))
|
||||
return (*uint8)(unsafe.Pointer(header.Data))
|
||||
}
|
||||
|
||||
// GoStr takes a null-terminated string returned by OpenGL and constructs a
|
||||
// corresponding Go string.
|
||||
func GoStr(cstr *uint8) string {
|
||||
return C.GoString((*C.char)(unsafe.Pointer(cstr)))
|
||||
}
|
||||
|
||||
// Strs takes a list of Go strings (with or without null-termination) and
|
||||
// returns their C counterpart.
|
||||
//
|
||||
// The returned free function must be called once you are done using the strings
|
||||
// in order to free the memory.
|
||||
//
|
||||
// If no strings are provided as a parameter this function will panic.
|
||||
func Strs(strs ...string) (cstrs **uint8, free func()) {
|
||||
if len(strs) == 0 {
|
||||
panic("Strs: expected at least 1 string")
|
||||
}
|
||||
|
||||
// Allocate a contiguous array large enough to hold all the strings' contents.
|
||||
n := 0
|
||||
for i := range strs {
|
||||
n += len(strs[i])
|
||||
}
|
||||
data := C.malloc(C.size_t(n))
|
||||
|
||||
// Copy all the strings into data.
|
||||
dataSlice := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
|
||||
Data: uintptr(data),
|
||||
Len: n,
|
||||
Cap: n,
|
||||
}))
|
||||
css := make([]*uint8, len(strs)) // Populated with pointers to each string.
|
||||
offset := 0
|
||||
for i := range strs {
|
||||
copy(dataSlice[offset:offset+len(strs[i])], strs[i][:]) // Copy strs[i] into proper data location.
|
||||
css[i] = (*uint8)(unsafe.Pointer(&dataSlice[offset])) // Set a pointer to it.
|
||||
offset += len(strs[i])
|
||||
}
|
||||
|
||||
return (**uint8)(&css[0]), func() { C.free(data) }
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
// Glow automatically generated OpenGL binding: http://github.com/go-gl/glow
|
||||
|
||||
package gl
|
||||
|
||||
import "C"
|
||||
import "unsafe"
|
||||
|
||||
type DebugProc func(
|
||||
source uint32,
|
||||
gltype uint32,
|
||||
id uint32,
|
||||
severity uint32,
|
||||
length int32,
|
||||
message string,
|
||||
userParam unsafe.Pointer)
|
||||
|
||||
var userDebugCallback DebugProc
|
||||
|
||||
//export glowDebugCallback_gl21
|
||||
func glowDebugCallback_gl21(
|
||||
source uint32,
|
||||
gltype uint32,
|
||||
id uint32,
|
||||
severity uint32,
|
||||
length int32,
|
||||
message *uint8,
|
||||
userParam unsafe.Pointer) {
|
||||
if userDebugCallback != nil {
|
||||
userDebugCallback(source, gltype, id, severity, length, GoStr(message), userParam)
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,65 @@
|
|||
// This file implements GlowGetProcAddress for every supported platform. The
|
||||
// correct version is chosen automatically based on build tags:
|
||||
// windows: WGL
|
||||
// darwin: CGL
|
||||
// linux freebsd: GLX
|
||||
// Use of EGL instead of the platform's default (listed above) is made possible
|
||||
// via the "egl" build tag.
|
||||
// It is also possible to install your own function outside this package for
|
||||
// retrieving OpenGL function pointers, to do this see InitWithProcAddrFunc.
|
||||
package gl
|
||||
|
||||
/*
|
||||
#cgo windows CFLAGS: -DTAG_WINDOWS
|
||||
#cgo windows LDFLAGS: -lopengl32
|
||||
#cgo darwin CFLAGS: -DTAG_DARWIN
|
||||
#cgo darwin LDFLAGS: -framework OpenGL
|
||||
#cgo linux freebsd CFLAGS: -DTAG_POSIX
|
||||
#cgo linux freebsd LDFLAGS: -lGL
|
||||
#cgo egl CFLAGS: -DTAG_EGL
|
||||
#cgo egl LDFLAGS: -lEGL
|
||||
// Check the EGL tag first as it takes priority over the platform's default
|
||||
// configuration of WGL/GLX/CGL.
|
||||
#if defined(TAG_EGL)
|
||||
#include <stdlib.h>
|
||||
#include <EGL/egl.h>
|
||||
void* GlowGetProcAddress_gl21(const char* name) {
|
||||
return eglGetProcAddress(name);
|
||||
}
|
||||
#elif defined(TAG_WINDOWS)
|
||||
#define WIN32_LEAN_AND_MEAN 1
|
||||
#include <windows.h>
|
||||
#include <stdlib.h>
|
||||
static HMODULE ogl32dll = NULL;
|
||||
void* GlowGetProcAddress_gl21(const char* name) {
|
||||
void* pf = wglGetProcAddress((LPCSTR) name);
|
||||
if (pf) {
|
||||
return pf;
|
||||
}
|
||||
if (ogl32dll == NULL) {
|
||||
ogl32dll = LoadLibraryA("opengl32.dll");
|
||||
}
|
||||
return GetProcAddress(ogl32dll, (LPCSTR) name);
|
||||
}
|
||||
#elif defined(TAG_DARWIN)
|
||||
#include <stdlib.h>
|
||||
#include <dlfcn.h>
|
||||
void* GlowGetProcAddress_gl21(const char* name) {
|
||||
return dlsym(RTLD_DEFAULT, name);
|
||||
}
|
||||
#elif defined(TAG_POSIX)
|
||||
#include <stdlib.h>
|
||||
#include <GL/glx.h>
|
||||
void* GlowGetProcAddress_gl21(const char* name) {
|
||||
return glXGetProcAddress((const GLubyte *) name);
|
||||
}
|
||||
#endif
|
||||
*/
|
||||
import "C"
|
||||
import "unsafe"
|
||||
|
||||
func getProcAddress(namea string) unsafe.Pointer {
|
||||
cname := C.CString(namea)
|
||||
defer C.free(unsafe.Pointer(cname))
|
||||
return C.GlowGetProcAddress_gl21(cname)
|
||||
}
|
|
@ -0,0 +1,109 @@
|
|||
// Glow automatically generated OpenGL binding: http://github.com/go-gl/glow
|
||||
|
||||
package gles2
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// #include <stdlib.h>
|
||||
import "C"
|
||||
|
||||
// Ptr takes a slice or pointer (to a singular scalar value or the first
|
||||
// element of an array or slice) and returns its GL-compatible address.
|
||||
//
|
||||
// For example:
|
||||
//
|
||||
// var data []uint8
|
||||
// ...
|
||||
// gl.TexImage2D(gl.TEXTURE_2D, ..., gl.UNSIGNED_BYTE, gl.Ptr(&data[0]))
|
||||
func Ptr(data interface{}) unsafe.Pointer {
|
||||
if data == nil {
|
||||
return unsafe.Pointer(nil)
|
||||
}
|
||||
var addr unsafe.Pointer
|
||||
v := reflect.ValueOf(data)
|
||||
switch v.Type().Kind() {
|
||||
case reflect.Ptr:
|
||||
e := v.Elem()
|
||||
switch e.Kind() {
|
||||
case
|
||||
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
|
||||
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
|
||||
reflect.Float32, reflect.Float64:
|
||||
addr = unsafe.Pointer(e.UnsafeAddr())
|
||||
default:
|
||||
panic(fmt.Errorf("unsupported pointer to type %s; must be a slice or pointer to a singular scalar value or the first element of an array or slice", e.Kind()))
|
||||
}
|
||||
case reflect.Uintptr:
|
||||
addr = unsafe.Pointer(v.Pointer())
|
||||
case reflect.Slice:
|
||||
addr = unsafe.Pointer(v.Index(0).UnsafeAddr())
|
||||
default:
|
||||
panic(fmt.Errorf("unsupported type %s; must be a slice or pointer to a singular scalar value or the first element of an array or slice", v.Type()))
|
||||
}
|
||||
return addr
|
||||
}
|
||||
|
||||
// PtrOffset takes a pointer offset and returns a GL-compatible pointer.
|
||||
// Useful for functions such as glVertexAttribPointer that take pointer
|
||||
// parameters indicating an offset rather than an absolute memory address.
|
||||
func PtrOffset(offset int) unsafe.Pointer {
|
||||
return unsafe.Pointer(uintptr(offset))
|
||||
}
|
||||
|
||||
// Str takes a null-terminated Go string and returns its GL-compatible address.
|
||||
// This function reaches into Go string storage in an unsafe way so the caller
|
||||
// must ensure the string is not garbage collected.
|
||||
func Str(str string) *uint8 {
|
||||
if !strings.HasSuffix(str, "\x00") {
|
||||
panic("str argument missing null terminator: " + str)
|
||||
}
|
||||
header := (*reflect.StringHeader)(unsafe.Pointer(&str))
|
||||
return (*uint8)(unsafe.Pointer(header.Data))
|
||||
}
|
||||
|
||||
// GoStr takes a null-terminated string returned by OpenGL and constructs a
|
||||
// corresponding Go string.
|
||||
func GoStr(cstr *uint8) string {
|
||||
return C.GoString((*C.char)(unsafe.Pointer(cstr)))
|
||||
}
|
||||
|
||||
// Strs takes a list of Go strings (with or without null-termination) and
|
||||
// returns their C counterpart.
|
||||
//
|
||||
// The returned free function must be called once you are done using the strings
|
||||
// in order to free the memory.
|
||||
//
|
||||
// If no strings are provided as a parameter this function will panic.
|
||||
func Strs(strs ...string) (cstrs **uint8, free func()) {
|
||||
if len(strs) == 0 {
|
||||
panic("Strs: expected at least 1 string")
|
||||
}
|
||||
|
||||
// Allocate a contiguous array large enough to hold all the strings' contents.
|
||||
n := 0
|
||||
for i := range strs {
|
||||
n += len(strs[i])
|
||||
}
|
||||
data := C.malloc(C.size_t(n))
|
||||
|
||||
// Copy all the strings into data.
|
||||
dataSlice := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
|
||||
Data: uintptr(data),
|
||||
Len: n,
|
||||
Cap: n,
|
||||
}))
|
||||
css := make([]*uint8, len(strs)) // Populated with pointers to each string.
|
||||
offset := 0
|
||||
for i := range strs {
|
||||
copy(dataSlice[offset:offset+len(strs[i])], strs[i][:]) // Copy strs[i] into proper data location.
|
||||
css[i] = (*uint8)(unsafe.Pointer(&dataSlice[offset])) // Set a pointer to it.
|
||||
offset += len(strs[i])
|
||||
}
|
||||
|
||||
return (**uint8)(&css[0]), func() { C.free(data) }
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
// Glow automatically generated OpenGL binding: http://github.com/go-gl/glow
|
||||
|
||||
package gles2
|
||||
|
||||
import "C"
|
||||
import "unsafe"
|
||||
|
||||
type DebugProc func(
|
||||
source uint32,
|
||||
gltype uint32,
|
||||
id uint32,
|
||||
severity uint32,
|
||||
length int32,
|
||||
message string,
|
||||
userParam unsafe.Pointer)
|
||||
|
||||
var userDebugCallback DebugProc
|
||||
|
||||
//export glowDebugCallback_gles231
|
||||
func glowDebugCallback_gles231(
|
||||
source uint32,
|
||||
gltype uint32,
|
||||
id uint32,
|
||||
severity uint32,
|
||||
length int32,
|
||||
message *uint8,
|
||||
userParam unsafe.Pointer) {
|
||||
if userDebugCallback != nil {
|
||||
userDebugCallback(source, gltype, id, severity, length, GoStr(message), userParam)
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,65 @@
|
|||
// This file implements GlowGetProcAddress for every supported platform. The
|
||||
// correct version is chosen automatically based on build tags:
|
||||
// windows: WGL
|
||||
// darwin: CGL
|
||||
// linux freebsd: GLX
|
||||
// Use of EGL instead of the platform's default (listed above) is made possible
|
||||
// via the "egl" build tag.
|
||||
// It is also possible to install your own function outside this package for
|
||||
// retrieving OpenGL function pointers, to do this see InitWithProcAddrFunc.
|
||||
package gles2
|
||||
|
||||
/*
|
||||
#cgo windows CFLAGS: -DTAG_WINDOWS
|
||||
#cgo windows LDFLAGS: -lopengl32
|
||||
#cgo darwin CFLAGS: -DTAG_DARWIN
|
||||
#cgo darwin LDFLAGS: -framework OpenGL
|
||||
#cgo linux freebsd CFLAGS: -DTAG_POSIX
|
||||
#cgo linux freebsd LDFLAGS: -lGL
|
||||
#cgo egl CFLAGS: -DTAG_EGL
|
||||
#cgo egl LDFLAGS: -lEGL
|
||||
// Check the EGL tag first as it takes priority over the platform's default
|
||||
// configuration of WGL/GLX/CGL.
|
||||
#if defined(TAG_EGL)
|
||||
#include <stdlib.h>
|
||||
#include <EGL/egl.h>
|
||||
void* GlowGetProcAddress_gles231(const char* name) {
|
||||
return eglGetProcAddress(name);
|
||||
}
|
||||
#elif defined(TAG_WINDOWS)
|
||||
#define WIN32_LEAN_AND_MEAN 1
|
||||
#include <windows.h>
|
||||
#include <stdlib.h>
|
||||
static HMODULE ogl32dll = NULL;
|
||||
void* GlowGetProcAddress_gles231(const char* name) {
|
||||
void* pf = wglGetProcAddress((LPCSTR) name);
|
||||
if (pf) {
|
||||
return pf;
|
||||
}
|
||||
if (ogl32dll == NULL) {
|
||||
ogl32dll = LoadLibraryA("opengl32.dll");
|
||||
}
|
||||
return GetProcAddress(ogl32dll, (LPCSTR) name);
|
||||
}
|
||||
#elif defined(TAG_DARWIN)
|
||||
#include <stdlib.h>
|
||||
#include <dlfcn.h>
|
||||
void* GlowGetProcAddress_gles231(const char* name) {
|
||||
return dlsym(RTLD_DEFAULT, name);
|
||||
}
|
||||
#elif defined(TAG_POSIX)
|
||||
#include <stdlib.h>
|
||||
#include <GL/glx.h>
|
||||
void* GlowGetProcAddress_gles231(const char* name) {
|
||||
return glXGetProcAddress((const GLubyte *) name);
|
||||
}
|
||||
#endif
|
||||
*/
|
||||
import "C"
|
||||
import "unsafe"
|
||||
|
||||
func getProcAddress(namea string) unsafe.Pointer {
|
||||
cname := C.CString(namea)
|
||||
defer C.free(unsafe.Pointer(cname))
|
||||
return C.GlowGetProcAddress_gles231(cname)
|
||||
}
|
|
@ -0,0 +1,109 @@
|
|||
// Glow automatically generated OpenGL binding: http://github.com/go-gl/glow
|
||||
|
||||
package gl
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// #include <stdlib.h>
|
||||
import "C"
|
||||
|
||||
// Ptr takes a slice or pointer (to a singular scalar value or the first
|
||||
// element of an array or slice) and returns its GL-compatible address.
|
||||
//
|
||||
// For example:
|
||||
//
|
||||
// var data []uint8
|
||||
// ...
|
||||
// gl.TexImage2D(gl.TEXTURE_2D, ..., gl.UNSIGNED_BYTE, gl.Ptr(&data[0]))
|
||||
func Ptr(data interface{}) unsafe.Pointer {
|
||||
if data == nil {
|
||||
return unsafe.Pointer(nil)
|
||||
}
|
||||
var addr unsafe.Pointer
|
||||
v := reflect.ValueOf(data)
|
||||
switch v.Type().Kind() {
|
||||
case reflect.Ptr:
|
||||
e := v.Elem()
|
||||
switch e.Kind() {
|
||||
case
|
||||
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
|
||||
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
|
||||
reflect.Float32, reflect.Float64:
|
||||
addr = unsafe.Pointer(e.UnsafeAddr())
|
||||
default:
|
||||
panic(fmt.Errorf("unsupported pointer to type %s; must be a slice or pointer to a singular scalar value or the first element of an array or slice", e.Kind()))
|
||||
}
|
||||
case reflect.Uintptr:
|
||||
addr = unsafe.Pointer(v.Pointer())
|
||||
case reflect.Slice:
|
||||
addr = unsafe.Pointer(v.Index(0).UnsafeAddr())
|
||||
default:
|
||||
panic(fmt.Errorf("unsupported type %s; must be a slice or pointer to a singular scalar value or the first element of an array or slice", v.Type()))
|
||||
}
|
||||
return addr
|
||||
}
|
||||
|
||||
// PtrOffset takes a pointer offset and returns a GL-compatible pointer.
|
||||
// Useful for functions such as glVertexAttribPointer that take pointer
|
||||
// parameters indicating an offset rather than an absolute memory address.
|
||||
func PtrOffset(offset int) unsafe.Pointer {
|
||||
return unsafe.Pointer(uintptr(offset))
|
||||
}
|
||||
|
||||
// Str takes a null-terminated Go string and returns its GL-compatible address.
|
||||
// This function reaches into Go string storage in an unsafe way so the caller
|
||||
// must ensure the string is not garbage collected.
|
||||
func Str(str string) *uint8 {
|
||||
if !strings.HasSuffix(str, "\x00") {
|
||||
panic("str argument missing null terminator: " + str)
|
||||
}
|
||||
header := (*reflect.StringHeader)(unsafe.Pointer(&str))
|
||||
return (*uint8)(unsafe.Pointer(header.Data))
|
||||
}
|
||||
|
||||
// GoStr takes a null-terminated string returned by OpenGL and constructs a
|
||||
// corresponding Go string.
|
||||
func GoStr(cstr *uint8) string {
|
||||
return C.GoString((*C.char)(unsafe.Pointer(cstr)))
|
||||
}
|
||||
|
||||
// Strs takes a list of Go strings (with or without null-termination) and
|
||||
// returns their C counterpart.
|
||||
//
|
||||
// The returned free function must be called once you are done using the strings
|
||||
// in order to free the memory.
|
||||
//
|
||||
// If no strings are provided as a parameter this function will panic.
|
||||
func Strs(strs ...string) (cstrs **uint8, free func()) {
|
||||
if len(strs) == 0 {
|
||||
panic("Strs: expected at least 1 string")
|
||||
}
|
||||
|
||||
// Allocate a contiguous array large enough to hold all the strings' contents.
|
||||
n := 0
|
||||
for i := range strs {
|
||||
n += len(strs[i])
|
||||
}
|
||||
data := C.malloc(C.size_t(n))
|
||||
|
||||
// Copy all the strings into data.
|
||||
dataSlice := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
|
||||
Data: uintptr(data),
|
||||
Len: n,
|
||||
Cap: n,
|
||||
}))
|
||||
css := make([]*uint8, len(strs)) // Populated with pointers to each string.
|
||||
offset := 0
|
||||
for i := range strs {
|
||||
copy(dataSlice[offset:offset+len(strs[i])], strs[i][:]) // Copy strs[i] into proper data location.
|
||||
css[i] = (*uint8)(unsafe.Pointer(&dataSlice[offset])) // Set a pointer to it.
|
||||
offset += len(strs[i])
|
||||
}
|
||||
|
||||
return (**uint8)(&css[0]), func() { C.free(data) }
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
// Glow automatically generated OpenGL binding: http://github.com/go-gl/glow
|
||||
|
||||
package gl
|
||||
|
||||
import "C"
|
||||
import "unsafe"
|
||||
|
||||
type DebugProc func(
|
||||
source uint32,
|
||||
gltype uint32,
|
||||
id uint32,
|
||||
severity uint32,
|
||||
length int32,
|
||||
message string,
|
||||
userParam unsafe.Pointer)
|
||||
|
||||
var userDebugCallback DebugProc
|
||||
|
||||
//export glowDebugCallback_glcompatibility32
|
||||
func glowDebugCallback_glcompatibility32(
|
||||
source uint32,
|
||||
gltype uint32,
|
||||
id uint32,
|
||||
severity uint32,
|
||||
length int32,
|
||||
message *uint8,
|
||||
userParam unsafe.Pointer) {
|
||||
if userDebugCallback != nil {
|
||||
userDebugCallback(source, gltype, id, severity, length, GoStr(message), userParam)
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,65 @@
|
|||
// This file implements GlowGetProcAddress for every supported platform. The
|
||||
// correct version is chosen automatically based on build tags:
|
||||
// windows: WGL
|
||||
// darwin: CGL
|
||||
// linux freebsd: GLX
|
||||
// Use of EGL instead of the platform's default (listed above) is made possible
|
||||
// via the "egl" build tag.
|
||||
// It is also possible to install your own function outside this package for
|
||||
// retrieving OpenGL function pointers, to do this see InitWithProcAddrFunc.
|
||||
package gl
|
||||
|
||||
/*
|
||||
#cgo windows CFLAGS: -DTAG_WINDOWS
|
||||
#cgo windows LDFLAGS: -lopengl32
|
||||
#cgo darwin CFLAGS: -DTAG_DARWIN
|
||||
#cgo darwin LDFLAGS: -framework OpenGL
|
||||
#cgo linux freebsd CFLAGS: -DTAG_POSIX
|
||||
#cgo linux freebsd LDFLAGS: -lGL
|
||||
#cgo egl CFLAGS: -DTAG_EGL
|
||||
#cgo egl LDFLAGS: -lEGL
|
||||
// Check the EGL tag first as it takes priority over the platform's default
|
||||
// configuration of WGL/GLX/CGL.
|
||||
#if defined(TAG_EGL)
|
||||
#include <stdlib.h>
|
||||
#include <EGL/egl.h>
|
||||
void* GlowGetProcAddress_glcompatibility32(const char* name) {
|
||||
return eglGetProcAddress(name);
|
||||
}
|
||||
#elif defined(TAG_WINDOWS)
|
||||
#define WIN32_LEAN_AND_MEAN 1
|
||||
#include <windows.h>
|
||||
#include <stdlib.h>
|
||||
static HMODULE ogl32dll = NULL;
|
||||
void* GlowGetProcAddress_glcompatibility32(const char* name) {
|
||||
void* pf = wglGetProcAddress((LPCSTR) name);
|
||||
if (pf) {
|
||||
return pf;
|
||||
}
|
||||
if (ogl32dll == NULL) {
|
||||
ogl32dll = LoadLibraryA("opengl32.dll");
|
||||
}
|
||||
return GetProcAddress(ogl32dll, (LPCSTR) name);
|
||||
}
|
||||
#elif defined(TAG_DARWIN)
|
||||
#include <stdlib.h>
|
||||
#include <dlfcn.h>
|
||||
void* GlowGetProcAddress_glcompatibility32(const char* name) {
|
||||
return dlsym(RTLD_DEFAULT, name);
|
||||
}
|
||||
#elif defined(TAG_POSIX)
|
||||
#include <stdlib.h>
|
||||
#include <GL/glx.h>
|
||||
void* GlowGetProcAddress_glcompatibility32(const char* name) {
|
||||
return glXGetProcAddress((const GLubyte *) name);
|
||||
}
|
||||
#endif
|
||||
*/
|
||||
import "C"
|
||||
import "unsafe"
|
||||
|
||||
func getProcAddress(namea string) unsafe.Pointer {
|
||||
cname := C.CString(namea)
|
||||
defer C.free(unsafe.Pointer(cname))
|
||||
return C.GlowGetProcAddress_glcompatibility32(cname)
|
||||
}
|
|
@ -0,0 +1,109 @@
|
|||
// Glow automatically generated OpenGL binding: http://github.com/go-gl/glow
|
||||
|
||||
package gl
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// #include <stdlib.h>
|
||||
import "C"
|
||||
|
||||
// Ptr takes a slice or pointer (to a singular scalar value or the first
|
||||
// element of an array or slice) and returns its GL-compatible address.
|
||||
//
|
||||
// For example:
|
||||
//
|
||||
// var data []uint8
|
||||
// ...
|
||||
// gl.TexImage2D(gl.TEXTURE_2D, ..., gl.UNSIGNED_BYTE, gl.Ptr(&data[0]))
|
||||
func Ptr(data interface{}) unsafe.Pointer {
|
||||
if data == nil {
|
||||
return unsafe.Pointer(nil)
|
||||
}
|
||||
var addr unsafe.Pointer
|
||||
v := reflect.ValueOf(data)
|
||||
switch v.Type().Kind() {
|
||||
case reflect.Ptr:
|
||||
e := v.Elem()
|
||||
switch e.Kind() {
|
||||
case
|
||||
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
|
||||
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
|
||||
reflect.Float32, reflect.Float64:
|
||||
addr = unsafe.Pointer(e.UnsafeAddr())
|
||||
default:
|
||||
panic(fmt.Errorf("unsupported pointer to type %s; must be a slice or pointer to a singular scalar value or the first element of an array or slice", e.Kind()))
|
||||
}
|
||||
case reflect.Uintptr:
|
||||
addr = unsafe.Pointer(v.Pointer())
|
||||
case reflect.Slice:
|
||||
addr = unsafe.Pointer(v.Index(0).UnsafeAddr())
|
||||
default:
|
||||
panic(fmt.Errorf("unsupported type %s; must be a slice or pointer to a singular scalar value or the first element of an array or slice", v.Type()))
|
||||
}
|
||||
return addr
|
||||
}
|
||||
|
||||
// PtrOffset takes a pointer offset and returns a GL-compatible pointer.
|
||||
// Useful for functions such as glVertexAttribPointer that take pointer
|
||||
// parameters indicating an offset rather than an absolute memory address.
|
||||
func PtrOffset(offset int) unsafe.Pointer {
|
||||
return unsafe.Pointer(uintptr(offset))
|
||||
}
|
||||
|
||||
// Str takes a null-terminated Go string and returns its GL-compatible address.
|
||||
// This function reaches into Go string storage in an unsafe way so the caller
|
||||
// must ensure the string is not garbage collected.
|
||||
func Str(str string) *uint8 {
|
||||
if !strings.HasSuffix(str, "\x00") {
|
||||
panic("str argument missing null terminator: " + str)
|
||||
}
|
||||
header := (*reflect.StringHeader)(unsafe.Pointer(&str))
|
||||
return (*uint8)(unsafe.Pointer(header.Data))
|
||||
}
|
||||
|
||||
// GoStr takes a null-terminated string returned by OpenGL and constructs a
|
||||
// corresponding Go string.
|
||||
func GoStr(cstr *uint8) string {
|
||||
return C.GoString((*C.char)(unsafe.Pointer(cstr)))
|
||||
}
|
||||
|
||||
// Strs takes a list of Go strings (with or without null-termination) and
|
||||
// returns their C counterpart.
|
||||
//
|
||||
// The returned free function must be called once you are done using the strings
|
||||
// in order to free the memory.
|
||||
//
|
||||
// If no strings are provided as a parameter this function will panic.
|
||||
func Strs(strs ...string) (cstrs **uint8, free func()) {
|
||||
if len(strs) == 0 {
|
||||
panic("Strs: expected at least 1 string")
|
||||
}
|
||||
|
||||
// Allocate a contiguous array large enough to hold all the strings' contents.
|
||||
n := 0
|
||||
for i := range strs {
|
||||
n += len(strs[i])
|
||||
}
|
||||
data := C.malloc(C.size_t(n))
|
||||
|
||||
// Copy all the strings into data.
|
||||
dataSlice := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
|
||||
Data: uintptr(data),
|
||||
Len: n,
|
||||
Cap: n,
|
||||
}))
|
||||
css := make([]*uint8, len(strs)) // Populated with pointers to each string.
|
||||
offset := 0
|
||||
for i := range strs {
|
||||
copy(dataSlice[offset:offset+len(strs[i])], strs[i][:]) // Copy strs[i] into proper data location.
|
||||
css[i] = (*uint8)(unsafe.Pointer(&dataSlice[offset])) // Set a pointer to it.
|
||||
offset += len(strs[i])
|
||||
}
|
||||
|
||||
return (**uint8)(&css[0]), func() { C.free(data) }
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
// Glow automatically generated OpenGL binding: http://github.com/go-gl/glow
|
||||
|
||||
package gl
|
||||
|
||||
import "C"
|
||||
import "unsafe"
|
||||
|
||||
type DebugProc func(
|
||||
source uint32,
|
||||
gltype uint32,
|
||||
id uint32,
|
||||
severity uint32,
|
||||
length int32,
|
||||
message string,
|
||||
userParam unsafe.Pointer)
|
||||
|
||||
var userDebugCallback DebugProc
|
||||
|
||||
//export glowDebugCallback_glcore32
|
||||
func glowDebugCallback_glcore32(
|
||||
source uint32,
|
||||
gltype uint32,
|
||||
id uint32,
|
||||
severity uint32,
|
||||
length int32,
|
||||
message *uint8,
|
||||
userParam unsafe.Pointer) {
|
||||
if userDebugCallback != nil {
|
||||
userDebugCallback(source, gltype, id, severity, length, GoStr(message), userParam)
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,65 @@
|
|||
// This file implements GlowGetProcAddress for every supported platform. The
|
||||
// correct version is chosen automatically based on build tags:
|
||||
// windows: WGL
|
||||
// darwin: CGL
|
||||
// linux freebsd: GLX
|
||||
// Use of EGL instead of the platform's default (listed above) is made possible
|
||||
// via the "egl" build tag.
|
||||
// It is also possible to install your own function outside this package for
|
||||
// retrieving OpenGL function pointers, to do this see InitWithProcAddrFunc.
|
||||
package gl
|
||||
|
||||
/*
|
||||
#cgo windows CFLAGS: -DTAG_WINDOWS
|
||||
#cgo windows LDFLAGS: -lopengl32
|
||||
#cgo darwin CFLAGS: -DTAG_DARWIN
|
||||
#cgo darwin LDFLAGS: -framework OpenGL
|
||||
#cgo linux freebsd CFLAGS: -DTAG_POSIX
|
||||
#cgo linux freebsd LDFLAGS: -lGL
|
||||
#cgo egl CFLAGS: -DTAG_EGL
|
||||
#cgo egl LDFLAGS: -lEGL
|
||||
// Check the EGL tag first as it takes priority over the platform's default
|
||||
// configuration of WGL/GLX/CGL.
|
||||
#if defined(TAG_EGL)
|
||||
#include <stdlib.h>
|
||||
#include <EGL/egl.h>
|
||||
void* GlowGetProcAddress_glcore32(const char* name) {
|
||||
return eglGetProcAddress(name);
|
||||
}
|
||||
#elif defined(TAG_WINDOWS)
|
||||
#define WIN32_LEAN_AND_MEAN 1
|
||||
#include <windows.h>
|
||||
#include <stdlib.h>
|
||||
static HMODULE ogl32dll = NULL;
|
||||
void* GlowGetProcAddress_glcore32(const char* name) {
|
||||
void* pf = wglGetProcAddress((LPCSTR) name);
|
||||
if (pf) {
|
||||
return pf;
|
||||
}
|
||||
if (ogl32dll == NULL) {
|
||||
ogl32dll = LoadLibraryA("opengl32.dll");
|
||||
}
|
||||
return GetProcAddress(ogl32dll, (LPCSTR) name);
|
||||
}
|
||||
#elif defined(TAG_DARWIN)
|
||||
#include <stdlib.h>
|
||||
#include <dlfcn.h>
|
||||
void* GlowGetProcAddress_glcore32(const char* name) {
|
||||
return dlsym(RTLD_DEFAULT, name);
|
||||
}
|
||||
#elif defined(TAG_POSIX)
|
||||
#include <stdlib.h>
|
||||
#include <GL/glx.h>
|
||||
void* GlowGetProcAddress_glcore32(const char* name) {
|
||||
return glXGetProcAddress((const GLubyte *) name);
|
||||
}
|
||||
#endif
|
||||
*/
|
||||
import "C"
|
||||
import "unsafe"
|
||||
|
||||
func getProcAddress(namea string) unsafe.Pointer {
|
||||
cname := C.CString(namea)
|
||||
defer C.free(unsafe.Pointer(cname))
|
||||
return C.GlowGetProcAddress_glcore32(cname)
|
||||
}
|
|
@ -0,0 +1,109 @@
|
|||
// Glow automatically generated OpenGL binding: http://github.com/go-gl/glow
|
||||
|
||||
package gl
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// #include <stdlib.h>
|
||||
import "C"
|
||||
|
||||
// Ptr takes a slice or pointer (to a singular scalar value or the first
|
||||
// element of an array or slice) and returns its GL-compatible address.
|
||||
//
|
||||
// For example:
|
||||
//
|
||||
// var data []uint8
|
||||
// ...
|
||||
// gl.TexImage2D(gl.TEXTURE_2D, ..., gl.UNSIGNED_BYTE, gl.Ptr(&data[0]))
|
||||
func Ptr(data interface{}) unsafe.Pointer {
|
||||
if data == nil {
|
||||
return unsafe.Pointer(nil)
|
||||
}
|
||||
var addr unsafe.Pointer
|
||||
v := reflect.ValueOf(data)
|
||||
switch v.Type().Kind() {
|
||||
case reflect.Ptr:
|
||||
e := v.Elem()
|
||||
switch e.Kind() {
|
||||
case
|
||||
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
|
||||
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
|
||||
reflect.Float32, reflect.Float64:
|
||||
addr = unsafe.Pointer(e.UnsafeAddr())
|
||||
default:
|
||||
panic(fmt.Errorf("unsupported pointer to type %s; must be a slice or pointer to a singular scalar value or the first element of an array or slice", e.Kind()))
|
||||
}
|
||||
case reflect.Uintptr:
|
||||
addr = unsafe.Pointer(v.Pointer())
|
||||
case reflect.Slice:
|
||||
addr = unsafe.Pointer(v.Index(0).UnsafeAddr())
|
||||
default:
|
||||
panic(fmt.Errorf("unsupported type %s; must be a slice or pointer to a singular scalar value or the first element of an array or slice", v.Type()))
|
||||
}
|
||||
return addr
|
||||
}
|
||||
|
||||
// PtrOffset takes a pointer offset and returns a GL-compatible pointer.
|
||||
// Useful for functions such as glVertexAttribPointer that take pointer
|
||||
// parameters indicating an offset rather than an absolute memory address.
|
||||
func PtrOffset(offset int) unsafe.Pointer {
|
||||
return unsafe.Pointer(uintptr(offset))
|
||||
}
|
||||
|
||||
// Str takes a null-terminated Go string and returns its GL-compatible address.
|
||||
// This function reaches into Go string storage in an unsafe way so the caller
|
||||
// must ensure the string is not garbage collected.
|
||||
func Str(str string) *uint8 {
|
||||
if !strings.HasSuffix(str, "\x00") {
|
||||
panic("str argument missing null terminator: " + str)
|
||||
}
|
||||
header := (*reflect.StringHeader)(unsafe.Pointer(&str))
|
||||
return (*uint8)(unsafe.Pointer(header.Data))
|
||||
}
|
||||
|
||||
// GoStr takes a null-terminated string returned by OpenGL and constructs a
|
||||
// corresponding Go string.
|
||||
func GoStr(cstr *uint8) string {
|
||||
return C.GoString((*C.char)(unsafe.Pointer(cstr)))
|
||||
}
|
||||
|
||||
// Strs takes a list of Go strings (with or without null-termination) and
|
||||
// returns their C counterpart.
|
||||
//
|
||||
// The returned free function must be called once you are done using the strings
|
||||
// in order to free the memory.
|
||||
//
|
||||
// If no strings are provided as a parameter this function will panic.
|
||||
func Strs(strs ...string) (cstrs **uint8, free func()) {
|
||||
if len(strs) == 0 {
|
||||
panic("Strs: expected at least 1 string")
|
||||
}
|
||||
|
||||
// Allocate a contiguous array large enough to hold all the strings' contents.
|
||||
n := 0
|
||||
for i := range strs {
|
||||
n += len(strs[i])
|
||||
}
|
||||
data := C.malloc(C.size_t(n))
|
||||
|
||||
// Copy all the strings into data.
|
||||
dataSlice := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
|
||||
Data: uintptr(data),
|
||||
Len: n,
|
||||
Cap: n,
|
||||
}))
|
||||
css := make([]*uint8, len(strs)) // Populated with pointers to each string.
|
||||
offset := 0
|
||||
for i := range strs {
|
||||
copy(dataSlice[offset:offset+len(strs[i])], strs[i][:]) // Copy strs[i] into proper data location.
|
||||
css[i] = (*uint8)(unsafe.Pointer(&dataSlice[offset])) // Set a pointer to it.
|
||||
offset += len(strs[i])
|
||||
}
|
||||
|
||||
return (**uint8)(&css[0]), func() { C.free(data) }
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
// Glow automatically generated OpenGL binding: http://github.com/go-gl/glow
|
||||
|
||||
package gl
|
||||
|
||||
import "C"
|
||||
import "unsafe"
|
||||
|
||||
type DebugProc func(
|
||||
source uint32,
|
||||
gltype uint32,
|
||||
id uint32,
|
||||
severity uint32,
|
||||
length int32,
|
||||
message string,
|
||||
userParam unsafe.Pointer)
|
||||
|
||||
var userDebugCallback DebugProc
|
||||
|
||||
//export glowDebugCallback_glcompatibility33
|
||||
func glowDebugCallback_glcompatibility33(
|
||||
source uint32,
|
||||
gltype uint32,
|
||||
id uint32,
|
||||
severity uint32,
|
||||
length int32,
|
||||
message *uint8,
|
||||
userParam unsafe.Pointer) {
|
||||
if userDebugCallback != nil {
|
||||
userDebugCallback(source, gltype, id, severity, length, GoStr(message), userParam)
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,65 @@
|
|||
// This file implements GlowGetProcAddress for every supported platform. The
|
||||
// correct version is chosen automatically based on build tags:
|
||||
// windows: WGL
|
||||
// darwin: CGL
|
||||
// linux freebsd: GLX
|
||||
// Use of EGL instead of the platform's default (listed above) is made possible
|
||||
// via the "egl" build tag.
|
||||
// It is also possible to install your own function outside this package for
|
||||
// retrieving OpenGL function pointers, to do this see InitWithProcAddrFunc.
|
||||
package gl
|
||||
|
||||
/*
|
||||
#cgo windows CFLAGS: -DTAG_WINDOWS
|
||||
#cgo windows LDFLAGS: -lopengl32
|
||||
#cgo darwin CFLAGS: -DTAG_DARWIN
|
||||
#cgo darwin LDFLAGS: -framework OpenGL
|
||||
#cgo linux freebsd CFLAGS: -DTAG_POSIX
|
||||
#cgo linux freebsd LDFLAGS: -lGL
|
||||
#cgo egl CFLAGS: -DTAG_EGL
|
||||
#cgo egl LDFLAGS: -lEGL
|
||||
// Check the EGL tag first as it takes priority over the platform's default
|
||||
// configuration of WGL/GLX/CGL.
|
||||
#if defined(TAG_EGL)
|
||||
#include <stdlib.h>
|
||||
#include <EGL/egl.h>
|
||||
void* GlowGetProcAddress_glcompatibility33(const char* name) {
|
||||
return eglGetProcAddress(name);
|
||||
}
|
||||
#elif defined(TAG_WINDOWS)
|
||||
#define WIN32_LEAN_AND_MEAN 1
|
||||
#include <windows.h>
|
||||
#include <stdlib.h>
|
||||
static HMODULE ogl32dll = NULL;
|
||||
void* GlowGetProcAddress_glcompatibility33(const char* name) {
|
||||
void* pf = wglGetProcAddress((LPCSTR) name);
|
||||
if (pf) {
|
||||
return pf;
|
||||
}
|
||||
if (ogl32dll == NULL) {
|
||||
ogl32dll = LoadLibraryA("opengl32.dll");
|
||||
}
|
||||
return GetProcAddress(ogl32dll, (LPCSTR) name);
|
||||
}
|
||||
#elif defined(TAG_DARWIN)
|
||||
#include <stdlib.h>
|
||||
#include <dlfcn.h>
|
||||
void* GlowGetProcAddress_glcompatibility33(const char* name) {
|
||||
return dlsym(RTLD_DEFAULT, name);
|
||||
}
|
||||
#elif defined(TAG_POSIX)
|
||||
#include <stdlib.h>
|
||||
#include <GL/glx.h>
|
||||
void* GlowGetProcAddress_glcompatibility33(const char* name) {
|
||||
return glXGetProcAddress((const GLubyte *) name);
|
||||
}
|
||||
#endif
|
||||
*/
|
||||
import "C"
|
||||
import "unsafe"
|
||||
|
||||
func getProcAddress(namea string) unsafe.Pointer {
|
||||
cname := C.CString(namea)
|
||||
defer C.free(unsafe.Pointer(cname))
|
||||
return C.GlowGetProcAddress_glcompatibility33(cname)
|
||||
}
|
|
@ -0,0 +1,109 @@
|
|||
// Glow automatically generated OpenGL binding: http://github.com/go-gl/glow
|
||||
|
||||
package gl
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// #include <stdlib.h>
|
||||
import "C"
|
||||
|
||||
// Ptr takes a slice or pointer (to a singular scalar value or the first
|
||||
// element of an array or slice) and returns its GL-compatible address.
|
||||
//
|
||||
// For example:
|
||||
//
|
||||
// var data []uint8
|
||||
// ...
|
||||
// gl.TexImage2D(gl.TEXTURE_2D, ..., gl.UNSIGNED_BYTE, gl.Ptr(&data[0]))
|
||||
func Ptr(data interface{}) unsafe.Pointer {
|
||||
if data == nil {
|
||||
return unsafe.Pointer(nil)
|
||||
}
|
||||
var addr unsafe.Pointer
|
||||
v := reflect.ValueOf(data)
|
||||
switch v.Type().Kind() {
|
||||
case reflect.Ptr:
|
||||
e := v.Elem()
|
||||
switch e.Kind() {
|
||||
case
|
||||
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
|
||||
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
|
||||
reflect.Float32, reflect.Float64:
|
||||
addr = unsafe.Pointer(e.UnsafeAddr())
|
||||
default:
|
||||
panic(fmt.Errorf("unsupported pointer to type %s; must be a slice or pointer to a singular scalar value or the first element of an array or slice", e.Kind()))
|
||||
}
|
||||
case reflect.Uintptr:
|
||||
addr = unsafe.Pointer(v.Pointer())
|
||||
case reflect.Slice:
|
||||
addr = unsafe.Pointer(v.Index(0).UnsafeAddr())
|
||||
default:
|
||||
panic(fmt.Errorf("unsupported type %s; must be a slice or pointer to a singular scalar value or the first element of an array or slice", v.Type()))
|
||||
}
|
||||
return addr
|
||||
}
|
||||
|
||||
// PtrOffset takes a pointer offset and returns a GL-compatible pointer.
|
||||
// Useful for functions such as glVertexAttribPointer that take pointer
|
||||
// parameters indicating an offset rather than an absolute memory address.
|
||||
func PtrOffset(offset int) unsafe.Pointer {
|
||||
return unsafe.Pointer(uintptr(offset))
|
||||
}
|
||||
|
||||
// Str takes a null-terminated Go string and returns its GL-compatible address.
|
||||
// This function reaches into Go string storage in an unsafe way so the caller
|
||||
// must ensure the string is not garbage collected.
|
||||
func Str(str string) *uint8 {
|
||||
if !strings.HasSuffix(str, "\x00") {
|
||||
panic("str argument missing null terminator: " + str)
|
||||
}
|
||||
header := (*reflect.StringHeader)(unsafe.Pointer(&str))
|
||||
return (*uint8)(unsafe.Pointer(header.Data))
|
||||
}
|
||||
|
||||
// GoStr takes a null-terminated string returned by OpenGL and constructs a
|
||||
// corresponding Go string.
|
||||
func GoStr(cstr *uint8) string {
|
||||
return C.GoString((*C.char)(unsafe.Pointer(cstr)))
|
||||
}
|
||||
|
||||
// Strs takes a list of Go strings (with or without null-termination) and
|
||||
// returns their C counterpart.
|
||||
//
|
||||
// The returned free function must be called once you are done using the strings
|
||||
// in order to free the memory.
|
||||
//
|
||||
// If no strings are provided as a parameter this function will panic.
|
||||
func Strs(strs ...string) (cstrs **uint8, free func()) {
|
||||
if len(strs) == 0 {
|
||||
panic("Strs: expected at least 1 string")
|
||||
}
|
||||
|
||||
// Allocate a contiguous array large enough to hold all the strings' contents.
|
||||
n := 0
|
||||
for i := range strs {
|
||||
n += len(strs[i])
|
||||
}
|
||||
data := C.malloc(C.size_t(n))
|
||||
|
||||
// Copy all the strings into data.
|
||||
dataSlice := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
|
||||
Data: uintptr(data),
|
||||
Len: n,
|
||||
Cap: n,
|
||||
}))
|
||||
css := make([]*uint8, len(strs)) // Populated with pointers to each string.
|
||||
offset := 0
|
||||
for i := range strs {
|
||||
copy(dataSlice[offset:offset+len(strs[i])], strs[i][:]) // Copy strs[i] into proper data location.
|
||||
css[i] = (*uint8)(unsafe.Pointer(&dataSlice[offset])) // Set a pointer to it.
|
||||
offset += len(strs[i])
|
||||
}
|
||||
|
||||
return (**uint8)(&css[0]), func() { C.free(data) }
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
// Glow automatically generated OpenGL binding: http://github.com/go-gl/glow
|
||||
|
||||
package gl
|
||||
|
||||
import "C"
|
||||
import "unsafe"
|
||||
|
||||
type DebugProc func(
|
||||
source uint32,
|
||||
gltype uint32,
|
||||
id uint32,
|
||||
severity uint32,
|
||||
length int32,
|
||||
message string,
|
||||
userParam unsafe.Pointer)
|
||||
|
||||
var userDebugCallback DebugProc
|
||||
|
||||
//export glowDebugCallback_glcore33
|
||||
func glowDebugCallback_glcore33(
|
||||
source uint32,
|
||||
gltype uint32,
|
||||
id uint32,
|
||||
severity uint32,
|
||||
length int32,
|
||||
message *uint8,
|
||||
userParam unsafe.Pointer) {
|
||||
if userDebugCallback != nil {
|
||||
userDebugCallback(source, gltype, id, severity, length, GoStr(message), userParam)
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,65 @@
|
|||
// This file implements GlowGetProcAddress for every supported platform. The
|
||||
// correct version is chosen automatically based on build tags:
|
||||
// windows: WGL
|
||||
// darwin: CGL
|
||||
// linux freebsd: GLX
|
||||
// Use of EGL instead of the platform's default (listed above) is made possible
|
||||
// via the "egl" build tag.
|
||||
// It is also possible to install your own function outside this package for
|
||||
// retrieving OpenGL function pointers, to do this see InitWithProcAddrFunc.
|
||||
package gl
|
||||
|
||||
/*
|
||||
#cgo windows CFLAGS: -DTAG_WINDOWS
|
||||
#cgo windows LDFLAGS: -lopengl32
|
||||
#cgo darwin CFLAGS: -DTAG_DARWIN
|
||||
#cgo darwin LDFLAGS: -framework OpenGL
|
||||
#cgo linux freebsd CFLAGS: -DTAG_POSIX
|
||||
#cgo linux freebsd LDFLAGS: -lGL
|
||||
#cgo egl CFLAGS: -DTAG_EGL
|
||||
#cgo egl LDFLAGS: -lEGL
|
||||
// Check the EGL tag first as it takes priority over the platform's default
|
||||
// configuration of WGL/GLX/CGL.
|
||||
#if defined(TAG_EGL)
|
||||
#include <stdlib.h>
|
||||
#include <EGL/egl.h>
|
||||
void* GlowGetProcAddress_glcore33(const char* name) {
|
||||
return eglGetProcAddress(name);
|
||||
}
|
||||
#elif defined(TAG_WINDOWS)
|
||||
#define WIN32_LEAN_AND_MEAN 1
|
||||
#include <windows.h>
|
||||
#include <stdlib.h>
|
||||
static HMODULE ogl32dll = NULL;
|
||||
void* GlowGetProcAddress_glcore33(const char* name) {
|
||||
void* pf = wglGetProcAddress((LPCSTR) name);
|
||||
if (pf) {
|
||||
return pf;
|
||||
}
|
||||
if (ogl32dll == NULL) {
|
||||
ogl32dll = LoadLibraryA("opengl32.dll");
|
||||
}
|
||||
return GetProcAddress(ogl32dll, (LPCSTR) name);
|
||||
}
|
||||
#elif defined(TAG_DARWIN)
|
||||
#include <stdlib.h>
|
||||
#include <dlfcn.h>
|
||||
void* GlowGetProcAddress_glcore33(const char* name) {
|
||||
return dlsym(RTLD_DEFAULT, name);
|
||||
}
|
||||
#elif defined(TAG_POSIX)
|
||||
#include <stdlib.h>
|
||||
#include <GL/glx.h>
|
||||
void* GlowGetProcAddress_glcore33(const char* name) {
|
||||
return glXGetProcAddress((const GLubyte *) name);
|
||||
}
|
||||
#endif
|
||||
*/
|
||||
import "C"
|
||||
import "unsafe"
|
||||
|
||||
func getProcAddress(namea string) unsafe.Pointer {
|
||||
cname := C.CString(namea)
|
||||
defer C.free(unsafe.Pointer(cname))
|
||||
return C.GlowGetProcAddress_glcore33(cname)
|
||||
}
|
|
@ -0,0 +1,109 @@
|
|||
// Glow automatically generated OpenGL binding: http://github.com/go-gl/glow
|
||||
|
||||
package gl
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// #include <stdlib.h>
|
||||
import "C"
|
||||
|
||||
// Ptr takes a slice or pointer (to a singular scalar value or the first
|
||||
// element of an array or slice) and returns its GL-compatible address.
|
||||
//
|
||||
// For example:
|
||||
//
|
||||
// var data []uint8
|
||||
// ...
|
||||
// gl.TexImage2D(gl.TEXTURE_2D, ..., gl.UNSIGNED_BYTE, gl.Ptr(&data[0]))
|
||||
func Ptr(data interface{}) unsafe.Pointer {
|
||||
if data == nil {
|
||||
return unsafe.Pointer(nil)
|
||||
}
|
||||
var addr unsafe.Pointer
|
||||
v := reflect.ValueOf(data)
|
||||
switch v.Type().Kind() {
|
||||
case reflect.Ptr:
|
||||
e := v.Elem()
|
||||
switch e.Kind() {
|
||||
case
|
||||
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
|
||||
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
|
||||
reflect.Float32, reflect.Float64:
|
||||
addr = unsafe.Pointer(e.UnsafeAddr())
|
||||
default:
|
||||
panic(fmt.Errorf("unsupported pointer to type %s; must be a slice or pointer to a singular scalar value or the first element of an array or slice", e.Kind()))
|
||||
}
|
||||
case reflect.Uintptr:
|
||||
addr = unsafe.Pointer(v.Pointer())
|
||||
case reflect.Slice:
|
||||
addr = unsafe.Pointer(v.Index(0).UnsafeAddr())
|
||||
default:
|
||||
panic(fmt.Errorf("unsupported type %s; must be a slice or pointer to a singular scalar value or the first element of an array or slice", v.Type()))
|
||||
}
|
||||
return addr
|
||||
}
|
||||
|
||||
// PtrOffset takes a pointer offset and returns a GL-compatible pointer.
|
||||
// Useful for functions such as glVertexAttribPointer that take pointer
|
||||
// parameters indicating an offset rather than an absolute memory address.
|
||||
func PtrOffset(offset int) unsafe.Pointer {
|
||||
return unsafe.Pointer(uintptr(offset))
|
||||
}
|
||||
|
||||
// Str takes a null-terminated Go string and returns its GL-compatible address.
|
||||
// This function reaches into Go string storage in an unsafe way so the caller
|
||||
// must ensure the string is not garbage collected.
|
||||
func Str(str string) *uint8 {
|
||||
if !strings.HasSuffix(str, "\x00") {
|
||||
panic("str argument missing null terminator: " + str)
|
||||
}
|
||||
header := (*reflect.StringHeader)(unsafe.Pointer(&str))
|
||||
return (*uint8)(unsafe.Pointer(header.Data))
|
||||
}
|
||||
|
||||
// GoStr takes a null-terminated string returned by OpenGL and constructs a
|
||||
// corresponding Go string.
|
||||
func GoStr(cstr *uint8) string {
|
||||
return C.GoString((*C.char)(unsafe.Pointer(cstr)))
|
||||
}
|
||||
|
||||
// Strs takes a list of Go strings (with or without null-termination) and
|
||||
// returns their C counterpart.
|
||||
//
|
||||
// The returned free function must be called once you are done using the strings
|
||||
// in order to free the memory.
|
||||
//
|
||||
// If no strings are provided as a parameter this function will panic.
|
||||
func Strs(strs ...string) (cstrs **uint8, free func()) {
|
||||
if len(strs) == 0 {
|
||||
panic("Strs: expected at least 1 string")
|
||||
}
|
||||
|
||||
// Allocate a contiguous array large enough to hold all the strings' contents.
|
||||
n := 0
|
||||
for i := range strs {
|
||||
n += len(strs[i])
|
||||
}
|
||||
data := C.malloc(C.size_t(n))
|
||||
|
||||
// Copy all the strings into data.
|
||||
dataSlice := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
|
||||
Data: uintptr(data),
|
||||
Len: n,
|
||||
Cap: n,
|
||||
}))
|
||||
css := make([]*uint8, len(strs)) // Populated with pointers to each string.
|
||||
offset := 0
|
||||
for i := range strs {
|
||||
copy(dataSlice[offset:offset+len(strs[i])], strs[i][:]) // Copy strs[i] into proper data location.
|
||||
css[i] = (*uint8)(unsafe.Pointer(&dataSlice[offset])) // Set a pointer to it.
|
||||
offset += len(strs[i])
|
||||
}
|
||||
|
||||
return (**uint8)(&css[0]), func() { C.free(data) }
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
// Glow automatically generated OpenGL binding: http://github.com/go-gl/glow
|
||||
|
||||
package gl
|
||||
|
||||
import "C"
|
||||
import "unsafe"
|
||||
|
||||
type DebugProc func(
|
||||
source uint32,
|
||||
gltype uint32,
|
||||
id uint32,
|
||||
severity uint32,
|
||||
length int32,
|
||||
message string,
|
||||
userParam unsafe.Pointer)
|
||||
|
||||
var userDebugCallback DebugProc
|
||||
|
||||
//export glowDebugCallback_glcompatibility41
|
||||
func glowDebugCallback_glcompatibility41(
|
||||
source uint32,
|
||||
gltype uint32,
|
||||
id uint32,
|
||||
severity uint32,
|
||||
length int32,
|
||||
message *uint8,
|
||||
userParam unsafe.Pointer) {
|
||||
if userDebugCallback != nil {
|
||||
userDebugCallback(source, gltype, id, severity, length, GoStr(message), userParam)
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,65 @@
|
|||
// This file implements GlowGetProcAddress for every supported platform. The
|
||||
// correct version is chosen automatically based on build tags:
|
||||
// windows: WGL
|
||||
// darwin: CGL
|
||||
// linux freebsd: GLX
|
||||
// Use of EGL instead of the platform's default (listed above) is made possible
|
||||
// via the "egl" build tag.
|
||||
// It is also possible to install your own function outside this package for
|
||||
// retrieving OpenGL function pointers, to do this see InitWithProcAddrFunc.
|
||||
package gl
|
||||
|
||||
/*
|
||||
#cgo windows CFLAGS: -DTAG_WINDOWS
|
||||
#cgo windows LDFLAGS: -lopengl32
|
||||
#cgo darwin CFLAGS: -DTAG_DARWIN
|
||||
#cgo darwin LDFLAGS: -framework OpenGL
|
||||
#cgo linux freebsd CFLAGS: -DTAG_POSIX
|
||||
#cgo linux freebsd LDFLAGS: -lGL
|
||||
#cgo egl CFLAGS: -DTAG_EGL
|
||||
#cgo egl LDFLAGS: -lEGL
|
||||
// Check the EGL tag first as it takes priority over the platform's default
|
||||
// configuration of WGL/GLX/CGL.
|
||||
#if defined(TAG_EGL)
|
||||
#include <stdlib.h>
|
||||
#include <EGL/egl.h>
|
||||
void* GlowGetProcAddress_glcompatibility41(const char* name) {
|
||||
return eglGetProcAddress(name);
|
||||
}
|
||||
#elif defined(TAG_WINDOWS)
|
||||
#define WIN32_LEAN_AND_MEAN 1
|
||||
#include <windows.h>
|
||||
#include <stdlib.h>
|
||||
static HMODULE ogl32dll = NULL;
|
||||
void* GlowGetProcAddress_glcompatibility41(const char* name) {
|
||||
void* pf = wglGetProcAddress((LPCSTR) name);
|
||||
if (pf) {
|
||||
return pf;
|
||||
}
|
||||
if (ogl32dll == NULL) {
|
||||
ogl32dll = LoadLibraryA("opengl32.dll");
|
||||
}
|
||||
return GetProcAddress(ogl32dll, (LPCSTR) name);
|
||||
}
|
||||
#elif defined(TAG_DARWIN)
|
||||
#include <stdlib.h>
|
||||
#include <dlfcn.h>
|
||||
void* GlowGetProcAddress_glcompatibility41(const char* name) {
|
||||
return dlsym(RTLD_DEFAULT, name);
|
||||
}
|
||||
#elif defined(TAG_POSIX)
|
||||
#include <stdlib.h>
|
||||
#include <GL/glx.h>
|
||||
void* GlowGetProcAddress_glcompatibility41(const char* name) {
|
||||
return glXGetProcAddress((const GLubyte *) name);
|
||||
}
|
||||
#endif
|
||||
*/
|
||||
import "C"
|
||||
import "unsafe"
|
||||
|
||||
func getProcAddress(namea string) unsafe.Pointer {
|
||||
cname := C.CString(namea)
|
||||
defer C.free(unsafe.Pointer(cname))
|
||||
return C.GlowGetProcAddress_glcompatibility41(cname)
|
||||
}
|
|
@ -0,0 +1,109 @@
|
|||
// Glow automatically generated OpenGL binding: http://github.com/go-gl/glow
|
||||
|
||||
package gl
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// #include <stdlib.h>
|
||||
import "C"
|
||||
|
||||
// Ptr takes a slice or pointer (to a singular scalar value or the first
|
||||
// element of an array or slice) and returns its GL-compatible address.
|
||||
//
|
||||
// For example:
|
||||
//
|
||||
// var data []uint8
|
||||
// ...
|
||||
// gl.TexImage2D(gl.TEXTURE_2D, ..., gl.UNSIGNED_BYTE, gl.Ptr(&data[0]))
|
||||
func Ptr(data interface{}) unsafe.Pointer {
|
||||
if data == nil {
|
||||
return unsafe.Pointer(nil)
|
||||
}
|
||||
var addr unsafe.Pointer
|
||||
v := reflect.ValueOf(data)
|
||||
switch v.Type().Kind() {
|
||||
case reflect.Ptr:
|
||||
e := v.Elem()
|
||||
switch e.Kind() {
|
||||
case
|
||||
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
|
||||
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
|
||||
reflect.Float32, reflect.Float64:
|
||||
addr = unsafe.Pointer(e.UnsafeAddr())
|
||||
default:
|
||||
panic(fmt.Errorf("unsupported pointer to type %s; must be a slice or pointer to a singular scalar value or the first element of an array or slice", e.Kind()))
|
||||
}
|
||||
case reflect.Uintptr:
|
||||
addr = unsafe.Pointer(v.Pointer())
|
||||
case reflect.Slice:
|
||||
addr = unsafe.Pointer(v.Index(0).UnsafeAddr())
|
||||
default:
|
||||
panic(fmt.Errorf("unsupported type %s; must be a slice or pointer to a singular scalar value or the first element of an array or slice", v.Type()))
|
||||
}
|
||||
return addr
|
||||
}
|
||||
|
||||
// PtrOffset takes a pointer offset and returns a GL-compatible pointer.
|
||||
// Useful for functions such as glVertexAttribPointer that take pointer
|
||||
// parameters indicating an offset rather than an absolute memory address.
|
||||
func PtrOffset(offset int) unsafe.Pointer {
|
||||
return unsafe.Pointer(uintptr(offset))
|
||||
}
|
||||
|
||||
// Str takes a null-terminated Go string and returns its GL-compatible address.
|
||||
// This function reaches into Go string storage in an unsafe way so the caller
|
||||
// must ensure the string is not garbage collected.
|
||||
func Str(str string) *uint8 {
|
||||
if !strings.HasSuffix(str, "\x00") {
|
||||
panic("str argument missing null terminator: " + str)
|
||||
}
|
||||
header := (*reflect.StringHeader)(unsafe.Pointer(&str))
|
||||
return (*uint8)(unsafe.Pointer(header.Data))
|
||||
}
|
||||
|
||||
// GoStr takes a null-terminated string returned by OpenGL and constructs a
|
||||
// corresponding Go string.
|
||||
func GoStr(cstr *uint8) string {
|
||||
return C.GoString((*C.char)(unsafe.Pointer(cstr)))
|
||||
}
|
||||
|
||||
// Strs takes a list of Go strings (with or without null-termination) and
|
||||
// returns their C counterpart.
|
||||
//
|
||||
// The returned free function must be called once you are done using the strings
|
||||
// in order to free the memory.
|
||||
//
|
||||
// If no strings are provided as a parameter this function will panic.
|
||||
func Strs(strs ...string) (cstrs **uint8, free func()) {
|
||||
if len(strs) == 0 {
|
||||
panic("Strs: expected at least 1 string")
|
||||
}
|
||||
|
||||
// Allocate a contiguous array large enough to hold all the strings' contents.
|
||||
n := 0
|
||||
for i := range strs {
|
||||
n += len(strs[i])
|
||||
}
|
||||
data := C.malloc(C.size_t(n))
|
||||
|
||||
// Copy all the strings into data.
|
||||
dataSlice := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
|
||||
Data: uintptr(data),
|
||||
Len: n,
|
||||
Cap: n,
|
||||
}))
|
||||
css := make([]*uint8, len(strs)) // Populated with pointers to each string.
|
||||
offset := 0
|
||||
for i := range strs {
|
||||
copy(dataSlice[offset:offset+len(strs[i])], strs[i][:]) // Copy strs[i] into proper data location.
|
||||
css[i] = (*uint8)(unsafe.Pointer(&dataSlice[offset])) // Set a pointer to it.
|
||||
offset += len(strs[i])
|
||||
}
|
||||
|
||||
return (**uint8)(&css[0]), func() { C.free(data) }
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
// Glow automatically generated OpenGL binding: http://github.com/go-gl/glow
|
||||
|
||||
package gl
|
||||
|
||||
import "C"
|
||||
import "unsafe"
|
||||
|
||||
type DebugProc func(
|
||||
source uint32,
|
||||
gltype uint32,
|
||||
id uint32,
|
||||
severity uint32,
|
||||
length int32,
|
||||
message string,
|
||||
userParam unsafe.Pointer)
|
||||
|
||||
var userDebugCallback DebugProc
|
||||
|
||||
//export glowDebugCallback_glcore41
|
||||
func glowDebugCallback_glcore41(
|
||||
source uint32,
|
||||
gltype uint32,
|
||||
id uint32,
|
||||
severity uint32,
|
||||
length int32,
|
||||
message *uint8,
|
||||
userParam unsafe.Pointer) {
|
||||
if userDebugCallback != nil {
|
||||
userDebugCallback(source, gltype, id, severity, length, GoStr(message), userParam)
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,65 @@
|
|||
// This file implements GlowGetProcAddress for every supported platform. The
|
||||
// correct version is chosen automatically based on build tags:
|
||||
// windows: WGL
|
||||
// darwin: CGL
|
||||
// linux freebsd: GLX
|
||||
// Use of EGL instead of the platform's default (listed above) is made possible
|
||||
// via the "egl" build tag.
|
||||
// It is also possible to install your own function outside this package for
|
||||
// retrieving OpenGL function pointers, to do this see InitWithProcAddrFunc.
|
||||
package gl
|
||||
|
||||
/*
|
||||
#cgo windows CFLAGS: -DTAG_WINDOWS
|
||||
#cgo windows LDFLAGS: -lopengl32
|
||||
#cgo darwin CFLAGS: -DTAG_DARWIN
|
||||
#cgo darwin LDFLAGS: -framework OpenGL
|
||||
#cgo linux freebsd CFLAGS: -DTAG_POSIX
|
||||
#cgo linux freebsd LDFLAGS: -lGL
|
||||
#cgo egl CFLAGS: -DTAG_EGL
|
||||
#cgo egl LDFLAGS: -lEGL
|
||||
// Check the EGL tag first as it takes priority over the platform's default
|
||||
// configuration of WGL/GLX/CGL.
|
||||
#if defined(TAG_EGL)
|
||||
#include <stdlib.h>
|
||||
#include <EGL/egl.h>
|
||||
void* GlowGetProcAddress_glcore41(const char* name) {
|
||||
return eglGetProcAddress(name);
|
||||
}
|
||||
#elif defined(TAG_WINDOWS)
|
||||
#define WIN32_LEAN_AND_MEAN 1
|
||||
#include <windows.h>
|
||||
#include <stdlib.h>
|
||||
static HMODULE ogl32dll = NULL;
|
||||
void* GlowGetProcAddress_glcore41(const char* name) {
|
||||
void* pf = wglGetProcAddress((LPCSTR) name);
|
||||
if (pf) {
|
||||
return pf;
|
||||
}
|
||||
if (ogl32dll == NULL) {
|
||||
ogl32dll = LoadLibraryA("opengl32.dll");
|
||||
}
|
||||
return GetProcAddress(ogl32dll, (LPCSTR) name);
|
||||
}
|
||||
#elif defined(TAG_DARWIN)
|
||||
#include <stdlib.h>
|
||||
#include <dlfcn.h>
|
||||
void* GlowGetProcAddress_glcore41(const char* name) {
|
||||
return dlsym(RTLD_DEFAULT, name);
|
||||
}
|
||||
#elif defined(TAG_POSIX)
|
||||
#include <stdlib.h>
|
||||
#include <GL/glx.h>
|
||||
void* GlowGetProcAddress_glcore41(const char* name) {
|
||||
return glXGetProcAddress((const GLubyte *) name);
|
||||
}
|
||||
#endif
|
||||
*/
|
||||
import "C"
|
||||
import "unsafe"
|
||||
|
||||
func getProcAddress(namea string) unsafe.Pointer {
|
||||
cname := C.CString(namea)
|
||||
defer C.free(unsafe.Pointer(cname))
|
||||
return C.GlowGetProcAddress_glcore41(cname)
|
||||
}
|
|
@ -0,0 +1,109 @@
|
|||
// Glow automatically generated OpenGL binding: http://github.com/go-gl/glow
|
||||
|
||||
package gl
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// #include <stdlib.h>
|
||||
import "C"
|
||||
|
||||
// Ptr takes a slice or pointer (to a singular scalar value or the first
|
||||
// element of an array or slice) and returns its GL-compatible address.
|
||||
//
|
||||
// For example:
|
||||
//
|
||||
// var data []uint8
|
||||
// ...
|
||||
// gl.TexImage2D(gl.TEXTURE_2D, ..., gl.UNSIGNED_BYTE, gl.Ptr(&data[0]))
|
||||
func Ptr(data interface{}) unsafe.Pointer {
|
||||
if data == nil {
|
||||
return unsafe.Pointer(nil)
|
||||
}
|
||||
var addr unsafe.Pointer
|
||||
v := reflect.ValueOf(data)
|
||||
switch v.Type().Kind() {
|
||||
case reflect.Ptr:
|
||||
e := v.Elem()
|
||||
switch e.Kind() {
|
||||
case
|
||||
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
|
||||
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
|
||||
reflect.Float32, reflect.Float64:
|
||||
addr = unsafe.Pointer(e.UnsafeAddr())
|
||||
default:
|
||||
panic(fmt.Errorf("unsupported pointer to type %s; must be a slice or pointer to a singular scalar value or the first element of an array or slice", e.Kind()))
|
||||
}
|
||||
case reflect.Uintptr:
|
||||
addr = unsafe.Pointer(v.Pointer())
|
||||
case reflect.Slice:
|
||||
addr = unsafe.Pointer(v.Index(0).UnsafeAddr())
|
||||
default:
|
||||
panic(fmt.Errorf("unsupported type %s; must be a slice or pointer to a singular scalar value or the first element of an array or slice", v.Type()))
|
||||
}
|
||||
return addr
|
||||
}
|
||||
|
||||
// PtrOffset takes a pointer offset and returns a GL-compatible pointer.
|
||||
// Useful for functions such as glVertexAttribPointer that take pointer
|
||||
// parameters indicating an offset rather than an absolute memory address.
|
||||
func PtrOffset(offset int) unsafe.Pointer {
|
||||
return unsafe.Pointer(uintptr(offset))
|
||||
}
|
||||
|
||||
// Str takes a null-terminated Go string and returns its GL-compatible address.
|
||||
// This function reaches into Go string storage in an unsafe way so the caller
|
||||
// must ensure the string is not garbage collected.
|
||||
func Str(str string) *uint8 {
|
||||
if !strings.HasSuffix(str, "\x00") {
|
||||
panic("str argument missing null terminator: " + str)
|
||||
}
|
||||
header := (*reflect.StringHeader)(unsafe.Pointer(&str))
|
||||
return (*uint8)(unsafe.Pointer(header.Data))
|
||||
}
|
||||
|
||||
// GoStr takes a null-terminated string returned by OpenGL and constructs a
|
||||
// corresponding Go string.
|
||||
func GoStr(cstr *uint8) string {
|
||||
return C.GoString((*C.char)(unsafe.Pointer(cstr)))
|
||||
}
|
||||
|
||||
// Strs takes a list of Go strings (with or without null-termination) and
|
||||
// returns their C counterpart.
|
||||
//
|
||||
// The returned free function must be called once you are done using the strings
|
||||
// in order to free the memory.
|
||||
//
|
||||
// If no strings are provided as a parameter this function will panic.
|
||||
func Strs(strs ...string) (cstrs **uint8, free func()) {
|
||||
if len(strs) == 0 {
|
||||
panic("Strs: expected at least 1 string")
|
||||
}
|
||||
|
||||
// Allocate a contiguous array large enough to hold all the strings' contents.
|
||||
n := 0
|
||||
for i := range strs {
|
||||
n += len(strs[i])
|
||||
}
|
||||
data := C.malloc(C.size_t(n))
|
||||
|
||||
// Copy all the strings into data.
|
||||
dataSlice := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
|
||||
Data: uintptr(data),
|
||||
Len: n,
|
||||
Cap: n,
|
||||
}))
|
||||
css := make([]*uint8, len(strs)) // Populated with pointers to each string.
|
||||
offset := 0
|
||||
for i := range strs {
|
||||
copy(dataSlice[offset:offset+len(strs[i])], strs[i][:]) // Copy strs[i] into proper data location.
|
||||
css[i] = (*uint8)(unsafe.Pointer(&dataSlice[offset])) // Set a pointer to it.
|
||||
offset += len(strs[i])
|
||||
}
|
||||
|
||||
return (**uint8)(&css[0]), func() { C.free(data) }
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
// Glow automatically generated OpenGL binding: http://github.com/go-gl/glow
|
||||
|
||||
package gl
|
||||
|
||||
import "C"
|
||||
import "unsafe"
|
||||
|
||||
type DebugProc func(
|
||||
source uint32,
|
||||
gltype uint32,
|
||||
id uint32,
|
||||
severity uint32,
|
||||
length int32,
|
||||
message string,
|
||||
userParam unsafe.Pointer)
|
||||
|
||||
var userDebugCallback DebugProc
|
||||
|
||||
//export glowDebugCallback_glcompatibility42
|
||||
func glowDebugCallback_glcompatibility42(
|
||||
source uint32,
|
||||
gltype uint32,
|
||||
id uint32,
|
||||
severity uint32,
|
||||
length int32,
|
||||
message *uint8,
|
||||
userParam unsafe.Pointer) {
|
||||
if userDebugCallback != nil {
|
||||
userDebugCallback(source, gltype, id, severity, length, GoStr(message), userParam)
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,65 @@
|
|||
// This file implements GlowGetProcAddress for every supported platform. The
|
||||
// correct version is chosen automatically based on build tags:
|
||||
// windows: WGL
|
||||
// darwin: CGL
|
||||
// linux freebsd: GLX
|
||||
// Use of EGL instead of the platform's default (listed above) is made possible
|
||||
// via the "egl" build tag.
|
||||
// It is also possible to install your own function outside this package for
|
||||
// retrieving OpenGL function pointers, to do this see InitWithProcAddrFunc.
|
||||
package gl
|
||||
|
||||
/*
|
||||
#cgo windows CFLAGS: -DTAG_WINDOWS
|
||||
#cgo windows LDFLAGS: -lopengl32
|
||||
#cgo darwin CFLAGS: -DTAG_DARWIN
|
||||
#cgo darwin LDFLAGS: -framework OpenGL
|
||||
#cgo linux freebsd CFLAGS: -DTAG_POSIX
|
||||
#cgo linux freebsd LDFLAGS: -lGL
|
||||
#cgo egl CFLAGS: -DTAG_EGL
|
||||
#cgo egl LDFLAGS: -lEGL
|
||||
// Check the EGL tag first as it takes priority over the platform's default
|
||||
// configuration of WGL/GLX/CGL.
|
||||
#if defined(TAG_EGL)
|
||||
#include <stdlib.h>
|
||||
#include <EGL/egl.h>
|
||||
void* GlowGetProcAddress_glcompatibility42(const char* name) {
|
||||
return eglGetProcAddress(name);
|
||||
}
|
||||
#elif defined(TAG_WINDOWS)
|
||||
#define WIN32_LEAN_AND_MEAN 1
|
||||
#include <windows.h>
|
||||
#include <stdlib.h>
|
||||
static HMODULE ogl32dll = NULL;
|
||||
void* GlowGetProcAddress_glcompatibility42(const char* name) {
|
||||
void* pf = wglGetProcAddress((LPCSTR) name);
|
||||
if (pf) {
|
||||
return pf;
|
||||
}
|
||||
if (ogl32dll == NULL) {
|
||||
ogl32dll = LoadLibraryA("opengl32.dll");
|
||||
}
|
||||
return GetProcAddress(ogl32dll, (LPCSTR) name);
|
||||
}
|
||||
#elif defined(TAG_DARWIN)
|
||||
#include <stdlib.h>
|
||||
#include <dlfcn.h>
|
||||
void* GlowGetProcAddress_glcompatibility42(const char* name) {
|
||||
return dlsym(RTLD_DEFAULT, name);
|
||||
}
|
||||
#elif defined(TAG_POSIX)
|
||||
#include <stdlib.h>
|
||||
#include <GL/glx.h>
|
||||
void* GlowGetProcAddress_glcompatibility42(const char* name) {
|
||||
return glXGetProcAddress((const GLubyte *) name);
|
||||
}
|
||||
#endif
|
||||
*/
|
||||
import "C"
|
||||
import "unsafe"
|
||||
|
||||
func getProcAddress(namea string) unsafe.Pointer {
|
||||
cname := C.CString(namea)
|
||||
defer C.free(unsafe.Pointer(cname))
|
||||
return C.GlowGetProcAddress_glcompatibility42(cname)
|
||||
}
|
|
@ -0,0 +1,109 @@
|
|||
// Glow automatically generated OpenGL binding: http://github.com/go-gl/glow
|
||||
|
||||
package gl
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// #include <stdlib.h>
|
||||
import "C"
|
||||
|
||||
// Ptr takes a slice or pointer (to a singular scalar value or the first
|
||||
// element of an array or slice) and returns its GL-compatible address.
|
||||
//
|
||||
// For example:
|
||||
//
|
||||
// var data []uint8
|
||||
// ...
|
||||
// gl.TexImage2D(gl.TEXTURE_2D, ..., gl.UNSIGNED_BYTE, gl.Ptr(&data[0]))
|
||||
func Ptr(data interface{}) unsafe.Pointer {
|
||||
if data == nil {
|
||||
return unsafe.Pointer(nil)
|
||||
}
|
||||
var addr unsafe.Pointer
|
||||
v := reflect.ValueOf(data)
|
||||
switch v.Type().Kind() {
|
||||
case reflect.Ptr:
|
||||
e := v.Elem()
|
||||
switch e.Kind() {
|
||||
case
|
||||
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
|
||||
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
|
||||
reflect.Float32, reflect.Float64:
|
||||
addr = unsafe.Pointer(e.UnsafeAddr())
|
||||
default:
|
||||
panic(fmt.Errorf("unsupported pointer to type %s; must be a slice or pointer to a singular scalar value or the first element of an array or slice", e.Kind()))
|
||||
}
|
||||
case reflect.Uintptr:
|
||||
addr = unsafe.Pointer(v.Pointer())
|
||||
case reflect.Slice:
|
||||
addr = unsafe.Pointer(v.Index(0).UnsafeAddr())
|
||||
default:
|
||||
panic(fmt.Errorf("unsupported type %s; must be a slice or pointer to a singular scalar value or the first element of an array or slice", v.Type()))
|
||||
}
|
||||
return addr
|
||||
}
|
||||
|
||||
// PtrOffset takes a pointer offset and returns a GL-compatible pointer.
|
||||
// Useful for functions such as glVertexAttribPointer that take pointer
|
||||
// parameters indicating an offset rather than an absolute memory address.
|
||||
func PtrOffset(offset int) unsafe.Pointer {
|
||||
return unsafe.Pointer(uintptr(offset))
|
||||
}
|
||||
|
||||
// Str takes a null-terminated Go string and returns its GL-compatible address.
|
||||
// This function reaches into Go string storage in an unsafe way so the caller
|
||||
// must ensure the string is not garbage collected.
|
||||
func Str(str string) *uint8 {
|
||||
if !strings.HasSuffix(str, "\x00") {
|
||||
panic("str argument missing null terminator: " + str)
|
||||
}
|
||||
header := (*reflect.StringHeader)(unsafe.Pointer(&str))
|
||||
return (*uint8)(unsafe.Pointer(header.Data))
|
||||
}
|
||||
|
||||
// GoStr takes a null-terminated string returned by OpenGL and constructs a
|
||||
// corresponding Go string.
|
||||
func GoStr(cstr *uint8) string {
|
||||
return C.GoString((*C.char)(unsafe.Pointer(cstr)))
|
||||
}
|
||||
|
||||
// Strs takes a list of Go strings (with or without null-termination) and
|
||||
// returns their C counterpart.
|
||||
//
|
||||
// The returned free function must be called once you are done using the strings
|
||||
// in order to free the memory.
|
||||
//
|
||||
// If no strings are provided as a parameter this function will panic.
|
||||
func Strs(strs ...string) (cstrs **uint8, free func()) {
|
||||
if len(strs) == 0 {
|
||||
panic("Strs: expected at least 1 string")
|
||||
}
|
||||
|
||||
// Allocate a contiguous array large enough to hold all the strings' contents.
|
||||
n := 0
|
||||
for i := range strs {
|
||||
n += len(strs[i])
|
||||
}
|
||||
data := C.malloc(C.size_t(n))
|
||||
|
||||
// Copy all the strings into data.
|
||||
dataSlice := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
|
||||
Data: uintptr(data),
|
||||
Len: n,
|
||||
Cap: n,
|
||||
}))
|
||||
css := make([]*uint8, len(strs)) // Populated with pointers to each string.
|
||||
offset := 0
|
||||
for i := range strs {
|
||||
copy(dataSlice[offset:offset+len(strs[i])], strs[i][:]) // Copy strs[i] into proper data location.
|
||||
css[i] = (*uint8)(unsafe.Pointer(&dataSlice[offset])) // Set a pointer to it.
|
||||
offset += len(strs[i])
|
||||
}
|
||||
|
||||
return (**uint8)(&css[0]), func() { C.free(data) }
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
// Glow automatically generated OpenGL binding: http://github.com/go-gl/glow
|
||||
|
||||
package gl
|
||||
|
||||
import "C"
|
||||
import "unsafe"
|
||||
|
||||
type DebugProc func(
|
||||
source uint32,
|
||||
gltype uint32,
|
||||
id uint32,
|
||||
severity uint32,
|
||||
length int32,
|
||||
message string,
|
||||
userParam unsafe.Pointer)
|
||||
|
||||
var userDebugCallback DebugProc
|
||||
|
||||
//export glowDebugCallback_glcore42
|
||||
func glowDebugCallback_glcore42(
|
||||
source uint32,
|
||||
gltype uint32,
|
||||
id uint32,
|
||||
severity uint32,
|
||||
length int32,
|
||||
message *uint8,
|
||||
userParam unsafe.Pointer) {
|
||||
if userDebugCallback != nil {
|
||||
userDebugCallback(source, gltype, id, severity, length, GoStr(message), userParam)
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,65 @@
|
|||
// This file implements GlowGetProcAddress for every supported platform. The
|
||||
// correct version is chosen automatically based on build tags:
|
||||
// windows: WGL
|
||||
// darwin: CGL
|
||||
// linux freebsd: GLX
|
||||
// Use of EGL instead of the platform's default (listed above) is made possible
|
||||
// via the "egl" build tag.
|
||||
// It is also possible to install your own function outside this package for
|
||||
// retrieving OpenGL function pointers, to do this see InitWithProcAddrFunc.
|
||||
package gl
|
||||
|
||||
/*
|
||||
#cgo windows CFLAGS: -DTAG_WINDOWS
|
||||
#cgo windows LDFLAGS: -lopengl32
|
||||
#cgo darwin CFLAGS: -DTAG_DARWIN
|
||||
#cgo darwin LDFLAGS: -framework OpenGL
|
||||
#cgo linux freebsd CFLAGS: -DTAG_POSIX
|
||||
#cgo linux freebsd LDFLAGS: -lGL
|
||||
#cgo egl CFLAGS: -DTAG_EGL
|
||||
#cgo egl LDFLAGS: -lEGL
|
||||
// Check the EGL tag first as it takes priority over the platform's default
|
||||
// configuration of WGL/GLX/CGL.
|
||||
#if defined(TAG_EGL)
|
||||
#include <stdlib.h>
|
||||
#include <EGL/egl.h>
|
||||
void* GlowGetProcAddress_glcore42(const char* name) {
|
||||
return eglGetProcAddress(name);
|
||||
}
|
||||
#elif defined(TAG_WINDOWS)
|
||||
#define WIN32_LEAN_AND_MEAN 1
|
||||
#include <windows.h>
|
||||
#include <stdlib.h>
|
||||
static HMODULE ogl32dll = NULL;
|
||||
void* GlowGetProcAddress_glcore42(const char* name) {
|
||||
void* pf = wglGetProcAddress((LPCSTR) name);
|
||||
if (pf) {
|
||||
return pf;
|
||||
}
|
||||
if (ogl32dll == NULL) {
|
||||
ogl32dll = LoadLibraryA("opengl32.dll");
|
||||
}
|
||||
return GetProcAddress(ogl32dll, (LPCSTR) name);
|
||||
}
|
||||
#elif defined(TAG_DARWIN)
|
||||
#include <stdlib.h>
|
||||
#include <dlfcn.h>
|
||||
void* GlowGetProcAddress_glcore42(const char* name) {
|
||||
return dlsym(RTLD_DEFAULT, name);
|
||||
}
|
||||
#elif defined(TAG_POSIX)
|
||||
#include <stdlib.h>
|
||||
#include <GL/glx.h>
|
||||
void* GlowGetProcAddress_glcore42(const char* name) {
|
||||
return glXGetProcAddress((const GLubyte *) name);
|
||||
}
|
||||
#endif
|
||||
*/
|
||||
import "C"
|
||||
import "unsafe"
|
||||
|
||||
func getProcAddress(namea string) unsafe.Pointer {
|
||||
cname := C.CString(namea)
|
||||
defer C.free(unsafe.Pointer(cname))
|
||||
return C.GlowGetProcAddress_glcore42(cname)
|
||||
}
|
|
@ -0,0 +1,109 @@
|
|||
// Glow automatically generated OpenGL binding: http://github.com/go-gl/glow
|
||||
|
||||
package gl
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// #include <stdlib.h>
|
||||
import "C"
|
||||
|
||||
// Ptr takes a slice or pointer (to a singular scalar value or the first
|
||||
// element of an array or slice) and returns its GL-compatible address.
|
||||
//
|
||||
// For example:
|
||||
//
|
||||
// var data []uint8
|
||||
// ...
|
||||
// gl.TexImage2D(gl.TEXTURE_2D, ..., gl.UNSIGNED_BYTE, gl.Ptr(&data[0]))
|
||||
func Ptr(data interface{}) unsafe.Pointer {
|
||||
if data == nil {
|
||||
return unsafe.Pointer(nil)
|
||||
}
|
||||
var addr unsafe.Pointer
|
||||
v := reflect.ValueOf(data)
|
||||
switch v.Type().Kind() {
|
||||
case reflect.Ptr:
|
||||
e := v.Elem()
|
||||
switch e.Kind() {
|
||||
case
|
||||
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
|
||||
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
|
||||
reflect.Float32, reflect.Float64:
|
||||
addr = unsafe.Pointer(e.UnsafeAddr())
|
||||
default:
|
||||
panic(fmt.Errorf("unsupported pointer to type %s; must be a slice or pointer to a singular scalar value or the first element of an array or slice", e.Kind()))
|
||||
}
|
||||
case reflect.Uintptr:
|
||||
addr = unsafe.Pointer(v.Pointer())
|
||||
case reflect.Slice:
|
||||
addr = unsafe.Pointer(v.Index(0).UnsafeAddr())
|
||||
default:
|
||||
panic(fmt.Errorf("unsupported type %s; must be a slice or pointer to a singular scalar value or the first element of an array or slice", v.Type()))
|
||||
}
|
||||
return addr
|
||||
}
|
||||
|
||||
// PtrOffset takes a pointer offset and returns a GL-compatible pointer.
|
||||
// Useful for functions such as glVertexAttribPointer that take pointer
|
||||
// parameters indicating an offset rather than an absolute memory address.
|
||||
func PtrOffset(offset int) unsafe.Pointer {
|
||||
return unsafe.Pointer(uintptr(offset))
|
||||
}
|
||||
|
||||
// Str takes a null-terminated Go string and returns its GL-compatible address.
|
||||
// This function reaches into Go string storage in an unsafe way so the caller
|
||||
// must ensure the string is not garbage collected.
|
||||
func Str(str string) *uint8 {
|
||||
if !strings.HasSuffix(str, "\x00") {
|
||||
panic("str argument missing null terminator: " + str)
|
||||
}
|
||||
header := (*reflect.StringHeader)(unsafe.Pointer(&str))
|
||||
return (*uint8)(unsafe.Pointer(header.Data))
|
||||
}
|
||||
|
||||
// GoStr takes a null-terminated string returned by OpenGL and constructs a
|
||||
// corresponding Go string.
|
||||
func GoStr(cstr *uint8) string {
|
||||
return C.GoString((*C.char)(unsafe.Pointer(cstr)))
|
||||
}
|
||||
|
||||
// Strs takes a list of Go strings (with or without null-termination) and
|
||||
// returns their C counterpart.
|
||||
//
|
||||
// The returned free function must be called once you are done using the strings
|
||||
// in order to free the memory.
|
||||
//
|
||||
// If no strings are provided as a parameter this function will panic.
|
||||
func Strs(strs ...string) (cstrs **uint8, free func()) {
|
||||
if len(strs) == 0 {
|
||||
panic("Strs: expected at least 1 string")
|
||||
}
|
||||
|
||||
// Allocate a contiguous array large enough to hold all the strings' contents.
|
||||
n := 0
|
||||
for i := range strs {
|
||||
n += len(strs[i])
|
||||
}
|
||||
data := C.malloc(C.size_t(n))
|
||||
|
||||
// Copy all the strings into data.
|
||||
dataSlice := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
|
||||
Data: uintptr(data),
|
||||
Len: n,
|
||||
Cap: n,
|
||||
}))
|
||||
css := make([]*uint8, len(strs)) // Populated with pointers to each string.
|
||||
offset := 0
|
||||
for i := range strs {
|
||||
copy(dataSlice[offset:offset+len(strs[i])], strs[i][:]) // Copy strs[i] into proper data location.
|
||||
css[i] = (*uint8)(unsafe.Pointer(&dataSlice[offset])) // Set a pointer to it.
|
||||
offset += len(strs[i])
|
||||
}
|
||||
|
||||
return (**uint8)(&css[0]), func() { C.free(data) }
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
// Glow automatically generated OpenGL binding: http://github.com/go-gl/glow
|
||||
|
||||
package gl
|
||||
|
||||
import "C"
|
||||
import "unsafe"
|
||||
|
||||
type DebugProc func(
|
||||
source uint32,
|
||||
gltype uint32,
|
||||
id uint32,
|
||||
severity uint32,
|
||||
length int32,
|
||||
message string,
|
||||
userParam unsafe.Pointer)
|
||||
|
||||
var userDebugCallback DebugProc
|
||||
|
||||
//export glowDebugCallback_glcompatibility43
|
||||
func glowDebugCallback_glcompatibility43(
|
||||
source uint32,
|
||||
gltype uint32,
|
||||
id uint32,
|
||||
severity uint32,
|
||||
length int32,
|
||||
message *uint8,
|
||||
userParam unsafe.Pointer) {
|
||||
if userDebugCallback != nil {
|
||||
userDebugCallback(source, gltype, id, severity, length, GoStr(message), userParam)
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,65 @@
|
|||
// This file implements GlowGetProcAddress for every supported platform. The
|
||||
// correct version is chosen automatically based on build tags:
|
||||
// windows: WGL
|
||||
// darwin: CGL
|
||||
// linux freebsd: GLX
|
||||
// Use of EGL instead of the platform's default (listed above) is made possible
|
||||
// via the "egl" build tag.
|
||||
// It is also possible to install your own function outside this package for
|
||||
// retrieving OpenGL function pointers, to do this see InitWithProcAddrFunc.
|
||||
package gl
|
||||
|
||||
/*
|
||||
#cgo windows CFLAGS: -DTAG_WINDOWS
|
||||
#cgo windows LDFLAGS: -lopengl32
|
||||
#cgo darwin CFLAGS: -DTAG_DARWIN
|
||||
#cgo darwin LDFLAGS: -framework OpenGL
|
||||
#cgo linux freebsd CFLAGS: -DTAG_POSIX
|
||||
#cgo linux freebsd LDFLAGS: -lGL
|
||||
#cgo egl CFLAGS: -DTAG_EGL
|
||||
#cgo egl LDFLAGS: -lEGL
|
||||
// Check the EGL tag first as it takes priority over the platform's default
|
||||
// configuration of WGL/GLX/CGL.
|
||||
#if defined(TAG_EGL)
|
||||
#include <stdlib.h>
|
||||
#include <EGL/egl.h>
|
||||
void* GlowGetProcAddress_glcompatibility43(const char* name) {
|
||||
return eglGetProcAddress(name);
|
||||
}
|
||||
#elif defined(TAG_WINDOWS)
|
||||
#define WIN32_LEAN_AND_MEAN 1
|
||||
#include <windows.h>
|
||||
#include <stdlib.h>
|
||||
static HMODULE ogl32dll = NULL;
|
||||
void* GlowGetProcAddress_glcompatibility43(const char* name) {
|
||||
void* pf = wglGetProcAddress((LPCSTR) name);
|
||||
if (pf) {
|
||||
return pf;
|
||||
}
|
||||
if (ogl32dll == NULL) {
|
||||
ogl32dll = LoadLibraryA("opengl32.dll");
|
||||
}
|
||||
return GetProcAddress(ogl32dll, (LPCSTR) name);
|
||||
}
|
||||
#elif defined(TAG_DARWIN)
|
||||
#include <stdlib.h>
|
||||
#include <dlfcn.h>
|
||||
void* GlowGetProcAddress_glcompatibility43(const char* name) {
|
||||
return dlsym(RTLD_DEFAULT, name);
|
||||
}
|
||||
#elif defined(TAG_POSIX)
|
||||
#include <stdlib.h>
|
||||
#include <GL/glx.h>
|
||||
void* GlowGetProcAddress_glcompatibility43(const char* name) {
|
||||
return glXGetProcAddress((const GLubyte *) name);
|
||||
}
|
||||
#endif
|
||||
*/
|
||||
import "C"
|
||||
import "unsafe"
|
||||
|
||||
func getProcAddress(namea string) unsafe.Pointer {
|
||||
cname := C.CString(namea)
|
||||
defer C.free(unsafe.Pointer(cname))
|
||||
return C.GlowGetProcAddress_glcompatibility43(cname)
|
||||
}
|
|
@ -0,0 +1,109 @@
|
|||
// Glow automatically generated OpenGL binding: http://github.com/go-gl/glow
|
||||
|
||||
package gl
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// #include <stdlib.h>
|
||||
import "C"
|
||||
|
||||
// Ptr takes a slice or pointer (to a singular scalar value or the first
|
||||
// element of an array or slice) and returns its GL-compatible address.
|
||||
//
|
||||
// For example:
|
||||
//
|
||||
// var data []uint8
|
||||
// ...
|
||||
// gl.TexImage2D(gl.TEXTURE_2D, ..., gl.UNSIGNED_BYTE, gl.Ptr(&data[0]))
|
||||
func Ptr(data interface{}) unsafe.Pointer {
|
||||
if data == nil {
|
||||
return unsafe.Pointer(nil)
|
||||
}
|
||||
var addr unsafe.Pointer
|
||||
v := reflect.ValueOf(data)
|
||||
switch v.Type().Kind() {
|
||||
case reflect.Ptr:
|
||||
e := v.Elem()
|
||||
switch e.Kind() {
|
||||
case
|
||||
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
|
||||
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
|
||||
reflect.Float32, reflect.Float64:
|
||||
addr = unsafe.Pointer(e.UnsafeAddr())
|
||||
default:
|
||||
panic(fmt.Errorf("unsupported pointer to type %s; must be a slice or pointer to a singular scalar value or the first element of an array or slice", e.Kind()))
|
||||
}
|
||||
case reflect.Uintptr:
|
||||
addr = unsafe.Pointer(v.Pointer())
|
||||
case reflect.Slice:
|
||||
addr = unsafe.Pointer(v.Index(0).UnsafeAddr())
|
||||
default:
|
||||
panic(fmt.Errorf("unsupported type %s; must be a slice or pointer to a singular scalar value or the first element of an array or slice", v.Type()))
|
||||
}
|
||||
return addr
|
||||
}
|
||||
|
||||
// PtrOffset takes a pointer offset and returns a GL-compatible pointer.
|
||||
// Useful for functions such as glVertexAttribPointer that take pointer
|
||||
// parameters indicating an offset rather than an absolute memory address.
|
||||
func PtrOffset(offset int) unsafe.Pointer {
|
||||
return unsafe.Pointer(uintptr(offset))
|
||||
}
|
||||
|
||||
// Str takes a null-terminated Go string and returns its GL-compatible address.
|
||||
// This function reaches into Go string storage in an unsafe way so the caller
|
||||
// must ensure the string is not garbage collected.
|
||||
func Str(str string) *uint8 {
|
||||
if !strings.HasSuffix(str, "\x00") {
|
||||
panic("str argument missing null terminator: " + str)
|
||||
}
|
||||
header := (*reflect.StringHeader)(unsafe.Pointer(&str))
|
||||
return (*uint8)(unsafe.Pointer(header.Data))
|
||||
}
|
||||
|
||||
// GoStr takes a null-terminated string returned by OpenGL and constructs a
|
||||
// corresponding Go string.
|
||||
func GoStr(cstr *uint8) string {
|
||||
return C.GoString((*C.char)(unsafe.Pointer(cstr)))
|
||||
}
|
||||
|
||||
// Strs takes a list of Go strings (with or without null-termination) and
|
||||
// returns their C counterpart.
|
||||
//
|
||||
// The returned free function must be called once you are done using the strings
|
||||
// in order to free the memory.
|
||||
//
|
||||
// If no strings are provided as a parameter this function will panic.
|
||||
func Strs(strs ...string) (cstrs **uint8, free func()) {
|
||||
if len(strs) == 0 {
|
||||
panic("Strs: expected at least 1 string")
|
||||
}
|
||||
|
||||
// Allocate a contiguous array large enough to hold all the strings' contents.
|
||||
n := 0
|
||||
for i := range strs {
|
||||
n += len(strs[i])
|
||||
}
|
||||
data := C.malloc(C.size_t(n))
|
||||
|
||||
// Copy all the strings into data.
|
||||
dataSlice := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
|
||||
Data: uintptr(data),
|
||||
Len: n,
|
||||
Cap: n,
|
||||
}))
|
||||
css := make([]*uint8, len(strs)) // Populated with pointers to each string.
|
||||
offset := 0
|
||||
for i := range strs {
|
||||
copy(dataSlice[offset:offset+len(strs[i])], strs[i][:]) // Copy strs[i] into proper data location.
|
||||
css[i] = (*uint8)(unsafe.Pointer(&dataSlice[offset])) // Set a pointer to it.
|
||||
offset += len(strs[i])
|
||||
}
|
||||
|
||||
return (**uint8)(&css[0]), func() { C.free(data) }
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
// Glow automatically generated OpenGL binding: http://github.com/go-gl/glow
|
||||
|
||||
package gl
|
||||
|
||||
import "C"
|
||||
import "unsafe"
|
||||
|
||||
type DebugProc func(
|
||||
source uint32,
|
||||
gltype uint32,
|
||||
id uint32,
|
||||
severity uint32,
|
||||
length int32,
|
||||
message string,
|
||||
userParam unsafe.Pointer)
|
||||
|
||||
var userDebugCallback DebugProc
|
||||
|
||||
//export glowDebugCallback_glcore43
|
||||
func glowDebugCallback_glcore43(
|
||||
source uint32,
|
||||
gltype uint32,
|
||||
id uint32,
|
||||
severity uint32,
|
||||
length int32,
|
||||
message *uint8,
|
||||
userParam unsafe.Pointer) {
|
||||
if userDebugCallback != nil {
|
||||
userDebugCallback(source, gltype, id, severity, length, GoStr(message), userParam)
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,65 @@
|
|||
// This file implements GlowGetProcAddress for every supported platform. The
|
||||
// correct version is chosen automatically based on build tags:
|
||||
// windows: WGL
|
||||
// darwin: CGL
|
||||
// linux freebsd: GLX
|
||||
// Use of EGL instead of the platform's default (listed above) is made possible
|
||||
// via the "egl" build tag.
|
||||
// It is also possible to install your own function outside this package for
|
||||
// retrieving OpenGL function pointers, to do this see InitWithProcAddrFunc.
|
||||
package gl
|
||||
|
||||
/*
|
||||
#cgo windows CFLAGS: -DTAG_WINDOWS
|
||||
#cgo windows LDFLAGS: -lopengl32
|
||||
#cgo darwin CFLAGS: -DTAG_DARWIN
|
||||
#cgo darwin LDFLAGS: -framework OpenGL
|
||||
#cgo linux freebsd CFLAGS: -DTAG_POSIX
|
||||
#cgo linux freebsd LDFLAGS: -lGL
|
||||
#cgo egl CFLAGS: -DTAG_EGL
|
||||
#cgo egl LDFLAGS: -lEGL
|
||||
// Check the EGL tag first as it takes priority over the platform's default
|
||||
// configuration of WGL/GLX/CGL.
|
||||
#if defined(TAG_EGL)
|
||||
#include <stdlib.h>
|
||||
#include <EGL/egl.h>
|
||||
void* GlowGetProcAddress_glcore43(const char* name) {
|
||||
return eglGetProcAddress(name);
|
||||
}
|
||||
#elif defined(TAG_WINDOWS)
|
||||
#define WIN32_LEAN_AND_MEAN 1
|
||||
#include <windows.h>
|
||||
#include <stdlib.h>
|
||||
static HMODULE ogl32dll = NULL;
|
||||
void* GlowGetProcAddress_glcore43(const char* name) {
|
||||
void* pf = wglGetProcAddress((LPCSTR) name);
|
||||
if (pf) {
|
||||
return pf;
|
||||
}
|
||||
if (ogl32dll == NULL) {
|
||||
ogl32dll = LoadLibraryA("opengl32.dll");
|
||||
}
|
||||
return GetProcAddress(ogl32dll, (LPCSTR) name);
|
||||
}
|
||||
#elif defined(TAG_DARWIN)
|
||||
#include <stdlib.h>
|
||||
#include <dlfcn.h>
|
||||
void* GlowGetProcAddress_glcore43(const char* name) {
|
||||
return dlsym(RTLD_DEFAULT, name);
|
||||
}
|
||||
#elif defined(TAG_POSIX)
|
||||
#include <stdlib.h>
|
||||
#include <GL/glx.h>
|
||||
void* GlowGetProcAddress_glcore43(const char* name) {
|
||||
return glXGetProcAddress((const GLubyte *) name);
|
||||
}
|
||||
#endif
|
||||
*/
|
||||
import "C"
|
||||
import "unsafe"
|
||||
|
||||
func getProcAddress(namea string) unsafe.Pointer {
|
||||
cname := C.CString(namea)
|
||||
defer C.free(unsafe.Pointer(cname))
|
||||
return C.GlowGetProcAddress_glcore43(cname)
|
||||
}
|
|
@ -0,0 +1,109 @@
|
|||
// Glow automatically generated OpenGL binding: http://github.com/go-gl/glow
|
||||
|
||||
package gl
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// #include <stdlib.h>
|
||||
import "C"
|
||||
|
||||
// Ptr takes a slice or pointer (to a singular scalar value or the first
|
||||
// element of an array or slice) and returns its GL-compatible address.
|
||||
//
|
||||
// For example:
|
||||
//
|
||||
// var data []uint8
|
||||
// ...
|
||||
// gl.TexImage2D(gl.TEXTURE_2D, ..., gl.UNSIGNED_BYTE, gl.Ptr(&data[0]))
|
||||
func Ptr(data interface{}) unsafe.Pointer {
|
||||
if data == nil {
|
||||
return unsafe.Pointer(nil)
|
||||
}
|
||||
var addr unsafe.Pointer
|
||||
v := reflect.ValueOf(data)
|
||||
switch v.Type().Kind() {
|
||||
case reflect.Ptr:
|
||||
e := v.Elem()
|
||||
switch e.Kind() {
|
||||
case
|
||||
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
|
||||
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
|
||||
reflect.Float32, reflect.Float64:
|
||||
addr = unsafe.Pointer(e.UnsafeAddr())
|
||||
default:
|
||||
panic(fmt.Errorf("unsupported pointer to type %s; must be a slice or pointer to a singular scalar value or the first element of an array or slice", e.Kind()))
|
||||
}
|
||||
case reflect.Uintptr:
|
||||
addr = unsafe.Pointer(v.Pointer())
|
||||
case reflect.Slice:
|
||||
addr = unsafe.Pointer(v.Index(0).UnsafeAddr())
|
||||
default:
|
||||
panic(fmt.Errorf("unsupported type %s; must be a slice or pointer to a singular scalar value or the first element of an array or slice", v.Type()))
|
||||
}
|
||||
return addr
|
||||
}
|
||||
|
||||
// PtrOffset takes a pointer offset and returns a GL-compatible pointer.
|
||||
// Useful for functions such as glVertexAttribPointer that take pointer
|
||||
// parameters indicating an offset rather than an absolute memory address.
|
||||
func PtrOffset(offset int) unsafe.Pointer {
|
||||
return unsafe.Pointer(uintptr(offset))
|
||||
}
|
||||
|
||||
// Str takes a null-terminated Go string and returns its GL-compatible address.
|
||||
// This function reaches into Go string storage in an unsafe way so the caller
|
||||
// must ensure the string is not garbage collected.
|
||||
func Str(str string) *uint8 {
|
||||
if !strings.HasSuffix(str, "\x00") {
|
||||
panic("str argument missing null terminator: " + str)
|
||||
}
|
||||
header := (*reflect.StringHeader)(unsafe.Pointer(&str))
|
||||
return (*uint8)(unsafe.Pointer(header.Data))
|
||||
}
|
||||
|
||||
// GoStr takes a null-terminated string returned by OpenGL and constructs a
|
||||
// corresponding Go string.
|
||||
func GoStr(cstr *uint8) string {
|
||||
return C.GoString((*C.char)(unsafe.Pointer(cstr)))
|
||||
}
|
||||
|
||||
// Strs takes a list of Go strings (with or without null-termination) and
|
||||
// returns their C counterpart.
|
||||
//
|
||||
// The returned free function must be called once you are done using the strings
|
||||
// in order to free the memory.
|
||||
//
|
||||
// If no strings are provided as a parameter this function will panic.
|
||||
func Strs(strs ...string) (cstrs **uint8, free func()) {
|
||||
if len(strs) == 0 {
|
||||
panic("Strs: expected at least 1 string")
|
||||
}
|
||||
|
||||
// Allocate a contiguous array large enough to hold all the strings' contents.
|
||||
n := 0
|
||||
for i := range strs {
|
||||
n += len(strs[i])
|
||||
}
|
||||
data := C.malloc(C.size_t(n))
|
||||
|
||||
// Copy all the strings into data.
|
||||
dataSlice := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
|
||||
Data: uintptr(data),
|
||||
Len: n,
|
||||
Cap: n,
|
||||
}))
|
||||
css := make([]*uint8, len(strs)) // Populated with pointers to each string.
|
||||
offset := 0
|
||||
for i := range strs {
|
||||
copy(dataSlice[offset:offset+len(strs[i])], strs[i][:]) // Copy strs[i] into proper data location.
|
||||
css[i] = (*uint8)(unsafe.Pointer(&dataSlice[offset])) // Set a pointer to it.
|
||||
offset += len(strs[i])
|
||||
}
|
||||
|
||||
return (**uint8)(&css[0]), func() { C.free(data) }
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
// Glow automatically generated OpenGL binding: http://github.com/go-gl/glow
|
||||
|
||||
package gl
|
||||
|
||||
import "C"
|
||||
import "unsafe"
|
||||
|
||||
type DebugProc func(
|
||||
source uint32,
|
||||
gltype uint32,
|
||||
id uint32,
|
||||
severity uint32,
|
||||
length int32,
|
||||
message string,
|
||||
userParam unsafe.Pointer)
|
||||
|
||||
var userDebugCallback DebugProc
|
||||
|
||||
//export glowDebugCallback_glcompatibility44
|
||||
func glowDebugCallback_glcompatibility44(
|
||||
source uint32,
|
||||
gltype uint32,
|
||||
id uint32,
|
||||
severity uint32,
|
||||
length int32,
|
||||
message *uint8,
|
||||
userParam unsafe.Pointer) {
|
||||
if userDebugCallback != nil {
|
||||
userDebugCallback(source, gltype, id, severity, length, GoStr(message), userParam)
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,65 @@
|
|||
// This file implements GlowGetProcAddress for every supported platform. The
|
||||
// correct version is chosen automatically based on build tags:
|
||||
// windows: WGL
|
||||
// darwin: CGL
|
||||
// linux freebsd: GLX
|
||||
// Use of EGL instead of the platform's default (listed above) is made possible
|
||||
// via the "egl" build tag.
|
||||
// It is also possible to install your own function outside this package for
|
||||
// retrieving OpenGL function pointers, to do this see InitWithProcAddrFunc.
|
||||
package gl
|
||||
|
||||
/*
|
||||
#cgo windows CFLAGS: -DTAG_WINDOWS
|
||||
#cgo windows LDFLAGS: -lopengl32
|
||||
#cgo darwin CFLAGS: -DTAG_DARWIN
|
||||
#cgo darwin LDFLAGS: -framework OpenGL
|
||||
#cgo linux freebsd CFLAGS: -DTAG_POSIX
|
||||
#cgo linux freebsd LDFLAGS: -lGL
|
||||
#cgo egl CFLAGS: -DTAG_EGL
|
||||
#cgo egl LDFLAGS: -lEGL
|
||||
// Check the EGL tag first as it takes priority over the platform's default
|
||||
// configuration of WGL/GLX/CGL.
|
||||
#if defined(TAG_EGL)
|
||||
#include <stdlib.h>
|
||||
#include <EGL/egl.h>
|
||||
void* GlowGetProcAddress_glcompatibility44(const char* name) {
|
||||
return eglGetProcAddress(name);
|
||||
}
|
||||
#elif defined(TAG_WINDOWS)
|
||||
#define WIN32_LEAN_AND_MEAN 1
|
||||
#include <windows.h>
|
||||
#include <stdlib.h>
|
||||
static HMODULE ogl32dll = NULL;
|
||||
void* GlowGetProcAddress_glcompatibility44(const char* name) {
|
||||
void* pf = wglGetProcAddress((LPCSTR) name);
|
||||
if (pf) {
|
||||
return pf;
|
||||
}
|
||||
if (ogl32dll == NULL) {
|
||||
ogl32dll = LoadLibraryA("opengl32.dll");
|
||||
}
|
||||
return GetProcAddress(ogl32dll, (LPCSTR) name);
|
||||
}
|
||||
#elif defined(TAG_DARWIN)
|
||||
#include <stdlib.h>
|
||||
#include <dlfcn.h>
|
||||
void* GlowGetProcAddress_glcompatibility44(const char* name) {
|
||||
return dlsym(RTLD_DEFAULT, name);
|
||||
}
|
||||
#elif defined(TAG_POSIX)
|
||||
#include <stdlib.h>
|
||||
#include <GL/glx.h>
|
||||
void* GlowGetProcAddress_glcompatibility44(const char* name) {
|
||||
return glXGetProcAddress((const GLubyte *) name);
|
||||
}
|
||||
#endif
|
||||
*/
|
||||
import "C"
|
||||
import "unsafe"
|
||||
|
||||
func getProcAddress(namea string) unsafe.Pointer {
|
||||
cname := C.CString(namea)
|
||||
defer C.free(unsafe.Pointer(cname))
|
||||
return C.GlowGetProcAddress_glcompatibility44(cname)
|
||||
}
|
|
@ -0,0 +1,109 @@
|
|||
// Glow automatically generated OpenGL binding: http://github.com/go-gl/glow
|
||||
|
||||
package gl
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// #include <stdlib.h>
|
||||
import "C"
|
||||
|
||||
// Ptr takes a slice or pointer (to a singular scalar value or the first
|
||||
// element of an array or slice) and returns its GL-compatible address.
|
||||
//
|
||||
// For example:
|
||||
//
|
||||
// var data []uint8
|
||||
// ...
|
||||
// gl.TexImage2D(gl.TEXTURE_2D, ..., gl.UNSIGNED_BYTE, gl.Ptr(&data[0]))
|
||||
func Ptr(data interface{}) unsafe.Pointer {
|
||||
if data == nil {
|
||||
return unsafe.Pointer(nil)
|
||||
}
|
||||
var addr unsafe.Pointer
|
||||
v := reflect.ValueOf(data)
|
||||
switch v.Type().Kind() {
|
||||
case reflect.Ptr:
|
||||
e := v.Elem()
|
||||
switch e.Kind() {
|
||||
case
|
||||
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
|
||||
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
|
||||
reflect.Float32, reflect.Float64:
|
||||
addr = unsafe.Pointer(e.UnsafeAddr())
|
||||
default:
|
||||
panic(fmt.Errorf("unsupported pointer to type %s; must be a slice or pointer to a singular scalar value or the first element of an array or slice", e.Kind()))
|
||||
}
|
||||
case reflect.Uintptr:
|
||||
addr = unsafe.Pointer(v.Pointer())
|
||||
case reflect.Slice:
|
||||
addr = unsafe.Pointer(v.Index(0).UnsafeAddr())
|
||||
default:
|
||||
panic(fmt.Errorf("unsupported type %s; must be a slice or pointer to a singular scalar value or the first element of an array or slice", v.Type()))
|
||||
}
|
||||
return addr
|
||||
}
|
||||
|
||||
// PtrOffset takes a pointer offset and returns a GL-compatible pointer.
|
||||
// Useful for functions such as glVertexAttribPointer that take pointer
|
||||
// parameters indicating an offset rather than an absolute memory address.
|
||||
func PtrOffset(offset int) unsafe.Pointer {
|
||||
return unsafe.Pointer(uintptr(offset))
|
||||
}
|
||||
|
||||
// Str takes a null-terminated Go string and returns its GL-compatible address.
|
||||
// This function reaches into Go string storage in an unsafe way so the caller
|
||||
// must ensure the string is not garbage collected.
|
||||
func Str(str string) *uint8 {
|
||||
if !strings.HasSuffix(str, "\x00") {
|
||||
panic("str argument missing null terminator: " + str)
|
||||
}
|
||||
header := (*reflect.StringHeader)(unsafe.Pointer(&str))
|
||||
return (*uint8)(unsafe.Pointer(header.Data))
|
||||
}
|
||||
|
||||
// GoStr takes a null-terminated string returned by OpenGL and constructs a
|
||||
// corresponding Go string.
|
||||
func GoStr(cstr *uint8) string {
|
||||
return C.GoString((*C.char)(unsafe.Pointer(cstr)))
|
||||
}
|
||||
|
||||
// Strs takes a list of Go strings (with or without null-termination) and
|
||||
// returns their C counterpart.
|
||||
//
|
||||
// The returned free function must be called once you are done using the strings
|
||||
// in order to free the memory.
|
||||
//
|
||||
// If no strings are provided as a parameter this function will panic.
|
||||
func Strs(strs ...string) (cstrs **uint8, free func()) {
|
||||
if len(strs) == 0 {
|
||||
panic("Strs: expected at least 1 string")
|
||||
}
|
||||
|
||||
// Allocate a contiguous array large enough to hold all the strings' contents.
|
||||
n := 0
|
||||
for i := range strs {
|
||||
n += len(strs[i])
|
||||
}
|
||||
data := C.malloc(C.size_t(n))
|
||||
|
||||
// Copy all the strings into data.
|
||||
dataSlice := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
|
||||
Data: uintptr(data),
|
||||
Len: n,
|
||||
Cap: n,
|
||||
}))
|
||||
css := make([]*uint8, len(strs)) // Populated with pointers to each string.
|
||||
offset := 0
|
||||
for i := range strs {
|
||||
copy(dataSlice[offset:offset+len(strs[i])], strs[i][:]) // Copy strs[i] into proper data location.
|
||||
css[i] = (*uint8)(unsafe.Pointer(&dataSlice[offset])) // Set a pointer to it.
|
||||
offset += len(strs[i])
|
||||
}
|
||||
|
||||
return (**uint8)(&css[0]), func() { C.free(data) }
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
// Glow automatically generated OpenGL binding: http://github.com/go-gl/glow
|
||||
|
||||
package gl
|
||||
|
||||
import "C"
|
||||
import "unsafe"
|
||||
|
||||
type DebugProc func(
|
||||
source uint32,
|
||||
gltype uint32,
|
||||
id uint32,
|
||||
severity uint32,
|
||||
length int32,
|
||||
message string,
|
||||
userParam unsafe.Pointer)
|
||||
|
||||
var userDebugCallback DebugProc
|
||||
|
||||
//export glowDebugCallback_glcore44
|
||||
func glowDebugCallback_glcore44(
|
||||
source uint32,
|
||||
gltype uint32,
|
||||
id uint32,
|
||||
severity uint32,
|
||||
length int32,
|
||||
message *uint8,
|
||||
userParam unsafe.Pointer) {
|
||||
if userDebugCallback != nil {
|
||||
userDebugCallback(source, gltype, id, severity, length, GoStr(message), userParam)
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,65 @@
|
|||
// This file implements GlowGetProcAddress for every supported platform. The
|
||||
// correct version is chosen automatically based on build tags:
|
||||
// windows: WGL
|
||||
// darwin: CGL
|
||||
// linux freebsd: GLX
|
||||
// Use of EGL instead of the platform's default (listed above) is made possible
|
||||
// via the "egl" build tag.
|
||||
// It is also possible to install your own function outside this package for
|
||||
// retrieving OpenGL function pointers, to do this see InitWithProcAddrFunc.
|
||||
package gl
|
||||
|
||||
/*
|
||||
#cgo windows CFLAGS: -DTAG_WINDOWS
|
||||
#cgo windows LDFLAGS: -lopengl32
|
||||
#cgo darwin CFLAGS: -DTAG_DARWIN
|
||||
#cgo darwin LDFLAGS: -framework OpenGL
|
||||
#cgo linux freebsd CFLAGS: -DTAG_POSIX
|
||||
#cgo linux freebsd LDFLAGS: -lGL
|
||||
#cgo egl CFLAGS: -DTAG_EGL
|
||||
#cgo egl LDFLAGS: -lEGL
|
||||
// Check the EGL tag first as it takes priority over the platform's default
|
||||
// configuration of WGL/GLX/CGL.
|
||||
#if defined(TAG_EGL)
|
||||
#include <stdlib.h>
|
||||
#include <EGL/egl.h>
|
||||
void* GlowGetProcAddress_glcore44(const char* name) {
|
||||
return eglGetProcAddress(name);
|
||||
}
|
||||
#elif defined(TAG_WINDOWS)
|
||||
#define WIN32_LEAN_AND_MEAN 1
|
||||
#include <windows.h>
|
||||
#include <stdlib.h>
|
||||
static HMODULE ogl32dll = NULL;
|
||||
void* GlowGetProcAddress_glcore44(const char* name) {
|
||||
void* pf = wglGetProcAddress((LPCSTR) name);
|
||||
if (pf) {
|
||||
return pf;
|
||||
}
|
||||
if (ogl32dll == NULL) {
|
||||
ogl32dll = LoadLibraryA("opengl32.dll");
|
||||
}
|
||||
return GetProcAddress(ogl32dll, (LPCSTR) name);
|
||||
}
|
||||
#elif defined(TAG_DARWIN)
|
||||
#include <stdlib.h>
|
||||
#include <dlfcn.h>
|
||||
void* GlowGetProcAddress_glcore44(const char* name) {
|
||||
return dlsym(RTLD_DEFAULT, name);
|
||||
}
|
||||
#elif defined(TAG_POSIX)
|
||||
#include <stdlib.h>
|
||||
#include <GL/glx.h>
|
||||
void* GlowGetProcAddress_glcore44(const char* name) {
|
||||
return glXGetProcAddress((const GLubyte *) name);
|
||||
}
|
||||
#endif
|
||||
*/
|
||||
import "C"
|
||||
import "unsafe"
|
||||
|
||||
func getProcAddress(namea string) unsafe.Pointer {
|
||||
cname := C.CString(namea)
|
||||
defer C.free(unsafe.Pointer(cname))
|
||||
return C.GlowGetProcAddress_glcore44(cname)
|
||||
}
|
|
@ -0,0 +1,109 @@
|
|||
// Glow automatically generated OpenGL binding: http://github.com/go-gl/glow
|
||||
|
||||
package gl
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// #include <stdlib.h>
|
||||
import "C"
|
||||
|
||||
// Ptr takes a slice or pointer (to a singular scalar value or the first
|
||||
// element of an array or slice) and returns its GL-compatible address.
|
||||
//
|
||||
// For example:
|
||||
//
|
||||
// var data []uint8
|
||||
// ...
|
||||
// gl.TexImage2D(gl.TEXTURE_2D, ..., gl.UNSIGNED_BYTE, gl.Ptr(&data[0]))
|
||||
func Ptr(data interface{}) unsafe.Pointer {
|
||||
if data == nil {
|
||||
return unsafe.Pointer(nil)
|
||||
}
|
||||
var addr unsafe.Pointer
|
||||
v := reflect.ValueOf(data)
|
||||
switch v.Type().Kind() {
|
||||
case reflect.Ptr:
|
||||
e := v.Elem()
|
||||
switch e.Kind() {
|
||||
case
|
||||
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
|
||||
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
|
||||
reflect.Float32, reflect.Float64:
|
||||
addr = unsafe.Pointer(e.UnsafeAddr())
|
||||
default:
|
||||
panic(fmt.Errorf("unsupported pointer to type %s; must be a slice or pointer to a singular scalar value or the first element of an array or slice", e.Kind()))
|
||||
}
|
||||
case reflect.Uintptr:
|
||||
addr = unsafe.Pointer(v.Pointer())
|
||||
case reflect.Slice:
|
||||
addr = unsafe.Pointer(v.Index(0).UnsafeAddr())
|
||||
default:
|
||||
panic(fmt.Errorf("unsupported type %s; must be a slice or pointer to a singular scalar value or the first element of an array or slice", v.Type()))
|
||||
}
|
||||
return addr
|
||||
}
|
||||
|
||||
// PtrOffset takes a pointer offset and returns a GL-compatible pointer.
|
||||
// Useful for functions such as glVertexAttribPointer that take pointer
|
||||
// parameters indicating an offset rather than an absolute memory address.
|
||||
func PtrOffset(offset int) unsafe.Pointer {
|
||||
return unsafe.Pointer(uintptr(offset))
|
||||
}
|
||||
|
||||
// Str takes a null-terminated Go string and returns its GL-compatible address.
|
||||
// This function reaches into Go string storage in an unsafe way so the caller
|
||||
// must ensure the string is not garbage collected.
|
||||
func Str(str string) *uint8 {
|
||||
if !strings.HasSuffix(str, "\x00") {
|
||||
panic("str argument missing null terminator: " + str)
|
||||
}
|
||||
header := (*reflect.StringHeader)(unsafe.Pointer(&str))
|
||||
return (*uint8)(unsafe.Pointer(header.Data))
|
||||
}
|
||||
|
||||
// GoStr takes a null-terminated string returned by OpenGL and constructs a
|
||||
// corresponding Go string.
|
||||
func GoStr(cstr *uint8) string {
|
||||
return C.GoString((*C.char)(unsafe.Pointer(cstr)))
|
||||
}
|
||||
|
||||
// Strs takes a list of Go strings (with or without null-termination) and
|
||||
// returns their C counterpart.
|
||||
//
|
||||
// The returned free function must be called once you are done using the strings
|
||||
// in order to free the memory.
|
||||
//
|
||||
// If no strings are provided as a parameter this function will panic.
|
||||
func Strs(strs ...string) (cstrs **uint8, free func()) {
|
||||
if len(strs) == 0 {
|
||||
panic("Strs: expected at least 1 string")
|
||||
}
|
||||
|
||||
// Allocate a contiguous array large enough to hold all the strings' contents.
|
||||
n := 0
|
||||
for i := range strs {
|
||||
n += len(strs[i])
|
||||
}
|
||||
data := C.malloc(C.size_t(n))
|
||||
|
||||
// Copy all the strings into data.
|
||||
dataSlice := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
|
||||
Data: uintptr(data),
|
||||
Len: n,
|
||||
Cap: n,
|
||||
}))
|
||||
css := make([]*uint8, len(strs)) // Populated with pointers to each string.
|
||||
offset := 0
|
||||
for i := range strs {
|
||||
copy(dataSlice[offset:offset+len(strs[i])], strs[i][:]) // Copy strs[i] into proper data location.
|
||||
css[i] = (*uint8)(unsafe.Pointer(&dataSlice[offset])) // Set a pointer to it.
|
||||
offset += len(strs[i])
|
||||
}
|
||||
|
||||
return (**uint8)(&css[0]), func() { C.free(data) }
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
// Glow automatically generated OpenGL binding: http://github.com/go-gl/glow
|
||||
|
||||
package gl
|
||||
|
||||
import "C"
|
||||
import "unsafe"
|
||||
|
||||
type DebugProc func(
|
||||
source uint32,
|
||||
gltype uint32,
|
||||
id uint32,
|
||||
severity uint32,
|
||||
length int32,
|
||||
message string,
|
||||
userParam unsafe.Pointer)
|
||||
|
||||
var userDebugCallback DebugProc
|
||||
|
||||
//export glowDebugCallback_glcompatibility45
|
||||
func glowDebugCallback_glcompatibility45(
|
||||
source uint32,
|
||||
gltype uint32,
|
||||
id uint32,
|
||||
severity uint32,
|
||||
length int32,
|
||||
message *uint8,
|
||||
userParam unsafe.Pointer) {
|
||||
if userDebugCallback != nil {
|
||||
userDebugCallback(source, gltype, id, severity, length, GoStr(message), userParam)
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue