mirror of https://github.com/auygun/kaliber.git
CreateRenderer
This commit is contained in:
parent
2afe66857a
commit
a00110cc94
|
@ -37,20 +37,14 @@ Engine* Engine::singleton = nullptr;
|
|||
|
||||
Engine::Engine(Platform* platform)
|
||||
: platform_(platform),
|
||||
#if (USE_VULKAN_RENDERER == 1)
|
||||
renderer_{std::make_unique<RendererVulkan>()},
|
||||
#else
|
||||
renderer_{std::make_unique<RendererOpenGL>()},
|
||||
#endif
|
||||
audio_mixer_{std::make_unique<AudioMixer>()},
|
||||
quad_{CreateRenderResource<Geometry>()},
|
||||
pass_through_shader_{CreateRenderResource<Shader>()},
|
||||
solid_shader_{CreateRenderResource<Shader>()} {
|
||||
quad_{std::make_unique<Geometry>(nullptr)},
|
||||
pass_through_shader_{std::make_unique<Shader>(nullptr)},
|
||||
solid_shader_{std::make_unique<Shader>(nullptr)} {
|
||||
DCHECK(!singleton);
|
||||
singleton = this;
|
||||
|
||||
platform_->SetObserver(this);
|
||||
renderer_->SetContextLostCB(std::bind(&Engine::ContextLost, this));
|
||||
|
||||
stats_ = std::make_unique<ImageQuad>();
|
||||
}
|
||||
|
@ -74,7 +68,7 @@ Engine& Engine::Get() {
|
|||
}
|
||||
|
||||
void Engine::Run() {
|
||||
CHECK(Initialize()) << "Failed to initialize the engine.";
|
||||
Initialize();
|
||||
|
||||
timer_.Reset();
|
||||
float accumulator = 0.0;
|
||||
|
@ -105,10 +99,10 @@ void Engine::Run() {
|
|||
}
|
||||
}
|
||||
|
||||
bool Engine::Initialize() {
|
||||
void Engine::Initialize() {
|
||||
thread_pool_.Initialize();
|
||||
|
||||
InitializeRenderer();
|
||||
CreateRenderer(true);
|
||||
|
||||
// Normalize viewport.
|
||||
if (GetScreenWidth() > GetScreenHeight()) {
|
||||
|
@ -125,29 +119,16 @@ bool Engine::Initialize() {
|
|||
|
||||
LOG << "image scale factor: " << GetImageScaleFactor();
|
||||
|
||||
CreateTextureCompressors();
|
||||
|
||||
system_font_ = std::make_unique<Font>();
|
||||
system_font_->Load("engine/RobotoMono-Regular.ttf");
|
||||
|
||||
if (!CreateRenderResources())
|
||||
return false;
|
||||
|
||||
SetImageSource("stats_tex", std::bind(&Engine::PrintStats, this));
|
||||
stats_->SetZOrder(std::numeric_limits<int>::max());
|
||||
|
||||
game_ = GameFactoryBase::CreateGame("");
|
||||
if (!game_) {
|
||||
LOG << "No game found to run.";
|
||||
return false;
|
||||
}
|
||||
CHECK(game_) << "No game found to run.";
|
||||
|
||||
if (!game_->Initialize()) {
|
||||
LOG << "Failed to initialize the game.";
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
CHECK(game_->Initialize()) << "Failed to initialize the game.";
|
||||
}
|
||||
|
||||
void Engine::Update(float delta_time) {
|
||||
|
@ -216,26 +197,31 @@ void Engine::RemoveAnimator(Animator* animator) {
|
|||
}
|
||||
}
|
||||
|
||||
void Engine::SwitchRenderer(bool vulkan) {
|
||||
// Renderer* new_renderer = platform_->SwitchRenderer(vulkan);
|
||||
// if (new_renderer == renderer_)
|
||||
// return;
|
||||
void Engine::CreateRenderer(bool vulkan) {
|
||||
if ((dynamic_cast<RendererVulkan*>(renderer_.get()) && vulkan) ||
|
||||
(dynamic_cast<RendererOpenGL*>(renderer_.get()) && !vulkan))
|
||||
return;
|
||||
|
||||
// renderer_ = new_renderer;
|
||||
// renderer_->SetContextLostCB(std::bind(&Engine::ContextLost, this));
|
||||
// CreateTextureCompressors();
|
||||
if (vulkan)
|
||||
renderer_ = std::make_unique<RendererVulkan>();
|
||||
else
|
||||
renderer_ = std::make_unique<RendererOpenGL>();
|
||||
renderer_->SetContextLostCB(std::bind(&Engine::ContextLost, this));
|
||||
|
||||
// for (auto& t : textures_)
|
||||
// t.second.texture->SetRenderer(renderer_);
|
||||
bool result = InitializeRenderer();
|
||||
LOG_IF(!result) << "Failed to initialize " << renderer_->GetDebugName()
|
||||
<< " renderer.";
|
||||
if (!result && vulkan) {
|
||||
LOG << "Fallback to OpenGL renderer.";
|
||||
renderer_ = std::make_unique<RendererOpenGL>();
|
||||
renderer_->SetContextLostCB(std::bind(&Engine::ContextLost, this));
|
||||
result = InitializeRenderer();
|
||||
}
|
||||
CHECK(result) << "Failed to initialize " << renderer_->GetDebugName()
|
||||
<< " renderer.";
|
||||
|
||||
// for (auto& s : shaders_)
|
||||
// s.second.shader->SetRenderer(renderer_);
|
||||
|
||||
// quad_->SetRenderer(renderer_);
|
||||
// pass_through_shader_->SetRenderer(renderer_);
|
||||
// solid_shader_->SetRenderer(renderer_);
|
||||
|
||||
// ContextLost();
|
||||
CreateTextureCompressors();
|
||||
ContextLost();
|
||||
}
|
||||
|
||||
void Engine::Exit() {
|
||||
|
@ -560,15 +546,12 @@ void Engine::AddInputEvent(std::unique_ptr<InputEvent> event) {
|
|||
input_queue_.push_back(std::move(event));
|
||||
}
|
||||
|
||||
void Engine::InitializeRenderer() {
|
||||
bool res;
|
||||
bool Engine::InitializeRenderer() {
|
||||
#if defined(__ANDROID__)
|
||||
res = renderer_->Initialize(platform_->GetWindow());
|
||||
return renderer_->Initialize(platform_->GetWindow());
|
||||
#elif defined(__linux__)
|
||||
res = renderer_->Initialize(platform_->GetDisplay(), platform_->GetWindow());
|
||||
return renderer_->Initialize(platform_->GetDisplay(), platform_->GetWindow());
|
||||
#endif
|
||||
CHECK(res) << "Failed to initialize " << renderer_->GetDebugName()
|
||||
<< " renderer.";
|
||||
}
|
||||
|
||||
void Engine::CreateTextureCompressors() {
|
||||
|
@ -594,24 +577,10 @@ void Engine::CreateTextureCompressors() {
|
|||
}
|
||||
|
||||
void Engine::ContextLost() {
|
||||
CreateRenderResources();
|
||||
quad_->SetRenderer(renderer_.get());
|
||||
pass_through_shader_->SetRenderer(renderer_.get());
|
||||
solid_shader_->SetRenderer(renderer_.get());
|
||||
|
||||
for (auto& t : textures_) {
|
||||
t.second.texture->Destroy();
|
||||
RefreshImage(t.first);
|
||||
}
|
||||
|
||||
for (auto& s : shaders_) {
|
||||
auto source = std::make_unique<ShaderSource>();
|
||||
if (source->Load(s.second.file_name))
|
||||
s.second.shader->Create(std::move(source), quad_->vertex_description(),
|
||||
quad_->primitive(), false);
|
||||
}
|
||||
|
||||
game_->ContextLost();
|
||||
}
|
||||
|
||||
bool Engine::CreateRenderResources() {
|
||||
// This creates a normalized unit sized quad.
|
||||
static const char vertex_description[] = "p2f;t2f";
|
||||
static const float vertices[] = {
|
||||
|
@ -626,23 +595,37 @@ bool Engine::CreateRenderResources() {
|
|||
|
||||
// Create the shader we can reuse for texture rendering.
|
||||
auto source = std::make_unique<ShaderSource>();
|
||||
if (!source->Load("engine/pass_through.glsl")) {
|
||||
LOG << "Could not create pass through shader.";
|
||||
return false;
|
||||
}
|
||||
if (source->Load("engine/pass_through.glsl")) {
|
||||
pass_through_shader_->Create(std::move(source), quad_->vertex_description(),
|
||||
quad_->primitive(), false);
|
||||
} else {
|
||||
LOG << "Could not create pass through shader.";
|
||||
}
|
||||
|
||||
// Create the shader we can reuse for solid rendering.
|
||||
source = std::make_unique<ShaderSource>();
|
||||
if (!source->Load("engine/solid.glsl")) {
|
||||
LOG << "Could not create solid shader.";
|
||||
return false;
|
||||
}
|
||||
if (source->Load("engine/solid.glsl")) {
|
||||
solid_shader_->Create(std::move(source), quad_->vertex_description(),
|
||||
quad_->primitive(), false);
|
||||
} else {
|
||||
LOG << "Could not create solid shader.";
|
||||
}
|
||||
|
||||
return true;
|
||||
for (auto& t : textures_) {
|
||||
t.second.texture->SetRenderer(renderer_.get());
|
||||
RefreshImage(t.first);
|
||||
}
|
||||
|
||||
for (auto& s : shaders_) {
|
||||
s.second.shader->SetRenderer(renderer_.get());
|
||||
auto source = std::make_unique<ShaderSource>();
|
||||
if (source->Load(s.second.file_name))
|
||||
s.second.shader->Create(std::move(source), quad_->vertex_description(),
|
||||
quad_->primitive(), false);
|
||||
}
|
||||
|
||||
if (game_)
|
||||
game_->ContextLost();
|
||||
}
|
||||
|
||||
void Engine::SetSatsVisible(bool visible) {
|
||||
|
|
|
@ -49,7 +49,7 @@ class Engine : public PlatformObserver {
|
|||
void AddAnimator(Animator* animator);
|
||||
void RemoveAnimator(Animator* animator);
|
||||
|
||||
void SwitchRenderer(bool vulkan);
|
||||
void CreateRenderer(bool vulkan);
|
||||
|
||||
void Exit();
|
||||
|
||||
|
@ -218,7 +218,7 @@ class Engine : public PlatformObserver {
|
|||
base::Timer timer_;
|
||||
base::Randomf random_;
|
||||
|
||||
bool Initialize();
|
||||
void Initialize();
|
||||
|
||||
void Update(float delta_time);
|
||||
void Draw(float frame_frac);
|
||||
|
@ -231,14 +231,12 @@ class Engine : public PlatformObserver {
|
|||
void GainedFocus(bool from_interstitial_ad) final;
|
||||
void AddInputEvent(std::unique_ptr<InputEvent> event) final;
|
||||
|
||||
void InitializeRenderer();
|
||||
bool InitializeRenderer();
|
||||
|
||||
void CreateTextureCompressors();
|
||||
|
||||
void ContextLost();
|
||||
|
||||
bool CreateRenderResources();
|
||||
|
||||
void SetSatsVisible(bool visible);
|
||||
std::unique_ptr<Image> PrintStats();
|
||||
|
||||
|
|
Loading…
Reference in New Issue