diff --git a/scripts/functions/Function_Graphics.js b/scripts/functions/Function_Graphics.js index 629d0c09..1f8e8965 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,27 @@ 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; + } + // 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 @@ -3098,13 +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++) points.push(yyGetReal(polygons[n])); - 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]); @@ -3137,10 +3155,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..ac76d8ef 100644 --- a/scripts/yyRoom.js +++ b/scripts/yyRoom.js @@ -3978,9 +3978,15 @@ 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) + // (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 = (g_SurfaceStack.length > surfaceStackDepth); } //scaled port SHOULD BE port coords in final canvas space ( for canvas mouse -> view/world mouse coords ) @@ -4032,13 +4038,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++; @@ -4054,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 //{ 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;