Proper logging using scheme; implemented most MRB FFI in scheme

This commit is contained in:
John Alanbrook 2022-12-15 23:30:22 +00:00
parent 688d816bce
commit fa8dfaf8e8
11 changed files with 212 additions and 142 deletions

View file

@ -62,8 +62,8 @@ COMPILER_FLAGS = $(includeflag) $(QFLAGS) -MD $(WARNING_FLAGS) -DVER=\"$(VER)\"
LIBPATH = -L$(BIN)
ifeq ($(OS), WIN32)
LINKER_FLAGS = $(QFLAGS)
ELIBS = engine ucrt pthread yughc portaudio glfw3 opengl32 gdi32 ws2_32 ole32 winmm setupapi m
LINKER_FLAGS = $(QFLAGS) -static
ELIBS = engine ucrt yughc portaudio glfw3 opengl32 gdi32 ws2_32 ole32 winmm setupapi m
CLIBS =
else
LINKER_FLAGS = $(QFLAGS) -L/usr/local/lib
@ -72,8 +72,7 @@ else
endif
ELIBS != $(call prefix, $(ELIBS), -l)
ELIBS := $(CLIBS) $(ELIBS)
CLIBS != $(call prefix, $(CLIBS), -l);
objects = $(eobjects)
DEPENDS = $(objects:.o=.d)
@ -97,7 +96,7 @@ $(BIN)$(NAME): $(objprefix)/source/engine/yugine.o $(ENGINE)
$(CC) $< $(LINK) -o $(BIN)$(NAME)
@echo Finished build
$(BIN)$(DIST): $(BIN)$(NAME)
$(BIN)$(DIST): $(BIN)$(NAME) source/scripts/* source/shaders/*
@echo Creating distribution $(DIST)
@mkdir -p $(BIN)dist
@cp $(BIN)$(NAME) $(BIN)dist

View file

@ -11,9 +11,9 @@
//char *logstr[] = { "INFO", "WARN", "\x1b[1;31mERROR\x1b[0m", "CRITICAL" };
char *logstr[] = { "INFO", "WARN", "ERROR", "CRITICAL" };
char *catstr[] = {"ENGINE"};
char *catstr[] = {"ENGINE", "SCRIPT"};
FILE *logfile = NULL;
void mYughLog(int category, int priority, int line, const char *file, const char *message, ...)
{
@ -29,17 +29,23 @@ void mYughLog(int category, int priority, int line, const char *file, const char
va_end(args);
char buffer[ERROR_BUFFER] = { '\0' };
snprintf(buffer, ERROR_BUFFER, "%s | %s | %s [ %s:%d ] %s\n", logstr[priority], catstr[0], dt, file, line, msgbuffer);
snprintf(buffer, ERROR_BUFFER, "%s | %s | %s [ %s:%d ] %s\n", logstr[priority], catstr[category], dt, file, line, msgbuffer);
fprintf(stderr, "%s", buffer);
fprintf(stdout, "%s", buffer);
fflush(stdout);
fflush(stderr);
if (logfile) {
fprintf(logfile, "%s", buffer);
fflush(logfile);
}
}
}
void log_setfile(char *file) {
YughInfo("Opening output log %s.", file);
freopen(file, "w", stderr);
freopen(file, "w", stdout);
}
void log_cat(FILE *f) {

View file

@ -337,6 +337,7 @@ static void edit_input_cb(GLFWwindow *w, int key, int scancode, int action, int
break;
case GLFW_KEY_F5:
break;
case GLFW_KEY_F6:
@ -348,7 +349,7 @@ static void edit_input_cb(GLFWwindow *w, int key, int scancode, int action, int
break;
case GLFW_KEY_F8:
//NEGATE(editor.debug.show);
NEGATE(editor.debug.show);
break;
case GLFW_KEY_F9:
@ -535,10 +536,10 @@ void editor_project_gui() {
NK_MENU_END()
NK_MENU_START(export)
nk_layout_row_dynamic(ctx, 25,2);
if (nk_button_label(ctx, "Bake")) {
nuke_nel(2);
if (nuke_btn("Bake")) {
}
if (nk_button_label(ctx, "Build")) {
if (nuke_btn("Build")) {
}
NK_MENU_END()
@ -550,8 +551,7 @@ void editor_project_gui() {
// 126, nk_filter_default);
if (nk_tree_push(ctx, NK_TREE_NODE, "Physics", NK_MINIMIZED)) {
nk_property_float(ctx, "2d Gravity", -5000.f, &phys2d_gravity, 0.f, 1.f,
0.1f);
nuke_prop_float("2d Gravity", -5000.f, &phys2d_gravity, 0.f, 1.f, 0.1f);
phys2d_apply();
nk_tree_pop(ctx);
}
@ -563,8 +563,8 @@ void editor_project_gui() {
NK_MENU_END()
NK_MENU_START(stats)
nk_labelf(ctx, NK_TEXT_LEFT, "FPS: %2.4f", 1.f / deltaT);
nk_labelf(ctx, NK_TEXT_LEFT, "Triangles rendered: %llu", triCount);
nuke_labelf("FPS: %2.4f", 1.f / deltaT);
nuke_labelf("Triangles rendered: %llu", triCount);
NK_MENU_END()
NK_MENU_START(repl)
@ -584,7 +584,7 @@ void editor_project_gui() {
NK_MENU_END()
NK_MENU_START(hierarchy)
nk_layout_row_dynamic(ctx, 25, 1);
nuke_nel(1);
if (nuke_btn("New Object")) {
MakeGameobject();
@ -595,15 +595,15 @@ void editor_project_gui() {
NK_MENU_END()
NK_FORCE(simulate)
nk_layout_row_dynamic(ctx, 25, 2);
nuke_nel(2);
if (physOn) {
if (nk_button_label(ctx, "Pause"))
if (nuke_btn("Pause"))
game_pause();
if (nk_button_label(ctx, "Stop"))
if (nuke_btn("Stop"))
game_stop();
} else {
if (nk_button_label(ctx, "Play"))
if (nuke_btn("Play"))
game_start();
}
@ -611,7 +611,6 @@ void editor_project_gui() {
NK_FORCE(prefab)
nuke_nel(1);
nk_layout_row_dynamic(ctx, 25, 1);
vec_walk(prefabs, editor_prefab_btn);
NK_FORCE_END()
@ -648,21 +647,21 @@ void editor_project_gui() {
nk_tree_pop(ctx);
}
if (nk_button_label(ctx, "Reload Shaders")) {
if (nuke_btn("Reload Shaders")) {
shader_compile_all();
}
nk_property_int(ctx, "Grid 1 Span", 1, &grid1_span, 500, 1, 1);
nk_checkbox_label(ctx, "Draw", &grid1_draw);
nuke_property_int("Grid 1 Span", 1, &grid1_span, 500, 1);
nuke_checkbox("Draw", &grid1_draw);
nk_property_int(ctx, "Grid 2 Span", 10, &grid2_span, 1000, 1, 1);
nk_checkbox_label(ctx, "Draw", &grid2_draw);
nuke_property_int("Grid 2 Span", 10, &grid2_span, 1000, 1);
nuke_checkbox("Draw", &grid2_draw);
nk_property_float(ctx, "Grid Opacity", 0.f, &gridOpacity, 1.f, 0.01f, 0.01f);
nk_property_float(ctx, "Small unit", 0.5f, &smallGridUnit, 5.f, 0.1f, 0.1f);
nk_property_float(ctx, "Big unit", 10.f, &bigGridUnit, 50.f, 1.f, 0.1f);
nk_property_float(ctx, "Small thickness", 1.f, &gridSmallThickness, 10.f, 0.1f, 0.1f);
nk_property_float(ctx, "Big thickness", 1.f, &gridBigThickness, 10.f, 0.1f, 0.1f);
nuke_property_float("Grid Opacity", 0.f, &gridOpacity, 1.f, 0.01f, 0.01f);
nuke_property_float("Small unit", 0.5f, &smallGridUnit, 5.f, 0.1f, 0.1f);
nuke_property_float("Big unit", 10.f, &bigGridUnit, 50.f, 1.f, 0.1f);
nuke_property_float("Small thickness", 1.f, &gridSmallThickness, 10.f, 0.1f, 0.1f);
nuke_property_float("Big thickness", 1.f, &gridBigThickness, 10.f, 0.1f, 0.1f);
static struct nk_colorf smgrd;
static struct nk_colorf lgrd;
@ -680,12 +679,13 @@ startobjectgui:
NK_FORCE(gameobject)
nk_layout_row_dynamic(ctx, 30, 3);
nuke_nel(3);
if (nk_button_label(ctx, "Save"))
if (nuke_btn("Save"))
gameobject_saveprefab(selectedobject);
if (nk_button_label(ctx, "Del")) {
if (nuke_btn("Del")) {
gameobject_delete(selected_index);
pickGameObject(-1);
nk_end(ctx);
@ -695,19 +695,20 @@ startobjectgui:
if (selectedobject->editor.prefabSync && nk_button_label(ctx, "Revert"))
gameobject_revertprefab(selectedobject);
nk_label(ctx, "Name", NK_TEXT_LEFT);
nuke_label("Name");
nk_edit_string_zero_terminated(ctx, NK_EDIT_SIMPLE, selectedobject->editor.mname, 50, nk_filter_ascii);
nk_label(ctx, "Prefab", NK_TEXT_LEFT);
nuke_label("Prefab");
nk_edit_string_zero_terminated(ctx, NK_EDIT_SIMPLE, selectedobject->editor.prefabName, 50, nk_filter_ascii);
object_gui(selectedobject);
// nuke_label("Components");
nk_layout_row_dynamic(ctx,25,3);
nuke_nel(3);
for (int i = 0; i < ncomponent; i++) {
if (nk_button_label(ctx, components[i].name)) {
if (nuke_btn(components[i].name)) {
gameobject_addcomponent(selectedobject, &components[i]);
}
}
@ -716,9 +717,10 @@ startobjectgui:
/*
NK_FORCE(components)
nk_layout_row_dynamic(ctx,25,1);
nuke_nel(1);
for (int i = 0; i < ncomponent; i++) {
if (nk_button_label(ctx, components[i].name)) {
if (nuke_btn(components[i].name)) {
gameobject_addcomponent(selectedobject, &components[i]);
}
}
@ -750,7 +752,7 @@ int is_allowed_extension(const char *ext) {
}
void editor_level_btn(char *level) {
if (nk_button_label(ctx, level)) {
if (nuke_btn(level)) {
load_level(level);
strcpy(current_level, level);
}
@ -892,7 +894,7 @@ void editor_asset_text_gui(char *text) {
editor.text_ed = nk_edit_string_zero_terminated(ctx, NK_EDIT_BOX, text, ASSET_TEXT_BUF, nk_filter_ascii);
nuke_nel(4);
if (nk_button_label(ctx, "Save")) {
if (nuke_btn("Save")) {
FILE *f = fopen(selected_asset->filename, "wd");
size_t len = strlen(text);
fwrite(text, len, 1, f);

View file

@ -1,7 +1,7 @@
#ifndef EDITOR_H
#define EDITOR_H
#include <config.h>
#include "config.h"
#include <stdbool.h>
#include "resources.h"

View file

@ -10,8 +10,12 @@
#include "engine.h"
#include "log.h"
#include "s7.h"
#include "nuke.h"
extern s7_scheme *s7;
/* FFI */
s7_pointer s7_ui_label(s7_scheme *sc, s7_pointer args) {
if (s7_is_string(s7_car(args))) {
@ -46,6 +50,75 @@ s7_pointer s7_ui_text(s7_scheme *sc, s7_pointer args) {
return s7_make_string(sc, str);
}
s7_pointer s7_settings_cmd(s7_scheme *sc, s7_pointer args) {
int cmd = s7_integer(s7_car(args));
double val = s7_real(s7_cadr(args));
YughInfo("Changing a setting.");
switch(cmd) {
case 0: // render fps
renderMS = (double)val;
break;
case 1:
updateMS = (double)val;
break;
case 2:
physMS = (double)val;
break;
}
return args;
}
s7_pointer s7_log(s7_scheme *sc, s7_pointer args) {
int lvl = s7_integer(s7_car(args));
const char *msg = s7_string(s7_object_to_string(sc, s7_cadr(args), 0));
const char *file = s7_string(s7_caddr(args));
int line = s7_integer(s7_cadddr(args));
mYughLog(1, lvl, line, file, msg);
//YughInfo(s7_string(s7_object_to_string(sc, s7_car(args), 0)));
return args;
}
/* Call like (ui_rendertext "string" (xpos ypos) size) */
s7_pointer s7_ui_rendertext(s7_scheme *sc, s7_pointer args) {
const char *s = s7_string(s7_car(args));
double pos[2];
pos[0] = s7_real(s7_car(s7_cadr(args)));
pos[1] = s7_real(s7_cadr(s7_cadr(args)));
double size = s7_real(s7_caddr(args));
double white[3] = {1.f, 1.f, 1.f};
renderText(s, pos, size, white, 0);
return args;
}
s7_pointer s7_win_cmd(s7_scheme *sc, s7_pointer args) {
int win = s7_integer(s7_car(args));
int cmd = s7_integer(s7_cadr(args));
struct window *w = window_i(win);
switch (cmd) {
case 0: // toggle fullscreen
window_togglefullscreen(w);
break;
case 1: // Fullscreen on
window_makefullscreen(w);
break;
case 2: // Fullscreen off
window_unfullscreen(w);
break;
}
return args;
}
/*
mrb_value mrb_ui_begin(mrb_state *mrb, mrb_value self) {
@ -60,47 +133,12 @@ mrb_value mrb_ui_begin(mrb_state *mrb, mrb_value self) {
*/
/*
mrb_value mrb_ui_rendertext(mrb_state *mrb, mrb_value self) {
char *s;
mrb_float pos[2];
mrb_float size, ol;
mrb_get_args(mrb, "zffff", &s, &pos[0], &pos[1], &size, &ol);
static float white[3] = {1.f, 1.f, 1.f};
float fpos[2] = {(float)pos[0], (float)pos[1]};
renderText(s, fpos, size, white, ol);
return self;
}
mrb_value mrb_win_make(mrb_state *mrb, mrb_value self) {
char name[50] = "New Window";
struct window *new = MakeSDLWindow(name, 500, 500, 0);
return mrb_float_value(mrb, new->id);
}
mrb_value mrb_win_cmd(mrb_state *mrb, mrb_value self) {
mrb_float win, cmd;
mrb_get_args(mrb, "ff", &win, &cmd);
struct window *new = window_i(win);
switch ((int)cmd)
{
case 0: // toggle fullscreen
window_togglefullscreen(new);
break;
case 1: // Fullscreen on
window_makefullscreen(new);
break;
case 2: // Fullscreen off
window_unfullscreen(new);
break;
}
return self;
}
mrb_value mrb_nuke_cb(mrb_state *mrb, mrb_value self) {
mrb_float win;
mrb_sym cb;
@ -148,27 +186,6 @@ mrb_value mrb_sound_cmd(mrb_state *mrb, mrb_value self) {
return self;
}
mrb_value mrb_settings_cmd(mrb_state *mrb, mrb_value self) {
mrb_float cmd, val;
mrb_get_args(mrb, "ff", &cmd, &val);
switch((int)cmd)
{
case 0: // render fps
renderMS = (double)val;
break;
case 1:
updateMS = (double)val;
break;
case 2:
physMS = (double)val;
break;
}
return self;
}
mrb_value mrb_editor_render(mrb_state *mrb, mrb_value self) {
editor_render();
@ -176,44 +193,17 @@ mrb_value mrb_editor_render(mrb_state *mrb, mrb_value self) {
}
*/
//#define MRB_FUNC(NAME, ARGS) mrb_define_method(mrb, mrb->object_class, #NAME, mrb_ ## NAME, ARGS)
#define S7_FUNC(NAME, ARGS) s7_define_function(s7, #NAME, s7_ ##NAME, ARGS, 0, 0, "")
void ffi_load() {
//s7_define_function(s7, "ui_label", s7_ui_label, 1, 0, 0, "Draw UI label with given string");
S7_FUNC(ui_label, 1);
S7_FUNC(ui_btn, 1);
S7_FUNC(ui_nel, 1);
S7_FUNC(ui_prop, 6);
S7_FUNC(ui_text, 2);
/*
MRB_FUNC(load, MRB_ARGS_REQ(1));
MRB_FUNC(ui_label, MRB_ARGS_REQ(1));
MRB_FUNC(ui_btn, MRB_ARGS_REQ(1));
MRB_FUNC(ui_nel, MRB_ARGS_REQ(2));
MRB_FUNC(ui_begin, MRB_ARGS_REQ(3));
MRB_FUNC(ui_prop, MRB_ARGS_REQ(6));
MRB_FUNC(ui_text, MRB_ARGS_REQ(2));
MRB_FUNC(ui_rendertext, MRB_ARGS_REQ(5));
MRB_FUNC(c_reload, MRB_ARGS_REQ(1));
MRB_FUNC(win_make, MRB_ARGS_REQ(1));
MRB_FUNC(win_cmd, MRB_ARGS_REQ(2));
MRB_FUNC(nuke_cb, MRB_ARGS_REQ(2));
MRB_FUNC(gui_cb, MRB_ARGS_REQ(2));
MRB_FUNC(sound_make, MRB_ARGS_REQ(1));
MRB_FUNC(sound_cmd, MRB_ARGS_REQ(2));
MRB_FUNC(editor_render, MRB_ARGS_REQ(0));
MRB_FUNC(settings_cmd, MRB_ARGS_REQ(2));
*/
S7_FUNC(ui_label, 1);
S7_FUNC(ui_btn, 1);
S7_FUNC(ui_nel, 1);
S7_FUNC(ui_prop, 6);
S7_FUNC(ui_text, 2);
S7_FUNC(settings_cmd, 2);
S7_FUNC(win_cmd, 2);
S7_FUNC(ui_rendertext, 3);
S7_FUNC(log, 4);
}

View file

@ -60,6 +60,10 @@ void nk_property_float2(struct nk_context *ctx, const char *label, float min, fl
nk_property_float(ctx, "#Y", min, &val[1], max, step, dragstep);
}
void nuke_property_float(const char *lbl, float min, float *val, float max, float step, float dragstep) {
nk_property_float(ctx, lbl, min, val, max, step, dragstep);
}
int nuke_btn(const char *lbl) {
return nk_button_label(ctx, lbl);
}

View file

@ -14,6 +14,7 @@ void nuke_end();
void nk_property_float3(struct nk_context *ctx, const char *label, float min, float *val, float max, float step, float dragstep);
void nk_property_float2(struct nk_context *ctx, const char *label, float min, float *val, float max, float step, float dragstep);
void nuke_property_float(const char *lbl, float min, float *val, float max, float step, float dragstep);
void nuke_property_int(const char *lbl, int min, int *val, int max, int step);
void nk_radio_button_label(struct nk_context *ctx, const char *label, int *val, int cmp);

View file

@ -7,8 +7,22 @@
s7_scheme *s7 = NULL;
static void my_print(s7_scheme *sc, uint8_t c, s7_pointer port) {
static char buffer[1024];
static char *p = buffer;
if (c != '\0' && p != &buffer[1023]) {
*p = c;
p++;
} else {
YughInfo(buffer);
p = buffer;
}
}
void script_init() {
s7 = s7_init();
s7_set_current_error_port(s7, s7_open_output_string(s7));
s7_set_current_output_port(s7, s7_open_output_function(s7, my_print));
ffi_load();
}
@ -17,7 +31,21 @@ void script_run(const char *script) {
}
int script_dofile(const char *file) {
s7_load(s7, file);
/* static char fload[512];
snprintf(fload, 512, "(write (load \"%s\"))", file);
s7_eval_c_string(s7, fload);
*/
if (!s7_load(s7, file)) {
YughError("Can't find file %s.", file);
return 1;
}
const char *errmsg = s7_get_output_string(s7, s7_current_error_port(s7));
if (errmsg && (*errmsg))
mYughLog(1, 2, 0, "script", "Scripting error: %s", errmsg);
return 0;
}

View file

@ -2,7 +2,6 @@
#define SCRIPT_H
#include "s7.h"
extern s7_scheme *s7;
void script_init();
void script_run(const char *script);

View file

@ -57,6 +57,8 @@ void seghandle(int sig) {
}
int main(int argc, char **args) {
int logout = 1;
for (int i = 1; i < argc; i++) {
if (args[i][0] == '-') {
switch(args[i][1]) {
@ -91,11 +93,15 @@ int main(int argc, char **args) {
exit(0);
break;
case 'c':
logout = 0;
break;
}
}
}
if (DBG) {
if (DBG && logout) {
time_t now = time(NULL);
char fname[100];
snprintf(fname, 100, "yugine-%d.log", now);

View file

@ -0,0 +1,35 @@
(define-macro (time expr)
`(let ((start (*s7* 'cpu-time)))
(let ((res (list ,expr)))
(list (car res)
(- (*s7* 'cpu-time) start)))))
(define-macro (funcinfo func info)
`(*function* (funclet ,func) ,info))
(define-macro (funcloc func)
`(format #f "~A:~A" (funcinfo ,func 'file) (funcinfo ,func 'line)))
(define-macro (funcsrc func)
`(funcinfo ,func 'source))
(define-macro (glog data lvl)
(let ((f (gensym)))
`(begin
(define (,f) ())
(log ,lvl ,data (funcinfo ,f 'file) (funcinfo ,f 'line)))))
(define (loginfo data) (glog data 0))
(define (logwarn data) (glog data 1))
(define (logerr data) (glog data 2))
(define (logcrit data) (glog data 3))
(define (set_fps fps) (settings_cmd 0 (/ 1 fps)))
(define (set_update fps) (settings_cmd 1 (/ 1 fps)))
(define (set_phys fps) (settings_cmd 2 (/ 1 fps)))
(define (win_fulltoggle w) (win_cmd w 0))
(define (win_fullscreen w) (win_cmd w 1))
(define (win_unfullscreen w) (win_cmd w 2))