Debian Codegraph @

CorsixTH/Lua/movie_player.lua

raw
1 --[[ Copyright (c) 2012 Stephen Baker
2
3 Permission is hereby granted, free of charge, to any person obtaining a copy of
4 this software and associated documentation files (the "Software"), to deal in
5 the Software without restriction, including without limitation the rights to
6 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7 of the Software, and to permit persons to whom the Software is furnished to do
8 so, subject to the following conditions:
9
10 The above copyright notice and this permission notice shall be included in all
11 copies or substantial portions of the Software.
12
13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 SOFTWARE. --]]
20
21 --! Layer which handles the Lua-facing side of loading and playing video.
22
23 local TH = require("TH")
24 local pathsep = package.config:sub(1, 1)
25
26 class "MoviePlayer"
27
28 ---@type MoviePlayer
29 local MoviePlayer = _G["MoviePlayer"]
30
31 --! Calculate the position and size for a movie
32 --!
33 --! Returns x and y position, width and height, and the scale factor of the
34 --! movie to be displayed based on the native size of the movie and the current
35 --! screen dimensions.
36 --!
37 --! Theme Hospital movies were 320x240 but played in game at 640x480 so when
38 --! scaling other assets to match the movies they should be scaled by half
39 --! the returned scaling factor. Some movies were 320x200 and those should
40 --! be aspect corrected to 4:3 during scaling.
41 --!
42 --!param me The MoviePlayer object
43 local calculateSize = function(me)
44 -- calculate target dimensions
45 local x, y, w, h, scale
46 local screen_w, screen_h = me.app.config.width, me.app.config.height
47 local native_w = me.moviePlayer:getNativeWidth()
48 local native_h = me.moviePlayer:getNativeHeight()
49 if native_w == 320 and native_h == 200 then
50 -- This resolution was intended to be stretched for a 4:3 screen
51 native_h = 240
52 end
53 if native_w ~= 0 and native_h ~= 0 then
54 local ar = native_w / native_h
55 if math.abs((screen_w / screen_h) - ar) < 0.001 then
56 x, y = 0, 0
57 w, h = screen_w, screen_h
58 scale = screen_h / native_h
59 else
60 if screen_w > screen_h / native_h * native_w then
61 w = math.floor(screen_h / native_h * native_w)
62 h = screen_h
63 x = math.floor((screen_w - w) / 2)
64 y = 0
65 scale = screen_h / native_h
66 else
67 w = screen_w
68 h = math.floor(screen_w / native_w * native_h)
69 x = 0
70 y = math.floor((screen_h - h) / 2)
71 scale = screen_w / native_w
72 end
73 end
74 else
75 x, y = 0, 0
76 w, h = screen_w, screen_h
77 scale = 1
78 end
79
80 return x, y, w, h, scale
81 end
82
83 local destroyMovie = function(me)
84 me.moviePlayer:unload()
85 if me.opengl_mode_index then
86 me.app.modes[me.opengl_mode_index] = "opengl"
87 end
88 if me.channel >= 0 then
89 me.audio:releaseChannel(me.channel)
90 me.channel = -1
91 end
92 if me.holding_bg_music then
93 -- If possible we want to continue playing music where we were
94 me.audio:pauseBackgroundTrack()
95 me.holding_bg_music = false
96 end
97 if me.sound then
98 me.audio:stopSound(me.sound)
99 me.sound = nil
100 end
101 me.refresh_overlay = nil
102 me.playing = false
103 me.movie_over = false
104 if me.callback_on_destroy_movie then
105 me.callback_on_destroy_movie()
106 me.callback_on_destroy_movie = nil
107 end
108 end
109
110 local loseMovieOverlay = function(me, lose_movie_index)
111
112 -- These are the times in milliseconds when the headline should appear as
113 -- determined experimentally
114 local start_movie_pts = {
115 me.movie_length - 1760,
116 me.movie_length - 860,
117 me.movie_length - 1600,
118 me.movie_length - 1000,
119 me.movie_length - 860,
120 me.movie_length - 780,
121 }
122
123 local start_pts = start_movie_pts[lose_movie_index]
124 local headlines = _S.newspaper[lose_movie_index]
125 local headline = headlines[math.random(#headlines)]
126
127 -- The LOSE movies are 320x240 but are drawn in the original game at 640x480
128 -- before drawing the font over them. The headline area at double size is 40px
129 -- tall and 590px wide if we exclude the padding which has no other ink. If we
130 -- let the headline fill the entire width of the paper it could be 618px wide.
131 --
132 -- The widest original english headline is headline 4 for LOSE5 which is 708px
133 -- wide in standard font and 520px wide in narrow font.
134 local hl_font = me.lose_font
135 local hl_top = 132
136 local hl_w, hl_h = me.lose_font:sizeOf(headline)
137 if me.lose_font:isBitmap() then
138 if hl_w > 590 then
139 hl_font = me.lose_font_narrow
140
141 -- The narrow font and standard font have a different baseline so we need
142 -- to adjust the top.
143 hl_top = 138
144 end
145 else
146 -- For TTF fonts we try to vertically center them in the headline area
147 hl_top = 132 + math.floor((52 - hl_h) / 2)
148 end
149
150 return function(player, x, y, w, h, scale, pts)
151 if pts >= start_pts then
152 local vs = scale * 0.5
153 player.video:scale(vs)
154 hl_font:draw(player.video, headline, math.floor(x / vs), math.floor(y / vs) + hl_top, 640, 0, "center")
155 player.video:scale(1)
156 end
157 end
158 end
159
160 function MoviePlayer:MoviePlayer(app, audio, video)
161 self.app = app
162 self.audio = audio
163 self.video = video
164 self.playing = false
165 self.holding_bg_music = false
166 self.channel = -1
167 self.sound = nil
168 self.lose_movies = {}
169 self.advance_movies = {}
170 self.intro_movie = nil
171 self.demo_movie = nil
172 self.win_game_movie = nil
173 self.win_level_movie = nil
174 self.wait_for_stop = false
175 self.wait_for_over = false
176 self.movie_over = false
177 self.movie_length = 0
178 self.lose_font = nil
179 self.lose_font_narrow = nil
180 end
181
182 --! Initialises the different movies used in the game
183 function MoviePlayer:init()
184 self.moviePlayer = TH.moviePlayer()
185 self.moviePlayer:setRenderer(self.video)
186
187 local lose_palette = self.app.gfx:getPalette("lose.pl8")
188 self.lose_font = self.app.gfx:loadFontAndSpriteTable("QData", "Font39v", false, lose_palette)
189 self.lose_font_narrow = self.app.gfx:loadFontAndSpriteTable("QData", "Font40v", false, lose_palette)
190
191 --find movies in Anims folder
192 local fs = self.app.fs
193 local movies = fs:listFiles("Anims")
194 if movies then
195 for _, movie in pairs(movies) do
196 local num
197
198 --lose level movies
199 num = movie:upper():match(pathsep .. "LOSE(%d+)%.[^" .. pathsep .. "]+$")
200 if num then
201 self.lose_movies[tonumber(num, 10)] = fs:fileUri(movie)
202 end
203
204 --advance level movies
205 num = movie:upper():match(pathsep .. "AREA(%d+)V%.[^" .. pathsep .. "]+$")
206 if num then
207 self.advance_movies[tonumber(num, 10)] = fs:fileUri(movie)
208 end
209
210 --win game movie
211 if movie:upper():match(pathsep .. "WINGAME%.[^" .. pathsep .. "]+$") then
212 self.win_game_movie = fs:fileUri(movie)
213 end
214
215 --win level movie
216 if movie:upper():match(pathsep .. "WINLEVEL%.[^" .. pathsep .. "]+$") then
217 self.win_level_movie = fs:fileUri(movie)
218 end
219 end
220 end
221
222 --find intro and demo movies
223 movies = self.app.fs:listFiles("Intro")
224 if movies then
225 for _, movie in pairs(movies) do
226 if movie:upper():match(pathsep .. "INTRO%.SM4$") then
227 self.intro_movie = fs:fileUri(movie)
228 elseif movie:upper():match(pathsep .. "ATTRACT.SMK$") then
229 self.demo_movie = fs:fileUri(movie)
230 end
231 end
232 end
233 end
234
235 --! Plays the opening movie from TH
236 --!param callback_after_movie (function) What to do once movie ends
237 function MoviePlayer:playIntro(callback_after_movie)
238 self:playMovie(self.intro_movie, false, callback_after_movie)
239 end
240
241 --! Plays the demo gameplay footage movie from TH
242 function MoviePlayer:playDemoMovie()
243 self:playMovie(self.demo_movie, false)
244 end
245
246 --! Plays the movie for winning the game
247 function MoviePlayer:playWinMovie()
248 self:playMovie(self.win_game_movie, false)
249 end
250
251 --! Play the movie for winning the level
252 function MoviePlayer:playWinLevelMovie()
253 self:playMovie(self.win_level_movie, true)
254 end
255
256 --! Plays the level advance movie, which is going to the next level on the game board
257 --! This is for the original campaign only.
258 --!param level (number) What level we're going to.
259 function MoviePlayer:playAdvanceMovie(level)
260 local filename = self.advance_movies[level]
261
262 if self.moviePlayer == nil or not self.moviePlayer:getEnabled() or
263 not self.app.config.movies or filename == nil then
264 return
265 end
266
267 if self.audio.background_music then
268 self.holding_bg_music = self.audio:pauseBackgroundTrack()
269 end
270
271 if level == 12 then
272 self.sound = self.audio:playSound("DICE122M.WAV")
273 else
274 self.sound = self.audio:playSound("DICEYFIN.WAV")
275 end
276 self:playMovie(filename, true)
277 end
278
279 --! Plays one of the lose scenario movies at random
280 function MoviePlayer:playLoseMovie()
281 if #self.lose_movies > 0 then
282 local lose_movie_index = math.random(#self.lose_movies)
283 local filename = self.lose_movies[lose_movie_index]
284 self:playMovie(filename, true)
285 self.refresh_overlay = loseMovieOverlay(self, lose_movie_index)
286 end
287 end
288
289 --! Function used to tell the Movie Player to play something.
290 --!param filename (string) Location of the movie file
291 --!param wait_for_stop (boolean) If true, movie will not dismiss automatically
292 --! (requires a mouse/key press)
293 --!param callback (function) What to do after the movie ends
294 function MoviePlayer:playMovie(filename, wait_for_stop, callback)
295 local success, warning
296
297 if self.moviePlayer == nil or not self.moviePlayer:getEnabled() or
298 not self.app.config.movies or filename == nil then
299 if callback then
300 callback()
301 end
302 return
303 end
304
305 success, warning = self.moviePlayer:load(filename)
306 if warning ~= nil and warning ~= "" then
307 local message = "MoviePlayer:playMovie - Warning: " .. warning
308 if self.app.world then
309 self.app.world:gameLog(message)
310 elseif self.app.config.debug then
311 print(message)
312 end
313 end
314 if not success then
315 -- Indicates failure to load movie
316 if callback then
317 callback()
318 end
319 return
320 end
321 -- Abort any loading of music
322 self.audio.load_music = false
323 if self.moviePlayer:hasAudioTrack() then
324 self.channel = self.audio:reserveChannel()
325 if self.audio.background_music then
326 self.holding_bg_music = self.audio:pauseBackgroundTrack()
327 end
328 end
329
330 self.movie_length = self.moviePlayer:getLength()
331
332 self.video:startFrame()
333 self.video:fillBlack()
334 self.video:endFrame()
335
336 self.wait_for_stop = wait_for_stop
337 self.wait_for_over = true
338
339 self.callback_on_destroy_movie = callback
340
341 self.opengl_mode_index = nil
342 for i=1, #self.app.modes do
343 if self.app.modes[i] == "opengl" then
344 self.opengl_mode_index = i
345 end
346 end
347 if self.opengl_mode_index then
348 self.app.modes[self.opengl_mode_index] = ""
349 end
350
351 warning = self.moviePlayer:play(self.channel)
352 if warning ~= nil and warning ~= "" then
353 local message = "MoviePlayer:playMovie - Warning: " .. warning
354 if self.app.world then
355 self.app.world:gameLog(message)
356 elseif self.app.config.debug then
357 print(message)
358 end
359 end
360 self.playing = true
361 end
362
363 --NB: Call after any changes to TH.surface
364 function MoviePlayer:allocatePictureBuffer()
365 if self.moviePlayer == nil then return end
366
367 self.moviePlayer:allocatePictureBuffer()
368 end
369
370 --NB: Call before any changes to TH.surface
371 function MoviePlayer:deallocatePictureBuffer()
372 if self.moviePlayer == nil then return end
373
374 self.moviePlayer:deallocatePictureBuffer()
375 end
376
377 --! Handles when the movie ends
378 function MoviePlayer:onMovieOver()
379 if self.moviePlayer == nil then return end
380
381 self.movie_over = true
382 self.wait_for_over = false
383 if not self.wait_for_stop then
384 destroyMovie(self)
385 end
386 end
387
388 --! Handles ending the movie prematurely (user input)
389 function MoviePlayer:stop()
390 if self.moviePlayer == nil then return end
391
392 self.moviePlayer:stop()
393 self.wait_for_stop = false
394 if not self.wait_for_over then
395 destroyMovie(self)
396 end
397 end
398
399 function MoviePlayer:refresh()
400 if self.moviePlayer == nil then return end
401
402 local x, y, w, h, scale = calculateSize(self)
403 local pts = self.moviePlayer:refresh(x, y, w, h)
404 if self.refresh_overlay then
405 self.refresh_overlay(self, x, y, w, h, scale, pts)
406 end
407 end
408
409 function MoviePlayer:updateRenderer()
410 if self.moviePlayer == nil then return end
411
412 self.moviePlayer:setRenderer(self.video)
413 end
414
415 function MoviePlayer:togglePause()
416 if self.moviePlayer == nil then return end
417
418 self.moviePlayer:togglePause()
419 if self.sound then
420 self.audio:togglePauseSound(self.sound)
421 end
422 end