kaliber/src/engine/engine.h

253 lines
6.4 KiB
C
Raw Normal View History

#ifndef ENGINE_ENGINE_H
#define ENGINE_ENGINE_H
2020-04-13 11:24:53 +00:00
#include <deque>
#include <functional>
#include <list>
2020-04-13 11:24:53 +00:00
#include <memory>
#include <unordered_map>
#include "base/random.h"
2023-05-25 05:43:09 +00:00
#include "base/thread_pool.h"
#include "base/vecmath.h"
#include "engine/persistent_data.h"
2023-05-25 05:43:09 +00:00
#include "engine/platform/platform_observer.h"
2020-04-13 11:24:53 +00:00
class TextureCompressor;
namespace eng {
class Animator;
2023-06-21 21:24:29 +00:00
class AudioBus;
2023-05-17 23:17:31 +00:00
class AudioMixer;
2023-06-01 18:55:39 +00:00
class Drawable;
2020-04-13 11:24:53 +00:00
class Font;
class Game;
2023-06-01 18:55:39 +00:00
class Geometry;
class Image;
class ImageQuad;
2023-06-01 18:55:39 +00:00
class InputEvent;
class Platform;
2020-04-13 11:24:53 +00:00
class Renderer;
class Shader;
class Texture;
2023-06-01 18:55:39 +00:00
enum class RendererType;
2020-04-13 11:24:53 +00:00
2023-05-25 05:43:09 +00:00
class Engine : public PlatformObserver {
2020-04-13 11:24:53 +00:00
public:
using CreateImageCB = std::function<std::unique_ptr<Image>()>;
2023-05-25 05:43:09 +00:00
Engine(Platform* platform);
2020-04-13 11:24:53 +00:00
~Engine();
static Engine& Get();
2023-05-25 05:43:09 +00:00
void Run();
2020-04-13 11:24:53 +00:00
void AddDrawable(Drawable* drawable);
void RemoveDrawable(Drawable* drawable);
void AddAnimator(Animator* animator);
void RemoveAnimator(Animator* animator);
2023-06-01 18:55:39 +00:00
void CreateRenderer(RendererType type);
RendererType GetRendererType();
2020-04-13 11:24:53 +00:00
void Exit();
// Convert size from pixels to viewport scale.
2021-02-18 14:42:08 +00:00
base::Vector2f ToScale(const base::Vector2f& vec);
2020-04-13 11:24:53 +00:00
// Convert position form pixels to viewport coordinates.
2021-02-18 14:42:08 +00:00
base::Vector2f ToPosition(const base::Vector2f& vec);
2020-04-13 11:24:53 +00:00
void SetImageSource(const std::string& asset_name,
const std::string& file_name,
bool persistent = false);
void SetImageSource(const std::string& asset_name,
CreateImageCB create_image,
bool persistent = false);
void RefreshImage(const std::string& asset_name);
2021-10-28 20:20:53 +00:00
Texture* AcquireTexture(const std::string& asset_name);
void ReleaseTexture(const std::string& asset_name);
2023-06-21 21:24:29 +00:00
void SetShaderSource(const std::string& asset_name,
const std::string& file_name);
Shader* GetShader(const std::string& asset_name);
void AsyncLoadSound(const std::string& asset_name,
const std::string& file_name,
bool stream = false);
std::shared_ptr<AudioBus> GetAudioBus(const std::string& asset_name);
2023-05-17 09:03:04 +00:00
2020-04-13 11:24:53 +00:00
std::unique_ptr<InputEvent> GetNextInputEvent();
void StartRecording(const Json::Value& payload);
void EndRecording(const std::string file_name);
bool Replay(const std::string file_name, Json::Value& payload);
// Vibrate (if supported by the platform) for the specified duration.
void Vibrate(int duration);
void ShowInterstitialAd();
void ShareFile(const std::string& file_name);
void SetKeepScreenOn(bool keep_screen_on);
void SetEnableAudio(bool enable);
void SetEnableVibration(bool enable) { vibration_enabled_ = enable; }
2023-05-17 23:17:31 +00:00
AudioMixer* GetAudioMixer() { return audio_mixer_.get(); }
2021-09-30 00:00:18 +00:00
2020-04-13 11:24:53 +00:00
// Access to the render resources.
Geometry* GetQuad() { return quad_.get(); }
Shader* GetPassThroughShader() { return pass_through_shader_.get(); }
Shader* GetSolidShader() { return solid_shader_.get(); }
2020-04-13 11:24:53 +00:00
const Font* GetSystemFont() { return system_font_.get(); }
std::unique_ptr<Image> Print(const std::string& text,
base::Vector4f bg_color);
2022-08-25 23:28:53 +00:00
base::Randomf& GetRandomGenerator() { return random_; }
2020-04-13 11:24:53 +00:00
TextureCompressor* GetTextureCompressor(bool opacity);
Game* GetGame() { return game_.get(); }
// Return screen width/height in pixels.
int GetScreenWidth() const;
int GetScreenHeight() const;
// Return screen size in viewport scale.
2021-02-18 14:42:08 +00:00
base::Vector2f GetScreenSize() const { return screen_size_; }
2020-04-13 11:24:53 +00:00
2021-02-18 14:42:08 +00:00
const base::Matrix4f& GetProjectionMatrix() const { return projection_; }
2020-04-13 11:24:53 +00:00
float GetImageScaleFactor() const;
2020-04-13 11:24:53 +00:00
const std::string& GetRootPath() const;
const std::string& GetDataPath() const;
const std::string& GetSharedDataPath() const;
size_t GetAudioHardwareSampleRate();
2020-04-13 11:24:53 +00:00
bool IsMobile() const;
float seconds_accumulated() const { return seconds_accumulated_; }
float time_step() { return time_step_; }
int fps() const { return fps_; }
2020-04-13 11:24:53 +00:00
private:
// Class holding information about texture resources managed by engine.
// Texture is created from the image returned by create_image callback.
struct TextureResource {
2021-10-28 20:20:53 +00:00
std::unique_ptr<Texture> texture;
CreateImageCB create_image;
bool persistent = false;
2021-10-28 20:20:53 +00:00
size_t use_count = 0;
};
2023-05-17 09:03:04 +00:00
// Class holding information about shader resources managed by engine.
struct ShaderResource {
std::unique_ptr<Shader> shader;
std::string file_name;
};
2020-04-13 11:24:53 +00:00
static Engine* singleton;
Platform* platform_ = nullptr;
2023-05-25 05:43:09 +00:00
std::unique_ptr<Renderer> renderer_;
2023-05-17 23:17:31 +00:00
std::unique_ptr<AudioMixer> audio_mixer_;
2020-04-13 11:24:53 +00:00
std::unique_ptr<Game> game_;
std::unique_ptr<Geometry> quad_;
std::unique_ptr<Shader> pass_through_shader_;
std::unique_ptr<Shader> solid_shader_;
2020-04-13 11:24:53 +00:00
2021-02-18 14:42:08 +00:00
base::Vector2f screen_size_ = {0, 0};
base::Matrix4f projection_;
2020-04-13 11:24:53 +00:00
std::unique_ptr<Font> system_font_;
std::unique_ptr<TextureCompressor> tex_comp_opaque_;
std::unique_ptr<TextureCompressor> tex_comp_alpha_;
std::list<Drawable*> drawables_;
std::list<Animator*> animators_;
2023-06-21 21:24:29 +00:00
// Resources mapped by asset name.
std::unordered_map<std::string, TextureResource> textures_;
2023-05-17 09:03:04 +00:00
std::unordered_map<std::string, ShaderResource> shaders_;
2023-06-21 21:24:29 +00:00
std::unordered_map<std::string, std::shared_ptr<AudioBus>> audio_buses_;
size_t async_work_count_ = 0;
std::unique_ptr<ImageQuad> stats_;
2020-04-13 11:24:53 +00:00
float fps_seconds_ = 0;
int fps_ = 0;
float seconds_accumulated_ = 0.0f;
float time_step_ = 1.0f / 60.0f;
size_t tick_ = 0;
bool vibration_enabled_ = true;
2020-04-13 11:24:53 +00:00
std::deque<std::unique_ptr<InputEvent>> input_queue_;
PersistentData replay_data_;
bool recording_ = false;
bool replaying_ = false;
unsigned int replay_index_ = 0;
2023-05-25 05:43:09 +00:00
base::ThreadPool thread_pool_;
2022-08-25 23:28:53 +00:00
base::Randomf random_;
2020-04-13 11:24:53 +00:00
2023-05-25 05:43:09 +00:00
void Initialize();
void Update(float delta_time);
void Draw(float frame_frac);
// PlatformObserver implementation
void OnWindowCreated() final;
void OnWindowDestroyed() final;
void OnWindowResized(int width, int height) final;
void LostFocus() final;
void GainedFocus(bool from_interstitial_ad) final;
void AddInputEvent(std::unique_ptr<InputEvent> event) final;
2023-06-21 21:24:29 +00:00
void CreateRendererInternal(RendererType type);
void CreateTextureCompressors();
2023-07-01 19:36:26 +00:00
void CreateProjectionMatrix();
2020-04-13 11:24:53 +00:00
void ContextLost();
2023-06-21 21:24:29 +00:00
void CreateRenderResources();
void WaitForAsyncWork();
void SetStatsVisible(bool visible);
std::unique_ptr<Image> PrintStats();
2020-04-13 11:24:53 +00:00
Engine(const Engine&) = delete;
Engine& operator=(const Engine&) = delete;
};
} // namespace eng
#endif // ENGINE_ENGINE_H