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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
#include "main.h"
#include "state.h"
#include "lua_core.h"
#include "splash.h"
#ifdef _WIN32
// Forward declarations for Windows console functions
extern __declspec(dllimport) int __stdcall AllocConsole(void);
extern __declspec(dllimport) int __stdcall FreeConsole(void);
extern __declspec(dllimport) void* __stdcall GetStdHandle(unsigned long nStdHandle);
extern __declspec(dllimport) int __stdcall SetStdHandle(unsigned long nStdHandle, void* hHandle);
#endif
static inline 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 );
}
#ifdef LUA_VERSION
TraceLog( LOG_INFO, "%s", LUA_VERSION );
#endif
#ifdef LUAJIT_VERSION
TraceLog( LOG_INFO, "%s", LUAJIT_VERSION );
#endif
}
int main( int argn, const char** argc ) {
char basePath[ STRING_LEN ] = { '\0' };
bool interpret_mode = false;
bool show_console = false;
bool skip_splash = false;
#ifdef _WIN32
/* Check for --log and --no-logo arguments */
for ( int i = 1; i < argn; i++ ) {
if ( strcmp( argc[i], "--log" ) == 0 ) {
show_console = true;
}
if ( strcmp( argc[i], "--no-logo" ) == 0 ) {
skip_splash = true;
}
}
/* Show or hide console based on --log argument */
if ( show_console ) {
/* Allocate a console if we don't have one */
if ( AllocConsole() ) {
freopen( "CONOUT$", "w", stdout );
freopen( "CONOUT$", "w", stderr );
freopen( "CONIN$", "r", stdin );
}
}
else {
/* Hide console window */
FreeConsole();
}
#else
/* Check for --no-logo on non-Windows platforms */
for ( int i = 1; i < argn; i++ ) {
if ( strcmp( argc[i], "--no-logo" ) == 0 ) {
skip_splash = true;
break;
}
}
#endif
if ( 1 < argn ) {
/* Skip --log and --no-logo flags to find the actual command */
int arg_index = 1;
while ( arg_index < argn && ( strcmp( argc[arg_index], "--log" ) == 0 || strcmp( argc[arg_index], "--no-logo" ) == 0 ) ) {
arg_index++;
}
if ( arg_index < argn && ( strcmp( argc[arg_index], "--version" ) == 0 || strcmp( argc[arg_index], "-v" ) == 0 ) ) {
printVersion();
return 1;
}
else if ( arg_index < argn && ( strcmp( argc[arg_index], "--help" ) == 0 || strcmp( argc[arg_index], "-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--log\t\tShow console for logging\n--no-logo\tSkip splash screens (development)\n" );
return 1;
}
else if ( arg_index < argn && ( strcmp( argc[arg_index], "--interpret" ) == 0 || strcmp( argc[arg_index], "-i" ) == 0 ) ) {
interpret_mode = true;
if ( arg_index + 1 < argn ) {
sprintf( basePath, "%s/%s", GetWorkingDirectory(), argc[arg_index + 1] );
}
}
else if ( arg_index < argn ) {
sprintf( basePath, "%s/%s", GetWorkingDirectory(), argc[arg_index] );
}
else {
/* Only flags were provided, use default path search */
char testPath[ STRING_LEN ] = { '\0' };
sprintf( testPath, "%s/main.lua", GetWorkingDirectory() );
if ( FileExists( testPath ) ) {
sprintf( basePath, "%s", GetWorkingDirectory() );
}
else {
sprintf( basePath, "%s", GetApplicationDirectory() );
}
}
}
/* If no argument given, check current directory first, then exe directory. */
else {
char testPath[ STRING_LEN ] = { '\0' };
sprintf( testPath, "%s/main.lua", GetWorkingDirectory() );
if ( FileExists( testPath ) ) {
sprintf( basePath, "%s", GetWorkingDirectory() );
}
else {
sprintf( basePath, "%s", GetApplicationDirectory() );
}
}
if ( interpret_mode ) {
stateInitInterpret( argn, argc );
lua_State* L = state->luaState;
lua_pushcfunction( L, luaTraceback );
int tracebackidx = lua_gettop( L );
luaL_loadfile( L, basePath );
if ( lua_pcall( L, 0, 0, tracebackidx ) != 0 ) {
TraceLog( LOG_ERROR, "Lua error: %s", lua_tostring( L, -1 ) );
return false;
}
}
else {
printVersion();
stateInit( argn, argc, basePath );
/* Show splash screens if not skipped */
if ( !skip_splash ) {
splashInit();
bool splashDone = false;
while ( !splashDone && !WindowShouldClose() ) {
float delta = GetFrameTime();
splashDone = splashUpdate( delta );
splashDraw();
}
splashCleanup();
}
/* Now run the main Lua program */
state->run = luaCallMain();
while ( state->run ) {
if ( WindowShouldClose() ) {
state->run = false;
}
luaCallUpdate();
luaCallDraw();
}
luaCallExit();
}
stateFree();
return 0;
}
|