1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Copyright (c) 2014 by SiegeLord
//
// All rights reserved. Distributed under ZLib. For full terms see the file LICENSE.

use ffi::*;
use libc::*;
use std::process;
use std::ptr;
use std::thread::spawn;

static mut GLOBAL_MAIN_FUNC: Option<fn()> = None;

pub fn run(main_func: fn()) -> !
{
	unsafe {
		GLOBAL_MAIN_FUNC = Some(main_func);
		let ret = al_run_main(0, ptr::null(), allegro_main);
		process::exit(ret)
	}
}

extern "C" fn allegro_main(_: i32, _: *const *const i8) -> c_int
{
	unsafe {
		let ok = spawn(move || {
			(GLOBAL_MAIN_FUNC.unwrap())();
		})
		.join()
		.is_ok();
		al_uninstall_system();
		if ok
		{
			0
		}
		else
		{
			1
		}
	}
}