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_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)

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}/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)

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,34 +1,38 @@
#pragma once
#include <SFML/Graphics.hpp>
#include "World.h"
#include "framework/Core.h"
#include <SFML/Graphics.hpp>
namespace ly {
class World;
class Application {
public:
Application();
void Run();
class Application {
public:
Application();
void Run();
template<typename WorldType>
weak<World> LoadWorld() {
shared<WorldType> newWorld{ new WorldType{this} };
currentWorld = newWorld;
currentWorld->BeginPlayInternal();
return newWorld;
}
private:
void TickInternal(float deltaTime);
void RenderInternal();
template <typename WorldType>
weak<World> 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<World> currentWorld;
};
sf::RenderWindow mWindow;
float mTargetFrameRate;
sf::Clock mTickClock;
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,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;
};
}
template <typename ActorType>
weak<ActorType> SpawnActor() {
shared<ActorType> newActor{new ActorType{this}};
mPendingActors.push_back(newActor);
return newActor;
}
private:
void BeginPlay();
void Tick(float deltaTime);
Application *mOwningApp;
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,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::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);
}
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.f, mWindow.getSize().y / 2.f);
mWindow.draw(rect);
}
} // namespace ly