├── glfw.v └── v.mod /glfw.v: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2019 Alexander Medvednikov. All rights reserved. 2 | // Use of this source code is governed by an MIT license 3 | // that can be found in the LICENSE file. 4 | 5 | module glfw 6 | 7 | import gl 8 | 9 | #flag -I @VROOT/thirdparty/glfw 10 | #flag -L @VROOT/thirdparty/glfw 11 | 12 | // Debugging a custom build 13 | //-#flag darwin -L/var/tmp/glfw/src/ 14 | 15 | // MacPorts 16 | #flag darwin -L/opt/local/lib 17 | 18 | #flag darwin -lglfw 19 | #flag linux -lglfw 20 | #flag windows -lglfw3 21 | #include 22 | // #flag darwin -framework Carbon 23 | // #flag darwin -framework Cocoa 24 | // #flag darwin -framework CoreVideo 25 | // #flag darwin -framework IOKit 26 | const ( 27 | RESIZABLE = 1 28 | DECORATED = 2 29 | ) 30 | 31 | import const ( 32 | GLFW_RESIZABLE 33 | GLFW_DECORATED 34 | GLFW_FLOATING 35 | ) 36 | 37 | import const ( 38 | GLFW_KEY_ENTER 39 | GLFW_KEY_A 40 | GLFW_KEY_B 41 | GLFW_KEY_P 42 | GLFW_KEY_F 43 | GLFW_KEY_M 44 | GLFW_KEY_L 45 | GLFW_KEY_V 46 | GLFW_KEY_R 47 | GLFW_KEY_D 48 | GLFW_KEY_7 49 | GLFW_KEY_Z 50 | GLFW_KEY_UP 51 | GLFW_KEY_DOWN 52 | GLFW_KEY_UP 53 | GLFW_KEY_LEFT 54 | GLFW_KEY_RIGHT 55 | GLFW_KEY_BACKSPACE 56 | GLFW_KEY_ENTER 57 | GLFW_KEY_ESCAPE 58 | GLFW_KEY_N 59 | GLFW_KEY_PERIOD 60 | GLFW_KEY_SLASH 61 | GLFW_KEY_F5 62 | GLFW_KEY_F6 63 | GLFW_KEY_MINUS 64 | GLFW_KEY_EQUAL 65 | GLFW_KEY_C 66 | GLFW_KEY_G 67 | GLFW_KEY_I 68 | GLFW_KEY_J 69 | GLFW_KEY_E 70 | GLFW_KEY_K 71 | GLFW_KEY_O 72 | GLFW_KEY_T 73 | GLFW_KEY_H 74 | GLFW_KEY_L 75 | GLFW_KEY_N 76 | GLFW_KEY_U 77 | GLFW_KEY_X 78 | GLFW_KEY_W 79 | GLFW_KEY_Y 80 | GLFW_KEY_Q 81 | GLFW_KEY_RIGHT_BRACKET 82 | GLFW_KEY_LEFT_BRACKET 83 | GLFW_KEY_8 84 | GLFW_KEY_TAB 85 | GLFW_KEY_COMMA 86 | GLFW_KEY_QUESTION 87 | ) 88 | 89 | const ( 90 | KEY_ESCAPE = 256 91 | KEY_LEFT_SUPER = 343 92 | ) 93 | 94 | const ( 95 | KeyUp = 265 96 | KeyLeft = 263 97 | KeyRight = 262 98 | KeyDown = 264 99 | ) 100 | 101 | // TODO COPY PASTA 102 | struct WinCfg { 103 | width int 104 | height int 105 | title string 106 | ptr voidptr 107 | borderless bool 108 | is_modal int 109 | is_browser bool 110 | url string 111 | always_on_top bool 112 | } 113 | 114 | // data *C.GLFWwindow 115 | // TODO change data to cobj 116 | struct Window { 117 | data voidptr 118 | title string 119 | mx int 120 | my int 121 | } 122 | 123 | struct Size { 124 | pub: 125 | width int 126 | height int 127 | } 128 | 129 | struct Pos { 130 | x int 131 | y int 132 | } 133 | 134 | // type clickpub fn pub fn (window * GLFWwindow, button, action, mods int) 135 | type clickpubfn fn (window voidptr, button, action, mods int) 136 | 137 | pub fn init() { 138 | C.glfwInit() 139 | # glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); 140 | # glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); 141 | # glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); 142 | # glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); 143 | } 144 | 145 | pub fn (w &Window) destroy() { 146 | C.glfwDestroyWindow(w.data) 147 | } 148 | 149 | pub fn terminate() { 150 | C.glfwTerminate() 151 | } 152 | 153 | // pub fn mouse_move(w * GLFWwindow, x, y double) { 154 | pub fn mouse_move(w voidptr, x, y f64) { 155 | // #printf("%f : %f => %d \n", x,y); 156 | } 157 | 158 | pub fn window_hint(key, val int) { 159 | C.glfwWindowHint(key, val) 160 | } 161 | 162 | pub fn create_window(c WinCfg) *Window { 163 | if c.borderless { 164 | window_hint(GLFW_RESIZABLE, 0) 165 | window_hint(GLFW_DECORATED, 0) 166 | } 167 | if c.always_on_top { 168 | window_hint(GLFW_FLOATING, 1) 169 | } 170 | cwindow := C.glfwCreateWindow(c.width, c.height, c.title.str, 0, 0) 171 | if isnil(cwindow) { 172 | println('failed to create glfw window') 173 | C.glfwTerminate() 174 | } 175 | // # glfwSetCursorPosCallback(cwindow, glfw__mouse_move) ; 176 | // C.glfwSetCursorPosCallback(cwindow, mouse_move) 177 | C.printf('create window wnd=%p ptr==%p\n', cwindow, c.ptr) 178 | C.glfwSetWindowUserPointer(cwindow, c.ptr) 179 | // # void *a =glfwGetWindowUserPointer(cwindow); 180 | // # printf("aaaaaa=%p d=%d\n", a,a); 181 | window := &Window { 182 | data: cwindow, 183 | title: c.title, 184 | } 185 | return window 186 | } 187 | 188 | pub fn (w &Window) set_title(title string) { 189 | C.glfwSetWindowTitle(w.data, title.str) 190 | } 191 | 192 | pub fn (w &Window) make_context_current() { 193 | // ChatsRepo 194 | kkk := 0 195 | // println('making context current' ) 196 | C.glfwMakeContextCurrent(w.data) 197 | } 198 | 199 | pub fn swap_interval(interval int) { 200 | C.glfwSwapInterval(interval) 201 | } 202 | 203 | pub fn wait_events() { 204 | C.glfwWaitEvents() 205 | } 206 | 207 | pub fn poll_events() { 208 | C.glfwPollEvents() 209 | } 210 | 211 | pub fn set_should_close(w voidptr, close bool) { 212 | C.glfwSetWindowShouldClose(w, close) 213 | } 214 | 215 | pub fn (w &Window) should_close() bool { 216 | // ChatsRepo 217 | return C.glfwWindowShouldClose(w.data) 218 | } 219 | 220 | pub fn (w &Window) swap_buffers() { 221 | C.glfwSwapBuffers(w.data) 222 | } 223 | 224 | pub fn (w mut Window) onmousemove(cb voidptr) { 225 | C.glfwSetCursorPosCallback(w.data, cb) 226 | } 227 | 228 | pub fn (w mut Window) set_mouse_button_callback(cb voidptr) { 229 | C.glfwSetMouseButtonCallback(w.data, cb) 230 | } 231 | 232 | pub fn (w mut Window) on_click(cb voidptr) { 233 | C.glfwSetMouseButtonCallback(w.data, cb) 234 | } 235 | 236 | pub fn (w &Window) set_scroll_callback(cb voidptr) { 237 | C.glfwSetScrollCallback(w.data, cb) 238 | } 239 | 240 | pub fn (w &Window) on_scroll(cb voidptr) { 241 | C.glfwSetScrollCallback(w.data, cb) 242 | } 243 | 244 | pub fn post_empty_event() { 245 | C.glfwPostEmptyEvent() 246 | } 247 | 248 | pub fn (w mut Window) onkeydown(cb voidptr) { 249 | C.glfwSetKeyCallback(w.data, cb) 250 | } 251 | 252 | pub fn (w mut Window) onchar(cb voidptr) { 253 | C.glfwSetCharModsCallback(w.data, cb) 254 | } 255 | 256 | pub fn get_time() f64 { 257 | return C.glfwGetTime() 258 | } 259 | 260 | pub fn key_pressed(wnd voidptr, key int) bool { 261 | # return glfwGetKey(wnd, key) == GLFW_PRESS; 262 | return false 263 | } 264 | 265 | // TODO not mut 266 | pub fn (w mut Window) get_clipboard_text() string { 267 | return string(byteptr(C.glfwGetClipboardString(w.data))) 268 | } 269 | 270 | pub fn (w &Window) set_clipboard_text(s string) { 271 | C.glfwSetClipboardString(w.data, s.str) 272 | } 273 | 274 | pub fn (w &Window) get_cursor_pos() Pos { 275 | x := f64(0) 276 | y := f64(0) 277 | C.glfwGetCursorPos(w.data, &x, &y) 278 | return Pos { 279 | x: int(x) 280 | y: int(y) 281 | } 282 | } 283 | 284 | pub fn (w &Window) user_ptr() voidptr { 285 | return C.glfwGetWindowUserPointer(w.data) 286 | } 287 | 288 | pub fn (w &Window) set_user_ptr(ptr voidptr) { 289 | C.glfwSetWindowUserPointer(w.data, ptr) 290 | } 291 | 292 | pub fn C.glfwGetVideoMode() C.GLFWvideoMode 293 | 294 | pub fn get_monitor_size() Size { 295 | # GLFWvidmode* mode = glfwGetVideoMode(glfwGetPrimaryMonitor()); 296 | // window_width = mode->width; 297 | // window_height = mode->height; 298 | // monitor := C.glfwGetPrimaryMonitor() 299 | res := Size{} 300 | # res.width=mode->width; 301 | # res.height=mode->height; 302 | // C.glfwGetMonitorPhysicalSize(monitor, &res.width, &res.height) 303 | return res 304 | } 305 | 306 | pub fn (size Size) str() string { 307 | return '{$size.width, $size.height}' 308 | } 309 | 310 | pub fn get_window_user_pointer(gwnd voidptr) voidptr { 311 | return C.glfwGetWindowUserPointer(gwnd) 312 | } 313 | -------------------------------------------------------------------------------- /v.mod: -------------------------------------------------------------------------------- 1 | Module { 2 | name: 'glfw' 3 | version: '0.1' 4 | deps: ['gl'] 5 | } 6 | 7 | --------------------------------------------------------------------------------