updated URL of forum and table in readme file
[neverball] / share / st_resol.c
1 /*
2  * Copyright (C) 2003 Robert Kooima - 2006 Jean Privat
3  * Part of the Neverball Project http://icculus.org/neverball/
4  *
5  * NEVERBALL is  free software; you can redistribute  it and/or modify
6  * it under the  terms of the GNU General  Public License as published
7  * by the Free  Software Foundation; either version 2  of the License,
8  * or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT  ANY  WARRANTY;  without   even  the  implied  warranty  of
12  * MERCHANTABILITY or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU
13  * General Public License for more details.
14  */
15
16
17 #include <string.h>
18
19 #include "gui.h"
20 #include "back.h"
21 #include "geom.h"
22 #include "part.h"
23 #include "audio.h"
24 #include "config.h"
25
26 #include "st_resol.h"
27
28 extern struct state st_conf;
29 extern struct state st_null;
30
31 static SDL_Rect **resolutions;
32
33 /*---------------------------------------------------------------------------*/
34
35 #define LANG_BACK 100
36
37 static int resol_action(int i)
38 {
39     int f = config_get_d(CONFIG_FULLSCREEN);
40     int r = 1;
41
42     switch (i)
43     {
44     case LANG_BACK:
45         goto_state(&st_conf);
46         break;
47
48     default:
49         goto_state(&st_null);
50         r = config_mode(f, resolutions[i - 1]->w, resolutions[i - 1]->h);
51         goto_state(&st_conf);
52         break;
53     }
54
55     return r;
56 }
57
58 static int resol_enter(void)
59 {
60     int id, jd, kd;
61     int i;
62     int w, h;
63     int wp, hp;
64     int c;
65
66     back_init("back/gui.png", config_get_d(CONFIG_GEOMETRY));
67
68     /* Get the current resolution. */
69     w = config_get_d(CONFIG_WIDTH);
70     h = config_get_d(CONFIG_HEIGHT);
71
72     /* Get the resolution list. */
73     resolutions = SDL_ListModes(NULL, SDL_OPENGL | SDL_FULLSCREEN);
74
75     if ((int)resolutions == -1)
76     {
77         resolutions = NULL;
78         printf("Any resolution\n");
79     }
80     else if (resolutions == NULL)
81     {
82         printf("No resolution\n");
83     }
84
85     if ((id = gui_harray(0)))
86     {
87         if ((jd = gui_varray(id)))
88         {
89             if ((kd = gui_harray(jd)))
90             {
91                 gui_label(kd, _("Resolution"), GUI_SML, GUI_ALL, 0, 0);
92                 gui_filler(kd);
93                 gui_start(kd, _("Back"), GUI_SML, LANG_BACK, 0);
94             }
95
96             if (resolutions != NULL)
97             {
98                 hp = wp = -1;
99                 c = 0;
100                 for(i = 0; resolutions[i]; i++)
101                 {
102                     if (wp != resolutions[i]->w || hp != resolutions[i]->h)
103                     {
104                         static char st[100];
105                         wp = resolutions[i]->w;
106                         hp = resolutions[i]->h;
107                         sprintf(st, "%d x %d", wp, hp);
108
109                         if (c % 4 == 0)
110                                 kd = gui_harray(jd);
111
112                         gui_state(kd, st, GUI_SML, i + 1,
113                                   (w == wp) && (h == hp));
114                         c++;
115                     }
116                 }
117
118                 for(; c % 4 != 0; c++)
119                         gui_filler(kd);
120             }
121         }
122         gui_layout(id, 0, 0);
123     }
124
125     audio_music_fade_to(0.5f, "bgm/inter.ogg");
126
127     return id;
128 }
129
130 static void resol_leave(int id)
131 {
132     back_free();
133     gui_delete(id);
134 }
135
136 static void resol_paint(int id, float st)
137 {
138     config_push_persp((float) config_get_d(CONFIG_VIEW_FOV), 0.1f, FAR_DIST);
139     {
140         back_draw(0);
141     }
142     config_pop_matrix();
143     gui_paint(id);
144 }
145
146 static void resol_timer(int id, float dt)
147 {
148     gui_timer(id, dt);
149     audio_timer(dt);
150 }
151
152 static void resol_point(int id, int x, int y, int dx, int dy)
153 {
154     gui_pulse(gui_point(id, x, y), 1.2f);
155 }
156
157 static void resol_stick(int id, int a, int v)
158 {
159     if (config_tst_d(CONFIG_JOYSTICK_AXIS_X, a))
160         gui_pulse(gui_stick(id, v, 0), 1.2f);
161     if (config_tst_d(CONFIG_JOYSTICK_AXIS_Y, a))
162         gui_pulse(gui_stick(id, 0, v), 1.2f);
163 }
164
165 static int resol_click(int b, int d)
166 {
167     if (b < 0 && d == 1)
168         return resol_action(gui_token(gui_click()));
169     return 1;
170 }
171
172 static int resol_keybd(int c, int d)
173 {
174     return (d && c == SDLK_ESCAPE) ? goto_state(&st_conf) : 1;
175 }
176
177 static int resol_buttn(int b, int d)
178 {
179     if (d)
180     {
181         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
182             return resol_action(gui_token(gui_click()));
183         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_B, b))
184             return goto_state(&st_conf);
185         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
186             return goto_state(&st_conf);
187     }
188     return 1;
189 }
190
191 /*---------------------------------------------------------------------------*/
192
193
194 struct state st_resol = {
195     resol_enter,
196     resol_leave,
197     resol_paint,
198     resol_timer,
199     resol_point,
200     resol_stick,
201     resol_click,
202     resol_keybd,
203     resol_buttn,
204     1, 0
205 };
206