Skip to content

Commit

Permalink
make screen config be configurable from the UI (hit escape). progress…
Browse files Browse the repository at this point in the history
… on startup since it is getting sluggish

git-svn-id: https://pioneer.svn.sourceforge.net/svnroot/pioneer/trunk@618 e632f14b-6550-0410-b89e-a82653faca30
  • Loading branch information
tompox committed Jul 5, 2010
1 parent 6aade5a commit 32e1e8c
Show file tree
Hide file tree
Showing 14 changed files with 328 additions and 145 deletions.
161 changes: 94 additions & 67 deletions src/GameMenuView.cpp
Expand Up @@ -11,64 +11,14 @@
#include <sys/types.h>
#elif _WIN32
#include <../msvc/win32-dirent.h>
#include <shlobj.h>
#include <shlwapi.h>
#endif

/*
* Must create the folders if they do not exist already.
*/
std::string GetFullSavefileDirPath()
{
// i think this test only works with glibc...
#if _GNU_SOURCE
const char *homedir = getenv("HOME");
std::string path = join_path(homedir, ".pioneer", 0);
DIR *dir = opendir(path.c_str());
if (!dir) {
if (mkdir(path.c_str(), 0770) == -1) {
Gui::Screen::ShowBadError(stringf(128, "Error: Could not create or open '%s'.", path.c_str()).c_str());
}
}
closedir(dir);
path = join_path(homedir, ".pioneer", "savefiles", 0);
dir = opendir(path.c_str());
if (!dir) {
if (mkdir(path.c_str(), 0770) == -1) {
Gui::Screen::ShowBadError(stringf(128, "Error: Could not create or open '%s'.", path.c_str()).c_str());
}
}
closedir(dir);
return path;
#elif _WIN32
try {
TCHAR path[MAX_PATH];
if(S_OK != SHGetFolderPath(0, CSIDL_LOCAL_APPDATA, 0, SHGFP_TYPE_CURRENT, path))
throw std::runtime_error("SHGetFolderPath");

TCHAR temp[MAX_PATH];
MultiByteToWideChar(CP_ACP, 0, "Pioneer", strlen("Pioneer")+1, temp, MAX_PATH);
if(!PathAppend(path, temp))
throw std::runtime_error("PathAppend");

MultiByteToWideChar(CP_ACP, 0, "savefiles", strlen("savefiles")+1, temp, MAX_PATH);
if(!PathAppend(path, temp))
throw std::runtime_error("PathAppend");

if(!PathFileExists(path) && ERROR_SUCCESS != SHCreateDirectoryEx(0, path, 0))
throw std::runtime_error("SHCreateDirectoryEx");

char temp2[MAX_PATH];
WideCharToMultiByte(CP_ACP, 0, path, wcslen(path)+1, temp2, MAX_PATH, 0, 0);
return std::string(temp2);
}
catch(const std::exception&) {
Gui::Screen::ShowBadError("Can't get path to save directory");
return "";
}
#else
# error Unsupported system
#endif
return GetPiUserDir("savefiles");
}

/* Not dirs, not . or .. */
Expand Down Expand Up @@ -254,31 +204,77 @@ GameMenuView::GameMenuView(): View()
l->Color(1,.7,0);
m_rightRegion2->Add(l, 10, 0);

Gui::LabelButton *b;
{
Gui::LabelButton *b;
Gui::Box *hbox = new Gui::HBox();
hbox->SetSpacing(5.0f);
Add(hbox, 20, 50);
b = new Gui::LabelButton(new Gui::Label("[S] Save the game"));
b->SetShortcut(SDLK_s, KMOD_NONE);
b->onClick.connect(sigc::mem_fun(this, &GameMenuView::OpenSaveDialog));
hbox->PackEnd(b, false);
b = new Gui::LabelButton(new Gui::Label("[L] Load a game"));
b->onClick.connect(sigc::mem_fun(this, &GameMenuView::OpenLoadDialog));
b->SetShortcut(SDLK_l, KMOD_NONE);
hbox->PackEnd(b, false);
b = new Gui::LabelButton(new Gui::Label("Exit this game"));
b->onClick.connect(sigc::mem_fun(this, &GameMenuView::HideAll));
b->onClick.connect(sigc::ptr_fun(&Pi::EndGame));
hbox->PackEnd(b, false);
}

Gui::Box *vbox = new Gui::VBox();
vbox->SetSpacing(5.0);
Add(vbox, 5, 100);

b = new Gui::LabelButton(new Gui::Label("[S] Save the game"));
b->SetShortcut(SDLK_s, KMOD_NONE);
b->onClick.connect(sigc::mem_fun(this, &GameMenuView::OpenSaveDialog));
vbox->PackEnd(b, false);
b = new Gui::LabelButton(new Gui::Label("[L] Load a game"));
b->onClick.connect(sigc::mem_fun(this, &GameMenuView::OpenLoadDialog));
b->SetShortcut(SDLK_l, KMOD_NONE);
vbox->PackEnd(b, false);
b = new Gui::LabelButton(new Gui::Label("Exit this game"));
b->onClick.connect(sigc::mem_fun(this, &GameMenuView::HideAll));
b->onClick.connect(sigc::ptr_fun(&Pi::EndGame));
vbox->PackEnd(b, false);
Add(vbox, 20, 100);

vbox->PackEnd(new Gui::Label("Video resolution (restart game to apply)"), false);

Gui::RadioGroup *g = new Gui::RadioGroup();
SDL_Rect **modes;
modes = SDL_ListModes(NULL, SDL_FULLSCREEN|SDL_HWSURFACE);
if ((modes!=0) && (modes != (SDL_Rect**)-1)) {
for (int i=0; modes[i]; ++i) {
Gui::RadioButton *temp = new Gui::RadioButton(g);
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, false);
hbox->PackEnd(new Gui::Label(stringf(256, "%dx%d", modes[i]->w, modes[i]->h)), false);
vbox->PackEnd(hbox, false);
if ((Pi::GetScrWidth() == modes[i]->w) && (Pi::GetScrHeight() == modes[i]->h)) {
temp->SetSelected(true);
}
}
}

{
vbox->PackEnd(new Gui::Label("Windowed or fullscreen (restart to apply)"), false);
m_toggleFullscreen = new Gui::ToggleButton();
m_toggleFullscreen->onChange.connect(sigc::mem_fun(this, &GameMenuView::OnToggleFullscreen));
Gui::HBox *hbox = new Gui::HBox();
hbox->SetSpacing(5.0f);
hbox->PackEnd(m_toggleFullscreen, false);
hbox->PackEnd(new Gui::Label("Full screen"), false);
vbox->PackEnd(hbox, false);

vbox->PackEnd(new Gui::Label("Other graphics settings"), false);
m_toggleShaders = new Gui::ToggleButton();
m_toggleShaders->onChange.connect(sigc::mem_fun(this, &GameMenuView::OnToggleShaders));
hbox = new Gui::HBox();
hbox->SetSpacing(5.0f);
hbox->PackEnd(m_toggleShaders, false);
hbox->PackEnd(new Gui::Label("Use shaders"), false);
vbox->PackEnd(hbox, false);
}


vbox = new Gui::VBox();
vbox->SetSpacing(5.0f);
Add(vbox, 600, 100);

vbox->PackEnd(new Gui::Label("Planet detail level:"));
Gui::RadioGroup *g = new Gui::RadioGroup();
g = new Gui::RadioGroup();

for (int i=0; i<5; i++) {
m_planetDetail[i] = new Gui::RadioButton(g);
Expand Down Expand Up @@ -312,12 +308,41 @@ void GameMenuView::OnChangePlanetDetail(int level)
{
m_changedDetailLevel = true;
Pi::detail.planets = level;
Pi::config.SetInt("DetailPlanets", level);
Pi::config.Save();
}

void GameMenuView::OnChangeCityDetail(int level)
{
m_changedDetailLevel = true;
Pi::detail.cities = level;
Pi::config.SetInt("DetailCities", level);
Pi::config.Save();
}

void GameMenuView::OnChangeVideoResolution(int res)
{
SDL_Rect **modes = SDL_ListModes(NULL, SDL_FULLSCREEN|SDL_HWSURFACE);
Pi::config.SetInt("ScrWidth", modes[res]->w);
Pi::config.SetInt("ScrHeight", modes[res]->h);
Pi::config.Save();
}

void GameMenuView::OnToggleFullscreen(Gui::ToggleButton *b, bool state)
{
Pi::config.SetInt("StartFullscreen", (state ? 1 : 0));
Pi::config.Save();
//#ifndef _WIN32
// XXX figure out how to do it in windows
// SDL_WM_ToggleFullScreen(Pi::scrSurface);
//#endif
}

void GameMenuView::OnToggleShaders(Gui::ToggleButton *b, bool state)
{
Pi::config.SetInt("DisableShaders", (state ? 0 : 1));
Pi::config.Save();
Render::ToggleShaders();
}

void GameMenuView::HideAll()
Expand Down Expand Up @@ -354,6 +379,8 @@ void GameMenuView::OnSwitchTo() {
} else {
m_planetDetail[Pi::detail.planets]->OnActivate();
m_cityDetail[Pi::detail.cities]->OnActivate();
m_toggleShaders->SetPressed(Render::AreShadersEnabled());
m_toggleFullscreen->SetPressed(Pi::config.Int("StartFullscreen"));
}
}

5 changes: 5 additions & 0 deletions src/GameMenuView.h
Expand Up @@ -19,10 +19,15 @@ class GameMenuView: public View {
private:
void OnChangePlanetDetail(int level);
void OnChangeCityDetail(int level);
void OnChangeVideoResolution(int res);
void OnToggleShaders(Gui::ToggleButton *b, bool state);
void OnToggleFullscreen(Gui::ToggleButton *b, bool state);
bool m_changedDetailLevel;
View *m_subview;
Gui::RadioButton *m_planetDetail[5];
Gui::RadioButton *m_cityDetail[5];
Gui::ToggleButton *m_toggleShaders;
Gui::ToggleButton *m_toggleFullscreen;
};

#endif /* _GAMEMENUVIEW_H */
2 changes: 1 addition & 1 deletion src/GeoSphere.cpp
Expand Up @@ -254,7 +254,7 @@ class GeoPatch {
if (hiEdgeIndices[i][j] < s_hiMinIdx[i]) s_hiMinIdx[i] = hiEdgeIndices[i][j];
if (hiEdgeIndices[i][j] > s_hiMaxIdx[i]) s_hiMaxIdx[i] = hiEdgeIndices[i][j];
}
printf("%d:\nLo %d:%d\nHi: %d:%d\n", i, s_loMinIdx[i], s_loMaxIdx[i], s_hiMinIdx[i], s_hiMaxIdx[i]);
//printf("%d:\nLo %d:%d\nHi: %d:%d\n", i, s_loMinIdx[i], s_loMaxIdx[i], s_hiMinIdx[i], s_hiMaxIdx[i]);
}

if (USE_VBO) {
Expand Down
52 changes: 52 additions & 0 deletions src/IniConfig.cpp
@@ -0,0 +1,52 @@
#include "libs.h"
#include "IniConfig.h"

void IniConfig::Load(const std::string &filename)
{
this->clear();
this->filename = filename;
FILE *f = fopen(filename.c_str(), "r");
if (f) {
char buf[1024];
while (fgets(buf, sizeof(buf), f)) {
if (buf[0] == '#') continue;
char *sep = strchr(buf, '=');
char *kend = sep;
if (!sep) continue;
*sep = 0;
// strip whitespace
while (isspace(*(--kend))) *kend = 0;
while (isspace(*(++sep))) *sep = 0;
// snip \r, \n
char *vend = sep;
while (*(++vend)) if ((*vend == '\r') || (*vend == '\n')) { *vend = 0; break; }
std::string key = std::string(buf);
std::string val = std::string(sep);
(*this)[key] = val;
}
fclose(f);
} else {
// set defaults
(*this)["DisableShaders"] = "0";
(*this)["DisableSound"] = "0";
(*this)["StartFullscreen"] = "1";
(*this)["ScrWidth"] = "0";
(*this)["ScrHeight"] = "0";
(*this)["DetailCities"] = "1";
(*this)["DetailPlanets"] = "1";
}
}

bool IniConfig::Save()
{
FILE *f = fopen(filename.c_str(), "w");
if (!f) {
return false;
} else {
for (std::map<std::string, std::string>::const_iterator i = begin(); i!=end(); ++i) {
fprintf(f, "%s=%s\n", (*i).first.c_str(), (*i).second.c_str());
}
fclose(f);
return true;
}
}
29 changes: 29 additions & 0 deletions src/IniConfig.h
@@ -0,0 +1,29 @@
#ifndef INICONFIG_H
#define INICONFIG_H

#include <map>
#include <string>

class IniConfig: private std::map<std::string, std::string> {
public:
void Load(const std::string &filename);
void SetInt(const char *key, int val) {
(*this)[key] = stringf(64, "%d", val);
}
int Int(const char *key) {
return atoi((*this)[key].c_str());
}
float Float(const char *key) {
float val;
if (sscanf((*this)[key].c_str(), "%f", &val)==1) return val;
else return 0;
}
std::string String(const char *key) {
return (*this)[key];
}
bool Save();
private:
std::string filename;
};

#endif /* INICONFIG_H */
4 changes: 2 additions & 2 deletions src/Makefile.am
Expand Up @@ -14,7 +14,7 @@ include_HEADERS = Body.h Frame.h GenericSystemView.h glfreetype.h GuiButton.h Gu
CityOnPlanet.h ShipCpanelMultiFuncDisplays.h Projectile.h Render.h Sound.h GuiRepeaterButton.h \
CommodityTradeWidget.h GenericChatForm.h PoliceChatForm.h SysLoc.h PersistSystemData.h GalacticView.h \
Galaxy.h GameMenuView.h GuiTextEntry.h Missile.h HyperspaceCloud.h LmrModel.h MyLuaMathTypes.h BezierCurve.h \
PiLuaModules.h LuaChatForm.h mylua.h PiLuaAPI.h
PiLuaModules.h LuaChatForm.h mylua.h PiLuaAPI.h IniConfig.h

libgui_a_SOURCES = GuiButton.cpp Gui.cpp GuiFixed.cpp GuiScreen.cpp GuiLabel.cpp GuiToolTip.cpp GuiToggleButton.cpp GuiRadioButton.cpp \
GuiRadioGroup.cpp GuiImageButton.cpp GuiImage.cpp GuiImageRadioButton.cpp GuiMultiStateImageButton.cpp GuiWidget.cpp \
Expand All @@ -30,7 +30,7 @@ pioneer_SOURCES = main.cpp glfreetype.cpp Body.cpp Space.cpp Ship.cpp Player.cpp
Mission.cpp Polit.cpp CityOnPlanet.cpp ShipCpanelMultiFuncDisplays.cpp \
Projectile.cpp Render.cpp Sound.cpp CommodityTradeWidget.cpp GenericChatForm.cpp PoliceChatForm.cpp \
SysLoc.cpp GalacticView.cpp Galaxy.cpp GameMenuView.cpp Missile.cpp HyperspaceCloud.cpp \
LmrModel.cpp MyLuaMathTypes.cpp PiLuaModules.cpp LuaChatForm.cpp PiLuaAPI.cpp
LmrModel.cpp MyLuaMathTypes.cpp PiLuaModules.cpp LuaChatForm.cpp PiLuaAPI.cpp IniConfig.cpp
pioneer_LDADD = lua/liblua.a oolua/liboolua.a collider/libcollider.a libgui.a

modelviewer_SOURCES = LuaModelViewer.cpp glfreetype.cpp LmrModel.cpp perlin.cpp Render.cpp utils.cpp MyLuaMathTypes.cpp
Expand Down

0 comments on commit 32e1e8c

Please sign in to comment.