Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 32 additions & 12 deletions scripts/functions/Function_Graphics.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand All @@ -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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: The comment says "a call that is rejected changes nothing", but that only holds within one entry. Entries are checked and applied one at a time in the outer loop, so view_set_viewport([{ screenX: 10, polygons: [...] }, { polygons: [0, 0, 1] }]) applies entry 0 in full (port, polygon, surface, camera) before entry 1 raises the odd-length error. A game that catches the error is left with some entries applied and others not. Either check every entry in a first pass (fields, polygon, earclip, callables) and apply them in a second, or change the comment to say "an entry that is rejected changes nothing for that entry".

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
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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);
Expand Down
17 changes: 15 additions & 2 deletions scripts/yyRoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down Expand Up @@ -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();
Comment thread
rwkay marked this conversation as resolved.
Resolved
// @endif
Current_View++;

Expand All @@ -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
//{
Expand Down
45 changes: 42 additions & 3 deletions scripts/yyView.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:<summary>
/// 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)
/// </summary>
// #############################################################################################
function ViewPolygon_FreeSurface(_id)
{
if (CheckForSurface(_id)) {
g_ViewPolygonSurfacesToFree.push({ id: _id, surface: g_Surfaces.Get(_id) });
return;
}
surface_free(_id);
}

// #############################################################################################
/// Function:<summary>
/// 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.
/// </summary>
// #############################################################################################
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:<summary>
/// Is the surface view_set_viewport made still there, and still ours? Surface IDs are reused
Expand Down Expand Up @@ -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) {
Expand All @@ -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();
}
Expand Down Expand Up @@ -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;
Expand Down