Add Engine::Print method

Also add a hello world example to readme
This commit is contained in:
Attila Uygun 2023-07-28 18:25:33 +02:00
parent e65d37e846
commit 26a1270702
4 changed files with 62 additions and 6 deletions

View File

@ -27,3 +27,43 @@ cd build/android
[vma](https://github.com/GPUOpen-LibrariesAndSDKs/VulkanMemoryAllocator), [vma](https://github.com/GPUOpen-LibrariesAndSDKs/VulkanMemoryAllocator),
[vulkan-sdk](https://vulkan.lunarg.com), [vulkan-sdk](https://vulkan.lunarg.com),
[volk](https://github.com/zeux/volk) [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
```

View File

@ -442,6 +442,19 @@ TextureCompressor* Engine::GetTextureCompressor(bool opacity) {
return opacity ? tex_comp_alpha_.get() : tex_comp_opaque_.get(); return opacity ? tex_comp_alpha_.get() : tex_comp_opaque_.get();
} }
std::unique_ptr<Image> 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>();
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 { int Engine::GetScreenWidth() const {
return renderer_->GetScreenWidth(); return renderer_->GetScreenWidth();
} }

View File

@ -112,6 +112,9 @@ class Engine : public PlatformObserver {
const Font* GetSystemFont() { return system_font_.get(); } const Font* GetSystemFont() { return system_font_.get(); }
std::unique_ptr<Image> Print(const std::string& text,
base::Vector4f bg_color);
base::Randomf& GetRandomGenerator() { return random_; } base::Randomf& GetRandomGenerator() { return random_; }
TextureCompressor* GetTextureCompressor(bool opacity); TextureCompressor* GetTextureCompressor(bool opacity);

View File

@ -9,18 +9,18 @@ class Game {
virtual ~Game() = default; virtual ~Game() = default;
// Called before async-loading assets. // Called before async-loading assets.
virtual bool PreInitialize() = 0; virtual bool PreInitialize() { return true; }
// Called after resources are created. // 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: private:
Game(const Game&) = delete; Game(const Game&) = delete;