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) 2006, 2007 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 
28 #include "systems/sdl/sdl_system.h"
29 
30 #include <SDL/SDL.h>
31 
32 #include <sstream>
33 
34 #include "libreallive/defs.h"
35 #include "libreallive/gameexe.h"
36 #include "machine/rlmachine.h"
37 #include "systems/base/graphics_object.h"
38 #include "systems/base/graphics_object_data.h"
39 #include "systems/base/platform.h"
40 #include "systems/sdl/sdl_event_system.h"
41 #include "systems/sdl/sdl_graphics_system.h"
42 #include "systems/sdl/sdl_sound_system.h"
43 #include "systems/sdl/sdl_text_system.h"
44 
45 // -----------------------------------------------------------------------
46 
SDLSystem(Gameexe & gameexe)47 SDLSystem::SDLSystem(Gameexe& gameexe) : System(), gameexe_(gameexe) {
48   // First, initialize SDL's video subsystem.
49   if (SDL_Init(SDL_INIT_VIDEO) < 0) {
50     std::ostringstream ss;
51     ss << "Video initialization failed: " << SDL_GetError();
52     throw libreallive::Error(ss.str());
53   }
54 
55   // Initialize the various subsystems
56   graphics_system_.reset(new SDLGraphicsSystem(*this, gameexe));
57   event_system_.reset(new SDLEventSystem(*this, gameexe));
58   text_system_.reset(new SDLTextSystem(*this, gameexe));
59   sound_system_.reset(new SDLSoundSystem(*this));
60 
61   event_system_->AddMouseListener(graphics_system_.get());
62   event_system_->AddMouseListener(text_system_.get());
63 }
64 
65 // -----------------------------------------------------------------------
66 
~SDLSystem()67 SDLSystem::~SDLSystem() {
68   event_system_->RemoveMouseListener(text_system_.get());
69   event_system_->RemoveMouseListener(graphics_system_.get());
70 
71   // Some combinations of SDL and FT on the Mac require us to destroy the
72   // Platform first. This will crash on Tiger if this isn't here, but it won't
73   // crash under Linux...
74   platform_.reset();
75 
76   // Force the deletion of the various systems before we shut down
77   // SDL.
78   sound_system_.reset();
79   graphics_system_.reset();
80   event_system_.reset();
81   text_system_.reset();
82 
83   SDL_Quit();
84 }
85 
86 // -----------------------------------------------------------------------
87 
Run(RLMachine & machine)88 void SDLSystem::Run(RLMachine& machine) {
89   // Give the event handler a chance to run.
90   event_system_->ExecuteEventSystem(machine);
91   text_system_->ExecuteTextSystem();
92   sound_system_->ExecuteSoundSystem();
93   graphics_system_->ExecuteGraphicsSystem(machine);
94 
95   if (platform())
96     platform()->Run(machine);
97 }
98 
99 // -----------------------------------------------------------------------
100 
graphics()101 GraphicsSystem& SDLSystem::graphics() { return *graphics_system_; }
102 
103 // -----------------------------------------------------------------------
104 
event()105 EventSystem& SDLSystem::event() { return *event_system_; }
106 
107 // -----------------------------------------------------------------------
108 
gameexe()109 Gameexe& SDLSystem::gameexe() { return gameexe_; }
110 
111 // -----------------------------------------------------------------------
112 
text()113 SDLTextSystem& SDLSystem::text() { return *text_system_; }
114 
115 // -----------------------------------------------------------------------
116 
sound()117 SoundSystem& SDLSystem::sound() { return *sound_system_; }
118 
getSDLGraphics(System & system)119 SDLGraphicsSystem* getSDLGraphics(System& system) {
120   return static_cast<SDLGraphicsSystem*>(&system.graphics());
121 }
122