diff --git a/scripts/_GameMaker.js b/scripts/_GameMaker.js index ff520a7d..acdbff7f 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(v); + } + // @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/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 723bfac1..629d0c09 100644 --- a/scripts/functions/Function_Graphics.js +++ b/scripts/functions/Function_Graphics.js @@ -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: +/// 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. +/// +// ############################################################################################# +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; + } + + // 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 + 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; 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/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/yyBuiltIn.js b/scripts/yyBuiltIn.js index 36daece0..73bc3734 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, true); + // @endif +}; yyBuiltIn.prototype.get_view_visible = function (_val) { return g_pBuiltIn.view_visible[0]; }; diff --git a/scripts/yyIOManager.js b/scripts/yyIOManager.js index 6a9b3de2..44d05ce0 100644 --- a/scripts/yyIOManager.js +++ b/scripts/yyIOManager.js @@ -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) && + ((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); diff --git a/scripts/yyRoom.js b/scripts/yyRoom.js index 8cb19dad..81fa3338 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]; @@ -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); @@ -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++; diff --git a/scripts/yyView.js b/scripts/yyView.js index 2187c3b6..2159ce14 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,373 @@ 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. Returns +/// [] 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) +{ + 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) { + // 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); + // 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 = []; + 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 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) { + 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]; + 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 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; + } + + // 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; + } + else { + i++; + misses++; + } + if (i >= m) i = 0; + } + + 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]); + } + + // 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; +} + +// ############################################################################################# +/// Function: +/// 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.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: +/// 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. 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 () { + if (this.m_polygonSurface == -1) return false; + var pSurf = g_Surfaces.Get(this.m_polygonSurface); + return (pSurf != null) && (pSurf.m_viewPolygonOwner === this); +}; + +// ############################################################################################# +/// Function: +/// Forgets the surface view_set_viewport made (without freeing it) +/// +// ############################################################################################# +yyView.prototype.ForgetPolygonSurface = function () { + this.m_polygonSurface = -1; +}; + +// ############################################################################################# +/// 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. _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, _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); + 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 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, 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 (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.ForgetPolygonSurface(); + } + 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; + var pSurf = g_Surfaces.Get(surf); + if (pSurf) pSurf.m_viewPolygonOwner = this; + 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, false); + return; + } + this.EnsurePolygonSurface(_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 (and still ours) +/// +// ############################################################################################# +yyView.prototype.ResetPolygon = function (_index) { + this.CheckPolygonSurface(_index, false); + if (this.m_polygonSurface != -1) { + if (this.OwnsPolygonSurface()) surface_free(this.m_polygonSurface); + this.SetPolygonViewSurface(_index, -1); + } + this.m_polygon = null; + this.ForgetPolygonSurface(); + this.m_polygonStartFunc = null; + 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; + + // 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.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; + 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 () { + 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) + Graphics_Save(); + 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") return; + var self = _func.boundObject ?? global; + _func(self, self); + }; + + try { + callFunc(startFunc); + + 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) { + 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(endFunc); + } + finally { + Graphics_Restore(); + } +}; +// @endif view_set_viewport 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; }