refactor SDL out of core files
This commit is contained in:
parent
82ed6b4ea9
commit
9d183341ae
21 changed files with 1419 additions and 1028 deletions
|
|
@ -7,7 +7,6 @@
|
|||
#include <lauxlib.h>
|
||||
#include <lua.h>
|
||||
#include <lualib.h>
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include "pxl8_macros.h"
|
||||
#include "pxl8_script.h"
|
||||
|
|
@ -222,12 +221,12 @@ const char* pxl8_script_get_last_error(pxl8_script* script) {
|
|||
}
|
||||
|
||||
pxl8_script* pxl8_script_create(void) {
|
||||
pxl8_script* script = SDL_calloc(1, sizeof(pxl8_script));
|
||||
pxl8_script* script = (pxl8_script*)calloc(1, sizeof(pxl8_script));
|
||||
if (!script) return NULL;
|
||||
|
||||
script->L = luaL_newstate();
|
||||
if (!script->L) {
|
||||
SDL_free(script);
|
||||
free(script);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
|
@ -285,7 +284,7 @@ void pxl8_script_destroy(pxl8_script* script) {
|
|||
if (script->L) {
|
||||
lua_close(script->L);
|
||||
}
|
||||
SDL_free(script);
|
||||
free(script);
|
||||
}
|
||||
|
||||
void pxl8_script_set_gfx(pxl8_script* script, pxl8_gfx* gfx) {
|
||||
|
|
@ -600,3 +599,175 @@ bool pxl8_script_check_reload(pxl8_script* script) {
|
|||
|
||||
return false;
|
||||
}
|
||||
|
||||
#include <pthread.h>
|
||||
#include <string.h>
|
||||
#include <linenoise.h>
|
||||
|
||||
#define PXL8_MAX_REPL_COMMANDS 4096
|
||||
|
||||
struct pxl8_script_repl_command {
|
||||
char buffer[PXL8_MAX_REPL_COMMANDS];
|
||||
struct pxl8_script_repl_command* next;
|
||||
};
|
||||
|
||||
struct pxl8_script_repl {
|
||||
pthread_t thread;
|
||||
pthread_mutex_t mutex;
|
||||
pxl8_script_repl_command* queue_head;
|
||||
pxl8_script_repl_command* queue_tail;
|
||||
bool running;
|
||||
};
|
||||
|
||||
static void pxl8_script_repl_completion(const char* buf, linenoiseCompletions* lc) {
|
||||
const char* fennel_keywords[] = {
|
||||
"fn", "let", "var", "set", "global", "local",
|
||||
"if", "when", "do", "while", "for", "each",
|
||||
"lambda", "λ", "partial", "macro", "macros",
|
||||
"require", "include", "import-macros",
|
||||
"values", "select", "table", "length",
|
||||
".", "..", ":", "->", "->>", "-?>", "-?>>",
|
||||
"doto", "match", "case", "pick-values",
|
||||
"collect", "icollect", "accumulate"
|
||||
};
|
||||
|
||||
const char* pxl8_functions[] = {
|
||||
"pxl8.clr", "pxl8.pixel", "pxl8.get_pixel",
|
||||
"pxl8.line", "pxl8.rect", "pxl8.rect_fill",
|
||||
"pxl8.circle", "pxl8.circle_fill", "pxl8.text",
|
||||
"pxl8.get_screen", "pxl8.info", "pxl8.warn",
|
||||
"pxl8.error", "pxl8.debug", "pxl8.trace"
|
||||
};
|
||||
|
||||
size_t buf_len = strlen(buf);
|
||||
|
||||
for (size_t i = 0; i < sizeof(fennel_keywords) / sizeof(fennel_keywords[0]); i++) {
|
||||
if (strncmp(buf, fennel_keywords[i], buf_len) == 0) {
|
||||
linenoiseAddCompletion(lc, fennel_keywords[i]);
|
||||
}
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < sizeof(pxl8_functions) / sizeof(pxl8_functions[0]); i++) {
|
||||
if (strncmp(buf, pxl8_functions[i], buf_len) == 0) {
|
||||
linenoiseAddCompletion(lc, pxl8_functions[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static char* pxl8_script_repl_hints(const char* buf, int* color, int* bold) {
|
||||
if (strncmp(buf, "pxl8.", 5) == 0 && strlen(buf) == 5) {
|
||||
*color = 35;
|
||||
*bold = 0;
|
||||
return "clr|pixel|line|rect|circle|text|get_screen";
|
||||
}
|
||||
|
||||
if (strcmp(buf, "(fn") == 0) {
|
||||
*color = 36;
|
||||
*bold = 0;
|
||||
return " [args] body)";
|
||||
}
|
||||
|
||||
if (strcmp(buf, "(let") == 0) {
|
||||
*color = 36;
|
||||
*bold = 0;
|
||||
return " [bindings] body)";
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void* pxl8_script_repl_stdin_thread(void* user_data) {
|
||||
pxl8_script_repl* repl = (pxl8_script_repl*)user_data;
|
||||
char* line;
|
||||
const char* history_file = ".pxl8_history";
|
||||
|
||||
linenoiseHistorySetMaxLen(100);
|
||||
linenoiseSetMultiLine(1);
|
||||
linenoiseSetCompletionCallback(pxl8_script_repl_completion);
|
||||
linenoiseSetHintsCallback(pxl8_script_repl_hints);
|
||||
linenoiseHistoryLoad(history_file);
|
||||
|
||||
while (repl->running && (line = linenoise(">> "))) {
|
||||
if (strlen(line) > 0) {
|
||||
linenoiseHistoryAdd(line);
|
||||
linenoiseHistorySave(history_file);
|
||||
|
||||
pxl8_script_repl_command* cmd = (pxl8_script_repl_command*)malloc(sizeof(pxl8_script_repl_command));
|
||||
if (cmd) {
|
||||
strncpy(cmd->buffer, line, PXL8_MAX_REPL_COMMANDS - 1);
|
||||
cmd->buffer[PXL8_MAX_REPL_COMMANDS - 1] = '\0';
|
||||
cmd->next = NULL;
|
||||
|
||||
pthread_mutex_lock(&repl->mutex);
|
||||
if (repl->queue_tail) {
|
||||
repl->queue_tail->next = cmd;
|
||||
repl->queue_tail = cmd;
|
||||
} else {
|
||||
repl->queue_head = repl->queue_tail = cmd;
|
||||
}
|
||||
pthread_mutex_unlock(&repl->mutex);
|
||||
}
|
||||
}
|
||||
linenoiseFree(line);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
pxl8_script_repl* pxl8_script_repl_create(void) {
|
||||
pxl8_script_repl* repl = (pxl8_script_repl*)calloc(1, sizeof(pxl8_script_repl));
|
||||
return repl;
|
||||
}
|
||||
|
||||
void pxl8_script_repl_destroy(pxl8_script_repl* repl) {
|
||||
if (!repl) return;
|
||||
free(repl);
|
||||
}
|
||||
|
||||
void pxl8_script_repl_init(pxl8_script_repl* repl) {
|
||||
if (!repl) return;
|
||||
|
||||
repl->queue_head = NULL;
|
||||
repl->queue_tail = NULL;
|
||||
repl->running = true;
|
||||
pthread_mutex_init(&repl->mutex, NULL);
|
||||
pthread_create(&repl->thread, NULL, pxl8_script_repl_stdin_thread, repl);
|
||||
}
|
||||
|
||||
void pxl8_script_repl_shutdown(pxl8_script_repl* repl) {
|
||||
if (!repl) return;
|
||||
|
||||
repl->running = false;
|
||||
pthread_join(repl->thread, NULL);
|
||||
pthread_mutex_destroy(&repl->mutex);
|
||||
|
||||
pxl8_script_repl_command* cmd = repl->queue_head;
|
||||
while (cmd) {
|
||||
pxl8_script_repl_command* next = cmd->next;
|
||||
free(cmd);
|
||||
cmd = next;
|
||||
}
|
||||
}
|
||||
|
||||
pxl8_script_repl_command* pxl8_script_repl_pop_command(pxl8_script_repl* repl) {
|
||||
if (!repl) return NULL;
|
||||
|
||||
pthread_mutex_lock(&repl->mutex);
|
||||
pxl8_script_repl_command* cmd = repl->queue_head;
|
||||
if (cmd) {
|
||||
repl->queue_head = cmd->next;
|
||||
if (!repl->queue_head) {
|
||||
repl->queue_tail = NULL;
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&repl->mutex);
|
||||
return cmd;
|
||||
}
|
||||
|
||||
const char* pxl8_script_repl_command_buffer(pxl8_script_repl_command* cmd) {
|
||||
return cmd ? cmd->buffer : NULL;
|
||||
}
|
||||
|
||||
void pxl8_script_repl_command_free(pxl8_script_repl_command* cmd) {
|
||||
free(cmd);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue