summaryrefslogtreecommitdiff
path: root/src/main.c
blob: d1be4a1172411c61c4000014c33c84d3ea28e55c (plain)
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "main.h"
#include "state.h"
#include "lua_core.h"

inline static void printVersion() {
	if ( VERSION_DEV ) {
		TraceLog( LOG_INFO, "ReiLua %d.%d.%d-Dev", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH );
	}
	else {
		TraceLog( LOG_INFO, "ReiLua %d.%d.%d", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH );
	}
}

int main( int argn, const char **argc ) {
	char exePath[ STRING_LEN ] = { '\0' };
	bool interpret_mode = false;

	if ( 1 < argn ) {
		if ( strcmp( argc[1], "--version" ) == 0 || strcmp( argc[1], "-v" ) == 0 ) {
			printVersion();
			return 1;
		}
		else if ( strcmp( argc[1], "--help" ) == 0 || strcmp( argc[1], "-h" ) == 0 ) {
			printf(
"Usage: ReiLua [Options] [Directory to main.lua or main]\nOptions:\n-h --help\tThis help\n-v --version\tShow ReiLua version\n-i --interpret\tInterpret mode [File name]\n" );

			return 1;
		}
		else if ( strcmp( argc[1], "--interpret" ) == 0 || strcmp( argc[1], "-i" ) == 0 ) {
			interpret_mode = true;

			if ( 2 < argn ) {
				sprintf( exePath, "%s/%s", GetWorkingDirectory(), argc[2] );
			}
		}
		else{
			sprintf( exePath, "%s/%s", GetWorkingDirectory(), argc[1] );
		}
	}
	else {
		sprintf( exePath, "%s/", GetWorkingDirectory() );
	}

	if ( interpret_mode ) {
		stateInitInterpret();

		lua_State *L = state->luaState;
		lua_pushcfunction( L, luaTraceback );
		int tracebackidx = lua_gettop( L );

		luaL_loadfile( L, exePath );

		if ( lua_pcall( L, 0, 0, tracebackidx ) != 0 ) {
			TraceLog( LOG_ERROR, "Lua error: %s", lua_tostring( L, -1 ) );
			return false;
		}
	}
	else {
		printVersion();
		stateInit( exePath );
		luaRegister();
		state->run = luaCallMain();

		while ( state->run ) {
			if ( WindowShouldClose() ) {
				state->run = false;
			}
			if ( IsAudioDeviceReady() ) {
				UpdateMusicStream( state->music );
			}
			luaCallProcess();
			luaCallDraw();
		}
		luaCallExit();
	}

	stateFree();

	return 1;
}