Skip to content

Commit

Permalink
Refactor: Added C++ HEdge class
Browse files Browse the repository at this point in the history
Also, removed runtime_mapdata_header_t from Surface, as it seems that
it is not a proper MapElement type but rather some kind of a
sub-element. Anyway, the header wasn't apparently being used for
anything (?).

Todo: Scour the code for illegal/forced/unnecessary casts and switch
to de::MapElement* instead of void*.
  • Loading branch information
skyjake committed Jan 14, 2013
1 parent 4c69ba3 commit 9526852
Show file tree
Hide file tree
Showing 13 changed files with 75 additions and 60 deletions.
1 change: 0 additions & 1 deletion doomsday/engine/api/dd_types.h
Expand Up @@ -63,7 +63,6 @@ typedef char filename_t[FILENAME_T_MAXLEN];
typedef void (*con_textfilter_t) (char* text);

// Forward declarations for map data types.
struct hedge_s;
struct polyblock_s;
struct polyobj_s;
struct surface_s;
Expand Down
5 changes: 3 additions & 2 deletions doomsday/engine/include/map/bspleaf.h
Expand Up @@ -42,19 +42,20 @@
///@}

class Sector;
class HEdge;

class BspLeaf : public de::MapElement
{
public:
struct hedge_s* hedge; /// First HEdge in this leaf.
HEdge *hedge; /// First HEdge in this leaf.
int flags; /// @ref bspLeafFlags.
uint index; /// Unique. Set when saving the BSP.
int addSpriteCount; /// Frame number of last R_AddSprites.
int validCount;
uint hedgeCount; /// Number of HEdge's in this leaf.
Sector *sector;
struct polyobj_s* polyObj; /// First polyobj in this leaf. Can be @c NULL.
struct hedge_s* fanBase; /// HEdge whose vertex to use as the base for a trifan. If @c NULL then midPoint is used instead.
HEdge *fanBase; /// HEdge whose vertex to use as the base for a trifan. If @c NULL then midPoint is used instead.
struct shadowlink_s* shadows;
AABoxd aaBox; /// HEdge Vertex bounding box in the map coordinate space.
coord_t midPoint[2]; /// Center of vertices.
Expand Down
30 changes: 14 additions & 16 deletions doomsday/engine/include/map/hedge.h
Expand Up @@ -57,19 +57,23 @@

class Vertex;

typedef struct hedge_s {
runtime_mapdata_header_t header;
/**
* Half-edge.
*/
class HEdge : public de::MapElement
{
public:
Vertex *v[2]; /// [Start, End] of the segment.
struct hedge_s* next;
struct hedge_s* prev;
HEdge *next;
HEdge *prev;

// Half-edge on the other side, or NULL if one-sided. This relationship
// is always one-to-one -- if one of the half-edges is split, the twin
// must also be split.
struct hedge_s* twin;
BspLeaf* bspLeaf;
HEdge *twin;
BspLeaf *bspLeaf;

LineDef* lineDef;
LineDef *lineDef;
Sector *sector;
angle_t angle;
byte side; /// On which side of the LineDef (0=front, 1=back)?
Expand All @@ -78,18 +82,12 @@ typedef struct hedge_s {
biassurface_t* bsuf[3]; /// For each @ref SideDefSection.
short frameFlags;
uint index; /// Unique. Set when saving the BSP.
} HEdge;

/*
class HEdge : public de::MapObject, public hedge_s
{
public:
HEdge() : de::MapObject(DMU_HEDGE)
{
memset(static_cast<hedge_s *>(this), 0, sizeof(hedge_s));
}
HEdge();
HEdge(HEdge const &other);
~HEdge();
};
*/

struct bsphedgeinfo_s;

Expand Down
5 changes: 3 additions & 2 deletions doomsday/engine/include/map/linedef.h
Expand Up @@ -80,12 +80,13 @@

class Sector;
class SideDef;
class HEdge;

typedef struct lineside_s {
Sector* sector; /// Sector on this side.
SideDef* sideDef; /// SideDef on this side.
struct hedge_s* hedgeLeft; /// Left-most HEdge on this side.
struct hedge_s* hedgeRight; /// Right-most HEdge on this side.
HEdge* hedgeLeft; /// Left-most HEdge on this side.
HEdge* hedgeRight; /// Right-most HEdge on this side.
unsigned short shadowVisFrame; /// Framecount of last time shadows were drawn on this side.
} lineside_t;

Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/include/map/surface.h
Expand Up @@ -41,7 +41,7 @@ typedef struct surfacedecor_s {
} surfacedecor_t;

typedef struct surface_s {
runtime_mapdata_header_t header;
//runtime_mapdata_header_t header;
ddmobj_base_t base;
void* owner; // Either @c DMU_SIDEDEF, or @c DMU_PLANE
int flags; // SUF_ flags
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/include/render/rend_bias.h
Expand Up @@ -113,7 +113,7 @@ void SB_RendPoly(struct ColorRawf_s* rcolors,
const struct rvertex_s* rvertices,
size_t numVertices, const vectorcompf_t* normal,
float sectorLightLevel,
void* mapObject, uint elmIdx, boolean isHEdge);
de::MapElement const *mapElement, uint elmIdx);
void SB_EndFrame(void);

int SB_NewSourceAt(coord_t x, coord_t y, coord_t z, float size, float minLight,
Expand Down
6 changes: 1 addition & 5 deletions doomsday/engine/src/edit_bsp.cpp
Expand Up @@ -88,11 +88,7 @@ static int hedgeCollector(BspTreeNode& tree, void* parameters)
do
{
// Take ownership of this HEdge.
runtime_mapdata_header_t* hdr = reinterpret_cast<runtime_mapdata_header_t*>(hedge);
DENG2_ASSERT(hdr);

// TODO: uncomment when HEdge is derived from MapElement
//p->builder->take(hdr);
p->builder->take(hedge);

// Add this HEdge to the LUT.
hedge->index = p->curIdx++;
Expand Down
5 changes: 3 additions & 2 deletions doomsday/engine/src/edit_map.cpp
Expand Up @@ -1165,7 +1165,8 @@ static void hardenPolyobjs(GameMap* dest, EditMap* src)
destP->prevPts = (povertex_t*) Z_Malloc(destP->lineCount * sizeof(povertex_t), PU_MAP, 0);

// Create a hedge for each line of this polyobj.
hedges = (HEdge*) Z_Calloc(sizeof(HEdge) * destP->lineCount, PU_MAP, 0);
// TODO: Polyobj has ownership, must free it.
hedges = new HEdge[destP->lineCount];

destP->lines = (LineDef**) Z_Malloc(sizeof(*destP->lines) * (destP->lineCount+1), PU_MAP, 0);
for(j = 0; j < destP->lineCount; ++j)
Expand All @@ -1176,7 +1177,7 @@ static void hardenPolyobjs(GameMap* dest, EditMap* src)
// This line belongs to a polyobj.
line->inFlags |= LF_POLYOBJ;

hedge->header.type = DMU_HEDGE;
//hedge->header.type = DMU_HEDGE;
hedge->lineDef = line;
hedge->length = V2d_Distance(line->L_v2origin, line->L_v1origin);
hedge->twin = NULL;
Expand Down
53 changes: 37 additions & 16 deletions doomsday/engine/src/map/hedge.cpp
Expand Up @@ -26,6 +26,40 @@

#include <de/Log>

HEdge::HEdge() : de::MapElement(DMU_HEDGE)
{
memset(v, 0, sizeof(v));
next = 0;
prev = 0;
twin = 0;
bspLeaf = 0;
lineDef = 0;
sector = 0;
angle = 0;
side = 0;
length = 0;
offset = 0;
memset(bsuf, 0, sizeof(bsuf));
frameFlags = 0;
index = 0;
}

HEdge::HEdge(HEdge const &other) : de::MapElement(DMU_HEDGE)
{
*this = other;
}

HEdge::~HEdge()
{
for(uint i = 0; i < 3; ++i)
{
if(bsuf[i])
{
SB_DestroySurface(bsuf[i]);
}
}
}

coord_t WallDivNode_Height(walldivnode_t* node)
{
Q_ASSERT(node);
Expand Down Expand Up @@ -311,30 +345,17 @@ boolean HEdge_PrepareWallDivs(HEdge* hedge, SideDefSection section,

HEdge* HEdge_New(void)
{
HEdge* hedge = static_cast<HEdge*>(Z_Calloc(sizeof *hedge, PU_MAPSTATIC, 0));
hedge->header.type = DMU_HEDGE;
return hedge;
return new HEdge;
}

HEdge* HEdge_NewCopy(const HEdge* other)
{
assert(other);
HEdge* hedge = static_cast<HEdge*>(Z_Malloc(sizeof *hedge, PU_MAPSTATIC, 0));
memcpy(hedge, other, sizeof *hedge);
return hedge;
return new HEdge(*other);
}

void HEdge_Delete(HEdge* hedge)
{
assert(hedge);
for(uint i = 0; i < 3; ++i)
{
if(hedge->bsuf[i])
{
SB_DestroySurface(hedge->bsuf[i]);
}
}
Z_Free(hedge);
delete hedge;
}

coord_t HEdge_PointDistance(HEdge* hedge, coord_t const point[2], coord_t* offset)
Expand Down
5 changes: 2 additions & 3 deletions doomsday/engine/src/map/p_dmu.cpp
Expand Up @@ -382,8 +382,7 @@ uint P_ToIndex(void const *ptr)
return GET_VERTEX_IDX(elem->castTo<Vertex>());

case DMU_HEDGE:
DENG2_ASSERT(false); // TODO: update this!
//return GET_HEDGE_IDX((HEdge *) ptr);
return GET_HEDGE_IDX(elem->castTo<HEdge>());

case DMU_LINEDEF:
return GET_LINE_IDX(elem->castTo<LineDef>());
Expand All @@ -405,7 +404,7 @@ uint P_ToIndex(void const *ptr)

case DMU_MATERIAL:
DENG2_ASSERT(false); // TODO: update this!
//return Materials_Id((material_t *) ptr);
//return Materials_Id(elem->castTo<Material>());

default:
DENG2_ASSERT(false); // Unknown/non-indexable DMU type.
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/src/map/r_world.cpp
Expand Up @@ -481,7 +481,7 @@ Plane *R_NewPlaneForSector(Sector *sec)
// Initialize the surface.
std::memset(&plane->surface, 0, sizeof(plane->surface)); // TODO: surface_s: update for C++ Surface
Surface *suf = &plane->surface;
suf->header.type = DMU_SURFACE; // Setup header for DMU.
//suf->header.type = DMU_SURFACE; // Setup header for DMU.
suf->normal[VZ] = 1;
V3f_BuildTangents(suf->tangent, suf->bitangent, suf->normal);

Expand Down
11 changes: 5 additions & 6 deletions doomsday/engine/src/render/rend_bias.cpp
Expand Up @@ -917,15 +917,14 @@ static boolean SB_CheckColorOverride(biasaffection_t *affected)
* @param numVertices Number of vertices (in the array) to be lit.
* @param normal Surface normal.
* @param sectorLightLevel Sector light level.
* @param mapObject Ptr to either a HEdge or BspLeaf.
* @param mapElement Ptr to either a HEdge or BspLeaf.
* @param elmIdx Used with BspLeafs to select a specific plane.
* @param isHEdge @c true, if @a mapObject is a HEdge ELSE a BspLeaf.
*/
void SB_RendPoly(struct ColorRawf_s* rcolors, biassurface_t* bsuf,
const struct rvertex_s* rvertices,
size_t numVertices, const vectorcompf_t* normal,
float sectorLightLevel,
void* mapObject, uint elmIdx, boolean isHEdge)
de::MapElement const *mapElement, uint elmIdx)
{
uint i;
boolean forced;
Expand Down Expand Up @@ -956,15 +955,15 @@ void SB_RendPoly(struct ColorRawf_s* rcolors, biassurface_t* bsuf,
* @todo This could be enhanced so that only the lights on the
* right side of the surface are taken into consideration.
*/
if(isHEdge)
if(mapElement->type() == DMU_HEDGE)
{
HEdge* hedge = (HEdge*) mapObject;
HEdge const *hedge = mapElement->castTo<HEdge>();

updateAffected(bsuf, hedge->HE_v1origin, hedge->HE_v2origin, normal);
}
else
{
BspLeaf* bspLeaf = (BspLeaf*) mapObject;
BspLeaf const *bspLeaf = mapElement->castTo<BspLeaf>();
vec3d_t point;

V3d_Set(point, bspLeaf->midPoint[VX], bspLeaf->midPoint[VY],
Expand Down
8 changes: 4 additions & 4 deletions doomsday/engine/src/render/rend_main.cpp
Expand Up @@ -753,7 +753,7 @@ typedef struct {
boolean forceOpaque;

// For bias:
void *mapObject;
de::MapElement *mapElement;
uint elmIdx;
biassurface_t *bsuf;

Expand Down Expand Up @@ -930,7 +930,7 @@ static boolean renderWorldPoly(rvertex_t *rvertices, uint numVertices,
{ // Non-uniform color.
if(useBias && p->bsuf)
{ // Do BIAS lighting for this poly.
SB_RendPoly(rcolors, p->bsuf, rvertices, numVertices, p->normal, p->sectorLightLevel, p->mapObject, p->elmIdx, p->isWall);
SB_RendPoly(rcolors, p->bsuf, rvertices, numVertices, p->normal, p->sectorLightLevel, p->mapElement, p->elmIdx);
if(glowing > 0)
{
uint i;
Expand Down Expand Up @@ -1282,7 +1282,7 @@ static boolean doRenderHEdge(HEdge* hedge, const pvec3f_t normal,
params.wall.segLength = &hedge->length;
params.forceOpaque = (alpha < 0? true : false);
params.alpha = (alpha < 0? 1 : alpha);
params.mapObject = hedge;
params.mapElement = hedge;
params.elmIdx = elmIdx;
params.bsuf = bsuf;
params.normal = normal;
Expand Down Expand Up @@ -1436,7 +1436,7 @@ static void renderPlane(BspLeaf* bspLeaf, planetype_t type, coord_t height,

params.flags = RPF_DEFAULT;
params.isWall = false;
params.mapObject = bspLeaf;
params.mapElement = bspLeaf;
params.elmIdx = elmIdx;
params.bsuf = bsuf;
params.normal = normal;
Expand Down

0 comments on commit 9526852

Please sign in to comment.