glfw.go (4272B)
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 return acceptError(APIUnavailable) 38 } 39 40 // Terminate destroys all remaining windows, frees any allocated resources and 41 // sets the library to an uninitialized state. Once this is called, you must 42 // again call Init successfully before you will be able to use most GLFW 43 // functions. 44 // 45 // If GLFW has been successfully initialized, this function should be called 46 // before the program exits. If initialization fails, there is no need to call 47 // this function, as it is called by Init before it returns failure. 48 // 49 // This function may only be called from the main thread. 50 func Terminate() { 51 flushErrors() 52 C.glfwTerminate() 53 } 54 55 // InitHint function sets hints for the next initialization of GLFW. 56 // 57 // The values you set hints to are never reset by GLFW, but they only take 58 // effect during initialization. Once GLFW has been initialized, any values you 59 // set will be ignored until the library is terminated and initialized again. 60 // 61 // Some hints are platform specific. These may be set on any platform but they 62 // will only affect their specific platform. Other platforms will ignore them. 63 // Setting these hints requires no platform specific headers or functions. 64 // 65 // This function must only be called from the main thread. 66 func InitHint(hint Hint, value int) { 67 C.glfwInitHint(C.int(hint), C.int(value)) 68 } 69 70 // GetVersion retrieves the major, minor and revision numbers of the GLFW 71 // library. It is intended for when you are using GLFW as a shared library and 72 // want to ensure that you are using the minimum required version. 73 // 74 // This function may be called before Init. 75 func GetVersion() (major, minor, revision int) { 76 var ( 77 maj C.int 78 min C.int 79 rev C.int 80 ) 81 82 C.glfwGetVersion(&maj, &min, &rev) 83 return int(maj), int(min), int(rev) 84 } 85 86 // GetVersionString returns a static string generated at compile-time according 87 // to which configuration macros were defined. This is intended for use when 88 // submitting bug reports, to allow developers to see which code paths are 89 // enabled in a binary. 90 // 91 // This function may be called before Init. 92 func GetVersionString() string { 93 return C.GoString(C.glfwGetVersionString()) 94 } 95 96 // GetClipboardString returns the contents of the system clipboard, if it 97 // contains or is convertible to a UTF-8 encoded string. 98 // 99 // This function may only be called from the main thread. 100 func GetClipboardString() string { 101 cs := C.glfwGetClipboardString(nil) 102 if cs == nil { 103 acceptError(FormatUnavailable) 104 return "" 105 } 106 return C.GoString(cs) 107 } 108 109 // SetClipboardString sets the system clipboard to the specified UTF-8 encoded 110 // string. 111 // 112 // This function may only be called from the main thread. 113 func SetClipboardString(str string) { 114 cp := C.CString(str) 115 defer C.free(unsafe.Pointer(cp)) 116 C.glfwSetClipboardString(nil, cp) 117 panicError() 118 }