Prettying up code, Part Two. I think Ball's done. Hope I didn't break things.
[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
38     /* Load the level */
39     level_ok = level_play_go();
40
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
53             gui_filler(jd);
54             if ((kd = gui_vstack(jd)))
55             {
56                 if (b)
57                     gui_label(kd, _("*** BONUS ***"), GUI_MED, GUI_TOP, gui_wht,
58                               gui_grn);
59                 if ((ld = gui_hstack(kd)))
60                 {
61                     if (b)
62                     {
63                         gui_label(ld, ln, GUI_LRG, 0, gui_wht, gui_grn);
64                         gui_label(ld, _("Level "), GUI_LRG, 0, gui_wht,
65                                   gui_grn);
66                     }
67                     else
68                     {
69                         gui_label(ld, ln, GUI_LRG, GUI_NE, 0, 0);
70                         gui_label(ld, _("Level "), GUI_LRG, GUI_NW, 0, 0);
71                     }
72                 }
73
74                 gui_label(kd, _(curr_set()->name), GUI_SML, GUI_BOT, gui_wht,
75                           gui_wht);
76             }
77             gui_filler(jd);
78         }
79         gui_space(id);
80
81         if (!level_ok)
82             gui_label(id, _("Cannot load the level file."), GUI_SML, GUI_ALL,
83                       gui_red, gui_red);
84         else if (lg->level->message[0] != '\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     audio_timer(dt);
100 }
101
102 static int level_click(int b, int d)
103 {
104     if (b < 0 && d == 1)
105     {
106         if (level_ok)
107             return goto_state(&st_play_ready);
108         else
109             return goto_end_level();
110     }
111     else
112        return 1;
113 }
114
115 static int level_keybd(int c, int d)
116 {
117     if (d && c == SDLK_F12)
118         return goto_state(&st_poser);
119     return 1;
120 }
121
122 static int level_buttn(int b, int d)
123 {
124     if (d)
125     {
126         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
127         {
128             if (level_ok)
129                 return goto_state(&st_play_ready);
130             else
131                 return goto_end_level();
132         }
133         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
134             return goto_end_level();
135     }
136     return 1;
137 }
138
139 /*---------------------------------------------------------------------------*/
140
141 static void poser_paint(int id, float st)
142 {
143     game_draw(1, st);
144 }
145
146 static int poser_buttn(int c, int d)
147 {
148     return (d && config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, c)) ? goto_state(&st_level) : 1;
149 }
150
151 /*---------------------------------------------------------------------------*/
152
153 struct state st_level = {
154     level_enter,
155     shared_leave,
156     shared_paint,
157     level_timer,
158     NULL,
159     NULL,
160     level_click,
161     level_keybd,
162     level_buttn,
163     1, 0
164 };
165
166 struct state st_poser = {
167     NULL,
168     NULL,
169     poser_paint,
170     NULL,
171     NULL,
172     NULL,
173     NULL,
174     NULL,
175     poser_buttn,
176     1, 0
177 };