This commit is contained in:
2026-01-23 10:00:52 -06:00
parent 78312b28a0
commit c171fcaf65
2 changed files with 167 additions and 26 deletions

View File

@@ -89,6 +89,16 @@ void SDL_Surface_free(JSRuntime *rt, SDL_Surface *s) {
// Class definition // Class definition
QJSCLASS(SDL_Surface,) QJSCLASS(SDL_Surface,)
JSValue make_surface(JSContext *js, SDL_Surface *s){
JSValue ret = SDL_Surface2js(js,s);
JS_SetPropertyStr(js, ret, "width", JS_NewInt32(js, s->w));
JS_SetPropertyStr(js, ret, "height", JS_NewInt32(js, s->h));
JS_SetPropertyStr(js, ret, "format", pixelformat2js(js, s->format));
JS_SetPropertyStr(js, ret, "pitch", JS_NewFlaot64(js, s->pitch));
return JS_Stone(js, ret);
}
// SDL_Surface methods // SDL_Surface methods
JSC_CCALL(surface_blit, JSC_CCALL(surface_blit,
SDL_Surface *dst = js2SDL_Surface(js, self); SDL_Surface *dst = js2SDL_Surface(js, self);

183
video.c
View File

@@ -596,8 +596,7 @@ JSValue js_window_get_sizeInPixels(JSContext *js, JSValue self)
return vec22js(js, (vec2){width,height}); return vec22js(js, (vec2){width,height});
} }
// Surface related JSValue js_window_surface(JSContext *js, JSValue self, int argc, JSValue *argv)
JSValue js_window_get_surface(JSContext *js, JSValue self)
{ {
SDL_Window *w = js2SDL_Window(js,self); SDL_Window *w = js2SDL_Window(js,self);
SDL_Surface *surf = SDL_GetWindowSurface(w); SDL_Surface *surf = SDL_GetWindowSurface(w);
@@ -677,6 +676,159 @@ JSValue js_window_sync(JSContext *js, JSValue self, int argc, JSValue *argv)
return JS_NULL; return JS_NULL;
} }
JSValue js_window_set(JSContext *js, JSValue self, int argc, JSValue *argv)
{
SDL_Window *w = js2SDL_Window(js,self);
JSValue obj = argv[0];
JSValue val;
val = JS_GetPropertyStr(js, obj, "title");
if (!JS_IsNull(val)) { js_window_set_title(js, self, val); JS_FreeValue(js, val); }
val = JS_GetPropertyStr(js, obj, "size");
if (!JS_IsNull(val)) { js_window_set_size(js, self, val); JS_FreeValue(js, val); }
val = JS_GetPropertyStr(js, obj, "position");
if (!JS_IsNull(val)) { js_window_set_position(js, self, val); JS_FreeValue(js, val); }
val = JS_GetPropertyStr(js, obj, "mouseGrab");
if (!JS_IsNull(val)) { js_window_set_mouseGrab(js, self, val); JS_FreeValue(js, val); }
val = JS_GetPropertyStr(js, obj, "keyboardGrab");
if (!JS_IsNull(val)) { js_window_set_keyboardGrab(js, self, val); JS_FreeValue(js, val); }
val = JS_GetPropertyStr(js, obj, "opacity");
if (!JS_IsNull(val)) { js_window_set_opacity(js, self, val); JS_FreeValue(js, val); }
val = JS_GetPropertyStr(js, obj, "minimumSize");
if (!JS_IsNull(val)) { js_window_set_minimumSize(js, self, val); JS_FreeValue(js, val); }
val = JS_GetPropertyStr(js, obj, "maximumSize");
if (!JS_IsNull(val)) { js_window_set_maximumSize(js, self, val); JS_FreeValue(js, val); }
val = JS_GetPropertyStr(js, obj, "resizable");
if (!JS_IsNull(val)) { js_window_set_resizable(js, self, val); JS_FreeValue(js, val); }
val = JS_GetPropertyStr(js, obj, "bordered");
if (!JS_IsNull(val)) { js_window_set_bordered(js, self, val); JS_FreeValue(js, val); }
val = JS_GetPropertyStr(js, obj, "alwaysOnTop");
if (!JS_IsNull(val)) { js_window_set_alwaysOnTop(js, self, val); JS_FreeValue(js, val); }
val = JS_GetPropertyStr(js, obj, "fullscreen");
if (!JS_IsNull(val)) { js_window_set_fullscreen(js, self, val); JS_FreeValue(js, val); }
val = JS_GetPropertyStr(js, obj, "focusable");
if (!JS_IsNull(val)) { js_window_set_focusable(js, self, val); JS_FreeValue(js, val); }
val = JS_GetPropertyStr(js, obj, "modal");
if (!JS_IsNull(val)) { js_window_set_modal(js, self, val); JS_FreeValue(js, val); }
val = JS_GetPropertyStr(js, obj, "visible");
if (!JS_IsNull(val)) { js_window_set_visible(js, self, val); JS_FreeValue(js, val); }
val = JS_GetPropertyStr(js, obj, "minimized");
if (!JS_IsNull(val)) { js_window_set_minimized(js, self, val); JS_FreeValue(js, val); }
val = JS_GetPropertyStr(js, obj, "maximized");
if (!JS_IsNull(val)) { js_window_set_maximized(js, self, val); JS_FreeValue(js, val); }
val = JS_GetPropertyStr(js, obj, "parent");
if (!JS_IsNull(val)) { js_window_set_parent(js, self, val); JS_FreeValue(js, val); }
return JS_NULL;
}
JSValue js_window_state(JSContext *js, JSValue self, int argc, JSValue *argv)
{
SDL_Window *w = js2SDL_Window(js,self);
JSValue ret = JS_NewObject(js);
// Title
const char *title = SDL_GetWindowTitle(w);
JS_SetPropertyStr(js, ret, "title", JS_NewString(js, title ? title : ""));
// Size
int width, height;
SDL_GetWindowSize(w, &width, &height);
JSValue sizeObj = JS_NewObject(js);
JS_SetPropertyStr(js, sizeObj, "x", JS_NewInt32(js, width));
JS_SetPropertyStr(js, sizeObj, "y", JS_NewInt32(js, height));
JS_SetPropertyStr(js, ret, "size", sizeObj);
// Position
int x, y;
SDL_GetWindowPosition(w, &x, &y);
JSValue posObj = JS_NewObject(js);
JS_SetPropertyStr(js, posObj, "x", JS_NewInt32(js, x));
JS_SetPropertyStr(js, posObj, "y", JS_NewInt32(js, y));
JS_SetPropertyStr(js, ret, "position", posObj);
// Minimum size
int minW, minH;
SDL_GetWindowMinimumSize(w, &minW, &minH);
JSValue minSizeObj = JS_NewObject(js);
JS_SetPropertyStr(js, minSizeObj, "x", JS_NewInt32(js, minW));
JS_SetPropertyStr(js, minSizeObj, "y", JS_NewInt32(js, minH));
JS_SetPropertyStr(js, ret, "minimumSize", minSizeObj);
// Maximum size
int maxW, maxH;
SDL_GetWindowMaximumSize(w, &maxW, &maxH);
JSValue maxSizeObj = JS_NewObject(js);
JS_SetPropertyStr(js, maxSizeObj, "x", JS_NewInt32(js, maxW));
JS_SetPropertyStr(js, maxSizeObj, "y", JS_NewInt32(js, maxH));
JS_SetPropertyStr(js, ret, "maximumSize", maxSizeObj);
// Opacity
float opacity = SDL_GetWindowOpacity(w);
JS_SetPropertyStr(js, ret, "opacity", JS_NewFloat64(js, opacity));
// Grabs
JS_SetPropertyStr(js, ret, "mouseGrab", JS_NewBool(js, SDL_GetWindowMouseGrab(w)));
JS_SetPropertyStr(js, ret, "keyboardGrab", JS_NewBool(js, SDL_GetWindowKeyboardGrab(w)));
// Flags-based properties
SDL_WindowFlags flags = SDL_GetWindowFlags(w);
JS_SetPropertyStr(js, ret, "fullscreen", JS_NewBool(js, flags & SDL_WINDOW_FULLSCREEN));
JS_SetPropertyStr(js, ret, "resizable", JS_NewBool(js, flags & SDL_WINDOW_RESIZABLE));
JS_SetPropertyStr(js, ret, "bordered", JS_NewBool(js, !(flags & SDL_WINDOW_BORDERLESS)));
JS_SetPropertyStr(js, ret, "alwaysOnTop", JS_NewBool(js, flags & SDL_WINDOW_ALWAYS_ON_TOP));
JS_SetPropertyStr(js, ret, "focusable", JS_NewBool(js, !(flags & SDL_WINDOW_NOT_FOCUSABLE)));
JS_SetPropertyStr(js, ret, "modal", JS_NewBool(js, flags & SDL_WINDOW_MODAL));
JS_SetPropertyStr(js, ret, "visible", JS_NewBool(js, !(flags & SDL_WINDOW_HIDDEN)));
JS_SetPropertyStr(js, ret, "minimized", JS_NewBool(js, flags & SDL_WINDOW_MINIMIZED));
JS_SetPropertyStr(js, ret, "maximized", JS_NewBool(js, flags & SDL_WINDOW_MAXIMIZED));
// Display properties
JS_SetPropertyStr(js, ret, "displayScale", JS_NewFloat64(js, SDL_GetWindowDisplayScale(w)));
JS_SetPropertyStr(js, ret, "pixelDensity", JS_NewFloat64(js, SDL_GetWindowPixelDensity(w)));
// Size in pixels
int pixelW, pixelH;
SDL_GetWindowSizeInPixels(w, &pixelW, &pixelH);
JSValue pixelSizeObj = JS_NewObject(js);
JS_SetPropertyStr(js, pixelSizeObj, "x", JS_NewInt32(js, pixelW));
JS_SetPropertyStr(js, pixelSizeObj, "y", JS_NewInt32(js, pixelH));
JS_SetPropertyStr(js, ret, "sizeInPixels", pixelSizeObj);
// Flags
JS_SetPropertyStr(js, ret, "flags", JS_NewInt64(js, flags));
JS_SetPropertyStr(js, ret, "id", JS_NewUint32(js, SDL_GetWindowID(w)));
// Parent
SDL_Window *parent = SDL_GetWindowParent(w);
if (parent)
JS_SetPropertyStr(js, ret, "parent", SDL_Window2js(js, parent));
else
JS_SetPropertyStr(js, ret, "parent", JS_NULL);
return ret;
}
static const JSCFunctionListEntry js_SDL_Window_funcs[] = { static const JSCFunctionListEntry js_SDL_Window_funcs[] = {
MIST_FUNC_DEF(SDL_Window, fullscreen, 0), MIST_FUNC_DEF(SDL_Window, fullscreen, 0),
MIST_FUNC_DEF(SDL_Window, keyboard_shown, 0), MIST_FUNC_DEF(SDL_Window, keyboard_shown, 0),
@@ -688,30 +840,9 @@ static const JSCFunctionListEntry js_SDL_Window_funcs[] = {
MIST_FUNC_DEF(window, flash, 1), MIST_FUNC_DEF(window, flash, 1),
MIST_FUNC_DEF(window, destroy, 0), MIST_FUNC_DEF(window, destroy, 0),
MIST_FUNC_DEF(window, sync, 0), MIST_FUNC_DEF(window, sync, 0),
CGETSET_ADD(window, title), MIST_FUNC_DEF(window, set, 1),
CGETSET_ADD(window, size), MIST_FUNC_DEF(window, state, 0),
CGETSET_ADD(window, position), MIST_FUNC_DEF(window, surface, 0),
CGETSET_ADD(window, mouseGrab),
CGETSET_ADD(window, keyboardGrab),
CGETSET_ADD(window, opacity),
CGETSET_ADD(window, minimumSize),
CGETSET_ADD(window, maximumSize),
CGETSET_ADD(window, resizable),
CGETSET_ADD(window, bordered),
CGETSET_ADD(window, alwaysOnTop),
CGETSET_ADD(window, fullscreen),
CGETSET_ADD(window, focusable),
CGETSET_ADD(window, modal),
CGETSET_ADD(window, visible),
CGETSET_ADD(window, minimized),
CGETSET_ADD(window, maximized),
CGETSET_ADD(window, parent),
JS_CGETSET_DEF("id", js_window_get_id, NULL),
JS_CGETSET_DEF("pixelDensity", js_window_get_pixelDensity, NULL),
JS_CGETSET_DEF("displayScale", js_window_get_displayScale, NULL),
JS_CGETSET_DEF("sizeInPixels", js_window_get_sizeInPixels, NULL),
JS_CGETSET_DEF("flags", js_window_get_flags, NULL),
JS_CGETSET_DEF("surface", js_window_get_surface, NULL),
MIST_FUNC_DEF(window, updateSurface, 0), MIST_FUNC_DEF(window, updateSurface, 0),
MIST_FUNC_DEF(window, updateSurfaceRects, 1), MIST_FUNC_DEF(window, updateSurfaceRects, 1),
}; };