From 2d3b838ba2103b158a04512a29ee88b9fec6b74f Mon Sep 17 00:00:00 2001 From: danij Date: Tue, 12 Mar 2013 00:00:57 +0000 Subject: [PATCH] Refactor|Sector: Moved all Sector_* functions to methods of Sector --- doomsday/client/include/map/sector.h | 195 ++++++++++++++---------- doomsday/client/src/audio/s_environ.cpp | 4 +- doomsday/client/src/edit_map.cpp | 18 +-- doomsday/client/src/map/dam_file.cpp | 2 +- doomsday/client/src/map/p_dmu.cpp | 4 +- doomsday/client/src/map/r_world.cpp | 2 +- doomsday/client/src/map/sector.cpp | 155 +++++++++---------- 7 files changed, 206 insertions(+), 174 deletions(-) diff --git a/doomsday/client/include/map/sector.h b/doomsday/client/include/map/sector.h index 7dac23832f..7ceb23154a 100644 --- a/doomsday/client/include/map/sector.h +++ b/doomsday/client/include/map/sector.h @@ -1,9 +1,7 @@ -/** - * @file sector.h - * Map Sector. @ingroup map +/** @file sector.h Map Sector. * - * @authors Copyright © 2003-2013 Jaakko Keränen - * @authors Copyright © 2006-2013 Daniel Swanson + * @authors Copyright © 2003-2013 Jaakko Keränen + * @authors Copyright © 2006-2013 Daniel Swanson * * @par License * GPL: http://www.gnu.org/licenses/gpl.html @@ -23,17 +21,22 @@ #ifndef LIBDENG_MAP_SECTOR #define LIBDENG_MAP_SECTOR -#ifndef __cplusplus -# error "map/sector.h requires C++" -#endif - -#include "MapElement" +#include +#include +#include #include "map/plane.h" #include "p_mapdata.h" #include "p_dmu.h" -#include +#include "MapElement" -// Helper macros for accessing sector floor/ceiling plane data elements. +class BspLeaf; +class LineDef; + +/* + * Helper macros for accessing the Planes of a Sector: + */ +/// @addtogroup map +///@{ #define SP_plane(n) planes[(n)] #define SP_planesurface(n) SP_plane(n)->surface() @@ -71,10 +74,7 @@ #define SP_floortarget SP_planetarget(Plane::Floor) #define SP_floorspeed SP_planespeed(Plane::Floor) #define SP_floorvisheight SP_planevisheight(Plane::Floor) - -#define S_skyfix(n) skyFix[(n)] -#define S_floorskyfix S_skyfix(Plane::Floor) -#define S_ceilskyfix S_skyfix(Plane::Ceiling) +///@} // Sector frame flags #define SIF_VISIBLE 0x1 // Sector is visible on this frame. @@ -82,40 +82,78 @@ #define SIF_LIGHT_CHANGED 0x2 typedef struct msector_s { - // Sector index. Always valid after loading & pruning. + /// Sector index. Always valid after loading & pruning. int index; int refCount; } msector_t; -class Plane; - +/** + * Map sector. + * + * @ingroup map + */ class Sector : public de::MapElement { public: + /// The referenced property does not exist. @ingroup errors + DENG2_ERROR(UnknownPropertyError); + + /// The referenced property is not writeable. @ingroup errors + DENG2_ERROR(WritePropertyError); + +public: /// @todo Make private: typedef QList Planes; - int frameFlags; - int validCount; // if == validCount, already checked. - AABoxd aaBox; // Bounding box for the sector. - coord_t roughArea; // Rough approximation of sector area. - float lightLevel; - float oldLightLevel; - float rgb[3]; - float oldRGB[3]; - struct mobj_s* mobjList; // List of mobjs in the sector. - unsigned int lineDefCount; - LineDef** lineDefs; // [lineDefCount+1] size. - unsigned int bspLeafCount; - BspLeaf** bspLeafs; // [bspLeafCount+1] size. - unsigned int numReverbBspLeafAttributors; - BspLeaf** reverbBspLeafs; // [numReverbBspLeafAttributors] size. - ddmobj_base_t base; - Planes planes; - unsigned int blockCount; // Number of gridblocks in the sector. - unsigned int changedBlockCount; // Number of blocks to mark changed. - unsigned short* blocks; // Light grid block indices. - float reverb[NUM_REVERB_DATA]; - msector_t buildData; + int frameFlags; + + /// if == validCount, already checked. + int validCount; + + /// Bounding box for the sector. + AABoxd aaBox; + + /// Rough approximation of sector area. + coord_t roughArea; + + float lightLevel; + + float oldLightLevel; + + float rgb[3]; + + float oldRGB[3]; + + /// List of mobjs in the sector. + struct mobj_s *mobjList; + + /// [lineDefCount+1] size. + LineDef **lineDefs; + uint lineDefCount; + + /// [bspLeafCount+1] size. + BspLeaf **bspLeafs; + uint bspLeafCount; + + /// [numReverbBspLeafAttributors] size. + BspLeaf **reverbBspLeafs; + uint numReverbBspLeafAttributors; + + ddmobj_base_t base; + + Planes planes; + + /// Number of gridblocks in the sector. + uint blockCount; + + /// Number of blocks to mark changed. + uint changedBlockCount; + + /// Light grid block indices. + ushort *blocks; + + float reverb[NUM_REVERB_DATA]; + + msector_t buildData; public: Sector(); @@ -141,51 +179,44 @@ class Sector : public de::MapElement /// @copydoc ceiling() inline Plane const &ceiling() const { return *planes[Plane::Ceiling]; } -}; -/** - * Update the Sector's map space axis-aligned bounding box to encompass the points - * defined by it's LineDefs' vertices. - * - * @pre LineDef list must have been initialized. - * - * @param sector Sector instance. - */ -void Sector_UpdateAABox(Sector* sector); + /** + * Update the sector's map space axis-aligned bounding box to encompass + * the points defined by it's LineDefs' vertexes. + * + * @pre LineDef list must have been initialized. + */ + void updateAABox(); -/** - * Update the Sector's rough area approximation. - * - * @pre Axis-aligned bounding box must have been initialized. - * - * @param sector Sector instance. - */ -void Sector_UpdateArea(Sector* sector); + /** + * Update the sector's rough area approximation. + * + * @pre Axis-aligned bounding box must have been initialized. + */ + void updateArea(); -/** - * Update the origin of the sector according to the point defined by the center of - * the sector's axis-aligned bounding box (which must be initialized before calling). - * - * @param sector Sector instance. - */ -void Sector_UpdateBaseOrigin(Sector *sector); + /** + * Update the origin of the sector according to the point defined by the + * center of the sector's axis-aligned bounding box (which must be + * initialized before calling). + */ + void updateBaseOrigin(); -/** - * Get a property value, selected by DMU_* name. - * - * @param sector Sector instance. - * @param args Property arguments. - * @return Always @c 0 (can be used as an iterator). - */ -int Sector_GetProperty(const Sector* sector, setargs_t* args); + /** + * Get a property value, selected by DMU_* name. + * + * @param args Property arguments. + * @return Always @c 0 (can be used as an iterator). + */ + int property(setargs_t &args) const; -/** - * Update a property value, selected by DMU_* name. - * - * @param sector Sector instance. - * @param args Property arguments. - * @return Always @c 0 (can be used as an iterator). - */ -int Sector_SetProperty(Sector* sector, const setargs_t* args); + /** + * Update a property value, selected by DMU_* name. + * + * @param args Property arguments. + * @return Always @c 0 (can be used as an iterator). + */ + int setProperty(setargs_t const &args); +}; #endif // LIBDENG_MAP_SECTOR diff --git a/doomsday/client/src/audio/s_environ.cpp b/doomsday/client/src/audio/s_environ.cpp index be57678558..f0bf4e4782 100644 --- a/doomsday/client/src/audio/s_environ.cpp +++ b/doomsday/client/src/audio/s_environ.cpp @@ -300,7 +300,7 @@ static boolean calcBspLeafReverb(BspLeaf* bspLeaf) return true; } -static void Sector_CalculateReverb(Sector* sec) +static void calculateSectorReverb(Sector *sec) { if(!sec || !sec->lineDefCount) return; @@ -392,7 +392,7 @@ void S_UpdateReverbForSector(Sector* sec) // If update has been requested for this sector, calculate it now. if(reverbUpdateRequested.find(sec) != reverbUpdateRequested.end()) { - Sector_CalculateReverb(sec); + calculateSectorReverb(sec); reverbUpdateRequested.erase(sec); } } diff --git a/doomsday/client/src/edit_map.cpp b/doomsday/client/src/edit_map.cpp index b6716937d8..5861f7af1a 100644 --- a/doomsday/client/src/edit_map.cpp +++ b/doomsday/client/src/edit_map.cpp @@ -615,26 +615,26 @@ static void buildSectorLineLists(GameMap* map) M_Free(sectorLineLinks); } -static void finishSectors(GameMap* map) +static void finishSectors(GameMap *map) { DENG_ASSERT(map); for(uint i = 0; i < map->sectorCount(); ++i) { - Sector* sec = &map->sectors[i]; + Sector &sec = map->sectors[i]; - Sector_UpdateAABox(sec); - Sector_UpdateArea(sec); - Sector_UpdateBaseOrigin(sec); + sec.updateAABox(); + sec.updateArea(); + sec.updateBaseOrigin(); // Set the position of the sound origin for all plane sound origins. // Set target heights for all planes. - for(uint k = 0; k < sec->planeCount(); ++k) + for(uint k = 0; k < sec.planeCount(); ++k) { - Plane *pln = sec->planes[k]; + Plane &pln = *sec.planes[k]; - pln->surface().updateBaseOrigin(); - pln->_targetHeight = pln->_height; + pln.surface().updateBaseOrigin(); + pln._targetHeight = pln._height; } } } diff --git a/doomsday/client/src/map/dam_file.cpp b/doomsday/client/src/map/dam_file.cpp index ff5e11a4bc..cb8c9159ea 100644 --- a/doomsday/client/src/map/dam_file.cpp +++ b/doomsday/client/src/map/dam_file.cpp @@ -537,7 +537,7 @@ static void readSector(GameMap *map, uint idx) s->aaBox.maxX = readFloat(); s->aaBox.maxY = readFloat(); - Sector_UpdateBaseOrigin(s); + s->updateBaseOrigin(); for(i = 0; i < numPlanes; ++i) { Plane *pln = s->planes[i]; diff --git a/doomsday/client/src/map/p_dmu.cpp b/doomsday/client/src/map/p_dmu.cpp index 9f49db4db9..f8ccee8a77 100644 --- a/doomsday/client/src/map/p_dmu.cpp +++ b/doomsday/client/src/map/p_dmu.cpp @@ -1038,7 +1038,7 @@ static int setProperty(void *ptr, void *context) break; case DMU_SECTOR: - Sector_SetProperty(elem->castTo(), args); + elem->castTo()->setProperty(*args); break; case DMU_MATERIAL: @@ -1520,7 +1520,7 @@ static int getProperty(void *ptr, void *context) break; case DMU_SECTOR: - Sector_GetProperty(elem->castTo(), args); + elem->castTo()->property(*args); break; case DMU_SIDEDEF: diff --git a/doomsday/client/src/map/r_world.cpp b/doomsday/client/src/map/r_world.cpp index b84266a462..1cfa71ee76 100644 --- a/doomsday/client/src/map/r_world.cpp +++ b/doomsday/client/src/map/r_world.cpp @@ -1491,7 +1491,7 @@ boolean R_UpdateSector(Sector *sec, boolean forceUpdate) if(forceUpdate || planeChanged) { - Sector_UpdateBaseOrigin(sec); + sec->updateBaseOrigin(); R_UpdateLinedefsOfSector(sec); S_MarkSectorReverbDirty(sec); changed = true; diff --git a/doomsday/client/src/map/sector.cpp b/doomsday/client/src/map/sector.cpp index cd82665266..f71ef328a3 100644 --- a/doomsday/client/src/map/sector.cpp +++ b/doomsday/client/src/map/sector.cpp @@ -20,32 +20,37 @@ */ #include "de_base.h" -#include "de_console.h" -#include "de_play.h" +#include "map/bspleaf.h" +#include "map/linedef.h" +#include -Sector::Sector() : de::MapElement(DMU_SECTOR) +#include "map/sector.h" + +using namespace de; + +Sector::Sector() : MapElement(DMU_SECTOR) { frameFlags = 0; validCount = 0; - memset(&aaBox, 0, sizeof(aaBox)); + std::memset(&aaBox, 0, sizeof(aaBox)); roughArea = 0; lightLevel = 0; oldLightLevel = 0; - memset(rgb, 0, sizeof(rgb)); - memset(oldRGB, 0, sizeof(oldRGB)); + std::memset(rgb, 0, sizeof(rgb)); + std::memset(oldRGB, 0, sizeof(oldRGB)); mobjList = 0; - lineDefCount = 0; lineDefs = 0; - bspLeafCount = 0; + lineDefCount = 0; bspLeafs = 0; - numReverbBspLeafAttributors = 0; + bspLeafCount = 0; reverbBspLeafs = 0; - memset(&base, 0, sizeof(base)); + numReverbBspLeafAttributors = 0; + std::memset(&base, 0, sizeof(base)); blockCount = 0; changedBlockCount = 0; blocks = 0; - memset(reverb, 0, sizeof(reverb)); - memset(&buildData, 0, sizeof(buildData)); + std::memset(reverb, 0, sizeof(reverb)); + std::memset(&buildData, 0, sizeof(buildData)); } Sector::~Sector() @@ -56,123 +61,119 @@ Sector::~Sector() } } -void Sector_UpdateAABox(Sector* sec) +void Sector::updateAABox() { - LineDef** lineIter; - LineDef* line; - assert(sec); - - V2d_Set(sec->aaBox.min, DDMAXFLOAT, DDMAXFLOAT); - V2d_Set(sec->aaBox.max, DDMINFLOAT, DDMINFLOAT); + V2d_Set(aaBox.min, DDMAXFLOAT, DDMAXFLOAT); + V2d_Set(aaBox.max, DDMINFLOAT, DDMINFLOAT); - lineIter = sec->lineDefs; + LineDef **lineIter = lineDefs; if(!lineIter) return; - line = *lineIter; - V2d_InitBox(sec->aaBox.arvec2, line->aaBox.min); - V2d_AddToBox(sec->aaBox.arvec2, line->aaBox.max); + LineDef *line = *lineIter; + V2d_InitBox(aaBox.arvec2, line->aaBox.min); + V2d_AddToBox(aaBox.arvec2, line->aaBox.max); lineIter++; for(; *lineIter; lineIter++) { line = *lineIter; - V2d_AddToBox(sec->aaBox.arvec2, line->aaBox.min); - V2d_AddToBox(sec->aaBox.arvec2, line->aaBox.max); + V2d_AddToBox(aaBox.arvec2, line->aaBox.min); + V2d_AddToBox(aaBox.arvec2, line->aaBox.max); } } -void Sector_UpdateArea(Sector* sec) +void Sector::updateArea() { - assert(sec); // Only a very rough estimate is required. - sec->roughArea = ((sec->aaBox.maxX - sec->aaBox.minX) / 128) * - ((sec->aaBox.maxY - sec->aaBox.minY) / 128); + roughArea = ((aaBox.maxX - aaBox.minX) / 128) * + ((aaBox.maxY - aaBox.minY) / 128); } -void Sector_UpdateBaseOrigin(Sector* sec) +void Sector::updateBaseOrigin() { - assert(sec); - sec->base.origin[VX] = (sec->aaBox.minX + sec->aaBox.maxX) / 2; - sec->base.origin[VY] = (sec->aaBox.minY + sec->aaBox.maxY) / 2; - sec->base.origin[VZ] = (sec->SP_floorheight + sec->SP_ceilheight) / 2; + base.origin[VX] = (aaBox.minX + aaBox.maxX) / 2; + base.origin[VY] = (aaBox.minY + aaBox.maxY) / 2; + base.origin[VZ] = (SP_floorheight + SP_ceilheight) / 2; } -int Sector_SetProperty(Sector* sec, const setargs_t* args) +int Sector::property(setargs_t &args) const { - switch(args->prop) + switch(args.prop) { + case DMU_LIGHT_LEVEL: + DMU_GetValue(DMT_SECTOR_LIGHTLEVEL, &lightLevel, &args, 0); + break; case DMU_COLOR: - DMU_SetValue(DMT_SECTOR_RGB, &sec->rgb[0], args, 0); - DMU_SetValue(DMT_SECTOR_RGB, &sec->rgb[1], args, 1); - DMU_SetValue(DMT_SECTOR_RGB, &sec->rgb[2], args, 2); + DMU_GetValue(DMT_SECTOR_RGB, &rgb[0], &args, 0); + DMU_GetValue(DMT_SECTOR_RGB, &rgb[1], &args, 1); + DMU_GetValue(DMT_SECTOR_RGB, &rgb[2], &args, 2); break; case DMU_COLOR_RED: - DMU_SetValue(DMT_SECTOR_RGB, &sec->rgb[0], args, 0); + DMU_GetValue(DMT_SECTOR_RGB, &rgb[0], &args, 0); break; case DMU_COLOR_GREEN: - DMU_SetValue(DMT_SECTOR_RGB, &sec->rgb[1], args, 0); + DMU_GetValue(DMT_SECTOR_RGB, &rgb[1], &args, 0); break; case DMU_COLOR_BLUE: - DMU_SetValue(DMT_SECTOR_RGB, &sec->rgb[2], args, 0); + DMU_GetValue(DMT_SECTOR_RGB, &rgb[2], &args, 0); break; - case DMU_LIGHT_LEVEL: - DMU_SetValue(DMT_SECTOR_LIGHTLEVEL, &sec->lightLevel, args, 0); + case DMU_BASE: { + ddmobj_base_t const *baseAdr = &base; + DMU_GetValue(DMT_SECTOR_BASE, &baseAdr, &args, 0); + break; } + case DMU_LINEDEF_COUNT: { + int val = int( lineDefCount ); + DMU_GetValue(DDVT_INT, &val, &args, 0); + break; } + case DMT_MOBJS: + DMU_GetValue(DMT_SECTOR_MOBJLIST, &mobjList, &args, 0); break; case DMU_VALID_COUNT: - DMU_SetValue(DMT_SECTOR_VALIDCOUNT, &sec->validCount, args, 0); + DMU_GetValue(DMT_SECTOR_VALIDCOUNT, &validCount, &args, 0); break; + case DMU_FLOOR_PLANE: { + Plane *pln = planes[Plane::Floor]; + DMU_GetValue(DMT_SECTOR_FLOORPLANE, &pln, &args, 0); + break; } + case DMU_CEILING_PLANE: { + Plane* pln = planes[Plane::Ceiling]; + DMU_GetValue(DMT_SECTOR_CEILINGPLANE, &pln, &args, 0); + break; } default: - Con_Error("Sector_SetProperty: Property %s is not writable.\n", DMU_Str(args->prop)); + /// @throw UnknownPropertyError The requested property does not exist. + throw UnknownPropertyError("Sector::property", QString("Property '%1' is unknown").arg(DMU_Str(args.prop))); } return false; // Continue iteration. } -int Sector_GetProperty(const Sector* sec, setargs_t* args) +int Sector::setProperty(setargs_t const &args) { - switch(args->prop) + switch(args.prop) { - case DMU_LIGHT_LEVEL: - DMU_GetValue(DMT_SECTOR_LIGHTLEVEL, &sec->lightLevel, args, 0); - break; case DMU_COLOR: - DMU_GetValue(DMT_SECTOR_RGB, &sec->rgb[0], args, 0); - DMU_GetValue(DMT_SECTOR_RGB, &sec->rgb[1], args, 1); - DMU_GetValue(DMT_SECTOR_RGB, &sec->rgb[2], args, 2); + DMU_SetValue(DMT_SECTOR_RGB, &rgb[0], &args, 0); + DMU_SetValue(DMT_SECTOR_RGB, &rgb[1], &args, 1); + DMU_SetValue(DMT_SECTOR_RGB, &rgb[2], &args, 2); break; case DMU_COLOR_RED: - DMU_GetValue(DMT_SECTOR_RGB, &sec->rgb[0], args, 0); + DMU_SetValue(DMT_SECTOR_RGB, &rgb[0], &args, 0); break; case DMU_COLOR_GREEN: - DMU_GetValue(DMT_SECTOR_RGB, &sec->rgb[1], args, 0); + DMU_SetValue(DMT_SECTOR_RGB, &rgb[1], &args, 0); break; case DMU_COLOR_BLUE: - DMU_GetValue(DMT_SECTOR_RGB, &sec->rgb[2], args, 0); + DMU_SetValue(DMT_SECTOR_RGB, &rgb[2], &args, 0); break; - case DMU_BASE: { - const ddmobj_base_t* base = &sec->base; - DMU_GetValue(DMT_SECTOR_BASE, &base, args, 0); - break; } - case DMU_LINEDEF_COUNT: { - int val = (int) sec->lineDefCount; - DMU_GetValue(DDVT_INT, &val, args, 0); - break; } - case DMT_MOBJS: - DMU_GetValue(DMT_SECTOR_MOBJLIST, &sec->mobjList, args, 0); + case DMU_LIGHT_LEVEL: + DMU_SetValue(DMT_SECTOR_LIGHTLEVEL, &lightLevel, &args, 0); break; case DMU_VALID_COUNT: - DMU_GetValue(DMT_SECTOR_VALIDCOUNT, &sec->validCount, args, 0); + DMU_SetValue(DMT_SECTOR_VALIDCOUNT, &validCount, &args, 0); break; - case DMU_FLOOR_PLANE: { - Plane* pln = sec->planes[Plane::Floor]; - DMU_GetValue(DMT_SECTOR_FLOORPLANE, &pln, args, 0); - break; } - case DMU_CEILING_PLANE: { - Plane* pln = sec->planes[Plane::Ceiling]; - DMU_GetValue(DMT_SECTOR_CEILINGPLANE, &pln, args, 0); - break; } default: - Con_Error("Sector_GetProperty: No property %s.\n", DMU_Str(args->prop)); + /// @throw WritePropertyError The requested property is not writable. + throw WritePropertyError("Sector::setProperty", QString("Property '%1' is not writable").arg(DMU_Str(args.prop))); } return false; // Continue iteration.