From c545d800d2f16fd3ea497df5763c582d9bc7ad64 Mon Sep 17 00:00:00 2001 From: Daniel Henry Date: Tue, 2 Sep 2025 09:32:30 -0500 Subject: [PATCH] Finished lecture 82 Signed-off-by: Daniel Henry --- CMakeLists.txt | 8 +- LightYearsEngine/CMakeLists.txt | 3 + LightYearsEngine/include/framework/Actor.h | 20 ++++ .../include/framework/Application.h | 52 +++++----- LightYearsEngine/include/framework/World.h | 40 +++++--- LightYearsEngine/src/framework/Actor.cpp | 22 +++++ .../src/framework/Application.cpp | 96 +++++++++---------- 7 files changed, 149 insertions(+), 92 deletions(-) create mode 100644 LightYearsEngine/include/framework/Actor.h create mode 100644 LightYearsEngine/src/framework/Actor.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index e133f40..a8e1a44 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,6 +7,12 @@ project(LightYears set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_EXTENSIONS OFf) +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + +if(APPLE) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-command-line-argument") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-command-line-argument") +endif() include(FetchContent) set(SFML_LIB_NAME SFML) @@ -24,4 +30,4 @@ set(LIGHT_YEARS_ENGINE_TARGET_NAME LightYearsEngine) set(LIGHT_YEARS_GAME_TARGET_NAME LightYearsGame) add_subdirectory(LightYearsEngine) -add_subdirectory(LightYearsGame) \ No newline at end of file +add_subdirectory(LightYearsGame) diff --git a/LightYearsEngine/CMakeLists.txt b/LightYearsEngine/CMakeLists.txt index 8eac05d..45c41a5 100644 --- a/LightYearsEngine/CMakeLists.txt +++ b/LightYearsEngine/CMakeLists.txt @@ -10,6 +10,9 @@ add_library(${LIGHT_YEARS_ENGINE_TARGET_NAME} STATIC ${CMAKE_CURRENT_SOURCE_DIR}/include/framework/World.h ${CMAKE_CURRENT_SOURCE_DIR}/src/framework/World.cpp + + ${CMAKE_CURRENT_SOURCE_DIR}/include/framework/Actor.h + ${CMAKE_CURRENT_SOURCE_DIR}/src/framework/Actor.cpp ) target_include_directories(${LIGHT_YEARS_ENGINE_TARGET_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include) diff --git a/LightYearsEngine/include/framework/Actor.h b/LightYearsEngine/include/framework/Actor.h new file mode 100644 index 0000000..936c2b8 --- /dev/null +++ b/LightYearsEngine/include/framework/Actor.h @@ -0,0 +1,20 @@ +#pragma once + +class World; + +namespace ly { + +class Actor { +public: + Actor(World *owningWorld); + + void BeginPlayInternal(); + virtual void BeginPlay(); + virtual void Tick(float deltaTime); + +private: + World *mOwningWorld; + bool mHasBeganPlay; +}; + +} // namespace ly diff --git a/LightYearsEngine/include/framework/Application.h b/LightYearsEngine/include/framework/Application.h index dd666c6..f955833 100644 --- a/LightYearsEngine/include/framework/Application.h +++ b/LightYearsEngine/include/framework/Application.h @@ -1,34 +1,38 @@ #pragma once -#include +#include "World.h" #include "framework/Core.h" +#include namespace ly { - class World; - class Application { - public: - Application(); - void Run(); +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(); + template + weak LoadWorld(); - virtual void Render(); - virtual void Tick(float deltaTime); +private: + void TickInternal(float deltaTime); + void RenderInternal(); - sf::RenderWindow mWindow; - float mTargetFrameRate; - sf::Clock mTickClock; + virtual void Render(); + virtual void Tick(float deltaTime); - shared currentWorld; - }; + sf::RenderWindow mWindow; + float mTargetFrameRate; + sf::Clock mTickClock; -} \ No newline at end of file + shared currentWorld; +}; + +template +inline weak Application::LoadWorld() { + shared newWorld{new WorldType{this}}; + currentWorld = newWorld; + currentWorld->BeginPlayInternal(); + return newWorld; +} + +} // namespace ly diff --git a/LightYearsEngine/include/framework/World.h b/LightYearsEngine/include/framework/World.h index 5934b4e..41b0911 100644 --- a/LightYearsEngine/include/framework/World.h +++ b/LightYearsEngine/include/framework/World.h @@ -1,21 +1,33 @@ #pragma once +#include "framework/Core.h" +class Application; +class Actor; + namespace ly { - class Application; - class World { - public: - World(Application *owningApp); +class World { +public: + World(Application *owningApp); - void BeginPlayInternal(); - void TickInternal(float deltaTime); + void BeginPlayInternal(); + void TickInternal(float deltaTime); - virtual ~World(); + virtual ~World(); - private: - void BeginPlay(); - void Tick(float deltaTime); - Application* mOwningApp; - bool mBeganPlay; - }; -} \ No newline at end of file + template + weak SpawnActor() { + shared newActor{new ActorType{this}}; + mPendingActors.push_back(newActor); + return newActor; + } + +private: + void BeginPlay(); + void Tick(float deltaTime); + Application *mOwningApp; + bool mBeganPlay; + List> mActors; + List> mPendingActors; +}; +} // namespace ly diff --git a/LightYearsEngine/src/framework/Actor.cpp b/LightYearsEngine/src/framework/Actor.cpp new file mode 100644 index 0000000..366e2c8 --- /dev/null +++ b/LightYearsEngine/src/framework/Actor.cpp @@ -0,0 +1,22 @@ +#include "framework/Actor.h" +#include "framework/Core.h" + +namespace ly { +Actor::Actor(World *owningWorld) + : mOwningWorld{owningWorld}, mHasBeganPlay{false} {} + +void Actor::BeginPlayInternal() { + + if (!mHasBeganPlay) { + mHasBeganPlay = true; + BeginPlay(); + } +} + +void Actor::BeginPlay() { + LOG("Actor begin play"); +} +void Actor::Tick(float deltaTime) { + LOG("Actor tick"); +} +} // namespace ly diff --git a/LightYearsEngine/src/framework/Application.cpp b/LightYearsEngine/src/framework/Application.cpp index e77db6c..e5e6712 100644 --- a/LightYearsEngine/src/framework/Application.cpp +++ b/LightYearsEngine/src/framework/Application.cpp @@ -3,63 +3,53 @@ #include "framework/World.h" namespace ly { - Application::Application(): - mWindow{sf::VideoMode(1440,1024), "LightYears"}, - mTargetFrameRate{60.0f}, - mTickClock{}, - currentWorld{nullptr} - { - } +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::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::TickInternal(float deltaTime) { + Tick(deltaTime); + if (currentWorld) { + currentWorld->TickInternal(deltaTime); + } +} - void Application::Tick(float deltaTime) - { - } +void Application::Tick(float deltaTime) {} - void Application::RenderInternal() - { - mWindow.clear(); +void Application::RenderInternal() { + mWindow.clear(); - Render(); + Render(); - mWindow.display(); - } + 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 +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.f, mWindow.getSize().y / 2.f); + mWindow.draw(rect); +} +} // namespace ly