From 5890a30fed496bf0829e9d1808383aa3077f7f36 Mon Sep 17 00:00:00 2001 From: kko Date: Sun, 23 Sep 2012 09:08:16 -0400 Subject: [PATCH] Only use SDL_ListModes from Graphics.cpp --- src/GameMenuView.cpp | 61 ++++++++++++++++++++------------------- src/GameMenuView.h | 4 ++- src/graphics/Graphics.cpp | 36 ++++++++++++++++------- src/graphics/Graphics.h | 10 +++++++ 4 files changed, 69 insertions(+), 42 deletions(-) diff --git a/src/GameMenuView.cpp b/src/GameMenuView.cpp index d86a02419d0..f4281097cc9 100644 --- a/src/GameMenuView.cpp +++ b/src/GameMenuView.cpp @@ -244,41 +244,42 @@ GameMenuView::GameMenuView(): View() m_musicVolume->onChanged.connect(sigc::mem_fun(this, &GameMenuView::OnChangeVolume)); } + // Video mode selector + m_videoModes = Graphics::GetAvailableVideoModes(); vbox->PackEnd((new Gui::Label(Lang::VIDEO_RESOLUTION))->Color(1.0f,1.0f,0.0f)); m_screenModesGroup = new Gui::RadioGroup(); - SDL_Rect **modes; - modes = SDL_ListModes(NULL, SDL_FULLSCREEN|SDL_HWSURFACE); - if ((modes!=0) && (modes != reinterpret_cast(-1))) { - // box to put the scroll portal and its scroll bar into - Gui::HBox *scrollHBox = new Gui::HBox(); - vbox->PackEnd(scrollHBox); - Gui::VScrollBar *scroll = new Gui::VScrollBar(); - Gui::VScrollPortal *portal = new Gui::VScrollPortal(280); - scroll->SetAdjustment(&portal->vscrollAdjust); - scrollHBox->PackEnd(portal); - scrollHBox->PackEnd(scroll); + // box to put the scroll portal and its scroll bar into + Gui::HBox *scrollHBox = new Gui::HBox(); + vbox->PackEnd(scrollHBox); - Gui::VBox *vbox2 = new Gui::VBox(); - portal->Add(vbox2); + Gui::VScrollBar *scroll = new Gui::VScrollBar(); + Gui::VScrollPortal *portal = new Gui::VScrollPortal(280); + scroll->SetAdjustment(&portal->vscrollAdjust); + scrollHBox->PackEnd(portal); + scrollHBox->PackEnd(scroll); - for (int i=0; modes[i]; ++i) { - Gui::RadioButton *temp = new Gui::RadioButton(m_screenModesGroup); - temp->onSelect.connect(sigc::bind(sigc::mem_fun(this, - &GameMenuView::OnChangeVideoResolution), i)); - Gui::HBox *hbox = new Gui::HBox(); - hbox->SetSpacing(5.0f); - hbox->PackEnd(temp); - hbox->PackEnd(new Gui::Label(stringf(Lang::X_BY_X, formatarg("x", int(modes[i]->w)), formatarg("y", int(modes[i]->h))))); - vbox2->PackEnd(hbox); - if ((Pi::GetScrWidth() == modes[i]->w) && (Pi::GetScrHeight() == modes[i]->h)) { - temp->SetSelected(true); - } + Gui::VBox *vbox2 = new Gui::VBox(); + portal->Add(vbox2); + + for (std::vector::const_iterator it = m_videoModes.begin(); + it != m_videoModes.end(); ++it) { + Gui::RadioButton *temp = new Gui::RadioButton(m_screenModesGroup); + temp->onSelect.connect(sigc::bind(sigc::mem_fun(this, + &GameMenuView::OnChangeVideoResolution), it - m_videoModes.begin())); + Gui::HBox *hbox = new Gui::HBox(); + hbox->SetSpacing(5.0f); + hbox->PackEnd(temp); + hbox->PackEnd(new Gui::Label(stringf(Lang::X_BY_X, formatarg("x", int(it->width)), formatarg("y", int(it->height))))); + vbox2->PackEnd(hbox); + //mark the current video mode + if ((Pi::GetScrWidth() == it->width) && (Pi::GetScrHeight() == it->height)) { + temp->SetSelected(true); } } - + //Graphical detail settings Gui::HBox *detailBox = new Gui::HBox(); detailBox->SetSpacing(20.0f); mainTab->Add(detailBox, 350, 60); @@ -568,11 +569,11 @@ void GameMenuView::OnChangeLanguage(std::string &lang) Pi::config->Save(); } -void GameMenuView::OnChangeVideoResolution(int res) +void GameMenuView::OnChangeVideoResolution(int modeIndex) { - SDL_Rect **modes = SDL_ListModes(NULL, SDL_FULLSCREEN|SDL_HWSURFACE); - Pi::config->SetInt("ScrWidth", modes[res]->w); - Pi::config->SetInt("ScrHeight", modes[res]->h); + const Graphics::VideoMode &mode = m_videoModes.at(modeIndex); + Pi::config->SetInt("ScrWidth", mode.width); + Pi::config->SetInt("ScrHeight", mode.height); Pi::config->Save(); } diff --git a/src/GameMenuView.h b/src/GameMenuView.h index f5211e26211..6698a204459 100644 --- a/src/GameMenuView.h +++ b/src/GameMenuView.h @@ -6,8 +6,9 @@ #include "libs.h" #include "gui/Gui.h" -#include "View.h" #include "KeyBindings.h" +#include "View.h" +#include "graphics/Graphics.h" //contains a slider, mute button and the necessary layout fluff class VolumeControl : public Gui::HBox @@ -103,6 +104,7 @@ class GameMenuView: public View { Gui::ToggleButton *m_toggleJoystick; Gui::ToggleButton *m_toggleMouseYInvert; Gui::ToggleButton *m_toggleNavTunnel; + std::vector m_videoModes; }; #endif /* _GAMEMENUVIEW_H */ diff --git a/src/graphics/Graphics.cpp b/src/graphics/Graphics.cpp index affeadd789f..8ada61ce0cb 100644 --- a/src/graphics/Graphics.cpp +++ b/src/graphics/Graphics.cpp @@ -63,18 +63,11 @@ Renderer* Init(const Settings &vs) // no mode set, find an ok one if ((width <= 0) || (height <= 0)) { - SDL_Rect **modes = SDL_ListModes(NULL, SDL_HWSURFACE | SDL_FULLSCREEN); + const std::vector modes = GetAvailableVideoModes(); + assert(!modes.empty()); - if (modes == 0) { - fprintf(stderr, "It seems no video modes are available..."); - } - if (modes == reinterpret_cast(-1)) { - // hm. all modes available. odd. try 800x600 - width = 800; height = 600; - } else { - width = modes[0]->w; - height = modes[0]->h; - } + width = modes.front().width; + height = modes.front().height; } const SDL_VideoInfo *info = SDL_GetVideoInfo(); @@ -192,6 +185,27 @@ bool AreShadersEnabled() return shadersEnabled; } +std::vector GetAvailableVideoModes() +{ + std::vector modes; + //querying modes using the current pixel format + //note - this has always been sdl_fullscreen, hopefully it does not matter + SDL_Rect **sdlmodes = SDL_ListModes(NULL, SDL_HWSURFACE | SDL_FULLSCREEN); + + if (sdlmodes == 0) + OS::Error("Failed to query video modes"); + + if (sdlmodes == reinterpret_cast(-1)) { + // Modes restricted. Fall back to 800x600 + modes.push_back(VideoMode(800, 600)); + } else { + for (int i=0; sdlmodes[i]; ++i) { + modes.push_back(VideoMode(sdlmodes[i]->w, sdlmodes[i]->h)); + } + } + return modes; +} + void Graphics::State::SetLights(int n, const Light *lights) { m_lights.clear(); diff --git a/src/graphics/Graphics.h b/src/graphics/Graphics.h index 256fd2a5945..79c507e249a 100644 --- a/src/graphics/Graphics.h +++ b/src/graphics/Graphics.h @@ -28,6 +28,15 @@ namespace Graphics { int width; }; + //for querying available modes + struct VideoMode { + VideoMode(unsigned int w, unsigned int h) + : width(w), height(h) { } + + unsigned int width; + unsigned int height; + }; + /* static */ class State { private: static std::vector m_lights; @@ -47,6 +56,7 @@ namespace Graphics { Renderer* Init(const Settings&); void Uninit(); bool AreShadersEnabled(); + std::vector GetAvailableVideoModes(); void UnbindAllBuffers(); void BindArrayBuffer(GLuint bo);