2 // Copyright (C) 2008 Albert Brown
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>
17 // engine_core: operates subsystems when registered. Controls timing
27 void engine_subsystem::tick(state
&s
)
29 // To be overloaded by derived subsystems
33 engine_core
*engine_core::_instance
= NULL
;
35 engine_core::engine_core(int argc
, char *argv
[])
37 // This is cleaner then waiting for the libraries to throw a fit
39 throw std::logic_error("Only one engine_core instance "
40 "at a time, please.");
42 // Init SDL (nothing window/video/audio related
43 if(SDL_Init(SDL_INIT_EVERYTHING
))
45 // Something went wrong
46 std::string msg
= std::string("SDL failed to initialize: ")
48 throw std::runtime_error(msg
.c_str());
51 std::cout
<< "engine_core initialized." << std::endl
;
55 engine_core::~engine_core()
61 std::cout
<< "engine_core shut down." << std::endl
;
64 static bool subsystem_order_sort(std::pair
<int, engine_subsystem
*> a
,
65 std::pair
<int, engine_subsystem
*> b
)
67 return (a
.first
< b
.first
);
70 void engine_core::register_subsystem(engine_subsystem
*sys
, int order
)
72 _subsystems
.push_back(std::pair
<int, engine_subsystem
*>(order
, sys
));
73 _subsystems
.sort(subsystem_order_sort
);
76 void engine_core::release_subsystem(engine_subsystem
*sys
)
78 std::list
<std::pair
<int, engine_subsystem
*> >::iterator it
;
80 for(it
= _subsystems
.begin(); it
!= _subsystems
.end(); ++it
)
82 if(sys
== (*it
).second
)
84 _subsystems
.erase(it
);
91 void engine_core::run()
93 // TODO: when we have a conf system, we can un-hardcode these values
94 _state
.running
= true;
100 //std::cout << _subsystems.size() << std::endl;
101 // we'll never get a signal to stop if there are no active subsystems
102 if(_subsystems
.empty())
104 std::cerr
<< "Run engine with no subsystems??" << std::endl
;
108 _state
.time
= SDL_GetTicks();
110 // Run each subsystem's 'tick' function
111 std::list
<std::pair
<int, engine_subsystem
*> >::iterator it
;
112 for(it
= _subsystems
.begin(); it
!= _subsystems
.end(); ++it
)
113 (*it
).second
->tick(_state
);
115 SDL_Delay(1000 / _state
.tickrate
);