From be41e30c00d4050dba5802fe6731a4a1cbe55c02 Mon Sep 17 00:00:00 2001 From: Joel Teichroeb Date: Wed, 10 Jul 2013 16:17:28 -0700 Subject: [PATCH] GRIM: Make lights use an enum instead of checking against a string to get the type --- engines/grim/gfx_opengl.cpp | 10 ++++---- engines/grim/gfx_tinygl.cpp | 10 ++++---- engines/grim/savegame.cpp | 2 +- engines/grim/set.cpp | 47 ++++++++++++++++++++++--------------- engines/grim/set.h | 9 ++++++- 5 files changed, 45 insertions(+), 33 deletions(-) diff --git a/engines/grim/gfx_opengl.cpp b/engines/grim/gfx_opengl.cpp index 314b062458d..648ad9585eb 100644 --- a/engines/grim/gfx_opengl.cpp +++ b/engines/grim/gfx_opengl.cpp @@ -701,16 +701,16 @@ void GfxOpenGL::setupLight(Light *light, int lightId) { lightColor[1] = ((float)light->_color.getGreen() / 15.0f) * intensity; lightColor[2] = ((float)light->_color.getBlue() / 15.0f) * intensity; - if (light->_type == "omni") { + if (light->_type == Light::Omni) { lightPos[0] = light->_pos.x(); lightPos[1] = light->_pos.y(); lightPos[2] = light->_pos.z(); - } else if (light->_type == "direct") { + } else if (light->_type == Light::Direct) { lightPos[0] = -light->_dir.x(); lightPos[1] = -light->_dir.y(); lightPos[2] = -light->_dir.z(); lightPos[3] = 0; - } else if (light->_type == "spot") { + } else if (light->_type == Light::Spot) { lightPos[0] = light->_pos.x(); lightPos[1] = light->_pos.y(); lightPos[2] = light->_pos.z(); @@ -718,10 +718,8 @@ void GfxOpenGL::setupLight(Light *light, int lightId) { lightDir[1] = light->_dir.y(); lightDir[2] = light->_dir.z(); cutoff = light->_penumbraangle; - } else { - error("Set::setupLights() Unknown type of light: %s", light->_type.c_str()); - return; } + glDisable(GL_LIGHT0 + lightId); glLightfv(GL_LIGHT0 + lightId, GL_DIFFUSE, lightColor); glLightfv(GL_LIGHT0 + lightId, GL_POSITION, lightPos); diff --git a/engines/grim/gfx_tinygl.cpp b/engines/grim/gfx_tinygl.cpp index d36d9c0e5bb..20d432c2f03 100644 --- a/engines/grim/gfx_tinygl.cpp +++ b/engines/grim/gfx_tinygl.cpp @@ -810,16 +810,16 @@ void GfxTinyGL::setupLight(Light *light, int lightId) { lightColor[1] = ((float)light->_color.getGreen() / 15.0f) * intensity; lightColor[2] = ((float)light->_color.getBlue() / 15.0f) * intensity; - if (light->_type == "omni") { + if (light->_type == Light::Omni) { lightPos[0] = light->_pos.x(); lightPos[1] = light->_pos.y(); lightPos[2] = light->_pos.z(); - } else if (light->_type == "direct") { + } else if (light->_type == Light::Direct) { lightPos[0] = -light->_dir.x(); lightPos[1] = -light->_dir.y(); lightPos[2] = -light->_dir.z(); lightPos[3] = 0; - } else if (light->_type == "spot") { + } else if (light->_type == Light::Spot) { lightPos[0] = light->_pos.x(); lightPos[1] = light->_pos.y(); lightPos[2] = light->_pos.z(); @@ -831,10 +831,8 @@ void GfxTinyGL::setupLight(Light *light, int lightId) { Reproducing: turn off all lights (comment out), go to scene "al", and walk along left wall under the lamp. */ cutoff = 90.0f; - } else { - error("Set::setupLights() Unknown type of light: %s", light->_type.c_str()); - return; } + tglDisable(TGL_LIGHT0 + lightId); tglLightfv(TGL_LIGHT0 + lightId, TGL_DIFFUSE, lightColor); tglLightfv(TGL_LIGHT0 + lightId, TGL_POSITION, lightPos); diff --git a/engines/grim/savegame.cpp b/engines/grim/savegame.cpp index af787dceb98..0bd379961ef 100644 --- a/engines/grim/savegame.cpp +++ b/engines/grim/savegame.cpp @@ -35,7 +35,7 @@ namespace Grim { #define SAVEGAME_FOOTERTAG 'ESAV' uint SaveGame::SAVEGAME_MAJOR_VERSION = 22; -uint SaveGame::SAVEGAME_MINOR_VERSION = 7; +uint SaveGame::SAVEGAME_MINOR_VERSION = 8; SaveGame *SaveGame::openForLoading(const Common::String &filename) { Common::InSaveFile *inSaveFile = g_system->getSavefileManager()->openForLoading(filename); diff --git a/engines/grim/set.cpp b/engines/grim/set.cpp index 4f34fca3e9b..4f6071da554 100644 --- a/engines/grim/set.cpp +++ b/engines/grim/set.cpp @@ -424,7 +424,16 @@ void Light::load(TextSplitter &ts) { _name = buf; ts.scanString(" type %256s", 1, buf); - _type = buf; + Common::String type = buf; + if (type == "spot") { + _type = Spot; + } else if (type == "omni") { + _type = Omni; + } else if (type == "direct") { + _type = Direct; + } else { + error("Light::load() Unknown type of light: %s", buf); + } ts.scanString(" position %f %f %f", 3, &_pos.x(), &_pos.y(), &_pos.z()); ts.scanString(" direction %f %f %f", 3, &_dir.x(), &_dir.y(), &_dir.z()); @@ -455,23 +464,12 @@ void Light::loadBinary(Common::SeekableReadStream *data) { data->read(&_dir.z(), 4); data->read(&_intensity, 4); - int type = data->readSint32LE(); + // This relies on the order of the LightType enum, which might not be correct. + // The order should only affect EMI, and not Grim. + _type = (LightType)data->readSint32LE(); - //Probably all wrong. - switch (type) { - case 1: - _type = "spot"; - break; - case 2: - _type = "direct"; - break; - case 3: - _type = "omni"; - break; - case 4: - //This is probably some new kind of ambient light - _type = "omni"; - break; + if (_type == UnknownLight) { + warning("light %s using UnkownLight"); } // No ideas for these two. @@ -503,7 +501,7 @@ void Light::saveState(SaveGame *savedState) const { savedState->writeBool(_enabled); //type - savedState->writeString(_type); + savedState->writeLEUint32(_type); savedState->writeVector3d(_pos); savedState->writeVector3d(_dir); @@ -518,7 +516,18 @@ void Light::saveState(SaveGame *savedState) const { bool Light::restoreState(SaveGame *savedState) { _name = savedState->readString(); _enabled = savedState->readBool(); - _type = savedState->readString(); + if (savedState->saveMinorVersion() > 7) { + _type = (LightType)savedState->readLEUint32(); + } else { + Common::String type = savedState->readString(); + if (type == "spot") { + _type = Spot; + } else if (type == "omni") { + _type = Omni; + } else if (type == "direct") { + _type = Direct; + } + } _pos = savedState->readVector3d(); _dir = savedState->readVector3d(); diff --git a/engines/grim/set.h b/engines/grim/set.h index 7de8ab381aa..2eddb535abd 100644 --- a/engines/grim/set.h +++ b/engines/grim/set.h @@ -156,8 +156,15 @@ public: void saveState(SaveGame *savedState) const; bool restoreState(SaveGame *savedState); + enum LightType { + Spot, + Direct, + Omni, + UnknownLight + }; + Common::String _name; - Common::String _type; + LightType _type; Math::Vector3d _pos, _dir; Color _color; float _intensity, _umbraangle, _penumbraangle;