feck/src/main.c

106 lines
2.3 KiB
C
Raw Normal View History

2021-11-18 00:11:52 +01:00
#include "fe.h"
2021-11-18 11:53:47 +01:00
#include "file.h"
2021-11-18 01:07:06 +01:00
#include "raygint/display.h"
#include <stdlib.h>
2021-11-18 11:53:47 +01:00
#define UNUSED(c) (void)(c)
2021-11-18 01:15:26 +01:00
static fe_Object *f_dblock(fe_Context *ctx, fe_Object *arg);
2021-11-18 01:07:06 +01:00
static fe_Object *f_dcolor(fe_Context *ctx, fe_Object *arg);
static fe_Object *f_dclear(fe_Context *ctx, fe_Object *arg);
static fe_Object *f_drect(fe_Context *ctx, fe_Object *arg);
2021-11-18 11:53:47 +01:00
static char read_custom(fe_Context *ctx, void *udata);
2021-11-18 01:07:06 +01:00
static int r = 0;
static int g = 0;
static int b = 0;
2021-11-18 00:11:52 +01:00
int
main(void)
{
const int size = 0xffffff;
void *data = malloc(size);
fe_Context *ctx = fe_open(data, size);
int gc = fe_savegc(ctx);
2021-11-18 01:07:06 +01:00
2021-11-18 01:15:26 +01:00
fe_set(ctx, fe_symbol(ctx, "dblock"), fe_cfunc(ctx, f_dblock));
2021-11-18 01:07:06 +01:00
fe_set(ctx, fe_symbol(ctx, "dcolor"), fe_cfunc(ctx, f_dcolor));
fe_set(ctx, fe_symbol(ctx, "dclear"), fe_cfunc(ctx, f_dclear));
fe_set(ctx, fe_symbol(ctx, "drect"), fe_cfunc(ctx, f_drect));
2021-11-18 00:11:52 +01:00
2021-11-18 11:53:47 +01:00
rDisplayInit();
#ifdef RAYLIB
SetTargetFPS(30);
#endif
2021-11-18 00:11:52 +01:00
for (;;) {
2021-11-18 11:53:47 +01:00
fe_Object *obj = fe_read(ctx, read_custom, test_fe);
2021-11-18 00:11:52 +01:00
/* break if there's nothing left to read */
if (!obj)
break;
/* evaluate read object */
fe_eval(ctx, obj);
/* restore GC stack which would now contain both the
* read object and result from evaluation */
fe_restoregc(ctx, gc);
}
2021-11-18 01:07:06 +01:00
rDisplayDeinit();
2021-11-18 00:11:52 +01:00
fe_close(ctx);
free(data);
return 0;
}
2021-11-18 01:07:06 +01:00
static fe_Object *
2021-11-18 01:15:26 +01:00
f_dblock(fe_Context *ctx, fe_Object *arg)
2021-11-18 01:07:06 +01:00
{
rDrawBegin();
2021-11-18 01:15:26 +01:00
while (fe_nextarg(ctx, &arg))
;
2021-11-18 01:07:06 +01:00
rDrawEnd();
return NULL;
}
static fe_Object *
f_dcolor(fe_Context *ctx, fe_Object *arg)
{
r = fe_tonumber(ctx, fe_nextarg(ctx, &arg));
g = fe_tonumber(ctx, fe_nextarg(ctx, &arg));
b = fe_tonumber(ctx, fe_nextarg(ctx, &arg));
return NULL;
}
static fe_Object *
f_dclear(fe_Context *ctx, fe_Object *arg)
{
2021-11-18 11:53:47 +01:00
UNUSED(ctx);
UNUSED(arg);
2021-11-18 01:07:06 +01:00
dclear(C_RGB(r, g, b));
return NULL;
}
static fe_Object *
f_drect(fe_Context *ctx, fe_Object *arg)
{
const int x = fe_tonumber(ctx, fe_nextarg(ctx, &arg));
const int y = fe_tonumber(ctx, fe_nextarg(ctx, &arg));
const int w = fe_tonumber(ctx, fe_nextarg(ctx, &arg));
const int h = fe_tonumber(ctx, fe_nextarg(ctx, &arg));
drect(x, y, x + w - 1, y + h - 1, C_RGB(r, g, b));
return NULL;
}
2021-11-18 11:53:47 +01:00
static char
read_custom(fe_Context *ctx, void *udata)
{
static int cursor = 0;
const char *data = (char *)udata;
UNUSED(ctx);
if (data[cursor])
return data[cursor++];
return data[cursor];
}