Finished lecture 82
Signed-off-by: Daniel Henry <iamdanhenry@gmail.com>
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
20
LightYearsEngine/include/framework/Actor.h
Normal file
20
LightYearsEngine/include/framework/Actor.h
Normal 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
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
22
LightYearsEngine/src/framework/Actor.cpp
Normal file
22
LightYearsEngine/src/framework/Actor.cpp
Normal 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
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user