png支持
This commit is contained in:
@@ -6,4 +6,5 @@
|
||||
#include <behavior.hpp>
|
||||
#include <render.hpp>
|
||||
#include <config.hpp>
|
||||
#include <scene.hpp>
|
||||
#include <engine.hpp>
|
||||
|
||||
@@ -15,6 +15,7 @@ struct WindowConfig {
|
||||
|
||||
struct MeshConfig {
|
||||
std::string shape = "quad";
|
||||
std::string texture;
|
||||
std::array<float, 4> color{0.5F, 0.5F, 0.5F, 1.0F};
|
||||
};
|
||||
|
||||
@@ -33,9 +34,19 @@ struct InputBindingConfig {
|
||||
std::vector<int> mouse_buttons;
|
||||
};
|
||||
|
||||
struct SceneConfig {
|
||||
std::string name;
|
||||
std::string path;
|
||||
std::vector<ActorConfig> actors;
|
||||
};
|
||||
|
||||
struct GameConfig {
|
||||
WindowConfig window;
|
||||
std::vector<InputBindingConfig> input_bindings;
|
||||
std::vector<SceneConfig> scenes;
|
||||
std::string start_scene;
|
||||
|
||||
// 兼容旧的单场景写法:直接写在游戏配置里的 actors。
|
||||
std::vector<ActorConfig> actors;
|
||||
};
|
||||
|
||||
|
||||
@@ -1,10 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace boundard {
|
||||
|
||||
class Scene;
|
||||
|
||||
class Game {
|
||||
private:
|
||||
size_t id_cnt;
|
||||
std::string current_scene_name_;
|
||||
std::string pending_scene_;
|
||||
std::unordered_map<std::string, std::shared_ptr<Scene>> scenes_;
|
||||
std::shared_ptr<Scene> current_scene_;
|
||||
|
||||
public:
|
||||
Game();
|
||||
@@ -17,5 +28,20 @@ class Game {
|
||||
static Game& getInstance();
|
||||
void launch();
|
||||
size_t new_id();
|
||||
|
||||
// 场景注册与查询。
|
||||
void add_scene(std::shared_ptr<Scene> scene);
|
||||
std::shared_ptr<Scene> scene(const std::string& name) const;
|
||||
std::shared_ptr<Scene> current_scene() const;
|
||||
const std::string& current_scene_name() const;
|
||||
bool has_scene(const std::string& name) const;
|
||||
|
||||
// 引擎在安全时机调用,把目标场景设为当前场景。
|
||||
void set_current_scene(std::shared_ptr<Scene> scene);
|
||||
|
||||
// 供行为函数调用的切换 API。场景会在主循环的安全时机切换。
|
||||
bool switch_scene(const std::string& name);
|
||||
void request_scene(const std::string& name);
|
||||
std::string take_scene_request();
|
||||
};
|
||||
} // namespace boundard
|
||||
|
||||
+7
-6
@@ -9,17 +9,18 @@
|
||||
|
||||
namespace boundard {
|
||||
|
||||
// 渲染组件:描述 Actor 的几何形状、颜色和纹理占位。
|
||||
// 渲染组件:描述 Actor 的几何形状、颜色和可选 PNG 纹理。
|
||||
class Mesh {
|
||||
public:
|
||||
struct Vertex {
|
||||
std::array<float, 2> position;
|
||||
std::array<float, 4> color;
|
||||
std::array<float, 2> uv;
|
||||
};
|
||||
|
||||
std::vector<Vertex> vertices;
|
||||
std::vector<uint32_t> indices;
|
||||
std::string texture; // 纹理路径占位,暂未采样。
|
||||
std::string texture; // PNG 纹理路径;为空时使用白色纹理。
|
||||
|
||||
std::array<float, 4> color() const {
|
||||
std::lock_guard<std::mutex> lock(*color_mutex_);
|
||||
@@ -34,10 +35,10 @@ class Mesh {
|
||||
static Mesh quad() {
|
||||
Mesh mesh;
|
||||
mesh.vertices = {
|
||||
{{-1.0F, -1.0F}, {1.0F, 1.0F, 1.0F, 1.0F}},
|
||||
{{1.0F, -1.0F}, {1.0F, 1.0F, 1.0F, 1.0F}},
|
||||
{{1.0F, 1.0F}, {1.0F, 1.0F, 1.0F, 1.0F}},
|
||||
{{-1.0F, 1.0F}, {1.0F, 1.0F, 1.0F, 1.0F}},
|
||||
{{-1.0F, -1.0F}, {1.0F, 1.0F, 1.0F, 1.0F}, {0.0F, 0.0F}},
|
||||
{{1.0F, -1.0F}, {1.0F, 1.0F, 1.0F, 1.0F}, {1.0F, 0.0F}},
|
||||
{{1.0F, 1.0F}, {1.0F, 1.0F, 1.0F, 1.0F}, {1.0F, 1.0F}},
|
||||
{{-1.0F, 1.0F}, {1.0F, 1.0F, 1.0F, 1.0F}, {0.0F, 1.0F}},
|
||||
};
|
||||
mesh.indices = {0, 1, 2, 2, 3, 0};
|
||||
return mesh;
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace boundard::render {
|
||||
class InputMap;
|
||||
}
|
||||
|
||||
namespace boundard {
|
||||
|
||||
class Actor;
|
||||
|
||||
// 运行时场景:持有场景名和其中的 Actor,并统一驱动 Actor 的 init/tick。
|
||||
class Scene {
|
||||
public:
|
||||
explicit Scene(std::string name = {});
|
||||
~Scene();
|
||||
|
||||
Scene(const Scene&) = delete;
|
||||
Scene& operator=(const Scene&) = delete;
|
||||
Scene(Scene&&) noexcept = delete;
|
||||
Scene& operator=(Scene&&) noexcept = delete;
|
||||
|
||||
const std::string& name() const;
|
||||
void set_name(std::string name);
|
||||
|
||||
void add_actor(std::shared_ptr<Actor> actor);
|
||||
void set_actors(std::vector<std::shared_ptr<Actor>> actors);
|
||||
void clear();
|
||||
|
||||
const std::vector<Actor*>& actors() const;
|
||||
std::vector<Actor*>& actors();
|
||||
|
||||
void initialize();
|
||||
void process(float dt, const render::InputMap& input);
|
||||
|
||||
private:
|
||||
std::string name_;
|
||||
std::vector<std::shared_ptr<Actor>> actor_storage_;
|
||||
std::vector<Actor*> actors_;
|
||||
};
|
||||
|
||||
} // namespace boundard
|
||||
Reference in New Issue
Block a user