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 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)
|
||||||
|
|||||||
@@ -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)
|
||||||
|
|||||||
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
|
#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;
|
|
||||||
currentWorld->BeginPlayInternal();
|
|
||||||
return newWorld;
|
|
||||||
}
|
|
||||||
private:
|
|
||||||
void TickInternal(float deltaTime);
|
|
||||||
void RenderInternal();
|
|
||||||
|
|
||||||
virtual void Render();
|
private:
|
||||||
virtual void Tick(float deltaTime);
|
void TickInternal(float deltaTime);
|
||||||
|
void RenderInternal();
|
||||||
|
|
||||||
sf::RenderWindow mWindow;
|
virtual void Render();
|
||||||
float mTargetFrameRate;
|
virtual void Tick(float deltaTime);
|
||||||
sf::Clock mTickClock;
|
|
||||||
|
|
||||||
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
|
#pragma once
|
||||||
|
|
||||||
|
#include "framework/Core.h"
|
||||||
|
class Application;
|
||||||
|
class Actor;
|
||||||
|
|
||||||
namespace ly {
|
namespace ly {
|
||||||
class Application;
|
|
||||||
|
|
||||||
class World {
|
class World {
|
||||||
public:
|
public:
|
||||||
World(Application *owningApp);
|
World(Application *owningApp);
|
||||||
|
|
||||||
void BeginPlayInternal();
|
void BeginPlayInternal();
|
||||||
void TickInternal(float deltaTime);
|
void TickInternal(float deltaTime);
|
||||||
|
|
||||||
virtual ~World();
|
virtual ~World();
|
||||||
|
|
||||||
private:
|
template <typename ActorType>
|
||||||
void BeginPlay();
|
weak<ActorType> SpawnActor() {
|
||||||
void Tick(float deltaTime);
|
shared<ActorType> newActor{new ActorType{this}};
|
||||||
Application* mOwningApp;
|
mPendingActors.push_back(newActor);
|
||||||
bool mBeganPlay;
|
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"
|
#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;
|
while (mWindow.isOpen()) {
|
||||||
while (mWindow.isOpen()) {
|
sf::Event windowEvent;
|
||||||
sf::Event windowEvent;
|
while (mWindow.pollEvent(windowEvent)) {
|
||||||
while (mWindow.pollEvent(windowEvent)) {
|
if (windowEvent.type == sf::Event::EventType::Closed) {
|
||||||
if (windowEvent.type == sf::Event::EventType::Closed) {
|
mWindow.close();
|
||||||
mWindow.close();
|
}
|
||||||
}
|
}
|
||||||
}
|
float frameDeltaTime = mTickClock.restart().asSeconds();
|
||||||
float frameDeltaTime = mTickClock.restart().asSeconds();
|
accumulatedTime += frameDeltaTime;
|
||||||
accumulatedTime += frameDeltaTime;
|
while (accumulatedTime > targetDeltaTime) {
|
||||||
while (accumulatedTime > targetDeltaTime) {
|
accumulatedTime -= targetDeltaTime;
|
||||||
accumulatedTime -= targetDeltaTime;
|
TickInternal(targetDeltaTime);
|
||||||
TickInternal(targetDeltaTime);
|
RenderInternal();
|
||||||
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::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