From 933faae1e998a0d9cbfed974d96403a40397d0dd Mon Sep 17 00:00:00 2001 From: Alexander Klingenbeck Date: Wed, 28 Jun 2023 23:22:00 +0200 Subject: [PATCH] Texture generator example --- bindings/src/index.ts | 4 +- examples/gen.js | 70 +++++++++++++++++++++++++++++++++++ examples/lib.raylib.d.ts | 4 ++ generate-bindings.js | 4 +- src/bindings/js_raylib_core.h | 22 +++++++++++ 5 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 examples/gen.js diff --git a/bindings/src/index.ts b/bindings/src/index.ts index dafade3..ca9485a 100644 --- a/bindings/src/index.ts +++ b/bindings/src/index.ts @@ -299,7 +299,9 @@ function main(){ } getStruct(api.structs, "RenderTexture")!.binding = { properties: { - id: { get: true } + id: { get: true }, + texture: { get: true }, + depth: { get: true }, }, aliases: getAliases(api.aliases, "RenderTexture") //destructor: "UnloadRenderTexture" diff --git a/examples/gen.js b/examples/gen.js new file mode 100644 index 0000000..b9f55fb --- /dev/null +++ b/examples/gen.js @@ -0,0 +1,70 @@ +initWindow(100,100,"Gen") + +const input = [["orange",ORANGE],["green", LIME], ["purple", PURPLE],["red", MAROON], ["lightgrey", LIGHTGRAY], ["grey", GRAY], ["blue", BLUE]] + +input.forEach(t => { + traceLog(LOG_INFO, t) + const [name, color] = t + const outDir = "../build/out/" + createPlaceholder(128,128,color, 64, 32, `${outDir}grid_128_${name}.png`) + createPlaceholder(64,64,color, 32, 16, `${outDir}grid_64_${name}.png`) + createPlaceholder(32,32,color, 16, 8, `${outDir}grid_32_${name}.png`) + createDoor(128,128, color, `${outDir}door_${name}.png`) + createWindow(128,128, color, `${outDir}window_${name}.png`) +}); + + +closeWindow() + +function createDoor(sizex, sizey, color, filename){ + const img = genImageColor(sizex, sizey, color) + drawGrid(img, sizex, sizey, 8, blend(color, WHITE, 0.12)) + drawGrid(img, sizex, sizey, 16, blend(color, WHITE, 0.25)) + drawGrid(img, sizex, sizey, 32, blend(color, WHITE, 0.5)) + imageDrawLine(img, 0,0,sizex,0, WHITE) + imageDrawLine(img, 0,0,0,sizey, WHITE) + imageDrawText(img, `Door 48x64`, 2, 1, 10, WHITE) + imageDrawRectangle(img, 40, 64, 48, 64, blend(color, BLACK, 0.5)) + imageDrawRectangleLines(img, new Rectangle(40,64,48,64+1),1,WHITE) + exportImage(img, filename) + unloadImage(img) +} + +function createWindow(sizex, sizey, color, filename){ + const img = genImageColor(sizex, sizey, color) + drawGrid(img, sizex, sizey, 8, blend(color, WHITE, 0.12)) + drawGrid(img, sizex, sizey, 16, blend(color, WHITE, 0.25)) + drawGrid(img, sizex, sizey, 32, blend(color, WHITE, 0.5)) + imageDrawLine(img, 0,0,sizex,0, WHITE) + imageDrawLine(img, 0,0,0,sizey, WHITE) + imageDrawText(img, `Window 32x48`, 2, 1, 10, WHITE) + imageDrawRectangle(img, 48, 48, 32, 48, blend(color, BLACK, 0.5)) + imageDrawRectangleLines(img, new Rectangle(48,48,32,48),1,WHITE) + exportImage(img, filename) + unloadImage(img) +} + +function createPlaceholder(sizex, sizey, color, grid, subgrid, filename){ + const img = genImageColor(sizex, sizey, color) + drawGrid(img, sizex, sizey, subgrid, blend(color, WHITE, 0.25)) + drawGrid(img, sizex, sizey, grid, blend(color, WHITE, 0.5)) + imageDrawLine(img, 0,0,sizex,0, WHITE) + imageDrawLine(img, 0,0,0,sizey, WHITE) + imageDrawText(img, `${sizex}x${sizey}`, 2, 1, 10, WHITE) + exportImage(img, filename) + unloadImage(img) +} + +function drawGrid(img, w,h,size,color){ +for (let y = 0; y < h; y+=size) { + imageDrawLine(img, 0, y, w, y, color) + } + for (let x = 0; x < w; x+=size) { + imageDrawLine(img, x, 0, x, h, color) + } +} + +function blend(c1, c2, alpha){ + const b = (v1, v2) => v1 * (1-alpha) + v2 * alpha + return new Color(b(c1.r,c2.r), b(c1.g, c2.g), b(c1.b, c2.b), c1.a) +} \ No newline at end of file diff --git a/examples/lib.raylib.d.ts b/examples/lib.raylib.d.ts index 9a913d2..3afb149 100644 --- a/examples/lib.raylib.d.ts +++ b/examples/lib.raylib.d.ts @@ -99,6 +99,10 @@ declare var Texture: { interface RenderTexture { /** OpenGL framebuffer object id */ id: number, + /** Color buffer attachment texture */ + texture: Texture, + /** Depth buffer attachment texture */ + depth: Texture, } declare var RenderTexture: { prototype: RenderTexture; diff --git a/generate-bindings.js b/generate-bindings.js index 3140d7b..7b0b54a 100644 --- a/generate-bindings.js +++ b/generate-bindings.js @@ -1237,7 +1237,9 @@ function main() { }; getStruct(api.structs, "RenderTexture").binding = { properties: { - id: { get: true } + id: { get: true }, + texture: { get: true }, + depth: { get: true }, }, aliases: getAliases(api.aliases, "RenderTexture") //destructor: "UnloadRenderTexture" diff --git a/src/bindings/js_raylib_core.h b/src/bindings/js_raylib_core.h index 6394f8c..a125549 100644 --- a/src/bindings/js_raylib_core.h +++ b/src/bindings/js_raylib_core.h @@ -639,8 +639,30 @@ static JSValue js_RenderTexture_get_id(JSContext* ctx, JSValueConst this_val) { return ret; } +static JSValue js_RenderTexture_get_texture(JSContext* ctx, JSValueConst this_val) { + RenderTexture* ptr = JS_GetOpaque2(ctx, this_val, js_RenderTexture_class_id); + Texture texture = ptr->texture; + Texture* ret_ptr = (Texture*)js_malloc(ctx, sizeof(Texture)); + *ret_ptr = texture; + JSValue ret = JS_NewObjectClass(ctx, js_Texture_class_id); + JS_SetOpaque(ret, ret_ptr); + return ret; +} + +static JSValue js_RenderTexture_get_depth(JSContext* ctx, JSValueConst this_val) { + RenderTexture* ptr = JS_GetOpaque2(ctx, this_val, js_RenderTexture_class_id); + Texture depth = ptr->depth; + Texture* ret_ptr = (Texture*)js_malloc(ctx, sizeof(Texture)); + *ret_ptr = depth; + JSValue ret = JS_NewObjectClass(ctx, js_Texture_class_id); + JS_SetOpaque(ret, ret_ptr); + return ret; +} + static const JSCFunctionListEntry js_RenderTexture_proto_funcs[] = { JS_CGETSET_DEF("id",js_RenderTexture_get_id,NULL), + JS_CGETSET_DEF("texture",js_RenderTexture_get_texture,NULL), + JS_CGETSET_DEF("depth",js_RenderTexture_get_depth,NULL), JS_PROP_STRING_DEF("[Symbol.toStringTag]","RenderTexture", JS_PROP_CONFIGURABLE), };