From 8ef1195a49371b77c2d02e8f18aa339042b147e7 Mon Sep 17 00:00:00 2001 From: Russell Kay Date: Sat, 21 Nov 2026 18:32:42 +0000 Subject: [PATCH 1/2] fix(html5): view_set_viewport follow-ups: polygon off from a Draw event, camera fields, bad values - Turning a polygon off (polygons : []) from a Draw event, while the view's surface is the render target, no longer unbalances the surface stack. DrawViews resets the target only if it set it, rather than checking the view's surface again after the Draw event. The surface view_set_viewport made is freed once it's off the surface stack (surface_free won't free a target), instead of leaking. - The room rectangle only changes the camera fields that are given, so roomRectX alone doesn't undo a camera_set_view_pos y, and a call without roomRect fields leaves the camera alone. - polygons with an odd number of values, and non-finite values (NaN, infinity) in polygons or the screen/roomRect fields, raise errors. Found by GM-TestFramework#386 at 3cd731d. Claude-Session: https://claude.ai/code/session_01EgTASfpMoBvX7TBDy119t5 --- scripts/functions/Function_Graphics.js | 28 ++++++++++++---- scripts/yyRoom.js | 10 ++++-- scripts/yyView.js | 45 ++++++++++++++++++++++++-- 3 files changed, 72 insertions(+), 11 deletions(-) diff --git a/scripts/functions/Function_Graphics.js b/scripts/functions/Function_Graphics.js index 629d0c09..5f153d10 100644 --- a/scripts/functions/Function_Graphics.js +++ b/scripts/functions/Function_Graphics.js @@ -3044,7 +3044,11 @@ function view_set_viewport(_viewportData) { var given = {}; var getField = function (_name, _positive) { if (!variable_struct_exists(entry, _name)) return undefined; - var v = yyGetInt32(variable_struct_get(entry, _name)); + var raw = variable_struct_get(entry, _name); + if (!isFinite(yyGetReal(raw))) { + yyError("view_set_viewport() - " + _name + " in entry " + index + " is not a finite number"); + } + var v = yyGetInt32(raw); if (_positive && v <= 0) { yyError("view_set_viewport() - " + _name + " in entry " + index + " must be greater than 0"); } @@ -3066,6 +3070,10 @@ function view_set_viewport(_viewportData) { yyError("view_set_viewport() - polygons in entry " + index + " is not an array"); return; } + if (hasPolygons && ((polygons.length % 2) != 0)) { + yyError("view_set_viewport() - polygons in entry " + index + " has an odd number of values; it needs x,y pairs"); + return; + } // startFunc and endFunc take a method or a script function (a script index in older projects); // undefined clears them @@ -3099,7 +3107,13 @@ function view_set_viewport(_viewportData) { // 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])); + for (var n = 0; n < polygons.length; n++) { + var value = yyGetReal(polygons[n]); + if (!isFinite(value)) { + yyError("view_set_viewport() - polygons in entry " + index + " has a value that is not a finite number"); + } + points.push(value); + } var indices = ViewPolygon_EarClip(points); if (indices == null) { yyError("view_set_viewport(): polygon " + index + " couldn't be split into triangles; it may cross itself, or have all its points on one line"); @@ -3137,10 +3151,12 @@ function view_set_viewport(_viewportData) { 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]); + // only the fields that are given: the camera keeps the rest (it may have been moved with + // camera_set_view_pos, say, which doesn't change the view_xview arrays) + if (given.roomRectX) pCam.SetViewX(roomRectX); + if (given.roomRectY) pCam.SetViewY(roomRectY); + if (given.roomRectW) pCam.SetViewWidth(roomRectW); + if (given.roomRectH) pCam.SetViewHeight(roomRectH); // 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); diff --git a/scripts/yyRoom.js b/scripts/yyRoom.js index 81fa3338..325002ac 100644 --- a/scripts/yyRoom.js +++ b/scripts/yyRoom.js @@ -3978,9 +3978,13 @@ yyRoom.prototype.DrawViews = function (r) { g_pCurrentView.RefreshPolygonSurface(i); } // @endif + // reset the target below only if it was set here: the Draw event can change the view's surface + // (e.g. view_set_viewport turning its polygon off) + var viewSurfaceTargetSet = false; if (g_pCurrentView.surface_id != -1) { surface_set_target_system(g_pCurrentView.surface_id); + viewSurfaceTargetSet = true; } //scaled port SHOULD BE port coords in final canvas space ( for canvas mouse -> view/world mouse coords ) @@ -4032,13 +4036,15 @@ yyRoom.prototype.DrawViews = function (r) { this.DrawTheRoom(g_roomExtents, view_width, view_height); - if (g_pCurrentView.surface_id != -1) { + if (viewSurfaceTargetSet) { 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) + // now the view's surface isn't the target, draw it through its view_set_viewport polygon (if it has one), + // and free any view_set_viewport surface that was let go while it was the target g_pCurrentView.DrawPolygon(); + ViewPolygon_FlushSurfaceFrees(); // @endif Current_View++; diff --git a/scripts/yyView.js b/scripts/yyView.js index 2159ce14..e7ce8047 100644 --- a/scripts/yyView.js +++ b/scripts/yyView.js @@ -357,6 +357,45 @@ yyView.prototype.SetPolygonViewSurface = function (_index, _surface) { if (_index !== undefined) g_pBuiltIn.view_surface_id[_index] = _surface; }; +// Surfaces view_set_viewport made that were still a render target when they were let go (e.g. the +// polygon was turned off from a Draw event while the view was being drawn into its surface): +// { id, surface } pairs, freed by ViewPolygon_FlushSurfaceFrees once they're off the surface stack +var g_ViewPolygonSurfacesToFree = []; + +// ############################################################################################# +/// Function: +/// Frees a surface view_set_viewport made, or, if it is still a render target, frees it +/// once it isn't (surface_free won't free a surface on the surface stack) +/// +// ############################################################################################# +function ViewPolygon_FreeSurface(_id) +{ + if (CheckForSurface(_id)) { + g_ViewPolygonSurfacesToFree.push({ id: _id, surface: g_Surfaces.Get(_id) }); + return; + } + surface_free(_id); +} + +// ############################################################################################# +/// Function: +/// Frees the surfaces ViewPolygon_FreeSurface had to leave, once they're off the surface +/// stack. One that has been freed already (its ID may now hold another surface) is dropped. +/// +// ############################################################################################# +function ViewPolygon_FlushSurfaceFrees() +{ + if (g_ViewPolygonSurfacesToFree.length == 0) return; + var keep = []; + for (var n = 0; n < g_ViewPolygonSurfacesToFree.length; n++) { + var entry = g_ViewPolygonSurfacesToFree[n]; + if (g_Surfaces.Get(entry.id) !== entry.surface) continue; + if (CheckForSurface(entry.id)) keep.push(entry); + else surface_free(entry.id); + } + g_ViewPolygonSurfacesToFree = keep; +} + // ############################################################################################# /// Function: /// Is the surface view_set_viewport made still there, and still ours? Surface IDs are reused @@ -395,7 +434,7 @@ yyView.prototype.CheckPolygonSurface = function (_index, _setByGame) { if (this.m_polygonSurface == -1) return; var owns = this.OwnsPolygonSurface(); if (this.PolygonViewSurface(_index) != this.m_polygonSurface) { - if (owns) surface_free(this.m_polygonSurface); + if (owns) ViewPolygon_FreeSurface(this.m_polygonSurface); this.ForgetPolygonSurface(); } else if (_setByGame && !owns) { @@ -422,7 +461,7 @@ yyView.prototype.EnsurePolygonSurface = function (_index) { // 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); + ViewPolygon_FreeSurface(this.m_polygonSurface); } this.ForgetPolygonSurface(); } @@ -467,7 +506,7 @@ yyView.prototype.RefreshPolygonSurface = function (_index) { yyView.prototype.ResetPolygon = function (_index) { this.CheckPolygonSurface(_index, false); if (this.m_polygonSurface != -1) { - if (this.OwnsPolygonSurface()) surface_free(this.m_polygonSurface); + if (this.OwnsPolygonSurface()) ViewPolygon_FreeSurface(this.m_polygonSurface); this.SetPolygonViewSurface(_index, -1); } this.m_polygon = null; From 75a27d3869433f1489870f1035e0f5fec5ace42f Mon Sep 17 00:00:00 2001 From: Russell Kay Date: Sat, 21 Nov 2026 18:48:02 +0000 Subject: [PATCH 2/2] fix(html5): view_set_viewport review nits: reject before changing anything, flush after the views, exact target flag - The polygon's values are checked (finite) and the polygon triangulated before any of the entry is applied, next to the odd-length check, so a rejected call changes nothing. - DrawViews also frees queued view_set_viewport surfaces once after the views loop, so a surface let go while the game had it as the target outside DrawViews is freed even if no view is visible. - viewSurfaceTargetSet is true only when surface_set_target_system actually pushed the surface (it pushes nothing for a surface that no longer exists). Claude-Session: https://claude.ai/code/session_01EgTASfpMoBvX7TBDy119t5 --- scripts/functions/Function_Graphics.js | 30 +++++++++++++++----------- scripts/yyRoom.js | 9 +++++++- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/scripts/functions/Function_Graphics.js b/scripts/functions/Function_Graphics.js index 5f153d10..1f8e8965 100644 --- a/scripts/functions/Function_Graphics.js +++ b/scripts/functions/Function_Graphics.js @@ -3074,6 +3074,23 @@ function view_set_viewport(_viewportData) { yyError("view_set_viewport() - polygons in entry " + index + " has an odd number of values; it needs x,y pairs"); return; } + // check and triangulate the polygon before anything is changed, so a call that is rejected changes nothing + var points = null, indices = null; + if (hasPolygons) { + points = []; + for (var n = 0; n < polygons.length; n++) { + var value = yyGetReal(polygons[n]); + if (!isFinite(value)) { + yyError("view_set_viewport() - polygons in entry " + index + " has a value that is not a finite number"); + } + points.push(value); + } + indices = ViewPolygon_EarClip(points); + if (indices == null) { + 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; + } + } // startFunc and endFunc take a method or a script function (a script index in older projects); // undefined clears them @@ -3106,19 +3123,6 @@ function view_set_viewport(_viewportData) { // 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++) { - var value = yyGetReal(polygons[n]); - if (!isFinite(value)) { - yyError("view_set_viewport() - polygons in entry " + index + " has a value that is not a finite number"); - } - points.push(value); - } - var indices = ViewPolygon_EarClip(points); - if (indices == null) { - 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) { 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 325002ac..ac76d8ef 100644 --- a/scripts/yyRoom.js +++ b/scripts/yyRoom.js @@ -3980,11 +3980,13 @@ yyRoom.prototype.DrawViews = function (r) { // @endif // reset the target below only if it was set here: the Draw event can change the view's surface // (e.g. view_set_viewport turning its polygon off) + // (surface_set_target_system pushes nothing for a surface that no longer exists) var viewSurfaceTargetSet = false; if (g_pCurrentView.surface_id != -1) { + var surfaceStackDepth = g_SurfaceStack.length; surface_set_target_system(g_pCurrentView.surface_id); - viewSurfaceTargetSet = true; + viewSurfaceTargetSet = (g_SurfaceStack.length > surfaceStackDepth); } //scaled port SHOULD BE port coords in final canvas space ( for canvas mouse -> view/world mouse coords ) @@ -4060,6 +4062,11 @@ yyRoom.prototype.DrawViews = function (r) { Graphics_Restore(); } } + // @if function("view_set_viewport") + // also free any view_set_viewport surface let go while it was a target outside the views above + // (e.g. the game set it as the target itself in a Step event), even if no view is visible + ViewPolygon_FlushSurfaceFrees(); + // @endif //} //else //{