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