zorldo

Goofing around with Ebiten
git clone git://bsandro.tech/zorldo
Log | Files | Refs | README

glfw.go (4577B)


      1 package glfw
      2 
      3 //#include <stdlib.h>
      4 //#define GLFW_INCLUDE_NONE
      5 //#include "glfw/include/GLFW/glfw3.h"
      6 import "C"
      7 import "unsafe"
      8 
      9 // Version constants.
     10 const (
     11 	VersionMajor    = C.GLFW_VERSION_MAJOR    // This is incremented when the API is changed in non-compatible ways.
     12 	VersionMinor    = C.GLFW_VERSION_MINOR    // This is incremented when features are added to the API but it remains backward-compatible.
     13 	VersionRevision = C.GLFW_VERSION_REVISION // This is incremented when a bug fix release is made that does not contain any API changes.
     14 )
     15 
     16 // Init initializes the GLFW library. Before most GLFW functions can be used,
     17 // GLFW must be initialized, and before a program terminates GLFW should be
     18 // terminated in order to free any resources allocated during or after
     19 // initialization.
     20 //
     21 // If this function fails, it calls Terminate before returning. If it succeeds,
     22 // you should call Terminate before the program exits.
     23 //
     24 // Additional calls to this function after successful initialization but before
     25 // termination will succeed but will do nothing.
     26 //
     27 // This function may take several seconds to complete on some systems, while on
     28 // other systems it may take only a fraction of a second to complete.
     29 //
     30 // On Mac OS X, this function will change the current directory of the
     31 // application to the Contents/Resources subdirectory of the application's
     32 // bundle, if present.
     33 //
     34 // This function may only be called from the main thread.
     35 func Init() error {
     36 	C.glfwInit()
     37 	// invalidValue can happen when specific joysticks are used. This issue
     38 	// will be fixed in GLFW 3.3.5. As a temporary fix, ignore this error.
     39 	// See go-gl/glfw#292, go-gl/glfw#324, and glfw/glfw#1763.
     40 	err := acceptError(APIUnavailable, invalidValue)
     41 	if e, ok := err.(*Error); ok && e.Code == invalidValue {
     42 		return nil
     43 	}
     44 	return err
     45 }
     46 
     47 // Terminate destroys all remaining windows, frees any allocated resources and
     48 // sets the library to an uninitialized state. Once this is called, you must
     49 // again call Init successfully before you will be able to use most GLFW
     50 // functions.
     51 //
     52 // If GLFW has been successfully initialized, this function should be called
     53 // before the program exits. If initialization fails, there is no need to call
     54 // this function, as it is called by Init before it returns failure.
     55 //
     56 // This function may only be called from the main thread.
     57 func Terminate() {
     58 	flushErrors()
     59 	C.glfwTerminate()
     60 }
     61 
     62 // InitHint function sets hints for the next initialization of GLFW.
     63 //
     64 // The values you set hints to are never reset by GLFW, but they only take
     65 // effect during initialization. Once GLFW has been initialized, any values you
     66 // set will be ignored until the library is terminated and initialized again.
     67 //
     68 // Some hints are platform specific. These may be set on any platform but they
     69 // will only affect their specific platform. Other platforms will ignore them.
     70 // Setting these hints requires no platform specific headers or functions.
     71 //
     72 // This function must only be called from the main thread.
     73 func InitHint(hint Hint, value int) {
     74 	C.glfwInitHint(C.int(hint), C.int(value))
     75 }
     76 
     77 // GetVersion retrieves the major, minor and revision numbers of the GLFW
     78 // library. It is intended for when you are using GLFW as a shared library and
     79 // want to ensure that you are using the minimum required version.
     80 //
     81 // This function may be called before Init.
     82 func GetVersion() (major, minor, revision int) {
     83 	var (
     84 		maj C.int
     85 		min C.int
     86 		rev C.int
     87 	)
     88 
     89 	C.glfwGetVersion(&maj, &min, &rev)
     90 	return int(maj), int(min), int(rev)
     91 }
     92 
     93 // GetVersionString returns a static string generated at compile-time according
     94 // to which configuration macros were defined. This is intended for use when
     95 // submitting bug reports, to allow developers to see which code paths are
     96 // enabled in a binary.
     97 //
     98 // This function may be called before Init.
     99 func GetVersionString() string {
    100 	return C.GoString(C.glfwGetVersionString())
    101 }
    102 
    103 // GetClipboardString returns the contents of the system clipboard, if it
    104 // contains or is convertible to a UTF-8 encoded string.
    105 //
    106 // This function may only be called from the main thread.
    107 func GetClipboardString() string {
    108 	cs := C.glfwGetClipboardString(nil)
    109 	if cs == nil {
    110 		acceptError(FormatUnavailable)
    111 		return ""
    112 	}
    113 	return C.GoString(cs)
    114 }
    115 
    116 // SetClipboardString sets the system clipboard to the specified UTF-8 encoded
    117 // string.
    118 //
    119 // This function may only be called from the main thread.
    120 func SetClipboardString(str string) {
    121 	cp := C.CString(str)
    122 	defer C.free(unsafe.Pointer(cp))
    123 	C.glfwSetClipboardString(nil, cp)
    124 	panicError()
    125 }