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
pull/777/head
aguspiza 2019-06-27 22:17:13 +02:00 committed by Alexander Medvednikov
parent 84f5d7e64b
commit bed7440ebb
4 changed files with 44 additions and 1 deletions

View File

@ -131,6 +131,11 @@ fn main() {
game.draw_scene() game.draw_scene()
window.swap_buffers() window.swap_buffers()
glfw.wait_events() 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 // Fetch the game object stored in the user pointer
mut game := &Game(glfw.get_window_user_pointer(wnd)) mut game := &Game(glfw.get_window_user_pointer(wnd))
switch key { switch key {
case glfw.KEY_ESCAPE:
glfw.set_should_close(wnd, true)
case glfw.KeyUp: case glfw.KeyUp:
// Rotate the tetro // Rotate the tetro
game.rotation_idx++ game.rotation_idx++

View File

@ -342,7 +342,10 @@ pub fn new_context_text(cfg Cfg, scale int) *GG {
// face := FT_Face{} // face := FT_Face{}
mut font_path := 'RobotoMono-Regular.ttf' mut font_path := 'RobotoMono-Regular.ttf'
if !os.file_exists(font_path) { 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) { if !os.file_exists(font_path) {
println('failed to load RobotoMono-Regular.ttf') println('failed to load RobotoMono-Regular.ttf')

View File

@ -135,6 +135,14 @@ pub fn init() {
# glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); # 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 * GLFWwindow, x, y double) {
pub fn mouse_move(w voidptr, x, y f64) { pub fn mouse_move(w voidptr, x, y f64) {
// #printf("%f : %f => %d \n", x,y); // #printf("%f : %f => %d \n", x,y);
@ -203,6 +211,10 @@ pub fn poll_events() {
C.glfwPollEvents() C.glfwPollEvents()
} }
pub fn set_should_close(w voidptr, close bool) {
C.glfwSetWindowShouldClose(w, close)
}
pub fn (w &Window) should_close() bool { pub fn (w &Window) should_close() bool {
// ChatsRepo // ChatsRepo
return C.glfwWindowShouldClose(w.data) return C.glfwWindowShouldClose(w.data)

21
os/os.v
View File

@ -7,6 +7,7 @@ module os
#include <sys/stat.h> #include <sys/stat.h>
const ( const (
args = []string args = []string
MAX_PATH = 4096
) )
struct FILE { struct FILE {
@ -463,3 +464,23 @@ fn on_segfault(f voidptr) {
C.sigaction(SIGSEGV, &sa, 0) 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 ''
}
}