GRIM: Make lights use an enum instead of checking against a string to get the type

This commit is contained in:
Joel Teichroeb 2013-07-10 16:17:28 -07:00
parent 48bc8de2c8
commit be41e30c00
5 changed files with 45 additions and 33 deletions

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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();

View File

@ -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;