diff --git a/src/demo/enemy.cc b/src/demo/enemy.cc index 69a1513..9c34824 100644 --- a/src/demo/enemy.cc +++ b/src/demo/enemy.cc @@ -90,10 +90,13 @@ bool Enemy::PreInitialize() { Engine::Get().SetImageSource("shield_tex", "woom_enemy_shield.png", true); Engine::Get().SetImageSource("crate_tex", "nuke_pack_OK.png", true); - for (int i = 0; i < kEnemyType_Max; ++i) + for (int i = 0; i < kEnemyType_Max; ++i) { + if (i == kEnemyType_PowerUp) + continue; Engine::Get().SetImageSource( "score_tex"s + std::to_string(i), std::bind(&Enemy::GetScoreImage, this, (EnemyType)i), true); + } Engine::Get().SetShaderSource("chromatic_aberration", "chromatic_aberration.glsl"); @@ -647,11 +650,13 @@ void Enemy::SpawnUnit(EnemyType enemy_type, e.health_bar.PlaceToBottomOf(e.sprite); e.health_bar.SetColor({0.161f, 0.89f, 0.322f, 1}); - e.score.Create("score_tex"s + std::to_string(e.enemy_type)); - e.score.SetZOrder(12); - e.score.Scale(1.1f); - e.score.SetColor({1, 1, 1, 1}); - e.score.SetPosition(spawn_pos); + if (enemy_type != kEnemyType_PowerUp) { + e.score.Create("score_tex"s + std::to_string(e.enemy_type)); + e.score.SetZOrder(12); + e.score.Scale(1.1f); + e.score.SetColor({1, 1, 1, 1}); + e.score.SetPosition(spawn_pos); + } e.target_animator.Attach(&e.target); @@ -1164,11 +1169,7 @@ int Enemy::GetScore(EnemyType enemy_type) { std::unique_ptr Enemy::GetScoreImage(EnemyType enemy_type) { const Font& font = static_cast(Engine::Get().GetGame())->GetFont(); - int score = GetScore(enemy_type); - if (!score) - return nullptr; - - std::string text = std::to_string(score); + std::string text = std::to_string(GetScore(enemy_type)); int width, height; font.CalculateBoundingBox(text.c_str(), width, height); diff --git a/src/demo/hud.cc b/src/demo/hud.cc index bae7d0d..fc755b3 100644 --- a/src/demo/hud.cc +++ b/src/demo/hud.cc @@ -198,7 +198,6 @@ void Hud::ShowMessage(const std::string& text, float duration) { message_text_ = text; Engine::Get().RefreshImage("message"); - message_.AutoScale(); message_.Scale(1.5f); message_.SetColor({1, 1, 1, 0}); message_.SetVisible(true); @@ -215,7 +214,6 @@ void Hud::ShowMessage(const std::string& text, float duration) { void Hud::ShowBonus(size_t bonus) { bonus_score_ = bonus; Engine::Get().RefreshImage("bonus_tex"); - bonus_.AutoScale(); bonus_.Scale(1.3f); bonus_.SetColor({1, 1, 1, 1}); bonus_.SetVisible(true); @@ -245,26 +243,20 @@ std::unique_ptr Hud::CreateMessageImage() { font.Print(x, 0, message_text_.c_str(), image->GetBuffer(), image->GetWidth()); image->Compress(); - return image; } std::unique_ptr Hud::CreateBonusImage() { const Font& font = static_cast(Engine::Get().GetGame())->GetFont(); - if (bonus_score_ == 0) - return nullptr; + auto image = CreateImage(); std::string text = std::to_string(bonus_score_); - int width, height; - font.CalculateBoundingBox(text.c_str(), width, height); - - auto image = std::make_unique(); - image->Create(width, height); - image->Clear({1, 1, 1, 0}); - - font.Print(0, 0, text.c_str(), image->GetBuffer(), image->GetWidth()); + int w, h; + font.CalculateBoundingBox(text.c_str(), w, h); + float x = (image->GetWidth() - w) / 2; + font.Print(x, 0, text.c_str(), image->GetBuffer(), image->GetWidth()); image->Compress(); return image; } @@ -282,7 +274,7 @@ std::unique_ptr Hud::Print(int i, const std::string& text) { } font.Print(x, 0, text.c_str(), image->GetBuffer(), image->GetWidth()); - + image->Compress(); return image; } diff --git a/src/engine/animatable.h b/src/engine/animatable.h index ef7762d..a9b4d98 100644 --- a/src/engine/animatable.h +++ b/src/engine/animatable.h @@ -51,7 +51,7 @@ class Animatable : public Drawable { protected: base::Vector2f position_ = {0, 0}; - base::Vector2f size_ = {1, 1}; + base::Vector2f size_ = {0, 0}; base::Vector2f scale_ = {1, 1}; base::Vector2f rotation_ = {0, 1}; float theta_ = 0; diff --git a/src/engine/engine.cc b/src/engine/engine.cc index f727985..8214449 100644 --- a/src/engine/engine.cc +++ b/src/engine/engine.cc @@ -115,8 +115,6 @@ void Engine::Initialize() { CreateProjectionMatrix(); - LOG(0) << "image scale factor: " << GetImageScaleFactor(); - system_font_ = std::make_unique(); system_font_->Load("engine/RobotoMono-Regular.ttf"); @@ -148,10 +146,8 @@ void Engine::Update(float delta_time) { fps_seconds_ = 0; } - if (stats_->IsVisible()) { + if (stats_->IsVisible()) RefreshImage("stats_tex"); - stats_->AutoScale(); - } } void Engine::Draw(float frame_frac) { @@ -454,14 +450,8 @@ int Engine::GetScreenHeight() const { return renderer_->GetScreenHeight(); } -int Engine::GetDeviceDpi() const { - return platform_->GetDeviceDpi(); -} - float Engine::GetImageScaleFactor() const { - float width_inch = static_cast(renderer_->GetScreenWidth()) / - static_cast(platform_->GetDeviceDpi()); - return 2.57143f / width_inch; + return static_cast(renderer_->GetScreenWidth()) / 514.286f; } const std::string& Engine::GetRootPath() const { @@ -605,17 +595,11 @@ void Engine::CreateTextureCompressors() { } void Engine::CreateProjectionMatrix() { - if (GetScreenWidth() > GetScreenHeight()) { - float aspect_ratio = (float)GetScreenWidth() / (float)GetScreenHeight(); - LOG(0) << "aspect ratio: " << aspect_ratio; - screen_size_ = {aspect_ratio * 2.0f, 2.0f}; - projection_.CreateOrthoProjection(-aspect_ratio, aspect_ratio, -1.0f, 1.0f); - } else { - float aspect_ratio = (float)GetScreenHeight() / (float)GetScreenWidth(); - LOG(0) << "aspect_ratio: " << aspect_ratio; - screen_size_ = {2.0f, aspect_ratio * 2.0f}; - projection_.CreateOrthoProjection(-1.0, 1.0, -aspect_ratio, aspect_ratio); - } + float aspect_ratio = (float)GetScreenHeight() / (float)GetScreenWidth(); + LOG(0) << "aspect_ratio: " << aspect_ratio; + screen_size_ = {1.0f, aspect_ratio * 1.0f}; + projection_.CreateOrthoProjection(-0.5f, 0.5f, -aspect_ratio * 0.5f, + aspect_ratio * 0.5f); } void Engine::ContextLost() { diff --git a/src/engine/engine.h b/src/engine/engine.h index e6f1b53..d758774 100644 --- a/src/engine/engine.h +++ b/src/engine/engine.h @@ -99,8 +99,6 @@ class Engine : public PlatformObserver { void SetKeepScreenOn(bool keep_screen_on); - void SetImageDpi(float dpi) { image_dpi_ = dpi; } - void SetEnableAudio(bool enable); void SetEnableVibration(bool enable) { vibration_enabled_ = enable; } @@ -129,8 +127,6 @@ class Engine : public PlatformObserver { const base::Matrix4f& GetProjectionMatrix() const { return projection_; } - int GetDeviceDpi() const; - float GetImageScaleFactor() const; const std::string& GetRootPath() const; @@ -145,8 +141,6 @@ class Engine : public PlatformObserver { float seconds_accumulated() const { return seconds_accumulated_; } - float image_dpi() const { return image_dpi_; } - float time_step() { return time_step_; } int fps() const { return fps_; } @@ -207,8 +201,6 @@ class Engine : public PlatformObserver { float time_step_ = 1.0f / 60.0f; size_t tick_ = 0; - float image_dpi_ = 200; - bool vibration_enabled_ = true; std::deque> input_queue_; diff --git a/src/engine/image_quad.cc b/src/engine/image_quad.cc index 6656da0..b5c7c10 100644 --- a/src/engine/image_quad.cc +++ b/src/engine/image_quad.cc @@ -21,15 +21,15 @@ void ImageQuad::Create(const std::string& asset_name, int frame_width, int frame_height) { texture_ = Engine::Get().AcquireTexture(asset_name); - num_frames_ = std::move(num_frames); frame_width_ = frame_width; frame_height_ = frame_height; - - if ((frame_width_ > 0 && frame_height_ > 0) || texture_->IsValid()) - AutoScale(); - asset_name_ = asset_name; + + DCHECK((frame_width_ > 0 && frame_height_ > 0) || texture_->IsValid()) + << asset_name; + SetSize(Engine::Get().ToScale({GetFrameWidth(), GetFrameHeight()}) * + Engine::Get().GetImageScaleFactor()); } void ImageQuad::Destroy() { @@ -39,16 +39,6 @@ void ImageQuad::Destroy() { } } -void ImageQuad::AutoScale() { - auto& engine = Engine::Get(); - Vector2f dimensions = {GetFrameWidth(), GetFrameHeight()}; - Vector2f size = engine.ToScale(dimensions); - float s = - static_cast(engine.image_dpi()) * engine.GetImageScaleFactor(); - size *= static_cast(engine.GetDeviceDpi()) / s; - SetSize(size); -} - void ImageQuad::SetCustomShader(const std::string& asset_name) { custom_shader_ = Engine::Get().GetShader(asset_name); custom_uniforms_.clear(); diff --git a/src/engine/image_quad.h b/src/engine/image_quad.h index c7e7629..a010aa0 100644 --- a/src/engine/image_quad.h +++ b/src/engine/image_quad.h @@ -26,8 +26,6 @@ class ImageQuad final : public Animatable { void Destroy(); - void AutoScale(); - void SetCustomShader(const std::string& asset_name); template