2 #include <msp/core/getopt.h>
3 #include <msp/gl/pipelinetemplate.h>
4 #include <msp/input/keys.h>
5 #include <msp/io/console.h>
6 #include <msp/io/file.h>
7 #include <msp/strings/regex.h>
8 #include <msp/time/utils.h>
11 #include "launchscreen.h"
19 LauncherBase::Options::Options(Graphics::Display &dpy, int argc, char **argv):
25 unsigned fullscreen = 0;
26 string fullscreen_size;
27 unsigned windowed = 0;
29 float seek_seconds = 0.0f;
32 getopt.add_option('f', "fullscreen", fullscreen_size, GetOpt::OPTIONAL_ARG).bind_seen_count(fullscreen).set_help("Start in fullscreen mode", "SIZE");
33 getopt.add_option('w', "windowed", windowed_size, GetOpt::OPTIONAL_ARG).bind_seen_count(windowed).set_help("Start in windowed mode without delay", "SIZE");
35 getopt.add_option('s', "seek", seek_seconds, GetOpt::REQUIRED_ARG).set_help("Start at TIME seconds", "TIME");
36 getopt.add_option("record-frames", frame_dump_fn, GetOpt::REQUIRED_ARG).set_help("Record raw frame data to FILE", "FILE");
38 getopt.add_option("no-music", no_music, GetOpt::NO_ARG).set_help("Disable music playback");
39 getopt.add_option("fps", framerate, GetOpt::REQUIRED_ARG).set_help("Run at NUM fps", "NUM");
40 getopt.add_option("no-vsync", no_vsync, GetOpt::NO_ARG).set_help("Disable vertical sync");
43 if(fullscreen && windowed)
44 throw usage_error("--fullscreen and --windowed are exclusive");
51 const Graphics::VideoMode &desktop_mode = dpy.get_desktop_mode();
52 if(!windowed_size.empty())
53 parse_size(windowed_size, win_opts);
54 else if(desktop_mode.width>1920 && desktop_mode.height>1080)
56 win_opts.width = 1920;
57 win_opts.height = 1080;
61 win_opts.width = 1280;
62 win_opts.height = 720;
65 if(!fullscreen_size.empty())
66 parse_size(fullscreen_size, fullscreen_opts);
69 fullscreen_opts.width = desktop_mode.width;
70 fullscreen_opts.height = desktop_mode.height;
72 fullscreen_opts.fullscreen = true;
73 fullscreen_opts.fullscreen_monitor = desktop_mode.monitor;
74 fullscreen_opts.fullscreen_exclusive = false;
76 seek = seek_seconds*Time::sec;
79 void LauncherBase::Options::parse_size(const string &size, Graphics::WindowOptions &win_opts)
81 static Regex r_size("^([0-9]+)x([0-9]+)$");
82 RegMatch m = r_size.match(size);
84 throw invalid_argument("Options::parse_size");
86 win_opts.width = lexical_cast<unsigned>(m[1].str);
87 win_opts.height = lexical_cast<unsigned>(m[2].str);
91 LauncherBase::LauncherBase(int argc, char **argv):
92 options(display, argc, argv),
93 window(display, options.start_fullscreen>0 ? options.fullscreen_opts : options.win_opts),
96 al_device(options.no_music ? 0 : new AL::Device),
97 al_context(options.no_music ? 0 : new AL::Context(*al_device)),
104 gl_context.set_swap_interval(options.no_vsync ? 0 : 1);
105 window.signal_close.connect(sigc::bind(sigc::mem_fun(this, &LauncherBase::exit), 0));
106 keyboard.signal_button_press.connect(sigc::bind_return(sigc::mem_fun(this, &LauncherBase::key_press), false));
108 GL::PipelineTemplate::register_postprocessor<Vignette>("vignette");
111 LauncherBase::~LauncherBase()
113 delete launch_screen;
115 delete[] frame_dump_buffer;
120 void LauncherBase::start()
123 if(options.start_fullscreen>=0)
124 start_demo(options.start_fullscreen);
127 launch_screen = new LaunchScreen(get_resources());
128 timeout = Time::now()+5*Time::sec;
132 void LauncherBase::tick()
136 else if(launch_screen)
137 tick_launch_screen();
140 void LauncherBase::tick_demo()
142 Time::TimeStamp t = Time::now();
147 timeout = Time::TimeStamp();
150 if(!options.no_vsync && t<demo->get_next_frame_time())
156 #ifdef WITH_DEVELOPER
159 glReadPixels(0, 0, window.get_width(), window.get_height(), GL_RGB, GL_UNSIGNED_BYTE, frame_dump_buffer);
160 frame_dump->write(frame_dump_buffer, frame_size);
165 void LauncherBase::tick_launch_screen()
167 Time::TimeStamp t = Time::now();
168 Time::TimeDelta time_left = timeout-t;
169 if(time_left>Time::zero)
170 launch_screen->set_countdown(ceil(time_left/Time::sec));
175 launch_screen->render();
176 gl_context.swap_buffers();
180 delete launch_screen;
185 void LauncherBase::start_demo(bool fullscreen)
189 window.reconfigure(options.fullscreen_opts);
190 window.show_cursor(false);
191 timeout = Time::now()+Time::sec;
194 timeout = Time::TimeStamp();
196 demo = create_demo();
197 if(!options.no_music)
198 demo->enable_music();
199 if(options.framerate)
200 demo->set_fixed_framerate(options.framerate);
202 demo->seek(options.seek);
203 demo->signal_finished.connect(sigc::bind(sigc::mem_fun(this, &LauncherBase::exit), 0));
205 if(!options.frame_dump_fn.empty())
207 if(options.frame_dump_fn=="-")
208 frame_dump = &IO::cout;
210 frame_dump = new IO::File(options.frame_dump_fn, IO::M_WRITE);
211 frame_size = window.get_width()*window.get_height()*3;
212 frame_dump_buffer = new char[frame_size];
216 void LauncherBase::key_press(unsigned key)
218 if(key==Input::KEY_ENTER && !demo)
220 else if(key==Input::KEY_ESC)
224 } // namespace DemoScene