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"
17 LauncherBase::Options::Options(Graphics::Display &dpy, int argc, char **argv):
23 unsigned fullscreen = 0;
24 string fullscreen_size;
25 unsigned windowed = 0;
27 float seek_seconds = 0.0f;
30 getopt.add_option('f', "fullscreen", fullscreen_size, GetOpt::OPTIONAL_ARG).bind_seen_count(fullscreen).set_help("Start in fullscreen mode", "SIZE");
31 getopt.add_option('w', "windowed", windowed_size, GetOpt::OPTIONAL_ARG).bind_seen_count(windowed).set_help("Start in windowed mode without delay", "SIZE");
33 getopt.add_option('s', "seek", seek_seconds, GetOpt::REQUIRED_ARG).set_help("Start at TIME seconds", "TIME");
34 getopt.add_option("record-frames", frame_dump_fn, GetOpt::REQUIRED_ARG).set_help("Record raw frame data to FILE", "FILE");
36 getopt.add_option("no-music", no_music, GetOpt::NO_ARG).set_help("Disable music playback");
37 getopt.add_option("fps", framerate, GetOpt::REQUIRED_ARG).set_help("Run at NUM fps", "NUM");
38 getopt.add_option("no-vsync", no_vsync, GetOpt::NO_ARG).set_help("Disable vertical sync");
41 if(fullscreen && windowed)
42 throw usage_error("--fullscreen and --windowed are exclusive");
49 const Graphics::VideoMode &desktop_mode = dpy.get_desktop_mode();
50 if(!windowed_size.empty())
51 parse_size(windowed_size, win_opts);
52 else if(desktop_mode.width>1920 && desktop_mode.height>1080)
54 win_opts.width = 1920;
55 win_opts.height = 1080;
59 win_opts.width = 1280;
60 win_opts.height = 720;
63 if(!fullscreen_size.empty())
64 parse_size(fullscreen_size, fullscreen_opts);
67 fullscreen_opts.width = desktop_mode.width;
68 fullscreen_opts.height = desktop_mode.height;
70 fullscreen_opts.fullscreen = true;
71 fullscreen_opts.fullscreen_monitor = desktop_mode.monitor;
72 fullscreen_opts.fullscreen_exclusive = false;
74 seek = seek_seconds*Time::sec;
77 void LauncherBase::Options::parse_size(const string &size, Graphics::WindowOptions &win_opts)
79 static Regex r_size("^([0-9]+)x([0-9]+)$");
80 RegMatch m = r_size.match(size);
82 throw invalid_argument("Options::parse_size");
84 win_opts.width = lexical_cast<unsigned>(m[1].str);
85 win_opts.height = lexical_cast<unsigned>(m[2].str);
89 LauncherBase::LauncherBase(int argc, char **argv):
90 options(display, argc, argv),
91 window(display, options.start_fullscreen>0 ? options.fullscreen_opts : options.win_opts),
94 al_device(options.no_music ? 0 : new AL::Device),
95 al_context(options.no_music ? 0 : new AL::Context(*al_device)),
102 gl_context.set_swap_interval(options.no_vsync ? 0 : 1);
103 window.signal_close.connect(sigc::bind(sigc::mem_fun(this, &LauncherBase::exit), 0));
104 keyboard.signal_button_press.connect(sigc::bind_return(sigc::mem_fun(this, &LauncherBase::key_press), false));
107 LauncherBase::~LauncherBase()
109 delete launch_screen;
111 delete[] frame_dump_buffer;
116 void LauncherBase::start()
119 if(options.start_fullscreen>=0)
120 start_demo(options.start_fullscreen);
123 launch_screen = new LaunchScreen(get_resources());
124 timeout = Time::now()+5*Time::sec;
128 void LauncherBase::tick()
132 else if(launch_screen)
133 tick_launch_screen();
136 void LauncherBase::tick_demo()
138 Time::TimeStamp t = Time::now();
143 timeout = Time::TimeStamp();
146 if(!options.no_vsync && t<demo->get_next_frame_time())
152 #ifdef WITH_DEVELOPER
155 glReadPixels(0, 0, window.get_width(), window.get_height(), GL_RGB, GL_UNSIGNED_BYTE, frame_dump_buffer);
156 frame_dump->write(frame_dump_buffer, frame_size);
161 void LauncherBase::tick_launch_screen()
163 Time::TimeStamp t = Time::now();
164 Time::TimeDelta time_left = timeout-t;
165 if(time_left>Time::zero)
166 launch_screen->set_countdown(ceil(time_left/Time::sec));
171 launch_screen->render();
172 gl_context.swap_buffers();
176 delete launch_screen;
181 void LauncherBase::start_demo(bool fullscreen)
185 window.reconfigure(options.fullscreen_opts);
186 window.show_cursor(false);
187 timeout = Time::now()+Time::sec;
190 timeout = Time::TimeStamp();
192 demo = create_demo();
193 if(!options.no_music)
194 demo->enable_music();
195 if(options.framerate)
196 demo->set_fixed_framerate(options.framerate);
198 demo->seek(options.seek);
199 demo->signal_finished.connect(sigc::bind(sigc::mem_fun(this, &LauncherBase::exit), 0));
201 if(!options.frame_dump_fn.empty())
203 if(options.frame_dump_fn=="-")
204 frame_dump = &IO::cout;
206 frame_dump = new IO::File(options.frame_dump_fn, IO::M_WRITE);
207 frame_size = window.get_width()*window.get_height()*3;
208 frame_dump_buffer = new char[frame_size];
212 void LauncherBase::key_press(unsigned key)
214 if(key==Input::KEY_ENTER && !demo)
216 else if(key==Input::KEY_ESC)
220 } // namespace DemoScene