#include #define WIN32_LEAN_AND_MEAN #include #include #include #include #include "DuvelEngine.h" #include "InputManager.h" #include "SceneManager.h" #include "Renderer.h" #include "ResourceManager.h" #include SDL_Window* g_window{}; void PrintSDLVersion() { SDL_version version{}; SDL_VERSION(&version); printf("We compiled against SDL version %u.%u.%u ...\n", version.major, version.minor, version.patch); SDL_GetVersion(&version); printf("We are linking against SDL version %u.%u.%u.\n", version.major, version.minor, version.patch); SDL_IMAGE_VERSION(&version); printf("We compiled against SDL_image version %u.%u.%u ...\n", version.major, version.minor, version.patch); version = *IMG_Linked_Version(); printf("We are linking against SDL_image version %u.%u.%u.\n", version.major, version.minor, version.patch); SDL_TTF_VERSION(&version) printf("We compiled against SDL_ttf version %u.%u.%u ...\n", version.major, version.minor, version.patch); version = *TTF_Linked_Version(); printf("We are linking against SDL_ttf version %u.%u.%u.\n", version.major, version.minor, version.patch); } dae::DuvelEngine::DuvelEngine(const std::string &dataPath) { PrintSDLVersion(); if (SDL_Init(SDL_INIT_VIDEO) != 0) { throw std::runtime_error(std::string("SDL_Init Error: ") + SDL_GetError()); } g_window = SDL_CreateWindow( "Prog4Exam - BubbleBobble - 2DAE08 Yomtov Nir", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 256 * 2, 224 * 2, SDL_WINDOW_OPENGL ); if (g_window == nullptr) { throw std::runtime_error(std::string("SDL_CreateWindow Error: ") + SDL_GetError()); } Renderer::GetInstance().Init(g_window); ResourceManager::GetInstance().Init(dataPath); } dae::DuvelEngine::~DuvelEngine() { Renderer::GetInstance().Destroy(); SDL_DestroyWindow(g_window); g_window = nullptr; SDL_Quit(); } void dae::DuvelEngine::Run(const std::function& load) { load(); auto& renderer = Renderer::GetInstance(); auto& sceneManager = SceneManager::GetInstance(); auto& input = InputManager::GetInstance(); // todo: this update loop could use some work. bool doContinue = true; auto lastTime{ std::chrono::high_resolution_clock::now() }; const int frameTimeMs{ int(1000 / m_TargetFps) }; float lag{}; while (doContinue) { const auto currentTime{ std::chrono::high_resolution_clock::now() }; const float deltaTime = std::chrono::duration(currentTime - lastTime).count(); lastTime = currentTime; lag += deltaTime; doContinue = input.ProcessInput(sceneManager.GetActiveSceneId()); while (lag >= m_FixedTimeStep) { sceneManager.FixedUpdate(m_FixedTimeStep); lag -= m_FixedTimeStep; } sceneManager.Update(deltaTime); sceneManager.LateUpdate(deltaTime); renderer.Render(); sceneManager.HandleOwnershipChanges(); sceneManager.HandleDeletions(); const auto sleepTime = currentTime + std::chrono::milliseconds(frameTimeMs) - std::chrono::high_resolution_clock::now(); std::this_thread::sleep_for(sleepTime); } }