Fixed the problem that in a rare case, a set is downloaded and the neverballrc is...
[neverball] / ball / st_level.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 <string.h>
16
17 #include "gui.h"
18 #include "game.h"
19 #include "set.h"
20 #include "levels.h"
21 #include "audio.h"
22 #include "config.h"
23 #include "st_shared.h"
24
25 #include "st_level.h"
26 #include "st_play.h"
27 #include "st_start.h"
28 #include "st_over.h"
29
30 /*---------------------------------------------------------------------------*/
31
32 static int level_enter(void)
33 {
34     int id, jd, kd, ld;
35     const char *ln;
36     const struct level_game *lg = curr_lg();
37     int b;
38     const float *textcol1, *textcol2;
39
40     if ((id = gui_vstack(0)))
41     {
42         if ((jd = gui_hstack(id)))
43         {
44             ln = lg->level->repr;
45             b = lg->level->is_bonus;
46             textcol1 = b ? gui_wht : 0;
47             textcol2 = b ? gui_grn : 0;
48
49             gui_filler(jd);
50
51             if ((kd = gui_vstack(jd)))
52             {
53                 gui_label(kd, set_name(curr_set()), GUI_SML, GUI_ALL,
54                           gui_wht, gui_wht);
55
56                 gui_space(kd);
57
58                 if ((ld = gui_hstack(kd)))
59                 {
60                     if (b == 0)
61                     {
62                         gui_label(ld, ln,          GUI_LRG, GUI_NE,
63                                   textcol1, textcol2);
64                         gui_label(ld, _("Level "), GUI_LRG, GUI_NW,
65                                   textcol1, textcol2);
66                     }
67                     else
68                     {
69                         gui_label(ld, ln,                GUI_MED, GUI_NE,
70                                   textcol1, textcol2);
71                         gui_label(ld, _("Bonus Level "), GUI_MED, GUI_NW,
72                                   textcol1, textcol2);
73                     }
74                 }
75
76                 gui_label(kd, mode_to_str(lg->mode, 1), GUI_SML, GUI_BOT,
77                           gui_wht, gui_wht);
78
79             }
80             gui_filler(jd);
81         }
82         gui_space(id);
83
84         if (strlen(lg->level->message) != 0)
85             gui_multi(id, _(lg->level->message), GUI_SML, GUI_ALL, gui_wht,
86                       gui_wht);
87
88         gui_layout(id, 0, 0);
89     }
90
91     game_set_fly(1.f);
92
93     return id;
94 }
95
96 static void level_timer(int id, float dt)
97 {
98     game_step_fade(dt);
99 }
100
101 static int level_click(int b, int d)
102 {
103     return (b < 0 && d == 1) ? goto_state(&st_play_ready) : 1;
104 }
105
106 static int level_keybd(int c, int d)
107 {
108     if (d && c == SDLK_F12)
109         return goto_state(&st_poser);
110     return 1;
111 }
112
113 static int level_buttn(int b, int d)
114 {
115     if (d)
116     {
117         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
118         {
119             return goto_state(&st_play_ready);
120         }
121         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
122         {
123             level_stat(GAME_NONE, curr_clock(), curr_coins());
124             level_stop();
125             return goto_state(&st_over);
126         }
127     }
128     return 1;
129 }
130
131 /*---------------------------------------------------------------------------*/
132
133 static void poser_paint(int id, float st)
134 {
135     game_draw(1, st);
136 }
137
138 static int poser_buttn(int c, int d)
139 {
140     if (d && config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, c))
141         return goto_state(&st_level);
142
143     return 1;
144 }
145
146 /*---------------------------------------------------------------------------*/
147
148 struct state st_level = {
149     level_enter,
150     shared_leave,
151     shared_paint,
152     level_timer,
153     NULL,
154     NULL,
155     NULL,
156     level_click,
157     level_keybd,
158     level_buttn,
159     1, 0
160 };
161
162 struct state st_poser = {
163     NULL,
164     NULL,
165     poser_paint,
166     NULL,
167     NULL,
168     NULL,
169     NULL,
170     NULL,
171     NULL,
172     poser_buttn,
173     1, 0
174 };