glfw.go 4.5 KB

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