From 86b19d64c2692665aba33e44ee5db6cbf36b6342 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Wed, 13 Sep 2017 21:54:39 +0300 Subject: Add option to select any existing SDL renderer drivers for SDL2 default mode. --- src/defaults.cpp | 1 + src/graphicsmanager.cpp | 2 +- src/gui/models/namesmodel.cpp | 11 +++ src/gui/models/namesmodel.h | 2 + src/gui/widgets/setupitem.cpp | 125 +++++++++++++++++++++++++++++- src/gui/widgets/setupitem.h | 44 +++++++++++ src/gui/widgets/tabs/setup_perfomance.cpp | 28 ++++++- src/gui/widgets/tabs/setup_perfomance.h | 3 + src/utils/sdl2helper.cpp | 39 ++++++++++ src/utils/sdl2helper.h | 5 ++ 10 files changed, 256 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/defaults.cpp b/src/defaults.cpp index e7cd485ca..5dade038f 100644 --- a/src/defaults.cpp +++ b/src/defaults.cpp @@ -417,6 +417,7 @@ void setConfigDefaults(Configuration &cfg) AddDEF("blockAltTab", true); AddDEF("sdlLogLevel", 0); AddDEF("allowHighDPI", false); + AddDEF("sdlDriver", ""); } void setConfigDefaults2(Configuration &cfg) diff --git a/src/graphicsmanager.cpp b/src/graphicsmanager.cpp index dd730af82..108facc73 100644 --- a/src/graphicsmanager.cpp +++ b/src/graphicsmanager.cpp @@ -620,7 +620,7 @@ SDL_Window *GraphicsManager::createWindow(const int w, const int h, SDL_Renderer *GraphicsManager::createRenderer(SDL_Window *const window, const int flags) { - // +++ need use different drivers and different flags + SDL::setRendererHint(config.getStringValue("sdlDriver")); SDL_Renderer *const renderer = SDL_CreateRenderer(window, -1, flags); SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND); return renderer; diff --git a/src/gui/models/namesmodel.cpp b/src/gui/models/namesmodel.cpp index ee26dbdaa..5344a7a7d 100644 --- a/src/gui/models/namesmodel.cpp +++ b/src/gui/models/namesmodel.cpp @@ -21,6 +21,7 @@ #include "gui/models/namesmodel.h" #include "utils/cast.h" +#include "utils/foreach.h" #include "utils/gettext.h" #include "debug.h" @@ -55,3 +56,13 @@ void NamesModel::fillFromArray(const char *const *const arr, for (size_t f = 0; f < sz; f ++) mNames.push_back(gettext(arr[f])); } + +void NamesModel::fillFromVector(const StringVect &vect) +{ + FOR_EACH(StringVectCIter, it, vect) + { + const std::string str = *it; + if (!str.empty()) + mNames.push_back(str); + } +} diff --git a/src/gui/models/namesmodel.h b/src/gui/models/namesmodel.h index ba9a928a4..a239bd9aa 100644 --- a/src/gui/models/namesmodel.h +++ b/src/gui/models/namesmodel.h @@ -55,6 +55,8 @@ class NamesModel notfinal : public ListModel void fillFromArray(const char *const *const arr, const std::size_t size); + void fillFromVector(const StringVect &vect); + protected: StringVect mNames; }; diff --git a/src/gui/widgets/setupitem.cpp b/src/gui/widgets/setupitem.cpp index ee19caed1..67d52ec0e 100644 --- a/src/gui/widgets/setupitem.cpp +++ b/src/gui/widgets/setupitem.cpp @@ -27,6 +27,8 @@ #include "gui/fonts/font.h" +#include "gui/models/listmodel.h" + #include "gui/windows/editdialog.h" #include "gui/widgets/button.h" @@ -692,7 +694,7 @@ void SetupItemDropDown::createControls() mDropDown->setActionEventId(mEventName); mDropDown->addActionListener(mParent); mDropDown->setWidth(mWidth); - mDropDown->setSelected(atoi(mValue.c_str())); + mDropDown->setSelected(selectionByValue()); mWidget = mDropDown; if (!mText.empty()) @@ -706,6 +708,11 @@ void SetupItemDropDown::createControls() mWidget->addActionListener(this); } +int SetupItemDropDown::selectionByValue() +{ + return atoi(mValue.c_str()); +} + void SetupItemDropDown::fromWidget() { if (mDropDown == nullptr) @@ -719,7 +726,121 @@ void SetupItemDropDown::toWidget() if (mDropDown == nullptr) return; - mDropDown->setSelected(atoi(mValue.c_str())); + mDropDown->setSelected(selectionByValue()); +} + + +SetupItemDropDownStr::SetupItemDropDownStr(const std::string &restrict text, + const std::string &restrict + description, + const std::string &restrict keyName, + SetupTabScroll *restrict const + parent, + const std::string &restrict + eventName, + ListModel *restrict const model, + const int width, + const MainConfig mainConfig) : + SetupItem(text, description, keyName, parent, eventName, mainConfig), + mHorizont(nullptr), + mLabel(nullptr), + mModel(model), + mDropDown(nullptr), + mWidth(width) +{ + mValueType = VSTR; + createControls(); +} + +SetupItemDropDownStr::SetupItemDropDownStr(const std::string &restrict text, + const std::string &restrict + description, + const std::string &restrict keyName, + SetupTabScroll *restrict const + parent, + const std::string &restrict + eventName, + ListModel *restrict const model, + const int width, + const std::string &restrict def, + const MainConfig mainConfig) : + SetupItem(text, description, keyName, parent, eventName, def, mainConfig), + mHorizont(nullptr), + mLabel(nullptr), + mModel(model), + mDropDown(nullptr), + mWidth(width) +{ + mValueType = VSTR; + createControls(); +} + +SetupItemDropDownStr::~SetupItemDropDownStr() +{ + mHorizont = nullptr; + mWidget = nullptr; + mModel = nullptr; + mDropDown = nullptr; + mLabel = nullptr; +} + +void SetupItemDropDownStr::createControls() +{ + load(); + mHorizont = new HorizontContainer(this, 32, 2); + + mLabel = new Label(this, mText); + mLabel->setToolTip(mDescription); + mDropDown = new DropDown(this, mModel); + mDropDown->setActionEventId(mEventName); + mDropDown->addActionListener(mParent); + mDropDown->setWidth(mWidth); + mDropDown->setSelected(selectionByValue()); + + mWidget = mDropDown; + if (!mText.empty()) + fixFirstItemSize(mLabel); + mHorizont->add(mLabel); + mHorizont->add(mDropDown); + + mParent->getContainer()->add2(mHorizont, true, 4); + mParent->addControl(this); + mParent->addActionListener(this); + mWidget->addActionListener(this); +} + +int SetupItemDropDownStr::selectionByValue() +{ + const int sz = mModel->getNumberOfElements(); + for (int f = 0; f < sz; f ++) + { + if (mModel->getElementAt(f) == mValue) + { + return f; + } + } + return 0; +} + +void SetupItemDropDownStr::fromWidget() +{ + if (mDropDown == nullptr) + return; + + const int sel = mDropDown->getSelected(); + // use first element in model as empty string + if (sel == 0 || sel >= mModel->getNumberOfElements()) + mValue = ""; + else + mValue = mModel->getElementAt(sel); +} + +void SetupItemDropDownStr::toWidget() +{ + if (mDropDown == nullptr) + return; + + mDropDown->setSelected(selectionByValue()); } diff --git a/src/gui/widgets/setupitem.h b/src/gui/widgets/setupitem.h index a60c970c9..eec63a4bc 100644 --- a/src/gui/widgets/setupitem.h +++ b/src/gui/widgets/setupitem.h @@ -340,6 +340,50 @@ class SetupItemDropDown final : public SetupItem void toWidget() override final; + int selectionByValue(); + + protected: + HorizontContainer *mHorizont; + Label *mLabel; + ListModel *mModel; + DropDown *mDropDown; + int mWidth; +}; + +class SetupItemDropDownStr final : public SetupItem +{ + public: + SetupItemDropDownStr(const std::string &restrict text, + const std::string &restrict description, + const std::string &restrict keyName, + SetupTabScroll *restrict const parent, + const std::string &restrict eventName, + ListModel *restrict const model, + const int width, + const MainConfig mainConfig = MainConfig_true); + + SetupItemDropDownStr(const std::string &restrict text, + const std::string &restrict description, + const std::string &restrict keyName, + SetupTabScroll *restrict const parent, + const std::string &restrict eventName, + ListModel *restrict const model, + const int width, + const std::string &restrict def, + const MainConfig mainConfig = MainConfig_true); + + A_DELETE_COPY(SetupItemDropDownStr) + + ~SetupItemDropDownStr(); + + void createControls(); + + void fromWidget() override final; + + void toWidget() override final; + + int selectionByValue(); + protected: HorizontContainer *mHorizont; Label *mLabel; diff --git a/src/gui/widgets/tabs/setup_perfomance.cpp b/src/gui/widgets/tabs/setup_perfomance.cpp index d28b82680..664183276 100644 --- a/src/gui/widgets/tabs/setup_perfomance.cpp +++ b/src/gui/widgets/tabs/setup_perfomance.cpp @@ -22,6 +22,8 @@ #include "gui/widgets/tabs/setup_perfomance.h" +#include "configuration.h" + #include "gui/models/namesmodel.h" #include "gui/widgets/containerplacer.h" @@ -31,6 +33,9 @@ #include "utils/delete2.h" #include "utils/gettext.h" +#ifdef USE_SDL2 +#include "utils/sdlhelper.h" +#endif // USE_SDL2 #include "debug.h" @@ -48,7 +53,8 @@ static const char *const texturesList[] = Setup_Perfomance::Setup_Perfomance(const Widget2 *const widget) : SetupTabScroll(widget), - mTexturesList(new NamesModel) + mTexturesList(new NamesModel), + mSdlDriversList(new NamesModel) { // TRANSLATORS: settings tab name setName(_("Performance")); @@ -58,6 +64,15 @@ Setup_Perfomance::Setup_Perfomance(const Widget2 *const widget) : ContainerPlacer place = h.getPlacer(0, 0); place(0, 0, mScroll, 10, 10); +#ifdef USE_SDL2 + StringVect sdlDriversList; + SDL::getRenderers(sdlDriversList, + config.getStringValue("sdlDriver")); + sdlDriversList.insert(sdlDriversList.begin(), + // TRANSLATORS: sdl driver name + N_("default")); +#endif // USE_SDL2 + // TRANSLATORS: settings option new SetupItemLabel(_("Better performance (enable for better performance)"), "", this); @@ -138,6 +153,14 @@ Setup_Perfomance::Setup_Perfomance(const Widget2 *const widget) : new SetupItemLabel(_("Different options (enable or disable can " "improve performance)"), "", this); +#ifdef USE_SDL2 + mSdlDriversList->fillFromVector(sdlDriversList); + new SetupItemDropDownStr( + // TRANSLATORS: settings option + _("Try first sdl driver (only for SDL2 default mode)"), + "", "sdlDriver", this, "sdlDriverEvent", mSdlDriversList, 100); +#endif // USE_SDL2 + mTexturesList->fillFromArray(&texturesList[0], texturesListSize); // TRANSLATORS: settings option new SetupItemDropDown(_("Enable texture compression (OpenGL)"), "", @@ -179,4 +202,7 @@ Setup_Perfomance::Setup_Perfomance(const Widget2 *const widget) : Setup_Perfomance::~Setup_Perfomance() { delete2(mTexturesList); +#ifdef USE_SDL2 + delete2(mSdlDriversList); +#endif // USE_SDL2 } diff --git a/src/gui/widgets/tabs/setup_perfomance.h b/src/gui/widgets/tabs/setup_perfomance.h index 6e933c0fc..a4043ab4e 100644 --- a/src/gui/widgets/tabs/setup_perfomance.h +++ b/src/gui/widgets/tabs/setup_perfomance.h @@ -38,6 +38,9 @@ class Setup_Perfomance final : public SetupTabScroll private: NamesModel *mTexturesList; +#ifdef USE_SDL2 + NamesModel *mSdlDriversList; +#endif // USE_SDL2 }; #endif // GUI_WIDGETS_TABS_SETUP_PERFOMANCE_H diff --git a/src/utils/sdl2helper.cpp b/src/utils/sdl2helper.cpp index da86d2b41..ad559b0e6 100644 --- a/src/utils/sdl2helper.cpp +++ b/src/utils/sdl2helper.cpp @@ -28,10 +28,13 @@ #include "utils/sdl2logger.h" #include "utils/stringutils.h" +#include + PRAGMA48(GCC diagnostic push) PRAGMA48(GCC diagnostic ignored "-Wshadow") #include #include +#include #include PRAGMA48(GCC diagnostic pop) @@ -225,4 +228,40 @@ void SDL::allowScreenSaver(const bool allow) } } +void SDL::getRenderers(StringVect &list, + const std::string ¤tRenderer) +{ + SDL_RendererInfo info; + const int num = SDL_GetNumRenderDrivers(); + for (int f = 0; f < num; f ++) + { + if (!SDL_GetRenderDriverInfo(f, &info)) + list.push_back(info.name); + } + if (!currentRenderer.empty()) + { + bool found(false); + FOR_EACH (StringVectCIter, it, list) + { + if (*it == currentRenderer) + { + found = true; + break; + } + } + if (!found) + list.push_back(currentRenderer); + } + std::sort(list.begin(), list.end()); +} + +void SDL::setRendererHint(const std::string &driver) +{ + if (!driver.empty()) + { + SDL_SetHint(SDL_HINT_RENDER_DRIVER, + driver.c_str()); + } +} + #endif // USE_SDL2 diff --git a/src/utils/sdl2helper.h b/src/utils/sdl2helper.h index 4dbbad0f9..6c50bb1be 100644 --- a/src/utils/sdl2helper.h +++ b/src/utils/sdl2helper.h @@ -73,6 +73,11 @@ namespace SDL bool PollEvent(SDL_Event *event); void allowScreenSaver(const bool allow); + + void getRenderers(StringVect &list, + const std::string ¤tRenderer); + + void setRendererHint(const std::string &driver); } // namespace SDL #endif // USE_SDL2 -- cgit v1.2.3-70-g09d2