BACKENDS: SDL: Do not allocate memory in SurfaceSdlGraphicsManager::getSupportedGraphicsModes

This commit is contained in:
Thierry Crozat 2021-05-08 13:25:50 +01:00
parent 800c673b62
commit bf406f66c0
3 changed files with 15 additions and 31 deletions

View file

@ -300,27 +300,8 @@ static void initGraphicsModes() {
s_supportedGraphicsModes->push_back(gm); s_supportedGraphicsModes->push_back(gm);
} }
const OSystem::GraphicsMode *SurfaceSdlGraphicsManager::supportedGraphicsModes() const {
if (!s_supportedGraphicsModes)
initGraphicsModes();
int size = s_supportedGraphicsModes->size();
OSystem::GraphicsMode *modes = new OSystem::GraphicsMode[size];
memcpy(modes, &(*s_supportedGraphicsModes)[0], size * sizeof(OSystem::GraphicsMode));
// Do deep copy. Each can be freed independently of the other.
OSystem::GraphicsMode *gm = modes;
while (gm->name) {
gm->name = scumm_strdup(gm->name);
gm->description = scumm_strdup(gm->description);
++gm;
}
return modes;
}
const OSystem::GraphicsMode *SurfaceSdlGraphicsManager::getSupportedGraphicsModes() const { const OSystem::GraphicsMode *SurfaceSdlGraphicsManager::getSupportedGraphicsModes() const {
return supportedGraphicsModes(); return s_supportedGraphicsModes->begin();
} }
int SurfaceSdlGraphicsManager::getDefaultGraphicsMode() const { int SurfaceSdlGraphicsManager::getDefaultGraphicsMode() const {

View file

@ -65,7 +65,6 @@ public:
virtual void setFeatureState(OSystem::Feature f, bool enable) override; virtual void setFeatureState(OSystem::Feature f, bool enable) override;
virtual bool getFeatureState(OSystem::Feature f) const override; virtual bool getFeatureState(OSystem::Feature f) const override;
const OSystem::GraphicsMode *supportedGraphicsModes() const;
virtual const OSystem::GraphicsMode *getSupportedGraphicsModes() const override; virtual const OSystem::GraphicsMode *getSupportedGraphicsModes() const override;
virtual int getDefaultGraphicsMode() const override; virtual int getDefaultGraphicsMode() const override;
virtual bool setGraphicsMode(int mode, uint flags = OSystem::kGfxModeNoFlags) override; virtual bool setGraphicsMode(int mode, uint flags = OSystem::kGfxModeNoFlags) override;

View file

@ -857,22 +857,24 @@ void OSystem_SDL::setupGraphicsModes() {
_defaultSDLMode = _defaultGLMode = -1; _defaultSDLMode = _defaultGLMode = -1;
// Count the number of graphics modes // Count the number of graphics modes
const OSystem::GraphicsMode *srcModes;
const OSystem::GraphicsMode *srcMode; const OSystem::GraphicsMode *srcMode;
int defaultMode; int defaultMode;
GraphicsManager *manager = new SurfaceSdlGraphicsManager(_eventSource, _window); GraphicsManager *manager = new SurfaceSdlGraphicsManager(_eventSource, _window);
defaultMode = manager->getDefaultGraphicsMode(); defaultMode = manager->getDefaultGraphicsMode();
srcModes = manager->getSupportedGraphicsModes(); srcMode = manager->getSupportedGraphicsModes();
srcMode = srcModes;
while (srcMode->name) { while (srcMode->name) {
if (defaultMode == srcMode->id) { if (defaultMode == srcMode->id) {
_defaultSDLMode = _graphicsModes.size(); _defaultSDLMode = _graphicsModes.size();
} }
_graphicsModes.push_back(*srcMode); OSystem::GraphicsMode mode = *srcMode;
// Do deep copy as we are going to delete the GraphicsManager and this may free
// the memory used for its graphics modes.
mode.name = scumm_strdup(srcMode->name);
mode.description = scumm_strdup(srcMode->description);
_graphicsModes.push_back(mode);
srcMode++; srcMode++;
} }
delete[] srcModes;
delete manager; delete manager;
assert(_defaultSDLMode != -1); assert(_defaultSDLMode != -1);
@ -884,7 +886,12 @@ void OSystem_SDL::setupGraphicsModes() {
if (defaultMode == srcMode->id) { if (defaultMode == srcMode->id) {
_defaultGLMode = _graphicsModes.size(); _defaultGLMode = _graphicsModes.size();
} }
_graphicsModes.push_back(*srcMode); OSystem::GraphicsMode mode = *srcMode;
// Do deep copy as we are going to delete the GraphicsManager and this may free
// the memory used for its graphics modes.
mode.name = scumm_strdup(srcMode->name);
mode.description = scumm_strdup(srcMode->description);
_graphicsModes.push_back(mode);
srcMode++; srcMode++;
} }
delete manager; delete manager;
@ -907,14 +914,11 @@ void OSystem_SDL::setupGraphicsModes() {
void OSystem_SDL::clearGraphicsModes() { void OSystem_SDL::clearGraphicsModes() {
if (!_graphicsModes.empty()) { if (!_graphicsModes.empty()) {
int i = 0;
OSystem::GraphicsMode *mode = _graphicsModes.begin(); OSystem::GraphicsMode *mode = _graphicsModes.begin();
while (mode->name && i < _firstGLMode) { while (mode->name) {
free(const_cast<char *>(mode->name)); free(const_cast<char *>(mode->name));
free(const_cast<char *>(mode->description)); free(const_cast<char *>(mode->description));
mode++; mode++;
i++;
} }
_graphicsModes.clear(); _graphicsModes.clear();
} }