]> git.tdb.fi Git - libs/demoscene.git/blob - source/launcher.cpp
aefa1be2b4b57b808f7379ee312bb9034d45b2dd
[libs/demoscene.git] / source / launcher.cpp
1 #include <cmath>
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>
9 #include "demo.h"
10 #include "filmgrain.h"
11 #include "launcher.h"
12 #include "launchscreen.h"
13 #include "vignette.h"
14
15 using namespace std;
16
17 namespace Msp {
18 namespace DemoScene {
19
20 LauncherBase::Options::Options(Graphics::Display &dpy, int argc, char **argv):
21         start_fullscreen(-1),
22         framerate(0),
23         no_music(false),
24         no_vsync(false)
25 {
26         unsigned fullscreen = 0;
27         string fullscreen_size;
28         unsigned windowed = 0;
29         string windowed_size;
30         float seek_seconds = 0.0f;
31
32         GetOpt getopt;
33         getopt.add_option('f', "fullscreen", fullscreen_size, GetOpt::OPTIONAL_ARG).bind_seen_count(fullscreen).set_help("Start in fullscreen mode", "SIZE");
34         getopt.add_option('w', "windowed", windowed_size, GetOpt::OPTIONAL_ARG).bind_seen_count(windowed).set_help("Start in windowed mode without delay", "SIZE");
35 #ifdef WITH_DEVELOPER
36         getopt.add_option('s', "seek", seek_seconds, GetOpt::REQUIRED_ARG).set_help("Start at TIME seconds", "TIME");
37         getopt.add_option("record-frames", frame_dump_fn, GetOpt::REQUIRED_ARG).set_help("Record raw frame data to FILE", "FILE");
38 #endif
39         getopt.add_option("no-music", no_music, GetOpt::NO_ARG).set_help("Disable music playback");
40         getopt.add_option("fps", framerate, GetOpt::REQUIRED_ARG).set_help("Run at NUM fps", "NUM");
41         getopt.add_option("no-vsync", no_vsync, GetOpt::NO_ARG).set_help("Disable vertical sync");
42         getopt(argc, argv);
43
44         if(fullscreen && windowed)
45                 throw usage_error("--fullscreen and --windowed are exclusive");
46
47         if(fullscreen)
48                 start_fullscreen = 1;
49         else if(windowed)
50                 start_fullscreen = 0;
51
52         const Graphics::VideoMode &desktop_mode = dpy.get_desktop_mode();
53         if(!windowed_size.empty())
54                 parse_size(windowed_size, win_opts);
55         else if(desktop_mode.width>1920 && desktop_mode.height>1080)
56         {
57                 win_opts.width = 1920;
58                 win_opts.height = 1080;
59         }
60         else
61         {
62                 win_opts.width = 1280;
63                 win_opts.height = 720;
64         }
65
66         if(!fullscreen_size.empty())
67                 parse_size(fullscreen_size, fullscreen_opts);
68         else
69         {
70                 fullscreen_opts.width = desktop_mode.width;
71                 fullscreen_opts.height = desktop_mode.height;
72         }
73         fullscreen_opts.fullscreen = true;
74         fullscreen_opts.fullscreen_monitor = desktop_mode.monitor;
75         fullscreen_opts.fullscreen_exclusive = false;
76
77         seek = seek_seconds*Time::sec;
78 }
79
80 void LauncherBase::Options::parse_size(const string &size, Graphics::WindowOptions &win_opts)
81 {
82         static Regex r_size("^([0-9]+)x([0-9]+)$");
83         RegMatch m = r_size.match(size);
84         if(!m)
85                 throw invalid_argument("Options::parse_size");
86
87         win_opts.width = lexical_cast<unsigned>(m[1].str);
88         win_opts.height = lexical_cast<unsigned>(m[2].str);
89 }
90
91
92 LauncherBase::LauncherBase(int argc, char **argv):
93         options(display, argc, argv),
94         window(display, options.start_fullscreen>0 ? options.fullscreen_opts : options.win_opts),
95         gl_context(window),
96         keyboard(window),
97         al_device(options.no_music ? 0 : new AL::Device),
98         al_context(options.no_music ? 0 : new AL::Context(*al_device)),
99         launch_screen(0),
100         demo(0),
101         frame_dump(0),
102         frame_size(0),
103         frame_dump_buffer(0)
104 {
105         gl_context.set_swap_interval(options.no_vsync ? 0 : 1);
106         window.signal_close.connect(sigc::bind(sigc::mem_fun(this, &LauncherBase::exit), 0));
107         keyboard.signal_button_press.connect(sigc::bind_return(sigc::mem_fun(this, &LauncherBase::key_press), false));
108
109         GL::PipelineTemplate::register_postprocessor<FilmGrain>("film_grain");
110         GL::PipelineTemplate::register_postprocessor<Vignette>("vignette");
111 }
112
113 LauncherBase::~LauncherBase()
114 {
115         delete launch_screen;
116         delete demo;
117         delete[] frame_dump_buffer;
118         delete al_context;
119         delete al_device;
120 }
121
122 void LauncherBase::start()
123 {
124         window.show();
125         if(options.start_fullscreen>=0)
126                 start_demo(options.start_fullscreen);
127         else
128         {
129                 launch_screen = new LaunchScreen(get_resources());
130                 timeout = Time::now()+5*Time::sec;
131         }
132 }
133
134 void LauncherBase::tick()
135 {
136         if(demo)
137                 tick_demo();
138         else if(launch_screen)
139                 tick_launch_screen();
140 }
141
142 void LauncherBase::tick_demo()
143 {
144         Time::TimeStamp t = Time::now();
145         if(timeout)
146         {
147                 if(t<timeout)
148                         return;
149                 timeout = Time::TimeStamp();
150         }
151
152         if(!options.no_vsync && t<demo->get_next_frame_time())
153                 return;
154
155         display.tick();
156         demo->tick();
157
158 #ifdef WITH_DEVELOPER
159         if(frame_dump)
160         {
161                 glReadPixels(0, 0, window.get_width(), window.get_height(), GL_RGB, GL_UNSIGNED_BYTE, frame_dump_buffer);
162                 frame_dump->write(frame_dump_buffer, frame_size);
163         }
164 #endif
165 }
166
167 void LauncherBase::tick_launch_screen()
168 {
169         Time::TimeStamp t = Time::now();
170         Time::TimeDelta time_left = timeout-t;
171         if(time_left>Time::zero)
172                 launch_screen->set_countdown(ceil(time_left/Time::sec));
173         else
174                 start_demo(true);
175
176         display.tick();
177         launch_screen->render();
178         gl_context.swap_buffers();
179
180         if(demo)
181         {
182                 delete launch_screen;
183                 launch_screen = 0;
184         }
185 }
186
187 void LauncherBase::start_demo(bool fullscreen)
188 {
189         if(fullscreen)
190         {
191                 window.reconfigure(options.fullscreen_opts);
192                 window.show_cursor(false);
193                 timeout = Time::now()+Time::sec;
194         }
195         else
196                 timeout = Time::TimeStamp();
197
198         demo = create_demo();
199         if(!options.no_music)
200                 demo->enable_music();
201         if(options.framerate)
202                 demo->set_fixed_framerate(options.framerate);
203         if(options.seek)
204                 demo->seek(options.seek);
205         demo->signal_finished.connect(sigc::bind(sigc::mem_fun(this, &LauncherBase::exit), 0));
206
207         if(!options.frame_dump_fn.empty())
208         {
209                 if(options.frame_dump_fn=="-")
210                         frame_dump = &IO::cout;
211                 else
212                         frame_dump = new IO::File(options.frame_dump_fn, IO::M_WRITE);
213                 frame_size = window.get_width()*window.get_height()*3;
214                 frame_dump_buffer = new char[frame_size];
215         }
216 }
217
218 void LauncherBase::key_press(unsigned key)
219 {
220         if(key==Input::KEY_ENTER && !demo)
221                 start_demo(false);
222         else if(key==Input::KEY_ESC)
223                 exit(0);
224 }
225
226 } // namespace DemoScene
227 } // namespace Msp