More formatting tweaks.
[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 ((jd = gui_hstack(id)))
45         {
46             ln = lg->level->repr;
47             b = lg->level->is_bonus;
48             textcol1 = b ? gui_wht : 0;
49             textcol2 = b ? gui_grn : 0;
50
51             gui_filler(jd);
52
53             if ((kd = gui_vstack(jd)))
54             {
55                 gui_label(kd, _(curr_set()->name), GUI_SML,
56                           GUI_ALL, gui_wht, gui_wht);
57                 gui_space(kd);
58
59                 if ((ld = gui_hstack(kd)))
60                 {
61                     gui_label(ld, ln,          GUI_LRG, GUI_NE,
62                               textcol1, textcol2);
63                     gui_label(ld, _("Level "), GUI_LRG, GUI_NW,
64                               textcol1, textcol2);
65                 }
66
67                 gui_label(kd, mode_to_str(lg->mode, 1), GUI_SML, GUI_BOT,
68                           gui_wht, gui_wht);
69
70             }
71             gui_filler(jd);
72         }
73         gui_space(id);
74
75         if (!level_ok)
76             gui_label(id, _("Cannot load the level file."), GUI_SML, GUI_ALL,
77                       gui_red, gui_red);
78         else if (lg->level->message[0] != '\0')
79             gui_multi(id, _(lg->level->message), GUI_SML, GUI_ALL, gui_wht,
80                       gui_wht);
81
82         gui_layout(id, 0, 0);
83     }
84
85     game_set_fly(1.f);
86
87     return id;
88 }
89
90 static void level_timer(int id, float dt)
91 {
92     game_step_fade(dt);
93     audio_timer(dt);
94 }
95
96 static int level_click(int b, int d)
97 {
98     if (b < 0 && d == 1)
99     {
100         if (level_ok)
101         {
102             return goto_state(&st_play_ready);
103         }
104         else
105         {
106             level_stop(GAME_NONE, curr_clock(), curr_coins());
107             return goto_end_level();
108         }
109     }
110     return 1;
111 }
112
113 static int level_keybd(int c, int d)
114 {
115     if (d && c == SDLK_F12)
116         return goto_state(&st_poser);
117     return 1;
118 }
119
120 static int level_buttn(int b, int d)
121 {
122     if (d)
123     {
124         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
125         {
126             if (level_ok)
127             {
128                 return goto_state(&st_play_ready);
129             }
130             else
131             {
132                 level_stop(GAME_NONE, curr_clock(), curr_coins());
133                 return goto_end_level();
134             }
135         }
136         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
137         {
138             level_stop(GAME_NONE, curr_clock(), curr_coins());
139             return goto_end_level();
140         }
141     }
142     return 1;
143 }
144
145 /*---------------------------------------------------------------------------*/
146
147 static void poser_paint(int id, float st)
148 {
149     game_draw(1, st);
150 }
151
152 static int poser_buttn(int c, int d)
153 {
154     if (d && config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, c))
155         return goto_state(&st_level);
156     
157     return 1;
158 }
159
160 /*---------------------------------------------------------------------------*/
161
162 struct state st_level = {
163     level_enter,
164     shared_leave,
165     shared_paint,
166     level_timer,
167     NULL,
168     NULL,
169     level_click,
170     level_keybd,
171     level_buttn,
172     1, 0
173 };
174
175 struct state st_poser = {
176     NULL,
177     NULL,
178     poser_paint,
179     NULL,
180     NULL,
181     NULL,
182     NULL,
183     NULL,
184     poser_buttn,
185     1, 0
186 };