dll based wb plugins

main
Dominik Madarász 2023-09-18 19:50:50 +02:00
parent e4269dbc0d
commit 20796c35fa
6 changed files with 60 additions and 22 deletions

View File

@ -599,18 +599,27 @@ if "!vis!"=="yes" echo !cc! engine\v4k.c !export! !args! ^&^& if "!dll!"=="dll"
rem editor
if "!editor!"=="yes" (
set edit=-DCOOK_ON_DEMAND -DUI_LESSER_SPACING -DUI_ICONS_SMALL
set edit=-DCOOK_ON_DEMAND -DUI_LESSER_SPACING -DUI_ICONS_SMALL -DMAX_CACHED_FILES=0
rem if "!vis!"=="yes" echo !cc! !o! editor.exe tools\editor\editor.c !edit! !import! !args!
rem !echo! editor && !cc! !o! editor.exe tools\editor\editor.c !edit! !import! !args! || set rc=1
rem !echo! editor2 && !cc! !o! editor2.exe tools\editor\editor2.c !edit! !args! || set rc=1
set "plugins="
for %%f in ("workbench\plugins\*.c") do (
echo plugin: %%~nf.c
set "plugins=!plugins! workbench\plugins\%%~nf.c"
!echo! v4k && !cc! engine\v4k.c !export! !edit! !args! || set rc=1
if "!cc!"=="cl" (
set plug_export=/LD
) else if "!cc!"=="clang-cl" (
set plug_export=/LD
) else (
set plug_export=-shared
)
!echo! workbench && !cc! !o! workbench.exe workbench\workbench.c !plugins! -Iworkbench !edit! !args! || set rc=1
for %%f in ("workbench\plugins\*.c") do (
!echo! %%~nf && !cc! !o! %%~nf.dll %%f -Iworkbench !plug_export! !args! !import! || set rc=1
)
!echo! workbench && !cc! !o! workbench.exe workbench\workbench.c -Iworkbench !args! !import! || set rc=1
)
rem demos

12
demos/ctx/app.c 100644
View File

@ -0,0 +1,12 @@
#define API IMPORT
#include "v4k.h" // Minimal C sample
int main() {
window_create(75.0, 0); // 75% size, no extra flags
void (*test)(void) = dll("lib.dll", "test");
test();
while( window_swap() && !input(KEY_ESC) ) { // game loop
}
}

5
demos/ctx/lib.c 100644
View File

@ -0,0 +1,5 @@
#define API IMPORT
#include "v4k.h" // Minimal C sample
__declspec(dllexport) void test() {
ui_notify("dll test", "hello");
}

View File

@ -1,4 +1,6 @@
#pragma once
#define API IMPORT
#include "v4k.h"
struct asset_t;
@ -24,5 +26,3 @@ typedef struct {
bool opened;
uint64_t last_modified;
} asset_t;
#define PLUG_DECLARE(name) editor_vtable_t name##__procs = { name##_init, name##_tick, name##_quit, name##_ext };

View File

@ -1,4 +1,3 @@
#include "v4k.h"
#include "pluginapi.h"
enum { WIDTH = 1024, HEIGHT = 1024 };
@ -30,7 +29,7 @@ typedef struct {
array(shader_asset_t) shaders = 0;
char *shader_editor_ext() {
__declspec(dllexport) char *plug_ext() {
return "fx,glsl,vs,fs";
}
@ -48,7 +47,7 @@ void reload_shader(asset_t *f, shader_asset_t *s) {
s->reload = 0;
}
int shader_editor_init(asset_t *f) {
__declspec(dllexport) int plug_init(asset_t *f) {
shader_asset_t a = {0};
a.reload = 1;
@ -79,7 +78,7 @@ int shader_editor_init(asset_t *f) {
return 0;
}
int shader_editor_tick(asset_t *f) {
__declspec(dllexport) int plug_tick(asset_t *f) {
shader_asset_t *s = (shaders+f->slot);
if (s->reload) {
@ -162,12 +161,10 @@ int shader_editor_tick(asset_t *f) {
return 0;
}
int shader_editor_quit(asset_t *f) {
__declspec(dllexport) int plug_quit(asset_t *f) {
shader_asset_t *s = (shaders+f->slot);
fbo_destroy(s->fb);
fbo_destroy(s->flipFB);
return 0;
}
PLUG_DECLARE(shader_editor);

View File

@ -1,7 +1,6 @@
#define COOK_ON_DEMAND 1
#define MAX_CACHED_FILES 0
#define VFS_ALWAYS_PACK 1
#include "v4k.c"
// #define COOK_ON_DEMAND 1
// #define MAX_CACHED_FILES 0
// #define VFS_ALWAYS_PACK 1
#include "pluginapi.h"
array(editor_t) editors = 0;
@ -14,16 +13,32 @@ array(asset_t) assets = 0;
PLUGINS
#undef X
void load_editor(char *pname, editor_vtable_t f) {
editor_vtable_t load_editor_vtable(char *pname) {
char *name = va("%s.dll", pname);
editor_vtable_t f;
f.init = dll(name, "plug_init");
f.tick = dll(name, "plug_tick");
f.quit = dll(name, "plug_quit");
f.ext = dll(name, "plug_ext");
if (!f.init || !f.tick || !f.quit || !f.ext)
return (editor_vtable_t){0};
return f;
}
void load_editor(char *pname) {
editor_t ed = {0};
ed.f = f;
ed.name = file_base(STRDUP(pname)); // @leak
ed.f = load_editor_vtable(pname);
if (!ed.f.init) {
// PRINTF("unsupported plugin: '%s'\n", ed.name);
return;
}
PRINTF("loaded plugin: '%s'\n", ed.name);
array_push(editors, ed);
}
void load_editors() {
#define X(name) load_editor(#name, name##__procs);
#define X(name) load_editor(#name);
PLUGINS
#undef X
}