Kind of implemented engine_core loop. Implemented subsystem registration. Implement...
[plastic-engine.git] / src / engine.cpp
blob172ed0cbfa6a98f76bc23215ce578089d6c50eb8
1 // Plastic Engine
2 // Copyright (C) 2008 Albert Brown
3 //
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.
8 //
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
19 #include <stdexcept>
20 #include <iostream>
21 #include <string>
22 #include "SDL.h"
24 #include "engine.hpp"
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
38 if(_instance)
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: ")
47 + SDL_GetError();
48 throw std::runtime_error(msg.c_str());
51 std::cout << "engine_core initialized." << std::endl;
52 _instance = this;
55 engine_core::~engine_core()
57 // bring down SDL
58 SDL_Quit();
60 _instance = NULL;
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);
85 break;
91 void engine_core::run()
93 // TODO: when we have a conf system, we can un-hardcode these values
94 _state.running = true;
95 _state.tickrate = 15;
96 _state.speed = 100;
98 while(_state.running)
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;
105 break;
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);