Permalink
Browse files

Only use SDL_ListModes from Graphics.cpp

  • Loading branch information...
1 parent 70946eb commit 5890a30fed496bf0829e9d1808383aa3077f7f36 @Luomu committed Sep 23, 2012
Showing with 69 additions and 42 deletions.
  1. +31 −30 src/GameMenuView.cpp
  2. +3 −1 src/GameMenuView.h
  3. +25 −11 src/graphics/Graphics.cpp
  4. +10 −0 src/graphics/Graphics.h
View
@@ -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<SDL_Rect**>(-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<Graphics::VideoMode>::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();
}
View
@@ -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<Graphics::VideoMode> m_videoModes;
};
#endif /* _GAMEMENUVIEW_H */
@@ -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<VideoMode> modes = GetAvailableVideoModes();
+ assert(!modes.empty());
- if (modes == 0) {
- fprintf(stderr, "It seems no video modes are available...");
- }
- if (modes == reinterpret_cast<SDL_Rect **>(-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<VideoMode> GetAvailableVideoModes()
+{
+ std::vector<VideoMode> 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<SDL_Rect **>(-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();
@@ -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<Light> m_lights;
@@ -47,6 +56,7 @@ namespace Graphics {
Renderer* Init(const Settings&);
void Uninit();
bool AreShadersEnabled();
+ std::vector<VideoMode> GetAvailableVideoModes();
void UnbindAllBuffers();
void BindArrayBuffer(GLuint bo);

0 comments on commit 5890a30

Please sign in to comment.