From 77f59d498e10ef8c98f3ccdb369f50eb0ebb504b Mon Sep 17 00:00:00 2001 From: Russell Kay Date: Sat, 21 Nov 2026 12:42:17 +0000 Subject: [PATCH 01/10] feat(html5): add the experimental view_set_viewport (non-rectangular viewports) HTML5 version of the C++ runner's view_set_viewport (GMS2-Runner-Main#1076, GMS2-Runner-Main#1082). It takes an array of viewport structs, one per view from 0, or a JSON string of one. Each struct can set polygons (x,y pairs in the view surface's pixels; [] turns the polygon off), the port (screenX/Y/W/H), the room rectangle (roomRectX/Y/W/H, also applied to the view's camera) and startFunc/endFunc callbacks. - view_set_viewport (Function_Graphics.js) ear clips each polygon into a triangle list kept on the room's yyView, and writes the port, room rectangle, visibility and surface into the view arrays. A view without a surface gets one of screenW x screenH. - DrawViews draws a polygon view's surface through its triangles at the view's port position once the surface is no longer the target: onto the application surface, or the canvas without one. Port units are scaled by the display scale, as for the rectangular views. WebGL fills a triangle list from the surface's texture; Canvas clips to the triangles and draws the surface image. startFunc and endFunc run around the triangles. - The mouse-to-view lookup hit tests a polygon view's triangles. - Leaving a non-persistent room frees the surfaces view_set_viewport created for its views. - Everything is behind @if function("view_set_viewport"), and the WebGL path uses AllocVerts directly, so it doesn't need the primitive builder to be in the build. --- scripts/_GameMaker.js | 6 + scripts/functions/Function_Graphics.js | 117 ++++++++++++++ scripts/yyIOManager.js | 10 +- scripts/yyRoom.js | 5 + scripts/yyView.js | 210 ++++++++++++++++++++++++- 5 files changed, 345 insertions(+), 3 deletions(-) diff --git a/scripts/_GameMaker.js b/scripts/_GameMaker.js index ff520a7d..9eeb0a58 100644 --- a/scripts/_GameMaker.js +++ b/scripts/_GameMaker.js @@ -1305,6 +1305,12 @@ function StartRoom( _numb, _starting ) // is the current room persistant? if(g_RunRoom && !g_RunRoom.m_persistent) { + // @if function("view_set_viewport") + // free the surfaces view_set_viewport created for the room's views + for (var v = 0; v < g_RunRoom.m_Views.length; v++) { + if (g_RunRoom.m_Views[v]) g_RunRoom.m_Views[v].ResetPolygon(); + } + // @endif //var int_size = roughSizeOfObject_debug(g_RunRoom); //console.log("size: " + int_size); var storage = g_RunRoom.m_pStorage; // only bit we want to keep diff --git a/scripts/functions/Function_Graphics.js b/scripts/functions/Function_Graphics.js index 723bfac1..84de4593 100644 --- a/scripts/functions/Function_Graphics.js +++ b/scripts/functions/Function_Graphics.js @@ -2996,6 +2996,123 @@ function view_set_surface_id(view, surface_id) { g_pBuiltIn.view_surface_id[yyGetInt32(view)] = yyGetInt32(surface_id); }; +// @if function("view_set_viewport") +// ############################################################################################# +/// Function: +/// Experimental non-rectangular viewports (GMS2-Runner-Main#1076). Takes an array of +/// viewport structs, one per view from view 0, or a JSON string of one. Each struct can set +/// polygons (x,y pairs in the view surface's pixels; [] turns the polygon off), +/// screenX/Y/W/H (the port), roomRectX/Y/W/H (the room rectangle) and startFunc/endFunc. +/// +// ############################################################################################# +function view_set_viewport(_viewportData) { + + var data = _viewportData; + if (typeof (data) == "string") { + try { + data = json_parse(undefined, data); + } catch (e) { + yyError("view_set_viewport() - the string is not valid JSON"); + return; + } + } + if (!(data instanceof Array)) { + yyError("view_set_viewport() - expected an array of viewport structs, or a JSON string that holds one"); + return; + } + if (data.length > 8) { + yyError("view_set_viewport() - " + data.length + " viewports given, but there are only 8"); + return; + } + if (!g_RunRoom) return; + + for (var index = 0; index < data.length; index++) { + var entry = data[index]; + if ((entry == null) || (typeof (entry) != "object") || (entry instanceof Array)) { + yyError("view_set_viewport() - entry " + index + " is not a struct"); + return; + } + + // A field that is left out keeps its current value + var getInt = function (_name, _fallback) { + var v = variable_struct_get(entry, _name); + return (v === undefined) ? _fallback : yyGetInt32(v); + }; + var screenX = getInt("screenX", -1); + var screenY = getInt("screenY", -1); + var screenW = getInt("screenW", 0); + var screenH = getInt("screenH", 0); + var roomRectX = getInt("roomRectX", -1); + var roomRectY = getInt("roomRectY", -1); + var roomRectW = getInt("roomRectW", 0); + var roomRectH = getInt("roomRectH", 0); + + var polygons = variable_struct_get(entry, "polygons"); + if ((polygons !== undefined) && !(polygons instanceof Array)) { + yyError("view_set_viewport() - polygons in entry " + index + " is not an array"); + return; + } + + var startFunc = variable_struct_get(entry, "startFunc"); + var endFunc = variable_struct_get(entry, "endFunc"); + + var pView = g_RunRoom.m_Views[index]; + if (!pView) continue; + + // a polygon needs at least 3 x,y pairs; fewer (e.g. []) turns it off, back to a rectangular viewport + if (polygons !== undefined) { + var points = []; + for (var n = 0; n < polygons.length; n++) points.push(yyGetReal(polygons[n])); + var indices = ViewPolygon_EarClip(points); + + pView.surface_id = g_pBuiltIn.view_surface_id[index]; + pView.ResetPolygon(); + g_pBuiltIn.view_surface_id[index] = pView.surface_id; + + if (indices.length > 0) { + var tris = []; + for (var n = 0; n < indices.length; n++) tris.push(points[2 * indices[n]], points[2 * indices[n] + 1]); + pView.m_polygon = tris; + g_pBuiltIn.view_visible[index] = true; + if ((g_pBuiltIn.view_surface_id[index] == -1) && (screenW > 0) && (screenH > 0)) { + var surf = surface_create(screenW, screenH); + pView.m_polygonSurface = surf; + pView.surface_id = surf; + g_pBuiltIn.view_surface_id[index] = surf; + } + } + } + + if ((screenW > 0) && (screenH > 0)) { + g_pBuiltIn.view_wport[index] = screenW; + g_pBuiltIn.view_hport[index] = screenH; + } + if (screenX != -1) g_pBuiltIn.view_xport[index] = screenX; + if (screenY != -1) g_pBuiltIn.view_yport[index] = screenY; + if ((roomRectW > 0) && (roomRectH > 0)) { + g_pBuiltIn.view_wview[index] = roomRectW; + g_pBuiltIn.view_hview[index] = roomRectH; + } + if (roomRectX != -1) g_pBuiltIn.view_xview[index] = roomRectX; + if (roomRectY != -1) g_pBuiltIn.view_yview[index] = roomRectY; + + // startFunc and endFunc are replaced by every call (left out means none), as in the C++ runner + pView.m_polygonStartFunc = (typeof (startFunc) == "function") ? startFunc : null; + pView.m_polygonEndFunc = (typeof (endFunc) == "function") ? endFunc : null; + + // update the view's camera from the view + var camID = g_pBuiltIn.view_camera[index]; + var pCam = (camID != -1) ? g_pCameraManager.GetCamera(camID) : null; + if (pCam) { + pCam.SetViewX(g_pBuiltIn.view_xview[index]); + pCam.SetViewY(g_pBuiltIn.view_yview[index]); + pCam.SetViewWidth(g_pBuiltIn.view_wview[index]); + pCam.SetViewHeight(g_pBuiltIn.view_hview[index]); + } + } +}; +// @endif view_set_viewport + // Stubs function display_get_windows_vertex_buffer_method() { return 0; diff --git a/scripts/yyIOManager.js b/scripts/yyIOManager.js index 6a9b3de2..2219c00c 100644 --- a/scripts/yyIOManager.js +++ b/scripts/yyIOManager.js @@ -2130,8 +2130,14 @@ function IO_StartStep() //{ // CalcCanvasLocation(g_Surfaces.Get(pView.surface_id), g_CanvasRect); //} - if (((this.MouseX - g_CanvasRect.left) >= pView.scaledportx) && ((this.MouseX - g_CanvasRect.left) < pView.scaledportx2) && - ((this.MouseY - g_CanvasRect.top) >= pView.scaledporty) && ((this.MouseY - g_CanvasRect.top) < pView.scaledporty2)) + // @if function("view_set_viewport") + // a view with a view_set_viewport polygon is hit tested against its triangles + var inView = pView.ContainsCanvasPoint(this.MouseX - g_CanvasRect.left, this.MouseY - g_CanvasRect.top); + // @else + var inView = ((this.MouseX - g_CanvasRect.left) >= pView.scaledportx) && ((this.MouseX - g_CanvasRect.left) < pView.scaledportx2) && + ((this.MouseY - g_CanvasRect.top) >= pView.scaledporty) && ((this.MouseY - g_CanvasRect.top) < pView.scaledporty2); + // @endif + if (inView) { g_pBuiltIn.mouse_x = pView.GetMouseX(g_pIOManager.MouseX,g_pIOManager.MouseY); g_pBuiltIn.mouse_y = pView.GetMouseY(g_pIOManager.MouseX,g_pIOManager.MouseY); diff --git a/scripts/yyRoom.js b/scripts/yyRoom.js index 8cb19dad..bcc384a8 100644 --- a/scripts/yyRoom.js +++ b/scripts/yyRoom.js @@ -4025,6 +4025,11 @@ yyRoom.prototype.DrawViews = function (r) { if (g_pCurrentView.surface_id != -1) { surface_reset_target(); } + + // @if function("view_set_viewport") + // now the view's surface isn't the target, draw it through its view_set_viewport polygon (if it has one) + g_pCurrentView.DrawPolygon(); + // @endif Current_View++; diff --git a/scripts/yyView.js b/scripts/yyView.js index 2187c3b6..e227d930 100644 --- a/scripts/yyView.js +++ b/scripts/yyView.js @@ -55,6 +55,12 @@ function yyView() // A view this.surface_id = -1; this.cameraID = -1; this.m_targetInstance = -1; + + // view_set_viewport (GMS2-Runner-Main#1076): the view's surface is drawn through these triangles instead of as a rectangle + this.m_polygon = null; // triangle list as x,y pairs in the view surface's pixels, or null + this.m_polygonSurface = -1; // surface view_set_viewport created for the view (freed with the polygon), or -1 + this.m_polygonStartFunc = null; // called before the triangles are drawn (e.g. to set a shader) + this.m_polygonEndFunc = null; // called after they are drawn } @@ -231,4 +237,206 @@ function CreateViewFromStorage( _pViewStorage ) } return view; -} \ No newline at end of file +} + + +// @if function("view_set_viewport") +// ############################################################################################# +/// Function: +/// Ear clips a simple polygon (x,y pairs) into a triangle list of vertex indices +/// +// ############################################################################################# +function ViewPolygon_EarClip(_points) +{ + var indices = []; + var n = Math.floor(_points.length / 2); + if (n < 3) return indices; + + var crossSign = function (ax, ay, bx, by, cx, cy) { + var v = (bx - ax) * (cy - ay) - (by - ay) * (cx - ax); + return (v > 0) ? 1 : ((v < 0) ? -1 : 0); + }; + var pointInTriangle = function (px, py, ax, ay, bx, by, cx, cy) { + var s1 = crossSign(ax, ay, bx, by, px, py); + var s2 = crossSign(bx, by, cx, cy, px, py); + var s3 = crossSign(cx, cy, ax, ay, px, py); + return (s1 > 0 && s2 > 0 && s3 > 0) || (s1 < 0 && s2 < 0 && s3 < 0); + }; + + var work = []; + for (var i = 0; i < n; i++) work.push(i); + + // the winding of the whole polygon tells convex corners from reflex ones + var area2 = 0; + for (var i = 0; i < n; i++) { + var j = (i + 1) % n; + area2 += _points[2 * i] * _points[2 * j + 1] - _points[2 * j] * _points[2 * i + 1]; + } + var convexSign = (area2 > 0) ? 1 : -1; + + var m = n; + var guard = n * n + 16; + var i = 0; + while (m > 3 && guard-- > 0) { + var ip = (i == 0) ? m - 1 : i - 1; + var inx = (i == m - 1) ? 0 : i + 1; + var a = work[ip], b = work[i], c = work[inx]; + var ax = _points[2 * a], ay = _points[2 * a + 1]; + var bx = _points[2 * b], by = _points[2 * b + 1]; + var cx = _points[2 * c], cy = _points[2 * c + 1]; + + var isEar = crossSign(ax, ay, bx, by, cx, cy) == convexSign; + for (var k = 0; isEar && k < m; k++) { + if (k == ip || k == i || k == inx) continue; + var v = work[k]; + if (pointInTriangle(_points[2 * v], _points[2 * v + 1], ax, ay, bx, by, cx, cy)) isEar = false; + } + + if (isEar) { + indices.push(a, b, c); + work.splice(i, 1); + m--; + } + else { + i++; + } + if (i >= m) i = 0; + } + + if (m == 3) indices.push(work[0], work[1], work[2]); + return indices; +} + +// ############################################################################################# +/// Function: +/// Drops the view's polygon, freeing the surface view_set_viewport created for it +/// +// ############################################################################################# +yyView.prototype.ResetPolygon = function () { + if (this.m_polygonSurface != -1) { + if (surface_exists(this.m_polygonSurface)) surface_free(this.m_polygonSurface); + if (this.surface_id == this.m_polygonSurface) this.surface_id = -1; + } + this.m_polygon = null; + this.m_polygonSurface = -1; + this.m_polygonStartFunc = null; + this.m_polygonEndFunc = null; +}; + +// ############################################################################################# +/// Function: +/// Is (_x, _y), in view port units, inside the view's polygon? False if it has none. +/// +// ############################################################################################# +yyView.prototype.PolygonContains = function (_x, _y) { + var tris = this.m_polygon; + if (tris == null || this.surface_id == -1) return false; + + // the triangles are in surface pixels, drawn at the view's port position + var px = _x - this.portx; + var py = _y - this.porty; + for (var n = 0; n + 5 < tris.length; n += 6) { + var d1 = (tris[n + 2] - tris[n]) * (py - tris[n + 1]) - (tris[n + 3] - tris[n + 1]) * (px - tris[n]); + var d2 = (tris[n + 4] - tris[n + 2]) * (py - tris[n + 3]) - (tris[n + 5] - tris[n + 3]) * (px - tris[n + 2]); + var d3 = (tris[n] - tris[n + 4]) * (py - tris[n + 5]) - (tris[n + 1] - tris[n + 5]) * (px - tris[n + 4]); + var hasNeg = (d1 < 0) || (d2 < 0) || (d3 < 0); + var hasPos = (d1 > 0) || (d2 > 0) || (d3 > 0); + if (!(hasNeg && hasPos)) return true; + } + return false; +}; + +// ############################################################################################# +/// Function: +/// Is the canvas position (_x, _y) inside the view? Hit tests a view_set_viewport polygon, +/// or the scaled port rectangle. +/// +// ############################################################################################# +yyView.prototype.ContainsCanvasPoint = function (_x, _y) { + if (this.m_polygon != null && this.surface_id != -1) { + // canvas pixels back to view port units (see the scaledport* values set in DrawViews) + var scaleX = (this.portw != 0) ? (this.scaledportw / this.portw) : 1; + var scaleY = (this.porth != 0) ? (this.scaledporth / this.porth) : 1; + var px = this.portx + (_x - this.scaledportx) / scaleX; + var py = this.porty + (_y - this.scaledporty) / scaleY; + return this.PolygonContains(px, py); + } + return (_x >= this.scaledportx) && (_x < this.scaledportx2) && (_y >= this.scaledporty) && (_y < this.scaledporty2); +}; + +// ############################################################################################# +/// Function: +/// Draws the view's surface through its polygon onto the current target (the application +/// surface, or the canvas without one). Call it after the view has been drawn into its +/// surface and that surface is no longer the target. +/// +// ############################################################################################# +yyView.prototype.DrawPolygon = function () { + var tris = this.m_polygon; + if (tris == null || this.surface_id == -1 || !surface_exists(this.surface_id)) return; + var pSurf = g_Surfaces.Get(this.surface_id); + if (!pSurf) return; + + // The triangles are in view port units, which land on the target at port * display scale, as the + // rectangular views do (DrawViews sets their view ports the same way) + Graphics_Save(); + Graphics_SetViewPort(0, 0, g_ApplicationWidth, g_ApplicationHeight); + Graphics_SetViewArea(0, 0, g_ApplicationWidth / g_DisplayScaleX, g_ApplicationHeight / g_DisplayScaleY, 0); + + var callFunc = function (_func) { + if (typeof (_func) == "function") _func(global, global); + }; + + try { + callFunc(this.m_polygonStartFunc); + + // startFunc can replace the polygon with view_set_viewport + tris = this.m_polygon; + if (tris != null && tris.length >= 6) { + var ox = this.portx; + var oy = this.porty; + if (g_webGL) { + var pTPE = pSurf.m_pTPE; + if (!pTPE.texture.webgl_textureid) WebGL_BindTexture(pTPE); + var numVerts = Math.floor(tris.length / 2); + var pBuff = g_webGL.AllocVerts(yyGL.PRIM_TRIANGLE, pTPE.texture.webgl_textureid, g_webGL.VERTEX_FORMAT_2D, numVerts); + var stride = pBuff.GetStride() >> 2; // built-in vbuffer arrays have 4 byte long types + var index = stride * pBuff.Current; + pBuff.Current += numVerts; + var texW = pTPE.texture.width, texH = pTPE.texture.height; + for (var n = 0; n < numVerts; n++, index += stride) { + var u = tris[2 * n], v = tris[2 * n + 1]; + pBuff.Coords[index + 0] = ox + u; + pBuff.Coords[index + 1] = oy + v; + pBuff.Coords[index + 2] = GR_Depth; + pBuff.UVs[index + 0] = u / texW; + pBuff.UVs[index + 1] = v / texH; + pBuff.Colours[index] = 0xffffffff; + } + // draw the triangles now, while startFunc's shader is still set + g_webGL.Flush(); + } + else { + // Canvas: clip to the triangles and draw the surface at the port position + graphics.save(); + graphics.beginPath(); + for (var n = 0; n + 5 < tris.length; n += 6) { + graphics.moveTo(ox + tris[n], oy + tris[n + 1]); + graphics.lineTo(ox + tris[n + 2], oy + tris[n + 3]); + graphics.lineTo(ox + tris[n + 4], oy + tris[n + 5]); + graphics.closePath(); + } + graphics.clip(); + graphics.globalAlpha = 1; + graphics._drawImage(pSurf, ox, oy); + graphics.restore(); + } + } + + callFunc(this.m_polygonEndFunc); + } + finally { + Graphics_Restore(); + } +}; +// @endif view_set_viewport From c97a94b7c58d254ed2fea4c8272369cc4322c2ca Mon Sep 17 00:00:00 2001 From: Russell Kay Date: Sat, 21 Nov 2026 16:43:51 +0000 Subject: [PATCH 02/10] fix(html5): view_set_viewport keeps left-out fields, rebuilds the camera, hit-tests in every mouse path - A call that gives a polygon keeps the surface view_set_viewport made while it is the port's size (the port's size is the fallback when screenW/screenH are left out), and only remakes it when the size changes. A surface the game set is used and never freed; ours is only freed while it is still the view's surface. - Every field, startFunc/endFunc included, is applied only when it is given. polygons : [] turns the polygon off, frees our surface and clears the callbacks. screenW/H and roomRectW/H must be > 0. - The room rectangle rebuilds the camera's matrices, as the camera_set_view_* functions do. - DrawPolygon runs the callbacks it started with and looks the polygon and surface up again after startFunc, so startFunc can call view_set_viewport or free the surface. - The mouse uses the polygon only when it is drawn, and the rectangle test no longer sits in an @else (raw, unpreprocessed scripts ran both). window_views_mouse_get_x/y, GR_Window_Views_Convert and the touch path now skip a polygon view when the point is outside its triangles. Claude-Session: https://claude.ai/code/session_01EgTASfpMoBvX7TBDy119t5 --- scripts/_GameMaker.js | 2 +- scripts/device/iOS.js | 4 + scripts/functions/Function_Graphics.js | 128 ++++++++++++++----------- scripts/functions/Function_Window.js | 12 +++ scripts/yyIOManager.js | 7 +- scripts/yyView.js | 112 +++++++++++++++++++--- 6 files changed, 191 insertions(+), 74 deletions(-) diff --git a/scripts/_GameMaker.js b/scripts/_GameMaker.js index 9eeb0a58..acdbff7f 100644 --- a/scripts/_GameMaker.js +++ b/scripts/_GameMaker.js @@ -1308,7 +1308,7 @@ function StartRoom( _numb, _starting ) // @if function("view_set_viewport") // free the surfaces view_set_viewport created for the room's views for (var v = 0; v < g_RunRoom.m_Views.length; v++) { - if (g_RunRoom.m_Views[v]) g_RunRoom.m_Views[v].ResetPolygon(); + if (g_RunRoom.m_Views[v]) g_RunRoom.m_Views[v].ResetPolygon(v); } // @endif //var int_size = roughSizeOfObject_debug(g_RunRoom); diff --git a/scripts/device/iOS.js b/scripts/device/iOS.js index 57d4c04a..ec306d62 100644 --- a/scripts/device/iOS.js +++ b/scripts/device/iOS.js @@ -81,6 +81,10 @@ yyTouchEvent.prototype.SetTouchLocation = function(_x, _y) { //else { // CalcCanvasLocation(g_Surfaces.Get(pView.surface_id), g_CanvasRect); //} + // @if function("view_set_viewport") + // a view drawn through a view_set_viewport polygon only takes the points inside its triangles + if (pView.IsPolygonDrawn() && !pView.ContainsCanvasPoint(_x - g_CanvasRect.left, _y - g_CanvasRect.top)) continue; + // @endif if (((_x - g_CanvasRect.left) >= pView.scaledportx) && ((_x - g_CanvasRect.left) < pView.scaledportx2) && ((_y - g_CanvasRect.top) >= pView.scaledporty) && diff --git a/scripts/functions/Function_Graphics.js b/scripts/functions/Function_Graphics.js index 84de4593..32c2fd93 100644 --- a/scripts/functions/Function_Graphics.js +++ b/scripts/functions/Function_Graphics.js @@ -3003,6 +3003,8 @@ function view_set_surface_id(view, surface_id) { /// viewport structs, one per view from view 0, or a JSON string of one. Each struct can set /// polygons (x,y pairs in the view surface's pixels; [] turns the polygon off), /// screenX/Y/W/H (the port), roomRectX/Y/W/H (the room rectangle) and startFunc/endFunc. +/// A field that is left out keeps its current value. Like view_set_visible, this doesn't +/// turn views on: the room needs views enabled for the polygon to be drawn. /// // ############################################################################################# function view_set_viewport(_viewportData) { @@ -3033,81 +3035,99 @@ function view_set_viewport(_viewportData) { return; } - // A field that is left out keeps its current value - var getInt = function (_name, _fallback) { - var v = variable_struct_get(entry, _name); - return (v === undefined) ? _fallback : yyGetInt32(v); + // Only the fields that are given are applied; the rest keep their current values + var given = {}; + var getField = function (_name, _positive) { + if (!variable_struct_exists(entry, _name)) return undefined; + var v = yyGetInt32(variable_struct_get(entry, _name)); + if (_positive && v <= 0) { + yyError("view_set_viewport() - " + _name + " in entry " + index + " must be greater than 0"); + } + given[_name] = true; + return v; }; - var screenX = getInt("screenX", -1); - var screenY = getInt("screenY", -1); - var screenW = getInt("screenW", 0); - var screenH = getInt("screenH", 0); - var roomRectX = getInt("roomRectX", -1); - var roomRectY = getInt("roomRectY", -1); - var roomRectW = getInt("roomRectW", 0); - var roomRectH = getInt("roomRectH", 0); - - var polygons = variable_struct_get(entry, "polygons"); - if ((polygons !== undefined) && !(polygons instanceof Array)) { + var screenX = getField("screenX", false); + var screenY = getField("screenY", false); + var screenW = getField("screenW", true); + var screenH = getField("screenH", true); + var roomRectX = getField("roomRectX", false); + var roomRectY = getField("roomRectY", false); + var roomRectW = getField("roomRectW", true); + var roomRectH = getField("roomRectH", true); + + var hasPolygons = variable_struct_exists(entry, "polygons"); + var polygons = hasPolygons ? variable_struct_get(entry, "polygons") : undefined; + if (hasPolygons && !(polygons instanceof Array)) { yyError("view_set_viewport() - polygons in entry " + index + " is not an array"); return; } - var startFunc = variable_struct_get(entry, "startFunc"); - var endFunc = variable_struct_get(entry, "endFunc"); - var pView = g_RunRoom.m_Views[index]; if (!pView) continue; - // a polygon needs at least 3 x,y pairs; fewer (e.g. []) turns it off, back to a rectangular viewport - if (polygons !== undefined) { + // the port + if (given.screenX) g_pBuiltIn.view_xport[index] = screenX; + if (given.screenY) g_pBuiltIn.view_yport[index] = screenY; + if (given.screenW) g_pBuiltIn.view_wport[index] = screenW; + if (given.screenH) g_pBuiltIn.view_hport[index] = screenH; + pView.portx = g_pBuiltIn.view_xport[index]; + pView.porty = g_pBuiltIn.view_yport[index]; + pView.portw = g_pBuiltIn.view_wport[index]; + pView.porth = g_pBuiltIn.view_hport[index]; + + // the polygon: a polygon needs at least 3 x,y pairs; fewer (e.g. []) turns it off, back to a rectangular viewport + if (hasPolygons) { var points = []; for (var n = 0; n < polygons.length; n++) points.push(yyGetReal(polygons[n])); var indices = ViewPolygon_EarClip(points); - - pView.surface_id = g_pBuiltIn.view_surface_id[index]; - pView.ResetPolygon(); - g_pBuiltIn.view_surface_id[index] = pView.surface_id; - if (indices.length > 0) { var tris = []; for (var n = 0; n < indices.length; n++) tris.push(points[2 * indices[n]], points[2 * indices[n] + 1]); pView.m_polygon = tris; g_pBuiltIn.view_visible[index] = true; - if ((g_pBuiltIn.view_surface_id[index] == -1) && (screenW > 0) && (screenH > 0)) { - var surf = surface_create(screenW, screenH); - pView.m_polygonSurface = surf; - pView.surface_id = surf; - g_pBuiltIn.view_surface_id[index] = surf; - } + pView.visible = true; + } + else { + pView.ResetPolygon(index); } } - if ((screenW > 0) && (screenH > 0)) { - g_pBuiltIn.view_wport[index] = screenW; - g_pBuiltIn.view_hport[index] = screenH; + // the polygon is drawn from the view's surface: keep the one view_set_viewport made while it is + // still the right size, otherwise make one the size of the port + if ((pView.m_polygon != null) && (hasPolygons || given.screenW || given.screenH)) { + pView.EnsurePolygonSurface(index); } - if (screenX != -1) g_pBuiltIn.view_xport[index] = screenX; - if (screenY != -1) g_pBuiltIn.view_yport[index] = screenY; - if ((roomRectW > 0) && (roomRectH > 0)) { - g_pBuiltIn.view_wview[index] = roomRectW; - g_pBuiltIn.view_hview[index] = roomRectH; + + // startFunc and endFunc are only replaced when they are given + if (variable_struct_exists(entry, "startFunc")) { + var startFunc = variable_struct_get(entry, "startFunc"); + pView.m_polygonStartFunc = (typeof (startFunc) == "function") ? startFunc : null; + } + if (variable_struct_exists(entry, "endFunc")) { + var endFunc = variable_struct_get(entry, "endFunc"); + pView.m_polygonEndFunc = (typeof (endFunc) == "function") ? endFunc : null; } - if (roomRectX != -1) g_pBuiltIn.view_xview[index] = roomRectX; - if (roomRectY != -1) g_pBuiltIn.view_yview[index] = roomRectY; - - // startFunc and endFunc are replaced by every call (left out means none), as in the C++ runner - pView.m_polygonStartFunc = (typeof (startFunc) == "function") ? startFunc : null; - pView.m_polygonEndFunc = (typeof (endFunc) == "function") ? endFunc : null; - - // update the view's camera from the view - var camID = g_pBuiltIn.view_camera[index]; - var pCam = (camID != -1) ? g_pCameraManager.GetCamera(camID) : null; - if (pCam) { - pCam.SetViewX(g_pBuiltIn.view_xview[index]); - pCam.SetViewY(g_pBuiltIn.view_yview[index]); - pCam.SetViewWidth(g_pBuiltIn.view_wview[index]); - pCam.SetViewHeight(g_pBuiltIn.view_hview[index]); + + // the room rectangle, which is also the view's camera + if (given.roomRectX || given.roomRectY || given.roomRectW || given.roomRectH) { + if (given.roomRectX) g_pBuiltIn.view_xview[index] = roomRectX; + if (given.roomRectY) g_pBuiltIn.view_yview[index] = roomRectY; + if (given.roomRectW) g_pBuiltIn.view_wview[index] = roomRectW; + if (given.roomRectH) g_pBuiltIn.view_hview[index] = roomRectH; + + var camID = g_pBuiltIn.view_camera[index]; + var pCam = (camID != -1) ? g_pCameraManager.GetCamera(camID) : null; + if (pCam) { + pCam.SetViewX(g_pBuiltIn.view_xview[index]); + pCam.SetViewY(g_pBuiltIn.view_yview[index]); + pCam.SetViewWidth(g_pBuiltIn.view_wview[index]); + pCam.SetViewHeight(g_pBuiltIn.view_hview[index]); + // rebuild the matrices the camera draws with, as the camera_set_view_* functions do + var cx = pCam.GetViewX() + (pCam.GetViewWidth() * 0.5); + var cy = pCam.GetViewY() + (pCam.GetViewHeight() * 0.5); + if (pCam.IsOrthoProj()) pCam.Build2DView(cx, cy); + else pCam.Build3DView(cx, cy); + } } } }; diff --git a/scripts/functions/Function_Window.js b/scripts/functions/Function_Window.js index 5a205326..d71f8158 100644 --- a/scripts/functions/Function_Window.js +++ b/scripts/functions/Function_Window.js @@ -858,6 +858,10 @@ function GR_Window_Views_Convert(_x, _y, applyCamera = true) { continue; } + // @if function("view_set_viewport") + // a view drawn through a view_set_viewport polygon only takes the points inside its triangles + if (pView.IsPolygonDrawn() && !pView.ContainsCanvasPoint(_x - g_CanvasRect.left, _y - g_CanvasRect.top)) continue; + // @endif var m = pView.GetMouseCoords(_x, _y, applyCamera); // check that the results are within the view's region if (((m[0] >= pView.worldx) && (m[0] < pView.worldx + pView.worldw)) && @@ -887,6 +891,10 @@ function window_views_mouse_get_x() { continue; } + // @if function("view_set_viewport") + // a view drawn through a view_set_viewport polygon only takes the points inside its triangles + if (pView.IsPolygonDrawn() && !pView.ContainsCanvasPoint(g_pIOManager.MouseX - g_CanvasRect.left, g_pIOManager.MouseY - g_CanvasRect.top)) continue; + // @endif var mx = pView.GetMouseX(g_pIOManager.MouseX,g_pIOManager.MouseY); var my = pView.GetMouseY(g_pIOManager.MouseX,g_pIOManager.MouseY); // check that the results are within the view's region @@ -919,6 +927,10 @@ function window_views_mouse_get_y() { continue; } + // @if function("view_set_viewport") + // a view drawn through a view_set_viewport polygon only takes the points inside its triangles + if (pView.IsPolygonDrawn() && !pView.ContainsCanvasPoint(g_pIOManager.MouseX - g_CanvasRect.left, g_pIOManager.MouseY - g_CanvasRect.top)) continue; + // @endif var mx = pView.GetMouseX(g_pIOManager.MouseX,g_pIOManager.MouseY); var my = pView.GetMouseY(g_pIOManager.MouseX,g_pIOManager.MouseY); // check that the results are within the view's region diff --git a/scripts/yyIOManager.js b/scripts/yyIOManager.js index 2219c00c..44d05ce0 100644 --- a/scripts/yyIOManager.js +++ b/scripts/yyIOManager.js @@ -2130,12 +2130,11 @@ function IO_StartStep() //{ // CalcCanvasLocation(g_Surfaces.Get(pView.surface_id), g_CanvasRect); //} - // @if function("view_set_viewport") - // a view with a view_set_viewport polygon is hit tested against its triangles - var inView = pView.ContainsCanvasPoint(this.MouseX - g_CanvasRect.left, this.MouseY - g_CanvasRect.top); - // @else var inView = ((this.MouseX - g_CanvasRect.left) >= pView.scaledportx) && ((this.MouseX - g_CanvasRect.left) < pView.scaledportx2) && ((this.MouseY - g_CanvasRect.top) >= pView.scaledporty) && ((this.MouseY - g_CanvasRect.top) < pView.scaledporty2); + // @if function("view_set_viewport") + // a view drawn through a view_set_viewport polygon is hit tested against its triangles instead + if (pView.m_polygon != null) inView = pView.ContainsCanvasPoint(this.MouseX - g_CanvasRect.left, this.MouseY - g_CanvasRect.top); // @endif if (inView) { diff --git a/scripts/yyView.js b/scripts/yyView.js index e227d930..07f2d0f6 100644 --- a/scripts/yyView.js +++ b/scripts/yyView.js @@ -309,13 +309,84 @@ function ViewPolygon_EarClip(_points) // ############################################################################################# /// Function: -/// Drops the view's polygon, freeing the surface view_set_viewport created for it +/// The view's surface: the view_surface_id array when the view's index is given +/// (it is the newest value), otherwise the copy on the view /// // ############################################################################################# -yyView.prototype.ResetPolygon = function () { +yyView.prototype.PolygonViewSurface = function (_index) { + return (_index !== undefined) ? g_pBuiltIn.view_surface_id[_index] : this.surface_id; +}; + +// ############################################################################################# +/// Function: +/// Sets the view's surface, in the view_surface_id array too when the view's index is given +/// +// ############################################################################################# +yyView.prototype.SetPolygonViewSurface = function (_index, _surface) { + this.surface_id = _surface; + if (_index !== undefined) g_pBuiltIn.view_surface_id[_index] = _surface; +}; + +// ############################################################################################# +/// Function: +/// Forgets the surface view_set_viewport made if it isn't the view's surface any more +/// (the game set its own) or it has been freed. It is never freed here: once the game +/// has replaced it, it isn't the runner's to free. +/// +// ############################################################################################# +yyView.prototype.CheckPolygonSurface = function (_index) { + if (this.m_polygonSurface == -1) return; + if ((this.PolygonViewSurface(_index) != this.m_polygonSurface) || !surface_exists(this.m_polygonSurface)) { + this.m_polygonSurface = -1; + } +}; + +// ############################################################################################# +/// Function: +/// Makes sure a view with a polygon has a surface to draw it from. A surface the game +/// set is used as it is. Otherwise the one view_set_viewport made is kept while it is +/// the port's size, and made again (port sized) when it isn't or there isn't one. +/// +// ############################################################################################# +yyView.prototype.EnsurePolygonSurface = function (_index) { + this.CheckPolygonSurface(_index); + + var current = this.PolygonViewSurface(_index); + var w = (_index !== undefined) ? g_pBuiltIn.view_wport[_index] : this.portw; + var h = (_index !== undefined) ? g_pBuiltIn.view_hport[_index] : this.porth; + + if (this.m_polygonSurface != -1) { + // ours: keep it unless the port's size has changed + if ((surface_get_width(this.m_polygonSurface) == w) && (surface_get_height(this.m_polygonSurface) == h)) return; + surface_free(this.m_polygonSurface); + this.m_polygonSurface = -1; + current = -1; + } + else if ((current != -1) && surface_exists(current)) { + // the game's own surface + return; + } + + if ((w <= 0) || (h <= 0)) { + this.SetPolygonViewSurface(_index, -1); + return; + } + var surf = surface_create(w, h); + this.m_polygonSurface = surf; + this.SetPolygonViewSurface(_index, surf); +}; + +// ############################################################################################# +/// Function: +/// Drops the view's polygon and its callbacks, freeing the surface view_set_viewport made +/// for it if that is still the view's surface +/// +// ############################################################################################# +yyView.prototype.ResetPolygon = function (_index) { + this.CheckPolygonSurface(_index); if (this.m_polygonSurface != -1) { - if (surface_exists(this.m_polygonSurface)) surface_free(this.m_polygonSurface); - if (this.surface_id == this.m_polygonSurface) this.surface_id = -1; + surface_free(this.m_polygonSurface); + this.SetPolygonViewSurface(_index, -1); } this.m_polygon = null; this.m_polygonSurface = -1; @@ -323,14 +394,23 @@ yyView.prototype.ResetPolygon = function () { this.m_polygonEndFunc = null; }; +// ############################################################################################# +/// Function: +/// Is the polygon drawn? It needs triangles and a surface to draw them from. +/// +// ############################################################################################# +yyView.prototype.IsPolygonDrawn = function () { + return (this.m_polygon != null) && (this.m_polygon.length >= 6) && (this.surface_id != -1) && surface_exists(this.surface_id); +}; + // ############################################################################################# /// Function: /// Is (_x, _y), in view port units, inside the view's polygon? False if it has none. /// // ############################################################################################# yyView.prototype.PolygonContains = function (_x, _y) { + if (!this.IsPolygonDrawn()) return false; var tris = this.m_polygon; - if (tris == null || this.surface_id == -1) return false; // the triangles are in surface pixels, drawn at the view's port position var px = _x - this.portx; @@ -353,7 +433,7 @@ yyView.prototype.PolygonContains = function (_x, _y) { /// // ############################################################################################# yyView.prototype.ContainsCanvasPoint = function (_x, _y) { - if (this.m_polygon != null && this.surface_id != -1) { + if (this.IsPolygonDrawn()) { // canvas pixels back to view port units (see the scaledport* values set in DrawViews) var scaleX = (this.portw != 0) ? (this.scaledportw / this.portw) : 1; var scaleY = (this.porth != 0) ? (this.scaledporth / this.porth) : 1; @@ -372,10 +452,12 @@ yyView.prototype.ContainsCanvasPoint = function (_x, _y) { /// // ############################################################################################# yyView.prototype.DrawPolygon = function () { - var tris = this.m_polygon; - if (tris == null || this.surface_id == -1 || !surface_exists(this.surface_id)) return; - var pSurf = g_Surfaces.Get(this.surface_id); - if (!pSurf) return; + if (!this.IsPolygonDrawn()) return; + + // startFunc can change the view (even call view_set_viewport), so run the callbacks that are set + // now, and look the polygon and surface up again after startFunc + var startFunc = this.m_polygonStartFunc; + var endFunc = this.m_polygonEndFunc; // The triangles are in view port units, which land on the target at port * display scale, as the // rectangular views do (DrawViews sets their view ports the same way) @@ -388,11 +470,11 @@ yyView.prototype.DrawPolygon = function () { }; try { - callFunc(this.m_polygonStartFunc); + callFunc(startFunc); - // startFunc can replace the polygon with view_set_viewport - tris = this.m_polygon; - if (tris != null && tris.length >= 6) { + var pSurf = this.IsPolygonDrawn() ? g_Surfaces.Get(this.surface_id) : null; + if (pSurf) { + var tris = this.m_polygon; var ox = this.portx; var oy = this.porty; if (g_webGL) { @@ -433,7 +515,7 @@ yyView.prototype.DrawPolygon = function () { } } - callFunc(this.m_polygonEndFunc); + callFunc(endFunc); } finally { Graphics_Restore(); From 7bb05ba7adf21a544ad5572eabb3476fc45188d9 Mon Sep 17 00:00:00 2001 From: Russell Kay Date: Sat, 21 Nov 2026 16:46:54 +0000 Subject: [PATCH 03/10] fix(html5): free the view_set_viewport surface when the game sets its own As in the C++ runner (GMS2-Runner-Main#1082): when the game replaces the view's surface (view_set_surface_id, view_surface_id = , or view_surface_id[n] = , picked up when the arrays are copied into the views), the surface view_set_viewport made is freed and forgotten instead of leaking. The game's surface is never freed. Claude-Session: https://claude.ai/code/session_01EgTASfpMoBvX7TBDy119t5 --- scripts/functions/Function_Graphics.js | 7 ++++++- scripts/yyBuiltIn.js | 8 +++++++- scripts/yyRoom.js | 4 ++++ scripts/yyView.js | 13 +++++++++---- 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/scripts/functions/Function_Graphics.js b/scripts/functions/Function_Graphics.js index 32c2fd93..cd2d932a 100644 --- a/scripts/functions/Function_Graphics.js +++ b/scripts/functions/Function_Graphics.js @@ -2993,7 +2993,12 @@ function view_set_hport(view, hport) { }; function view_set_surface_id(view, surface_id) { - g_pBuiltIn.view_surface_id[yyGetInt32(view)] = yyGetInt32(surface_id); + var index = yyGetInt32(view); + g_pBuiltIn.view_surface_id[index] = yyGetInt32(surface_id); + // @if function("view_set_viewport") + // the game's own surface replaces the one view_set_viewport made for the view, which is freed + if (g_RunRoom && g_RunRoom.m_Views[index]) g_RunRoom.m_Views[index].CheckPolygonSurface(index); + // @endif }; // @if function("view_set_viewport") diff --git a/scripts/yyBuiltIn.js b/scripts/yyBuiltIn.js index 36daece0..5b6a816d 100644 --- a/scripts/yyBuiltIn.js +++ b/scripts/yyBuiltIn.js @@ -218,7 +218,13 @@ yyBuiltIn.prototype.set_view_vborder = function (_val) { g_pBuiltIn.view_vborder yyBuiltIn.prototype.set_view_hspeed = function (_val) { g_pBuiltIn.view_hspeed[0] = ~ ~_val; }; yyBuiltIn.prototype.set_view_vspeed = function (_val) { g_pBuiltIn.view_vspeed[0] = ~ ~_val; }; yyBuiltIn.prototype.set_view_object = function (_val) { g_pBuiltIn.view_object[0] = ~ ~_val; }; -yyBuiltIn.prototype.set_view_surface_id = function (_val) { g_pBuiltIn.view_surface_id[0] = ~ ~_val; }; +yyBuiltIn.prototype.set_view_surface_id = function (_val) { + g_pBuiltIn.view_surface_id[0] = ~ ~_val; + // @if function("view_set_viewport") + // the game's own surface replaces the one view_set_viewport made for the view, which is freed + if (g_RunRoom && g_RunRoom.m_Views[0]) g_RunRoom.m_Views[0].CheckPolygonSurface(0); + // @endif +}; yyBuiltIn.prototype.get_view_visible = function (_val) { return g_pBuiltIn.view_visible[0]; }; diff --git a/scripts/yyRoom.js b/scripts/yyRoom.js index bcc384a8..cbf4d4ce 100644 --- a/scripts/yyRoom.js +++ b/scripts/yyRoom.js @@ -973,6 +973,10 @@ yyRoom.prototype.CopyViewsFromArrays = function () { pView.hspeed = g_pBuiltIn.view_hspeed[index]; pView.vspeed = g_pBuiltIn.view_vspeed[index]; pView.objid = g_pBuiltIn.view_object[index]; + // @if function("view_set_viewport") + // view_surface_id[n] = s writes the array directly: free the surface view_set_viewport made if the game replaced it + pView.CheckPolygonSurface(index); + // @endif pView.surface_id = g_pBuiltIn.view_surface_id[index]; pView.cameraID = g_pBuiltIn.view_camera[index]; diff --git a/scripts/yyView.js b/scripts/yyView.js index 07f2d0f6..8ade84a2 100644 --- a/scripts/yyView.js +++ b/scripts/yyView.js @@ -329,14 +329,19 @@ yyView.prototype.SetPolygonViewSurface = function (_index, _surface) { // ############################################################################################# /// Function: -/// Forgets the surface view_set_viewport made if it isn't the view's surface any more -/// (the game set its own) or it has been freed. It is never freed here: once the game -/// has replaced it, it isn't the runner's to free. +/// Lets go of the surface view_set_viewport made once it isn't the view's surface any more. +/// If the game has set its own surface for the view, ours isn't used, so it is freed (as +/// in the C++ runner); the game's surface never is. If it has already been freed, it is +/// just forgotten. /// // ############################################################################################# yyView.prototype.CheckPolygonSurface = function (_index) { if (this.m_polygonSurface == -1) return; - if ((this.PolygonViewSurface(_index) != this.m_polygonSurface) || !surface_exists(this.m_polygonSurface)) { + if (!surface_exists(this.m_polygonSurface)) { + this.m_polygonSurface = -1; + } + else if (this.PolygonViewSurface(_index) != this.m_polygonSurface) { + surface_free(this.m_polygonSurface); this.m_polygonSurface = -1; } }; From fdc06e45e7aa6d865da7bbbf6dbb7efcd09c4f2e Mon Sep 17 00:00:00 2001 From: Russell Kay Date: Sat, 21 Nov 2026 16:55:56 +0000 Subject: [PATCH 04/10] fix(html5): remake a lost or resized view_set_viewport surface each frame; error on polygons that can't be clipped - Before a view with a polygon is drawn, the surface view_set_viewport made is made again if it has been lost (it no longer exists) or the port's size has changed (view_wport[n] =, view_set_wport). A surface the game set isn't touched. A lost surface stays ours until the game replaces it, so it can be remade. - The ear clip stops once a full pass over the remaining vertices finds no ear, and view_set_viewport raises "polygon N couldn't be triangulated, is it self-intersecting?", as the C++ runner and GMRT do, instead of running to a guard. Claude-Session: https://claude.ai/code/session_01EgTASfpMoBvX7TBDy119t5 --- scripts/functions/Function_Graphics.js | 4 ++ scripts/yyRoom.js | 6 +++ scripts/yyView.js | 56 +++++++++++++++++--------- 3 files changed, 47 insertions(+), 19 deletions(-) diff --git a/scripts/functions/Function_Graphics.js b/scripts/functions/Function_Graphics.js index cd2d932a..f5c1fc2d 100644 --- a/scripts/functions/Function_Graphics.js +++ b/scripts/functions/Function_Graphics.js @@ -3085,6 +3085,10 @@ function view_set_viewport(_viewportData) { var points = []; for (var n = 0; n < polygons.length; n++) points.push(yyGetReal(polygons[n])); var indices = ViewPolygon_EarClip(points); + if (indices == null) { + yyError("view_set_viewport(): polygon " + index + " couldn't be triangulated, is it self-intersecting?"); + return; + } if (indices.length > 0) { var tris = []; for (var n = 0; n < indices.length; n++) tris.push(points[2 * indices[n]], points[2 * indices[n] + 1]); diff --git a/scripts/yyRoom.js b/scripts/yyRoom.js index cbf4d4ce..81fa3338 100644 --- a/scripts/yyRoom.js +++ b/scripts/yyRoom.js @@ -3972,6 +3972,12 @@ yyRoom.prototype.DrawViews = function (r) { var graph = GlobalGraphicsHandle; Graphics_Save(); { + // @if function("view_set_viewport") + // make the surface view_set_viewport made again if it has been lost, or the port's size has changed + if (g_pCurrentView.m_polygon != null && this.m_enableviews) { + g_pCurrentView.RefreshPolygonSurface(i); + } + // @endif if (g_pCurrentView.surface_id != -1) { surface_set_target_system(g_pCurrentView.surface_id); diff --git a/scripts/yyView.js b/scripts/yyView.js index 8ade84a2..0ea7bc25 100644 --- a/scripts/yyView.js +++ b/scripts/yyView.js @@ -243,7 +243,8 @@ function CreateViewFromStorage( _pViewStorage ) // @if function("view_set_viewport") // ############################################################################################# /// Function: -/// Ear clips a simple polygon (x,y pairs) into a triangle list of vertex indices +/// Ear clips a simple polygon (x,y pairs) into a triangle list of vertex indices. Returns +/// [] for fewer than 3 points, or null if it can't be clipped (e.g. it intersects itself). /// // ############################################################################################# function ViewPolygon_EarClip(_points) @@ -275,9 +276,10 @@ function ViewPolygon_EarClip(_points) var convexSign = (area2 > 0) ? 1 : -1; var m = n; - var guard = n * n + 16; + var misses = 0; // vertices tried since the last ear: a full pass without one means it can't be clipped var i = 0; - while (m > 3 && guard-- > 0) { + while (m > 3) { + if (misses >= m) return null; var ip = (i == 0) ? m - 1 : i - 1; var inx = (i == m - 1) ? 0 : i + 1; var a = work[ip], b = work[i], c = work[inx]; @@ -296,9 +298,11 @@ function ViewPolygon_EarClip(_points) indices.push(a, b, c); work.splice(i, 1); m--; + misses = 0; } else { i++; + misses++; } if (i >= m) i = 0; } @@ -329,19 +333,16 @@ yyView.prototype.SetPolygonViewSurface = function (_index, _surface) { // ############################################################################################# /// Function: -/// Lets go of the surface view_set_viewport made once it isn't the view's surface any more. -/// If the game has set its own surface for the view, ours isn't used, so it is freed (as -/// in the C++ runner); the game's surface never is. If it has already been freed, it is -/// just forgotten. +/// Lets go of the surface view_set_viewport made once the game has set its own surface for +/// the view: ours isn't used any more, so it is freed (as in the C++ runner). The game's +/// surface never is. While ours is still the view's surface it is kept, even if it has +/// been lost, so that RefreshPolygonSurface can make it again. /// // ############################################################################################# yyView.prototype.CheckPolygonSurface = function (_index) { if (this.m_polygonSurface == -1) return; - if (!surface_exists(this.m_polygonSurface)) { - this.m_polygonSurface = -1; - } - else if (this.PolygonViewSurface(_index) != this.m_polygonSurface) { - surface_free(this.m_polygonSurface); + if (this.PolygonViewSurface(_index) != this.m_polygonSurface) { + if (surface_exists(this.m_polygonSurface)) surface_free(this.m_polygonSurface); this.m_polygonSurface = -1; } }; @@ -349,8 +350,8 @@ yyView.prototype.CheckPolygonSurface = function (_index) { // ############################################################################################# /// Function: /// Makes sure a view with a polygon has a surface to draw it from. A surface the game -/// set is used as it is. Otherwise the one view_set_viewport made is kept while it is -/// the port's size, and made again (port sized) when it isn't or there isn't one. +/// set is used as it is. Otherwise the one view_set_viewport made is kept while it exists +/// and is the port's size, and made again (port sized) when it isn't or there isn't one. /// // ############################################################################################# yyView.prototype.EnsurePolygonSurface = function (_index) { @@ -361,11 +362,12 @@ yyView.prototype.EnsurePolygonSurface = function (_index) { var h = (_index !== undefined) ? g_pBuiltIn.view_hport[_index] : this.porth; if (this.m_polygonSurface != -1) { - // ours: keep it unless the port's size has changed - if ((surface_get_width(this.m_polygonSurface) == w) && (surface_get_height(this.m_polygonSurface) == h)) return; - surface_free(this.m_polygonSurface); + // ours: keep it unless it has been lost or the port's size has changed + if (surface_exists(this.m_polygonSurface)) { + if ((surface_get_width(this.m_polygonSurface) == w) && (surface_get_height(this.m_polygonSurface) == h)) return; + surface_free(this.m_polygonSurface); + } this.m_polygonSurface = -1; - current = -1; } else if ((current != -1) && surface_exists(current)) { // the game's own surface @@ -381,6 +383,22 @@ yyView.prototype.EnsurePolygonSurface = function (_index) { this.SetPolygonViewSurface(_index, surf); }; +// ############################################################################################# +/// Function: +/// Called each frame before a view with a polygon is drawn: makes the surface +/// view_set_viewport made again if it has been lost, or the port's size has changed +/// (view_wport[n] = ..., view_set_wport). A surface the game set isn't touched. +/// +// ############################################################################################# +yyView.prototype.RefreshPolygonSurface = function (_index) { + if ((this.m_polygon == null) || (this.m_polygonSurface == -1)) return; + if (this.PolygonViewSurface(_index) != this.m_polygonSurface) { + this.CheckPolygonSurface(_index); + return; + } + this.EnsurePolygonSurface(_index); +}; + // ############################################################################################# /// Function: /// Drops the view's polygon and its callbacks, freeing the surface view_set_viewport made @@ -390,7 +408,7 @@ yyView.prototype.EnsurePolygonSurface = function (_index) { yyView.prototype.ResetPolygon = function (_index) { this.CheckPolygonSurface(_index); if (this.m_polygonSurface != -1) { - surface_free(this.m_polygonSurface); + if (surface_exists(this.m_polygonSurface)) surface_free(this.m_polygonSurface); this.SetPolygonViewSurface(_index, -1); } this.m_polygon = null; From c33acbbf04af50bb118548f9af9cbd79cda5e6de Mon Sep 17 00:00:00 2001 From: Russell Kay Date: Sat, 21 Nov 2026 17:01:57 +0000 Subject: [PATCH 05/10] fix(html5): view_set_viewport ear clip drops collinear vertices, as the C++ runner does A vertex on a straight line between its neighbours is dropped without a triangle, and a degenerate last triangle is skipped, matching GMS2-Runner-Main#1082 (0fd2439ad). A polygon with no area (all points on a line) now turns the polygon off, as in C++, instead of being drawn as a zero-area triangle. Claude-Session: https://claude.ai/code/session_01EgTASfpMoBvX7TBDy119t5 --- scripts/yyView.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/scripts/yyView.js b/scripts/yyView.js index 0ea7bc25..330fee97 100644 --- a/scripts/yyView.js +++ b/scripts/yyView.js @@ -287,15 +287,18 @@ function ViewPolygon_EarClip(_points) var bx = _points[2 * b], by = _points[2 * b + 1]; var cx = _points[2 * c], cy = _points[2 * c + 1]; - var isEar = crossSign(ax, ay, bx, by, cx, cy) == convexSign; + var turn = crossSign(ax, ay, bx, by, cx, cy); + var isEar = (turn == convexSign); for (var k = 0; isEar && k < m; k++) { if (k == ip || k == i || k == inx) continue; var v = work[k]; if (pointInTriangle(_points[2 * v], _points[2 * v + 1], ax, ay, bx, by, cx, cy)) isEar = false; } - if (isEar) { - indices.push(a, b, c); + // an ear is clipped off; a vertex on a straight line between its neighbours is dropped (it adds no area), + // as in the C++ runner + if (isEar || (turn == 0)) { + if (isEar) indices.push(a, b, c); work.splice(i, 1); m--; misses = 0; @@ -307,7 +310,10 @@ function ViewPolygon_EarClip(_points) if (i >= m) i = 0; } - if (m == 3) indices.push(work[0], work[1], work[2]); + if ((m == 3) && (crossSign(_points[2 * work[0]], _points[2 * work[0] + 1], _points[2 * work[1]], _points[2 * work[1] + 1], + _points[2 * work[2]], _points[2 * work[2] + 1]) != 0)) { + indices.push(work[0], work[1], work[2]); + } return indices; } From 919d9e5687bdc8d429986aa5932cdea126e8ef51 Mon Sep 17 00:00:00 2001 From: Russell Kay Date: Sat, 21 Nov 2026 17:09:30 +0000 Subject: [PATCH 06/10] fix(html5): view_set_viewport rejects polygons that cross themselves or have no area After clipping, the triangles' total area is compared with the polygon's (shoelace) area, with GMRT's tolerance (1e-3 of the area, at least 1). A polygon that crosses itself (a bow tie, a star) can still find ears, but its triangles then overlap or spill outside it and the areas differ. 3 or more points that give no triangle (all on one line) are rejected too. Both raise "view_set_viewport(): polygon N couldn't be split into triangles; it may cross itself, or have all its points on one line", as GMRT#7256 (46b4ea0729) and GMS2-Runner-Main#1082 do. Claude-Session: https://claude.ai/code/session_01EgTASfpMoBvX7TBDy119t5 --- scripts/functions/Function_Graphics.js | 2 +- scripts/yyView.js | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/scripts/functions/Function_Graphics.js b/scripts/functions/Function_Graphics.js index f5c1fc2d..e35a7cd7 100644 --- a/scripts/functions/Function_Graphics.js +++ b/scripts/functions/Function_Graphics.js @@ -3086,7 +3086,7 @@ function view_set_viewport(_viewportData) { for (var n = 0; n < polygons.length; n++) points.push(yyGetReal(polygons[n])); var indices = ViewPolygon_EarClip(points); if (indices == null) { - yyError("view_set_viewport(): polygon " + index + " couldn't be triangulated, is it self-intersecting?"); + yyError("view_set_viewport(): polygon " + index + " couldn't be split into triangles; it may cross itself, or have all its points on one line"); return; } if (indices.length > 0) { diff --git a/scripts/yyView.js b/scripts/yyView.js index 330fee97..d704e5b1 100644 --- a/scripts/yyView.js +++ b/scripts/yyView.js @@ -244,7 +244,8 @@ function CreateViewFromStorage( _pViewStorage ) // ############################################################################################# /// Function: /// Ear clips a simple polygon (x,y pairs) into a triangle list of vertex indices. Returns -/// [] for fewer than 3 points, or null if it can't be clipped (e.g. it intersects itself). +/// [] for fewer than 3 points, or null if it can't be split into triangles: no ear is found, +/// all the points are on one line, or the triangles don't cover the polygon's area (it crosses itself). /// // ############################################################################################# function ViewPolygon_EarClip(_points) @@ -314,6 +315,20 @@ function ViewPolygon_EarClip(_points) _points[2 * work[2]], _points[2 * work[2] + 1]) != 0)) { indices.push(work[0], work[1], work[2]); } + + // 3 or more points that give no triangle at all (all on one line) aren't a polygon either + if (indices.length == 0) return null; + + // The triangles of a simple polygon cover exactly its area. A polygon that crosses itself (a bow tie, say) can + // still be clipped, into triangles that overlap or spill outside it, and then the areas differ (as in GMRT). + var triangleArea2 = 0; + for (var t = 0; t + 2 < indices.length; t += 3) { + var ta = indices[t], tb = indices[t + 1], tc = indices[t + 2]; + triangleArea2 += Math.abs((_points[2 * tb] - _points[2 * ta]) * (_points[2 * tc + 1] - _points[2 * ta + 1]) - + (_points[2 * tb + 1] - _points[2 * ta + 1]) * (_points[2 * tc] - _points[2 * ta])); + } + var polygonArea2 = Math.abs(area2); + if (Math.abs(triangleArea2 - polygonArea2) > 1e-3 * Math.max(1, polygonArea2)) return null; return indices; } From f56d224efb3a075830c749af2c5a13ac1f2fac8b Mon Sep 17 00:00:00 2001 From: Russell Kay Date: Sat, 21 Nov 2026 17:30:30 +0000 Subject: [PATCH 07/10] view_set_viewport: a corner on a candidate ear's edge blocks it The point-in-triangle test was strict, so a reflex corner lying exactly on the ear's edge didn't block it, and a valid polygon such as [0,0, 10,0, 10,10, 5,5, 0,10] was clipped across its notch and rejected. Points on the boundary now block the ear, except one on the triangle's own corners (a duplicate vertex). Found in review of GMRT#7256. Claude-Session: https://claude.ai/code/session_01EgTASfpMoBvX7TBDy119t5 --- scripts/yyView.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/scripts/yyView.js b/scripts/yyView.js index d704e5b1..f44ec5f7 100644 --- a/scripts/yyView.js +++ b/scripts/yyView.js @@ -259,10 +259,15 @@ function ViewPolygon_EarClip(_points) return (v > 0) ? 1 : ((v < 0) ? -1 : 0); }; var pointInTriangle = function (px, py, ax, ay, bx, by, cx, cy) { + // a point on one of the triangle's corners (a duplicate vertex) doesn't block it + if ((px == ax && py == ay) || (px == bx && py == by) || (px == cx && py == cy)) return false; var s1 = crossSign(ax, ay, bx, by, px, py); var s2 = crossSign(bx, by, cx, cy, px, py); var s3 = crossSign(cx, cy, ax, ay, px, py); - return (s1 > 0 && s2 > 0 && s3 > 0) || (s1 < 0 && s2 < 0 && s3 < 0); + // inside or on an edge: a reflex corner lying on the candidate ear's edge must block it + var hasNeg = (s1 < 0) || (s2 < 0) || (s3 < 0); + var hasPos = (s1 > 0) || (s2 > 0) || (s3 > 0); + return !(hasNeg && hasPos); }; var work = []; From 39aefa365f254cbac5db988a50111994884ca36b Mon Sep 17 00:00:00 2001 From: Russell Kay Date: Sat, 21 Nov 2026 17:33:26 +0000 Subject: [PATCH 08/10] fix(html5): view_set_viewport takes a method or a script function for startFunc/endFunc startFunc and endFunc are resolved like getFunction does for the array functions: a JS function (a method, or a script function) is used as it is, and a script index (how a script function reaches the runner in these builds) is looked up in JSON_game.Scripts. undefined clears the callback, and anything else raises an error naming the field. They're resolved before any of the entry is applied. A method runs on the struct or instance it's bound to, a script function on global. Before this, a script function given as endFunc was silently ignored. Claude-Session: https://claude.ai/code/session_01EgTASfpMoBvX7TBDy119t5 --- scripts/functions/Function_Graphics.js | 26 ++++++++++++++++++-------- scripts/yyView.js | 5 ++++- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/scripts/functions/Function_Graphics.js b/scripts/functions/Function_Graphics.js index e35a7cd7..15e93d87 100644 --- a/scripts/functions/Function_Graphics.js +++ b/scripts/functions/Function_Graphics.js @@ -3067,6 +3067,22 @@ function view_set_viewport(_viewportData) { return; } + // startFunc and endFunc take a method or a script function (a script index in older projects); + // undefined clears them + var getCallable = function (_name) { + if (!variable_struct_exists(entry, _name)) return undefined; + var v = variable_struct_get(entry, _name); + if (v === undefined) return null; + if (typeof (v) == "function") return v; + if ((typeof (v) == "number") && (v >= 100000) && JSON_game && JSON_game.Scripts) { + var script = JSON_game.Scripts[v - 100000]; + if (typeof (script) == "function") return script; + } + yyError("view_set_viewport() - " + _name + " in entry " + index + " is not a method or script function"); + }; + var startFunc = getCallable("startFunc"); + var endFunc = getCallable("endFunc"); + var pView = g_RunRoom.m_Views[index]; if (!pView) continue; @@ -3108,14 +3124,8 @@ function view_set_viewport(_viewportData) { } // startFunc and endFunc are only replaced when they are given - if (variable_struct_exists(entry, "startFunc")) { - var startFunc = variable_struct_get(entry, "startFunc"); - pView.m_polygonStartFunc = (typeof (startFunc) == "function") ? startFunc : null; - } - if (variable_struct_exists(entry, "endFunc")) { - var endFunc = variable_struct_get(entry, "endFunc"); - pView.m_polygonEndFunc = (typeof (endFunc) == "function") ? endFunc : null; - } + if (startFunc !== undefined) pView.m_polygonStartFunc = startFunc; + if (endFunc !== undefined) pView.m_polygonEndFunc = endFunc; // the room rectangle, which is also the view's camera if (given.roomRectX || given.roomRectY || given.roomRectW || given.roomRectH) { diff --git a/scripts/yyView.js b/scripts/yyView.js index f44ec5f7..50afb390 100644 --- a/scripts/yyView.js +++ b/scripts/yyView.js @@ -514,8 +514,11 @@ yyView.prototype.DrawPolygon = function () { Graphics_SetViewPort(0, 0, g_ApplicationWidth, g_ApplicationHeight); Graphics_SetViewArea(0, 0, g_ApplicationWidth / g_DisplayScaleX, g_ApplicationHeight / g_DisplayScaleY, 0); + // a method runs on the struct or instance it is bound to; a script function on global var callFunc = function (_func) { - if (typeof (_func) == "function") _func(global, global); + if (typeof (_func) != "function") return; + var self = _func.boundObject ?? global; + _func(self, self); }; try { From 89315d3f8ccec74cb60a32474db86dcdcb241588 Mon Sep 17 00:00:00 2001 From: Russell Kay Date: Sat, 21 Nov 2026 17:36:01 +0000 Subject: [PATCH 09/10] fix(html5): tell the view_set_viewport surface from a later surface that reuses its ID Surface IDs are reused as soon as they're freed, so after the game frees the view's surface the next surface_create anywhere gets the same ID. The runner now keeps the surface object it created as well as the ID, and treats the surface as its own only while that ID still holds that object. Otherwise the ID is forgotten without being freed: - the per-frame check makes the view a new surface, leaving the game's alone; - when the game sets the view's surface (view_set_surface_id, view_surface_id =) to a surface that reused the ID, it is the game's, so it is used and never freed. Claude-Session: https://claude.ai/code/session_01EgTASfpMoBvX7TBDy119t5 --- scripts/functions/Function_Graphics.js | 2 +- scripts/yyBuiltIn.js | 2 +- scripts/yyView.js | 64 +++++++++++++++++++------- 3 files changed, 50 insertions(+), 18 deletions(-) diff --git a/scripts/functions/Function_Graphics.js b/scripts/functions/Function_Graphics.js index 15e93d87..629d0c09 100644 --- a/scripts/functions/Function_Graphics.js +++ b/scripts/functions/Function_Graphics.js @@ -2997,7 +2997,7 @@ function view_set_surface_id(view, surface_id) { g_pBuiltIn.view_surface_id[index] = yyGetInt32(surface_id); // @if function("view_set_viewport") // the game's own surface replaces the one view_set_viewport made for the view, which is freed - if (g_RunRoom && g_RunRoom.m_Views[index]) g_RunRoom.m_Views[index].CheckPolygonSurface(index); + if (g_RunRoom && g_RunRoom.m_Views[index]) g_RunRoom.m_Views[index].CheckPolygonSurface(index, true); // @endif }; diff --git a/scripts/yyBuiltIn.js b/scripts/yyBuiltIn.js index 5b6a816d..73bc3734 100644 --- a/scripts/yyBuiltIn.js +++ b/scripts/yyBuiltIn.js @@ -222,7 +222,7 @@ yyBuiltIn.prototype.set_view_surface_id = function (_val) { g_pBuiltIn.view_surface_id[0] = ~ ~_val; // @if function("view_set_viewport") // the game's own surface replaces the one view_set_viewport made for the view, which is freed - if (g_RunRoom && g_RunRoom.m_Views[0]) g_RunRoom.m_Views[0].CheckPolygonSurface(0); + if (g_RunRoom && g_RunRoom.m_Views[0]) g_RunRoom.m_Views[0].CheckPolygonSurface(0, true); // @endif }; diff --git a/scripts/yyView.js b/scripts/yyView.js index 50afb390..14b228c4 100644 --- a/scripts/yyView.js +++ b/scripts/yyView.js @@ -59,6 +59,7 @@ function yyView() // A view // view_set_viewport (GMS2-Runner-Main#1076): the view's surface is drawn through these triangles instead of as a rectangle this.m_polygon = null; // triangle list as x,y pairs in the view surface's pixels, or null this.m_polygonSurface = -1; // surface view_set_viewport created for the view (freed with the polygon), or -1 + this.m_polygonSurfaceObj = null; // ...and its surface object, to tell it from a later surface that reuses its ID this.m_polygonStartFunc = null; // called before the triangles are drawn (e.g. to set a shader) this.m_polygonEndFunc = null; // called after they are drawn } @@ -357,43 +358,73 @@ yyView.prototype.SetPolygonViewSurface = function (_index, _surface) { if (_index !== undefined) g_pBuiltIn.view_surface_id[_index] = _surface; }; +// ############################################################################################# +/// Function: +/// Is the surface view_set_viewport made still there, and still ours? Surface IDs are reused +/// as soon as they are freed, so the ID alone isn't enough: once ours has been freed, the next +/// surface_create anywhere in the game can get the same ID. It is ours only while that ID +/// still holds the surface object it was created with. +/// +// ############################################################################################# +yyView.prototype.OwnsPolygonSurface = function () { + return (this.m_polygonSurface != -1) && (this.m_polygonSurfaceObj != null) && + (g_Surfaces.Get(this.m_polygonSurface) === this.m_polygonSurfaceObj); +}; + +// ############################################################################################# +/// Function: +/// Forgets the surface view_set_viewport made (without freeing it) +/// +// ############################################################################################# +yyView.prototype.ForgetPolygonSurface = function () { + this.m_polygonSurface = -1; + this.m_polygonSurfaceObj = null; +}; + // ############################################################################################# /// Function: /// Lets go of the surface view_set_viewport made once the game has set its own surface for /// the view: ours isn't used any more, so it is freed (as in the C++ runner). The game's -/// surface never is. While ours is still the view's surface it is kept, even if it has -/// been lost, so that RefreshPolygonSurface can make it again. +/// surface never is. _setByGame is true when the game has just set the view's surface +/// (view_set_surface_id, view_surface_id =): if that reused our old ID, it is the game's. +/// While ours is still the view's surface it is kept, even if it has been lost, so that +/// RefreshPolygonSurface can make it again. /// // ############################################################################################# -yyView.prototype.CheckPolygonSurface = function (_index) { +yyView.prototype.CheckPolygonSurface = function (_index, _setByGame) { if (this.m_polygonSurface == -1) return; + var owns = this.OwnsPolygonSurface(); if (this.PolygonViewSurface(_index) != this.m_polygonSurface) { - if (surface_exists(this.m_polygonSurface)) surface_free(this.m_polygonSurface); - this.m_polygonSurface = -1; + if (owns) surface_free(this.m_polygonSurface); + this.ForgetPolygonSurface(); + } + else if (_setByGame && !owns) { + this.ForgetPolygonSurface(); } }; // ############################################################################################# /// Function: /// Makes sure a view with a polygon has a surface to draw it from. A surface the game -/// set is used as it is. Otherwise the one view_set_viewport made is kept while it exists -/// and is the port's size, and made again (port sized) when it isn't or there isn't one. +/// set is used as it is. Otherwise the one view_set_viewport made is kept while it is ours +/// and the port's size, and made again (port sized) when it isn't or there isn't one. /// // ############################################################################################# yyView.prototype.EnsurePolygonSurface = function (_index) { - this.CheckPolygonSurface(_index); + this.CheckPolygonSurface(_index, false); var current = this.PolygonViewSurface(_index); var w = (_index !== undefined) ? g_pBuiltIn.view_wport[_index] : this.portw; var h = (_index !== undefined) ? g_pBuiltIn.view_hport[_index] : this.porth; if (this.m_polygonSurface != -1) { - // ours: keep it unless it has been lost or the port's size has changed - if (surface_exists(this.m_polygonSurface)) { + // ours: keep it unless it has been lost (freed, perhaps with its ID now used by another surface, + // which is left alone) or the port's size has changed + if (this.OwnsPolygonSurface()) { if ((surface_get_width(this.m_polygonSurface) == w) && (surface_get_height(this.m_polygonSurface) == h)) return; surface_free(this.m_polygonSurface); } - this.m_polygonSurface = -1; + this.ForgetPolygonSurface(); } else if ((current != -1) && surface_exists(current)) { // the game's own surface @@ -406,6 +437,7 @@ yyView.prototype.EnsurePolygonSurface = function (_index) { } var surf = surface_create(w, h); this.m_polygonSurface = surf; + this.m_polygonSurfaceObj = g_Surfaces.Get(surf); this.SetPolygonViewSurface(_index, surf); }; @@ -419,7 +451,7 @@ yyView.prototype.EnsurePolygonSurface = function (_index) { yyView.prototype.RefreshPolygonSurface = function (_index) { if ((this.m_polygon == null) || (this.m_polygonSurface == -1)) return; if (this.PolygonViewSurface(_index) != this.m_polygonSurface) { - this.CheckPolygonSurface(_index); + this.CheckPolygonSurface(_index, false); return; } this.EnsurePolygonSurface(_index); @@ -428,17 +460,17 @@ yyView.prototype.RefreshPolygonSurface = function (_index) { // ############################################################################################# /// Function: /// Drops the view's polygon and its callbacks, freeing the surface view_set_viewport made -/// for it if that is still the view's surface +/// for it if that is still the view's surface (and still ours) /// // ############################################################################################# yyView.prototype.ResetPolygon = function (_index) { - this.CheckPolygonSurface(_index); + this.CheckPolygonSurface(_index, false); if (this.m_polygonSurface != -1) { - if (surface_exists(this.m_polygonSurface)) surface_free(this.m_polygonSurface); + if (this.OwnsPolygonSurface()) surface_free(this.m_polygonSurface); this.SetPolygonViewSurface(_index, -1); } this.m_polygon = null; - this.m_polygonSurface = -1; + this.ForgetPolygonSurface(); this.m_polygonStartFunc = null; this.m_polygonEndFunc = null; }; From 1084c17cc5dae5de87f51e3d33f7f4359d02d97e Mon Sep 17 00:00:00 2001 From: Russell Kay Date: Sat, 21 Nov 2026 18:11:40 +0000 Subject: [PATCH 10/10] fix(html5): a view_set_viewport surface stays the view's when the game resizes it surface_resize puts a new object under the same surface ID (surface_create with _forceid), so the object-identity check from 89315d3 treated a resized surface as not ours: the per-frame check forgot it without freeing it and made a new one, leaking a surface per frame for a game that kept resizing its view surface. Our surface object is now tagged with the view that made it (m_viewPolygonOwner), both _forceid paths in surface_create (Canvas and WebGL) carry the tag over to the replacement, and ownership checks the tag. A surface that merely reused a freed ID has no tag, so the ID-reuse protection stays. Claude-Session: https://claude.ai/code/session_01EgTASfpMoBvX7TBDy119t5 --- scripts/functions/Function_Surface.js | 5 +++++ scripts/yyView.js | 15 ++++++++------- scripts/yyWebGL.js | 5 +++++ 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/scripts/functions/Function_Surface.js b/scripts/functions/Function_Surface.js index a24a1dbb..02e7c5e8 100644 --- a/scripts/functions/Function_Surface.js +++ b/scripts/functions/Function_Surface.js @@ -169,6 +169,11 @@ function surface_create_RELEASE(_w, _h, _format, _forceid) if( _forceid != undefined ) { //replace existing surface at index _forceid + // @if function("view_set_viewport") + // a surface view_set_viewport made stays the view's when it is resized + var pOldSurf = g_Surfaces.Get( _forceid ); + if (pOldSurf && pOldSurf.m_viewPolygonOwner) pSurf.m_viewPolygonOwner = pOldSurf.m_viewPolygonOwner; + // @endif g_Surfaces.Set( _forceid, pSurf); pTPE.tp = _forceid; } diff --git a/scripts/yyView.js b/scripts/yyView.js index 14b228c4..2159ce14 100644 --- a/scripts/yyView.js +++ b/scripts/yyView.js @@ -59,7 +59,6 @@ function yyView() // A view // view_set_viewport (GMS2-Runner-Main#1076): the view's surface is drawn through these triangles instead of as a rectangle this.m_polygon = null; // triangle list as x,y pairs in the view surface's pixels, or null this.m_polygonSurface = -1; // surface view_set_viewport created for the view (freed with the polygon), or -1 - this.m_polygonSurfaceObj = null; // ...and its surface object, to tell it from a later surface that reuses its ID this.m_polygonStartFunc = null; // called before the triangles are drawn (e.g. to set a shader) this.m_polygonEndFunc = null; // called after they are drawn } @@ -362,13 +361,15 @@ yyView.prototype.SetPolygonViewSurface = function (_index, _surface) { /// Function: /// Is the surface view_set_viewport made still there, and still ours? Surface IDs are reused /// as soon as they are freed, so the ID alone isn't enough: once ours has been freed, the next -/// surface_create anywhere in the game can get the same ID. It is ours only while that ID -/// still holds the surface object it was created with. +/// surface_create anywhere in the game can get the same ID. Our surface object is tagged with +/// the view that made it (m_viewPolygonOwner), and surface_resize carries the tag over to the +/// object that replaces it, so a resized surface is still ours. /// // ############################################################################################# yyView.prototype.OwnsPolygonSurface = function () { - return (this.m_polygonSurface != -1) && (this.m_polygonSurfaceObj != null) && - (g_Surfaces.Get(this.m_polygonSurface) === this.m_polygonSurfaceObj); + if (this.m_polygonSurface == -1) return false; + var pSurf = g_Surfaces.Get(this.m_polygonSurface); + return (pSurf != null) && (pSurf.m_viewPolygonOwner === this); }; // ############################################################################################# @@ -378,7 +379,6 @@ yyView.prototype.OwnsPolygonSurface = function () { // ############################################################################################# yyView.prototype.ForgetPolygonSurface = function () { this.m_polygonSurface = -1; - this.m_polygonSurfaceObj = null; }; // ############################################################################################# @@ -437,7 +437,8 @@ yyView.prototype.EnsurePolygonSurface = function (_index) { } var surf = surface_create(w, h); this.m_polygonSurface = surf; - this.m_polygonSurfaceObj = g_Surfaces.Get(surf); + var pSurf = g_Surfaces.Get(surf); + if (pSurf) pSurf.m_viewPolygonOwner = this; this.SetPolygonViewSurface(_index, surf); }; diff --git a/scripts/yyWebGL.js b/scripts/yyWebGL.js index d5d55f17..0256def2 100644 --- a/scripts/yyWebGL.js +++ b/scripts/yyWebGL.js @@ -3875,6 +3875,11 @@ function WebGL_surface_create_RELEASE(_w, _h, _format, _forceid) { if( _forceid != undefined ) { //replace existing surface at index _forceid + // @if function("view_set_viewport") + // a surface view_set_viewport made stays the view's when it is resized + var pOldSurf = g_Surfaces.Get( _forceid ); + if (pOldSurf && pOldSurf.m_viewPolygonOwner) pTPE.m_viewPolygonOwner = pOldSurf.m_viewPolygonOwner; + // @endif g_Surfaces.Set( _forceid, pTPE); pTPE.tp = _forceid; }