create a new level structure, that makes the code more maintenable. Move level.*...
[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
36     /* Load the level */
37     level_ok = level_play_go();
38
39     if ((id = gui_vstack(0)))
40     {
41         if (level_mode() == MODE_SINGLE)
42         {
43             gui_label(id, _("Single Level"),  GUI_LRG, GUI_TOP, 0, 0);
44         }
45         else if ((jd = gui_hstack(id)))
46         {
47             gui_filler(jd);
48             if ((kd = gui_vstack(jd)))
49             {
50                 if (level_extra_bonus(curr_level()))
51                     gui_label(kd, _("*** BONUS ***"),  GUI_MED, GUI_TOP, gui_wht, gui_grn);
52                 if ((ld = gui_hstack(kd)))
53                 {
54                     ln = _(level_number_name(curr_level()));
55                     if (level_extra_bonus(curr_level()))
56                     {
57                         gui_label(ld, ln,          GUI_LRG, 0, gui_wht, gui_grn);
58                         gui_label(ld, _("Level "), GUI_LRG, 0, gui_wht, gui_grn);
59                     }
60                     else
61                     {
62                         gui_label(ld, ln,          GUI_LRG, GUI_NE, 0, 0);
63                         gui_label(ld, _("Level "), GUI_LRG, GUI_NW, 0, 0);
64                     }
65                 }
66
67                 gui_label(kd, _(set_name(set_curr())),  GUI_SML, GUI_BOT, gui_wht, gui_wht);
68             }
69             gui_filler(jd);
70         }
71         gui_space(id);
72         
73         if (level_ok)
74             gui_multi(id, _(curr_intro()), GUI_SML, GUI_ALL, gui_wht, gui_wht);
75         else
76             gui_label(id, _("Cannot load the level file."), GUI_SML, GUI_ALL, gui_red, gui_red);
77
78         gui_layout(id, 0, 0);
79     }
80
81     game_set_fly(1.f);
82
83     return id;
84 }
85
86 static void level_timer(int id, float dt)
87 {
88     game_step_fade(dt);
89     audio_timer(dt);
90 }
91
92 static int level_click(int b, int d)
93 {
94     if (b < 0 && d == 1)
95     {
96         if (level_ok)
97             return goto_state(&st_play_ready);
98         else
99             return goto_end_level();
100     }
101     else
102        return 1;
103 }
104
105 static int level_keybd(int c, int d)
106 {
107     if (d && c == SDLK_F12)
108         return goto_state(&st_poser);
109     return 1;
110 }
111
112 static int level_buttn(int b, int d)
113 {
114     if (d)
115     {
116         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
117         {
118             if (level_ok)
119                 return goto_state(&st_play_ready);
120             else
121                 return goto_end_level();
122         }
123         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
124             return goto_end_level();
125     }
126     return 1;
127 }
128
129 /*---------------------------------------------------------------------------*/
130
131 static void poser_paint(int id, float st)
132 {
133     game_draw(1, st);
134 }
135
136 static int poser_buttn(int c, int d)
137 {
138     return (d && config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, c)) ? goto_state(&st_level) : 1;
139 }
140
141 /*---------------------------------------------------------------------------*/
142
143 struct state st_level = {
144     level_enter,
145     shared_leave,
146     shared_paint,
147     level_timer,
148     NULL,
149     NULL,
150     level_click,
151     level_keybd,
152     level_buttn,
153     1, 0
154 };
155
156 struct state st_poser = {
157     NULL,
158     NULL,
159     poser_paint,
160     NULL,
161     NULL,
162     NULL,
163     NULL,
164     NULL,
165     poser_buttn,
166     1, 0
167 };