Small documentation fixups regarding joysticks/gamepads
[lsnes.git] / lsnes.cpp
blob1f4fef3d136829883de4edf741e3962c706300f8
1 #include <sstream>
2 #include "mainloop.hpp"
3 #include "command.hpp"
4 #include "lua.hpp"
5 #include "moviedata.hpp"
6 #include "rrdata.hpp"
7 #include "lsnes.hpp"
8 #include "rom.hpp"
9 #include "keymapper.hpp"
10 #include "misc.hpp"
11 #include "window.hpp"
12 #include <sys/time.h>
13 #include <snes/snes.hpp>
14 #include <ui-libsnes/libsnes.hpp>
15 #include "framerate.hpp"
16 #if defined(_WIN32) || defined(_WIN64) || defined(TEST_WIN32_CODE)
17 #include "SDL_main.h"
18 #endif
21 class my_interfaced : public SNES::Interface
23 string path(SNES::Cartridge::Slot slot, const string &hint)
25 return "./";
29 struct moviefile generate_movie_template(std::vector<std::string> cmdline, loaded_rom& r)
31 struct moviefile movie;
32 movie.coreversion = bsnes_core_version;
33 movie.projectid = get_random_hexstring(40);
34 movie.gametype = gtype::togametype(r.rtype, r.region);
35 movie.rom_sha256 = r.rom.sha256;
36 movie.romxml_sha256 = r.rom_xml.sha256;
37 movie.slota_sha256 = r.slota.sha256;
38 movie.slotaxml_sha256 = r.slota_xml.sha256;
39 movie.slotb_sha256 = r.slotb.sha256;
40 movie.slotbxml_sha256 = r.slotb_xml.sha256;
41 movie.movie_sram = load_sram_commandline(cmdline);
42 for(auto i = cmdline.begin(); i != cmdline.end(); i++) {
43 std::string o = *i;
44 if(o.length() >= 8 && o.substr(0, 8) == "--port1=")
45 movie.port1 = port_type::lookup(o.substr(8), false).ptype;
46 if(o.length() >= 8 && o.substr(0, 8) == "--port2=")
47 movie.port2 = port_type::lookup(o.substr(8), true).ptype;
48 if(o.length() >= 11 && o.substr(0, 11) == "--gamename=")
49 movie.gamename = o.substr(11);
50 if(o.length() >= 9 && o.substr(0, 9) == "--author=") {
51 std::string line = o.substr(9);
52 auto g = split_author(line);
53 movie.authors.push_back(g);
59 return movie;
62 namespace
64 void run_extra_scripts(const std::vector<std::string>& cmdline)
66 for(auto i = cmdline.begin(); i != cmdline.end(); i++) {
67 std::string o = *i;
68 if(o.length() >= 6 && o.substr(0, 6) == "--run=") {
69 std::string file = o.substr(6);
70 messages << "--- Running " << file << " --- " << std::endl;
71 command::invokeC("run-script " + file);
72 messages << "--- End running " << file << " --- " << std::endl;
78 int main(int argc, char** argv)
80 std::vector<std::string> cmdline;
81 for(int i = 1; i < argc; i++)
82 cmdline.push_back(argv[i]);
83 my_interfaced intrf;
84 SNES::system.interface = &intrf;
86 set_random_seed();
89 std::ostringstream x;
90 x << snes_library_id() << " (" << SNES::Info::Profile << " core)";
91 bsnes_core_version = x.str();
93 window::init();
94 init_lua();
96 messages << "BSNES version: " << bsnes_core_version << std::endl;
97 messages << "lsnes version: lsnes rr" << lsnes_version << std::endl;
98 messages << "Command line is: ";
99 for(auto k = cmdline.begin(); k != cmdline.end(); k++)
100 messages << "\"" << *k << "\" ";
101 messages << std::endl;
103 std::string cfgpath = get_config_path();
104 create_lsnesrc();
105 messages << "Saving per-user data to: " << get_config_path() << std::endl;
106 messages << "--- Running lsnesrc --- " << std::endl;
107 command::invokeC("run-script " + cfgpath + "/lsnes.rc");
108 messages << "--- End running lsnesrc --- " << std::endl;
110 run_extra_scripts(cmdline);
112 messages << "--- Loading ROM ---" << std::endl;
113 struct loaded_rom r;
114 try {
115 r = load_rom_from_commandline(cmdline);
116 r.load();
117 } catch(std::bad_alloc& e) {
118 OOM_panic();
119 } catch(std::exception& e) {
120 messages << "FATAL: Can't load ROM: " << e.what() << std::endl;
121 fatal_error();
122 exit(1);
124 messages << "Detected region: " << gtype::tostring(r.rtype, r.region) << std::endl;
125 if(r.region == REGION_PAL)
126 set_nominal_framerate(322445.0/6448.0);
127 else if(r.region == REGION_NTSC)
128 set_nominal_framerate(10738636.0/178683.0);
130 messages << "--- Internal memory mappings ---" << std::endl;
131 dump_region_map();
132 messages << "--- End of Startup --- " << std::endl;
133 moviefile movie;
134 movie.force_corrupt = true;
135 try {
136 bool loaded = false;
137 bool tried = false;
138 for(auto i = cmdline.begin(); i != cmdline.end(); i++)
139 if(i->length() > 0 && (*i)[0] != '-') {
140 try {
141 tried = true;
142 movie = moviefile(*i);
143 loaded = true;
144 } catch(std::bad_alloc& e) {
145 OOM_panic();
146 } catch(std::exception& e) {
147 messages << "Error loading '" << *i << "': " << e.what() << std::endl;
150 if(!tried)
151 movie = generate_movie_template(cmdline, r);
152 main_loop(r, movie);
153 } catch(std::bad_alloc& e) {
154 OOM_panic();
155 } catch(std::exception& e) {
156 messages << "FATAL: " << e.what() << std::endl;
157 fatal_error();
158 return 1;
160 rrdata::close();
161 window::quit();
162 return 0;