File: videooptions.cpp

package info (click to toggle)
qonk 0.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 2,076 kB
  • ctags: 2,026
  • sloc: cpp: 14,978; sh: 3,464; makefile: 200
file content (225 lines) | stat: -rw-r--r-- 4,202 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include <vector>
#include <sstream>

#include <SDL/SDL.h>

#include "guichan/guichan.hpp"

#include "ui/menuaction.h"

#include "settings.h"

#include "videooptions.h"
#include "main.h"

using namespace std;
using namespace gcn;

VideoOptions::VideoOptions()
  : toggleFullscreenAction(0), screenSizeAction(0), applyVideoOptionsAction(0)
{
  fullscreen = Settings::getFullscreen();
  screenWidth = Settings::getScreenWidth();
  screenHeight = Settings::getScreenHeight();

  SDL_Rect **modes = SDL_ListModes(NULL, SDL_FULLSCREEN
                                         | SDL_HWSURFACE
                                         | SDL_SWSURFACE);

  /* Check if there are any modes available */
  if(!modes)
  {
    // No modes available. :(
  } 
  else if(modes == (SDL_Rect **)-1){
    // This means we can chose every display size we want (will never happen).
  }
  else
  {
    for (int i=0; modes[i]; ++i)
      sizes.push_back(pair <int, int> (modes[i]->w, modes[i]->h));

    // Nicely sort the entries.
    sort (sizes.begin(), sizes.end());
  }

  // Initializes index with the proper value denoting the current
  // screen size.
  index = 0;
  int size = sizes.size();
  for (int i=0; i<size; i++)
  {
    if (sizes[i].first == screenWidth
        && sizes[i].second == screenHeight)
    {
      index = i;
      break;
    }
  }

}

void
VideoOptions::toggleFullscreen()
{
  fullscreen = !fullscreen;

  if (toggleFullscreenAction)
    toggleFullscreenAction->update();
}

class ToggleFullscreenAction : public MenuAction
{
  VideoOptions &videoOptions;
  CheckBox *check;

  public:
    ToggleFullscreenAction(VideoOptions &vo)
      : videoOptions(vo) { }

    virtual gcn::Widget *getWidget(string s)
    {
      return check = new CheckBox(s, videoOptions.getFullscreen());
    }

    void invoke()
    {
      videoOptions.toggleFullscreen();
    }

    void previous()
    {
      invoke();
    }

    void next()
    {
      invoke();
    }

    void update()
    {
      check->setMarked(videoOptions.getFullscreen());
    }
};

MenuAction *
VideoOptions::getToggleFullscreenAction()
{
  if (!toggleFullscreenAction)
    toggleFullscreenAction = new ToggleFullscreenAction(*this);

  return toggleFullscreenAction;
}

void
VideoOptions::nextScreenSize()
{
  if (++index >= sizes.size())
    index = 0;

  screenWidth = sizes[index].first;
  screenHeight = sizes[index].second;

  if (screenSizeAction)
    screenSizeAction->update();
}

void
VideoOptions::previousScreenSize()
{
  if (--index < 0)
    index = sizes.size() - 1;

  screenWidth = sizes[index].first;
  screenHeight = sizes[index].second;

  if (screenSizeAction)
    screenSizeAction->update();
}

class ScreenSizeAction : public MenuAction
{
  VideoOptions &videoOptions;
  Label *label;
  string baseCaption;

  public:
    ScreenSizeAction(VideoOptions &vo)
      : videoOptions(vo) { }

  virtual gcn::Widget *getWidget(string s)
  {
    baseCaption = s;
    stringstream ss;
    ss << baseCaption;
    ss << " ";
    ss << videoOptions.getScreenWidth();
    ss << "x";
    ss << videoOptions.getScreenHeight();

    return label = new Label(ss.str());
  }

  void invoke()
  {
    next();
  }

  void next()
  {
    videoOptions.nextScreenSize();
  }

  void previous()
  {
    videoOptions.previousScreenSize();
  }

  void update()
  {
    stringstream ss;
    ss << baseCaption;
    ss << " ";
    ss << videoOptions.getScreenWidth();
    ss << "x";
    ss << videoOptions.getScreenHeight();

    label->setCaption(ss.str());
    label->adjustSize();
  }

};

MenuAction *
VideoOptions::getScreenSizeAction()
{
  if (!screenSizeAction)
    screenSizeAction = new ScreenSizeAction(*this);

  return screenSizeAction;
}

class ApplyVideoOptionsAction : public MenuAction
{
  Main &main;
  VideoOptions &videoOptions;

  public:
    ApplyVideoOptionsAction(Main &m, VideoOptions &vo)
      : main(m), videoOptions(vo) { }

    void invoke()
    {
      main.applyVideoOptions(videoOptions);
    }
};

MenuAction *
VideoOptions::getApplyVideoOptionsAction(Main &main)
{
  if (!applyVideoOptionsAction)
    applyVideoOptionsAction = new ApplyVideoOptionsAction(main, *this);
    
  return applyVideoOptionsAction;
}