diff --git a/include/application.hpp b/include/application.hpp index acb40e3..8b6cdcb 100644 --- a/include/application.hpp +++ b/include/application.hpp @@ -14,6 +14,11 @@ namespace engine { class GFXDevice; // "gfx_device.hpp" class InputManager; // "input_manager.hpp" class SceneManager; // "scene_manager.hpp" + + namespace resources { + class Shader; + class Texture; + } class Application { diff --git a/include/resource_manager.hpp b/include/resource_manager.hpp index 66e8710..b834c4a 100644 --- a/include/resource_manager.hpp +++ b/include/resource_manager.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include #include @@ -28,6 +29,11 @@ namespace engine { } } + void addPersistent(const std::string& name, std::unique_ptr&& resource) + { + m_persistentResources.push_back(add(name, std::move(resource))); + } + std::shared_ptr get(const std::string& name) { if (m_resources.contains(name)) { @@ -45,6 +51,7 @@ namespace engine { private: std::unordered_map> m_resources{}; + std::vector> m_persistentResources{}; // This array owns persistent resources }; diff --git a/src/application.cpp b/src/application.cpp index df3c3f6..a37af1c 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -66,12 +66,34 @@ namespace engine { // get base path for resources m_resourcesPath = getResourcesPath(); - // initialise some basic resources - registerResourceManager(); - registerResourceManager(); - registerResourceManager(); + // register resource managers registerResourceManager(); + registerResourceManager(); + registerResourceManager(); + registerResourceManager(); + // default resources + { + resources::Shader::VertexParams vertParams{}; + vertParams.hasNormal = true; + vertParams.hasUV0 = true; + auto texturedShader = std::make_unique( + gfx(), + getResourcePath("engine/shaders/texture.vert").c_str(), + getResourcePath("engine/shaders/texture.frag").c_str(), + vertParams, + false, + true + ); + getResourceManager()->addPersistent("engine.textured", std::move(texturedShader)); + } + { + auto whiteTexture = std::make_unique( + gfx(), + getResourcePath("engine/textures/white.png") + ); + getResourceManager()->addPersistent("engine.white", std::move(whiteTexture)); + } } Application::~Application() {} diff --git a/test/src/game.cpp b/test/src/game.cpp index 6b77cdf..5bed49b 100644 --- a/test/src/game.cpp +++ b/test/src/game.cpp @@ -16,104 +16,49 @@ #include "systems/transform.hpp" #include "systems/render.hpp" -#include "resources/mesh.hpp" #include "resources/material.hpp" -#include "resources/shader.hpp" #include "resources/texture.hpp" #include "util/model_loader.hpp" -struct RotateComponent { - float rotation = 0.0f; -}; - -class RotateSystem : public engine::System { -public: - RotateSystem(engine::Scene* scene) - : System(scene, { typeid(engine::TransformComponent).hash_code(), typeid(RotateComponent).hash_code() }) - { - } - - void onUpdate(float ts) override - { - for (uint32_t entity : m_entities) { - auto t = m_scene->getComponent(entity); - auto r = m_scene->getComponent(entity); - r->rotation += ts; - r->rotation = glm::mod(r->rotation, glm::two_pi()); - t->rotation = glm::angleAxis(r->rotation, glm::vec3{0.0f, 0.0f, 1.0f}); - } - } -}; - - +static void configureInputs(engine::InputManager* inputManager) +{ + // user interface mappings + inputManager->addInputButton("fullscreen", engine::inputs::Key::K_F11); + // game buttons + inputManager->addInputButton("fire", engine::inputs::MouseButton::M_LEFT); + inputManager->addInputButton("aim", engine::inputs::MouseButton::M_RIGHT); + 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); + inputManager->addInputButtonAsAxis("movey", engine::inputs::Key::K_W, engine::inputs::Key::K_S); + // looking around + inputManager->addInputAxis("lookx", engine::inputs::MouseAxis::X); + inputManager->addInputAxis("looky", engine::inputs::MouseAxis::Y); +} void playGame() { engine::Application app(PROJECT_NAME, PROJECT_VERSION); - app.setFrameLimiter(false); - // configure window app.window()->setRelativeMouseMode(true); - - - - // 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 + + configureInputs(app.inputManager()); auto myScene = app.sceneManager()->createEmptyScene(); - myScene->registerComponent(); myScene->registerComponent(); - - myScene->registerSystem(); myScene->registerSystem(); - - - - auto camera = myScene->createEntity("camera"); myScene->addComponent(camera)->standingHeight = myScene->getComponent(camera)->position.y = 2.0f; myScene->addComponent(camera)->r = 1.0f; + myScene->getSystem()->setCameraEntity(camera); - engine::resources::Shader::VertexParams vertParams{}; - vertParams.hasNormal = true; - vertParams.hasUV0 = true; - auto theShader = std::make_unique( - 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( - app.gfx(), - app.getResourcePath("engine/textures/white.png") - ); - auto keepTexture = app.addResource("engine.white", std::move(whiteTexture)); - auto keepShader = app.addResource("engine.textured", std::move(theShader)); - - uint32_t astronaut = engine::util::loadMeshFromFile(myScene, app.getResourcePath("models/astronaut/astronaut.dae")); - (void)astronaut; + engine::util::loadMeshFromFile(myScene, app.getResourcePath("models/astronaut/astronaut.dae")); auto grassTexture = std::make_shared( app.gfx(), @@ -122,7 +67,7 @@ void playGame() uint32_t enemy = myScene->createEntity("enemy"); auto enemyRenderable = myScene->addComponent(enemy); - enemyRenderable->material = std::make_unique(keepShader); + enemyRenderable->material = std::make_unique(app.getResource("engine.textured")); enemyRenderable->material->m_texture = grassTexture; enemyRenderable->mesh = genSphereMesh(app.gfx(), 2.0f, 30, false); auto enemyT = myScene->getComponent(enemy); @@ -133,8 +78,8 @@ void playGame() uint32_t sphere = myScene->createEntity("sphere"); auto sphereRenderable = myScene->addComponent(sphere); - sphereRenderable->material = std::make_unique(keepShader); - sphereRenderable->material->m_texture = keepTexture; + sphereRenderable->material = std::make_unique(app.getResource("engine.textured")); + sphereRenderable->material->m_texture = app.getResource("engine.white"); sphereRenderable->mesh = genSphereMesh(app.gfx(), 100.0f, 100, true); uint32_t light = myScene->createEntity("light"); @@ -150,8 +95,6 @@ void playGame() floorRenderable->material->m_texture = grassTexture; floorRenderable->mesh = genCuboidMesh(app.gfx(), 100.0f, 0.1f, 100.0f); - app.gameLoop(); - INFO("texture addr: {}, shader addr: {}", (void*)keepTexture->getHandle(), (void*)keepShader->getPipeline()); } diff --git a/test/src/meshgen.cpp b/test/src/meshgen.cpp index a3b7952..e4f266f 100644 --- a/test/src/meshgen.cpp +++ b/test/src/meshgen.cpp @@ -1,5 +1,7 @@ #include "meshgen.hpp" +#include "resources/mesh.hpp" + #include #include #include diff --git a/test/src/meshgen.hpp b/test/src/meshgen.hpp index 6fd186d..65335b4 100644 --- a/test/src/meshgen.hpp +++ b/test/src/meshgen.hpp @@ -1,8 +1,7 @@ #pragma once -#include "resources/mesh.hpp" - #include +#include "resources/mesh.hpp" // generates a UV sphere std::unique_ptr genSphereMesh(engine::GFXDevice* gfx, float r, int detail, bool windInside = false, bool flipNormals = false);