mirror of
https://github.com/bailwillharr/engine.git
synced 2024-09-21 04:51:18 +00:00
Add persistent resources, clean up code
This commit is contained in:
parent
8afa35e940
commit
5791b11fb9
@ -14,6 +14,11 @@ namespace engine {
|
|||||||
class GFXDevice; // "gfx_device.hpp"
|
class GFXDevice; // "gfx_device.hpp"
|
||||||
class InputManager; // "input_manager.hpp"
|
class InputManager; // "input_manager.hpp"
|
||||||
class SceneManager; // "scene_manager.hpp"
|
class SceneManager; // "scene_manager.hpp"
|
||||||
|
|
||||||
|
namespace resources {
|
||||||
|
class Shader;
|
||||||
|
class Texture;
|
||||||
|
}
|
||||||
|
|
||||||
class Application {
|
class Application {
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
#include <vector>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
@ -28,6 +29,11 @@ namespace engine {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void addPersistent(const std::string& name, std::unique_ptr<T>&& resource)
|
||||||
|
{
|
||||||
|
m_persistentResources.push_back(add(name, std::move(resource)));
|
||||||
|
}
|
||||||
|
|
||||||
std::shared_ptr<T> get(const std::string& name)
|
std::shared_ptr<T> get(const std::string& name)
|
||||||
{
|
{
|
||||||
if (m_resources.contains(name)) {
|
if (m_resources.contains(name)) {
|
||||||
@ -45,6 +51,7 @@ namespace engine {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
std::unordered_map<std::string, std::weak_ptr<T>> m_resources{};
|
std::unordered_map<std::string, std::weak_ptr<T>> m_resources{};
|
||||||
|
std::vector<std::shared_ptr<T>> m_persistentResources{}; // This array owns persistent resources
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -66,12 +66,34 @@ namespace engine {
|
|||||||
// get base path for resources
|
// get base path for resources
|
||||||
m_resourcesPath = getResourcesPath();
|
m_resourcesPath = getResourcesPath();
|
||||||
|
|
||||||
// initialise some basic resources
|
// register resource managers
|
||||||
registerResourceManager<engine::resources::Mesh>();
|
|
||||||
registerResourceManager<engine::resources::Material>();
|
|
||||||
registerResourceManager<engine::resources::Shader>();
|
|
||||||
registerResourceManager<engine::resources::Texture>();
|
registerResourceManager<engine::resources::Texture>();
|
||||||
|
registerResourceManager<engine::resources::Shader>();
|
||||||
|
registerResourceManager<engine::resources::Material>();
|
||||||
|
registerResourceManager<engine::resources::Mesh>();
|
||||||
|
|
||||||
|
// default resources
|
||||||
|
{
|
||||||
|
resources::Shader::VertexParams vertParams{};
|
||||||
|
vertParams.hasNormal = true;
|
||||||
|
vertParams.hasUV0 = true;
|
||||||
|
auto texturedShader = std::make_unique<engine::resources::Shader>(
|
||||||
|
gfx(),
|
||||||
|
getResourcePath("engine/shaders/texture.vert").c_str(),
|
||||||
|
getResourcePath("engine/shaders/texture.frag").c_str(),
|
||||||
|
vertParams,
|
||||||
|
false,
|
||||||
|
true
|
||||||
|
);
|
||||||
|
getResourceManager<engine::resources::Shader>()->addPersistent("engine.textured", std::move(texturedShader));
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto whiteTexture = std::make_unique<engine::resources::Texture>(
|
||||||
|
gfx(),
|
||||||
|
getResourcePath("engine/textures/white.png")
|
||||||
|
);
|
||||||
|
getResourceManager<engine::resources::Texture>()->addPersistent("engine.white", std::move(whiteTexture));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Application::~Application() {}
|
Application::~Application() {}
|
||||||
|
@ -16,104 +16,49 @@
|
|||||||
#include "systems/transform.hpp"
|
#include "systems/transform.hpp"
|
||||||
#include "systems/render.hpp"
|
#include "systems/render.hpp"
|
||||||
|
|
||||||
#include "resources/mesh.hpp"
|
|
||||||
#include "resources/material.hpp"
|
#include "resources/material.hpp"
|
||||||
#include "resources/shader.hpp"
|
|
||||||
#include "resources/texture.hpp"
|
#include "resources/texture.hpp"
|
||||||
|
|
||||||
#include "util/model_loader.hpp"
|
#include "util/model_loader.hpp"
|
||||||
|
|
||||||
struct RotateComponent {
|
static void configureInputs(engine::InputManager* inputManager)
|
||||||
float rotation = 0.0f;
|
{
|
||||||
};
|
// user interface mappings
|
||||||
|
inputManager->addInputButton("fullscreen", engine::inputs::Key::K_F11);
|
||||||
class RotateSystem : public engine::System {
|
// game buttons
|
||||||
public:
|
inputManager->addInputButton("fire", engine::inputs::MouseButton::M_LEFT);
|
||||||
RotateSystem(engine::Scene* scene)
|
inputManager->addInputButton("aim", engine::inputs::MouseButton::M_RIGHT);
|
||||||
: System(scene, { typeid(engine::TransformComponent).hash_code(), typeid(RotateComponent).hash_code() })
|
inputManager->addInputButton("jump", engine::inputs::Key::K_SPACE);
|
||||||
{
|
inputManager->addInputButton("sprint", engine::inputs::Key::K_LSHIFT);
|
||||||
}
|
// game movement
|
||||||
|
inputManager->addInputButtonAsAxis("movex", engine::inputs::Key::K_D, engine::inputs::Key::K_A);
|
||||||
void onUpdate(float ts) override
|
inputManager->addInputButtonAsAxis("movey", engine::inputs::Key::K_W, engine::inputs::Key::K_S);
|
||||||
{
|
// looking around
|
||||||
for (uint32_t entity : m_entities) {
|
inputManager->addInputAxis("lookx", engine::inputs::MouseAxis::X);
|
||||||
auto t = m_scene->getComponent<engine::TransformComponent>(entity);
|
inputManager->addInputAxis("looky", engine::inputs::MouseAxis::Y);
|
||||||
auto r = m_scene->getComponent<RotateComponent>(entity);
|
}
|
||||||
r->rotation += ts;
|
|
||||||
r->rotation = glm::mod(r->rotation, glm::two_pi<float>());
|
|
||||||
t->rotation = glm::angleAxis(r->rotation, glm::vec3{0.0f, 0.0f, 1.0f});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void playGame()
|
void playGame()
|
||||||
{
|
{
|
||||||
engine::Application app(PROJECT_NAME, PROJECT_VERSION);
|
engine::Application app(PROJECT_NAME, PROJECT_VERSION);
|
||||||
|
|
||||||
app.setFrameLimiter(false);
|
|
||||||
|
|
||||||
// configure window
|
// configure window
|
||||||
app.window()->setRelativeMouseMode(true);
|
app.window()->setRelativeMouseMode(true);
|
||||||
|
|
||||||
|
configureInputs(app.inputManager());
|
||||||
|
|
||||||
// input config
|
|
||||||
|
|
||||||
// user interface mappings
|
|
||||||
app.inputManager()->addInputButton("fullscreen", engine::inputs::Key::K_F11);
|
|
||||||
// game buttons
|
|
||||||
app.inputManager()->addInputButton("fire", engine::inputs::MouseButton::M_LEFT);
|
|
||||||
app.inputManager()->addInputButton("aim", engine::inputs::MouseButton::M_RIGHT);
|
|
||||||
app.inputManager()->addInputButton("jump", engine::inputs::Key::K_SPACE);
|
|
||||||
app.inputManager()->addInputButton("sprint", engine::inputs::Key::K_LSHIFT);
|
|
||||||
// game movement
|
|
||||||
app.inputManager()->addInputButtonAsAxis("movex", engine::inputs::Key::K_D, engine::inputs::Key::K_A);
|
|
||||||
app.inputManager()->addInputButtonAsAxis("movey", engine::inputs::Key::K_W, engine::inputs::Key::K_S);
|
|
||||||
// looking around
|
|
||||||
app.inputManager()->addInputAxis("lookx", engine::inputs::MouseAxis::X);
|
|
||||||
app.inputManager()->addInputAxis("looky", engine::inputs::MouseAxis::Y);
|
|
||||||
|
|
||||||
// scene setup
|
|
||||||
|
|
||||||
auto myScene = app.sceneManager()->createEmptyScene();
|
auto myScene = app.sceneManager()->createEmptyScene();
|
||||||
|
|
||||||
myScene->registerComponent<RotateComponent>();
|
|
||||||
myScene->registerComponent<CameraControllerComponent>();
|
myScene->registerComponent<CameraControllerComponent>();
|
||||||
|
|
||||||
myScene->registerSystem<RotateSystem>();
|
|
||||||
myScene->registerSystem<CameraControllerSystem>();
|
myScene->registerSystem<CameraControllerSystem>();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
auto camera = myScene->createEntity("camera");
|
auto camera = myScene->createEntity("camera");
|
||||||
myScene->addComponent<CameraControllerComponent>(camera)->standingHeight = myScene->getComponent<engine::TransformComponent>(camera)->position.y = 2.0f;
|
myScene->addComponent<CameraControllerComponent>(camera)->standingHeight = myScene->getComponent<engine::TransformComponent>(camera)->position.y = 2.0f;
|
||||||
myScene->addComponent<engine::ColliderComponent>(camera)->r = 1.0f;
|
myScene->addComponent<engine::ColliderComponent>(camera)->r = 1.0f;
|
||||||
|
|
||||||
myScene->getSystem<engine::RenderSystem>()->setCameraEntity(camera);
|
myScene->getSystem<engine::RenderSystem>()->setCameraEntity(camera);
|
||||||
|
|
||||||
engine::resources::Shader::VertexParams vertParams{};
|
engine::util::loadMeshFromFile(myScene, app.getResourcePath("models/astronaut/astronaut.dae"));
|
||||||
vertParams.hasNormal = true;
|
|
||||||
vertParams.hasUV0 = true;
|
|
||||||
auto theShader = std::make_unique<engine::resources::Shader>(
|
|
||||||
app.gfx(),
|
|
||||||
app.getResourcePath("engine/shaders/texture.vert").c_str(),
|
|
||||||
app.getResourcePath("engine/shaders/texture.frag").c_str(),
|
|
||||||
vertParams,
|
|
||||||
false,
|
|
||||||
true
|
|
||||||
);
|
|
||||||
auto whiteTexture = std::make_unique<engine::resources::Texture>(
|
|
||||||
app.gfx(),
|
|
||||||
app.getResourcePath("engine/textures/white.png")
|
|
||||||
);
|
|
||||||
auto keepTexture = app.addResource<engine::resources::Texture>("engine.white", std::move(whiteTexture));
|
|
||||||
auto keepShader = app.addResource<engine::resources::Shader>("engine.textured", std::move(theShader));
|
|
||||||
|
|
||||||
uint32_t astronaut = engine::util::loadMeshFromFile(myScene, app.getResourcePath("models/astronaut/astronaut.dae"));
|
|
||||||
(void)astronaut;
|
|
||||||
|
|
||||||
auto grassTexture = std::make_shared<engine::resources::Texture>(
|
auto grassTexture = std::make_shared<engine::resources::Texture>(
|
||||||
app.gfx(),
|
app.gfx(),
|
||||||
@ -122,7 +67,7 @@ void playGame()
|
|||||||
|
|
||||||
uint32_t enemy = myScene->createEntity("enemy");
|
uint32_t enemy = myScene->createEntity("enemy");
|
||||||
auto enemyRenderable = myScene->addComponent<engine::RenderableComponent>(enemy);
|
auto enemyRenderable = myScene->addComponent<engine::RenderableComponent>(enemy);
|
||||||
enemyRenderable->material = std::make_unique<engine::resources::Material>(keepShader);
|
enemyRenderable->material = std::make_unique<engine::resources::Material>(app.getResource<engine::resources::Shader>("engine.textured"));
|
||||||
enemyRenderable->material->m_texture = grassTexture;
|
enemyRenderable->material->m_texture = grassTexture;
|
||||||
enemyRenderable->mesh = genSphereMesh(app.gfx(), 2.0f, 30, false);
|
enemyRenderable->mesh = genSphereMesh(app.gfx(), 2.0f, 30, false);
|
||||||
auto enemyT = myScene->getComponent<engine::TransformComponent>(enemy);
|
auto enemyT = myScene->getComponent<engine::TransformComponent>(enemy);
|
||||||
@ -133,8 +78,8 @@ void playGame()
|
|||||||
|
|
||||||
uint32_t sphere = myScene->createEntity("sphere");
|
uint32_t sphere = myScene->createEntity("sphere");
|
||||||
auto sphereRenderable = myScene->addComponent<engine::RenderableComponent>(sphere);
|
auto sphereRenderable = myScene->addComponent<engine::RenderableComponent>(sphere);
|
||||||
sphereRenderable->material = std::make_unique<engine::resources::Material>(keepShader);
|
sphereRenderable->material = std::make_unique<engine::resources::Material>(app.getResource<engine::resources::Shader>("engine.textured"));
|
||||||
sphereRenderable->material->m_texture = keepTexture;
|
sphereRenderable->material->m_texture = app.getResource<engine::resources::Texture>("engine.white");
|
||||||
sphereRenderable->mesh = genSphereMesh(app.gfx(), 100.0f, 100, true);
|
sphereRenderable->mesh = genSphereMesh(app.gfx(), 100.0f, 100, true);
|
||||||
|
|
||||||
uint32_t light = myScene->createEntity("light");
|
uint32_t light = myScene->createEntity("light");
|
||||||
@ -150,8 +95,6 @@ void playGame()
|
|||||||
floorRenderable->material->m_texture = grassTexture;
|
floorRenderable->material->m_texture = grassTexture;
|
||||||
floorRenderable->mesh = genCuboidMesh(app.gfx(), 100.0f, 0.1f, 100.0f);
|
floorRenderable->mesh = genCuboidMesh(app.gfx(), 100.0f, 0.1f, 100.0f);
|
||||||
|
|
||||||
|
|
||||||
app.gameLoop();
|
app.gameLoop();
|
||||||
|
|
||||||
INFO("texture addr: {}, shader addr: {}", (void*)keepTexture->getHandle(), (void*)keepShader->getPipeline());
|
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
#include "meshgen.hpp"
|
#include "meshgen.hpp"
|
||||||
|
|
||||||
|
#include "resources/mesh.hpp"
|
||||||
|
|
||||||
#include <glm/gtc/constants.hpp>
|
#include <glm/gtc/constants.hpp>
|
||||||
#include <glm/ext.hpp>
|
#include <glm/ext.hpp>
|
||||||
#include <glm/trigonometric.hpp>
|
#include <glm/trigonometric.hpp>
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "resources/mesh.hpp"
|
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include "resources/mesh.hpp"
|
||||||
|
|
||||||
// generates a UV sphere
|
// generates a UV sphere
|
||||||
std::unique_ptr<engine::resources::Mesh> genSphereMesh(engine::GFXDevice* gfx, float r, int detail, bool windInside = false, bool flipNormals = false);
|
std::unique_ptr<engine::resources::Mesh> genSphereMesh(engine::GFXDevice* gfx, float r, int detail, bool windInside = false, bool flipNormals = false);
|
||||||
|
Loading…
Reference in New Issue
Block a user