2 #include <msp/core/getopt.h>
3 #include <msp/input/keys.h>
4 #include <msp/io/console.h>
5 #include <msp/io/file.h>
6 #include <msp/strings/regex.h>
7 #include <msp/time/utils.h>
10 #include "launchscreen.h"
15 LauncherBase::Options::Options(Graphics::Display &dpy, int argc, char **argv):
21 unsigned fullscreen = 0;
22 string fullscreen_size;
23 unsigned windowed = 0;
25 float seek_seconds = 0.0f;
28 getopt.add_option('f', "fullscreen", fullscreen_size, GetOpt::OPTIONAL_ARG).bind_seen_count(fullscreen).set_help("Start in fullscreen mode", "SIZE");
29 getopt.add_option('w', "windowed", windowed_size, GetOpt::OPTIONAL_ARG).bind_seen_count(windowed).set_help("Start in windowed mode without delay", "SIZE");
31 getopt.add_option('s', "seek", seek_seconds, GetOpt::REQUIRED_ARG).set_help("Start at TIME seconds", "TIME");
32 getopt.add_option("record-frames", frame_dump_fn, GetOpt::REQUIRED_ARG).set_help("Record raw frame data to FILE", "FILE");
34 getopt.add_option("no-music", no_music, GetOpt::NO_ARG).set_help("Disable music playback");
35 getopt.add_option("fps", framerate, GetOpt::REQUIRED_ARG).set_help("Run at NUM fps", "NUM");
36 getopt.add_option("no-vsync", no_vsync, GetOpt::NO_ARG).set_help("Disable vertical sync");
39 if(fullscreen && windowed)
40 throw usage_error("--fullscreen and --windowed are exclusive");
47 const Graphics::VideoMode &desktop_mode = dpy.get_desktop_mode();
48 if(!windowed_size.empty())
49 parse_size(windowed_size, win_opts);
50 else if(desktop_mode.width>1920 && desktop_mode.height>1080)
52 win_opts.width = 1920;
53 win_opts.height = 1080;
57 win_opts.width = 1280;
58 win_opts.height = 720;
61 if(!fullscreen_size.empty())
62 parse_size(fullscreen_size, fullscreen_opts);
65 fullscreen_opts.width = desktop_mode.width;
66 fullscreen_opts.height = desktop_mode.height;
68 fullscreen_opts.fullscreen = true;
69 fullscreen_opts.fullscreen_monitor = desktop_mode.monitor;
70 fullscreen_opts.fullscreen_exclusive = false;
72 seek = seek_seconds*Time::sec;
75 void LauncherBase::Options::parse_size(const string &size, Graphics::WindowOptions &win_opts)
77 static Regex r_size("^([0-9]+)x([0-9]+)$");
78 RegMatch m = r_size.match(size);
80 throw invalid_argument("Options::parse_size");
82 win_opts.width = lexical_cast<unsigned>(m[1].str);
83 win_opts.height = lexical_cast<unsigned>(m[2].str);
87 LauncherBase::LauncherBase(int argc, char **argv):
88 options(display, argc, argv),
89 window(display, options.start_fullscreen>0 ? options.fullscreen_opts : options.win_opts),
92 al_device(options.no_music ? 0 : new AL::Device),
93 al_context(options.no_music ? 0 : new AL::Context(*al_device)),
100 gl_context.set_swap_interval(options.no_vsync ? 0 : 1);
101 window.signal_close.connect(sigc::bind(sigc::mem_fun(this, &LauncherBase::exit), 0));
102 keyboard.signal_button_press.connect(sigc::bind_return(sigc::mem_fun(this, &LauncherBase::key_press), false));
105 LauncherBase::~LauncherBase()
107 delete launch_screen;
109 delete[] frame_dump_buffer;
114 void LauncherBase::start()
117 if(options.start_fullscreen>=0)
118 start_demo(options.start_fullscreen);
121 launch_screen = new LaunchScreen(get_resources());
122 timeout = Time::now()+5*Time::sec;
126 void LauncherBase::tick()
130 else if(launch_screen)
131 tick_launch_screen();
134 void LauncherBase::tick_demo()
136 Time::TimeStamp t = Time::now();
141 timeout = Time::TimeStamp();
144 if(!options.no_vsync && t<demo->get_next_frame_time())
150 #ifdef WITH_DEVELOPER
153 glReadPixels(0, 0, window.get_width(), window.get_height(), GL_RGB, GL_UNSIGNED_BYTE, frame_dump_buffer);
154 frame_dump->write(frame_dump_buffer, frame_size);
159 void LauncherBase::tick_launch_screen()
161 Time::TimeStamp t = Time::now();
162 Time::TimeDelta time_left = timeout-t;
163 if(time_left>Time::zero)
164 launch_screen->set_countdown(ceil(time_left/Time::sec));
169 launch_screen->render();
170 gl_context.swap_buffers();
174 delete launch_screen;
179 void LauncherBase::start_demo(bool fullscreen)
183 window.reconfigure(options.fullscreen_opts);
184 window.show_cursor(false);
185 timeout = Time::now()+Time::sec;
188 timeout = Time::TimeStamp();
190 demo = create_demo();
191 if(!options.no_music)
192 demo->enable_music();
193 if(options.framerate)
194 demo->set_fixed_framerate(options.framerate);
196 demo->seek(options.seek);
197 demo->signal_finished.connect(sigc::bind(sigc::mem_fun(this, &LauncherBase::exit), 0));
199 if(!options.frame_dump_fn.empty())
201 if(options.frame_dump_fn=="-")
202 frame_dump = &IO::cout;
204 frame_dump = new IO::File(options.frame_dump_fn, IO::M_WRITE);
205 frame_size = window.get_width()*window.get_height()*3;
206 frame_dump_buffer = new char[frame_size];
210 void LauncherBase::key_press(unsigned key)
212 if(key==Input::KEY_ENTER && !demo)
214 else if(key==Input::KEY_ESC)