diff --git a/CMakeLists.txt b/CMakeLists.txt index 2d745c9..650f227 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,74 +12,24 @@ project(engine LANGUAGES CXX set(SRC_FILES "src/engine.cpp" "src/window.cpp" - "src/input.cpp" #TODO make input_manager - - "src/object.cpp" - "src/sceneroot.cpp" - - "src/components/component.cpp" - # TODO move functionality into "Object" class - "src/components/camera.cpp" - "src/components/mesh_renderer.cpp" - "src/components/text_ui_renderer.cpp" - "src/components/custom.cpp" - - "src/resources/resource.cpp" - "src/resources/mesh.cpp" - "src/resources/shader.cpp" - "src/resources/texture.cpp" - "src/resources/font.cpp" - - "src/util/model_loader.cpp" - "src/util/files.cpp" - - "src/resource_manager.cpp" - + "src/input.cpp" "src/gfx_device_vulkan.cpp" - "src/gfx_device_null.cpp" - "src/gfx_device_opengl45.cpp" + "src/util/files.cpp" ) set(INCLUDE_FILES - "include/engine_api.h" - "include/engine.hpp" - "include/util.hpp" - "include/log.hpp" - + "include/logger.hpp" "include/window.hpp" - "include/inputs/keyboard.hpp" "include/inputs/mouse.hpp" - "include/input.hpp" - - "include/transform.hpp" - "include/object.hpp" - "include/sceneroot.hpp" - - "include/components/component.hpp" - "include/components/camera.hpp" - "include/components/mesh_renderer.hpp" - "include/components/text_ui_renderer.hpp" - "include/components/custom.hpp" - - "include/resources/resource.hpp" - "include/resources/mesh.hpp" - "include/resources/shader.hpp" - "include/resources/texture.hpp" - "include/resources/font.hpp" - - "include/util/model_loader.hpp" - "include/util/files.hpp" - - "include/resource_manager.hpp" - "include/gfx.hpp" "include/gfx_device.hpp" + "include/util/files.hpp" ) add_library(${PROJECT_NAME} STATIC diff --git a/include/engine.hpp b/include/engine.hpp index b2edfee..94eb4af 100644 --- a/include/engine.hpp +++ b/include/engine.hpp @@ -1,16 +1,12 @@ #pragma once -#include - namespace engine { class Window; + class GFXDevice; class Input; - class ResourceManager; - class SceneRoot; class Application { - public: Application(const char* appName, const char* appVersion); @@ -21,35 +17,15 @@ namespace engine { void gameLoop(); - Window* window() - { - return m_win; - } - - Input* input() - { - return m_input; - } - - ResourceManager* resources() - { - return m_res; - } - - SceneRoot* scene() - { - return m_scene; - } - - + Window* window() { return m_win; } + GFXDevice* gfx() { return m_gfx; } + Input* input() { return m_input; } private: Window* m_win; + GFXDevice* m_gfx; Input* m_input; - ResourceManager* m_res; - SceneRoot* m_scene; - bool m_enableFrameLimiter = true; }; } diff --git a/include/gfx_device.hpp b/include/gfx_device.hpp index 931baf5..62a5b64 100644 --- a/include/gfx_device.hpp +++ b/include/gfx_device.hpp @@ -13,7 +13,7 @@ namespace engine { class ENGINE_API GFXDevice { public: - GFXDevice(const char* appName, const char* appVersion, SDL_Window* window, bool vsync); + GFXDevice(const char* appName, const char* appVersion, SDL_Window* window, bool vsync = false); GFXDevice(const GFXDevice&) = delete; GFXDevice& operator=(const GFXDevice&) = delete; @@ -49,6 +49,4 @@ namespace engine { }; - extern GFXDevice* gfxdev; - } diff --git a/include/logger.hpp b/include/logger.hpp index 9ab1d84..84ff884 100644 --- a/include/logger.hpp +++ b/include/logger.hpp @@ -1,11 +1,13 @@ #pragma once -#include - #include "log.hpp" + #include #include +#include +#include + namespace engine { // To be executed in the target application, NOT engine.dll diff --git a/include/object.hpp b/include/object.hpp index b6c4f71..5b15ddb 100644 --- a/include/object.hpp +++ b/include/object.hpp @@ -29,18 +29,12 @@ namespace engine { class CustomComponent; } - struct GameIO { - Window* win; - Input* input; - ResourceManager* resMan; - }; - // This object lives until it is deleted by its parent(s) or finally when the "Scene" is destroyed. // Therefore it is safe to return raw pointers class ENGINE_API Object { public: - Object(std::string name, Object* parent, SceneRoot& root, struct GameIO things); + Object(std::string name, Object* parent, SceneRoot& root); Object(const Object&) = delete; Object& operator=(const Object&) = delete; ~Object(); diff --git a/include/window.hpp b/include/window.hpp index 78ac5e2..718cbc7 100644 --- a/include/window.hpp +++ b/include/window.hpp @@ -5,15 +5,12 @@ #include "inputs/keyboard.hpp" #include "inputs/mouse.hpp" -#pragma warning (push, 0) #include -#pragma warning (pop) #include #include #include -#include ENGINE_API extern const uint64_t BILLION; diff --git a/src/engine.cpp b/src/engine.cpp index eb16dad..2d3c4f1 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -3,11 +3,8 @@ #include "log.hpp" #include "window.hpp" -#include "input.hpp" -#include "resource_manager.hpp" -#include "sceneroot.hpp" - #include "gfx_device.hpp" +#include "input.hpp" // To allow the FPS-limiter to put the thread to sleep #include @@ -17,28 +14,14 @@ namespace engine { Application::Application(const char* appName, const char* appVersion) { m_win = new Window(appName, true, true); - - gfxdev = new GFXDevice(appName, appVersion, m_win->getHandle(), false); - + m_gfx = new GFXDevice(appName, appVersion, m_win->getHandle()); m_input = new Input(*m_win); - m_res = new ResourceManager(); - - GameIO things{ - m_win, - m_input, - m_res - }; - m_scene = new SceneRoot(things); } Application::~Application() { - delete m_scene; - delete m_res; delete m_input; - - delete gfxdev; - + delete m_gfx; delete m_win; } @@ -51,29 +34,25 @@ namespace engine { auto beginFrame = std::chrono::steady_clock::now(); auto endFrame = beginFrame + FRAMETIME_LIMIT; - //m_enableFrameLimiter = false; - // single-threaded game loop while (m_win->isRunning()) { - m_scene->updateStuff(); + /* logic */ /* draw */ - gfxdev->renderFrame(); + m_gfx->renderFrame(); /* poll events */ m_win->getInputAndEvents(); /* fps limiter */ - if (m_enableFrameLimiter) { - std::this_thread::sleep_until(endFrame); - } + std::this_thread::sleep_until(endFrame); beginFrame = endFrame; endFrame = beginFrame + FRAMETIME_LIMIT; } - gfxdev->waitIdle(); + m_gfx->waitIdle(); } diff --git a/src/gfx_device_vulkan.cpp b/src/gfx_device_vulkan.cpp index 439d6be..6187aec 100644 --- a/src/gfx_device_vulkan.cpp +++ b/src/gfx_device_vulkan.cpp @@ -35,9 +35,6 @@ namespace engine { - // EXTERNED GLOBAL VARIABLE - GFXDevice* gfxdev = nullptr; - static constexpr uint32_t FRAMES_IN_FLIGHT = 2; // This improved FPS by 5x! (on Intel IGPU) static constexpr size_t PUSH_CONSTANT_MAX_SIZE = 128; // bytes @@ -1027,10 +1024,6 @@ namespace engine { GFXDevice::GFXDevice(const char* appName, const char* appVersion, SDL_Window* window, bool vsync) { - if (gfxdev != nullptr) { - throw std::runtime_error("There can only be one graphics device"); - } - gfxdev = this; pimpl = std::make_unique(); diff --git a/src/window.cpp b/src/window.cpp index 4b475b3..b499b68 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -15,9 +15,6 @@ namespace engine { // init SDL if (SDL_Init(SDL_INIT_VIDEO) != 0) { const std::string errMsg("Unable to initialise SDL: " + std::string(SDL_GetError())); - if (SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "SDL error", errMsg.c_str(), NULL) != 0) { - std::cerr << errMsg << "\nAre you in a graphical environment?\n"; - } throw std::runtime_error(errMsg); } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 133cbdb..4d2ffe2 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -9,15 +9,8 @@ project(enginetest LANGUAGES CXX set(GAME_SOURCES src/main.cpp - src/game.cpp src/game.hpp - src/meshgen.cpp - src/meshgen.hpp - src/terrain.cpp - src/terrain.hpp - src/camera_controller.cpp - src/camera_controller.hpp ) diff --git a/test/src/game.cpp b/test/src/game.cpp index 66c88c7..68a8b5c 100644 --- a/test/src/game.cpp +++ b/test/src/game.cpp @@ -1,29 +1,7 @@ #include "config.h" #include "engine.hpp" - #include "window.hpp" -#include "input.hpp" -#include "sceneroot.hpp" - -#include "components/camera.hpp" -#include "components/mesh_renderer.hpp" -#include "components/text_ui_renderer.hpp" - -#include "resource_manager.hpp" -#include "resources/texture.hpp" -#include "resources/font.hpp" - -#include "util/model_loader.hpp" - -#include "camera_controller.hpp" -#include "meshgen.hpp" - -#include - -#include - -#include void playGame() { @@ -32,136 +10,5 @@ void playGame() // configure window app.window()->setRelativeMouseMode(true); - // input config - - // game buttons - app.input()->addInputButton("fire", engine::inputs::MouseButton::M_LEFT); - app.input()->addInputButton("aim", engine::inputs::MouseButton::M_RIGHT); - app.input()->addInputButton("jump", engine::inputs::Key::SPACE); - app.input()->addInputButton("sneak", engine::inputs::Key::LSHIFT); - // game movement - app.input()->addInputButtonAsAxis("movex", engine::inputs::Key::D, engine::inputs::Key::A); - app.input()->addInputButtonAsAxis("movey", engine::inputs::Key::W, engine::inputs::Key::S); - // looking around - app.input()->addInputAxis("lookx", engine::inputs::MouseAxis::X); - app.input()->addInputAxis("looky", engine::inputs::MouseAxis::Y); - - - - // create the scene - - auto cam = app.scene()->createChild("cam"); - constexpr float HEIGHT_INCHES = 6.0f * 12.0f; - // eye level is about 4 1/2 inches below height - constexpr float EYE_LEVEL = (HEIGHT_INCHES - 4.5f) * 25.4f / 1000.0f; - cam->transform.position = { 0.0f, EYE_LEVEL, 0.0f }; - auto camCamera = cam->createComponent(); - camCamera->usePerspective(130.0f); - cam->createComponent(); - - /* - auto gun = cam->createChild("gun"); - gun->transform.position = glm::vec3{ 0.2f, -0.1f, -0.15f }; - gun->transform.rotation = glm::angleAxis(glm::pi(), glm::vec3{ 0.0f, 1.0f, 0.0f }); - float GUN_SCALE = 9.0f / 560.0f; - gun->transform.scale *= GUN_SCALE; - auto gunRenderer = gun->createComponent(); - gunRenderer->setMesh("meshes/gun.mesh"); - gunRenderer->setTexture("textures/gun.png"); - */ - - // FLOOR - constexpr float GRASS_DENSITY = 128.0f * 20.0f; - auto floor = app.scene()->createChild("floor"); - auto floorRenderer = floor->createComponent(); - floor->transform.position = glm::vec3{ 0.0f, 0.0f, 0.0f }; - floorRenderer->setTexture("textures/grass.jpg"); - floorRenderer->m_mesh = std::make_unique(std::vector{ - { { -16.0f, 0.0f, 16.0f }, { 0.0f, 1.0f, 0.0f }, { 0.0f, GRASS_DENSITY } }, - { { 16.0f, 0.0f, -16.0f }, { 0.0f, 1.0f, 0.0f }, { GRASS_DENSITY, 0.0f } }, - { { -16.0f, 0.0f, -16.0f }, { 0.0f, 1.0f, 0.0f }, { 0.0f, 0.0f } }, - { { 16.0f, 0.0f, 16.0f }, { 0.0f, 1.0f, 0.0f }, { GRASS_DENSITY, GRASS_DENSITY } }, - { { 16.0f, 0.0f, -16.0f }, { 0.0f, 1.0f, 0.0f }, { GRASS_DENSITY, 0.0f } }, - { { -16.0f, 0.0f, 16.0f }, { 0.0f, 1.0f, 0.0f }, { 0.0f, GRASS_DENSITY } } - }); - floor->transform.scale = { 100.0f, 1.0f, 100.0f }; - - auto cube = app.scene()->createChild("cube"); - auto cubeRen = cube->createComponent(); - cubeRen->setMesh("meshes/cube.mesh"); - cube->transform.position = glm::vec3{ -5.0f, 1.0f, 0.0f }; - class Spin : public engine::components::CustomComponent { - public: - Spin(engine::Object* parent) : CustomComponent(parent) - { - - } - void onUpdate(glm::mat4 t) override - { - m_yaw += win.dt(); - m_yaw = glm::mod(m_yaw, glm::two_pi()); - const float halfYaw = m_yaw / 2.0f; - glm::quat yawQuat{}; - yawQuat.x = 0.0f; - yawQuat.y = glm::sin(halfYaw); - yawQuat.z = 0.0f; - yawQuat.w = glm::cos(halfYaw); - parent.transform.rotation = yawQuat; - - constexpr float halfPitch = -glm::half_pi() / 2.0f; - glm::quat pitchQuat{}; - pitchQuat.x = glm::sin(halfPitch); - pitchQuat.y = 0.0f; - pitchQuat.z = 0.0f; - pitchQuat.w = glm::cos(halfPitch); - parent.transform.rotation *= pitchQuat; - } - private: - float m_yaw = 0.0f; - }; - cube->createComponent(); - - // boundary - auto bounds = app.scene()->createChild("bounds"); - auto boundsRen = bounds->createComponent(); - boundsRen->m_mesh = genSphereMesh(100.0f, 36, true); - boundsRen->setTexture("textures/metal.jpg"); - - auto message = app.scene()->createChild("message"); - message->transform.position = { -1.0f, 0.95f, 0.0f }; - message->transform.scale *= 0.5f; - auto messageUI = message->createComponent(); - class FPSTextUpdater : public engine::components::CustomComponent { - engine::components::UI* textUI = nullptr; - public: - FPSTextUpdater(engine::Object* parent) : CustomComponent(parent) - { - textUI = parent->getComponent(); - } - void onUpdate(glm::mat4 t) override - { - textUI->m_text = std::to_string(parent.win.dt() * 1000.0f) + " ms"; - } - }; - message->createComponent(); - - /* - auto myModel = engine::util::loadAssimpMeshFromFile(app.scene(), app.resources()->getFilePath("models/pyramid/pyramid.dae").string()); - myModel->transform.position = { -5.0f, 2.0f, 7.0f }; - - auto myRoom = engine::util::loadAssimpMeshFromFile(app.scene(), app.resources()->getFilePath("models/room/room.dae").string()); - myRoom->transform.position = { 9.0f, 0.1f, 3.0f }; - */ - auto astronaut = engine::util::loadAssimpMeshFromFile(app.scene(), app.resources()->getFilePath("models/astronaut/astronaut.dae").string()); - astronaut->transform.position.z += 5.0f; - astronaut->createComponent(); - -/* - auto lego = engine::util::loadAssimpMeshFromFile(app.scene(), app.resources()->getFilePath("models/lego/lego.dae").string()); - lego->transform.position = { -30.0f, -33.0f, -30.0f }; -*/ - - app.scene()->printTree(); - app.gameLoop(); } diff --git a/test/src/main.cpp b/test/src/main.cpp index fe4e5d5..04e94fb 100644 --- a/test/src/main.cpp +++ b/test/src/main.cpp @@ -1,4 +1,5 @@ #include "config.h" + #include "game.hpp" #include "logger.hpp"