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