mirror of https://github.com/auygun/kaliber.git
Code cleanup
This commit is contained in:
parent
0866349f58
commit
0bb653d985
|
@ -420,7 +420,7 @@ void Enemy::StopAllEnemyUnits(bool chromatic_aberration_effect) {
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (chromatic_aberration_effect) {
|
if (chromatic_aberration_effect) {
|
||||||
e.sprite.SetCustomShader(chromatic_aberration_);
|
e.sprite.SetCustomShader(chromatic_aberration_.get());
|
||||||
e.chromatic_aberration_active_ = true;
|
e.chromatic_aberration_active_ = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -106,7 +106,7 @@ class Enemy {
|
||||||
eng::SoundPlayer hit;
|
eng::SoundPlayer hit;
|
||||||
};
|
};
|
||||||
|
|
||||||
std::shared_ptr<eng::Shader> chromatic_aberration_;
|
std::unique_ptr<eng::Shader> chromatic_aberration_;
|
||||||
float chromatic_aberration_offset_ = 0;
|
float chromatic_aberration_offset_ = 0;
|
||||||
|
|
||||||
eng::ImageQuad boss_;
|
eng::ImageQuad boss_;
|
||||||
|
|
|
@ -37,13 +37,10 @@ Engine::Engine(Platform* platform, Renderer* renderer, Audio* audio)
|
||||||
solid_shader_ = CreateRenderResource<Shader>();
|
solid_shader_ = CreateRenderResource<Shader>();
|
||||||
|
|
||||||
stats_ = std::make_unique<ImageQuad>();
|
stats_ = std::make_unique<ImageQuad>();
|
||||||
stats_->SetZOrder(std::numeric_limits<int>::max());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Engine::~Engine() {
|
Engine::~Engine() {
|
||||||
game_.reset();
|
|
||||||
stats_.reset();
|
stats_.reset();
|
||||||
|
|
||||||
singleton = nullptr;
|
singleton = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -91,6 +88,7 @@ bool Engine::Initialize() {
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
SetImageSource("stats_tex", std::bind(&Engine::PrintStats, this));
|
SetImageSource("stats_tex", std::bind(&Engine::PrintStats, this));
|
||||||
|
stats_->SetZOrder(std::numeric_limits<int>::max());
|
||||||
|
|
||||||
game_ = GameFactoryBase::CreateGame("");
|
game_ = GameFactoryBase::CreateGame("");
|
||||||
if (!game_) {
|
if (!game_) {
|
||||||
|
@ -108,6 +106,9 @@ bool Engine::Initialize() {
|
||||||
|
|
||||||
void Engine::Shutdown() {
|
void Engine::Shutdown() {
|
||||||
LOG << "Shutting down engine.";
|
LOG << "Shutting down engine.";
|
||||||
|
game_.reset();
|
||||||
|
stats_->Destory();
|
||||||
|
textures_.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Engine::Update(float delta_time) {
|
void Engine::Update(float delta_time) {
|
||||||
|
@ -119,12 +120,6 @@ void Engine::Update(float delta_time) {
|
||||||
|
|
||||||
game_->Update(delta_time);
|
game_->Update(delta_time);
|
||||||
|
|
||||||
// Destroy unused textures.
|
|
||||||
for (auto& t : textures_) {
|
|
||||||
if (!t.second.persistent && t.second.texture.use_count() == 1)
|
|
||||||
t.second.texture->Destroy();
|
|
||||||
}
|
|
||||||
|
|
||||||
fps_seconds_ += delta_time;
|
fps_seconds_ += delta_time;
|
||||||
if (fps_seconds_ >= 1) {
|
if (fps_seconds_ >= 1) {
|
||||||
fps_ = renderer_->GetAndResetFPS();
|
fps_ = renderer_->GetAndResetFPS();
|
||||||
|
@ -227,15 +222,16 @@ void Engine::SetImageSource(const std::string& asset_name,
|
||||||
void Engine::SetImageSource(const std::string& asset_name,
|
void Engine::SetImageSource(const std::string& asset_name,
|
||||||
CreateImageCB create_image,
|
CreateImageCB create_image,
|
||||||
bool persistent) {
|
bool persistent) {
|
||||||
std::shared_ptr<Texture> texture;
|
Texture* texture;
|
||||||
auto it = textures_.find(asset_name);
|
auto it = textures_.find(asset_name);
|
||||||
if (it != textures_.end()) {
|
if (it != textures_.end()) {
|
||||||
texture = it->second.texture;
|
texture = it->second.texture.get();
|
||||||
it->second.create_image = create_image;
|
it->second.create_image = create_image;
|
||||||
it->second.persistent = persistent;
|
it->second.persistent = persistent;
|
||||||
} else {
|
} else {
|
||||||
texture = CreateRenderResource<Texture>();
|
auto& t = textures_[asset_name] = {CreateRenderResource<Texture>(),
|
||||||
textures_[asset_name] = {texture, create_image, persistent};
|
create_image, persistent, 0};
|
||||||
|
texture = t.texture.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (persistent) {
|
if (persistent) {
|
||||||
|
@ -259,18 +255,33 @@ void Engine::RefreshImage(const std::string& asset_name) {
|
||||||
it->second.texture->Destroy();
|
it->second.texture->Destroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<Texture> Engine::GetTexture(const std::string& asset_name) {
|
Texture* Engine::AcquireTexture(const std::string& asset_name) {
|
||||||
auto it = textures_.find(asset_name);
|
auto it = textures_.find(asset_name);
|
||||||
if (it != textures_.end()) {
|
if (it != textures_.end()) {
|
||||||
if (!it->second.texture->IsValid())
|
if (!it->second.texture->IsValid())
|
||||||
RefreshImage(it->first);
|
RefreshImage(it->first);
|
||||||
return it->second.texture;
|
it->second.use_count++;
|
||||||
|
return it->second.texture.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<Texture> texture = CreateRenderResource<Texture>();
|
auto& t = textures_[asset_name] = {CreateRenderResource<Texture>(), nullptr,
|
||||||
textures_[asset_name] = {texture};
|
false, 1};
|
||||||
|
return t.texture.get();
|
||||||
|
}
|
||||||
|
|
||||||
return texture;
|
void Engine::ReleaseTexture(const std::string& asset_name) {
|
||||||
|
auto it = textures_.find(asset_name);
|
||||||
|
if (it == textures_.end())
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (it->second.use_count == 0) {
|
||||||
|
DCHECK(!it->second.texture->IsValid());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
it->second.use_count--;
|
||||||
|
if (!it->second.persistent && it->second.use_count == 0)
|
||||||
|
it->second.texture->Destroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Engine::AddInputEvent(std::unique_ptr<InputEvent> event) {
|
void Engine::AddInputEvent(std::unique_ptr<InputEvent> event) {
|
||||||
|
|
|
@ -76,7 +76,8 @@ class Engine {
|
||||||
|
|
||||||
void RefreshImage(const std::string& asset_name);
|
void RefreshImage(const std::string& asset_name);
|
||||||
|
|
||||||
std::shared_ptr<Texture> GetTexture(const std::string& asset_name);
|
Texture* AcquireTexture(const std::string& asset_name);
|
||||||
|
void ReleaseTexture(const std::string& asset_name);
|
||||||
|
|
||||||
void AddInputEvent(std::unique_ptr<InputEvent> event);
|
void AddInputEvent(std::unique_ptr<InputEvent> event);
|
||||||
std::unique_ptr<InputEvent> GetNextInputEvent();
|
std::unique_ptr<InputEvent> GetNextInputEvent();
|
||||||
|
@ -151,9 +152,10 @@ class Engine {
|
||||||
// Class holding information about texture resources managed by engine.
|
// Class holding information about texture resources managed by engine.
|
||||||
// Texture is created from the image returned by create_image callback.
|
// Texture is created from the image returned by create_image callback.
|
||||||
struct TextureResource {
|
struct TextureResource {
|
||||||
std::shared_ptr<Texture> texture;
|
std::unique_ptr<Texture> texture;
|
||||||
CreateImageCB create_image;
|
CreateImageCB create_image;
|
||||||
bool persistent = false;
|
bool persistent = false;
|
||||||
|
size_t use_count = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
static Engine* singleton;
|
static Engine* singleton;
|
||||||
|
|
|
@ -10,11 +10,17 @@ using namespace base;
|
||||||
|
|
||||||
namespace eng {
|
namespace eng {
|
||||||
|
|
||||||
|
ImageQuad::ImageQuad() = default;
|
||||||
|
|
||||||
|
ImageQuad::~ImageQuad() {
|
||||||
|
Destory();
|
||||||
|
}
|
||||||
|
|
||||||
void ImageQuad::Create(const std::string& asset_name,
|
void ImageQuad::Create(const std::string& asset_name,
|
||||||
std::array<int, 2> num_frames,
|
std::array<int, 2> num_frames,
|
||||||
int frame_width,
|
int frame_width,
|
||||||
int frame_height) {
|
int frame_height) {
|
||||||
texture_ = Engine::Get().GetTexture(asset_name);
|
texture_ = Engine::Get().AcquireTexture(asset_name);
|
||||||
|
|
||||||
num_frames_ = std::move(num_frames);
|
num_frames_ = std::move(num_frames);
|
||||||
frame_width_ = frame_width;
|
frame_width_ = frame_width;
|
||||||
|
@ -27,7 +33,8 @@ void ImageQuad::Create(const std::string& asset_name,
|
||||||
}
|
}
|
||||||
|
|
||||||
void ImageQuad::Destory() {
|
void ImageQuad::Destory() {
|
||||||
texture_.reset();
|
Engine::Get().ReleaseTexture(asset_name_);
|
||||||
|
texture_ = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ImageQuad::AutoScale() {
|
void ImageQuad::AutoScale() {
|
||||||
|
@ -40,7 +47,7 @@ void ImageQuad::AutoScale() {
|
||||||
SetSize(size);
|
SetSize(size);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ImageQuad::SetCustomShader(std::shared_ptr<Shader> shader) {
|
void ImageQuad::SetCustomShader(Shader* shader) {
|
||||||
custom_shader_ = shader;
|
custom_shader_ = shader;
|
||||||
custom_uniforms_.clear();
|
custom_uniforms_.clear();
|
||||||
}
|
}
|
||||||
|
@ -66,8 +73,8 @@ void ImageQuad::Draw(float frame_frac) {
|
||||||
Vector2f tex_scale = {GetFrameWidth() / texture_->GetWidth(),
|
Vector2f tex_scale = {GetFrameWidth() / texture_->GetWidth(),
|
||||||
GetFrameHeight() / texture_->GetHeight()};
|
GetFrameHeight() / texture_->GetHeight()};
|
||||||
|
|
||||||
Shader* shader = custom_shader_ ? custom_shader_.get()
|
Shader* shader =
|
||||||
: Engine::Get().GetPassThroughShader();
|
custom_shader_ ? custom_shader_ : Engine::Get().GetPassThroughShader();
|
||||||
|
|
||||||
shader->Activate();
|
shader->Activate();
|
||||||
shader->SetUniform("offset", position_);
|
shader->SetUniform("offset", position_);
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
#define ENGINE_IMAGE_QUAD_H
|
#define ENGINE_IMAGE_QUAD_H
|
||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <memory>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <variant>
|
#include <variant>
|
||||||
|
@ -17,8 +16,8 @@ class Texture;
|
||||||
|
|
||||||
class ImageQuad : public Animatable {
|
class ImageQuad : public Animatable {
|
||||||
public:
|
public:
|
||||||
ImageQuad() = default;
|
ImageQuad();
|
||||||
~ImageQuad() override = default;
|
~ImageQuad() override;
|
||||||
|
|
||||||
void Create(const std::string& asset_name,
|
void Create(const std::string& asset_name,
|
||||||
std::array<int, 2> num_frames = {1, 1},
|
std::array<int, 2> num_frames = {1, 1},
|
||||||
|
@ -29,7 +28,7 @@ class ImageQuad : public Animatable {
|
||||||
|
|
||||||
void AutoScale();
|
void AutoScale();
|
||||||
|
|
||||||
void SetCustomShader(std::shared_ptr<Shader> shader);
|
void SetCustomShader(Shader* shader);
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void SetCustomUniform(const std::string& name, T value) {
|
void SetCustomUniform(const std::string& name, T value) {
|
||||||
|
@ -54,9 +53,9 @@ class ImageQuad : public Animatable {
|
||||||
float,
|
float,
|
||||||
int>;
|
int>;
|
||||||
|
|
||||||
std::shared_ptr<Texture> texture_;
|
Texture* texture_ = nullptr;
|
||||||
|
|
||||||
std::shared_ptr<Shader> custom_shader_;
|
Shader* custom_shader_ = nullptr;
|
||||||
std::unordered_map<std::string, UniformValue> custom_uniforms_;
|
std::unordered_map<std::string, UniformValue> custom_uniforms_;
|
||||||
|
|
||||||
size_t current_frame_ = 0;
|
size_t current_frame_ = 0;
|
||||||
|
|
|
@ -22,9 +22,9 @@ void Texture::Update(std::unique_ptr<Image> image) {
|
||||||
|
|
||||||
void Texture::Destroy() {
|
void Texture::Destroy() {
|
||||||
if (IsValid()) {
|
if (IsValid()) {
|
||||||
|
DLOG << "Texture destroyed. resource_id: " << resource_id_;
|
||||||
renderer_->DestroyTexture(resource_id_);
|
renderer_->DestroyTexture(resource_id_);
|
||||||
resource_id_ = 0;
|
resource_id_ = 0;
|
||||||
DLOG << "Texture destroyed. resource_id: " << resource_id_;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue