Fix high-score saving.
[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 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                         if (c % 4 == 0)
109                                 kd = gui_harray(jd);
110                         gui_state(kd, st,   GUI_SML, i+1, (w==wp) && (h==hp));
111                         c++;
112                     }
113                 }
114                 
115                 for(; c%4!=0; c++)
116                         gui_filler(kd);
117             }
118         }
119         gui_layout(id, 0, 0);
120     }
121
122     audio_music_fade_to(0.5f, "bgm/inter.ogg");
123
124     return id;
125 }
126
127 static void resol_leave(int id)
128 {
129     back_free();
130     gui_delete(id);
131 }
132
133 static void resol_paint(int id, float st)
134 {
135     config_push_persp((float) config_get_d(CONFIG_VIEW_FOV), 0.1f, FAR_DIST);
136     {
137         back_draw(0);
138     }
139     config_pop_matrix();
140     gui_paint(id);
141 }
142
143 static void resol_timer(int id, float dt)
144 {
145     gui_timer(id, dt);
146     audio_timer(dt);
147 }
148
149 static void resol_point(int id, int x, int y, int dx, int dy)
150 {
151     gui_pulse(gui_point(id, x, y), 1.2f);
152 }
153
154 static void resol_stick(int id, int a, int v)
155 {
156     if (config_tst_d(CONFIG_JOYSTICK_AXIS_X, a))
157         gui_pulse(gui_stick(id, v, 0), 1.2f);
158     if (config_tst_d(CONFIG_JOYSTICK_AXIS_Y, a))
159         gui_pulse(gui_stick(id, 0, v), 1.2f);
160 }
161
162 static int resol_click(int b, int d)
163 {
164     if (b < 0 && d == 1)
165         return resol_action(gui_token(gui_click()));
166     return 1;
167 }
168
169 static int resol_keybd(int c, int d)
170 {
171     return (d && c == SDLK_ESCAPE) ? goto_state(&st_conf) : 1;
172 }
173
174 static int resol_buttn(int b, int d)
175 {
176     if (d)
177     {
178         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
179             return resol_action(gui_token(gui_click()));
180         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_B, b))
181             return goto_state(&st_conf);
182         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
183             return goto_state(&st_conf);
184     }
185     return 1;
186 }
187
188 /*---------------------------------------------------------------------------*/
189
190
191 struct state st_resol = {
192     resol_enter,
193     resol_leave,
194     resol_paint,
195     resol_timer,
196     resol_point,
197     resol_stick,
198     resol_click,
199     resol_keybd,
200     resol_buttn,
201     1, 0
202 };
203