1 // -*- Mode: C++; tab-width:2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi:tw=80:et:ts=2:sts=2
3 //
4 // -----------------------------------------------------------------------
5 //
6 // This file is part of RLVM, a RealLive virtual machine clone.
7 //
8 // -----------------------------------------------------------------------
9 //
10 // Copyright (C) 2011 Elliot Glaysher
11 //
12 // This program is free software; you can redistribute it and/or modify
13 // it under the terms of the GNU General Public License as published by
14 // the Free Software Foundation; either version 3 of the License, or
15 // (at your option) any later version.
16 //
17 // This program is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 // GNU General Public License for more details.
21 //
22 // You should have received a copy of the GNU General Public License
23 // along with this program; if not, write to the Free Software
24 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
25 // -----------------------------------------------------------------------
26 
27 #include "long_operations/load_game_long_operation.h"
28 
29 #include <memory>
30 
31 #include "effects/fade_effect.h"
32 #include "machine/rlmachine.h"
33 #include "systems/base/colour.h"
34 #include "systems/base/graphics_system.h"
35 #include "systems/base/surface.h"
36 #include "systems/base/system.h"
37 
LoadGameLongOperation(RLMachine & machine)38 LoadGameLongOperation::LoadGameLongOperation(RLMachine& machine) {
39   // Render the current state of the screen
40   GraphicsSystem& graphics = machine.system().graphics();
41 
42   std::shared_ptr<Surface> currentWindow = graphics.RenderToSurface();
43   Size s = currentWindow->GetSize();
44 
45   // Blank dc0 (because we won't be using it anyway) for the image
46   // we're going to render to
47   std::shared_ptr<Surface> dc0 = graphics.GetDC(0);
48   dc0->Fill(RGBAColour::Black());
49 
50   machine.PushLongOperation(this);
51   machine.PushLongOperation(
52       new FadeEffect(machine, dc0, currentWindow, s, 250));
53 
54   // We have our before and after images to use as a transition now. Reset the
55   // system to prevent a brief flash of the previous contents of the screen for
56   // whatever number of user preceivable milliseconds.
57   machine.system().Reset();
58 }
59 
operator ()(RLMachine & machine)60 bool LoadGameLongOperation::operator()(RLMachine& machine) {
61   Load(machine);
62   // Warning: the stack has now been nuked and |this| is an invalid.
63 
64   // Render the current state of the screen
65   GraphicsSystem& graphics = machine.system().graphics();
66 
67   std::shared_ptr<Surface> currentWindow = graphics.RenderToSurface();
68   Size s = currentWindow->GetSize();
69 
70   // Blank dc0 (because we won't be using it anyway) for the image
71   // we're going to render to
72   std::shared_ptr<Surface> blankScreen = graphics.BuildSurface(s);
73   blankScreen->Fill(RGBAColour::Black());
74 
75   machine.PushLongOperation(
76       new FadeEffect(machine, currentWindow, blankScreen, s, 250));
77 
78   // At this point, the stack has been nuked, and this current
79   // object has already been deleted, leaving an invalid
80   // *this. Returning false is the correct thing to do since
81   // *returning true will pop an unrelated stack frame.
82   return false;
83 }
84