1 /***************************************************************************
2                           main.c  -  description
3                              -------------------
4     begin                : Don Sep  6 12:02:57 CEST 2001
5     copyright            : (C) 2001 by Michael Speck
6     email                : kulkanie@gmx.net
7  ***************************************************************************/
8 
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17 
18 #ifdef _WIN32
19 #include <fcntl.h>
20 #endif
21 
22 #ifdef __unix__
23 #include <unistd.h>
24 #endif
25 
26 #include "lbreakout.h"
27 #include "../game/game.h"
28 #include "file.h"
29 #include "chart.h"
30 #include "config.h"
31 #include "shrapnells.h"
32 #include "event.h"
33 #include "game.h"
34 #include "manager.h"
35 #include "editor.h"
36 #include "help.h"
37 #include "theme.h"
38 #include "client.h"
39 #include "../gui/gui.h"
40 #include "setselect.h"
41 
42 #define CHART_FILE_NAME "lbreakout2.hscr"
43 
44 extern int stk_quit_request;
45 extern SDL_Surface *stk_display;
46 extern Config config;
47 extern StkFont *font;
48 extern char **theme_names;
49 extern int theme_count;
50 extern SDL_Surface *mbkgnd, *cr_bkgnd;
51 extern char *edit_set;
52 extern char **levelset_names_local; /* non-network levelsets */
53 extern int gameSeed;
54 
55 /* used to compile net messages */
56 char msgbuf[MAX_MSG_SIZE];
57 int msglen = 0;
58 
59 FILE *hi_dir_chart_file = NULL;
60 
main(int argc,char * argv[])61 int main(int argc, char *argv[])
62 {
63     int result = ACTION_NONE;
64     int leave = 0;
65     char *editor_file = 0;
66     char path[512];
67     const char *set_name = NULL;
68     SDL_Surface *loading;
69 #ifdef __unix__
70     gid_t realgid;
71 
72     hi_dir_chart_file = fopen(HI_DIR "/" CHART_FILE_NAME, "r+");
73 
74     /* This is where we drop our setuid/setgid privileges.
75      */
76     realgid = getgid();
77     if (setresgid(-1, realgid, realgid) != 0) {
78         perror("Could not drop setgid privileges.  Aborting.");
79         exit(1);
80     }
81 #endif
82 
83 /* i18n */
84 #ifdef ENABLE_NLS
85     setlocale (LC_ALL, "");
86     bindtextdomain (PACKAGE, LOCALEDIR);
87     textdomain (PACKAGE);
88 #endif
89 
90 #ifdef _WIN32
91     /* Get Windows to open files in binary mode instead of default text mode */
92     _fmode = _O_BINARY;
93 #endif
94 
95     /* lbreakout info */
96     printf( "LBreakout2 %s\nCopyright 2001-2011 Michael Speck\nPublished under GNU GPL\n---\n", VERSION );
97     printf( "Looking up data in: %s\n", SRC_DIR );
98     printf( "Looking up highscores in: %s\n", HI_DIR );
99     printf( "Looking up custom levels in: %s/%s/lbreakout2-levels\n", (getenv( "HOME" )?getenv( "HOME" ):"."), CONFIG_DIR_NAME );
100 #ifndef AUDIO_ENABLED
101     printf( "Compiled without sound and music\n" );
102 #endif
103 
104     set_random_seed(); /* set random seed */
105 
106     config_load();
107 
108     stk_init( SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER | SDL_INIT_JOYSTICK );
109     if ( config.fullscreen )
110         stk_display_open( SDL_SWSURFACE | SDL_FULLSCREEN, 640, 480, 16 );
111     else
112         stk_display_open( SDL_SWSURFACE, 640, 480, 16 );
113     stk_audio_open( 0,0,0,config.audio_buffer_size );
114     SDL_WM_SetCaption( "LBreakout2", 0 );
115     SDL_SetEventFilter( event_filter );
116     stk_audio_enable_sound( config.sound );
117     stk_audio_set_sound_volume( config.volume * 16 );
118 
119     /* load a little loading pic */
120     stk_surface_set_path( SRC_DIR "/gfx" );
121     loading = stk_surface_load( SDL_SWSURFACE, "loading.png" );
122     stk_surface_blit( loading, 0,0,-1,-1, stk_display,
123                       (stk_display->w-loading->w)/2,
124                       (stk_display->h-loading->h)/2 );
125     stk_display_update( STK_UPDATE_ALL );
126 
127     /* load the GUI graphics from SRC_DIR/gui_theme */
128     stk_surface_set_path( SRC_DIR );
129     stk_audio_set_path( SRC_DIR );
130     gui_init( "gui_theme" );
131 
132     stk_surface_set_path( SRC_DIR "/gfx" );
133     stk_audio_set_path( SRC_DIR "/sounds" );
134 
135     /* load resources */
136     /* for simplicity all functions are kept but anything
137      * that is now themeable is loaded in
138      * theme_load instead of the original function
139      * (deleting resources works analouge)
140      */
141     theme_get_list();
142     if ( config.theme_count != theme_count ) {
143         if ( config.theme_id >= theme_count )
144             config.theme_id = 0;
145         config.theme_count = theme_count;
146     }
147     theme_load( theme_names[config.theme_id] );
148     /* old functions still with initialzations of
149      * lists or variables
150      */
151     client_game_create();
152     hint_load_res();
153     chart_load();
154     manager_create();
155     client_create();
156     exp_load();
157     editor_create();
158     help_create();
159     setselect_create();
160     /* run game */
161     manager_fade( STK_FADE_IN );
162     while( !leave && !stk_quit_request ) {
163         result = manager_run();
164         switch( result ) {
165             case ACTION_QUIT: leave = 1; break;
166             case ACTION_RESUME_0:
167                 manager_fade( STK_FADE_OUT );
168 		if ( client_game_resume_local( 0 ) )
169 			client_game_run();
170                 client_game_finalize();
171                 manager_fade( STK_FADE_IN );
172                 break;
173             case ACTION_PLAY_LBR:
174                 manager_fade( STK_FADE_OUT );
175                 gameSeed = rand(); /* set random seed for next FREAKOUT/BonusLevels */
176 		if ( client_game_init_local( "LBreakout2" ) )
177 			client_game_run();
178                 client_game_finalize();
179                 manager_fade( STK_FADE_IN );
180                 break;
181             case ACTION_PLAY_CUSTOM:
182                 /* run select dialog first */
183                 if ((set_name = setselect_run()) == NULL) {
184 			manager_show();
185 			break;
186 		}
187 
188                 manager_fade( STK_FADE_OUT );
189                 gameSeed = rand(); /* set random seed for next FREAKOUT/BonusLevels */
190                 if (gameSeed==0) gameSeed=1; /* not allowed because.... A HACK!!! 0 means to have
191                                                 no bonus levels to save a parameter */
192 		if ( client_game_init_local( set_name ) )
193 			client_game_run();
194                 client_game_finalize();
195                 manager_fade( STK_FADE_IN );
196                 break;
197             case ACTION_EDIT:
198                 /* new set? */
199                 if ( strequal( NEW_SET, edit_set ) ) {
200                     editor_file = calloc( 16, sizeof( char ) );
201                     snprintf( path, sizeof(path)-1, "%s/%s/lbreakout2-levels", getenv( "HOME" )? getenv("HOME"):".", CONFIG_DIR_NAME );
202                     if ( !enter_string( font, _("Set Name:"), editor_file, 12 ) || !file_check( path, editor_file, "w" ) ) {
203                         free( editor_file );
204                         break;
205                     }
206                     else
207                         manager_update_set_list();
208                 }
209                 else
210                     editor_file = strdup( edit_set );
211                 if ( editor_init( editor_file ) ) {
212                     manager_fade( STK_FADE_OUT );
213                     editor_run();
214                     editor_clear();
215                     manager_fade( STK_FADE_IN );
216                 }
217                 free( editor_file ); editor_file = 0;
218                 break;
219 			case ACTION_QUICK_HELP:
220 				help_run();
221 				break;
222             case ACTION_CLIENT:
223                 manager_fade( STK_FADE_OUT );
224                 client_run();
225                 manager_fade( STK_FADE_IN );
226                 break;
227             default: break;
228         }
229     }
230     manager_fade( STK_FADE_OUT );
231     /* delete stuff */
232     setselect_delete();
233     help_delete();
234     manager_delete();
235     chart_save();
236     chart_delete();
237     editor_delete();
238     exp_delete();
239     client_game_delete();
240     hint_delete_res();
241     theme_delete();
242     theme_delete_list();
243     stk_surface_free( &loading );
244 
245     config_save();
246 
247     if (hi_dir_chart_file)
248         fclose(hi_dir_chart_file);
249 
250     return EXIT_SUCCESS;
251 }
252