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