From 85ee7247547b62bce926c116e1b96dba07623fed Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Sun, 11 May 2025 09:34:31 -0500 Subject: [PATCH] add texture mode for renderer --- scripts/modules/draw2d.js | 19 +++------------- scripts/modules/sdl_render.js | 7 ++++-- source/jsffi.c | 42 ++++++++++++++++++++++++++++++----- 3 files changed, 45 insertions(+), 23 deletions(-) diff --git a/scripts/modules/draw2d.js b/scripts/modules/draw2d.js index c432328e..0c251b3e 100644 --- a/scripts/modules/draw2d.js +++ b/scripts/modules/draw2d.js @@ -347,10 +347,8 @@ function software_fill_round_rect(rect, radius) var rect_def = { thickness:1, - mode: 'fill', color: Color.white, - radius: 0, - thickness:1 + radius: 0 } draw.rectangle = function render_rectangle(rect, def, pipeline) { var opt = def ? {...rect_def, ...def} : rect_def @@ -390,18 +388,6 @@ draw.slice9 = function slice9(image, rect = [0,0], slice = 0, info = slice9_info image = graphics.texture(image) render.slice9(image, rect, slice, slice9_info, pipeline); -// var mesh = geometry.slice9(image.texture, rect, util.normalizeSpacing(slice), info) -// console.log(json.encode(mesh)) -// render.geometry(image, mesh, pipeline) -/* render.queue({ - type: 'geometry', - mesh, - image, - pipeline, - first_index:0, - num_indices:mesh.num_indices - }) -*/ } draw.slice9[prosperon.DOC] = ` :param image: An image object or string path to a texture. @@ -420,9 +406,10 @@ var image_info = { flip_x: false, flip_y: false, color: Color.white, + mode: 'linear' } -draw.image = function image(image, rect = [0,0], rotation = 0, anchor = [0,0], shear = [0,0], info, pipeline) { +draw.image = function image(image, rect = [0,0], rotation = 0, anchor = [0,0], shear = [0,0], info = {}, pipeline) { if (!image) throw Error('Need an image to render.') if (typeof image === "string") image = graphics.texture(image) diff --git a/scripts/modules/sdl_render.js b/scripts/modules/sdl_render.js index 118894c9..30dd36e0 100644 --- a/scripts/modules/sdl_render.js +++ b/scripts/modules/sdl_render.js @@ -28,8 +28,10 @@ render.initialize = function(config) url: "https://prosperon.dev" } - prosperon.window = prosperon.engine_start({width:500,height:500}) - context = prosperon.window.make_renderer("vulkan") + config.__proto__ = default_conf + prosperon.window = prosperon.engine_start(config) + context = prosperon.window.make_renderer() + context.logical_size([config.resolution_x, config.resolution_y], config.mode) } render.sprite = function(sprite) @@ -49,6 +51,7 @@ render.image = function(image, rect, rotation, anchor, shear, info) { // rect.width = image.rect_px.width; // rect.height = image.rect_px.height; + image.texture.mode(info.mode) context.texture(image.texture, image.rect_px, rect, rotation, anchor); } diff --git a/source/jsffi.c b/source/jsffi.c index fa6cec7a..e0ab335e 100644 --- a/source/jsffi.c +++ b/source/jsffi.c @@ -2894,7 +2894,7 @@ JSC_CCALL(renderer_rects, /* single-rect path */ rect w = js2rect(js, argv[0]); - w = renderer_worldrect_to_screen(ctx, w); + w = renderer_worldrect_to_screen(ctx, w); if (!SDL_RenderFillRects(r, &w, 1)) return JS_ThrowReferenceError(js, "SDL_RenderFillRects: %s", SDL_GetError()); ) @@ -3005,10 +3005,41 @@ JSC_CCALL(renderer_geometry2, JS_FreeValue(js, idx_v); ) +/* logical presentation helpers */ +typedef struct { const char *name; SDL_RendererLogicalPresentation pres; } pres_entry; + +static const pres_entry k_pres_table[] = { + { "stretch", SDL_LOGICAL_PRESENTATION_STRETCH }, + { "letterbox", SDL_LOGICAL_PRESENTATION_LETTERBOX }, + { "overscan", SDL_LOGICAL_PRESENTATION_OVERSCAN }, + { "integer", SDL_LOGICAL_PRESENTATION_INTEGER_SCALE }, + { NULL, SDL_LOGICAL_PRESENTATION_DISABLED } /* fallback */ +}; + +static JSValue logicalpresentation2js(JSContext *js, + SDL_RendererLogicalPresentation pres){ + const pres_entry *it; + for(it = k_pres_table; it->name; ++it) + if(it->pres == pres) break; + return JS_NewString(js, it->name ? it->name : "disabled"); +} + +static SDL_RendererLogicalPresentation +js2SDL_LogicalPresentation(JSContext *js, JSValue v){ + if(JS_IsUndefined(v)) return SDL_LOGICAL_PRESENTATION_DISABLED; + const char *s = JS_ToCString(js, v); + if(!s) return SDL_LOGICAL_PRESENTATION_DISABLED; + const pres_entry *it; + for(it = k_pres_table; it->name; ++it) + if(!strcmp(it->name, s)) break; + JS_FreeCString(js, s); + return it->pres; +} + JSC_CCALL(renderer_logical_size, SDL_Renderer *r = js2renderer_ctx(js,self)->sdl; HMM_Vec2 v = js2vec2(js,argv[0]); - SDL_SetRenderLogicalPresentation(r,v.x,v.y,SDL_LOGICAL_PRESENTATION_INTEGER_SCALE); + SDL_SetRenderLogicalPresentation(r,v.x,v.y,js2SDL_LogicalPresentation(js, argv[1])); ) JSC_CCALL(renderer_viewport, @@ -3202,7 +3233,7 @@ static const JSCFunctionListEntry js_renderer_ctx_funcs[] = { MIST_FUNC_DEF(renderer, get_image, 1), MIST_FUNC_DEF(renderer, scale, 1), - MIST_FUNC_DEF(renderer, logical_size,1), + MIST_FUNC_DEF(renderer, logical_size,2), MIST_FUNC_DEF(renderer, viewport,1), MIST_FUNC_DEF(renderer, clip,1), MIST_FUNC_DEF(renderer, vsync,1), @@ -5496,7 +5527,7 @@ static SDL_PixelFormat js2pixelformat(JSContext *js, JSValue v) JS_FreeCString(js,s); return it->fmt; -} +} typedef struct { const char *name; SDL_ScaleMode mode; } scale_entry; @@ -5708,7 +5739,8 @@ static const JSCFunctionListEntry js_SDL_Camera_funcs[] = JSC_CCALL(texture_mode, SDL_Texture *tex = js2SDL_Texture(js,self); - SDL_SetTextureScaleMode(tex,js2number(js,argv[0])); + SDL_ScaleMode mode = js2SDL_ScaleMode(js,argv[0]); + SDL_SetTextureScaleMode(tex,mode); ) static const JSCFunctionListEntry js_SDL_Texture_funcs[] = {