Это для моей университетской курсовой работы.
У меня есть класс с именем timestep
, который будет использоваться в качестве типичного таймера в игровом движке для расчета времени кадра, и т. Д. c, и application
class.
Я изо всех сил пытаюсь разобраться с общими указателями, но мне нужно использовать один для доступа к классу timestep
из класса application
. Он не выдает ошибок до тех пор, пока программа не запустится, после чего он печатает мой журнал "PRE TIMER"
в консоль и выдает исключение после достижения timer->setStart()
, помечает строку start = .....
в методе setStart
, и говорит **this** was nullptr
.
Timestep.h:
#pragma once
#include <chrono>
namespace Engine {
class Timestep {
private:
std::chrono::high_resolution_clock::time_point start;
std::chrono::high_resolution_clock::time_point end;
public:
Timestep();
void setStart();
void setEnd();
float getTimeSeconds() const;
float GetTimeMilliSeconds() const;
};
}
timestep. cpp:
#pragma once
#include "engine_pch.h"
#include "core/timestep.h"
namespace Engine {
Timestep::Timestep(){}
void Timestep::setStart() {
start = std::chrono::high_resolution_clock::now();
}
void Timestep::setEnd() {
end = std::chrono::high_resolution_clock::now();
}
float Timestep::getTimeSeconds() const {
std::chrono::duration<float> time = end - start;
return time.count();
}
float Timestep::GetTimeMilliSeconds() const {
std::chrono::duration<float, std::milli> time = end - start;
return time.count();
}
}
application. cpp:
#include "engine_pch.h"
#include "core/application.h"
namespace Engine {
Application* Application::s_instance = nullptr;
std::shared_ptr<Timestep> timer;
Application::Application()
{
if (s_instance == nullptr)
{
s_instance = this;
}
log::log();
LOG_INFO("Logger init success");
}
Application::~Application()
{
}
void Application::run()
{
LOG_INFO("PRE TIMER");
timer->setStart();
LOG_INFO("POST TIMER");
while (s_instance) {
timer->setEnd();
float a = timer->getTimeSeconds();
LOG_INFO("Time since last frame is {0}", a);
timer->setStart();
}
}
}