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 loadingpull/777/head
							parent
							
								
									84f5d7e64b
								
							
						
					
					
						commit
						bed7440ebb
					
				|  | @ -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++ | ||||
|  |  | |||
							
								
								
									
										5
									
								
								gg/gg.v
								
								
								
								
							
							
						
						
									
										5
									
								
								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') | ||||
|  |  | |||
							
								
								
									
										12
									
								
								glfw/glfw.v
								
								
								
								
							
							
						
						
									
										12
									
								
								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) | ||||
|  |  | |||
							
								
								
									
										21
									
								
								os/os.v
								
								
								
								
							
							
						
						
									
										21
									
								
								os/os.v
								
								
								
								
							|  | @ -7,6 +7,7 @@ module os | |||
| #include <sys/stat.h> | ||||
| 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 '' | ||||
| 	} | ||||
| } | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue