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
6 changes: 6 additions & 0 deletions scripts/_GameMaker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(v);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The surface view_set_viewport made is only freed here, when leaving a non-persistent room. game_restart from a persistent room skips this block.

What happens:

  • game_restart sets ROOM_RESTARTGAME, which runs Run_EndGame → ResetAll → StartGame → StartRoom(startRoom, true).
  • Because g_RunRoom.m_persistent is true, ResetPolygon never runs.
  • _starting then forces ispersistent = false, so CreateRoomFromStorage calls Init(), which does m_Views = [].
  • The old yyView, and the only record of m_polygonSurface, are dropped. The surface stays allocated in g_Surfaces (a WebGL framebuffer and texture) and nothing can free it anymore.

Result: each restart leaks one surface per polygon view.

Fix: free the polygons wherever a room's views get replaced, not only on this path. For example, at the top of yyRoom.prototype.CreateRoomFromStorage (before this.Init()), run ResetPolygon(v) on each existing this.m_Views[v], inside the same @if function("view_set_viewport") block. Another option is to also run this loop when _starting is true.

}
// @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
Expand Down
4 changes: 4 additions & 0 deletions scripts/device/iOS.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) &&
Expand Down
158 changes: 157 additions & 1 deletion scripts/functions/Function_Graphics.js
Original file line number Diff line number Diff line change
Expand Up @@ -2993,9 +2993,165 @@ 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, true);
// @endif
};

// @if function("view_set_viewport")
// #############################################################################################
/// Function:<summary>
/// 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.
/// 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.
/// </summary>
// #############################################################################################
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;
Comment thread
rwkay marked this conversation as resolved.
Resolved

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;
}

// 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 = 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;
}

// 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;

// 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);
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]);
pView.m_polygon = tris;
g_pBuiltIn.view_visible[index] = true;
pView.visible = true;
}
else {
pView.ResetPolygon(index);
}
}

// 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);
}

// startFunc and endFunc are only replaced when they are given
Comment thread
rwkay marked this conversation as resolved.
Resolved
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) {
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);
}
}
}
};
// @endif view_set_viewport

// Stubs
function display_get_windows_vertex_buffer_method() {
return 0;
Expand Down
5 changes: 5 additions & 0 deletions scripts/functions/Function_Surface.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
12 changes: 12 additions & 0 deletions scripts/functions/Function_Window.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) &&
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion scripts/yyBuiltIn.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, true);
// @endif
};


yyBuiltIn.prototype.get_view_visible = function (_val) { return g_pBuiltIn.view_visible[0]; };
Expand Down
9 changes: 7 additions & 2 deletions scripts/yyIOManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -2130,8 +2130,13 @@ 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))
var inView = ((this.MouseX - g_CanvasRect.left) >= pView.scaledportx) && ((this.MouseX - g_CanvasRect.left) < pView.scaledportx2) &&
Comment thread
rwkay marked this conversation as resolved.
Resolved
((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)
{
g_pBuiltIn.mouse_x = pView.GetMouseX(g_pIOManager.MouseX,g_pIOManager.MouseY);
g_pBuiltIn.mouse_y = pView.GetMouseY(g_pIOManager.MouseX,g_pIOManager.MouseY);
Expand Down
15 changes: 15 additions & 0 deletions scripts/yyRoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand Down Expand Up @@ -3968,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);
Expand Down Expand Up @@ -4025,6 +4035,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++;


Expand Down
Loading