Finish lecture 82: Implement the World class
Signed-off-by: Daniel Henry <iamdanhenry@gmail.com>
This commit is contained in:
36
LightYearsEngine/CMakeLists.txt
Normal file
36
LightYearsEngine/CMakeLists.txt
Normal file
@@ -0,0 +1,36 @@
|
||||
add_library(${LIGHT_YEARS_ENGINE_TARGET_NAME} STATIC
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/include/framework/Application.h
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/framework/Application.cpp
|
||||
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/include/EntryPoint.h
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/EntryPoint.cpp
|
||||
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/include/framework/Core.h
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/framework/Core.cpp
|
||||
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/include/framework/World.h
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/framework/World.cpp
|
||||
)
|
||||
|
||||
target_include_directories(${LIGHT_YEARS_ENGINE_TARGET_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
|
||||
|
||||
target_link_libraries(${LIGHT_YEARS_ENGINE_TARGET_NAME} PUBLIC sfml-graphics)
|
||||
target_link_libraries(${LIGHT_YEARS_ENGINE_TARGET_NAME} PUBLIC sfml-window)
|
||||
target_link_libraries(${LIGHT_YEARS_ENGINE_TARGET_NAME} PUBLIC sfml-system)
|
||||
target_link_libraries(${LIGHT_YEARS_ENGINE_TARGET_NAME} PUBLIC sfml-audio)
|
||||
|
||||
function(CopyLibToTarget LIB_NAME TARGET_NAME)
|
||||
add_custom_command(
|
||||
TARGET ${TARGET_NAME}
|
||||
POST_BUILD
|
||||
COMMAND
|
||||
${CMAKE_COMMAND} -E copy_if_different
|
||||
$<TARGET_FILE:${LIB_NAME}>
|
||||
$<TARGET_FILE_DIR:${TARGET_NAME}>
|
||||
)
|
||||
endfunction()
|
||||
|
||||
CopyLibToTarget(sfml-graphics ${LIGHT_YEARS_ENGINE_TARGET_NAME})
|
||||
CopyLibToTarget(sfml-window ${LIGHT_YEARS_ENGINE_TARGET_NAME})
|
||||
CopyLibToTarget(sfml-system ${LIGHT_YEARS_ENGINE_TARGET_NAME})
|
||||
CopyLibToTarget(sfml-audio ${LIGHT_YEARS_ENGINE_TARGET_NAME})
|
||||
7
LightYearsEngine/include/EntryPoint.h
Normal file
7
LightYearsEngine/include/EntryPoint.h
Normal file
@@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
namespace ly {
|
||||
class Application;
|
||||
}
|
||||
|
||||
extern ly::Application* GetApplication();
|
||||
34
LightYearsEngine/include/framework/Application.h
Normal file
34
LightYearsEngine/include/framework/Application.h
Normal file
@@ -0,0 +1,34 @@
|
||||
#pragma once
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include "framework/Core.h"
|
||||
|
||||
namespace ly {
|
||||
|
||||
class World;
|
||||
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();
|
||||
|
||||
virtual void Render();
|
||||
virtual void Tick(float deltaTime);
|
||||
|
||||
sf::RenderWindow mWindow;
|
||||
float mTargetFrameRate;
|
||||
sf::Clock mTickClock;
|
||||
|
||||
shared<World> currentWorld;
|
||||
};
|
||||
|
||||
}
|
||||
30
LightYearsEngine/include/framework/Core.h
Normal file
30
LightYearsEngine/include/framework/Core.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
#include <stdio.h>
|
||||
#include <memory>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace ly {
|
||||
|
||||
template<typename T>
|
||||
using unique = std::unique_ptr<T>;
|
||||
|
||||
template<typename T>
|
||||
using shared = std::shared_ptr<T>;
|
||||
|
||||
template<typename T>
|
||||
using weak = std::weak_ptr<T>;
|
||||
|
||||
template<typename T>
|
||||
using List = std::vector<T>;
|
||||
|
||||
template<typename keyType, typename valueType, typename Pr = std::less<keyType>>
|
||||
using Map = std::map<keyType, valueType, Pr>;
|
||||
|
||||
template<typename keyType, typename valueType, typename Hasher = std::hash<keyType>>
|
||||
using Dictionary = std::map<keyType, valueType, Hasher>;
|
||||
|
||||
// Logging Macro
|
||||
#define LOG(M, ...) printf(M "\n", ##__VA_ARGS__)
|
||||
}
|
||||
21
LightYearsEngine/include/framework/World.h
Normal file
21
LightYearsEngine/include/framework/World.h
Normal file
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
namespace ly {
|
||||
class Application;
|
||||
|
||||
class World {
|
||||
public:
|
||||
World(Application *owningApp);
|
||||
|
||||
void BeginPlayInternal();
|
||||
void TickInternal(float deltaTime);
|
||||
|
||||
virtual ~World();
|
||||
|
||||
private:
|
||||
void BeginPlay();
|
||||
void Tick(float deltaTime);
|
||||
Application* mOwningApp;
|
||||
bool mBeganPlay;
|
||||
};
|
||||
}
|
||||
8
LightYearsEngine/src/EntryPoint.cpp
Normal file
8
LightYearsEngine/src/EntryPoint.cpp
Normal file
@@ -0,0 +1,8 @@
|
||||
#include "EntryPoint.h"
|
||||
#include "framework/Application.h"
|
||||
|
||||
int main() {
|
||||
ly::Application* app = GetApplication();
|
||||
app->Run();
|
||||
delete app;
|
||||
}
|
||||
65
LightYearsEngine/src/framework/Application.cpp
Normal file
65
LightYearsEngine/src/framework/Application.cpp
Normal file
@@ -0,0 +1,65 @@
|
||||
#include "framework/Application.h"
|
||||
#include "framework/Core.h"
|
||||
#include "framework/World.h"
|
||||
|
||||
namespace ly {
|
||||
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);
|
||||
}
|
||||
}
|
||||
0
LightYearsEngine/src/framework/Core.cpp
Normal file
0
LightYearsEngine/src/framework/Core.cpp
Normal file
36
LightYearsEngine/src/framework/World.cpp
Normal file
36
LightYearsEngine/src/framework/World.cpp
Normal file
@@ -0,0 +1,36 @@
|
||||
#include "framework/World.h"
|
||||
#include "framework/Core.h"
|
||||
|
||||
namespace ly {
|
||||
World::World(Application* owningApp) : mOwningApp{ owningApp }, mBeganPlay{ false } {
|
||||
|
||||
}
|
||||
|
||||
void World::BeginPlayInternal() {
|
||||
if (!mBeganPlay) {
|
||||
mBeganPlay = true;
|
||||
BeginPlay();
|
||||
}
|
||||
}
|
||||
|
||||
World::~World()
|
||||
{
|
||||
}
|
||||
|
||||
void World::TickInternal(float deltaTime)
|
||||
{
|
||||
Tick(deltaTime);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void World::BeginPlay()
|
||||
{
|
||||
LOG("Began Play");
|
||||
}
|
||||
|
||||
void World::Tick(float deltaTime)
|
||||
{
|
||||
LOG("Ticking at frame rate: %f", 1.f / deltaTime);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user