aboutsummaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'main.c')
-rw-r--r--main.c105
1 files changed, 78 insertions, 27 deletions
diff --git a/main.c b/main.c
index 083a2d9..bbc7c35 100644
--- a/main.c
+++ b/main.c
@@ -1,4 +1,7 @@
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,7 +12,7 @@
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
15static int lua_getfield_int(lua_State *L, int index, const char *key) { 18static int lua_getfield_int(lua_State *L, int index, const char *key) {
@@ -91,33 +94,81 @@ static int l_draw_fps(lua_State *L) {
91 return 0; 94 return 0;
92} 95}
93 96
94int main(void) { 97static void help(const char *argv0) {
95 SetTraceLogLevel(DEBUG_LEVEL); 98 printf("Usage: %s [options]\n"
96 99 "\nAvailable options:\n"
97 lua_State *L = luaL_newstate(); 100 " -r,--run=file.lua run input file\n"
98 luaL_openlibs(L); 101 " -b,--bundle bundles this folder\n"
99 102 " -h,--help this help\n"
100 // Registring Raylib mappings. 103 " -v,--version show version\n",
101 lua_register(L, "open_window", l_open_window); 104 argv0);
102 lua_register(L, "close_window", l_close_window); 105}
103 lua_register(L, "window_running", l_window_running); 106
104 lua_register(L, "begin_drawing", l_begin_drawing); 107static void version(const char *argv0) {
105 lua_register(L, "end_drawing", l_end_drawing); 108 printf("%s version %s\n", argv0, VERSION);
106 lua_register(L, "set_fps", l_set_fps); 109}
107 lua_register(L, "draw_fps", l_draw_fps); 110
108 lua_register(L, "clear_window", l_clear_window); 111int main(int argc, char *argv[]) {
109 112 const char short_options[] = "r:bhv";
110 // Loading embeded modules into Lua state. 113 const struct option long_options[] = {
111 if (!load_embedded_module(L, json, json_len, "json")) return 1; 114 { "run", 1, NULL, 'r' },
112 if (!load_embedded_module(L, color, color_len, "color")) return 1; 115 { "bundle", 0, NULL, 'b' },
113 if (!load_embedded_module(L, tilemap, tilemap_len, "tilemap")) return 1; 116 { "help", 0, NULL, 'h' },
114 117 { "version", 0, NULL, 'v' },
115 // Interpreting and running input file Lua script. 118 { 0 },
116 if (luaL_loadfile(L, IN_FILE) || lua_pcall(L, 0, 0, 0)) { 119 };
117 fprintf(stderr, "Error: %s\n", lua_tostring(L, -1)); 120
118 return 1; 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 }
142
143 if (run_file) {
144 SetTraceLogLevel(DEBUG_LEVEL);
145
146 lua_State *L = luaL_newstate();
147 luaL_openlibs(L);
148
149 // Registring Raylib mappings.
150 lua_register(L, "open_window", l_open_window);
151 lua_register(L, "close_window", l_close_window);
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);
158
159 // Loading embeded modules into Lua state.
160 if (!load_embedded_module(L, json, json_len, "json")) return 1;
161 if (!load_embedded_module(L, color, color_len, "color")) 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}