| 1 |
#include <vector> |
| 2 |
#include <sstream> |
| •3 |
#include <algorithm> |
| 4 |
|
| 5 |
#include <SDL/SDL.h> |
| 6 |
|
| •7 |
#include "guichan.hpp" |
| 8 |
|
| 9 |
#include "ui/menuaction.h" |
| 10 |
|
| 11 |
#include "settings.h" |
| 12 |
|
| 13 |
#include "videooptions.h" |
| 14 |
#include "main.h" |
| 15 |
|
| 16 |
using namespace std; |
| 17 |
using namespace gcn; |
| 18 |
|
| 19 |
VideoOptions::VideoOptions() |
| 20 |
: toggleFullscreenAction(0), screenSizeAction(0), applyVideoOptionsAction(0) |
| 21 |
{ |
| 22 |
fullscreen = Settings::getFullscreen(); |
| 23 |
screenWidth = Settings::getScreenWidth(); |
| 24 |
screenHeight = Settings::getScreenHeight(); |
| 25 |
|
| 26 |
SDL_Rect **modes = SDL_ListModes(NULL, SDL_FULLSCREEN |
| 27 |
| SDL_HWSURFACE |
| 28 |
| SDL_SWSURFACE); |
| 29 |
|
| 30 |
/* Check if there are any modes available */ |
| 31 |
if(!modes) |
| 32 |
{ |
| 33 |
// No modes available. :( |
| 34 |
} |
| 35 |
else if(modes == (SDL_Rect **)-1){ |
| 36 |
// This means we can chose every display size we want (will never happen). |
| 37 |
} |
| 38 |
else |
| 39 |
{ |
| 40 |
for (int i=0; modes[i]; ++i) |
| 41 |
sizes.push_back(pair <int, int> (modes[i]->w, modes[i]->h)); |
| 42 |
|
| 43 |
// Nicely sort the entries. |
| 44 |
sort (sizes.begin(), sizes.end()); |
| 45 |
} |
| 46 |
|
| 47 |
// Initializes index with the proper value denoting the current |
| 48 |
// screen size. |
| 49 |
index = 0; |
| 50 |
int size = sizes.size(); |
| 51 |
for (int i=0; i<size; i++) |
| 52 |
{ |
| 53 |
if (sizes[i].first == screenWidth |
| 54 |
&& sizes[i].second == screenHeight) |
| 55 |
{ |
| 56 |
index = i; |
| 57 |
break; |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
} |
| 62 |
|
| 63 |
void |
| 64 |
VideoOptions::toggleFullscreen() |
| 65 |
{ |
| 66 |
fullscreen = !fullscreen; |
| 67 |
|
| 68 |
if (toggleFullscreenAction) |
| 69 |
toggleFullscreenAction->update(); |
| 70 |
} |
| 71 |
|
| 72 |
class ToggleFullscreenAction : public MenuAction |
| 73 |
{ |
| 74 |
VideoOptions &videoOptions; |
| 75 |
CheckBox *check; |
| 76 |
|
| 77 |
public: |
| 78 |
ToggleFullscreenAction(VideoOptions &vo) |
| 79 |
: videoOptions(vo) { } |
| 80 |
|
| 81 |
virtual gcn::Widget *getWidget(string s) |
| 82 |
{ |
| 83 |
return check = new CheckBox(s, videoOptions.getFullscreen()); |
| 84 |
} |
| 85 |
|
| 86 |
void invoke() |
| 87 |
{ |
| 88 |
videoOptions.toggleFullscreen(); |
| 89 |
} |
| 90 |
|
| 91 |
void previous() |
| 92 |
{ |
| 93 |
invoke(); |
| 94 |
} |
| 95 |
|
| 96 |
void next() |
| 97 |
{ |
| 98 |
invoke(); |
| 99 |
} |
| 100 |
|
| 101 |
void update() |
| 102 |
{ |
| 103 |
check->setMarked(videoOptions.getFullscreen()); |
| 104 |
} |
| 105 |
}; |
| 106 |
|
| 107 |
MenuAction * |
| 108 |
VideoOptions::getToggleFullscreenAction() |
| 109 |
{ |
| 110 |
if (!toggleFullscreenAction) |
| 111 |
toggleFullscreenAction = new ToggleFullscreenAction(*this); |
| 112 |
|
| 113 |
return toggleFullscreenAction; |
| 114 |
} |
| 115 |
|
| 116 |
void |
| 117 |
VideoOptions::nextScreenSize() |
| 118 |
{ |
| 119 |
if (++index >= sizes.size()) |
| 120 |
index = 0; |
| 121 |
|
| 122 |
screenWidth = sizes[index].first; |
| 123 |
screenHeight = sizes[index].second; |
| 124 |
|
| 125 |
if (screenSizeAction) |
| 126 |
screenSizeAction->update(); |
| 127 |
} |
| 128 |
|
| 129 |
void |
| 130 |
VideoOptions::previousScreenSize() |
| 131 |
{ |
| 132 |
if (--index < 0) |
| 133 |
index = sizes.size() - 1; |
| 134 |
|
| 135 |
screenWidth = sizes[index].first; |
| 136 |
screenHeight = sizes[index].second; |
| 137 |
|
| 138 |
if (screenSizeAction) |
| 139 |
screenSizeAction->update(); |
| 140 |
} |
| 141 |
|
| 142 |
class ScreenSizeAction : public MenuAction |
| 143 |
{ |
| 144 |
VideoOptions &videoOptions; |
| 145 |
Label *label; |
| 146 |
string baseCaption; |
| 147 |
|
| 148 |
public: |
| 149 |
ScreenSizeAction(VideoOptions &vo) |
| 150 |
: videoOptions(vo) { } |
| 151 |
|
| 152 |
virtual gcn::Widget *getWidget(string s) |
| 153 |
{ |
| 154 |
baseCaption = s; |
| 155 |
stringstream ss; |
| 156 |
ss << baseCaption; |
| 157 |
ss << " "; |
| 158 |
ss << videoOptions.getScreenWidth(); |
| 159 |
ss << "x"; |
| 160 |
ss << videoOptions.getScreenHeight(); |
| 161 |
|
| 162 |
return label = new Label(ss.str()); |
| 163 |
} |
| 164 |
|
| 165 |
void invoke() |
| 166 |
{ |
| 167 |
next(); |
| 168 |
} |
| 169 |
|
| 170 |
void next() |
| 171 |
{ |
| 172 |
videoOptions.nextScreenSize(); |
| 173 |
} |
| 174 |
|
| 175 |
void previous() |
| 176 |
{ |
| 177 |
videoOptions.previousScreenSize(); |
| 178 |
} |
| 179 |
|
| 180 |
void update() |
| 181 |
{ |
| 182 |
stringstream ss; |
| 183 |
ss << baseCaption; |
| 184 |
ss << " "; |
| 185 |
ss << videoOptions.getScreenWidth(); |
| 186 |
ss << "x"; |
| 187 |
ss << videoOptions.getScreenHeight(); |
| 188 |
|
| 189 |
label->setCaption(ss.str()); |
| 190 |
label->adjustSize(); |
| 191 |
} |
| 192 |
|
| 193 |
}; |
| 194 |
|
| 195 |
MenuAction * |
| 196 |
VideoOptions::getScreenSizeAction() |
| 197 |
{ |
| 198 |
if (!screenSizeAction) |
| 199 |
screenSizeAction = new ScreenSizeAction(*this); |
| 200 |
|
| 201 |
return screenSizeAction; |
| 202 |
} |
| 203 |
|
| 204 |
class ApplyVideoOptionsAction : public MenuAction |
| 205 |
{ |
| 206 |
Main &main; |
| 207 |
VideoOptions &videoOptions; |
| 208 |
|
| 209 |
public: |
| 210 |
ApplyVideoOptionsAction(Main &m, VideoOptions &vo) |
| 211 |
: main(m), videoOptions(vo) { } |
| 212 |
|
| 213 |
void invoke() |
| 214 |
{ |
| 215 |
main.applyVideoOptions(videoOptions); |
| 216 |
} |
| 217 |
}; |
| 218 |
|
| 219 |
MenuAction * |
| 220 |
VideoOptions::getApplyVideoOptionsAction(Main &main) |
| 221 |
{ |
| 222 |
if (!applyVideoOptionsAction) |
| 223 |
applyVideoOptionsAction = new ApplyVideoOptionsAction(main, *this); |
| 224 |
|
| 225 |
return applyVideoOptionsAction; |
| 226 |
} |