Since window is singleton anyway, get rid of window* parameters
[lsnes.git] / lsnes.cpp
blobbf9c84778b728b845f601f67aa3426f4499645b7
1 #include <sstream>
2 #include "mainloop.hpp"
3 #include "command.hpp"
4 #include "lua.hpp"
5 #include "rrdata.hpp"
6 #include "lsnes.hpp"
7 #include "rom.hpp"
8 #include "keymapper.hpp"
9 #include "misc.hpp"
10 #include <sys/time.h>
11 #include <snes/snes.hpp>
12 #include <ui-libsnes/libsnes.hpp>
13 #include "framerate.hpp"
14 #if defined(_WIN32) || defined(_WIN64)
15 #include "SDL_main.h"
16 #endif
19 class my_interfaced : public SNES::Interface
21 string path(SNES::Cartridge::Slot slot, const string &hint)
23 return "./";
27 struct moviefile generate_movie_template(std::vector<std::string> cmdline, loaded_rom& r)
29 struct moviefile movie;
30 movie.gametype = gtype::togametype(r.rtype, r.region);
31 movie.rom_sha256 = r.rom.sha256;
32 movie.romxml_sha256 = r.rom_xml.sha256;
33 movie.slota_sha256 = r.slota.sha256;
34 movie.slotaxml_sha256 = r.slota_xml.sha256;
35 movie.slotb_sha256 = r.slotb.sha256;
36 movie.slotbxml_sha256 = r.slotb_xml.sha256;
37 movie.movie_sram = load_sram_commandline(cmdline);
38 for(auto i = cmdline.begin(); i != cmdline.end(); i++) {
39 std::string o = *i;
40 if(o.length() >= 8 && o.substr(0, 8) == "--port1=")
41 movie.port1 = port_type::lookup(o.substr(8), false).ptype;
42 if(o.length() >= 8 && o.substr(0, 8) == "--port2=")
43 movie.port2 = port_type::lookup(o.substr(8), true).ptype;
44 if(o.length() >= 11 && o.substr(0, 11) == "--gamename=")
45 movie.gamename = o.substr(11);
46 if(o.length() >= 9 && o.substr(0, 9) == "--author=") {
47 std::string line = o.substr(9);
48 fieldsplitter f(line);
49 std::string full = f;
50 std::string nick = f;
51 if(full == "" && nick == "")
52 throw std::runtime_error("Bad author name, one of full or nickname must be present");
53 movie.authors.push_back(std::make_pair(full, nick));
59 return movie;
62 #if defined(_WIN32) || defined(_WIN64)
63 int SDL_main(int argc, char** argv)
64 #else
65 int main(int argc, char** argv)
66 #endif
68 std::vector<std::string> cmdline;
69 for(int i = 1; i < argc; i++)
70 cmdline.push_back(argv[i]);
71 my_interfaced intrf;
72 SNES::system.interface = &intrf;
74 set_random_seed();
77 std::ostringstream x;
78 x << snes_library_id() << " (" << SNES::Info::Profile << " core)";
79 bsnes_core_version = x.str();
81 window::init();
82 init_lua();
84 window::out() << "BSNES version: " << bsnes_core_version << std::endl;
85 window::out() << "lsnes version: lsnes rr" << lsnes_version << std::endl;
86 window::out() << "Command line is: ";
87 for(auto k = cmdline.begin(); k != cmdline.end(); k++)
88 window::out() << "\"" << *k << "\" ";
89 window::out() << std::endl;
91 std::string cfgpath = get_config_path();
92 create_lsnesrc();
93 window::out() << "Saving per-user data to: " << get_config_path() << std::endl;
94 window::out() << "--- Running lsnesrc --- " << std::endl;
95 command::invokeC("run-script " + cfgpath + "/lsnes.rc");
96 window::out() << "--- End running lsnesrc --- " << std::endl;
98 window::out() << "--- Loading ROM ---" << std::endl;
99 struct loaded_rom r;
100 try {
101 r = load_rom_from_commandline(cmdline);
102 r.load();
103 } catch(std::bad_alloc& e) {
104 OOM_panic();
105 } catch(std::exception& e) {
106 window::out() << "FATAL: Can't load ROM: " << e.what() << std::endl;
107 window::fatal_error();
108 exit(1);
110 window::out() << "Detected region: " << gtype::tostring(r.rtype, r.region) << std::endl;
111 if(r.region == REGION_PAL)
112 set_nominal_framerate(322445.0/6448.0);
113 else if(r.region == REGION_NTSC)
114 set_nominal_framerate(10738636.0/178683.0);
116 window::out() << "--- Internal memory mappings ---" << std::endl;
117 dump_region_map();
118 window::out() << "--- End of Startup --- " << std::endl;
119 try {
120 moviefile movie;
121 bool loaded = false;
122 for(auto i = cmdline.begin(); i != cmdline.end(); i++)
123 if(i->length() > 0 && (*i)[0] != '-') {
124 movie = moviefile(*i);
125 loaded = true;
127 if(!loaded)
128 movie = generate_movie_template(cmdline, r);
129 main_loop(r, movie);
130 } catch(std::bad_alloc& e) {
131 OOM_panic();
132 } catch(std::exception& e) {
133 window::message(std::string("Fatal: ") + e.what());
134 window::fatal_error();
135 return 1;
137 rrdata::close();
138 window::quit();
139 return 0;