summaryrefslogtreecommitdiff
path: root/main.c
blob: b6602a0fdd5ac24ed55043811de047293ab2e6d5 (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
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
#include <stdio.h>

#include "raylib.h"
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"

#include "stdlib/json.h"
#include "stdlib/color.h"
#include "stdlib/tilemap.h"

#define IN_FILE "test/main.lua"
#define DEBUG_LEVEL LOG_DEBUG

static int lua_getfield_int(lua_State *L, int index, const char *key) {
	lua_getfield(L, index, key);
	int val = (int)luaL_checknumber(L, -1);
	lua_pop(L, 1);
	return val;
}

static int lua_getfield_int_opt(lua_State *L, int index, const char *key, int def) {
	lua_getfield(L, index, key);
	int val = lua_isnil(L, -1) ? def : (int)luaL_checknumber(L, -1);
	lua_pop(L, 1);
	return val;
}

static int l_open_window(lua_State *L) {
	int width = luaL_checknumber(L, 1);
	int height = luaL_checknumber(L, 2);
	const char *title = luaL_checkstring(L, 3);
	SetConfigFlags(FLAG_WINDOW_RESIZABLE | FLAG_VSYNC_HINT | FLAG_WINDOW_HIGHDPI);
	InitWindow(width, height, title);
	TraceLog(LOG_DEBUG, "l_open_window");
	return 0;
}

// TODO: This function name is still a bit sus. Revisit the name later.
static int l_window_running(lua_State *L) {
	lua_pushboolean(L, !WindowShouldClose());
	return 1;
}

static int l_set_fps(lua_State *L) {
	int fps = luaL_checknumber(L, 1);
	SetTargetFPS(fps);
	TraceLog(LOG_DEBUG, "l_set_fps");
	return 0;
}

static int l_close_window(lua_State *L) {
	CloseWindow();
	TraceLog(LOG_DEBUG, "l_close_window");
	return 0;
}

static int l_begin_drawing(lua_State *L) {
	BeginDrawing();
	return 0;
}

static int l_end_drawing(lua_State *L) {
	EndDrawing();
	return 0;
}

static int l_clear_window(lua_State *L) {
	luaL_checktype(L, 1, LUA_TTABLE);
	Color color = {
		.r = (unsigned char)lua_getfield_int(L, 1, "r"),
		.g = (unsigned char)lua_getfield_int(L, 1, "g"),
		.b = (unsigned char)lua_getfield_int(L, 1, "b"),
		.a = (unsigned char)lua_getfield_int_opt(L, 1, "a", 255)
	};
	ClearBackground(color);
	return 0;
}

static int l_draw_fps(lua_State *L) {
	DrawFPS(GetScreenWidth() - 100, 20); 
	return 0;
}

int main(void) {
	SetTraceLogLevel(DEBUG_LEVEL);

	lua_State *L = luaL_newstate();
	luaL_openlibs(L);

	// Registring Raylib mappings.
	lua_register(L, "open_window", l_open_window);
	lua_register(L, "close_window", l_close_window);
	lua_register(L, "window_running", l_window_running);
	lua_register(L, "begin_drawing", l_begin_drawing);
	lua_register(L, "end_drawing", l_end_drawing);
	lua_register(L, "set_fps", l_set_fps);
	lua_register(L, "draw_fps", l_draw_fps);
	lua_register(L, "clear_window", l_clear_window);

	// Embedding JSON module.
	if (luaL_loadbuffer(L, json, json_len, "json") || lua_pcall(L, 0, 1, 0)) {
		fprintf(stderr, "Error loading json.lua: %s\n", lua_tostring(L, -1));
		lua_close(L);
		return 1;
	}
	lua_setglobal(L, "json");

	// Embedding color module.
	if (luaL_loadbuffer(L, color, color_len, "color") || lua_pcall(L, 0, 1, 0)) {
		fprintf(stderr, "Error loading color.lua: %s\n", lua_tostring(L, -1));
		lua_close(L);
		return 1;
	}
	lua_setglobal(L, "color");

	// Embedding tilemap module.
	if (luaL_loadbuffer(L, tilemap, tilemap_len, "tilemap") || lua_pcall(L, 0, 1, 0)) {
		fprintf(stderr, "Error loading tilemap.lua: %s\n", lua_tostring(L, -1));
		lua_close(L);
		return 1;
	}
	lua_setglobal(L, "tilemap");

	// Interpreting and running input file Lua script.
	if (luaL_loadfile(L, IN_FILE) || lua_pcall(L, 0, 0, 0)) {
		fprintf(stderr, "Error: %s\n", lua_tostring(L, -1));
		return 1;
	}

	lua_close(L);
	return 0;
}