| 1 |
/* |
| 2 |
* This file is part of the DXX-Rebirth project <https://www.dxx-rebirth.com/>. |
| 3 |
* It is copyright by its individual contributors, as recorded in the |
| 4 |
* project's Git history. See COPYING.txt at the top level for license |
| 5 |
* terms and a link to the Git history. |
| 6 |
*/ |
| 7 |
/* |
| 8 |
* Header for clipper.cpp |
| 9 |
*/ |
| 10 |
|
| 11 |
#pragma once |
| 12 |
#include "dxxsconf.h" |
| 13 |
#include <cstdint> |
| 14 |
|
| 15 |
#if !DXX_USE_OGL |
| 16 |
#include "dsx-ns.h" |
| 17 |
#include "3d.h" |
| 18 |
#include "globvars.h" |
| 19 |
#include <array> |
| 20 |
|
| 21 |
namespace dcx { |
| 22 |
|
| 23 |
struct polygon_clip_points : std::array<g3s_point *, MAX_POINTS_IN_POLY> {}; |
| 24 |
struct temporary_points_t |
| 25 |
{ |
| 26 |
using point_storage = std::array<g3s_point, MAX_POINTS_IN_POLY>; |
| 27 |
/* Initialize to 0 since no `free_points` members are in use. |
| 28 |
* `free_points_used` will increase and decrease as pointers are placed in |
| 29 |
* `free_points` and consumed from `free_points`. |
| 30 |
*/ |
| 31 |
unsigned free_points_used{}; |
| 32 |
/* Initialize to 0 since no `temp_points` members are in use. |
| 33 |
* `temporary_points_used` will increase when a new element of |
| 34 |
* `temp_points` is activated. `temporary_points_used` never decreases, |
| 35 |
* since deactivated elements in `temp_points` are pushed onto |
| 36 |
* `free_points`. The implementation could have been written to decrease |
| 37 |
* `temporary_points_used` for the special case that the freed point is the |
| 38 |
* last one in use, but this adds complexity for no benefit. |
| 39 |
*/ |
| 40 |
unsigned temporary_points_used{}; |
| 41 |
/* Elements in [0, `free_points_used`) are pointers to elements of |
| 42 |
* `temp_points` that were once used, but are now free and available for |
| 43 |
* reuse. |
| 44 |
* |
| 45 |
* Elements in [`free_points_used`, free_points.end()) are undefined. |
| 46 |
* |
| 47 |
* No two elements in `free_points` will point to the same element in |
| 48 |
* `temp_points`. Every valid pointer in `free_points` must point to an |
| 49 |
* element in `temp_points` from the same containing `temporary_points_t`. |
| 50 |
* |
| 51 |
* When a temporary point is needed, the last element of `free_points` will |
| 52 |
* be consumed to obtain a pointer to an available temporary point. If |
| 53 |
* `free_points` is empty, `temporary_points_used` will be read to pick the |
| 54 |
* next unused `temp_points` member, and that member will be used as an |
| 55 |
* available temporary point. |
| 56 |
* |
| 57 |
* When a temporary point is deactivated, its address will be written to |
| 58 |
* `free_points`. Only temporary points allocated from `temp_points` are |
| 59 |
* tracked, so `free_points` cannot overflow since the worst case is that |
| 60 |
* every element in `free_points` points to an element in `temp_points`. |
| 61 |
*/ |
| 62 |
std::array<point_storage::pointer, MAX_POINTS_IN_POLY> free_points |
| 63 |
#ifndef NDEBUG |
| 64 |
/* For debug builds, value-initialize the array with `nullptr` to make |
| 65 |
* bugs obvious. For release builds, leave it uninitialized for |
| 66 |
* performance. |
| 67 |
*/ |
| 68 |
{} |
| 69 |
#endif |
| 70 |
; |
| 71 |
point_storage temp_points; |
| 72 |
temporary_points_t(); |
| 73 |
void free_temp_point(g3s_point &cp); |
| 74 |
}; |
| 75 |
|
| 76 |
const polygon_clip_points &clip_polygon(polygon_clip_points &src, polygon_clip_points &dest, std::size_t *nv, g3s_codes *cc, temporary_points_t &); |
| 77 |
void clip_line(g3_draw_line_point *&p0, g3_draw_line_point *&p1, clipping_code codes_or, temporary_points_t &); |
| 78 |
|
| 79 |
} |
| 80 |
|
| 81 |
#endif |