Fix all warnings -Wall spews
[lsnes.git] / lsnes.cpp
blob3ee7b63f52aebf1c359ac9a0f89819c087688a23
1 #include <sstream>
2 #include "mainloop.hpp"
3 #include "lua.hpp"
4 #include "rrdata.hpp"
5 #include "lsnes.hpp"
6 #include "rom.hpp"
7 #include "keymapper.hpp"
8 #include "misc.hpp"
9 #include <sys/time.h>
10 #include <snes/snes.hpp>
11 #include <ui-libsnes/libsnes.hpp>
12 #include "framerate.hpp"
13 #if defined(_WIN32) || defined(_WIN64)
14 #include "SDL_main.h"
15 #endif
18 class my_interfaced : public SNES::Interface
20 string path(SNES::Cartridge::Slot slot, const string &hint)
22 return "./";
26 class mycommandhandlerd : public aliasexpand_commandhandler
28 public:
29 void docommand2(std::string& cmd, window* win) throw(std::bad_alloc, std::runtime_error)
31 if(cmd != "")
32 win->message("Unrecognized command: " + cmd);
36 struct moviefile generate_movie_template(std::vector<std::string> cmdline, loaded_rom& r, window* win)
38 struct moviefile movie;
39 movie.gametype = gtype::togametype(r.rtype, r.region);
40 movie.rom_sha256 = r.rom.sha256;
41 movie.romxml_sha256 = r.rom_xml.sha256;
42 movie.slota_sha256 = r.slota.sha256;
43 movie.slotaxml_sha256 = r.slota_xml.sha256;
44 movie.slotb_sha256 = r.slotb.sha256;
45 movie.slotbxml_sha256 = r.slotb_xml.sha256;
46 movie.movie_sram = load_sram_commandline(cmdline);
47 for(auto i = cmdline.begin(); i != cmdline.end(); i++) {
48 std::string o = *i;
49 if(o.length() >= 8 && o.substr(0, 8) == "--port1=")
50 movie.port1 = port_type::lookup(o.substr(8), false).ptype;
51 if(o.length() >= 8 && o.substr(0, 8) == "--port2=")
52 movie.port2 = port_type::lookup(o.substr(8), true).ptype;
53 if(o.length() >= 11 && o.substr(0, 11) == "--gamename=")
54 movie.gamename = o.substr(11);
55 if(o.length() >= 9 && o.substr(0, 9) == "--author=") {
56 std::string line = o.substr(9);
57 fieldsplitter f(line);
58 std::string full = f;
59 std::string nick = f;
60 if(full == "" && nick == "")
61 throw std::runtime_error("Bad author name, one of full or nickname must be present");
62 movie.authors.push_back(std::make_pair(full, nick));
68 return movie;
71 #if defined(_WIN32) || defined(_WIN64)
72 int SDL_main(int argc, char** argv)
73 #else
74 int main(int argc, char** argv)
75 #endif
77 std::vector<std::string> cmdline;
78 for(int i = 1; i < argc; i++)
79 cmdline.push_back(argv[i]);
80 mycommandhandlerd handler;
81 my_interfaced intrf;
82 SNES::system.interface = &intrf;
84 set_random_seed();
87 std::ostringstream x;
88 x << snes_library_id() << " (" << SNES::Info::Profile << " core)";
89 bsnes_core_version = x.str();
91 window win;
92 init_lua(&win);
93 lua_set_commandhandler(handler);
95 win.out() << "BSNES version: " << bsnes_core_version << std::endl;
96 win.out() << "lsnes version: lsnes rr" << lsnes_version << std::endl;
97 win.out() << "Command line is: ";
98 for(auto k = cmdline.begin(); k != cmdline.end(); k++)
99 win.out() << "\"" << *k << "\" ";
100 win.out() << std::endl;
102 std::string cfgpath = get_config_path(&win);
103 create_lsnesrc(&win);
104 out(&win) << "Saving per-user data to: " << get_config_path(&win) << std::endl;
105 out(&win) << "--- Running lsnesrc --- " << std::endl;
106 std::string rc = "run-script " + cfgpath + "/lsnes.rc";
107 handler.docommand(rc, &win);
108 out(&win) << "--- End running lsnesrc --- " << std::endl;
110 out(&win) << "--- Loading ROM ---" << std::endl;
111 struct loaded_rom r;
112 try {
113 r = load_rom_from_commandline(cmdline, &win);
114 r.load();
115 } catch(std::bad_alloc& e) {
116 OOM_panic(&win);
117 } catch(std::exception& e) {
118 win.out() << "FATAL: Can't load ROM: " << e.what() << std::endl;
119 win.fatal_error();
120 exit(1);
122 win.out() << "Detected region: " << gtype::tostring(r.rtype, r.region) << std::endl;
123 if(r.region == REGION_PAL)
124 set_nominal_framerate(322445.0/6448.0);
125 else if(r.region == REGION_NTSC)
126 set_nominal_framerate(10738636.0/178683.0);
128 out(&win) << "--- Internal memory mappings ---" << std::endl;
129 dump_region_map(&win);
130 out(&win) << "--- End of Startup --- " << std::endl;
131 try {
132 moviefile movie;
133 bool loaded = false;
134 for(auto i = cmdline.begin(); i != cmdline.end(); i++)
135 if(i->length() > 0 && (*i)[0] != '-') {
136 movie = moviefile(*i);
137 loaded = true;
139 if(!loaded)
140 movie = generate_movie_template(cmdline, r, &win);
141 main_loop(&win, r, movie);
142 } catch(std::bad_alloc& e) {
143 OOM_panic(&win);
144 } catch(std::exception& e) {
145 win.message(std::string("Fatal: ") + e.what());
146 win.fatal_error();
147 return 1;
149 rrdata::close();
150 return 0;