From 26a12707023330323fc446e1982a58b698cf9ce9 Mon Sep 17 00:00:00 2001 From: Attila Uygun Date: Fri, 28 Jul 2023 18:25:33 +0200 Subject: [PATCH] Add Engine::Print method Also add a hello world example to readme --- README.md | 40 ++++++++++++++++++++++++++++++++++++++++ src/engine/engine.cc | 13 +++++++++++++ src/engine/engine.h | 3 +++ src/engine/game.h | 12 ++++++------ 4 files changed, 62 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 3806be6..7dfef4c 100644 --- a/README.md +++ b/README.md @@ -27,3 +27,43 @@ cd build/android [vma](https://github.com/GPUOpen-LibrariesAndSDKs/VulkanMemoryAllocator), [vulkan-sdk](https://vulkan.lunarg.com), [volk](https://github.com/zeux/volk) + +#### Hello World example: +Simply shows a rotating "Hello Wolrd!". +```cpp +#include "base/interpolation.h" +#include "engine/animator.h" +#include "engine/asset/image.h" +#include "engine/engine.h" +#include "engine/game.h" +#include "engine/game_factory.h" +#include "engine/image_quad.h" + +class HelloWorld final : public eng::Game { + public: + ~HelloWorld() final = default; + + bool Initialize() final { + eng::Engine::Get().SetImageSource( + "hello_world_image", + std::bind(&eng::Engine::Print, &eng::Engine::Get(), "Hello World!", + base::Vector4f(1, 1, 1, 0))); + + hello_world_.Create("hello_world_image"); + hello_world_.SetVisible(true); + animator_.Attach(&hello_world_); + animator_.SetRotation(base::PI2f, 3, + std::bind(base::SmootherStep, std::placeholders::_1)); + animator_.Play(eng::Animator::kRotation, true); + return true; + } + + private: + eng::ImageQuad hello_world_; + eng::Animator animator_; +}; + +DECLARE_GAME_BEGIN +DECLARE_GAME(HelloWorld) +DECLARE_GAME_END +``` diff --git a/src/engine/engine.cc b/src/engine/engine.cc index 8214449..a400ba7 100644 --- a/src/engine/engine.cc +++ b/src/engine/engine.cc @@ -442,6 +442,19 @@ TextureCompressor* Engine::GetTextureCompressor(bool opacity) { return opacity ? tex_comp_alpha_.get() : tex_comp_opaque_.get(); } +std::unique_ptr Engine::Print(const std::string& text, + Vector4f bg_color) { + int width, height; + system_font_->CalculateBoundingBox(text.c_str(), width, height); + + auto image = std::make_unique(); + image->Create(width, height); + image->Clear(bg_color); + system_font_->Print(0, 0, text.c_str(), image->GetBuffer(), + image->GetWidth()); + return image; +} + int Engine::GetScreenWidth() const { return renderer_->GetScreenWidth(); } diff --git a/src/engine/engine.h b/src/engine/engine.h index d758774..2d94ccf 100644 --- a/src/engine/engine.h +++ b/src/engine/engine.h @@ -112,6 +112,9 @@ class Engine : public PlatformObserver { const Font* GetSystemFont() { return system_font_.get(); } + std::unique_ptr Print(const std::string& text, + base::Vector4f bg_color); + base::Randomf& GetRandomGenerator() { return random_; } TextureCompressor* GetTextureCompressor(bool opacity); diff --git a/src/engine/game.h b/src/engine/game.h index 93ef9e5..0ecda99 100644 --- a/src/engine/game.h +++ b/src/engine/game.h @@ -9,18 +9,18 @@ class Game { virtual ~Game() = default; // Called before async-loading assets. - virtual bool PreInitialize() = 0; + virtual bool PreInitialize() { return true; } // Called after resources are created. - virtual bool Initialize() = 0; + virtual bool Initialize() { return true; } - virtual void Update(float delta_time) = 0; + virtual void Update(float delta_time) {} - virtual void ContextLost() = 0; + virtual void ContextLost() {} - virtual void LostFocus() = 0; + virtual void LostFocus() {} - virtual void GainedFocus(bool from_interstitial_ad) = 0; + virtual void GainedFocus(bool from_interstitial_ad) {} private: Game(const Game&) = delete;