File: sdlmain.cpp

package info (click to toggle)
nestopia 1.49-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 9,424 kB
  • sloc: cpp: 86,935; xml: 26,956; makefile: 1,515; ansic: 772; sh: 19
file content (169 lines) | stat: -rw-r--r-- 3,747 bytes parent folder | download | duplicates (2)
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
/*
 * Nestopia UE
 * 
 * Copyright (C) 2007-2008 R. Belmont
 * Copyright (C) 2012-2018 R. Danbrook
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 * 
 */

// Nst Common
#include "nstcommon.h"
#include "cli.h"
#include "audio.h"
#include "video.h"
#include "input.h"
#include "config.h"

// Nst SDL
#include "sdlmain.h"
#include "sdlvideo.h"
#include "sdlinput.h"

using namespace Nes::Api;

static int nst_quit = 0;

extern Input::Controllers *cNstPads;
extern nstpaths_t nstpaths;

extern bool (*nst_archive_select)(const char*, char*, size_t);
extern void (*audio_deinit)();

void nst_schedule_quit() {
	nst_quit = 1;
}

int main(int argc, char *argv[]) {
	// This is the main function
	
	// Set up directories
	nst_set_dirs();
	
	// Set default config options
	config_set_default();
	
	// Read the config file and override defaults
	config_file_read(nstpaths.nstdir);
	
	// Exit if there is no CLI argument
	if (argc == 1) {
		cli_show_usage();
		return 0;
	}
	
	// Handle command line arguments
	cli_handle_command(argc, argv);
	
	// Set up callbacks
	nst_set_callbacks();
	
	// Initialize SDL
	if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) < 0) {
		fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
		return 1;
	}
	
	// Detect Joysticks
	nstsdl_input_joysticks_detect();
	
	// Set default input keys
	nstsdl_input_conf_defaults();
	
	// Read the input config file and override defaults
	nstsdl_input_conf_read();
	
	// Set archive handler function pointer
	nst_archive_select = &nst_archive_select_file;
	
	// Set audio function pointers
	audio_set_funcs();
	
	// Set the video dimensions
	video_set_dimensions();
	
	// Initialize and load FDS BIOS and NstDatabase.xml
	nst_fds_bios_load();
	nst_db_load();
	
	// Load a rom from the command line
	if (argc > 1) {
		if (!nst_load(argv[argc - 1])) { nst_quit = 1; }
		else {
			// Create the window
			nstsdl_video_create();
			nstsdl_video_set_title(nstpaths.gamename);
			
			// Set play in motion
			nst_play();
			
			// Set the cursor if needed
			nstsdl_video_set_cursor();
		}
	}
	
	// Start the main loop
	SDL_Event event;
	while (!nst_quit) {
		nst_ogl_render();
		nstsdl_video_swapbuffers();
		
		while (SDL_PollEvent(&event)) {
			switch (event.type) {
				case SDL_QUIT:
					nst_quit = 1;
					break;
				
				case SDL_KEYDOWN:
				case SDL_KEYUP:
				case SDL_JOYHATMOTION:
				case SDL_JOYAXISMOTION:
				case SDL_JOYBUTTONDOWN:
				case SDL_JOYBUTTONUP:
				case SDL_MOUSEBUTTONDOWN:
				case SDL_MOUSEBUTTONUP:
					nstsdl_input_process(cNstPads, event);
					break;
				default: break;
			}	
		}
		
		nst_emuloop();
	}
	
	// Remove the cartridge and shut down the NES
	nst_unload();
	
	// Unload the FDS BIOS, NstDatabase.xml, and the custom palette
	nst_db_unload();
	nst_fds_bios_unload();
	nst_palette_unload();
	
	// Deinitialize audio
	audio_deinit();
	
	// Deinitialize joysticks
	nstsdl_input_joysticks_close();
	
	// Write the input config file
	nstsdl_input_conf_write();
	
	// Write the config file
	config_file_write(nstpaths.nstdir);

	return 0;
}