Merge branch 'gles'
[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 "geom.h"
21 #include "part.h"
22 #include "audio.h"
23 #include "config.h"
24 #include "video.h"
25
26 #include "st_resol.h"
27
28 extern struct state  st_null;
29 static struct state *st_back;
30
31 static SDL_Rect **modes;
32
33 /*---------------------------------------------------------------------------*/
34
35 #define RESOL_BACK -1
36
37 static int resol_action(int i)
38 {
39     int r = 1;
40
41     audio_play("snd/menu.ogg", 1.0f);
42
43     switch (i)
44     {
45     case RESOL_BACK:
46         goto_state(st_back);
47         st_back = NULL;
48         break;
49
50     default:
51         goto_state(&st_null);
52         r = video_mode(config_get_d(CONFIG_FULLSCREEN),
53                        modes[i]->w, modes[i]->h);
54         goto_state(&st_resol);
55         break;
56     }
57
58     return r;
59 }
60
61 static int fill_row(int id, SDL_Rect **modes, int i, int n)
62 {
63     int complete;
64
65     if (n == 0)
66         return 1;
67
68     if (modes[i])
69     {
70         char label[20];
71
72         sprintf(label, "%d x %d", modes[i]->w, modes[i]->h);
73
74         complete = fill_row(id, modes, i + 1, n - 1);
75
76         gui_state(id, label, GUI_SML, i,
77                   config_get_d(CONFIG_WIDTH)  == modes[i]->w &&
78                   config_get_d(CONFIG_HEIGHT) == modes[i]->h);
79     }
80     else
81     {
82         for (; n; gui_space(id), n--);
83         complete = 0;
84     }
85
86     return complete;
87 }
88
89 static int resol_gui(void)
90 {
91     int id, jd;
92
93     if ((id = gui_vstack(0)))
94     {
95         if ((jd = gui_harray(id)))
96         {
97             gui_label(jd, _("Resolution"), GUI_SML, GUI_ALL, 0, 0);
98             gui_space(jd);
99             gui_start(jd, _("Back"),       GUI_SML, RESOL_BACK, 0);
100         }
101
102         gui_space(id);
103
104         if (modes)
105         {
106             int i;
107
108             for (i = 0; fill_row(gui_harray(id), modes, i, 4); i += 4);
109         }
110
111         gui_layout(id, 0, 0);
112     }
113
114     return id;
115 }
116
117 static int resol_enter(struct state *st, struct state *prev)
118 {
119     if (!st_back)
120     {
121         /* Note the parent screen if not done yet. */
122
123         st_back = prev;
124     }
125
126     back_init("back/gui.png");
127
128     modes = SDL_ListModes(NULL, SDL_OPENGL | SDL_FULLSCREEN);
129
130     if (modes == (SDL_Rect **) -1)
131         modes = NULL;
132
133     audio_music_fade_to(0.5f, "bgm/inter.ogg");
134
135     return resol_gui();
136 }
137
138 static void resol_leave(struct state *st, struct state *next, int id)
139 {
140     back_free();
141     gui_delete(id);
142 }
143
144 static void resol_paint(int id, float st)
145 {
146     video_push_persp((float) config_get_d(CONFIG_VIEW_FOV), 0.1f, FAR_DIST);
147     {
148         back_draw_easy();
149     }
150     video_pop_matrix();
151     gui_paint(id);
152 }
153
154 static void resol_timer(int id, float dt)
155 {
156     gui_timer(id, dt);
157 }
158
159 static void resol_point(int id, int x, int y, int dx, int dy)
160 {
161     gui_pulse(gui_point(id, x, y), 1.2f);
162 }
163
164 static void resol_stick(int id, int a, float v, int bump)
165 {
166     gui_pulse(gui_stick(id, a, v, bump), 1.2f);
167 }
168
169 static int resol_click(int b, int d)
170 {
171     if (b == SDL_BUTTON_LEFT && d == 1)
172         return resol_action(gui_token(gui_click()));
173     return 1;
174 }
175
176 static int resol_keybd(int c, int d)
177 {
178     return (d && c == SDLK_ESCAPE) ? resol_action(RESOL_BACK) : 1;
179 }
180
181 static int resol_buttn(int b, int d)
182 {
183     if (d)
184     {
185         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
186             return resol_action(gui_token(gui_click()));
187         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_B, b))
188             return resol_action(RESOL_BACK);
189         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
190             return resol_action(RESOL_BACK);
191     }
192     return 1;
193 }
194
195 /*---------------------------------------------------------------------------*/
196
197
198 struct state st_resol = {
199     resol_enter,
200     resol_leave,
201     resol_paint,
202     resol_timer,
203     resol_point,
204     resol_stick,
205     NULL,
206     resol_click,
207     resol_keybd,
208     resol_buttn
209 };
210