1 #include "hugo.h"
2
3
4 //! name of the backup ram filename
5 static char backup_mem[PATH_MAX];
6
7 struct host_machine host;
8 struct hugo_options option;
9
10 //! Setup the host machine
11 /*!
12 * \param argc The number of argument on command line
13 * \param argv The arguments on command line
14 * \return zero on success else non zero value
15 */
16 int
initialisation(int argc,char * argv[])17 initialisation (int argc, char *argv[])
18 {
19
20 #ifdef MSDOS
21 _crt0_startup_flags |= _CRT0_FLAG_NO_LFN;
22 // Disable long filename to avoid mem waste in select_rom func.
23 #endif
24
25 memset (&option, 0, sizeof (option));
26
27 option.want_stereo = FALSE;
28 option.want_fullscreen = FALSE;
29 option.want_fullscreen_aspect = FALSE;
30 option.configure_joypads = FALSE;
31 option.want_hardware_scaling = FALSE;
32
33 option.window_size = 1;
34
35 #if defined(ENABLE_NETPLAY)
36 option.local_input_mapping[0] = 0;
37 option.local_input_mapping[1] = 1;
38 option.local_input_mapping[2] = -1;
39 option.local_input_mapping[3] = -1;
40 option.local_input_mapping[4] = -1;
41 strcpy(option.server_hostname,"localhost");
42 #endif
43
44 /*
45 * Most sound cards won't like this value but we'll accept whatever SDL
46 * gives us. This frequency will get us really close to the original
47 * pc engine sound behaviour.
48 */
49 option.want_snd_freq = 21963;
50
51 // Initialise paths
52 osd_init_paths (argc, argv);
53
54 // Create the log file
55 init_log_file ();
56
57 // Init the random seed
58 srand ((unsigned int) time (NULL));
59
60 // Read configuration in ini file
61 parse_INIfile ();
62
63 // Read the command line
64 parse_commandline (argc, argv);
65
66 // Initialise the host machine
67 if (!osd_init_machine ())
68 return -1;
69
70 // If backup memory name hasn't been overriden on command line, use the default
71 if ((bmdefault) && (strcmp (bmdefault, "")))
72 snprintf (backup_mem, sizeof (backup_mem), "%s%s", short_exe_name,
73 bmdefault);
74 else
75 snprintf (backup_mem, sizeof (backup_mem), "%sbackup.dat",
76 short_exe_name);
77
78 // In case of crash, try to free audio related ressources if possible
79 // atexit (TrashSound);
80
81 // Initialise the input devices
82 if (osd_init_input () != 0)
83 {
84 fprintf (stderr, "Initialization of input system failed\n");
85 return (-2);
86 }
87
88 return 0;
89 }
90
91
92 //! Free ressources of the host machine
93 /*!
94 * Deallocate ressources reserved during the initialisation
95 */
96 void
cleanup()97 cleanup ()
98 {
99
100 osd_shutdown_input();
101
102 // Deinitialise the host machine
103 osd_shut_machine ();
104
105 }
106
107 //! Check if a game was asked
108 /*!
109 * \return non zero if a game must be played
110 */
111 int
game_asked()112 game_asked ()
113 {
114 return ((CD_emulation == 1) || (strcmp (cart_name, "")));
115 }
116
117 //! Run an instance of a rom or cd or iso
118 /*!
119 * \return non zero if another game has to be launched
120 */
121 int
play_game(void)122 play_game (void)
123 {
124
125 // Initialise the target machine (pce)
126 if (InitPCE (cart_name, backup_mem) != 0)
127 return 0;
128
129 if (!(*osd_gfx_driver_list[video_driver].init) ())
130 {
131 Log ("Can't set graphic mode\n");
132 printf (MESSAGE[language][cant_set_gmode]);
133 return 0;
134 }
135
136 if (!osd_snd_init_sound ())
137 {
138 Log ("Couldn't open any sound hardware on the host machine.\n");
139 printf (MESSAGE[language][audio_init_failed]);
140 }
141 else
142 printf (MESSAGE[language][audio_inited], 8, "SDL compatible soundcard",
143 host.sound.freq);
144
145 #if defined(ENABLE_NETPLAY)
146 osd_init_netplay();
147 #endif
148
149 // Run the emulation
150 RunPCE ();
151
152 #if defined(ENABLE_NETPLAY)
153 osd_shutdown_netplay();
154 #endif
155
156 osd_snd_trash_sound ();
157
158 (*osd_gfx_driver_list[video_driver].shut) ();
159
160 // Free the target machine (pce)
161 TrashPCE (backup_mem);
162
163 return cart_reload;
164 }
165
166
167 int
main(int argc,char * argv[])168 main (int argc, char *argv[])
169 {
170 int error;
171 error = 0;
172
173 error = initialisation (argc, argv);
174
175 #if defined(GTK)
176
177 if (!error)
178 {
179 if (game_asked ())
180 {
181 while (play_game ());
182 }
183 else
184 {
185 build_gtk_interface (argc, argv);
186 }
187 }
188
189 #else // not defined(GTK)
190
191 if (!error)
192 {
193 if (game_asked ())
194 {
195 while (play_game ());
196 }
197 else
198 {
199 printf ("No game specified\n");
200 }
201 }
202 #endif
203
204 cleanup ();
205
206 return 0;
207 }
208