Projects without an explicit frame hook got a bare ScreenName() call in the generated main: no drawing frame and no viewport argument, which did not compile against screens lowered as fn(Rectangle viewport). Wrap the call in BeginDrawing/BeginUIFrame and pass the screen rectangle, mirroring what hosts do around DrawAppScreen. The example font helper is now included in kryon_project.c when the app block asks for the examples font, and the examples link rule compiles kryon_project.c alongside the module so the binaries actually contain main.
347 lines
13 KiB
C
347 lines
13 KiB
C
/*
|
|
* k2c_project.c — emit kryon_project.h/.c after all files lower. The
|
|
* app-host ABI (CreateAppHost/DestroyAppHost + route table) consumed by
|
|
* the preview tool and IDE host builds. Routes auto-collect from public
|
|
* screens (screen/preview/page/frame/scene/fn keywords); explicit route{}
|
|
* blocks are not captured by Kir yet.
|
|
*/
|
|
#include "k2c_lower.h"
|
|
#include "kir.h"
|
|
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <sys/stat.h>
|
|
|
|
typedef struct K2cRoute {
|
|
char id[KIR_NAME_MAX];
|
|
char title[KIR_NAME_MAX];
|
|
char group[KIR_NAME_MAX];
|
|
char source_path[KIR_PATH_MAX];
|
|
const KirModule *m;
|
|
const KirFunction *fn;
|
|
} K2cRoute;
|
|
|
|
#define K2C_ROUTE_MAX 512
|
|
|
|
static void
|
|
mkdir_parent_local(const char *path)
|
|
{
|
|
char tmp[KIR_PATH_MAX];
|
|
size_t i;
|
|
|
|
snprintf(tmp, sizeof(tmp), "%s", path);
|
|
for(i = 1; i < strlen(tmp); i++) {
|
|
if(tmp[i] == '/') {
|
|
tmp[i] = '\0';
|
|
mkdir(tmp, 0755);
|
|
tmp[i] = '/';
|
|
}
|
|
}
|
|
}
|
|
|
|
static void
|
|
c_str(char *dst, size_t dst_size, const char *src)
|
|
{
|
|
size_t n = 0;
|
|
|
|
dst[n++] = '"';
|
|
for(const char *p = src != NULL ? src : "";
|
|
*p != '\0' && n + 3 < dst_size; p++) {
|
|
if(*p == '"' || *p == '\\') {
|
|
dst[n++] = '\\';
|
|
dst[n++] = *p;
|
|
} else if(*p == '\n') {
|
|
dst[n++] = '\\';
|
|
dst[n++] = 'n';
|
|
} else {
|
|
dst[n++] = *p;
|
|
}
|
|
}
|
|
if(n < dst_size)
|
|
dst[n++] = '"';
|
|
dst[n] = '\0';
|
|
}
|
|
|
|
static int
|
|
collect_routes(KirProgram *const *progs, int prog_count,
|
|
K2cRoute *routes, int max_routes)
|
|
{
|
|
int count = 0;
|
|
|
|
for(int i = 0; i < prog_count; i++) {
|
|
const KirProgram *prog = progs[i];
|
|
|
|
if(prog == NULL)
|
|
continue;
|
|
for(int m = 0; m < prog->module_count; m++) {
|
|
const KirModule *mod = &prog->modules[m];
|
|
|
|
for(int j = 0; j < mod->function_count; j++) {
|
|
const KirFunction *fn = &mod->functions[j];
|
|
|
|
/* Legacy route rule: public AND not a colon function
|
|
* (exact_name) — only screen/preview/page keyword screens
|
|
* are routes; colon helpers stay callable but unrouted. */
|
|
if(!fn->is_public || fn->is_colon || fn->name[0] == '\0')
|
|
continue;
|
|
if(count >= max_routes)
|
|
return count;
|
|
snprintf(routes[count].id, sizeof(routes[0].id), "%s",
|
|
fn->name);
|
|
snprintf(routes[count].title, sizeof(routes[0].title), "%s",
|
|
fn->name);
|
|
snprintf(routes[count].group, sizeof(routes[0].group),
|
|
"Project");
|
|
snprintf(routes[count].source_path,
|
|
sizeof(routes[0].source_path), "%s",
|
|
mod->source_path);
|
|
routes[count].m = mod;
|
|
routes[count].fn = fn;
|
|
count++;
|
|
}
|
|
}
|
|
}
|
|
return count;
|
|
}
|
|
|
|
void
|
|
k2c_write_project(KirProgram *const *progs, int prog_count,
|
|
const char *root, const char *out_dir, int no_main)
|
|
{
|
|
K2cRoute routes[K2C_ROUTE_MAX];
|
|
char path[KIR_PATH_MAX];
|
|
const KirModule *appmod = NULL;
|
|
FILE *out;
|
|
int count;
|
|
|
|
if(progs == NULL || prog_count <= 0)
|
|
return;
|
|
count = collect_routes(progs, prog_count, routes, K2C_ROUTE_MAX);
|
|
|
|
/* the one app{} module (for main()) */
|
|
for(int i = 0; i < prog_count && appmod == NULL; i++) {
|
|
if(progs[i] == NULL)
|
|
continue;
|
|
for(int m = 0; m < progs[i]->module_count; m++)
|
|
if(progs[i]->modules[m].app.has_app) {
|
|
appmod = &progs[i]->modules[m];
|
|
break;
|
|
}
|
|
}
|
|
|
|
/* --- kryon_project.h --- */
|
|
snprintf(path, sizeof(path), "%s/kryon_project.h", out_dir);
|
|
mkdir_parent_local(path);
|
|
out = fopen(path, "wb");
|
|
if(out == NULL)
|
|
return;
|
|
fprintf(out, "/* Generated by k2c from project Kry files. */\n");
|
|
fprintf(out, "#ifndef K_KRYON_PROJECT_H\n#define K_KRYON_PROJECT_H\n\n");
|
|
for(int i = 0; i < prog_count; i++) {
|
|
const KirProgram *prog = progs[i];
|
|
|
|
if(prog == NULL)
|
|
continue;
|
|
for(int m = 0; m < prog->module_count; m++) {
|
|
const KirModule *mod = &prog->modules[m];
|
|
char stem[KIR_PATH_MAX];
|
|
size_t n = strlen(mod->source_path);
|
|
|
|
if(n > 4 && strcmp(mod->source_path + n - 4, ".kry") == 0)
|
|
n -= 4;
|
|
if(n >= sizeof(stem))
|
|
n = sizeof(stem) - 1;
|
|
memcpy(stem, mod->source_path, n);
|
|
stem[n] = '\0';
|
|
fprintf(out, "#include \"%s.h\"\n", stem);
|
|
}
|
|
}
|
|
fprintf(out, "\n#endif\n");
|
|
fclose(out);
|
|
|
|
if(count <= 0)
|
|
return;
|
|
|
|
/* --- kryon_project.c --- */
|
|
snprintf(path, sizeof(path), "%s/kryon_project.c", out_dir);
|
|
out = fopen(path, "wb");
|
|
if(out == NULL)
|
|
return;
|
|
fprintf(out, "/* Generated by k2c from project Kry files. */\n");
|
|
fprintf(out, "#include \"kryon_project.h\"\n");
|
|
fprintf(out, "#include \"app_runtime.h\"\n");
|
|
if(appmod != NULL && appmod->app.font_examples)
|
|
fprintf(out, "#include \"kryon_example_font.h\"\n");
|
|
fprintf(out, "\n");
|
|
fprintf(out, "#if defined(__GNUC__) || defined(__clang__)\n");
|
|
fprintf(out, "void *CreateApp(const char *project_path) __attribute__((weak));\n");
|
|
fprintf(out, "void DestroyApp(void *app) __attribute__((weak));\n");
|
|
fprintf(out, "void ApplyRoute(void *app, const AppRouteInfo *route) __attribute__((weak));\n");
|
|
fprintf(out, "void BeginScreenDraw(void *app, Rectangle viewport) __attribute__((weak));\n");
|
|
fprintf(out, "#endif\n\n");
|
|
|
|
for(int i = 0; i < count; i++) {
|
|
char cname[KIR_NAME_MAX * 3];
|
|
char wrapper[64];
|
|
|
|
snprintf(wrapper, sizeof(wrapper), "kryon_project_draw_%d", i);
|
|
k2c_function_c_name(routes[i].m, routes[i].fn, cname, sizeof(cname));
|
|
fprintf(out, "static void\n%s(void *app, Rectangle viewport)\n{\n",
|
|
wrapper);
|
|
fprintf(out, " if(BeginScreenDraw != 0)\n");
|
|
fprintf(out, " BeginScreenDraw(app, viewport);\n");
|
|
if(strstr(routes[i].fn->args, "Rectangle") != NULL)
|
|
fprintf(out, " (void)app;\n %s(viewport);\n", cname);
|
|
else if(routes[i].fn->args[0] != '\0')
|
|
fprintf(out, " (void)viewport;\n %s(app);\n", cname);
|
|
else
|
|
fprintf(out, " (void)app;\n (void)viewport;\n %s();\n",
|
|
cname);
|
|
fprintf(out, "}\n\n");
|
|
}
|
|
|
|
fprintf(out, "static const AppRouteInfo kryon_project_routes[%d];\n\n",
|
|
count);
|
|
fprintf(out, "static void\nkryon_project_enter(void *app, int index)\n{\n");
|
|
fprintf(out, " if(ApplyRoute == 0 || index < 0 || index >= %d)\n",
|
|
count);
|
|
fprintf(out, " return;\n");
|
|
fprintf(out, " ApplyRoute(app, &kryon_project_routes[index]);\n");
|
|
fprintf(out, "}\n\n");
|
|
|
|
fprintf(out, "static const AppRouteInfo kryon_project_routes[] = {\n");
|
|
for(int i = 0; i < count; i++) {
|
|
char id[KIR_NAME_MAX * 2];
|
|
char title[KIR_NAME_MAX * 2];
|
|
char group[KIR_NAME_MAX * 2];
|
|
char source[KIR_PATH_MAX * 2];
|
|
char wrapper[64];
|
|
|
|
c_str(id, sizeof(id), routes[i].id);
|
|
c_str(title, sizeof(title), routes[i].title);
|
|
c_str(group, sizeof(group), routes[i].group);
|
|
c_str(source, sizeof(source), routes[i].source_path);
|
|
snprintf(wrapper, sizeof(wrapper), "kryon_project_draw_%d", i);
|
|
fprintf(out, " {%s, %s, %s, %s, kryon_project_enter, %s},\n",
|
|
id, group, title, source, wrapper);
|
|
}
|
|
fprintf(out, "};\n\n");
|
|
fprintf(out, "static App kryon_project_runtime;\n");
|
|
fprintf(out, "static AppHost kryon_project_host;\n\n");
|
|
fprintf(out, "AppHost *\nCreateAppHost(int abi_version, const char *project_path)\n{\n");
|
|
fprintf(out, " void *app = 0;\n");
|
|
fprintf(out, " if(abi_version != APP_HOST_ABI_VERSION || CreateApp == 0)\n");
|
|
fprintf(out, " return 0;\n");
|
|
fprintf(out, " app = CreateApp(project_path);\n");
|
|
fprintf(out, " if(app == 0)\n");
|
|
fprintf(out, " return 0;\n");
|
|
fprintf(out, " kryon_project_runtime.app = app;\n");
|
|
fprintf(out, " kryon_project_runtime.routes = kryon_project_routes;\n");
|
|
fprintf(out, " kryon_project_runtime.route_count = (int)(sizeof(kryon_project_routes) / sizeof(kryon_project_routes[0]));\n");
|
|
fprintf(out, " kryon_project_runtime.selected_route = 0;\n");
|
|
fprintf(out, " BindAppHost(&kryon_project_runtime, &kryon_project_host);\n");
|
|
fprintf(out, " SetAppScreen(&kryon_project_host, 0);\n");
|
|
fprintf(out, " return &kryon_project_host;\n}\n\n");
|
|
fprintf(out, "void\nDestroyAppHost(AppHost *host)\n{\n");
|
|
fprintf(out, " if(host == 0 || host != &kryon_project_host)\n");
|
|
fprintf(out, " return;\n");
|
|
fprintf(out, " if(DestroyApp != 0 && kryon_project_runtime.app != 0)\n");
|
|
fprintf(out, " DestroyApp(kryon_project_runtime.app);\n");
|
|
fprintf(out, " kryon_project_runtime = (App){0};\n");
|
|
fprintf(out, " kryon_project_host = (AppHost){0};\n}\n");
|
|
fclose(out);
|
|
|
|
/* --- main() (hook-driven, from KirAppMeta) --- */
|
|
if(no_main || appmod == NULL)
|
|
return;
|
|
out = fopen(path, "ab");
|
|
if(out == NULL)
|
|
return;
|
|
{
|
|
int width = appmod->app.width > 0 ? appmod->app.width : 800;
|
|
int height = appmod->app.height > 0 ? appmod->app.height : 600;
|
|
int fps = appmod->app.fps > 0 ? appmod->app.fps : 60;
|
|
char title[KIR_NAME_MAX * 2];
|
|
|
|
c_str(title, sizeof(title), appmod->app.title);
|
|
fprintf(out, "\nint\nmain(void)\n{\n");
|
|
fprintf(out, " InitWindow(%d, %d, %s);\n", width, height, title);
|
|
fprintf(out, " SetTargetFPS(%d);\n", fps);
|
|
if(appmod->app.font_examples)
|
|
fprintf(out, " LoadExampleUIFont();\n");
|
|
fprintf(out, " InitUI(%d, %d, GetUIScale());\n", width, height);
|
|
if(appmod->app.theme[0] != '\0')
|
|
fprintf(out, " SetCurrentTheme(%s, %d);\n",
|
|
appmod->app.theme, appmod->app.dark_mode);
|
|
if(appmod->app.init[0] != '\0') {
|
|
char hook[KIR_NAME_MAX * 3];
|
|
int found = 0;
|
|
|
|
for(int j = 0; j < appmod->function_count && !found; j++) {
|
|
if(strcmp(appmod->functions[j].name, appmod->app.init) == 0) {
|
|
k2c_function_c_name(appmod, &appmod->functions[j],
|
|
hook, sizeof(hook));
|
|
found = 1;
|
|
}
|
|
}
|
|
fprintf(out, " %s();\n",
|
|
found ? hook : appmod->app.init);
|
|
}
|
|
fprintf(out, " while(!WindowShouldClose())\n");
|
|
if(appmod->app.frame[0] != '\0') {
|
|
char hook[KIR_NAME_MAX * 3];
|
|
int found = 0;
|
|
|
|
for(int j = 0; j < appmod->function_count && !found; j++) {
|
|
if(strcmp(appmod->functions[j].name, appmod->app.frame) == 0) {
|
|
k2c_function_c_name(appmod, &appmod->functions[j],
|
|
hook, sizeof(hook));
|
|
found = 1;
|
|
}
|
|
}
|
|
fprintf(out, " %s();\n",
|
|
found ? hook : appmod->app.frame);
|
|
} else if(count > 0) {
|
|
char cname[KIR_NAME_MAX * 3];
|
|
|
|
k2c_function_c_name(routes[0].m, routes[0].fn, cname,
|
|
sizeof(cname));
|
|
/* Screens with no explicit frame hook still need a drawing
|
|
* frame and their viewport argument each tick. */
|
|
fprintf(out, " BeginDrawing();\n");
|
|
if(strstr(routes[0].fn->args, "Rectangle") != NULL) {
|
|
fprintf(out, " BeginUIFrame(GetScreenWidth(), "
|
|
"GetScreenHeight(), GetUIScale());\n");
|
|
fprintf(out, " %s((Rectangle){0, 0, "
|
|
"(float)GetScreenWidth(), (float)GetScreenHeight()});\n",
|
|
cname);
|
|
fprintf(out, " EndUIFrame();\n");
|
|
} else {
|
|
fprintf(out, " %s();\n", cname);
|
|
}
|
|
fprintf(out, " EndDrawing();\n");
|
|
} else {
|
|
fprintf(out, " ;\n");
|
|
}
|
|
if(appmod->app.shutdown[0] != '\0') {
|
|
char hook[KIR_NAME_MAX * 3];
|
|
int found = 0;
|
|
|
|
for(int j = 0; j < appmod->function_count && !found; j++) {
|
|
if(strcmp(appmod->functions[j].name, appmod->app.shutdown) == 0) {
|
|
k2c_function_c_name(appmod, &appmod->functions[j],
|
|
hook, sizeof(hook));
|
|
found = 1;
|
|
}
|
|
}
|
|
fprintf(out, " %s();\n",
|
|
found ? hook : appmod->app.shutdown);
|
|
}
|
|
if(appmod->app.font_examples)
|
|
fprintf(out, " UnloadExampleUIFont();\n");
|
|
fprintf(out, " CloseWindow();\n");
|
|
fprintf(out, " return 0;\n}\n");
|
|
}
|
|
fclose(out);
|
|
}
|