From 3bcfa42e13cea4086b0665c93a7c146b66209785 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaakko=20Kera=CC=88nen?= Date: Thu, 17 Nov 2016 20:46:31 +0200 Subject: [PATCH] =?UTF-8?q?libdoomsday|Map=20Data:=20Added=20=E2=80=9Cdoub?= =?UTF-8?q?le=E2=80=9D=20property=20value=20type?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../include/doomsday/world/entitydatabase.h | 6 +- .../include/doomsday/world/entitydef.h | 15 +- .../include/doomsday/world/propertyvalue.h | 113 +++++++++------ .../libdoomsday/src/world/entitydatabase.cpp | 6 +- .../apps/libdoomsday/src/world/entitydef.cpp | 136 +++++------------- 5 files changed, 122 insertions(+), 154 deletions(-) diff --git a/doomsday/apps/libdoomsday/include/doomsday/world/entitydatabase.h b/doomsday/apps/libdoomsday/include/doomsday/world/entitydatabase.h index 0c8cdf0455..62656ad573 100644 --- a/doomsday/apps/libdoomsday/include/doomsday/world/entitydatabase.h +++ b/doomsday/apps/libdoomsday/include/doomsday/world/entitydatabase.h @@ -46,13 +46,13 @@ class LIBDOOMSDAY_PUBLIC EntityDatabase EntityDatabase(); /// @return Total number of entities by definition @a entityDef. - uint entityCount(MapEntityDef const *entityDef); + uint entityCount(MapEntityDef const *entityDef) const; /** * Returns @c true iff an entity with definition @a entityDef and * @a elementIndex is known/present. */ - bool hasEntity(MapEntityDef const *entityDef, int elementIndex); + bool hasEntity(MapEntityDef const *entityDef, int elementIndex) const; /** * Lookup a known entity element property value in the database. @@ -62,7 +62,7 @@ class LIBDOOMSDAY_PUBLIC EntityDatabase * * @return The found PropertyValue. */ - PropertyValue const &property(MapEntityPropertyDef const *def, int elementIndex); + PropertyValue const &property(MapEntityPropertyDef const *def, int elementIndex) const; /** * Replace/add a value for a known entity element property to the database. diff --git a/doomsday/apps/libdoomsday/include/doomsday/world/entitydef.h b/doomsday/apps/libdoomsday/include/doomsday/world/entitydef.h index db012e66d4..e14b5d83d4 100644 --- a/doomsday/apps/libdoomsday/include/doomsday/world/entitydef.h +++ b/doomsday/apps/libdoomsday/include/doomsday/world/entitydef.h @@ -1,6 +1,6 @@ /** @file entitydef.h World map entity definitions. * - * @authors Copyright © 2003-2013 Jaakko Keränen + * @authors Copyright © 2003-2016 Jaakko Keränen * @authors Copyright © 2006-2013 Daniel Swanson * * @par License @@ -130,12 +130,13 @@ LIBDOOMSDAY_PUBLIC void P_ShutdownMapEntityDefs(); LIBDOOMSDAY_PUBLIC dd_bool P_RegisterMapObj(int identifier, char const *name); LIBDOOMSDAY_PUBLIC dd_bool P_RegisterMapObjProperty(int entityId, int propertyId, char const *propertyName, valuetype_t type); -LIBDOOMSDAY_PUBLIC byte P_GetGMOByte(int entityId, int elementIndex, int propertyId); -LIBDOOMSDAY_PUBLIC short P_GetGMOShort(int entityId, int elementIndex, int propertyId); -LIBDOOMSDAY_PUBLIC int P_GetGMOInt(int entityId, int elementIndex, int propertyId); -LIBDOOMSDAY_PUBLIC fixed_t P_GetGMOFixed(int entityId, int elementIndex, int propertyId); -LIBDOOMSDAY_PUBLIC angle_t P_GetGMOAngle(int entityId, int elementIndex, int propertyId); -LIBDOOMSDAY_PUBLIC float P_GetGMOFloat(int entityId, int elementIndex, int propertyId); +LIBDOOMSDAY_PUBLIC byte P_GetGMOByte(int entityId, int elementIndex, int propertyId); +LIBDOOMSDAY_PUBLIC short P_GetGMOShort(int entityId, int elementIndex, int propertyId); +LIBDOOMSDAY_PUBLIC int P_GetGMOInt(int entityId, int elementIndex, int propertyId); +LIBDOOMSDAY_PUBLIC fixed_t P_GetGMOFixed(int entityId, int elementIndex, int propertyId); +LIBDOOMSDAY_PUBLIC angle_t P_GetGMOAngle(int entityId, int elementIndex, int propertyId); +LIBDOOMSDAY_PUBLIC float P_GetGMOFloat(int entityId, int elementIndex, int propertyId); +LIBDOOMSDAY_PUBLIC double P_GetGMODouble(int entityId, int elementIndex, int propertyId); #ifdef __cplusplus } // extern "C" diff --git a/doomsday/apps/libdoomsday/include/doomsday/world/propertyvalue.h b/doomsday/apps/libdoomsday/include/doomsday/world/propertyvalue.h index 3a0c5f70dc..98968c48ea 100644 --- a/doomsday/apps/libdoomsday/include/doomsday/world/propertyvalue.h +++ b/doomsday/apps/libdoomsday/include/doomsday/world/propertyvalue.h @@ -38,12 +38,13 @@ class LIBDOOMSDAY_PUBLIC PropertyValue virtual valuetype_t type() const = 0; virtual char const* typeName() const = 0; - virtual byte asByte() const = 0; - virtual int16_t asInt16() const = 0; - virtual int32_t asInt32() const = 0; - virtual fixed_t asFixed() const = 0; - virtual angle_t asAngle() const = 0; - virtual float asFloat() const = 0; + virtual byte asByte() const = 0; + virtual int16_t asInt16() const = 0; + virtual int32_t asInt32() const = 0; + virtual fixed_t asFixed() const = 0; + virtual angle_t asAngle() const = 0; + virtual float asFloat() const = 0; + virtual double asDouble() const = 0; }; class LIBDOOMSDAY_PUBLIC PropertyByteValue : public PropertyValue @@ -56,12 +57,13 @@ class LIBDOOMSDAY_PUBLIC PropertyByteValue : public PropertyValue byte value() const { return value_; } - byte asByte() const { return value_; } - int16_t asInt16() const { return value_; } - int32_t asInt32() const { return value_; } - fixed_t asFixed() const { return value_ << FRACBITS; } - angle_t asAngle() const { return value_; } - float asFloat() const { return value_; } + byte asByte() const { return value_; } + int16_t asInt16() const { return value_; } + int32_t asInt32() const { return value_; } + fixed_t asFixed() const { return value_ << FRACBITS; } + angle_t asAngle() const { return value_; } + float asFloat() const { return value_; } + double asDouble() const { return value_; } private: byte value_; @@ -77,12 +79,13 @@ class LIBDOOMSDAY_PUBLIC PropertyInt16Value : public PropertyValue int16_t value() const { return value_; } - byte asByte() const { return byte(value_); } - int16_t asInt16() const { return value_; } - int32_t asInt32() const { return int32_t(value_); } - fixed_t asFixed() const { return fixed_t(value_ << FRACBITS); } - angle_t asAngle() const { return angle_t(value_); } - float asFloat() const { return float(value_); } + byte asByte() const { return byte(value_); } + int16_t asInt16() const { return value_; } + int32_t asInt32() const { return int32_t(value_); } + fixed_t asFixed() const { return fixed_t(value_ << FRACBITS); } + angle_t asAngle() const { return angle_t(value_); } + float asFloat() const { return float(value_); } + double asDouble() const { return double(value_); } private: int16_t value_; @@ -98,12 +101,13 @@ class LIBDOOMSDAY_PUBLIC PropertyInt32Value : public PropertyValue int32_t value() const { return value_; } - byte asByte() const { return byte(value_); } - int16_t asInt16() const { return int16_t(value_); } - int32_t asInt32() const { return value_; } - fixed_t asFixed() const { return fixed_t(value_ << FRACBITS); } - angle_t asAngle() const { return angle_t(value_); } - float asFloat() const { return float(value_); } + byte asByte() const { return byte(value_); } + int16_t asInt16() const { return int16_t(value_); } + int32_t asInt32() const { return value_; } + fixed_t asFixed() const { return fixed_t(value_ << FRACBITS); } + angle_t asAngle() const { return angle_t(value_); } + float asFloat() const { return float(value_); } + double asDouble() const { return double(value_); } private: int32_t value_; @@ -119,12 +123,13 @@ class LIBDOOMSDAY_PUBLIC PropertyFixedValue : public PropertyValue fixed_t value() const { return value_; } - byte asByte() const { return byte(value_ >> FRACBITS); } - int16_t asInt16() const { return int16_t(value_ >> FRACBITS); } - int32_t asInt32() const { return int32_t(value_ >> FRACBITS); } - fixed_t asFixed() const { return value_; } - angle_t asAngle() const { return angle_t(value_ >> FRACBITS); } - float asFloat() const { return FIX2FLT(value_); } + byte asByte() const { return byte(value_ >> FRACBITS); } + int16_t asInt16() const { return int16_t(value_ >> FRACBITS); } + int32_t asInt32() const { return int32_t(value_ >> FRACBITS); } + fixed_t asFixed() const { return value_; } + angle_t asAngle() const { return angle_t(value_ >> FRACBITS); } + float asFloat() const { return FIX2FLT(value_); } + double asDouble() const { return FIX2FLT(value_); } private: fixed_t value_; @@ -140,12 +145,13 @@ class LIBDOOMSDAY_PUBLIC PropertyAngleValue : public PropertyValue angle_t value() const { return value_; } - byte asByte() const { return byte(value_); } - int16_t asInt16() const { return int16_t(value_); } - int32_t asInt32() const { return int32_t(value_); } - fixed_t asFixed() const { return fixed_t(value_ << FRACBITS); } - angle_t asAngle() const { return value_; } - float asFloat() const { return float(value_); } + byte asByte() const { return byte(value_); } + int16_t asInt16() const { return int16_t(value_); } + int32_t asInt32() const { return int32_t(value_); } + fixed_t asFixed() const { return fixed_t(value_ << FRACBITS); } + angle_t asAngle() const { return value_; } + float asFloat() const { return float(value_); } + double asDouble() const { return double(value_); } private: angle_t value_; @@ -161,17 +167,40 @@ class LIBDOOMSDAY_PUBLIC PropertyFloatValue : public PropertyValue float value() const { return value_; } - byte asByte() const { return byte(value_); } - int16_t asInt16() const { return int16_t(value_); } - int32_t asInt32() const { return int32_t(value_); } - fixed_t asFixed() const { return FLT2FIX(value_); } - angle_t asAngle() const { return angle_t(value_); } - float asFloat() const { return value_; } + byte asByte() const { return byte(value_); } + int16_t asInt16() const { return int16_t(value_); } + int32_t asInt32() const { return int32_t(value_); } + fixed_t asFixed() const { return FLT2FIX(value_); } + angle_t asAngle() const { return angle_t(value_); } + float asFloat() const { return value_; } + double asDouble() const { return value_; } private: float value_; }; +class LIBDOOMSDAY_PUBLIC PropertyDoubleValue : public PropertyValue +{ +public: + PropertyDoubleValue(double value) : PropertyValue(), value_(value) {} + + valuetype_t type() const { return DDVT_DOUBLE; } + char const *typeName() const { return "double"; } + + double value() const { return value_; } + + byte asByte() const { return byte(value_); } + int16_t asInt16() const { return int16_t(value_); } + int32_t asInt32() const { return int32_t(value_); } + fixed_t asFixed() const { return FLT2FIX(value_); } + angle_t asAngle() const { return angle_t(value_); } + float asFloat() const { return float(value_); } + double asDouble() const { return value_; } + +private: + double value_; +}; + /** * Factory constructor for instantiation of new PropertyValues. * diff --git a/doomsday/apps/libdoomsday/src/world/entitydatabase.cpp b/doomsday/apps/libdoomsday/src/world/entitydatabase.cpp index 1101b3e1f5..2e41dde3c9 100644 --- a/doomsday/apps/libdoomsday/src/world/entitydatabase.cpp +++ b/doomsday/apps/libdoomsday/src/world/entitydatabase.cpp @@ -85,14 +85,14 @@ DENG2_PIMPL(EntityDatabase) EntityDatabase::EntityDatabase() : d(new Impl(this)) {} -uint EntityDatabase::entityCount(MapEntityDef const *entityDef) +uint EntityDatabase::entityCount(MapEntityDef const *entityDef) const { DENG2_ASSERT(entityDef); Entities *set = d->entities(entityDef->id); return set->size(); } -bool EntityDatabase::hasEntity(MapEntityDef const *entityDef, int elementIndex) +bool EntityDatabase::hasEntity(MapEntityDef const *entityDef, int elementIndex) const { DENG2_ASSERT(entityDef); Entities *set = d->entities(entityDef->id); @@ -100,7 +100,7 @@ bool EntityDatabase::hasEntity(MapEntityDef const *entityDef, int elementIndex) } PropertyValue const &EntityDatabase::property(MapEntityPropertyDef const *def, - int elementIndex) + int elementIndex) const { DENG2_ASSERT(def); Entities *set = d->entities(def->entity->id); diff --git a/doomsday/apps/libdoomsday/src/world/entitydef.cpp b/doomsday/apps/libdoomsday/src/world/entitydef.cpp index 9dcb99336a..4587a869c7 100644 --- a/doomsday/apps/libdoomsday/src/world/entitydef.cpp +++ b/doomsday/apps/libdoomsday/src/world/entitydef.cpp @@ -1,6 +1,6 @@ /** @file entitydef.cpp World playsim data structures. * - * @authors Copyright © 2003-2013 Jaakko Keränen + * @authors Copyright © 2003-2016 Jaakko Keränen * @authors Copyright © 2006-2015 Daniel Swanson * * @par License @@ -160,6 +160,7 @@ void MapEntityDef_AddProperty(MapEntityDef* def, int propertyId, const char* pro case DDVT_FIXED: case DDVT_ANGLE: case DDVT_FLOAT: + case DDVT_DOUBLE: break; default: @@ -299,133 +300,70 @@ static void setValue(void *dst, valuetype_t dstType, PropertyValue const &pvalue { switch (dstType) { - case DDVT_FIXED: *((fixed_t *) dst) = pvalue.asFixed(); break; - case DDVT_FLOAT: *( (float *) dst) = pvalue.asFloat(); break; - case DDVT_BYTE: *( (byte *) dst) = pvalue.asByte(); break; - case DDVT_INT: *( (int *) dst) = pvalue.asInt32(); break; - case DDVT_SHORT: *( (short *) dst) = pvalue.asInt16(); break; - case DDVT_ANGLE: *((angle_t *) dst) = pvalue.asAngle(); break; + case DDVT_FIXED: *((fixed_t *) dst) = pvalue.asFixed(); break; + case DDVT_FLOAT: *( (float *) dst) = pvalue.asFloat(); break; + case DDVT_DOUBLE: *( (double *) dst) = pvalue.asDouble(); break; + case DDVT_BYTE: *( (byte *) dst) = pvalue.asByte(); break; + case DDVT_INT: *( (int *) dst) = pvalue.asInt32(); break; + case DDVT_SHORT: *( (short *) dst) = pvalue.asInt16(); break; + case DDVT_ANGLE: *((angle_t *) dst) = pvalue.asAngle(); break; default: throw Error("setValue", QString("Unknown value type %d").arg(dstType)); } } -DENG_EXTERN_C byte P_GetGMOByte(int entityId, int elementIndex, int propertyId) +template +Type getEntityValue(int entityId, int elementIndex, int propertyId) { - byte returnVal = 0; - if (World::get().hasMap()) + try { - try - { - EntityDatabase &db = World::get().map().entityDatabase(); - MapEntityPropertyDef *propDef = entityPropertyDef(entityId, propertyId); - - setValue(&returnVal, DDVT_BYTE, db.property(propDef, elementIndex)); - } - catch (Error const &er) + Type returnVal = 0; + if (World::get().hasMap()) { - LOG_WARNING("%s. Returning 0.") << er.asText(); + EntityDatabase const &db = World::get().map().entityDatabase(); + MapEntityPropertyDef const *propDef = entityPropertyDef(entityId, propertyId); + setValue(&returnVal, returnValueType, db.property(propDef, elementIndex)); } + return returnVal; + } + catch (Error const &er) + { + LOG_WARNING("%s. Returning 0.") << er.asText(); + return 0; } - return returnVal; } -DENG_EXTERN_C short P_GetGMOShort(int entityId, int elementIndex, int propertyId) +DENG_EXTERN_C byte P_GetGMOByte(int entityId, int elementIndex, int propertyId) { - short returnVal = 0; - if (World::get().hasMap()) - { - try - { - EntityDatabase &db = World::get().map().entityDatabase(); - MapEntityPropertyDef *propDef = entityPropertyDef(entityId, propertyId); + return getEntityValue(entityId, elementIndex, propertyId); +} - setValue(&returnVal, DDVT_SHORT, db.property(propDef, elementIndex)); - } - catch (Error const &er) - { - LOG_WARNING("%s. Returning 0.") << er.asText(); - } - } - return returnVal; +DENG_EXTERN_C short P_GetGMOShort(int entityId, int elementIndex, int propertyId) +{ + return getEntityValue(entityId, elementIndex, propertyId); } DENG_EXTERN_C int P_GetGMOInt(int entityId, int elementIndex, int propertyId) { - int returnVal = 0; - if (World::get().hasMap()) - { - try - { - EntityDatabase &db = World::get().map().entityDatabase(); - MapEntityPropertyDef *propDef = entityPropertyDef(entityId, propertyId); - - setValue(&returnVal, DDVT_INT, db.property(propDef, elementIndex)); - } - catch (Error const &er) - { - LOG_WARNING("%s. Returning 0.") << er.asText(); - } - } - return returnVal; + return getEntityValue(entityId, elementIndex, propertyId); } DENG_EXTERN_C fixed_t P_GetGMOFixed(int entityId, int elementIndex, int propertyId) { - fixed_t returnVal = 0; - if (World::get().hasMap()) - { - try - { - EntityDatabase &db = World::get().map().entityDatabase(); - MapEntityPropertyDef *propDef = entityPropertyDef(entityId, propertyId); - - setValue(&returnVal, DDVT_FIXED, db.property(propDef, elementIndex)); - } - catch (Error const &er) - { - LOG_WARNING("%s. Returning 0.") << er.asText(); - } - } - return returnVal; + return getEntityValue(entityId, elementIndex, propertyId); } DENG_EXTERN_C angle_t P_GetGMOAngle(int entityId, int elementIndex, int propertyId) { - angle_t returnVal = 0; - if (World::get().hasMap()) - { - try - { - EntityDatabase &db = World::get().map().entityDatabase(); - MapEntityPropertyDef *propDef = entityPropertyDef(entityId, propertyId); - - setValue(&returnVal, DDVT_ANGLE, db.property(propDef, elementIndex)); - } - catch (Error const &er) - { - LOG_WARNING("%s. Returning 0.") << er.asText(); - } - } - return returnVal; + return getEntityValue(entityId, elementIndex, propertyId); } DENG_EXTERN_C float P_GetGMOFloat(int entityId, int elementIndex, int propertyId) { - float returnVal = 0; - if (World::get().hasMap()) - { - try - { - EntityDatabase &db = World::get().map().entityDatabase(); - MapEntityPropertyDef *propDef = entityPropertyDef(entityId, propertyId); + return getEntityValue(entityId, elementIndex, propertyId); +} - setValue(&returnVal, DDVT_FLOAT, db.property(propDef, elementIndex)); - } - catch (Error const &er) - { - LOG_WARNING("%s. Returning 0.") << er.asText(); - } - } - return returnVal; +DENG_EXTERN_C double P_GetGMODouble(int entityId, int elementIndex, int propertyId) +{ + return getEntityValue(entityId, elementIndex, propertyId); }