1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
v/vlib/glfw/glfw.v

317 lines
6.0 KiB
Go
Raw Normal View History

2019-06-23 05:21:30 +03:00
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
2019-06-22 21:20:28 +03:00
module glfw
#flag -I @VROOT/thirdparty/glfw
2019-07-12 06:04:04 +03:00
#flag -L @VROOT/thirdparty/glfw
2019-06-22 21:20:28 +03:00
// Debugging a custom build
2019-07-12 06:04:04 +03:00
//-#flag darwin -L/var/tmp/glfw/src/
2019-06-22 21:20:28 +03:00
#flag darwin -lglfw
#flag linux -lglfw
2019-07-12 06:04:04 +03:00
#flag windows -lglfw3
2019-06-22 21:20:28 +03:00
#include <GLFW/glfw3.h>
// #flag darwin -framework Carbon
// #flag darwin -framework Cocoa
// #flag darwin -framework CoreVideo
// #flag darwin -framework IOKit
const (
RESIZABLE = 1
DECORATED = 2
)
import const (
GLFW_RESIZABLE
GLFW_DECORATED
)
import const (
GLFW_KEY_ENTER
GLFW_KEY_A
GLFW_KEY_B
GLFW_KEY_P
GLFW_KEY_F
GLFW_KEY_M
GLFW_KEY_L
GLFW_KEY_V
GLFW_KEY_R
GLFW_KEY_D
GLFW_KEY_7
GLFW_KEY_Z
GLFW_KEY_UP
GLFW_KEY_DOWN
GLFW_KEY_UP
GLFW_KEY_LEFT
GLFW_KEY_RIGHT
GLFW_KEY_BACKSPACE
GLFW_KEY_ENTER
GLFW_KEY_ESCAPE
GLFW_KEY_N
GLFW_KEY_PERIOD
GLFW_KEY_SLASH
GLFW_KEY_F5
GLFW_KEY_F6
GLFW_KEY_MINUS
GLFW_KEY_EQUAL
GLFW_KEY_C
GLFW_KEY_G
GLFW_KEY_I
GLFW_KEY_J
GLFW_KEY_E
GLFW_KEY_K
GLFW_KEY_O
GLFW_KEY_T
GLFW_KEY_H
GLFW_KEY_L
GLFW_KEY_N
GLFW_KEY_U
GLFW_KEY_X
GLFW_KEY_W
GLFW_KEY_Y
GLFW_KEY_Q
GLFW_KEY_RIGHT_BRACKET
GLFW_KEY_LEFT_BRACKET
GLFW_KEY_8
GLFW_KEY_TAB
GLFW_KEY_COMMA
GLFW_KEY_QUESTION
)
const (
KEY_ESCAPE = 256
KEY_LEFT_SUPER = 343
)
const (
KeyUp = 265
KeyLeft = 263
KeyRight = 262
KeyDown = 264
)
// TODO COPY PASTA
struct WinCfg {
width int
height int
title string
ptr voidptr
borderless bool
is_modal int
is_browser bool
url string
}
// data *C.GLFWwindow
// TODO change data to cobj
struct Window {
data voidptr
title string
mx int
my int
}
struct Size {
pub:
width int
height int
}
struct Pos {
x int
y int
}
2019-06-26 18:49:50 +03:00
// type clickpub fn pub fn (window * GLFWwindow, button, action, mods int)
type clickpubfn fn (window voidptr, button, action, mods int)
2019-06-22 21:20:28 +03:00
2019-06-26 18:49:50 +03:00
pub fn init() {
2019-06-22 21:20:28 +03:00
C.glfwInit()
# glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
# glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
# glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
# glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
}
pub fn (w &Window) destroy() {
C.glfwDestroyWindow(w.data)
}
pub fn terminate() {
C.glfwTerminate()
}
2019-06-26 18:49:50 +03:00
// pub fn mouse_move(w * GLFWwindow, x, y double) {
pub fn mouse_move(w voidptr, x, y f64) {
2019-06-22 21:20:28 +03:00
// #printf("%f : %f => %d \n", x,y);
}
2019-06-26 18:49:50 +03:00
// pub fn create_window(title string, w, h int) * Window {
pub fn window_hint(key, val int) {
2019-06-22 21:20:28 +03:00
C.glfwWindowHint(key, val)
}
2019-06-26 18:49:50 +03:00
pub fn create_window(c WinCfg) *Window {
2019-06-22 21:20:28 +03:00
// TODO why i need this in stdlib? extern?
// # if (!gconst_init) { init_consts(); gconst_init = 1; }
// ChatsRepo
if c.borderless {
window_hint(GLFW_RESIZABLE, 0)
window_hint(GLFW_DECORATED, 0)
}
cwindow := C.glfwCreateWindow(c.width, c.height, c.title.str, 0, 0)
# if (!cwindow)
// if cwindow == 0
{
println('failed to credate glfw window')
C.glfwTerminate()
}
// # glfwSetCursorPosCallback(cwindow, glfw__mouse_move) ;
// C.glfwSetCursorPosCallback(cwindow, mouse_move)
C.printf('create window wnd=%p ptr==%p\n', cwindow, c.ptr)
C.glfwSetWindowUserPointer(cwindow, c.ptr)
// # void *a =glfwGetWindowUserPointer(cwindow);
// # printf("aaaaaa=%p d=%d\n", a,a);
window := &Window {
data: cwindow,
title: c.title,
}
// user_ptr: ptr,
// repo: repo,
// for !C.glfwWindowShouldClose(cwindow) {
// C.glfwPollEvents()
// wait_events()
// }
// C.glfwTerminate()
return window
}
2019-06-26 18:49:50 +03:00
pub fn (w &Window) set_title(title string) {
2019-06-22 21:20:28 +03:00
C.glfwSetWindowTitle(w.data, title.str)
}
2019-06-26 18:49:50 +03:00
pub fn (w &Window) make_context_current() {
2019-06-22 21:20:28 +03:00
// ChatsRepo
kkk := 0
// println('making context current' )
C.glfwMakeContextCurrent(w.data)
}
2019-06-26 18:49:50 +03:00
pub fn swap_interval(interval int) {
2019-06-22 21:20:28 +03:00
C.glfwSwapInterval(interval)
}
2019-06-26 18:49:50 +03:00
pub fn wait_events() {
2019-06-22 21:20:28 +03:00
C.glfwWaitEvents()
}
2019-06-26 18:49:50 +03:00
pub fn poll_events() {
2019-06-22 21:20:28 +03:00
C.glfwPollEvents()
}
pub fn set_should_close(w voidptr, close bool) {
C.glfwSetWindowShouldClose(w, close)
}
2019-06-26 18:49:50 +03:00
pub fn (w &Window) should_close() bool {
2019-06-22 21:20:28 +03:00
// ChatsRepo
return C.glfwWindowShouldClose(w.data)
}
2019-06-26 18:49:50 +03:00
pub fn (w &Window) swap_buffers() {
2019-06-22 21:20:28 +03:00
C.glfwSwapBuffers(w.data)
}
2019-06-26 18:49:50 +03:00
pub fn (w mut Window) onmousemove(cb voidptr) {
2019-06-22 21:20:28 +03:00
C.glfwSetCursorPosCallback(w.data, cb)
}
2019-06-26 18:49:50 +03:00
pub fn (w mut Window) set_mouse_button_callback(cb voidptr) {
2019-06-22 21:20:28 +03:00
C.glfwSetMouseButtonCallback(w.data, cb)
}
2019-06-26 18:49:50 +03:00
pub fn (w mut Window) on_click(cb voidptr) {
2019-06-22 21:20:28 +03:00
C.glfwSetMouseButtonCallback(w.data, cb)
}
2019-06-26 18:49:50 +03:00
pub fn (w &Window) set_scroll_callback(cb voidptr) {
2019-06-22 21:20:28 +03:00
C.glfwSetScrollCallback(w.data, cb)
}
2019-06-26 18:49:50 +03:00
pub fn (w &Window) on_scroll(cb voidptr) {
2019-06-22 21:20:28 +03:00
C.glfwSetScrollCallback(w.data, cb)
}
2019-06-26 18:49:50 +03:00
pub fn post_empty_event() {
2019-06-22 21:20:28 +03:00
C.glfwPostEmptyEvent()
}
2019-06-26 18:49:50 +03:00
pub fn (w mut Window) onkeydown(cb voidptr) {
2019-06-22 21:20:28 +03:00
C.glfwSetKeyCallback(w.data, cb)
}
2019-06-26 18:49:50 +03:00
pub fn (w mut Window) onchar(cb voidptr) {
2019-06-22 21:20:28 +03:00
C.glfwSetCharModsCallback(w.data, cb)
}
2019-06-26 18:49:50 +03:00
pub fn get_time() f64 {
2019-06-22 21:20:28 +03:00
return C.glfwGetTime()
}
2019-06-26 18:49:50 +03:00
pub fn key_pressed(wnd voidptr, key int) bool {
2019-06-22 21:20:28 +03:00
# return glfwGetKey(wnd, key) == GLFW_PRESS;
return false
}
// TODO not mut
2019-06-26 18:49:50 +03:00
pub fn (w mut Window) get_clipboard_text() string {
return string(byteptr(C.glfwGetClipboardString(w.data)))
2019-06-22 21:20:28 +03:00
}
2019-06-26 18:49:50 +03:00
pub fn (w &Window) set_clipboard_text(s string) {
2019-06-22 21:20:28 +03:00
C.glfwSetClipboardString(w.data, s.str)
}
2019-06-26 18:49:50 +03:00
pub fn (w &Window) get_cursor_pos() Pos {
2019-06-25 23:19:17 +03:00
x := f64(0)
y := f64(0)
2019-06-22 21:20:28 +03:00
C.glfwGetCursorPos(w.data, &x, &y)
return Pos {
x: int(x)
y: int(y)
}
}
2019-06-26 18:49:50 +03:00
pub fn (w &Window) user_ptr() voidptr {
2019-06-22 21:20:28 +03:00
return C.glfwGetWindowUserPointer(w.data)
}
2019-06-26 18:49:50 +03:00
pub fn (w &Window) set_user_ptr(ptr voidptr) {
2019-06-22 21:20:28 +03:00
C.glfwSetWindowUserPointer(w.data, ptr)
}
2019-06-26 18:49:50 +03:00
pub fn C.glfwGetVideoMode() C.GLFWvideoMode
2019-06-22 21:20:28 +03:00
2019-06-26 18:49:50 +03:00
pub fn get_monitor_size() Size {
2019-06-22 21:20:28 +03:00
# GLFWvidmode* mode = glfwGetVideoMode(glfwGetPrimaryMonitor());
// window_width = mode->width;
// window_height = mode->height;
// monitor := C.glfwGetPrimaryMonitor()
res := Size{}
# res.width=mode->width;
# res.height=mode->height;
// C.glfwGetMonitorPhysicalSize(monitor, &res.width, &res.height)
return res
}
2019-06-26 18:49:50 +03:00
pub fn (size Size) str() string {
2019-06-22 21:20:28 +03:00
return '{$size.width, $size.height}'
}
2019-06-26 18:49:50 +03:00
pub fn get_window_user_pointer(gwnd voidptr) voidptr {
2019-06-22 21:20:28 +03:00
return C.glfwGetWindowUserPointer(gwnd)
}