~ubuntu-branches/ubuntu/jaunty/bygfoot/jaunty

« back to all changes in this revision

Viewing changes to src/main.c

  • Committer: Bazaar Package Importer
  • Author(s): Isaac Clerencia
  • Date: 2005-07-05 23:53:40 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 sarge)
  • Revision ID: james.westby@ubuntu.com-20050705235340-akpef5bdm7gsm9m4
Tags: 1.9.0-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
 * Glade will not overwrite this file.
4
4
 */
5
5
 
6
 
#ifdef HAVE_CONFIG_H
7
 
#  include <config.h>
8
 
#endif
9
 
 
10
 
#include <gtk/gtk.h>
11
 
#include <stdlib.h>
12
6
#include <time.h>
13
7
 
14
 
#include "support.h"
15
 
#include "defs.h"
 
8
#include "file.h"
 
9
#include "free.h"
 
10
#include "language.h"
 
11
#include "live_game.h"
16
12
#include "main.h"
17
 
 
18
 
/* set some variables that are used but not saved
19
 
   in the game */
20
 
void set_variables(void)
21
 
{
22
 
    gint i, j;
23
 
    gchar buf[BUF_SIZE_SMALL];
24
 
 
25
 
    for(i=0;i<2;i++)
26
 
    {
27
 
        selected_rows[i] = -1;
28
 
        undo_info[i] = 0;
29
 
        for(j=0;j<2;j++)
30
 
            bookmaker_tips[i][j] = -1;
31
 
    }
32
 
 
33
 
    /* debug mode or no? */
34
 
    debug = FALSE;
35
 
 
36
 
    status = save_status = notify_status = popups_active = 0;
37
 
    /* reset main window pointer */
38
 
    main_window = NULL;
39
 
    /* reset default save file name */
40
 
    sprintf(buf, "%s/.bygfoot/saves/", getenv("HOME"));
41
 
    save_file = g_string_new(buf);
42
 
    /* reset history chained list */
43
 
    history = NULL;
44
 
    /* reset font and country file names */
45
 
    strcpy(font_name, "0");
46
 
    strcpy(country_file_name, "0");
47
 
 
48
 
    /* this plays a role in case the human player has
49
 
       specified a country file on the command line */
50
 
    strcpy(teams[0].name, "");    
51
 
}
52
 
 
53
 
gint bygfoot_init(gint argc, gchar *argv[])
 
13
#include "misc_callbacks.h"
 
14
#include "name_struct.h"
 
15
#include "option.h"
 
16
#include "stat_struct.h"
 
17
#include "transfer_struct.h"
 
18
#include "variables.h"
 
19
#include "window.h"
 
20
 
 
21
/**
 
22
   Initialize some global variables. Most of them get nullified.
 
23
*/
 
24
void
 
25
main_init_variables(void)
54
26
{
55
27
    gint i;
56
 
    gchar buf[BUF_SIZE_SMALL];
57
 
    gchar *buf2;
 
28
 
 
29
    ligs = cps = NULL;
 
30
    acps = NULL;
 
31
    country.name = country.symbol = country.sid = NULL;
 
32
 
 
33
    season = week = week_round = 1;
 
34
 
 
35
    for(i=0;i<COUNT_END;i++)
 
36
        counters[i] = 0;
 
37
 
 
38
    counters[COUNT_LEAGUE_ID] = ID_LEAGUE_START;
 
39
    counters[COUNT_CUP_ID] = ID_CUP_START;
 
40
 
 
41
    window.main = window.startup =
 
42
        window.live = window.warning = window.progress = window.digits =
 
43
        window.stadium = window.job_offer = window.yesno = 
 
44
        window.options = window.font_sel =
 
45
        window.file_chooser = window.contract = 
 
46
        window.menu_player = window.user_management = NULL;
 
47
    
 
48
    live_game_reset(&live_game_temp, NULL, FALSE);
 
49
 
 
50
    users = g_array_new(FALSE, FALSE, sizeof(User));
 
51
    transfer_list = g_array_new(FALSE, FALSE, sizeof(Transfer));
 
52
    season_stats = g_array_new(FALSE, FALSE, sizeof(SeasonStat));
 
53
    name_lists = g_array_new(FALSE, FALSE, sizeof(NameList));
 
54
 
 
55
    save_file = g_string_new("");
 
56
 
 
57
    constants_app.list = 
 
58
        constants.list = options.list = NULL;
 
59
    constants_app.datalist =
 
60
        constants.datalist = options.datalist = NULL;
 
61
 
 
62
    popups_active = 0;
 
63
    selected_row[0] = selected_row[1] = -1;
 
64
 
 
65
    timeout_id = -1;
 
66
 
 
67
    file_load_conf_files();
 
68
 
 
69
    language_set(language_get_code_index(opt_str("string_opt_language_code")) + 1);
 
70
}
 
71
 
 
72
/**
 
73
   Process the command line arguments and do some things
 
74
   that have to be done at the beginning (like initializing the
 
75
   random number generator).
 
76
   @param argc Number of command line arguments.
 
77
   @param argv Command line arguments array.
 
78
*/
 
79
void
 
80
main_init(gint argc, gchar *argv[])
 
81
{
 
82
    gchar buf[SMALL];
 
83
    gchar *pwd = g_get_current_dir();
58
84
 
59
85
    /* initialize the random nr generator */
60
 
    srandom((unsigned)time(NULL));
61
 
 
62
 
    set_variables();
63
 
    
64
 
    sprintf(buf, "%s/support_files", getenv("PWD"));
65
 
    add_support_directory(buf);
66
 
 
67
 
    sprintf(buf, "%s/.bygfoot", getenv("HOME"));
68
 
    add_support_directory(buf);
69
 
 
70
 
    /* look for pixmap directory and country file */
71
 
    for(i=1;i<argc - 1;i++)
72
 
    {
73
 
        if(strcmp(argv[i], "-d") == 0)
74
 
            add_support_directory (argv[i + 1]);
75
 
        else if(strcmp(argv[i], "-f") == 0)
76
 
            country_names(0, argv[i + 1]);
77
 
    }
78
 
    
79
 
    /* check for the files with team and player names
80
 
       and help file */
81
 
    check_files();
82
 
    
83
 
    check_home_dir();
84
 
 
85
 
    /* fill the player names array */
86
 
    text_file_number_to_char(TEXT_FILES_PLAYER_NAMES, buf, TRUE);
87
 
    get_names(buf, player_names);
88
 
 
89
 
    /* look for 'start editor' option */
90
 
    for(i=1;i<argc;i++)
91
 
        if(strcmp(argv[i], "--editor") == 0 ||
92
 
           strcmp(argv[i], "-e") == 0)
93
 
            return 5;
94
 
 
95
 
    main_window = return_main_window();
96
 
    
97
 
    /* look for savegame */
98
 
    for(i=1;i<argc;i++)
99
 
        if(strcmp(argv[i - 1], "-d") != 0 &&
100
 
           strcmp(argv[i], "-d") != 0 &&
101
 
           strcmp(argv[i - 1], "-f") != 0 &&
102
 
           strcmp(argv[i], "-f") != 0)
103
 
        {
104
 
            sprintf(buf, "%s", argv[i]);
105
 
            buf2 = find_support_file(buf);
106
 
 
107
 
            if(buf2 == NULL || argv[i][0] == '/')
108
 
                g_string_assign(save_file, argv[i]);
109
 
            else
110
 
                g_string_assign(save_file, buf2);
111
 
 
112
 
            if(buf2 != NULL)
113
 
                g_free(buf2);
114
 
                
115
 
            if(check_save_game(save_file->str))
116
 
            {
117
 
                load_game(save_file->str);              
118
 
                on_button_back_to_main_clicked(NULL, NULL);
119
 
                gtk_widget_show(main_window);
120
 
                set_save(1);
121
 
                return 10;
122
 
            }
123
 
            else
124
 
            {           
125
 
                g_print("\nUsage: bygfoot [-e|--editor] [-d pixmapdir] [-f country_file] [savegame]\n\n");
126
 
                g_print("Options\n");
127
 
                g_print("-e, --editor:\t Start Bygfoot Team Editor instead of the game itself\n");
128
 
                g_print("-d\t\t Add the 'pixmapdir' directory to the list Bygfoot searches when\n");
129
 
                g_print("\t\t looking for text files and pixmap files.\n");
130
 
                g_print("-f\t\t Load the file 'country_file' which is structured like\n");
131
 
                g_print("\t\t 'country_eng' and specifies team and cup names\n\n");
132
 
                g_print("Any other argument is interpreted as a savegame file.\n\n");
133
 
                
134
 
                if(strcmp(argv[i], "-h") != 0 &&
135
 
                   strcmp(argv[i], "--help") != 0)
136
 
                    g_print(
137
 
                        "\n *** Doesn't seem to be a Bygfoot savegame: %s ***\n\n",
138
 
                        save_file->str);
139
 
                
140
 
                g_string_free(save_file, TRUE);
141
 
                gtk_widget_destroy(main_window);
142
 
                exit(0);
143
 
            }
144
 
            break;
145
 
        }
146
 
 
147
 
    return 0;
 
86
    rand_generator = g_rand_new();
 
87
 
 
88
    support_directories = NULL;
 
89
 
 
90
    file_add_support_directory_recursive(PACKAGE_DATA_DIR "/" PACKAGE "/support_files");
 
91
 
 
92
    sprintf(buf, "%s/%s", g_get_home_dir(), HOMEDIRNAME);
 
93
    file_add_support_directory_recursive(buf);
 
94
 
 
95
    sprintf(buf, "%s/support_files", pwd);
 
96
    g_free(pwd);
 
97
    file_add_support_directory_recursive(buf);
 
98
    
 
99
    file_check_home_dir();
 
100
 
 
101
    main_init_variables();
148
102
}
149
103
 
 
104
/**
 
105
   Initialize the GTK stuff and the gettext stuff.
 
106
   Start the game.
 
107
   @param argc Number of command line arguments.
 
108
   @param argv Command line arguments array.
 
109
*/
150
110
gint
151
111
main (gint argc, gchar *argv[])
152
112
{
153
 
    gint return_value;
154
 
 
155
113
 
156
114
#ifdef ENABLE_NLS
157
115
    bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
158
116
    bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
159
117
    textdomain (GETTEXT_PACKAGE);
160
118
#endif
161
 
    
162
 
  gtk_set_locale ();
163
 
  gtk_init (&argc, &argv);
164
 
 
165
 
  add_support_directory(PACKAGE_DATA_DIR "/" PACKAGE "/support_files");
166
 
  
167
 
  return_value = bygfoot_init(argc, argv);
168
 
 
169
 
  if(return_value == 5)
170
 
      start_editor();
171
 
  else if(return_value == 0)
172
 
      start(0);
173
 
  
174
 
  gtk_main ();
175
 
  return 0;
 
119
 
 
120
    gtk_set_locale ();
 
121
    gtk_init (&argc, &argv);
 
122
  
 
123
    main_init(argc, argv);
 
124
 
 
125
    window_show_startup();
 
126
    stat0 = STATUS_TEAM_SELECTION;
 
127
 
 
128
    gtk_main ();
 
129
 
 
130
    main_exit_program(EXIT_OK, NULL);
 
131
 
 
132
    return 0;
 
133
}
 
134
 
 
135
/** Exit the program with the given exit code and message. Try to
 
136
    destroy all widgets and free all memory first.
 
137
    @param exit_code The number we return to the shell.
 
138
    @param exit_message The message we print.
 
139
    @return The exit code of the program. */
 
140
void
 
141
main_exit_program(gint exit_code, gchar *exit_message)
 
142
{
 
143
    if(gtk_main_level() > 0)
 
144
        gtk_main_quit();
 
145
 
 
146
    free_memory();
 
147
    
 
148
    if(exit_message != NULL)
 
149
        g_warning(exit_message);
 
150
    
 
151
    exit(exit_code);
176
152
}