...
Tawesoft Logo

Source file src/tawesoft.co.uk/go/glcaps/common.go

Documentation: src/tawesoft.co.uk/go/glcaps/common.go

     1  package glcaps
     2  
     3  import (
     4      "sort"
     5  )
     6  
     7  // Extensions is an ordered list of supported OpenGL extensions.
     8  type Extensions []string
     9  
    10  // HaveExtension returns true iff the ordered list of supported OpenGL extensions contains a given extension.
    11  func (extensions Extensions) Contains(key string) bool {
    12      var index = sort.SearchStrings(extensions, key)
    13      return (index < len(extensions)) && (extensions[index] == key)
    14  }
    15  
    16  // Binding implements a binding between this package and a specific OpenGL implementation (e.g. a specific `go-gl`
    17  // module).
    18  type Binding struct {
    19      GetIntegerv func(name uint32, data *int32)
    20      GetFloatv   func(name uint32, data *float32)
    21      GetString   func(name uint32) string // required to return a Go string, not a C string!
    22      GetStringi  func(name uint32, index uint32) string // required to return a Go string, not a C string!
    23  }
    24  
    25  // QueryExtensions returns all extensions supported by the current OpenGL context as a sorted list of strings. It is an
    26  // error to call this method if a current OpenGL context does not exist.
    27  func (b *Binding) QueryExtensions() Extensions {
    28      var numExtensions int32
    29      b.GetIntegerv(glconstants["GL_NUM_EXTENSIONS"], &numExtensions)
    30      if numExtensions <= 0 {
    31          panic("failed to query OpenGL extensions (is the OpenGL context current?)")
    32      }
    33  
    34      var xs = make([]string, 0, numExtensions)
    35      
    36      for i := uint32(0); i < uint32(numExtensions); i++ {
    37          x := b.GetStringi(glconstants["GL_EXTENSIONS"], i)
    38          if len(x) == 0 { continue }
    39          xs = append(xs, x)
    40      }
    41  
    42      sort.Strings(xs)
    43      return xs
    44  }
    45  
    46  // Error implements an error result type for reporting a capability that doesn't meet a requirement.
    47  type Error struct {
    48      Field       string // the name of the field in the struct that failed
    49      Tag         string // the original tag string
    50      Requirement requirement // the requirement that failed
    51      Message     string // a human-readable message
    52  }
    53  
    54  type Errors []Error
    55  
    56  func (es *Errors) append(e ... Error) {
    57      if *es == nil && (len(e) > 0) {
    58          *es = make([]Error, 0)
    59      }
    60      
    61      *es = append(*es, e...)
    62  }
    63  
    64  
    65  
    66  
    67  

View as plain text