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