Fix redundant glTexEnv calls
[neverball] / ball / st_set.c
1 /*
2  * Copyright (C) 2003 Robert Kooima
3  *
4  * NEVERBALL is  free software; you can redistribute  it and/or modify
5  * it under the  terms of the GNU General  Public License as published
6  * by the Free  Software Foundation; either version 2  of the License,
7  * or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT  ANY  WARRANTY;  without   even  the  implied  warranty  of
11  * MERCHANTABILITY or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU
12  * General Public License for more details.
13  */
14
15 #include "gui.h"
16 #include "set.h"
17 #include "progress.h"
18 #include "audio.h"
19 #include "config.h"
20 #include "util.h"
21 #include "common.h"
22
23 #include "game_common.h"
24
25 #include "st_set.h"
26 #include "st_title.h"
27 #include "st_start.h"
28 #include "st_shared.h"
29
30 /*---------------------------------------------------------------------------*/
31
32 #define SET_STEP 6
33
34 static int total = 0;
35 static int first = 0;
36
37 static int shot_id;
38 static int desc_id;
39
40 static int do_init = 1;
41
42 static int set_action(int i)
43 {
44     audio_play(AUD_MENU, 1.0f);
45
46     switch (i)
47     {
48     case GUI_BACK:
49         set_quit();
50         return goto_state(&st_title);
51         break;
52
53     case GUI_PREV:
54
55         first -= SET_STEP;
56
57         do_init = 0;
58         return goto_state(&st_set);
59
60         break;
61
62     case GUI_NEXT:
63
64         first += SET_STEP;
65
66         do_init = 0;
67         return goto_state(&st_set);
68
69         break;
70
71     case GUI_NULL:
72         return 1;
73         break;
74
75     default:
76         if (set_exists(i))
77         {
78             set_goto(i);
79             return goto_state(&st_start);
80         }
81     }
82
83     return 1;
84 }
85
86 static void gui_set(int id, int i)
87 {
88     if (set_exists(i))
89         gui_state(id, set_name(i), GUI_SML, i, 0);
90     else
91         gui_label(id, "", GUI_SML, GUI_ALL, 0, 0);
92 }
93
94 static int set_gui(void)
95 {
96     int w = config_get_d(CONFIG_WIDTH);
97     int h = config_get_d(CONFIG_HEIGHT);
98
99     int id, jd, kd;
100
101     int i;
102
103     if ((id = gui_vstack(0)))
104     {
105         if ((jd = gui_hstack(id)))
106         {
107             gui_label(jd, _("Level Set"), GUI_SML, GUI_ALL, gui_yel, gui_red);
108             gui_filler(jd);
109             gui_navig(jd, first > 0, first + SET_STEP < total);
110         }
111
112         gui_space(id);
113
114         if ((jd = gui_harray(id)))
115         {
116             shot_id = gui_image(jd, set_shot(first), 7 * w / 16, 7 * h / 16);
117
118             if ((kd = gui_varray(jd)))
119             {
120                 for (i = first; i < first + SET_STEP; i++)
121                     gui_set(kd, i);
122             }
123         }
124
125         gui_space(id);
126         desc_id = gui_multi(id, " \\ \\ \\ \\ \\", GUI_SML, GUI_ALL,
127                             gui_yel, gui_wht);
128
129         gui_layout(id, 0, 0);
130     }
131
132     return id;
133 }
134
135 static int set_enter(struct state *st, struct state *prev)
136 {
137     if (do_init)
138     {
139         total = set_init();
140         first = MIN(first, (total - 1) - ((total - 1) % SET_STEP));
141
142         audio_music_fade_to(0.5f, "bgm/inter.ogg");
143         audio_play(AUD_START, 1.f);
144     }
145     else do_init = 1;
146
147     return set_gui();
148 }
149
150 static void set_over(int i)
151 {
152     gui_set_image(shot_id, set_shot(i));
153     gui_set_multi(desc_id, set_desc(i));
154 }
155
156 static void set_point(int id, int x, int y, int dx, int dy)
157 {
158     int jd = shared_point_basic(id, x, y);
159     int i  = gui_token(jd);
160     if (jd && set_exists(i))
161         set_over(i);
162 }
163
164 static void set_stick(int id, int a, float v, int bump)
165 {
166     int jd = shared_stick_basic(id, a, v, bump);
167     int i  = gui_token(jd);
168     if (jd && set_exists(i))
169         set_over(i);
170 }
171
172 static int set_buttn(int b, int d)
173 {
174     if (d)
175     {
176         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
177             return set_action(gui_token(gui_click()));
178         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
179             return set_action(GUI_BACK);
180     }
181     return 1;
182 }
183
184 /*---------------------------------------------------------------------------*/
185
186 struct state st_set = {
187     set_enter,
188     shared_leave,
189     shared_paint,
190     shared_timer,
191     set_point,
192     set_stick,
193     shared_angle,
194     shared_click,
195     NULL,
196     set_buttn
197 };