forked from lantus/Cannonball-C
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.c
312 lines (259 loc) · 7.43 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/***************************************************************************
Cannonball Main Entry Point.
Copyright Chris White.
See license.txt for more details.
***************************************************************************/
// SDL Library
#include <SDL/SDL.h>
#include <stdint.h>
// SDL Specific Code
#include "sdl/timer.h"
#include "sdl/input.h"
#include "sdl/music.h"
#include "video.h"
#include "romloader.h"
#include "trackloader.h"
#include <stdint.h>
#include "main.h"
#include "setup.h"
#include "frontend/config.h"
#include "frontend/menu.h"
#include "cannonboard/interface.h"
#include "engine/oinputs.h"
#include "engine/ooutputs.h"
#include "engine/omusic.h"
// Initialize Shared Variables
int cannonball_state = STATE_BOOT;
double cannonball_frame_ms = 0;
int cannonball_frame = 0;
uint8_t cannonball_tick_frame = 1;
int cannonball_fps_counter = 0;
static void Clear_Cannonball()
{
#ifdef COMPILE_SOUND_CODE
Audio_stop_audio();
#endif
I_CAMD_StopSong();
I_CAMD_ShutdownMusic();
Input_close();
Render_disable();
SDL_Quit();
}
static void process_events(void)
{
SDL_Event event;
// Grab all events from the queue.
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_KEYDOWN:
// Handle key presses.
if (event.key.keysym.sym == SDLK_ESCAPE || event.key.keysym.sym == SDLK_3)
cannonball_state = STATE_QUIT;
else
Input_handle_key_down(&event.key.keysym);
break;
case SDL_KEYUP:
Input_handle_key_up(&event.key.keysym);
break;
case SDL_JOYAXISMOTION:
Input_handle_joy_axis(&event.jaxis);
break;
case SDL_JOYBUTTONDOWN:
Input_handle_joy_down(&event.jbutton);
break;
case SDL_JOYBUTTONUP:
Input_handle_joy_up(&event.jbutton);
break;
case SDL_QUIT:
// Handle quit requests (like Ctrl-c).
cannonball_state = STATE_QUIT;
break;
}
}
}
// Pause Engine
uint8_t pause_engine;
static void tick()
{
cannonball_frame++;
Packet* packet = Config_cannonboard.enabled ? Interface_get_packet() : NULL;
// Non standard FPS.
// Determine whether to tick the current frame.
if (Config_fps == 30)
cannonball_tick_frame = 1;
else if (Config_fps == 60)
cannonball_tick_frame = cannonball_frame & 1;
else if (Config_fps == 120)
cannonball_tick_frame = (cannonball_frame & 3) == 1;
process_events();
if (cannonball_tick_frame)
OInputs_tick(packet); // Do Controls
OInputs_do_gear(); // Digital Gear
switch (cannonball_state)
{
case STATE_GAME:
{
if (Input_has_pressed(INPUT_TIMER))
Outrun_freeze_timer = !Outrun_freeze_timer;
if (Input_has_pressed(INPUT_PAUSE))
pause_engine = !pause_engine;
if (Input_has_pressed(INPUT_MENU))
cannonball_state = STATE_INIT_MENU;
if (!pause_engine || Input_has_pressed(INPUT_STEP))
{
Outrun_tick(packet, cannonball_tick_frame);
Input_frame_done(); // Denote keys read
#ifdef COMPILE_SOUND_CODE
// Tick audio program code
OSoundInt_tick();
// Tick SDL Audio
Audio_tick();
#endif
}
else
{
Input_frame_done(); // Denote keys read
}
}
break;
case STATE_INIT_GAME:
if (Config_engine.jap && !Roms_load_japanese_roms())
{
cannonball_state = STATE_QUIT;
}
else
{
pause_engine = 0;
Outrun_init();
cannonball_state = STATE_GAME;
}
break;
case STATE_MENU:
{
Menu_tick(packet);
Input_frame_done();
#ifdef COMPILE_SOUND_CODE
// Tick audio program code
OSoundInt_tick();
// Tick SDL Audio
Audio_tick();
#endif
}
break;
case STATE_INIT_MENU:
OInputs_init();
OOutputs_init();
Menu_init();
cannonball_state = STATE_MENU;
break;
}
// Draw SDL Video
Video_draw_frame();
}
static void main_loop()
{
// FPS Counter (If Enabled)
Timer fps_count;
int frame = 0;
Timer_init(&fps_count);
Timer_start(&fps_count);
// General Frame Timing
Timer frame_time;
Timer_init(&frame_time);
int t;
double deltatime = 0;
int deltaintegral = 0;
while (cannonball_state != STATE_QUIT)
{
uint32_t start;
start = SDL_GetTicks();
Timer_start(&frame_time);
tick();
#ifdef COMPILE_SOUND_CODE
deltatime += (cannonball_frame_ms * Audio_adjust_speed());
#else
deltatime += cannonball_frame_ms;
#endif
deltaintegral = (int) deltatime;
t = Timer_get_ticks(&frame_time);
// Cap Frame Rate: Sleep Remaining Frame Time
/*if (t < deltatime)
{
SDL_Delay((Uint32) (deltatime - t));
}*/
deltatime -= deltaintegral;
if (Config_video.fps_count)
{
frame++;
if (Timer_get_ticks(&fps_count) >= 1000)
{
cannonball_fps_counter = frame;
frame = 0;
Timer_start(&fps_count);
}
}
}
}
int main(int argc, char* argv[])
{
// Initialize timer and video systems
if ( SDL_Init( SDL_INIT_TIMER | SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) == -1 )
{
fprintf(stderr, "SDL Initialization failed: %d\n", SDL_GetError());
return 1;
}
initTimer();
TrackLoader_Create();
// Load LayOut File
uint8_t loaded = 0;
if (argc == 3 && strcmp(argv[1], "-file") == 0)
{
if (TrackLoader_set_layout_track(argv[2]))
{
loaded = Roms_load_revb_roms();
}
}
// Load Roms Only
else
{
loaded = Roms_load_revb_roms();
}
//TrackLoader_set_layout_track("d:/temp.bin");
//loaded = Roms_load_revb_roms();
if (loaded)
{
// Load XML Config
Config_load(FILENAME_CONFIG);
I_CAMD_InitMusic();
// Load fixed PCM ROM based on config
if (Config_sound.fix_samples)
Roms_load_pcm_rom(1);
// Load patched widescreen tilemaps
if (!OMusic_load_widescreen_map())
{
fprintf(stderr, "Unable to load widescreen tilemaps.\n");
}
Video_Create();
// Initialize SDL Video, Only allow to run it if it is init
if (Video_init(&Config_video))
{
#ifdef COMPILE_SOUND_CODE
Audio_init();
#endif
cannonball_state = Config_menu.enabled ? STATE_INIT_MENU : STATE_INIT_GAME;
// Initalize controls
Input_init(Config_controls.pad_id,
Config_controls.keyconfig, Config_controls.padconfig,
Config_controls.analog, Config_controls.axis, Config_controls.asettings);
main_loop(); // Loop until we quit the app
}
}
else
{
Show_Warning();
}
Clear_Cannonball();
return 0;
}