diff --git a/CMakeLists.txt b/CMakeLists.txt index 58a7c86..e133f40 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,6 +8,20 @@ set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_EXTENSIONS OFf) +include(FetchContent) +set(SFML_LIB_NAME SFML) + +FetchContent_Declare( + ${SFML_LIB_NAME} + GIT_REPOSITORY https://github.com/SFML/SFML.git + GIT_TAG 2.6.0 + GIT_SHALLOW FALSE +) + +FetchContent_MakeAvailable(${SFML_LIB_NAME}) + +set(LIGHT_YEARS_ENGINE_TARGET_NAME LightYearsEngine) set(LIGHT_YEARS_GAME_TARGET_NAME LightYearsGame) -add_subdirectory(LightYears) \ No newline at end of file +add_subdirectory(LightYearsEngine) +add_subdirectory(LightYearsGame) \ No newline at end of file diff --git a/LightYears/CMakeLists.txt b/LightYears/CMakeLists.txt deleted file mode 100644 index ed79899..0000000 --- a/LightYears/CMakeLists.txt +++ /dev/null @@ -1,3 +0,0 @@ -add_executable(${LIGHT_YEARS_GAME_TARGET_NAME} - ${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp -) \ No newline at end of file diff --git a/LightYears/src/main.cpp b/LightYears/src/main.cpp deleted file mode 100644 index 3c229c7..0000000 --- a/LightYears/src/main.cpp +++ /dev/null @@ -1,5 +0,0 @@ -#include - -int main() { - std::cout << "Hello, world!" << std::endl; -} \ No newline at end of file diff --git a/LightYearsEngine/CMakeLists.txt b/LightYearsEngine/CMakeLists.txt new file mode 100644 index 0000000..8eac05d --- /dev/null +++ b/LightYearsEngine/CMakeLists.txt @@ -0,0 +1,36 @@ +add_library(${LIGHT_YEARS_ENGINE_TARGET_NAME} STATIC + ${CMAKE_CURRENT_SOURCE_DIR}/include/framework/Application.h + ${CMAKE_CURRENT_SOURCE_DIR}/src/framework/Application.cpp + + ${CMAKE_CURRENT_SOURCE_DIR}/include/EntryPoint.h + ${CMAKE_CURRENT_SOURCE_DIR}/src/EntryPoint.cpp + + ${CMAKE_CURRENT_SOURCE_DIR}/include/framework/Core.h + ${CMAKE_CURRENT_SOURCE_DIR}/src/framework/Core.cpp + + ${CMAKE_CURRENT_SOURCE_DIR}/include/framework/World.h + ${CMAKE_CURRENT_SOURCE_DIR}/src/framework/World.cpp +) + +target_include_directories(${LIGHT_YEARS_ENGINE_TARGET_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include) + +target_link_libraries(${LIGHT_YEARS_ENGINE_TARGET_NAME} PUBLIC sfml-graphics) +target_link_libraries(${LIGHT_YEARS_ENGINE_TARGET_NAME} PUBLIC sfml-window) +target_link_libraries(${LIGHT_YEARS_ENGINE_TARGET_NAME} PUBLIC sfml-system) +target_link_libraries(${LIGHT_YEARS_ENGINE_TARGET_NAME} PUBLIC sfml-audio) + +function(CopyLibToTarget LIB_NAME TARGET_NAME) +add_custom_command( + TARGET ${TARGET_NAME} + POST_BUILD + COMMAND + ${CMAKE_COMMAND} -E copy_if_different + $ + $ +) +endfunction() + +CopyLibToTarget(sfml-graphics ${LIGHT_YEARS_ENGINE_TARGET_NAME}) +CopyLibToTarget(sfml-window ${LIGHT_YEARS_ENGINE_TARGET_NAME}) +CopyLibToTarget(sfml-system ${LIGHT_YEARS_ENGINE_TARGET_NAME}) +CopyLibToTarget(sfml-audio ${LIGHT_YEARS_ENGINE_TARGET_NAME}) diff --git a/LightYearsEngine/include/EntryPoint.h b/LightYearsEngine/include/EntryPoint.h new file mode 100644 index 0000000..d456e2c --- /dev/null +++ b/LightYearsEngine/include/EntryPoint.h @@ -0,0 +1,7 @@ +#pragma once + +namespace ly { + class Application; +} + +extern ly::Application* GetApplication(); diff --git a/LightYearsEngine/include/framework/Application.h b/LightYearsEngine/include/framework/Application.h new file mode 100644 index 0000000..dd666c6 --- /dev/null +++ b/LightYearsEngine/include/framework/Application.h @@ -0,0 +1,34 @@ +#pragma once +#include +#include "framework/Core.h" + +namespace ly { + + class World; + class Application { + public: + Application(); + void Run(); + + template + weak LoadWorld() { + shared newWorld{ new WorldType{this} }; + currentWorld = newWorld; + currentWorld->BeginPlayInternal(); + return newWorld; + } + private: + void TickInternal(float deltaTime); + void RenderInternal(); + + virtual void Render(); + virtual void Tick(float deltaTime); + + sf::RenderWindow mWindow; + float mTargetFrameRate; + sf::Clock mTickClock; + + shared currentWorld; + }; + +} \ No newline at end of file diff --git a/LightYearsEngine/include/framework/Core.h b/LightYearsEngine/include/framework/Core.h new file mode 100644 index 0000000..659cae3 --- /dev/null +++ b/LightYearsEngine/include/framework/Core.h @@ -0,0 +1,30 @@ +#pragma once +#include +#include +#include +#include +#include + +namespace ly { + +template +using unique = std::unique_ptr; + +template +using shared = std::shared_ptr; + +template +using weak = std::weak_ptr; + +template +using List = std::vector; + +template> +using Map = std::map; + +template> +using Dictionary = std::map; + +// Logging Macro +#define LOG(M, ...) printf(M "\n", ##__VA_ARGS__) +} \ No newline at end of file diff --git a/LightYearsEngine/include/framework/World.h b/LightYearsEngine/include/framework/World.h new file mode 100644 index 0000000..5934b4e --- /dev/null +++ b/LightYearsEngine/include/framework/World.h @@ -0,0 +1,21 @@ +#pragma once + +namespace ly { + class Application; + + class World { + public: + World(Application *owningApp); + + void BeginPlayInternal(); + void TickInternal(float deltaTime); + + virtual ~World(); + + private: + void BeginPlay(); + void Tick(float deltaTime); + Application* mOwningApp; + bool mBeganPlay; + }; +} \ No newline at end of file diff --git a/LightYearsEngine/src/EntryPoint.cpp b/LightYearsEngine/src/EntryPoint.cpp new file mode 100644 index 0000000..e3b0dff --- /dev/null +++ b/LightYearsEngine/src/EntryPoint.cpp @@ -0,0 +1,8 @@ +#include "EntryPoint.h" +#include "framework/Application.h" + +int main() { + ly::Application* app = GetApplication(); + app->Run(); + delete app; +} \ No newline at end of file diff --git a/LightYearsEngine/src/framework/Application.cpp b/LightYearsEngine/src/framework/Application.cpp new file mode 100644 index 0000000..e77db6c --- /dev/null +++ b/LightYearsEngine/src/framework/Application.cpp @@ -0,0 +1,65 @@ +#include "framework/Application.h" +#include "framework/Core.h" +#include "framework/World.h" + +namespace ly { + Application::Application(): + mWindow{sf::VideoMode(1440,1024), "LightYears"}, + mTargetFrameRate{60.0f}, + mTickClock{}, + currentWorld{nullptr} + { + } + + void Application::Run() + { + mTickClock.restart(); + float accumulatedTime = 0.0f; + float targetDeltaTime = 1.f / mTargetFrameRate; + while (mWindow.isOpen()) { + sf::Event windowEvent; + while (mWindow.pollEvent(windowEvent)) { + if (windowEvent.type == sf::Event::EventType::Closed) { + mWindow.close(); + } + } + float frameDeltaTime = mTickClock.restart().asSeconds(); + accumulatedTime += frameDeltaTime; + while (accumulatedTime > targetDeltaTime) { + accumulatedTime -= targetDeltaTime; + TickInternal(targetDeltaTime); + RenderInternal(); + } + } + } + + void Application::TickInternal(float deltaTime) + { + Tick(deltaTime); + if (currentWorld) { + currentWorld->TickInternal(deltaTime); + } + } + + void Application::Tick(float deltaTime) + { + } + + void Application::RenderInternal() + { + mWindow.clear(); + + Render(); + + mWindow.display(); + } + + void Application::Render() + { + sf::RectangleShape rect{ sf::Vector2f{100,100} }; + rect.setFillColor(sf::Color::Green); + rect.setOrigin(50, 50); + rect.setPosition(mWindow.getSize().x / 2, mWindow.getSize().y / 2); + mWindow.draw(rect); + } +} \ No newline at end of file diff --git a/LightYearsEngine/src/framework/Core.cpp b/LightYearsEngine/src/framework/Core.cpp new file mode 100644 index 0000000..e69de29 diff --git a/LightYearsEngine/src/framework/World.cpp b/LightYearsEngine/src/framework/World.cpp new file mode 100644 index 0000000..58cba2e --- /dev/null +++ b/LightYearsEngine/src/framework/World.cpp @@ -0,0 +1,36 @@ +#include "framework/World.h" +#include "framework/Core.h" + +namespace ly { + World::World(Application* owningApp) : mOwningApp{ owningApp }, mBeganPlay{ false } { + + } + + void World::BeginPlayInternal() { + if (!mBeganPlay) { + mBeganPlay = true; + BeginPlay(); + } + } + + World::~World() + { + } + + void World::TickInternal(float deltaTime) + { + Tick(deltaTime); + } + + + + void World::BeginPlay() + { + LOG("Began Play"); + } + + void World::Tick(float deltaTime) + { + LOG("Ticking at frame rate: %f", 1.f / deltaTime); + } +} \ No newline at end of file diff --git a/LightYearsGame/CMakeLists.txt b/LightYearsGame/CMakeLists.txt new file mode 100644 index 0000000..f140901 --- /dev/null +++ b/LightYearsGame/CMakeLists.txt @@ -0,0 +1,21 @@ +add_executable(${LIGHT_YEARS_GAME_TARGET_NAME} + ${CMAKE_CURRENT_SOURCE_DIR}/include/gameFramework/GameApplication.h + ${CMAKE_CURRENT_SOURCE_DIR}/src/gameFramework/GameApplication.cpp +) + +target_include_directories(${LIGHT_YEARS_GAME_TARGET_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include) + +target_link_libraries(${LIGHT_YEARS_GAME_TARGET_NAME} PUBLIC ${LIGHT_YEARS_ENGINE_TARGET_NAME}) + +function(CopyLibDirToTarget LIB_NAME TARGET_NAME) +add_custom_command( + TARGET ${TARGET_NAME} + POST_BUILD + COMMAND + ${CMAKE_COMMAND} -E copy_directory + $ + $ +) +endfunction() + +CopyLibDirToTarget(${LIGHT_YEARS_ENGINE_TARGET_NAME} ${LIGHT_YEARS_GAME_TARGET_NAME}) diff --git a/LightYearsGame/include/gameFramework/GameApplication.h b/LightYearsGame/include/gameFramework/GameApplication.h new file mode 100644 index 0000000..2b11940 --- /dev/null +++ b/LightYearsGame/include/gameFramework/GameApplication.h @@ -0,0 +1,10 @@ +#pragma once +#include + +namespace ly { + class GameApplication : public Application { + public: + GameApplication(); + + }; +} \ No newline at end of file diff --git a/LightYearsGame/src/gameFramework/GameApplication.cpp b/LightYearsGame/src/gameFramework/GameApplication.cpp new file mode 100644 index 0000000..a38a4f9 --- /dev/null +++ b/LightYearsGame/src/gameFramework/GameApplication.cpp @@ -0,0 +1,13 @@ +#include "gameFramework/GameApplication.h" +#include "framework/World.h" + +ly::Application* GetApplication() { + return new ly::GameApplication{}; +} + +namespace ly { + GameApplication::GameApplication() + { + LoadWorld(); + } +} \ No newline at end of file