![]() |
Kukatz 3D
0.1
Török Attila szakdolgozata
|
00001 /* 00002 * main.cpp - Kukatz 3D 00003 * Copyright (c) 2010, 2011 - TÖRÖK Attila (torokati44@gmail.com) 00004 * 00005 * This software is provided 'as-is', without any express or implied 00006 * warranty. In no event will the authors be held liable for any damages 00007 * arising from the use of this software. 00008 * 00009 * Permission is granted to anyone to use this software for any purpose, 00010 * including commercial applications, and to alter it and redistribute it 00011 * freely, subject to the following restrictions: 00012 * 00013 * 1. The origin of this software must not be misrepresented; you must not 00014 * claim that you wrote the original software. If you use this software 00015 * in a product, an acknowledgment in the product documentation would be 00016 * appreciated but is not required. 00017 * 00018 * 2. Altered source versions must be plainly marked as such, and must not be 00019 * misrepresented as being the original software. 00020 * 00021 * 3. This notice may not be removed or altered from any source 00022 * distribution. 00023 */ 00024 00025 #include <vector> 00026 #include <iostream> 00027 #include <fstream> 00028 00029 #include "mainmenu.hpp" 00030 #include "statemanager.hpp" 00031 #include "resourcemanager.hpp" 00032 #include "windowmanager.hpp" 00033 #include "configmanager.hpp" 00034 #include "scoremanager.hpp" 00035 #include "profilemanager.hpp" 00036 00037 #define MEASURE_FPS 1 00038 #define RECORD_FRAMETIMES 0 00039 #define FIX_UPDATE 1 00040 00041 int main() 00042 { 00043 sf::Clock main_clock; 00044 00045 #if RECORD_FRAMETIMES 00046 std::ofstream ft("frametimes.txt"); 00047 std::ofstream ut("updatetimes.txt"); 00048 std::ofstream sut("singleupdatetimes.txt"); 00049 std::ofstream rt("rendertimes.txt"); 00050 #endif 00051 00052 #if MEASURE_FPS 00053 float fps = 0; 00054 float fps_last_measured = 0; 00055 float fps_measure_interval = 2.0f; 00056 int frames_accum = 0; 00057 #endif 00058 00059 #if FIX_UPDATE 00060 float update_frequency = 64; 00061 float update_time_accum = 0; 00062 #endif 00063 00064 float time_rate = 1.0f; 00065 00066 float last_time; 00067 float current_time; 00068 00069 const ConfigManager* conf_mngr_ptr = new ConfigManager(); 00070 std::cout << "ConfigManager is initialized at " << main_clock.GetElapsedTime() << "\n"; 00071 00072 const ProfileManager* pr_mngr_ptr = new ProfileManager(); 00073 const ScoreManager* sc_mngr_ptr = new ScoreManager(); 00074 00075 const WindowManager* win_mngr_ptr = new WindowManager(); 00076 std::cout << "WindowManager is initialized at " << main_clock.GetElapsedTime() << "\n"; 00077 00078 const ResourceManager* rs_mngr_ptr = new ResourceManager(); 00079 std::cout << "ResourceManager is initialized at " << main_clock.GetElapsedTime() << "\n"; 00080 00081 const StateManager* st_mngr_ptr = new StateManager(); 00082 std::cout << "StateManager is initialized at " << main_clock.GetElapsedTime() << "\n"; 00083 00084 sf::Window& win = WindowManager::instance().get_window(); // simplifying 00085 std::cout << "RenderWindow is initialized at " << main_clock.GetElapsedTime() << "\n"; 00086 00087 StateManager::instance().push_state(new MainMenu(win.GetWidth(), win.GetHeight())); // should start with menu or something 00088 00089 sf::Event event; 00090 std::vector< sf::Event > events; 00091 00092 current_time = main_clock.GetElapsedTime(); 00093 last_time = current_time; 00094 00095 std::cout << "Main loop started at " << current_time << "\n"; 00096 // -------- MAIN LOOP ----------- 00097 while (true) // closing the window will exit this 00098 { 00099 events.clear(); 00100 while (win.GetEvent(event)) // events 00101 { 00102 switch (event.Type) 00103 { 00104 case sf::Event::Closed: 00105 { 00106 win.Close(); 00107 } 00108 break; 00109 case sf::Event::KeyPressed: 00110 { 00111 events.push_back(event); 00112 } 00113 break; 00114 case sf::Event::Resized: 00115 { 00116 StateManager::instance().resize_all(sf::Vector2i(event.Size.Width, event.Size.Height)); 00117 } 00118 break; 00119 default: 00120 { 00121 events.push_back(event); 00122 } 00123 break; 00124 } 00125 00126 if (!win.IsOpened()) 00127 { 00128 break; 00129 } 00130 } 00131 00132 if (!win.IsOpened()) 00133 { 00134 break; 00135 } 00136 00137 StateManager::instance().process_events(events); 00138 00139 last_time = current_time; 00140 current_time = main_clock.GetElapsedTime(); 00141 00142 #if RECORD_FRAMETIMES 00143 ft << (current_time - last_time) * 1000 << "\n"; 00144 float time_before_updates = main_clock.GetElapsedTime(); 00145 #endif 00146 00147 #if FIX_UPDATE 00148 update_time_accum += (current_time - last_time); 00149 while (update_time_accum >= (1.0 / update_frequency)) 00150 { 00151 #if RECORD_FRAMETIMES 00152 float time_before_this_update = main_clock.GetElapsedTime(); 00153 #endif 00154 StateManager::instance().update(1.0 / update_frequency * time_rate); 00155 #if RECORD_FRAMETIMES 00156 sut << (main_clock.GetElapsedTime() - time_before_this_update) * 1000 << "\n"; 00157 #endif 00158 update_time_accum -= (1.0 / update_frequency); 00159 events.clear(); 00160 if (StateManager::instance().is_empty()) 00161 { 00162 break; 00163 } 00164 } 00165 #else 00166 StateManager::instance().update((current_time - last_time) * time_rate); 00167 events.clear(); 00168 #endif 00169 00170 if (StateManager::instance().is_empty()) 00171 { 00172 break; 00173 } 00174 00175 #if RECORD_FRAMETIMES 00176 ut << (main_clock.GetElapsedTime() - time_before_updates) * 1000 << "\n"; 00177 float time_before_render = main_clock.GetElapsedTime(); 00178 #endif 00179 00180 StateManager::instance().render(); 00181 win.Display(); 00182 00183 #if RECORD_FRAMETIMES 00184 rt << (main_clock.GetElapsedTime() - time_before_render) * 1000 << "\n"; 00185 #endif 00186 00187 #if MEASURE_FPS 00188 ++frames_accum; 00189 00190 float time_now = main_clock.GetElapsedTime(); 00191 00192 if (time_now > fps_last_measured + fps_measure_interval) 00193 { 00194 fps = frames_accum / (time_now - fps_last_measured); 00195 00196 std::cout << "FPS = " << fps << "\n"; 00197 00198 fps_last_measured = time_now; 00199 frames_accum = 0; 00200 } 00201 #endif 00202 } 00203 00204 delete st_mngr_ptr; 00205 delete rs_mngr_ptr; 00206 delete win_mngr_ptr; 00207 delete pr_mngr_ptr; 00208 delete sc_mngr_ptr; 00209 delete conf_mngr_ptr; 00210 00211 std::cout << "Normal shutdown at " << main_clock.GetElapsedTime() << "\n"; 00212 00213 #if RECORD_FRAMETIMES 00214 ft.flush(); 00215 ut.flush(); 00216 sut.flush(); 00217 rt.flush(); 00218 00219 ft.close(); 00220 ut.close(); 00221 sut.close(); 00222 rt.close(); 00223 #endif 00224 00225 //std::cout << gluErrorString(glGetError()) << " in file " << __FILE__ << " at line " << __LINE__ << ".\n"; 00226 return 0; 00227 }