Added arg parsing and refactored the code

Author Mitja Felicijan <mitja.felicijan@gmail.com> 2025-08-06 12:37:03 +0200
Committer Mitja Felicijan <mitja.felicijan@gmail.com> 2025-08-06 12:37:03 +0200
Commit ffefb35f01d5896c306d61e27c477d00d57819b1 (patch)
-rw-r--r-- main.c 97
1 files changed, 74 insertions, 23 deletions
diff --git a/main.c b/main.c
1
#include <stdio.h>
1
#include <stdio.h>
  
2
#include <stdarg.h>
  
3
#include <getopt.h>
  
4
#include <stdlib.h>
2
  
5
  
3
#include "raylib.h"
6
#include "raylib.h"
4
#include "lua.h"
7
#include "lua.h"
...
9
#include "stdlib/color.h"
12
#include "stdlib/color.h"
10
#include "stdlib/tilemap.h"
13
#include "stdlib/tilemap.h"
11
  
14
  
12
#define IN_FILE "test/main.lua"
15
#define VERSION "x.x"
13
#define DEBUG_LEVEL LOG_DEBUG
16
#define DEBUG_LEVEL LOG_DEBUG
14
  
17
  
15
static int lua_getfield_int(lua_State *L, int index, const char *key) {
18
static int lua_getfield_int(lua_State *L, int index, const char *key) {
...
91
	return 0;
94
	return 0;
92
}
95
}
93
  
96
  
94
int main(void) {
97
static void help(const char *argv0) {
95
	SetTraceLogLevel(DEBUG_LEVEL);
98
	printf("Usage: %s [options]\n"
  
99
			"\nAvailable options:\n"
  
100
			"  -r,--run=file.lua            run input file\n"
  
101
			"  -b,--bundle                  bundles this folder\n"
  
102
			"  -h,--help                    this help\n"
  
103
			"  -v,--version                 show version\n",
  
104
			argv0);
  
105
}
  
106
  
  
107
static void version(const char *argv0) {
  
108
	printf("%s version %s\n", argv0, VERSION);
  
109
}
  
110
  
  
111
int main(int argc, char *argv[]) {
  
112
	const char short_options[] = "r:bhv";
  
113
	const struct option long_options[] = {
  
114
		{ "run", 1, NULL, 'r' },
  
115
		{ "bundle", 0, NULL, 'b' },
  
116
		{ "help", 0, NULL, 'h' },
  
117
		{ "version", 0, NULL, 'v' },
  
118
		{ 0 },
  
119
	};
  
120
  
  
121
	int opt;
  
122
	const char *run_file = NULL;
  
123
	while ((opt = getopt_long(argc, argv, short_options, long_options, NULL)) != -1) {
  
124
		switch (opt) {
  
125
			case 'r':
  
126
				run_file = optarg;
  
127
				break;
  
128
			case 'b':
  
129
				printf("TODO: Bundler\n");
  
130
				return 0;
  
131
			case 'h':
  
132
				help(argv[0]);
  
133
				return 0;
  
134
			case 'v':
  
135
				version(argv[0]);
  
136
				return 0;
  
137
			default:
  
138
				fprintf(stdout, "Missing options. Check help.\n");
  
139
				return 0;
  
140
		}
  
141
	}
96
  
142
  
97
	lua_State *L = luaL_newstate();
143
	if (run_file) {
98
	luaL_openlibs(L);
144
		SetTraceLogLevel(DEBUG_LEVEL);
99
  
145
  
100
	// Registring Raylib mappings.
146
		lua_State *L = luaL_newstate();
101
	lua_register(L, "open_window", l_open_window);
147
		luaL_openlibs(L);
102
	lua_register(L, "close_window", l_close_window);
  
103
	lua_register(L, "window_running", l_window_running);
  
104
	lua_register(L, "begin_drawing", l_begin_drawing);
  
105
	lua_register(L, "end_drawing", l_end_drawing);
  
106
	lua_register(L, "set_fps", l_set_fps);
  
107
	lua_register(L, "draw_fps", l_draw_fps);
  
108
	lua_register(L, "clear_window", l_clear_window);
  
109
  
148
  
110
	// Loading embeded modules into Lua state.
149
		// Registring Raylib mappings.
111
	if (!load_embedded_module(L, json, json_len, "json")) return 1;
150
		lua_register(L, "open_window", l_open_window);
112
	if (!load_embedded_module(L, color, color_len, "color")) return 1;
151
		lua_register(L, "close_window", l_close_window);
113
	if (!load_embedded_module(L, tilemap, tilemap_len, "tilemap")) return 1;
152
		lua_register(L, "window_running", l_window_running);
  
153
		lua_register(L, "begin_drawing", l_begin_drawing);
  
154
		lua_register(L, "end_drawing", l_end_drawing);
  
155
		lua_register(L, "set_fps", l_set_fps);
  
156
		lua_register(L, "draw_fps", l_draw_fps);
  
157
		lua_register(L, "clear_window", l_clear_window);
114
  
158
  
115
	// Interpreting and running input file Lua script.
159
		// Loading embeded modules into Lua state.
116
	if (luaL_loadfile(L, IN_FILE) || lua_pcall(L, 0, 0, 0)) {
160
		if (!load_embedded_module(L, json, json_len, "json")) return 1;
117
		fprintf(stderr, "Error: %s\n", lua_tostring(L, -1));
161
		if (!load_embedded_module(L, color, color_len, "color")) return 1;
118
		return 1;
162
		if (!load_embedded_module(L, tilemap, tilemap_len, "tilemap")) return 1;
  
163
  
  
164
		// Interpreting and running input file Lua script.
  
165
		if (luaL_loadfile(L, run_file) || lua_pcall(L, 0, 0, 0)) {
  
166
			fprintf(stderr, "Error: %s\n", lua_tostring(L, -1));
  
167
			return 1;
  
168
		}
  
169
  
  
170
		lua_close(L);
119
	}
171
	}
120
  
172
  
121
	lua_close(L);
  
122
	return 0;
173
	return 0;
123
}
174
}