Finished lecture 82

Signed-off-by: Daniel Henry <iamdanhenry@gmail.com>
This commit is contained in:
2025-09-02 09:32:30 -05:00
parent 7d2cd86fd4
commit c545d800d2
7 changed files with 149 additions and 92 deletions

View File

@@ -7,6 +7,12 @@ project(LightYears
set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFf) 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) include(FetchContent)
set(SFML_LIB_NAME SFML) set(SFML_LIB_NAME SFML)

View File

@@ -10,6 +10,9 @@ add_library(${LIGHT_YEARS_ENGINE_TARGET_NAME} STATIC
${CMAKE_CURRENT_SOURCE_DIR}/include/framework/World.h ${CMAKE_CURRENT_SOURCE_DIR}/include/framework/World.h
${CMAKE_CURRENT_SOURCE_DIR}/src/framework/World.cpp ${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) target_include_directories(${LIGHT_YEARS_ENGINE_TARGET_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)

View File

@@ -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

View File

@@ -1,23 +1,19 @@
#pragma once #pragma once
#include <SFML/Graphics.hpp> #include "World.h"
#include "framework/Core.h" #include "framework/Core.h"
#include <SFML/Graphics.hpp>
namespace ly { namespace ly {
class World; class Application {
class Application { public:
public:
Application(); Application();
void Run(); void Run();
template<typename WorldType> template <typename WorldType>
weak<World> LoadWorld() { weak<World> LoadWorld();
shared<WorldType> newWorld{ new WorldType{this} };
currentWorld = newWorld; private:
currentWorld->BeginPlayInternal();
return newWorld;
}
private:
void TickInternal(float deltaTime); void TickInternal(float deltaTime);
void RenderInternal(); void RenderInternal();
@@ -29,6 +25,14 @@ namespace ly {
sf::Clock mTickClock; sf::Clock mTickClock;
shared<World> currentWorld; shared<World> currentWorld;
}; };
template <typename WorldType>
inline weak<World> Application::LoadWorld() {
shared<WorldType> newWorld{new WorldType{this}};
currentWorld = newWorld;
currentWorld->BeginPlayInternal();
return newWorld;
} }
} // namespace ly

View File

@@ -1,10 +1,13 @@
#pragma once #pragma once
namespace ly { #include "framework/Core.h"
class Application; class Application;
class Actor;
class World { namespace ly {
public:
class World {
public:
World(Application *owningApp); World(Application *owningApp);
void BeginPlayInternal(); void BeginPlayInternal();
@@ -12,10 +15,19 @@ namespace ly {
virtual ~World(); virtual ~World();
private: template <typename ActorType>
weak<ActorType> SpawnActor() {
shared<ActorType> newActor{new ActorType{this}};
mPendingActors.push_back(newActor);
return newActor;
}
private:
void BeginPlay(); void BeginPlay();
void Tick(float deltaTime); void Tick(float deltaTime);
Application* mOwningApp; Application *mOwningApp;
bool mBeganPlay; bool mBeganPlay;
}; List<shared<Actor>> mActors;
} List<shared<Actor>> mPendingActors;
};
} // namespace ly

View File

@@ -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

View File

@@ -3,16 +3,11 @@
#include "framework/World.h" #include "framework/World.h"
namespace ly { namespace ly {
Application::Application(): Application::Application()
mWindow{sf::VideoMode(1440,1024), "LightYears"}, : mWindow{sf::VideoMode(1440, 1024), "LightYears"}, mTargetFrameRate{60.0f},
mTargetFrameRate{60.0f}, mTickClock{}, currentWorld{nullptr} {}
mTickClock{},
currentWorld{nullptr}
{
}
void Application::Run() void Application::Run() {
{
mTickClock.restart(); mTickClock.restart();
float accumulatedTime = 0.0f; float accumulatedTime = 0.0f;
float targetDeltaTime = 1.f / mTargetFrameRate; float targetDeltaTime = 1.f / mTargetFrameRate;
@@ -31,35 +26,30 @@ namespace ly {
RenderInternal(); RenderInternal();
} }
} }
} }
void Application::TickInternal(float deltaTime) void Application::TickInternal(float deltaTime) {
{
Tick(deltaTime); Tick(deltaTime);
if (currentWorld) { if (currentWorld) {
currentWorld->TickInternal(deltaTime); currentWorld->TickInternal(deltaTime);
} }
} }
void Application::Tick(float deltaTime) void Application::Tick(float deltaTime) {}
{
}
void Application::RenderInternal() void Application::RenderInternal() {
{
mWindow.clear(); mWindow.clear();
Render(); Render();
mWindow.display(); mWindow.display();
} }
void Application::Render() void Application::Render() {
{ sf::RectangleShape rect{sf::Vector2f{100, 100}};
sf::RectangleShape rect{ sf::Vector2f{100,100} };
rect.setFillColor(sf::Color::Green); rect.setFillColor(sf::Color::Green);
rect.setOrigin(50, 50); rect.setOrigin(50, 50);
rect.setPosition(mWindow.getSize().x / 2, mWindow.getSize().y / 2); rect.setPosition(mWindow.getSize().x / 2.f, mWindow.getSize().y / 2.f);
mWindow.draw(rect); mWindow.draw(rect);
}
} }
} // namespace ly