├── .travis.yml ├── CONTRIBUTORS ├── GLFW_LICENSE ├── LICENSE ├── README.md └── v2.7 └── glfw ├── callback.c ├── callback.go ├── callback.h ├── constants.go ├── glfw.go ├── glfw_darwin.go ├── glfw_test.go ├── image.go └── vidmode.go /.travis.yml: -------------------------------------------------------------------------------- 1 | language: go 2 | 3 | install: 4 | - wget -q https://raw.github.com/go-gl/testutils/master/travis-helper-functions.sh 5 | - source travis-helper-functions.sh 6 | - initialize 7 | - subtest-init go-gl-legacy/glfw go-gl/examples 8 | 9 | script: 10 | - go build -v 11 | 12 | after_script: 13 | - go test -v 14 | - upload-to-imgur 15 | - subtest go-gl-legacy/glfw go-gl/examples 16 | 17 | after_failure: failure 18 | after_error: failure 19 | 20 | notifications: 21 | email: 22 | recipients: 23 | - peter@pwaller.net 24 | - jimteeuwen@gmail.com 25 | on_success: change 26 | on_failure: always 27 | 28 | -------------------------------------------------------------------------------- /CONTRIBUTORS: -------------------------------------------------------------------------------- 1 | # This is the list of people who can contribute (or have contributed) to this 2 | # project. This includes code, documentation, testing, content creation and 3 | # bugfixes. 4 | # 5 | # Names should be added to this file like so: 6 | # Name [] 7 | # 8 | # Please keep the list sorted. 9 | 10 | Andre Moraes 11 | Attila Tajti 12 | Jannis 13 | Jim Teeuwen 14 | TaeZ 15 | -------------------------------------------------------------------------------- /GLFW_LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Copyright © 2002-2009 Camilla Berglund 3 | 4 | This software is provided 'as-is', without any express or implied warranty. In 5 | no event will the authors be held liable for any damages arising from the use of 6 | this software. 7 | 8 | Permission is granted to anyone to use this software for any purpose, including 9 | commercial applications, and to alter it and redistribute it freely, subject to 10 | the following restrictions: 11 | 12 | 1. The origin of this software must not be misrepresented; you must not claim 13 | that you wrote the original software. If you use this software in a product, an 14 | acknowledgment in the product documentation would be appreciated but is not 15 | required. 16 | 17 | 2. Altered source versions must be plainly marked as such, and must not be 18 | misrepresented as being the original software. 19 | 20 | 3. This notice may not be removed or altered from any source distribution. 21 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012 The go-gl Authors. All rights reserved. 2 | 3 | Redistribution and use in source and binary forms, with or without 4 | modification, are permitted provided that the following conditions are 5 | met: 6 | 7 | * Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above 10 | copyright notice, this list of conditions and the following disclaimer 11 | in the documentation and/or other materials provided with the 12 | distribution. 13 | * Neither the name of go-gl nor the names of its contributors may be used 14 | to endorse or promote products derived from this software without specific 15 | prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## GLFW 2 | 3 | This is a set of Go bindings for libGLFW. 4 | 5 | GLFW is a small C library that lets you create and manage an OpenGL context and 6 | its associated window, enumerate and change display modes, as well as handle 7 | inputs such as keyboard, mouse, joystick and time. 8 | 9 | GLFW provides a thin, multi-platform abstraction layer, primarily for 10 | applications whose sole graphics output is through the OpenGL API. While GLFW is 11 | very useful when developing multi-platform OpenGL applications, single-platform 12 | developers can also benefit from avoiding the drudgery of kludgy platform- 13 | specific APIs. 14 | 15 | This wrapper targets GLFW 2.7 16 | 17 | We do not implement the thread/mutex api. 18 | Callback functions are working. 19 | 20 | ### Dependencies 21 | 22 | * [libglfw](http://www.glfw.org/download.html) 23 | 24 | _Important_: libglfw builds/installs itself as a static library by default. 25 | This does not work well with go. Eventhough the building of this package may succeed 26 | without problems, any application you use it in will likely throw up a range 27 | of symbol lookup errors. It is therefor strongly recommended to build libglfw 28 | as a SHARED library instead. See the libglfw README and makefiles on how to 29 | do this. 30 | 31 | 32 | ### Usage 33 | 34 | go get github.com/go-gl-legacy/glfw/v2.7/glfw 35 | 36 | 37 | ### License 38 | 39 | Copyright 2012 The go-gl Authors. All rights reserved. 40 | Use of this source code is governed by a BSD-style 41 | license that can be found in the LICENSE file. 42 | 43 | GLFW is licensed under the zlib/libpng license. 44 | Its contents can be found in the GLFW_LICENSE file. 45 | 46 | -------------------------------------------------------------------------------- /v2.7/glfw/callback.c: -------------------------------------------------------------------------------- 1 | // Copyright 2012 The go-gl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | #include "callback.h" 6 | 7 | void GLFWCALL glfwWindowSizeCB (int width, int height) { 8 | goWindowSizeCB(width, height); 9 | } 10 | 11 | int GLFWCALL glfwWindowCloseCB (void) { 12 | goWindowCloseCB(); 13 | } 14 | 15 | void GLFWCALL glfwWindowRefreshCB (void) { 16 | goWindowRefreshCB(); 17 | } 18 | 19 | void GLFWCALL glfwMouseButtonCB (int button, int action) { 20 | goMouseButtonCB(button, action); 21 | } 22 | 23 | void GLFWCALL glfwMousePosCB (int x, int y) { 24 | goMousePosCB(x, y); 25 | } 26 | 27 | void GLFWCALL glfwMouseWheelCB (int pos) { 28 | goMouseWheelCB(pos); 29 | } 30 | 31 | void GLFWCALL glfwKeyCB (int key, int action) { 32 | goKeyCB(key, action); 33 | } 34 | 35 | void GLFWCALL glfwCharCB (int character, int action) { 36 | goCharCB(character, action); 37 | } 38 | 39 | void glfwSetWindowSizeCB (void) 40 | { 41 | glfwSetWindowSizeCallback(glfwWindowSizeCB); 42 | } 43 | 44 | void glfwSetWindowCloseCB (void) 45 | { 46 | glfwSetWindowCloseCallback(glfwWindowCloseCB); 47 | } 48 | 49 | void glfwSetWindowRefreshCB (void) 50 | { 51 | glfwSetWindowRefreshCallback(glfwWindowRefreshCB); 52 | } 53 | 54 | void glfwSetMouseButtonCB (void) 55 | { 56 | glfwSetMouseButtonCallback(glfwMouseButtonCB); 57 | } 58 | 59 | void glfwSetMousePosCB (void) 60 | { 61 | glfwSetMousePosCallback(glfwMousePosCB); 62 | } 63 | 64 | void glfwSetMouseWheelCB (void) 65 | { 66 | glfwSetMouseWheelCallback(glfwMouseWheelCB); 67 | } 68 | 69 | void glfwSetKeyCB (void) 70 | { 71 | glfwSetKeyCallback(glfwKeyCB); 72 | } 73 | 74 | void glfwSetCharCB (void) 75 | { 76 | glfwSetCharCallback(glfwCharCB); 77 | } 78 | -------------------------------------------------------------------------------- /v2.7/glfw/callback.go: -------------------------------------------------------------------------------- 1 | // Copyright 2012 The go-gl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package glfw 6 | 7 | //#include "callback.h" 8 | import "C" 9 | 10 | // ============================================================================= 11 | 12 | type WindowSizeHandler func(width, height int) 13 | 14 | var windowSize WindowSizeHandler 15 | 16 | //export goWindowSizeCB 17 | func goWindowSizeCB(width, height C.int) { 18 | if windowSize == nil { 19 | return 20 | } 21 | windowSize(int(width), int(height)) 22 | } 23 | 24 | // SetWindowSizeCallback sets the callback for window size change events. 25 | func SetWindowSizeCallback(f WindowSizeHandler) { 26 | windowSize = f 27 | C.glfwSetWindowSizeCB() 28 | } 29 | 30 | // ============================================================================= 31 | 32 | type WindowCloseHandler func() int 33 | 34 | var windowClose WindowCloseHandler 35 | 36 | //export goWindowCloseCB 37 | func goWindowCloseCB() C.int { 38 | if windowClose == nil { 39 | return 0 40 | } 41 | return C.int(windowClose()) 42 | } 43 | 44 | // SetWindowCloseCallback sets the callback for window close events. 45 | // A window has to be opened for this function to have any effect. 46 | func SetWindowCloseCallback(f WindowCloseHandler) { 47 | windowClose = f 48 | C.glfwSetWindowCloseCB() 49 | } 50 | 51 | // ============================================================================= 52 | 53 | type WindowRefreshHandler func() 54 | 55 | var windowRefresh WindowRefreshHandler 56 | 57 | //export goWindowRefreshCB 58 | func goWindowRefreshCB() { 59 | if windowRefresh == nil { 60 | return 61 | } 62 | windowRefresh() 63 | } 64 | 65 | // SetWindowRefreshCallback sets the callback for window refresh events, which 66 | // occurs when any part of the window client area has been damaged, and needs to 67 | // be repainted (for instance, if a part of the window that was previously 68 | // occluded by another window has become visible). 69 | func SetWindowRefreshCallback(f WindowRefreshHandler) { 70 | windowRefresh = f 71 | C.glfwSetWindowRefreshCB() 72 | } 73 | 74 | // ============================================================================= 75 | 76 | type MouseButtonHandler func(button, state int) 77 | 78 | var mouseButton []MouseButtonHandler 79 | 80 | //export goMouseButtonCB 81 | func goMouseButtonCB(button, state C.int) { 82 | for _, f := range mouseButton { 83 | f(int(button), int(state)) 84 | } 85 | } 86 | 87 | // SetMouseButtonCallback sets the callback for mouse button events. 88 | // There can be more than one handler. 89 | func SetMouseButtonCallback(f MouseButtonHandler) { 90 | mouseButton = append(mouseButton, f) 91 | C.glfwSetMouseButtonCB() 92 | } 93 | 94 | // ============================================================================= 95 | 96 | type MousePosHandler func(x, y int) 97 | 98 | var mousePos []MousePosHandler 99 | 100 | //export goMousePosCB 101 | func goMousePosCB(x, y C.int) { 102 | for _, f := range mousePos { 103 | f(int(x), int(y)) 104 | } 105 | } 106 | 107 | // SetMousePosCallback sets a callback for mouse motion events. 108 | // There can be more than one handler. 109 | func SetMousePosCallback(f MousePosHandler) { 110 | mousePos = append(mousePos, f) 111 | C.glfwSetMousePosCB() 112 | } 113 | 114 | // ============================================================================= 115 | 116 | type MouseWheelHandler func(pos int) 117 | 118 | var mouseWheel []MouseWheelHandler 119 | 120 | //export goMouseWheelCB 121 | func goMouseWheelCB(pos C.int) { 122 | for _, f := range mouseWheel { 123 | f(int(pos)) 124 | } 125 | } 126 | 127 | // This function sets the callback for mouse wheel events. 128 | // There can be more than one handler. 129 | func SetMouseWheelCallback(f MouseWheelHandler) { 130 | mouseWheel = append(mouseWheel, f) 131 | C.glfwSetMouseWheelCB() 132 | } 133 | 134 | // ============================================================================= 135 | 136 | type KeyHandler func(key, state int) 137 | 138 | var key []KeyHandler 139 | 140 | //export goKeyCB 141 | func goKeyCB(k, state C.int) { 142 | for _, f := range key { 143 | f(int(k), int(state)) 144 | } 145 | } 146 | 147 | // SetKeyCallback sets the callback for keyboard key events. The callback 148 | // function is called every time the state of a single key is changed (from 149 | // released to pressed or vice versa). The reported keys are unaffected by any 150 | // modifiers (such as shift or alt) and each modifier is reported as a separate key. 151 | // 152 | // There can be more than one handler. 153 | func SetKeyCallback(f KeyHandler) { 154 | key = append(key, f) 155 | C.glfwSetKeyCB() 156 | } 157 | 158 | // ============================================================================= 159 | 160 | type CharHandler func(int, int) 161 | 162 | var char []CharHandler 163 | 164 | //export goCharCB 165 | func goCharCB(x, y C.int) { 166 | for _, f := range char { 167 | f(int(x), int(y)) 168 | } 169 | } 170 | 171 | // SetCharCallback sets the callback for keyboard character events. The callback 172 | // function is called every time a key that results in a printable Unicode 173 | // character is pressed or released. Characters are affected by modifiers 174 | // (such as shift or alt). 175 | func SetCharCallback(f CharHandler) { 176 | char = append(char, f) 177 | C.glfwSetCharCB() 178 | } 179 | -------------------------------------------------------------------------------- /v2.7/glfw/callback.h: -------------------------------------------------------------------------------- 1 | // Copyright 2012 The go-gl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | #ifndef _CALLBACK_H_ 6 | #define _CALLBACK_H_ 7 | 8 | #include 9 | #ifdef _WIN32 10 | #define GLFW_DLL 11 | #endif 12 | #include 13 | 14 | void GLFWCALL glfwWindowSizeCB(int, int); 15 | int GLFWCALL glfwWindowCloseCB(void); 16 | void GLFWCALL glfwWindowRefreshCB(void); 17 | void GLFWCALL glfwMouseButtonCB(int, int); 18 | void GLFWCALL glfwMousePosCB(int, int); 19 | void GLFWCALL glfwMouseWheelCB(int); 20 | void GLFWCALL glfwKeyCB(int, int); 21 | void GLFWCALL glfwCharCB(int, int); 22 | 23 | extern void goWindowSizeCB(int, int); 24 | extern int goWindowCloseCB(void); 25 | extern void goWindowRefreshCB(void); 26 | extern void goMouseButtonCB(int, int); 27 | extern void goMousePosCB(int, int); 28 | extern void goMouseWheelCB(int); 29 | extern void goKeyCB(int, int); 30 | extern void goCharCB(int, int); 31 | 32 | void glfwSetWindowSizeCB(void); 33 | void glfwSetWindowCloseCB(void); 34 | void glfwSetWindowRefreshCB(void); 35 | void glfwSetMouseButtonCB(void); 36 | void glfwSetMousePosCB(void); 37 | void glfwSetMouseWheelCB(void); 38 | void glfwSetKeyCB(void); 39 | void glfwSetCharCB(void); 40 | 41 | #endif // _CALLBACK_H_ 42 | -------------------------------------------------------------------------------- /v2.7/glfw/constants.go: -------------------------------------------------------------------------------- 1 | // Copyright 2012 The go-gl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package glfw 6 | 7 | // GLFW Version 8 | const ( 9 | VersionMajor = 2 10 | VersionMinor = 7 11 | VersionRevision = 0 12 | ) 13 | 14 | // Key and button state/action definitions 15 | const ( 16 | KeyRelease = iota 17 | KeyPress 18 | ) 19 | 20 | // Keyboard key definitions: 8-bit ISO-8859-1 (Latin 1) encoding is used 21 | // for printable keys (such as A-Z, 0-9 etc), and values above 256 22 | // represent Special (non-printable) keys (e.g. F1, Page Up etc). 23 | const ( 24 | KeyUnknown = -1 25 | KeySpace = 32 26 | KeySpecial = 256 27 | ) 28 | 29 | const ( 30 | _ = (KeySpecial + iota) 31 | KeyEsc 32 | KeyF1 33 | KeyF2 34 | KeyF3 35 | KeyF4 36 | KeyF5 37 | KeyF6 38 | KeyF7 39 | KeyF8 40 | KeyF9 41 | KeyF10 42 | KeyF11 43 | KeyF12 44 | KeyF13 45 | KeyF14 46 | KeyF15 47 | KeyF16 48 | KeyF17 49 | KeyF18 50 | KeyF19 51 | KeyF20 52 | KeyF21 53 | KeyF22 54 | KeyF23 55 | KeyF24 56 | KeyF25 57 | KeyUp 58 | KeyDown 59 | KeyLeft 60 | KeyRight 61 | KeyLshift 62 | KeyRshift 63 | KeyLctrl 64 | KeyRctrl 65 | KeyLalt 66 | KeyRalt 67 | KeyTab 68 | KeyEnter 69 | KeyBackspace 70 | KeyInsert 71 | KeyDel 72 | KeyPageup 73 | KeyPagedown 74 | KeyHome 75 | KeyEnd 76 | KeyKP0 77 | KeyKP1 78 | KeyKP2 79 | KeyKP3 80 | KeyKP4 81 | KeyKP5 82 | KeyKP6 83 | KeyKP7 84 | KeyKP8 85 | KeyKP9 86 | KeyKPDivide 87 | KeyKPMultiply 88 | KeyKPSubtract 89 | KeyKPAdd 90 | KeyKPDecimal 91 | KeyKPEqual 92 | KeyKPEnter 93 | KeyKPNumlock 94 | KeyCapslock 95 | KeyScrolllock 96 | KeyPause 97 | KeyLsuper 98 | KeyRsuper 99 | KeyMenu 100 | KeyLast = KeyMenu 101 | ) 102 | 103 | // Mouse button definitions 104 | const ( 105 | Mouse1 = iota 106 | Mouse2 107 | Mouse3 108 | Mouse4 109 | Mouse5 110 | Mouse6 111 | Mouse7 112 | Mouse8 113 | MouseLast = Mouse8 114 | MouseLeft = Mouse1 115 | MouseRight = Mouse2 116 | MouseMiddle = Mouse3 117 | ) 118 | 119 | // Joystick identifiers 120 | const ( 121 | Joy1 = iota 122 | Joy2 123 | Joy3 124 | Joy4 125 | Joy5 126 | Joy6 127 | Joy7 128 | Joy8 129 | Joy9 130 | Joy10 131 | Joy11 132 | Joy12 133 | Joy13 134 | Joy14 135 | Joy15 136 | Joy16 137 | JoyLast = Joy16 138 | ) 139 | 140 | // glfwOpenWindow modes 141 | const ( 142 | _ = 0x00010000 + iota 143 | Windowed 144 | Fullscreen 145 | ) 146 | 147 | // glfwGetWindowParam / glfwOpenWindowHint tokens 148 | const ( 149 | _ = 0x00020000 + iota 150 | Opened 151 | Active 152 | Iconified 153 | Accelerated 154 | RedBits 155 | GreenBits 156 | BlueBits 157 | AlphaBits 158 | DepthBits 159 | StencilBits 160 | 161 | // The following constants are used for both glfwGetWindowParam 162 | // and glfwOpenWindowHint 163 | RefreshRate 164 | AccumRedBits 165 | AccumGreenBits 166 | AccumBlueBits 167 | AccumAlphaBits 168 | AuxBuffers 169 | Stereo 170 | WindowNoResize 171 | FsaaSamples 172 | OpenGLVersionMajor 173 | OpenGLVersionMinor 174 | OpenGLForwardCompat 175 | OpenGLDebugContext 176 | OpenGLProfile 177 | ) 178 | 179 | // GLFW_OPENGL_PROFILE tokens 180 | const ( 181 | _ = 0x00050000 + iota 182 | OpenGLCoreProfile 183 | OpenGLCompatProfile 184 | ) 185 | 186 | // glfwEnable/glfwDisable tokens 187 | const ( 188 | _ = 0x00030000 + iota 189 | MouseCursor 190 | StickyKeys 191 | StickyMouseButtons 192 | SystemKeys 193 | KeyRepeat 194 | AutoPollEvents 195 | ) 196 | 197 | // glfwWaitThread wait modes 198 | const ( 199 | _ = 0x00040000 + iota 200 | Wait 201 | NoWait 202 | ) 203 | 204 | // glfwGetJoystickParam tokens 205 | const ( 206 | _ = 0x00050000 + iota 207 | Present 208 | Axes 209 | Buttons 210 | ) 211 | 212 | // glfwReadImage/glfwLoadTexture2D flags 213 | const ( 214 | NoRescaleBit = 1 << iota // Only for glfwReadImage 215 | OriginUlBit 216 | BuildMipmapsBit // Only for glfwLoadTexture2D 217 | AlphaMapBit 218 | ) 219 | 220 | // Time spans longer than this (seconds) are considered to be infinity 221 | const Infinity = 100000.0 222 | -------------------------------------------------------------------------------- /v2.7/glfw/glfw.go: -------------------------------------------------------------------------------- 1 | // Copyright 2012 The go-gl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package glfw 6 | 7 | //#cgo linux CFLAGS: -I/usr/local/include -pthread 8 | //#cgo linux LDFLAGS: -L/usr/local/lib -pthread -lX11 -lXrandr -lm -lGL -lrt -lglfw 9 | //#cgo darwin CFLAGS: -I/usr/local/include 10 | //#cgo darwin LDFLAGS: -L/usr/local/lib -framework Cocoa -framework OpenGL -framework IOKit -lglfw 11 | //#cgo windows LDFLAGS: -lglu32 -lopengl32 -lglfwdll 12 | //#cgo freebsd CFLAGS: -I/usr/local/include 13 | //#cgo freebsd LDFLAGS: -L/usr/local/lib -lglfw 14 | //#include 15 | //#ifdef _WIN32 16 | // #define GLFW_DLL 17 | //#endif 18 | //#include 19 | import "C" 20 | import ( 21 | "errors" 22 | "unsafe" 23 | ) 24 | 25 | // Init initializes GLFW. No other GLFW functions may be called before this 26 | // function has succeeded. 27 | func Init() (err error) { 28 | if C.glfwInit() != 1 { 29 | err = errors.New("Failed to initialize GLFW") 30 | } 31 | return 32 | } 33 | 34 | // Terminate closes the window, if open, and kills any running threads. This 35 | // function should be called before a program exits 36 | func Terminate() { C.glfwTerminate() } 37 | 38 | // OpenWindowHint sets additional properties for a window that is to be opened. 39 | // For a hint to be registered, the function must be called /before/ calling 40 | // OpenWindow. When the OpenWindow function is called, any hints that 41 | // were registered with the OpenWindowHint function are used for setting the 42 | // corresponding window properties, and then all hints are reset to their default values. 43 | // 44 | // RefreshRate: Vertical monitor refresh rate in Hz (only used 45 | // for fullscreen windows). Zero means system default. 46 | // AccumRedBits: Number of bits for the red channel of the accumulation buffer. 47 | // AccumGreenBits: Number of bits for the green channel of the accumulation buffer. 48 | // AccumBlueBits: Number of bits for the blue channel of the accumulation buffer. 49 | // AccumAlphaBits: Number of bits for the alpha channel of the accumulation buffer. 50 | // AuxBuffers: Number of auxiliary buffers. 51 | // Stereo: Specify if stereo rendering should be supported (can be 52 | // GL_TRUE or GL_FALSE). 53 | // WindowNoResize: Specify whether the window can be resized by the user 54 | // (not used for fullscreen windows) 55 | // FsaaSamples: Number of samples to use for the multisampling buffer. 56 | // Zero disables multisampling. 57 | // 58 | // OpenGLVersionMajor: Major number of the desired minimum OpenGL version. 59 | // OpenGLVersionMinor: Minor number of the desired minimum OpenGL version. 60 | // OpenGLForwardCompat: Specify whether the OpenGL context should be 61 | // forward-compatible (i.e. disallow legacy 62 | // functionality). This should only be used when 63 | // requesting OpenGL version 3.0 or above. 64 | // OpenGLDebugContext: Specify whether a debug context should be created. 65 | // OpenGLProfile: The OpenGL profile the context should implement, or 66 | // zero to let the system choose. Available profiles are 67 | // OpenGLCoreProfile and OpenGLCompatProfile. 68 | func OpenWindowHint(target, hint int) { C.glfwOpenWindowHint(C.int(target), C.int(hint)) } 69 | 70 | // OpenWindow opens a window that best matches the parameters given to the 71 | // function. How well the resulting window matches the desired window depends 72 | // mostly on the available hardware and OpenGL drivers. In general, selecting 73 | // a fullscreen mode has better chances of generating a close match of 74 | // buffers and channel sizes than does a normal desktop window, since GLFW can 75 | // freely select from all the available video modes. A desktop window is 76 | // normally restricted to the video mode of the desktop. 77 | // 78 | // Note: For additional control of window properties, see glfw.OpenWindowHint. 79 | // 80 | // width: The width of the window. If width is zero, it will be calculated 81 | // as width = 4/3 height, if height is not zero. If both width and 82 | // height are zero, width will be set to 640. 83 | // 84 | // height: The height of the window. If height is zero, it will be calculated 85 | // as height = 4/3 width, if width is not zero. If both width and 86 | // height are zero, height will be set to 480. 87 | // 88 | // r, g, b: The number of bits to use for each color component of the color 89 | // buffer (0 means default color depth). For instance, setting 90 | // r=5, g=6 and b=5 will create a 16-bit color buffer, if possible. 91 | // 92 | // a: The number of bits to use for the alpha channel of the color 93 | // buffer (0 means no alpha channel). 94 | // 95 | // depth: The number of bits to use for the depth buffer (0 means no depth buffer). 96 | // 97 | // stencil: The number of bits to use for the stencil buffer (0 means no 98 | // stencil buffer). 99 | // 100 | // mode: Selects which type of OpenGL window to use. Mode must be either 101 | // glfw.Windowed, which will generate a normal desktop window, or 102 | // glfw.Fullscreen, which will generate a window that covers the 103 | // entire screen. When glfw.Fullscreen is selected, the video mode 104 | // will be changed to the resolution that closest matches the width 105 | // and height parameters. 106 | func OpenWindow(width, height, r, g, b, a, depth, stencil, mode int) (err error) { 107 | if C.glfwOpenWindow( 108 | C.int(width), C.int(height), 109 | C.int(r), C.int(g), C.int(b), C.int(a), 110 | C.int(depth), C.int(stencil), C.int(mode), 111 | ) != 1 { 112 | err = errors.New("Failed to open window") 113 | } 114 | return 115 | } 116 | 117 | // CloseWindow closes an opened window and destroys the associated OpenGL context. 118 | func CloseWindow() { C.glfwCloseWindow() } 119 | 120 | // IconifyWindow Iconifies (minimizes) a window. If the window is in fullscreen 121 | // mode, then the desktop video mode will be restored. 122 | func IconifyWindow() { C.glfwIconifyWindow() } 123 | 124 | // RestoreWindow restores an iconified window. If the window that is restored is 125 | // in fullscreen mode, then the fullscreen video mode will be restored. 126 | func RestoreWindow() { C.glfwRestoreWindow() } 127 | 128 | // WindowParam is used for acquiring various properties of an opened window. 129 | // 130 | // Opened: GL_TRUE if window is opened, else GL_FALSE. 131 | // Active: GL_TRUE if window has focus, else GL_FALSE. 132 | // Iconified: GL_TRUE if window is iconified, else GL_FALSE. 133 | // Accelerated: GL_TRUE if window is hardware accelerated, else GL_FALSE. 134 | // RedBits: Number of bits for the red color component. 135 | // GreenBits: Number of bits for the green color component. 136 | // BlueBits: Number of bits for the blue color component. 137 | // AlphaBits: Number of bits for the alpha buffer. 138 | // DepthBits: Number of bits for the depth buffer. 139 | // StencilBits: Number of bits for the stencil buffer. 140 | // RefreshRate: Vertical monitor refresh rate in Hz. Zero indicates an 141 | // unknown or a default refresh rate. 142 | // AccumRedBits: Number of bits for the red channel of the accumulation buffer. 143 | // AccumGreenBits: Number of bits for the green channel of the accumulation buffer. 144 | // AccumBlueBits: Number of bits for the blue channel of the accumulation buffer. 145 | // AccumAlphaBits: Number of bits for the alpha channel of the accumulation buffer. 146 | // AuxBuffers: Number of auxiliary buffers. 147 | // Stereo: GL_TRUE if stereo rendering is supported, else GL_FALSE. 148 | // WindowNoResize: GL_TRUE if the window cannot be resized by the user, else GL_FALSE. 149 | // FsaaSamples: Number of multisampling buffer samples. Zero indicated 150 | // multisampling is disabled. 151 | // 152 | // OpenGLVersionMajor: Major number of the actual version of the context. 153 | // OpenGLVersionMinor: Minor number of the actual version of the context. 154 | // OpenGLForwardCompat: GL_TRUE if the context is forward-compatible, else GL_FALSE. 155 | // OpenGLDebugContext: GL_TRUE if the context is a debug context. 156 | // OpenGLProfile: The profile implemented by the context, or zero. 157 | // 158 | // Note: 'Accelerated' is only supported under Windows. Other systems will always 159 | // return GL_TRUE. Under Windows, Accelerated means that the OpenGL renderer is 160 | // a 3rd party renderer, rather than the fallback Microsoft software OpenGL 161 | // renderer. In other words, it is not a real guarantee that the OpenGL renderer 162 | // is actually hardware accelerated. 163 | func WindowParam(param int) int { return int(C.glfwGetWindowParam(C.int(param))) } 164 | 165 | // WindowSize is used for determining the size of an opened window. The returned 166 | // values are dimensions of the client area of the window (i.e. excluding any 167 | // window borders and decorations). 168 | // 169 | // Note: Even if the size of a fullscreen window does not change once the window 170 | // has been opened, it does not necessarily have to be the same as the size that 171 | // was requested using glfw.OpenWindow. Therefor it is wise to use this function 172 | // to determine the true size of the window once it has been opened. 173 | func WindowSize() (int, int) { 174 | var w, h C.int 175 | C.glfwGetWindowSize(&w, &h) 176 | return int(w), int(h) 177 | } 178 | 179 | // SetWindowSize changes the size of an opened window. The width and height 180 | // parameters denote the size of the client area of the window (i.e. excluding 181 | // any window borders and decorations). If the window is in fullscreen mode, the 182 | // video mode will be changed to a resolution that closest matches the width and 183 | // height parameters (the number of color bits will not be changed). 184 | // 185 | // Note: This function has no effect if the window is iconified. 186 | // 187 | // Note: The OpenGL context is guaranteed to be preserved after calling 188 | // SetWindowSize, even if the video mode is changed. 189 | func SetWindowSize(width, height int) { C.glfwSetWindowSize(C.int(width), C.int(height)) } 190 | 191 | // SetWindowPos changes the position of an opened window. It does not have any 192 | // effect on a fullscreen window. 193 | // 194 | // Note: The behaviour of this function on multi-monitor systems is ill-defined. 195 | func SetWindowPos(x, y int) { C.glfwSetWindowPos(C.int(x), C.int(y)) } 196 | 197 | // SetWindowTitle changes the title of the opened window. 198 | func SetWindowTitle(title string) { C.glfwSetWindowTitle(C.CString(title)) } 199 | 200 | // SetSwapInterval selects the minimum number of monitor vertical retraces that 201 | // should occur between two buffer swaps. If the selected swap interval is one, 202 | // the rate of buffer swaps will never be higher than the vertical refresh rate 203 | // of the monitor. If the selected swap interval is zero, the rate of buffer 204 | // swaps is only limited by the speed of the software and the hardware. 205 | func SetSwapInterval(interval int) { C.glfwSwapInterval(C.int(interval)) } 206 | 207 | // SwapBuffers swaps the back and front color buffers of the window. If 208 | // glfw.AutoPollEvents is enabled (which is the default), glfw.PollEvents() is 209 | // called after swapping the front and back buffers. 210 | func SwapBuffers() { C.glfwSwapBuffers() } 211 | 212 | // PollEvents is used for polling for events, such as user input and window 213 | // resize events. Upon calling this function, all window states, keyboard states 214 | // and mouse states are updated. If any related callback functions are 215 | // registered, these are called during the call to glfw.PollEvents. 216 | // 217 | // Note: glfw.PollEvents is called implicitly from glfw.SwapBuffers if 218 | // glfw.AutoPollEvents is enabled (as it is by default). Thus, if glfw.SwapBuffers 219 | // is called frequently, which is normally the case, there is no need to call 220 | // glfw.PollEvents. 221 | func PollEvents() { C.glfwPollEvents() } 222 | 223 | // WaitEvents is used for waiting for events, such as user input and window 224 | // resize events. Upon calling this function, the calling thread will be put to 225 | // sleep until any event appears in the event queue. When events are available, 226 | // they will be processed just as they are processed by glfw.PollEvents. 227 | // 228 | // Note: It is guaranteed that glfw.WaitEvents will wake up on any event that 229 | // can be processed by glfw.PollEvents. However, GLFW receives many events that 230 | // are only processed internally and the function may behave differently on 231 | // different systems. Do not make any assumptions about when or why 232 | // glfw.WaitEvents will return. 233 | func WaitEvents() { C.glfwWaitEvents() } 234 | 235 | // Key returns glfw.KeyPress if the given key is held down, or glfw.KeyRelease 236 | // otherwise. 237 | // 238 | // Note: Not all key codes are supported on all systems. Also, while some keys 239 | // are available on some keyboard layouts, they may not be available on other 240 | // keyboard layouts. 241 | // 242 | // Note: For systems that do not distinguish between left and right versions of 243 | // modifier keys (shift, alt and control), the left version is used (e.g. glfw.KeyLshift). 244 | func Key(key int) int { return int(C.glfwGetKey(C.int(key))) } 245 | 246 | // MouseButton returns glfw.KeyPress if the given mouse button is held down. 247 | // glfw.KeyRelease otherwise. 248 | func MouseButton(btn int) int { return int(C.glfwGetMouseButton(C.int(btn))) } 249 | 250 | // MousePos returns the current mouse position. If the cursor is not hidden, 251 | // the mouse position is the cursor position, relative to the upper left corner 252 | // of the window and with the Y-axis down. If the cursor is hidden, the mouse 253 | // position is a virtual absolute position, not limited to any boundaries except 254 | // to those implied by the maximum number that can be represented by a signed integer. 255 | func MousePos() (int, int) { 256 | var cx, cy C.int 257 | C.glfwGetMousePos(&cx, &cy) 258 | return int(cx), int(cy) 259 | } 260 | 261 | // SetMousePos changes the position of the mouse. If the cursor is visible 262 | // (not disabled), the cursor will be moved to the specified position, relative 263 | // to the upper left corner of the window client area and with the Y-axis down. 264 | // If the cursor is hidden (disabled), only the mouse position that is reported 265 | // by GLFW is changed. 266 | func SetMousePos(x, y int) { C.glfwSetMousePos(C.int(x), C.int(y)) } 267 | 268 | // MouseWheel returns the current mouse wheel position. The mouse wheel can be 269 | // thought of as a third mouse axis, which is available as a separate wheel or 270 | // up/down stick on some mice. 271 | func MouseWheel() int { return int(C.glfwGetMouseWheel()) } 272 | 273 | // This function changes the position of the mouse wheel. 274 | func SetMouseWheel(pos int) { C.glfwSetMouseWheel(C.int(pos)) } 275 | 276 | // JoystickParam is used for acquiring various properties of a joystick. 277 | func JoystickParam(joy, param int) int { return int(C.glfwGetJoystickParam(C.int(joy), C.int(param))) } 278 | 279 | // JoystickPos queries the current position of one or more axes of a joystick. 280 | // The positional values are returned in an array, where the first element 281 | // represents the first axis of the joystick (normally the X axis). Each 282 | // position is in the range -1.0 to 1.0. Where applicable, the positive 283 | // direction of an axis is right, forward or up, and the negative direction is 284 | // left, back or down. 285 | // 286 | // Note: If len(axes) exceeds the number of axes supported by the joystick, or if 287 | // the joystick is not available, the unused elements in the pos array will be 288 | // set to 0.0 (zero). 289 | // 290 | // Note: The function returns the number of actually returned axes. This is 291 | // the minimum of len(axes) and the number of axes supported by the joystick. 292 | // If the joystick is not supported or connected, the function will return 0 (zero). 293 | func JoystickPos(joy int, axes []float32) int { 294 | if len(axes) == 0 { 295 | return 0 296 | } 297 | 298 | return int(C.glfwGetJoystickPos(C.int(joy), (*C.float)(unsafe.Pointer(&axes[0])), C.int(len(axes)))) 299 | } 300 | 301 | // JoystickButtons queries the current state of one or more buttons of a joystick. 302 | // The button states are returned in an array, where the first element 303 | // represents the first button of the joystick. Each state can be either 304 | // glfw.KeyPress or glfw.KeyRelease. 305 | // 306 | // Note: If len(buttons) exceeds the number of buttons supported by the joystick, 307 | // or if the joystick is not available, the unused elements in the buttons array 308 | // will be set to glfw.KeyRelease. 309 | // 310 | // Note: The function returns the number of actually returned buttons. This is 311 | // the minimum of len(buttons) and the number of buttons supported by the 312 | // joystick. If the joystick is not supported or connected, the function will 313 | // return 0 (zero). 314 | func JoystickButtons(joy int, buttons []byte) int { 315 | if len(buttons) == 0 { 316 | return 0 317 | } 318 | 319 | return int(C.glfwGetJoystickButtons(C.int(joy), 320 | (*C.uchar)(unsafe.Pointer(&buttons[0])), C.int(len(buttons)))) 321 | } 322 | 323 | // Time returns the state of a high precision timer. Unless the timer has been 324 | // set by the glfw.SetTime function, the time is measured as the number of 325 | // seconds that have passed since glfwInit was called. 326 | // 327 | // Note: The resolution of the timer depends on which system the program is running on. 328 | func Time() float64 { return float64(C.glfwGetTime()) } 329 | 330 | // SetTime sets the current time of the high precision timer to the specified 331 | // time. Subsequent calls to glfw.Time will be relative to this time. The time 332 | // is given in seconds. 333 | func SetTime(t float64) { C.glfwSetTime(C.double(t)) } 334 | 335 | // Sleep puts the calling thread to sleep for the requested period of time. 336 | // Only the calling thread is put to sleep. Other threads within the same 337 | // process can still execute. 338 | // 339 | // Note: There is usually a system dependent minimum time for which it is 340 | // possible to sleep. This time is generally in the range 1 ms to 20 ms, 341 | // depending on thread sheduling time slot intervals etc. Using a shorter time 342 | // as a parameter to glfwSleep can give one of two results: either the thread 343 | // will sleep for the minimum possible sleep time, or the thread will not sleep 344 | // at all (glfw.Sleep returns immediately). The latter should only happen when 345 | // very short sleep times are specified, if at all. 346 | func Sleep(t float64) { C.glfwSleep(C.double(t)) } 347 | 348 | // Enable is used to enable a certain feature. 349 | // 350 | // Supported tokens are: MouseCursor, StickyKeys, StickyMouseButtons, 351 | // SystemKeys, KeyRepeat, AutoPollEvents 352 | func Enable(token int) { C.glfwEnable(C.int(token)) } 353 | 354 | // Disable is used to disable a certain feature. 355 | // 356 | // Supported tokens are: MouseCursor, StickyKeys, StickyMouseButtons, 357 | // SystemKeys, KeyRepeat, AutoPollEvents 358 | func Disable(token int) { C.glfwDisable(C.int(token)) } 359 | 360 | // VideoModes returns a list of supported video modes. 361 | // 362 | // The returned list is sorted, first by color depth (RedBits + GreenBits + BlueBits), 363 | // and then by resolution (W idth × Height), with the lowest resolution, fewest 364 | // bits per pixel mode first. 365 | func VideoModes(max int) []*VidMode { 366 | var vm C.GLFWvidmode 367 | 368 | size := int(unsafe.Sizeof(vm)) 369 | ptr := (*C.GLFWvidmode)(C.malloc(C.size_t(size * max))) 370 | defer C.free(unsafe.Pointer(ptr)) 371 | count := C.glfwGetVideoModes(ptr, C.int(max)) 372 | 373 | if count == 0 { 374 | return nil 375 | } 376 | 377 | list := make([]*VidMode, count) 378 | for i := range list { 379 | p := (*C.GLFWvidmode)(unsafe.Pointer(uintptr(unsafe.Pointer(ptr)) + uintptr(i*size))) 380 | list[i] = vidModeFromPtr(p) 381 | } 382 | 383 | return list 384 | } 385 | 386 | // DesktopMode returns the desktop video mode. 387 | // 388 | // Note: The color depth of the desktop display is always reported as the number 389 | // of bits for each individual color component (red, green and blue), even if 390 | // the desktop is not using an RGB or RGBA color format. For instance, an 391 | // indexed 256 color display may report RedBits = 3, GreenBits = 3 and 392 | // BlueBits = 2, which adds up to 8 bits in total. 393 | // 394 | // Note: The desktop video mode is the video mode used by the desktop at the 395 | // time the GLFW window was opened, not the current video mode (which may differ 396 | // from the desktop video mode if the GLFW window is a fullscreen window). 397 | func DesktopMode() *VidMode { 398 | var vm C.GLFWvidmode 399 | C.glfwGetDesktopMode(&vm) 400 | return vidModeFromPtr(&vm) 401 | } 402 | 403 | // ExtensionSupported does a string search in the list of supported OpenGL 404 | // extensions to find if the specified extension is listed. 405 | // 406 | // Note: An OpenGL context must be created before this function can be called. 407 | // 408 | // Note: In addition to checking for OpenGL extensions, GLFW also checks for 409 | // extensions in the operating system “glue API”, such as WGL extensions under 410 | // Microsoft Windows and GLX extensions under the X Window System. 411 | func ExtensionSupported(name string) bool { 412 | cs := C.CString(name) 413 | defer C.free(unsafe.Pointer(cs)) 414 | return C.glfwExtensionSupported(cs) == 1 415 | } 416 | 417 | // ProcAddress acquires the pointer to an OpenGL extension function. Some 418 | // (but not all) OpenGL extensions define new API functions, which are usually 419 | // not available through normal linking. It is therefore necessary to get access 420 | // to those API functions at runtime. 421 | // 422 | // Note: An OpenGL context must be created before this function can be called. 423 | // 424 | // Note: Some systems do not support dynamic function pointer retrieval, in 425 | // which case this function will always return 0 (NULL). 426 | func ProcAddress(name string) uintptr { 427 | cs := C.CString(name) 428 | defer C.free(unsafe.Pointer(cs)) 429 | return uintptr(C.glfwGetProcAddress(cs)) 430 | } 431 | 432 | // GLVersion returns the OpenGL implementation version. This is a convenient 433 | // function that parses the version number information at the beginning of the 434 | // string returned by calling glGetString( GL_VERSION ). The OpenGL version 435 | // information can be used to determine what functionality is supported by the 436 | // used OpenGL implementation. 437 | // 438 | // Note: An OpenGL context must be created before this function can be called. 439 | func GLVersion() (int, int, int) { 440 | var major, minor, rev C.int 441 | C.glfwGetGLVersion(&major, &minor, &rev) 442 | return int(major), int(minor), int(rev) 443 | } 444 | 445 | // NumberOfProcessors determines the number of active processors in the system. 446 | // 447 | // Note: Systems with several logical processors per physical processor, also 448 | // known as SMT (Symmetric Multi-Threading) processors, will report the number 449 | // of logical processors. 450 | func NumberOfProcessors() int { return int(C.glfwGetNumberOfProcessors()) } 451 | -------------------------------------------------------------------------------- /v2.7/glfw/glfw_darwin.go: -------------------------------------------------------------------------------- 1 | // Copyright 2012 The go-gl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package glfw 6 | 7 | import "runtime" 8 | 9 | // Fix issue #4 (https://github.com/jteeuwen/glfw/issues/4) on OSX. 10 | // 11 | // This used to be fixed in the Go runtime, but the patch was reverted. 12 | // See http://code.google.com/p/go/issues/detail?id=3125 for info. 13 | func init() { 14 | runtime.LockOSThread() 15 | } 16 | -------------------------------------------------------------------------------- /v2.7/glfw/glfw_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2012 The go-gl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package glfw 6 | 7 | import ( 8 | "testing" 9 | ) 10 | 11 | func Test(t *testing.T) { 12 | 13 | } 14 | -------------------------------------------------------------------------------- /v2.7/glfw/image.go: -------------------------------------------------------------------------------- 1 | // Copyright 2012 The go-gl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package glfw 6 | 7 | //#include 8 | //#ifdef _WIN32 9 | // #define GLFW_DLL 10 | //#endif 11 | //#include 12 | import "C" 13 | import ( 14 | "errors" 15 | "unsafe" 16 | ) 17 | 18 | // LoadTexture2D reads an image from the file specified by the parameter name 19 | // and uploads the image to OpenGL texture memory (using the glTexImage2D function). 20 | // 21 | // Note: If the glfw.BuildMipmapsBit flag is set, all mipmap levels for the 22 | // loaded texture are generated and uploaded to texture memory. 23 | // 24 | // Note: The read image is always rescaled to the nearest larger power of 25 | // two resolution using bilinear interpolation. 26 | // 27 | // Note: Unless the glfw.OriginUlBit flag is set, the first pixel in img.Data 28 | // is the lower left corner of the image. Otherwise the first pixel is the upper 29 | // left corner. 30 | // 31 | // Note: For single component images (i.e. gray scale), Format is set to 32 | // GL_ALPHA if the glfw.AlphaMapBit flag is set, otherwise Format is set to GL_LUMINANCE. 33 | // 34 | // Note: ReadImage supports the Truevision Targa version 1 file format 35 | // (.TGA). Supported pixel formats are: 8-bit gray scale, 8-bit paletted 36 | // (24/32-bit color), 24-bit true color and 32-bit true color + alpha. 37 | func LoadTexture2D(name string, flags int) bool { 38 | cn := C.CString(name) 39 | defer C.free(unsafe.Pointer(cn)) 40 | return int(C.glfwLoadTexture2D(cn, C.int(flags))) == 1 41 | } 42 | 43 | // LoadMemoryTexture2D reads an image from the memory buffer specified by the 44 | // parameter data and uploads the image to OpenG texture memory (using the 45 | // glTexImage2D function). 46 | // 47 | // Note: If the glfw.BuildMipmapsBit flag is set, all mipmap levels for the 48 | // loaded texture are generated and uploaded to texture memory. 49 | // 50 | // Note: The read image is always rescaled to the nearest larger power of 51 | // two resolution using bilinear interpolation. 52 | // 53 | // Note: Unless the glfw.OriginUlBit flag is set, the first pixel in img.Data 54 | // is the lower left corner of the image. Otherwise the first pixel is the upper 55 | // left corner. 56 | // 57 | // Note: For single component images (i.e. gray scale), Format is set to 58 | // GL_ALPHA if the glfw.AlphaMapBit flag is set, otherwise Format is set to GL_LUMINANCE. 59 | // 60 | // Note: ReadImage supports the Truevision Targa version 1 file format 61 | // (.TGA). Supported pixel formats are: 8-bit gray scale, 8-bit paletted 62 | // (24/32-bit color), 24-bit true color and 32-bit true color + alpha. 63 | func LoadMemoryTexture2D(data []byte, flags int) bool { 64 | if len(data) == 0 { 65 | return false 66 | } 67 | 68 | return int(C.glfwLoadMemoryTexture2D( 69 | unsafe.Pointer(&data[0]), C.long(len(data)), C.int(flags))) == 1 70 | } 71 | 72 | // The Image type holds data for loaded images. 73 | type Image struct { 74 | img C.GLFWimage 75 | } 76 | 77 | // ReadImage reads an image from the file specified by the parameter name and 78 | // returns the image or an error. 79 | // 80 | // Note: By default the read image is rescaled to the nearest larger power of 81 | // two resolution using bilinear interpolation, if necessary. Which is useful 82 | // if the image is to be used as an OpenGL texture. This behavior can be 83 | // disabled by setting the glfw.NoRescaleBit flag. 84 | // 85 | // Note: Unless the glfw.OriginUlBit flag is set, the first pixel in img.Data 86 | // is the lower left corner of the image. Otherwise the first pixel is the upper 87 | // left corner. 88 | // 89 | // Note: For single component images (i.e. gray scale), Format is set to 90 | // GL_ALPHA if the glfw.AlphaMapBit flag is set, otherwise Format is set to GL_LUMINANCE. 91 | // 92 | // Note: ReadImage supports the Truevision Targa version 1 file format 93 | // (.TGA). Supported pixel formats are: 8-bit gray scale, 8-bit paletted 94 | // (24/32-bit color), 24-bit true color and 32-bit true color + alpha. 95 | func ReadImage(name string, flags int) (i *Image, err error) { 96 | i = new(Image) 97 | 98 | str := C.CString(name) 99 | defer C.free(unsafe.Pointer(str)) 100 | 101 | if C.glfwReadImage(str, &i.img, C.int(flags)) != 1 { 102 | return nil, errors.New("Failed to read image " + name) 103 | } 104 | 105 | return 106 | } 107 | 108 | // ReadMemoryImage reads an image file from the memory buffer specified by the 109 | // parameter data and returns the image information or an error. 110 | // 111 | // Note: ReadMemoryImage supports the Truevision Targa version 1 file format 112 | // (.TGA). Supported pixel formats are: 8-bit gray scale, 8-bit paletted 113 | // (24/32-bit color), 24-bit true color and 32-bit true color + alpha. 114 | func ReadMemoryImage(data []byte, flags int) (i *Image, err error) { 115 | if len(data) == 0 { 116 | return nil, errors.New("No image data was supplied") 117 | } 118 | 119 | i = new(Image) 120 | 121 | if C.glfwReadMemoryImage(unsafe.Pointer(&data[0]), C.long(len(data)), &i.img, C.int(flags)) != 1 { 122 | return nil, errors.New("Failed to read image from memory") 123 | } 124 | 125 | return 126 | } 127 | 128 | // LoadTextureImage2D uploads the image specified by the parameter img to 129 | // OpenGL texture memory (using the glTexImage2D function). 130 | // 131 | // Note: If the glfw.BuildMipmapsBit flag is set, all mipmap levels for the 132 | // loaded texture are generated and uploaded to texture memory. 133 | // 134 | // Note: The read image is always rescaled to the nearest larger power of 135 | // two resolution using bilinear interpolation. 136 | // 137 | // Note: Unless the glfw.OriginUlBit flag is set, the first pixel in img.Data 138 | // is the lower left corner of the image. Otherwise the first pixel is the upper 139 | // left corner. 140 | // 141 | // Note: For single component images (i.e. gray scale), Format is set to 142 | // GL_ALPHA if the glfw.AlphaMapBit flag is set, otherwise Format is set to GL_LUMINANCE. 143 | // 144 | // Note: ReadImage supports the Truevision Targa version 1 file format 145 | // (.TGA). Supported pixel formats are: 8-bit gray scale, 8-bit paletted 146 | // (24/32-bit color), 24-bit true color and 32-bit true color + alpha. 147 | func (i *Image) LoadTextureImage2D(flags int) bool { 148 | return int(C.glfwLoadTextureImage2D(&i.img, C.int(flags))) == 1 149 | } 150 | 151 | // Release frees any memory occupied by a loaded image, and clears all the 152 | // fields of the GLFWimage struct. Any image that has been loaded by the 153 | // glfw.ReadImage function should be deallocated using this function once the 154 | // image is no longer needed. 155 | func (i *Image) Release() { C.glfwFreeImage(&i.img) } 156 | 157 | func (i *Image) Width() int { return int(i.img.Width) } 158 | 159 | func (i *Image) SetWidth(v int) { i.img.Width = C.int(v) } 160 | 161 | func (i *Image) Height() int { return int(i.img.Height) } 162 | 163 | func (i *Image) SetHeight(v int) { i.img.Height = C.int(v) } 164 | 165 | // Format specifies an OpenGL pixel format, which can be GL_LUMINANCE or 166 | // GL_ALPHA (for gray scale images), GL_RGB or GL_RGBA. 167 | func (i *Image) Format() int { return int(i.img.Format) } 168 | 169 | func (i *Image) SetFormat(v int) { i.img.Format = C.int(v) } 170 | 171 | // BytesPerPixel specifies the number of bytes per pixel. 172 | func (i *Image) BytesPerPixel() int { return int(i.img.BytesPerPixel) } 173 | 174 | func (i *Image) SetBytesPerPixel(v int) { i.img.BytesPerPixel = C.int(v) } 175 | 176 | // Data returns the actual pixel data. 177 | // 178 | // FIXME(jimt): This assumes images <= 2gb in size. 179 | func (i *Image) Data() []byte { 180 | size := i.img.BytesPerPixel * i.img.Width * i.img.Height 181 | return (*(*[1<<31 - 1]byte)(unsafe.Pointer(i.img.Data)))[:size] 182 | } 183 | 184 | // SetData sets the actual pixel data. 185 | // 186 | // FIXME(jimt): This assumes images <= 2gb in size. 187 | func (i *Image) SetData(v []byte) { 188 | i.img.Data = (*_Ctype_unsignedchar)(unsafe.Pointer(&v)) 189 | } 190 | -------------------------------------------------------------------------------- /v2.7/glfw/vidmode.go: -------------------------------------------------------------------------------- 1 | // Copyright 2012 The go-gl Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package glfw 6 | 7 | //#include 8 | //#ifdef _WIN32 9 | // #define GLFW_DLL 10 | //#endif 11 | //#include 12 | import "C" 13 | import ( 14 | "fmt" 15 | "unsafe" 16 | ) 17 | 18 | type VidMode struct { 19 | W int // width 20 | H int // height 21 | R int // red bits 22 | G int // green bits 23 | B int // blue bits 24 | } 25 | 26 | func vidModeFromPtr(ptr *C.GLFWvidmode) *VidMode { 27 | v := new(VidMode) 28 | v.W = int(ptr.Width) 29 | v.H = int(ptr.Height) 30 | v.R = int(ptr.RedBits) 31 | v.G = int(ptr.GreenBits) 32 | v.B = int(ptr.BlueBits) 33 | return v 34 | } 35 | 36 | func (this *VidMode) String() string { 37 | return fmt.Sprintf("%dx%d @ %d bpp", this.W, this.H, this.R+this.G+this.B) 38 | } 39 | 40 | func (this *VidMode) toPtr() (ptr *C.GLFWvidmode) { 41 | ptr = (*C.GLFWvidmode)(C.malloc(C.size_t(unsafe.Sizeof(ptr)))) 42 | ptr.Width = C.int(this.W) 43 | ptr.Height = C.int(this.H) 44 | ptr.RedBits = C.int(this.R) 45 | ptr.GreenBits = C.int(this.G) 46 | ptr.BlueBits = C.int(this.B) 47 | return 48 | } 49 | --------------------------------------------------------------------------------