Skip to content

Commit

Permalink
Further improve pyImageLibMod interface.
Browse files Browse the repository at this point in the history
Includes fixes suggested by code review.
  • Loading branch information
Deledrius committed Nov 10, 2024
1 parent a6c66f7 commit 13117a7
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 37 deletions.
26 changes: 15 additions & 11 deletions Sources/Plasma/FeatureLib/pfPython/pyImageLibMod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void pyImageLibMod::setKey(pyKey& ilmKey) // only for python glue, do NOT call
fModifierKey = ilmKey.getKey();
}

pyImage* pyImageLibMod::GetImage(const ST::string& name) const
PyObject* pyImageLibMod::GetImage(const ST::string& name) const
{
plBitmap* image;

Expand All @@ -67,35 +67,39 @@ pyImage* pyImageLibMod::GetImage(const ST::string& name) const
else
image = plImageLibMod::ConvertNoRef(fModifierKey->ObjectIsLoaded())->GetImage(name);

return pyImage::ConvertFrom(pyImage::New(dynamic_cast<plMipmap*>(image)));
if (image)
return pyImage::New(plMipmap::ConvertNoRef(image));

PYTHON_RETURN_NONE;
}

const std::vector<pyImage*> pyImageLibMod::GetImages() const
std::vector<PyObject*> pyImageLibMod::GetImages() const
{
std::vector<pyImage*> imageList;
std::vector<PyObject*> imageList;
plImageLibMod* mod;

if (fModifier)
mod = fModifier;
else
mod = plImageLibMod::ConvertNoRef(fModifierKey->ObjectIsLoaded());

for (const auto& image : mod->GetImages())
imageList.push_back(pyImage::ConvertFrom(pyImage::New(dynamic_cast<plMipmap*>(image))));
imageList.reserve(mod->GetImages().size());
for (const auto& image : mod->GetImages()) {
if (image)
imageList.push_back(pyImage::New(plMipmap::ConvertNoRef(image)));
}

return imageList;
}

const std::vector<ST::string> pyImageLibMod::GetImageNames() const
std::vector<ST::string> pyImageLibMod::GetImageNames() const
{
std::vector<ST::string> nameList;
plImageLibMod* mod;

if (fModifier)
mod = fModifier;
else
mod = plImageLibMod::ConvertNoRef(fModifierKey->ObjectIsLoaded());

for (const auto& name : mod->GetImageNames())
nameList.push_back(name);
return nameList;
return mod->GetImageNames();
}
8 changes: 4 additions & 4 deletions Sources/Plasma/FeatureLib/pfPython/pyImageLibMod.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ You can contact Cyan Worlds, Inc. by email [email protected]

#include "pyGlueHelpers.h"

#include "pnKeyedObject/plKey.h"
#include "plModifier/plImageLibMod.h"

class plKey;
class pyImage;

class pyImageLibMod
Expand Down Expand Up @@ -116,9 +116,9 @@ class pyImageLibMod
plKey GetKey() const { return fModifier ? fModifier->GetKey() : fModifierKey; }

// for python access
pyImage* GetImage (const ST::string& name) const;
const std::vector<ST::string> GetImageNames() const;
const std::vector<pyImage*> GetImages() const;
PyObject* GetImage (const ST::string& name) const;
std::vector<ST::string> GetImageNames() const;
std::vector<PyObject*> GetImages() const;
};

#endif // pyImageLibMod_h
12 changes: 4 additions & 8 deletions Sources/Plasma/FeatureLib/pfPython/pyImageLibModGlue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,15 @@ PYTHON_METHOD_DEFINITION(ptImageLibMod, getImage, args)
PYTHON_RETURN_ERROR;
}

pyImage* image = self->fThis->GetImage(name);
if (image)
return pyImage::New(image->GetKey());

PYTHON_RETURN_NONE;
return self->fThis->GetImage(name);
}

PYTHON_METHOD_DEFINITION_NOARGS(ptImageLibMod, getImages)
{
const std::vector<pyImage*> imageList = self->fThis->GetImages();
const std::vector<PyObject*> imageList = self->fThis->GetImages();
PyObject* retVal = PyTuple_New(imageList.size());
for (size_t curKey = 0; curKey < imageList.size(); curKey++)
PyTuple_SET_ITEM(retVal, curKey, pyImage::New(imageList[curKey]->GetKey()));
PyTuple_SET_ITEM(retVal, curKey, imageList[curKey]);
return retVal;
}

Expand All @@ -101,7 +97,7 @@ PYTHON_METHOD_DEFINITION_NOARGS(ptImageLibMod, getNames)
std::vector<ST::string> nameList = self->fThis->GetImageNames();
PyObject* retVal = PyTuple_New(nameList.size());
for (size_t curKey = 0; curKey < nameList.size(); curKey++)
PyTuple_SET_ITEM(retVal, curKey, PyUnicode_FromSTString(nameList[curKey])); // steals the nameList ref
PyTuple_SET_ITEM(retVal, curKey, PyUnicode_FromSTString(nameList[curKey]));
return retVal;
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/Plasma/FeatureLib/pfPython/pySceneObjectGlue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ PYTHON_METHOD_DEFINITION_NOARGS(ptSceneobject, getImageLibMods)
std::vector<PyObject*> vecList = self->fThis->GetImageLibMods();
PyObject* retVal = PyTuple_New(vecList.size());
for (int curKey = 0; curKey < vecList.size(); curKey++)
PyTuple_SetItem(retVal, curKey, vecList[curKey]);
PyTuple_SET_ITEM(retVal, curKey, vecList[curKey]);
return retVal;
}

Expand Down
14 changes: 4 additions & 10 deletions Sources/Plasma/PubUtilLib/plModifier/plImageLibMod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,12 @@ plBitmap* plImageLibMod::GetImage(const ST::string& imageName) const
return nullptr;
}

const std::vector<plBitmap*> plImageLibMod::GetImages() const
{
std::vector<plBitmap*> images;
for (auto image : fImages)
images.emplace_back(image);
return images;
}

const std::vector<ST::string> plImageLibMod::GetImageNames() const
std::vector<ST::string> plImageLibMod::GetImageNames() const
{
std::vector<ST::string> names;
for (auto image : fImages) {
names.reserve(fImages.size());

for (const auto& image : fImages) {
if (image)
names.emplace_back(image->GetKeyName());
}
Expand Down
5 changes: 2 additions & 3 deletions Sources/Plasma/PubUtilLib/plModifier/plImageLibMod.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ class plImageLibMod : public plSingleModifier

public:
plImageLibMod() {};
virtual ~plImageLibMod() {};

CLASSNAME_REGISTER( plImageLibMod );
GETINTERFACE_ANY( plImageLibMod, plSingleModifier );
Expand All @@ -76,8 +75,8 @@ class plImageLibMod : public plSingleModifier

size_t GetNumImages() const { return fImages.size(); }
plBitmap* GetImage(const ST::string&) const;
const std::vector<plBitmap*> GetImages() const;
const std::vector<ST::string> GetImageNames() const;
std::vector<plBitmap*> GetImages() const { return fImages; }
std::vector<ST::string> GetImageNames() const;
};

#endif // plImageLibMod_inc

0 comments on commit 13117a7

Please sign in to comment.