1 (* Copyright (C) DooM 2D:Forever Developers
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *)
16 {$INCLUDE ../shared/a_modes.inc}
17 // universal sweep-and-prune broad phase
20 interface
22 type
25 type
30 private
35 //nextLink: TSAPProxy; // next free or nothing
38 private
41 public
48 //property grid: TBodyGrid read mGrid;
53 private
54 type
55 //PInterval = ^TInterval;
57 public
61 public
66 private
76 private
83 //procedure remove (body: TSAPProxy);
85 public
89 function insertBody (aObj: TObject; ax, ay, aWidth, aHeight: Integer; aTag: Integer=0): TSAPProxy;
90 //procedure removeBody (aObj: TSAPProxy); // WARNING! this WILL destroy proxy!
92 //procedure moveBody (body: TSAPProxy; dx, dy: Integer);
93 //procedure resizeBody (body: TSAPProxy; sx, sy: Integer);
94 //procedure moveResizeBody (body: TSAPProxy; dx, dy, sx, sy: Integer);
98 //function getProxyForBody (aObj: TObject; x, y, w, h: Integer): TSAPProxy;
100 // call these functions before massive update (it may, or may be not faster)
108 implementation
110 uses
114 // ////////////////////////////////////////////////////////////////////////// //
116 begin
124 //nextLink := -1;
130 // ////////////////////////////////////////////////////////////////////////// //
132 var
134 begin
143 // v0 MUST be <= v1!
145 begin
152 // ////////////////////////////////////////////////////////////////////////// //
154 var
156 begin
159 // init intervals
161 begin
166 // init proxies
169 begin
181 //e_WriteLog(Format('created grid with size: %dx%d (tile size: %d); pix: %dx%d', [mWidth, mHeight, mTileSize, mWidth*mTileSize, mHeight*mTileSize]), MSG_NOTIFY);
186 var
188 begin
196 begin
197 e_WriteLog(Format('used intervals: %d; max proxies allocated: %d; proxies used: %d', [mIntrsUsed[0], mProxyMaxCount, mProxyCount]), MSG_NOTIFY);
202 begin
208 begin
210 begin
217 function TSweepAndPrune.allocProxy (aX, aY, aWidth, aHeight: Integer; aObj: TObject; aTag: Integer): TSAPProxy;
218 var
221 begin
223 begin
224 // no free proxies, resize list
231 // get one from list
236 // add to used list
238 // statistics
244 begin
246 if (mProxyCount = 0) then raise Exception.Create('wutafuuuuu in grid (no allocated proxies, what i should free now?)');
247 // add to free list
258 var
261 begin
265 begin
267 begin
271 begin
283 begin
290 var
294 var
296 begin
305 begin
313 function TSweepAndPrune.insertBody (aObj: TObject; aX, aY, aWidth, aHeight: Integer; aTag: Integer=0): TSAPProxy;
314 begin
324 var
327 begin
331 begin
332 // one element
334 end
335 else
336 begin
337 // do search
341 begin
347 //return (cmpfn(lines+i) == 0 ? i : -1);
348 if (i > 0) and not mIntrs[iidx][i].intersects(val0, val1) and mIntrs[iidx][i-1].intersects(val0, val1) then Dec(i);
349 if (i+1 < mIntrsUsed[iidx]) and not mIntrs[iidx][i].intersects(val0, val1) and mIntrs[iidx][i+1].intersects(val0, val1) then Inc(i);
352 begin
353 // first pass
355 begin
359 end
360 else
361 begin
362 // second pass
364 begin
367 begin
377 var
379 begin
384 // increase query counter
387 begin
388 // just in case of overflow
393 (*
394 * the algorithm is simple:
395 * find start for first interval (binary search will do)
396 * walk the interval, marking proxies with mLastQuery
397 * increment mLastQuery
398 * find start for second interval (binary search will do)
399 * walk the interval, returning proxies marked with mLastQuery
400 *)