From bed7440ebbf10292418ed2e7bd5b8f6bd79b3ec9 Mon Sep 17 00:00:00 2001 From: aguspiza Date: Thu, 27 Jun 2019 22:17:13 +0200 Subject: [PATCH] tetris improvements added getexepath() to os module added some wrapper functions to glfw removed hardcored path in gg module better tetris example exit and resource loading --- examples/tetris/tetris.v | 7 +++++++ gg/gg.v | 5 ++++- glfw/glfw.v | 12 ++++++++++++ os/os.v | 21 +++++++++++++++++++++ 4 files changed, 44 insertions(+), 1 deletion(-) diff --git a/examples/tetris/tetris.v b/examples/tetris/tetris.v index 6eb728a328..7cd6a350b0 100644 --- a/examples/tetris/tetris.v +++ b/examples/tetris/tetris.v @@ -131,6 +131,11 @@ fn main() { game.draw_scene() window.swap_buffers() glfw.wait_events() + if window.should_close() { + window.destroy() + glfw.terminate() + exit(0) + } } } @@ -322,6 +327,8 @@ fn key_down(wnd voidptr, key, code, action, mods int) { // Fetch the game object stored in the user pointer mut game := &Game(glfw.get_window_user_pointer(wnd)) switch key { + case glfw.KEY_ESCAPE: + glfw.set_should_close(wnd, true) case glfw.KeyUp: // Rotate the tetro game.rotation_idx++ diff --git a/gg/gg.v b/gg/gg.v index c15be5d65a..6ebfbb7685 100644 --- a/gg/gg.v +++ b/gg/gg.v @@ -342,7 +342,10 @@ pub fn new_context_text(cfg Cfg, scale int) *GG { // face := FT_Face{} mut font_path := 'RobotoMono-Regular.ttf' if !os.file_exists(font_path) { - font_path = '/var/tmp/RobotoMono-Regular.ttf' + exePath := os.getexepath() + exeDir := os.basedir(exePath) + println('Trying to load from $exeDir') + font_path = '${exeDir}/RobotoMono-Regular.ttf' } if !os.file_exists(font_path) { println('failed to load RobotoMono-Regular.ttf') diff --git a/glfw/glfw.v b/glfw/glfw.v index bd4e13ccfe..0e71e7403c 100644 --- a/glfw/glfw.v +++ b/glfw/glfw.v @@ -135,6 +135,14 @@ pub fn init() { # glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); } +pub fn (w &Window) destroy() { + C.glfwDestroyWindow(w.data) +} + +pub fn terminate() { + C.glfwTerminate() +} + // pub fn mouse_move(w * GLFWwindow, x, y double) { pub fn mouse_move(w voidptr, x, y f64) { // #printf("%f : %f => %d \n", x,y); @@ -203,6 +211,10 @@ pub fn poll_events() { C.glfwPollEvents() } +pub fn set_should_close(w voidptr, close bool) { + C.glfwSetWindowShouldClose(w, close) +} + pub fn (w &Window) should_close() bool { // ChatsRepo return C.glfwWindowShouldClose(w.data) diff --git a/os/os.v b/os/os.v index 18b2a20c9f..540c8b85b4 100644 --- a/os/os.v +++ b/os/os.v @@ -7,6 +7,7 @@ module os #include const ( args = []string + MAX_PATH = 4096 ) struct FILE { @@ -463,3 +464,23 @@ fn on_segfault(f voidptr) { C.sigaction(SIGSEGV, &sa, 0) } } + +pub fn getexepath() string { + mut result := [4096]byte // [MAX_PATH]byte --> error byte undefined + $if linux { + count := int(C.readlink('/proc/self/exe', result, MAX_PATH )) + if(count < 0) { + panic('error reading /proc/self/exe to get exe path') + } + return tos(result, count) + } + + $if windows { + return tos( result, C.GetModuleFileName( 0, result, MAX_PATH ) ) + } + + $if mac { + //panic('getexepath() not impl') + return '' + } +}