Skip to content

Commit

Permalink
Only use SDL_ListModes from Graphics.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
kko committed Sep 23, 2012
1 parent 70946eb commit 5890a30
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 42 deletions.
61 changes: 31 additions & 30 deletions src/GameMenuView.cpp
Expand Up @@ -244,41 +244,42 @@ GameMenuView::GameMenuView(): View()
m_musicVolume->onChanged.connect(sigc::mem_fun(this, &GameMenuView::OnChangeVolume)); 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)); vbox->PackEnd((new Gui::Label(Lang::VIDEO_RESOLUTION))->Color(1.0f,1.0f,0.0f));


m_screenModesGroup = new Gui::RadioGroup(); 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(); // box to put the scroll portal and its scroll bar into
Gui::VScrollPortal *portal = new Gui::VScrollPortal(280); Gui::HBox *scrollHBox = new Gui::HBox();
scroll->SetAdjustment(&portal->vscrollAdjust); vbox->PackEnd(scrollHBox);
scrollHBox->PackEnd(portal);
scrollHBox->PackEnd(scroll);


Gui::VBox *vbox2 = new Gui::VBox(); Gui::VScrollBar *scroll = new Gui::VScrollBar();
portal->Add(vbox2); 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::VBox *vbox2 = new Gui::VBox();
Gui::RadioButton *temp = new Gui::RadioButton(m_screenModesGroup); portal->Add(vbox2);
temp->onSelect.connect(sigc::bind(sigc::mem_fun(this,
&GameMenuView::OnChangeVideoResolution), i)); for (std::vector<Graphics::VideoMode>::const_iterator it = m_videoModes.begin();
Gui::HBox *hbox = new Gui::HBox(); it != m_videoModes.end(); ++it) {
hbox->SetSpacing(5.0f); Gui::RadioButton *temp = new Gui::RadioButton(m_screenModesGroup);
hbox->PackEnd(temp); temp->onSelect.connect(sigc::bind(sigc::mem_fun(this,
hbox->PackEnd(new Gui::Label(stringf(Lang::X_BY_X, formatarg("x", int(modes[i]->w)), formatarg("y", int(modes[i]->h))))); &GameMenuView::OnChangeVideoResolution), it - m_videoModes.begin()));
vbox2->PackEnd(hbox); Gui::HBox *hbox = new Gui::HBox();
if ((Pi::GetScrWidth() == modes[i]->w) && (Pi::GetScrHeight() == modes[i]->h)) { hbox->SetSpacing(5.0f);
temp->SetSelected(true); 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(); Gui::HBox *detailBox = new Gui::HBox();
detailBox->SetSpacing(20.0f); detailBox->SetSpacing(20.0f);
mainTab->Add(detailBox, 350, 60); mainTab->Add(detailBox, 350, 60);
Expand Down Expand Up @@ -568,11 +569,11 @@ void GameMenuView::OnChangeLanguage(std::string &lang)
Pi::config->Save(); Pi::config->Save();
} }


void GameMenuView::OnChangeVideoResolution(int res) void GameMenuView::OnChangeVideoResolution(int modeIndex)
{ {
SDL_Rect **modes = SDL_ListModes(NULL, SDL_FULLSCREEN|SDL_HWSURFACE); const Graphics::VideoMode &mode = m_videoModes.at(modeIndex);
Pi::config->SetInt("ScrWidth", modes[res]->w); Pi::config->SetInt("ScrWidth", mode.width);
Pi::config->SetInt("ScrHeight", modes[res]->h); Pi::config->SetInt("ScrHeight", mode.height);
Pi::config->Save(); Pi::config->Save();
} }


Expand Down
4 changes: 3 additions & 1 deletion src/GameMenuView.h
Expand Up @@ -6,8 +6,9 @@


#include "libs.h" #include "libs.h"
#include "gui/Gui.h" #include "gui/Gui.h"
#include "View.h"
#include "KeyBindings.h" #include "KeyBindings.h"
#include "View.h"
#include "graphics/Graphics.h"


//contains a slider, mute button and the necessary layout fluff //contains a slider, mute button and the necessary layout fluff
class VolumeControl : public Gui::HBox class VolumeControl : public Gui::HBox
Expand Down Expand Up @@ -103,6 +104,7 @@ class GameMenuView: public View {
Gui::ToggleButton *m_toggleJoystick; Gui::ToggleButton *m_toggleJoystick;
Gui::ToggleButton *m_toggleMouseYInvert; Gui::ToggleButton *m_toggleMouseYInvert;
Gui::ToggleButton *m_toggleNavTunnel; Gui::ToggleButton *m_toggleNavTunnel;
std::vector<Graphics::VideoMode> m_videoModes;
}; };


#endif /* _GAMEMENUVIEW_H */ #endif /* _GAMEMENUVIEW_H */
36 changes: 25 additions & 11 deletions src/graphics/Graphics.cpp
Expand Up @@ -63,18 +63,11 @@ Renderer* Init(const Settings &vs)


// no mode set, find an ok one // no mode set, find an ok one
if ((width <= 0) || (height <= 0)) { 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) { width = modes.front().width;
fprintf(stderr, "It seems no video modes are available..."); height = modes.front().height;
}
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;
}
} }


const SDL_VideoInfo *info = SDL_GetVideoInfo(); const SDL_VideoInfo *info = SDL_GetVideoInfo();
Expand Down Expand Up @@ -192,6 +185,27 @@ bool AreShadersEnabled()
return shadersEnabled; 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) void Graphics::State::SetLights(int n, const Light *lights)
{ {
m_lights.clear(); m_lights.clear();
Expand Down
10 changes: 10 additions & 0 deletions src/graphics/Graphics.h
Expand Up @@ -28,6 +28,15 @@ namespace Graphics {
int width; 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 { /* static */ class State {
private: private:
static std::vector<Light> m_lights; static std::vector<Light> m_lights;
Expand All @@ -47,6 +56,7 @@ namespace Graphics {
Renderer* Init(const Settings&); Renderer* Init(const Settings&);
void Uninit(); void Uninit();
bool AreShadersEnabled(); bool AreShadersEnabled();
std::vector<VideoMode> GetAvailableVideoModes();


void UnbindAllBuffers(); void UnbindAllBuffers();
void BindArrayBuffer(GLuint bo); void BindArrayBuffer(GLuint bo);
Expand Down

0 comments on commit 5890a30

Please sign in to comment.